attribution_processing/
fplugin_serde.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 crate::fkernel_serde;
6use serde::{Deserialize, Serialize};
7use {fidl_fuchsia_kernel as fkernel, fidl_fuchsia_memory_attribution_plugin as fplugin};
8
9#[derive(Serialize, Deserialize)]
10#[serde(remote = "fidl_fuchsia_memory_attribution_plugin::PerformanceImpactMetrics")]
11pub struct PerformanceImpactMetricsDef {
12    pub some_memory_stalls_ns: Option<i64>,
13    pub full_memory_stalls_ns: Option<i64>,
14    #[doc(hidden)]
15    #[serde(skip)]
16    pub __source_breaking: fidl::marker::SourceBreaking,
17}
18
19// TODO(https://github.com/serde-rs/serde/issues/723): Use remote serialization
20// with fkernel::KernelStatistics when supported inside options.
21#[derive(Default, PartialEq, Debug, Clone, Serialize, Deserialize)]
22pub struct KernelStatistics {
23    #[serde(with = "fkernel_serde::MemoryStatsDef")]
24    pub memory_statistics: fkernel::MemoryStats,
25    #[serde(with = "fkernel_serde::MemoryStatsCompressionDef")]
26    pub compression_statistics: fkernel::MemoryStatsCompression,
27}
28
29impl From<fplugin::KernelStatistics> for KernelStatistics {
30    fn from(value: fplugin::KernelStatistics) -> KernelStatistics {
31        KernelStatistics {
32            memory_statistics: value.memory_stats.unwrap(),
33            compression_statistics: value.compression_stats.unwrap(),
34        }
35    }
36}
37
38impl Into<fplugin::KernelStatistics> for KernelStatistics {
39    fn into(self) -> fplugin::KernelStatistics {
40        fplugin::KernelStatistics {
41            memory_stats: Some(self.memory_statistics),
42            compression_stats: Some(self.compression_statistics),
43            ..Default::default()
44        }
45    }
46}
47
48#[cfg(test)]
49mod test {
50    use super::*;
51    use crate::fplugin;
52    #[test]
53    fn test_convert() {
54        let fplugin_kernel_statistics = fplugin::KernelStatistics {
55            memory_stats: Some(fidl_fuchsia_kernel::MemoryStats {
56                total_bytes: Some(1),
57                free_bytes: Some(2),
58                free_loaned_bytes: Some(3),
59                wired_bytes: Some(4),
60                total_heap_bytes: Some(5),
61                free_heap_bytes: Some(6),
62                vmo_bytes: Some(7),
63                mmu_overhead_bytes: Some(8),
64                ipc_bytes: Some(9),
65                cache_bytes: Some(10),
66                slab_bytes: Some(11),
67                zram_bytes: Some(12),
68                other_bytes: Some(13),
69                vmo_reclaim_total_bytes: Some(14),
70                vmo_reclaim_newest_bytes: Some(15),
71                vmo_reclaim_oldest_bytes: Some(16),
72                vmo_reclaim_disabled_bytes: Some(17),
73                vmo_discardable_locked_bytes: Some(18),
74                vmo_discardable_unlocked_bytes: Some(19),
75                ..Default::default()
76            }),
77            compression_stats: Some(fidl_fuchsia_kernel::MemoryStatsCompression {
78                uncompressed_storage_bytes: Some(15),
79                compressed_storage_bytes: Some(16),
80                compressed_fragmentation_bytes: Some(17),
81                compression_time: Some(18),
82                decompression_time: Some(19),
83                total_page_compression_attempts: Some(20),
84                failed_page_compression_attempts: Some(21),
85                total_page_decompressions: Some(22),
86                compressed_page_evictions: Some(23),
87                eager_page_compressions: Some(24),
88                memory_pressure_page_compressions: Some(25),
89                critical_memory_page_compressions: Some(26),
90                pages_decompressed_unit_ns: Some(27),
91                pages_decompressed_within_log_time: Some([0, 1, 2, 3, 4, 5, 6, 7]),
92                ..Default::default()
93            }),
94            ..Default::default()
95        };
96
97        let kernel_statistics: KernelStatistics = fplugin_kernel_statistics.clone().into();
98
99        assert_eq!(kernel_statistics.memory_statistics.total_bytes, Some(1));
100        assert_eq!(kernel_statistics.memory_statistics.free_bytes, Some(2));
101
102        assert_eq!(kernel_statistics.compression_statistics.uncompressed_storage_bytes, Some(15));
103
104        assert_eq!(fplugin_kernel_statistics, kernel_statistics.into());
105    }
106}