builtins/
kernel_stats.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::Error;
use fidl_fuchsia_kernel as fkernel;
use fuchsia_async::DurationExt as _;
use futures::prelude::*;
use std::sync::Arc;
use zx::{self as zx, Resource};

/// An implementation of the `fuchsia.kernel.Stats` protocol.
pub struct KernelStats {
    resource: Resource,
}

impl KernelStats {
    /// `resource` must be the info resource.
    pub fn new(resource: Resource) -> Arc<Self> {
        Arc::new(Self { resource })
    }

    pub async fn serve(
        self: Arc<Self>,
        mut stream: fkernel::StatsRequestStream,
    ) -> Result<(), Error> {
        while let Some(stats_request) = stream.try_next().await? {
            match stats_request {
                fkernel::StatsRequest::GetMemoryStats { responder } => {
                    let mem_stats = &self.resource.mem_stats()?;
                    let stats = fkernel::MemoryStats {
                        total_bytes: Some(mem_stats.total_bytes),
                        free_bytes: Some(mem_stats.free_bytes),
                        wired_bytes: Some(mem_stats.wired_bytes),
                        total_heap_bytes: Some(mem_stats.total_heap_bytes),
                        free_heap_bytes: Some(mem_stats.free_heap_bytes),
                        vmo_bytes: Some(mem_stats.vmo_bytes),
                        mmu_overhead_bytes: Some(mem_stats.mmu_overhead_bytes),
                        ipc_bytes: Some(mem_stats.ipc_bytes),
                        other_bytes: Some(mem_stats.other_bytes),
                        free_loaned_bytes: Some(mem_stats.free_loaned_bytes),
                        cache_bytes: Some(mem_stats.cache_bytes),
                        slab_bytes: Some(mem_stats.slab_bytes),
                        zram_bytes: Some(mem_stats.zram_bytes),
                        vmo_reclaim_total_bytes: Some(mem_stats.vmo_reclaim_total_bytes),
                        vmo_reclaim_newest_bytes: Some(mem_stats.vmo_reclaim_newest_bytes),
                        vmo_reclaim_oldest_bytes: Some(mem_stats.vmo_reclaim_oldest_bytes),
                        vmo_reclaim_disabled_bytes: Some(mem_stats.vmo_reclaim_disabled_bytes),
                        vmo_discardable_locked_bytes: Some(mem_stats.vmo_discardable_locked_bytes),
                        vmo_discardable_unlocked_bytes: Some(
                            mem_stats.vmo_discardable_unlocked_bytes,
                        ),
                        ..Default::default()
                    };
                    responder.send(&stats)?;
                }
                fkernel::StatsRequest::GetMemoryStatsExtended { responder } => {
                    let mem_stats_extended = &self.resource.mem_stats_extended()?;
                    let stats = fkernel::MemoryStatsExtended {
                        total_bytes: Some(mem_stats_extended.total_bytes),
                        free_bytes: Some(mem_stats_extended.free_bytes),
                        wired_bytes: Some(mem_stats_extended.wired_bytes),
                        total_heap_bytes: Some(mem_stats_extended.total_heap_bytes),
                        free_heap_bytes: Some(mem_stats_extended.free_heap_bytes),
                        vmo_bytes: Some(mem_stats_extended.vmo_bytes),
                        vmo_pager_total_bytes: Some(mem_stats_extended.vmo_pager_total_bytes),
                        vmo_pager_newest_bytes: Some(mem_stats_extended.vmo_pager_newest_bytes),
                        vmo_pager_oldest_bytes: Some(mem_stats_extended.vmo_pager_oldest_bytes),
                        vmo_discardable_locked_bytes: Some(
                            mem_stats_extended.vmo_discardable_locked_bytes,
                        ),
                        vmo_discardable_unlocked_bytes: Some(
                            mem_stats_extended.vmo_discardable_unlocked_bytes,
                        ),
                        mmu_overhead_bytes: Some(mem_stats_extended.mmu_overhead_bytes),
                        ipc_bytes: Some(mem_stats_extended.ipc_bytes),
                        other_bytes: Some(mem_stats_extended.other_bytes),
                        ..Default::default()
                    };
                    responder.send(&stats)?;
                }
                fkernel::StatsRequest::GetMemoryStatsCompression { responder } => {
                    let mem_stats_compression = &self.resource.mem_stats_compression()?;
                    let stats = fkernel::MemoryStatsCompression {
                        uncompressed_storage_bytes: Some(
                            mem_stats_compression.uncompressed_storage_bytes,
                        ),
                        compressed_storage_bytes: Some(
                            mem_stats_compression.compressed_storage_bytes,
                        ),
                        compressed_fragmentation_bytes: Some(
                            mem_stats_compression.compressed_fragmentation_bytes,
                        ),
                        compression_time: Some(mem_stats_compression.compression_time),
                        decompression_time: Some(mem_stats_compression.decompression_time),
                        total_page_compression_attempts: Some(
                            mem_stats_compression.total_page_compression_attempts,
                        ),
                        failed_page_compression_attempts: Some(
                            mem_stats_compression.failed_page_compression_attempts,
                        ),
                        total_page_decompressions: Some(
                            mem_stats_compression.total_page_decompressions,
                        ),
                        compressed_page_evictions: Some(
                            mem_stats_compression.compressed_page_evictions,
                        ),
                        eager_page_compressions: Some(
                            mem_stats_compression.eager_page_compressions,
                        ),
                        memory_pressure_page_compressions: Some(
                            mem_stats_compression.memory_pressure_page_compressions,
                        ),
                        critical_memory_page_compressions: Some(
                            mem_stats_compression.critical_memory_page_compressions,
                        ),
                        pages_decompressed_unit_ns: Some(
                            mem_stats_compression.pages_decompressed_unit_ns,
                        ),
                        pages_decompressed_within_log_time: Some(
                            mem_stats_compression.pages_decompressed_within_log_time,
                        ),
                        ..Default::default()
                    };
                    responder.send(&stats)?;
                }
                fkernel::StatsRequest::GetCpuStats { responder } => {
                    let cpu_stats = &self.resource.cpu_stats()?;
                    let mut per_cpu_stats: Vec<fkernel::PerCpuStats> =
                        Vec::with_capacity(cpu_stats.len());
                    for cpu_stat in cpu_stats.iter() {
                        per_cpu_stats.push(fkernel::PerCpuStats {
                            cpu_number: Some(cpu_stat.cpu_number),
                            flags: Some(cpu_stat.flags),
                            idle_time: Some(cpu_stat.idle_time),
                            reschedules: Some(cpu_stat.reschedules),
                            context_switches: Some(cpu_stat.context_switches),
                            irq_preempts: Some(cpu_stat.irq_preempts),
                            yields: Some(cpu_stat.yields),
                            ints: Some(cpu_stat.ints),
                            timer_ints: Some(cpu_stat.timer_ints),
                            timers: Some(cpu_stat.timers),
                            page_faults: Some(cpu_stat.page_faults),
                            exceptions: Some(cpu_stat.exceptions),
                            syscalls: Some(cpu_stat.syscalls),
                            reschedule_ipis: Some(cpu_stat.reschedule_ipis),
                            generic_ipis: Some(cpu_stat.generic_ipis),
                            ..Default::default()
                        });
                    }
                    let stats = fkernel::CpuStats {
                        actual_num_cpus: per_cpu_stats.len() as u64,
                        per_cpu_stats: Some(per_cpu_stats),
                    };
                    responder.send(&stats)?;
                }
                fkernel::StatsRequest::GetCpuLoad { duration, responder } => {
                    if duration <= 0 {
                        return Err(anyhow::anyhow!("Duration must be greater than 0"));
                    }

                    // Record `start_time` before the first stats query, and `end_time` *after* the
                    // second stats query completes. This ensures the "total time" (`end_time` -
                    // `start_time`) will never be less than the duration spanned by `start_stats`
                    // to `end_stats`, which would be invalid.
                    let start_time = fuchsia_async::MonotonicInstant::now();
                    let start_stats = self.resource.cpu_stats()?;
                    fuchsia_async::Timer::new(
                        zx::MonotonicDuration::from_nanos(duration).after_now(),
                    )
                    .await;
                    let end_stats = self.resource.cpu_stats()?;
                    let end_time = fuchsia_async::MonotonicInstant::now();

                    let loads = calculate_cpu_loads(start_time, start_stats, end_time, end_stats);
                    responder.send(&loads)?;
                }
            }
        }
        Ok(())
    }
}

