attribution_processing/
kernel_statistics.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use {fidl_fuchsia_kernel as fkernel, fidl_fuchsia_memory_attribution_plugin as fplugin};
6
7#[derive(Default, Clone)]
8pub struct KernelStatistics {
9    pub memory_statistics: fkernel::MemoryStats,
10    pub compression_statistics: fkernel::MemoryStatsCompression,
11}
12
13impl From<fplugin::KernelStatistics> for KernelStatistics {
14    fn from(value: fplugin::KernelStatistics) -> KernelStatistics {
15        KernelStatistics {
16            memory_statistics: value.memory_stats.unwrap(),
17            compression_statistics: value.compression_stats.unwrap(),
18        }
19    }
20}
21
22impl Into<fplugin::KernelStatistics> for KernelStatistics {
23    fn into(self) -> fplugin::KernelStatistics {
24        fplugin::KernelStatistics {
25            memory_stats: Some(self.memory_statistics),
26            compression_stats: Some(self.compression_statistics),
27            ..Default::default()
28        }
29    }
30}
31
32#[cfg(test)]
33mod test {
34    use super::*;
35
36    #[test]
37    fn test_convert() {
38        let fplugin_kernel_statistics = fplugin::KernelStatistics {
39            memory_stats: Some(fidl_fuchsia_kernel::MemoryStats {
40                total_bytes: Some(1),
41                free_bytes: Some(2),
42                free_loaned_bytes: Some(3),
43                wired_bytes: Some(4),
44                total_heap_bytes: Some(5),
45                free_heap_bytes: Some(6),
46                vmo_bytes: Some(7),
47                mmu_overhead_bytes: Some(8),
48                ipc_bytes: Some(9),
49                cache_bytes: Some(10),
50                slab_bytes: Some(11),
51                zram_bytes: Some(12),
52                other_bytes: Some(13),
53                vmo_reclaim_total_bytes: Some(14),
54                vmo_reclaim_newest_bytes: Some(15),
55                vmo_reclaim_oldest_bytes: Some(16),
56                vmo_reclaim_disabled_bytes: Some(17),
57                vmo_discardable_locked_bytes: Some(18),
58                vmo_discardable_unlocked_bytes: Some(19),
59                ..Default::default()
60            }),
61            compression_stats: Some(fidl_fuchsia_kernel::MemoryStatsCompression {
62                uncompressed_storage_bytes: Some(15),
63                compressed_storage_bytes: Some(16),
64                compressed_fragmentation_bytes: Some(17),
65                compression_time: Some(18),
66                decompression_time: Some(19),
67                total_page_compression_attempts: Some(20),
68                failed_page_compression_attempts: Some(21),
69                total_page_decompressions: Some(22),
70                compressed_page_evictions: Some(23),
71                eager_page_compressions: Some(24),
72                memory_pressure_page_compressions: Some(25),
73                critical_memory_page_compressions: Some(26),
74                pages_decompressed_unit_ns: Some(27),
75                pages_decompressed_within_log_time: Some([0, 1, 2, 3, 4, 5, 6, 7]),
76                ..Default::default()
77            }),
78            ..Default::default()
79        };
80
81        let kernel_statistics: KernelStatistics = fplugin_kernel_statistics.clone().into();
82
83        assert_eq!(kernel_statistics.memory_statistics.total_bytes, Some(1));
84        assert_eq!(kernel_statistics.memory_statistics.free_bytes, Some(2));
85
86        assert_eq!(kernel_statistics.compression_statistics.uncompressed_storage_bytes, Some(15));
87
88        assert_eq!(fplugin_kernel_statistics, kernel_statistics.into());
89    }
90}