/// Uses start / end times and corresponding PerCpuStats to calculate and return a vector of per-CPU
/// load values as floats in the range 0.0 - 100.0.
fn calculate_cpu_loads(
    start_time: fuchsia_async::MonotonicInstant,
    start_stats: Vec<zx::PerCpuStats>,
    end_time: fuchsia_async::MonotonicInstant,
    end_stats: Vec<zx::PerCpuStats>,
) -> Vec<f32> {
    let elapsed_time = (end_time - start_time).into_nanos();
    start_stats
        .iter()
        .zip(end_stats.iter())
        .map(|(start, end)| {
            let busy_time = elapsed_time - (end.idle_time - start.idle_time);
            let load_pct = busy_time as f64 / elapsed_time as f64 * 100.0;
            load_pct as f32
        })
        .collect::<Vec<f32>>()
}

#[cfg(test)]
mod tests {
    use super::*;
    use fuchsia_component::client::connect_to_protocol;
    use {fidl_fuchsia_kernel as fkernel, fuchsia_async as fasync};

    async fn get_info_resource() -> Result<Resource, Error> {
        let info_resource_provider = connect_to_protocol::<fkernel::InfoResourceMarker>()?;
        let info_resource_handle = info_resource_provider.get().await?;
        Ok(Resource::from(info_resource_handle))
    }

    enum OnError {
        Panic,
        Ignore,
    }

    async fn serve_kernel_stats(on_error: OnError) -> Result<fkernel::StatsProxy, Error> {
        let info_resource = get_info_resource().await?;

        let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<fkernel::StatsMarker>();
        fasync::Task::local(KernelStats::new(info_resource).serve(stream).unwrap_or_else(
            move |e| match on_error {
                OnError::Panic => panic!("Error while serving kernel stats: {}", e),
                _ => {}
            },
        ))
        .detach();
        Ok(proxy)
    }

    #[fuchsia::test]
    async fn get_mem_stats() -> Result<(), Error> {
        let kernel_stats_provider = serve_kernel_stats(OnError::Panic).await?;
        let mem_stats = kernel_stats_provider.get_memory_stats().await?;

        assert!(mem_stats.total_bytes.unwrap() > 0);
        assert!(mem_stats.total_heap_bytes.unwrap() > 0);
        assert!(mem_stats.slab_bytes.unwrap() > 0);
        Ok(())
    }

    #[fuchsia::test]
    async fn get_mem_stats_extended() -> Result<(), Error> {
        let kernel_stats_provider = serve_kernel_stats(OnError::Panic).await?;
        let mem_stats_extended = kernel_stats_provider.get_memory_stats_extended().await?;

        assert!(mem_stats_extended.total_bytes.unwrap() > 0);
        assert!(mem_stats_extended.total_heap_bytes.unwrap() > 0);

        Ok(())
    }

    #[fuchsia::test]
    async fn get_cpu_stats() -> Result<(), Error> {
        let kernel_stats_provider = serve_kernel_stats(OnError::Panic).await?;
        let cpu_stats = kernel_stats_provider.get_cpu_stats().await?;
        let actual_num_cpus = cpu_stats.actual_num_cpus;
        assert!(actual_num_cpus > 0);
        let per_cpu_stats = cpu_stats.per_cpu_stats.unwrap();

        let mut idle_time_sum = 0;
        let mut syscalls_sum = 0;

        for per_cpu_stat in per_cpu_stats.iter() {
            idle_time_sum += per_cpu_stat.idle_time.unwrap();
            syscalls_sum += per_cpu_stat.syscalls.unwrap();
        }

        assert!(idle_time_sum > 0);
        assert!(syscalls_sum > 0);

        Ok(())
    }

    #[fuchsia::test]
    async fn get_cpu_load_invalid_duration() {
        let kernel_stats_provider = serve_kernel_stats(OnError::Ignore).await.unwrap();

        // The server should close the channel when it receives an invalid argument
        assert_matches::assert_matches!(
            kernel_stats_provider.get_cpu_load(0).await,
            Err(fidl::Error::ClientChannelClosed { .. })
        );
    }

    #[fuchsia::test]
    async fn get_cpu_load() -> Result<(), Error> {
        let kernel_stats_provider = serve_kernel_stats(OnError::Panic).await?;
        let cpu_loads = kernel_stats_provider
            .get_cpu_load(zx::MonotonicDuration::from_seconds(1).into_nanos())
            .await?;

        assert!(
            cpu_loads.iter().all(|l| l > &0.0 && l <= &100.0),
            "Invalid CPU load value (expected range 0.0 - 100.0, received {:?}",
            cpu_loads
        );

        Ok(())
    }

    // Takes a vector of CPU loads and generates the necessary parameters that can be fed into
    // `calculate_cpu_loads` to result in those load calculations.
    fn parameters_for_expected_cpu_loads(
        cpu_loads: Vec<f32>,
    ) -> (
        fuchsia_async::MonotonicInstant,
        Vec<zx::PerCpuStats>,
        fuchsia_async::MonotonicInstant,
        Vec<zx::PerCpuStats>,
    ) {
        let start_time = fuchsia_async::MonotonicInstant::from_nanos(0);
        let end_time = fuchsia_async::MonotonicInstant::from_nanos(1000000000);

        let (start_stats, end_stats) = std::iter::repeat(zx::PerCpuStats::default())
            .zip(cpu_loads.into_iter().map(|load| {
                let end_time_f32 = end_time.into_nanos() as f32;
                let idle_time = (end_time_f32 - (load / 100.0 * end_time_f32)) as i64;
                zx::PerCpuStats { idle_time, ..zx::PerCpuStats::default() }
            }))
            .unzip();

        (start_time, start_stats, end_time, end_stats)
    }

    #[fuchsia::test]
    fn test_calculate_cpu_loads() -> Result<(), Error> {
        // CPU0 loaded to 75%
        let (start_time, start_stats, end_time, end_stats) =
            parameters_for_expected_cpu_loads(vec![75.0, 0.0]);
        assert_eq!(
            calculate_cpu_loads(start_time, start_stats, end_time, end_stats),
            vec![75.0, 0.0]
        );

        // CPU1 loaded to 75%
        let (start_time, start_stats, end_time, end_stats) =
            parameters_for_expected_cpu_loads(vec![0.0, 75.0]);
        assert_eq!(
            calculate_cpu_loads(start_time, start_stats, end_time, end_stats),
            vec![0.0, 75.0]
        );

        Ok(())
    }
}