cgroups_rs/
stats.rs

1// Copyright (c) 2018 Levente Kurusa
2// Copyright (c) 2025 Ant Group
3//
4// SPDX-License-Identifier: Apache-2.0 or MIT
5//
6
7use std::collections::HashMap;
8
9#[derive(Debug, Default)]
10pub struct CgroupStats {
11    pub cpu: CpuCgroupStats,
12    pub memory: MemoryCgroupStats,
13    pub pids: PidsCgroupStats,
14    pub blkio: BlkioCgroupStats,
15    pub hugetlb: HugeTlbCgroupStats,
16}
17
18#[derive(Debug, Default)]
19pub struct CpuCgroupStats {
20    pub cpu_acct: Option<CpuAcctStats>,
21    pub cpu_throttling: Option<CpuThrottlingStats>,
22}
23
24#[derive(Debug, Default)]
25pub struct CpuAcctStats {
26    /// Usage in userspace, read from `cpuacct.stat` from the line starting
27    /// with `user`. Set 0 if no data.
28    pub user_usage: u64,
29    /// Usage in kernelspace, read from `cpuacct.stat` from the line
30    /// starting with `system`. Set 0 if no data.
31    pub system_usage: u64,
32    /// Total usage, read from `cpuacct.usage`. Set 0 if no data.
33    pub total_usage: u64,
34    /// Per-CPU usage, read from `cpuacct.usage_percpu`.
35    pub usage_percpu: Vec<u64>,
36}
37
38#[derive(Debug, Default)]
39pub struct CpuThrottlingStats {
40    /// Periods, read from `cpu.stat` from the line starting with
41    /// `nr_periods`. Set 0 if no data.
42    pub periods: u64,
43    /// Throttled periods, read from `cpu.stat` from the line starting with
44    /// `nr_throttled`. Set 0 if no data.
45    pub throttled_periods: u64,
46    /// Throttled time, read from `cpu.stat` from the line starting with
47    /// `throttled_time`. Set 0 if no data.
48    pub throttled_time: u64,
49}
50
51#[derive(Debug, Default)]
52pub struct MemoryCgroupStats {
53    pub memory: Option<MemoryStats>,
54    pub memory_swap: Option<MemoryStats>,
55    pub kernel_memory: Option<MemoryStats>,
56
57    /// Use hierarchy, read from `memory.use_hierarchy` in cgroups v1. Only
58    /// available in cgroups v1.
59    pub use_hierarchy: bool,
60
61    // The following data is read from `memory.stat`, see also
62    // `crate::fs::memory::MemoryStat::stat`.
63    pub cache: u64,
64    pub rss: u64,
65    pub rss_huge: u64,
66    pub shmem: u64,
67    pub mapped_file: u64,
68    pub dirty: u64,
69    pub writeback: u64,
70    pub swap: u64,
71    pub pgpgin: u64,
72    pub pgpgout: u64,
73    pub pgfault: u64,
74    pub pgmajfault: u64,
75    pub inactive_anon: u64,
76    pub active_anon: u64,
77    pub inactive_file: u64,
78    pub active_file: u64,
79    pub unevictable: u64,
80    pub hierarchical_memory_limit: i64,
81    pub hierarchical_memsw_limit: i64,
82    pub total_cache: u64,
83    pub total_rss: u64,
84    pub total_rss_huge: u64,
85    pub total_shmem: u64,
86    pub total_mapped_file: u64,
87    pub total_dirty: u64,
88    pub total_writeback: u64,
89    pub total_swap: u64,
90    pub total_pgpgin: u64,
91    pub total_pgpgout: u64,
92    pub total_pgfault: u64,
93    pub total_pgmajfault: u64,
94    pub total_inactive_anon: u64,
95    pub total_active_anon: u64,
96    pub total_inactive_file: u64,
97    pub total_active_file: u64,
98    pub total_unevictable: u64,
99}
100
101#[derive(Debug, Default)]
102pub struct MemoryStats {
103    /// Memory [swap] usage, read from `memory[.memsw].usage_in_bytes` in
104    /// cgroups v1 and `memory[.swap].current` in cgroups v2.
105    pub usage: u64,
106    /// Maximum memory [swap] usage observed by cgroups, read from
107    /// `memory[.memsw].max_usage_in_bytes` in cgroups v1 and
108    /// `memory[.swap].peak` in cgroups v2.
109    pub max_usage: u64,
110    /// Memory [swap] limit, read from `memory[.memsw].limit_in_bytes` in
111    /// cgroups v1 and `memory[.swap].max` in cgroups v2.
112    pub limit: i64,
113    /// Failure count, read from `memory[.memsw].failcnt`. Only available in
114    /// cgroups v1.
115    pub fail_cnt: u64,
116}
117
118#[derive(Debug, Default)]
119pub struct PidsCgroupStats {
120    /// Current number of processes in the cgroup, read from `pids.current`.
121    pub current: u64,
122    /// Maximum number of processes in the cgroup, read from `pids.limit`.
123    pub limit: i64,
124}
125
126#[derive(Debug, Default)]
127pub struct BlkioCgroupStats {
128    pub io_service_bytes_recursive: Vec<BlkioStat>,
129    pub io_serviced_recursive: Vec<BlkioStat>,
130    pub io_queued_recursive: Vec<BlkioStat>,
131    pub io_service_time_recursive: Vec<BlkioStat>,
132    pub io_wait_time_recursive: Vec<BlkioStat>,
133    pub io_merged_recursive: Vec<BlkioStat>,
134    pub io_time_recursive: Vec<BlkioStat>,
135    pub sectors_recursive: Vec<BlkioStat>,
136}
137
138#[derive(Debug, Default)]
139pub struct BlkioStat {
140    pub major: u64,
141    pub minor: u64,
142    pub op: String,
143    pub value: u64,
144}
145
146/// A structure representing the statistics of the `hugetlb` subsystem of a
147/// Cgroup. The key is the huge page size, and the value is the statistics
148/// for that size.
149pub type HugeTlbCgroupStats = HashMap<String, HugeTlbStat>;
150
151#[derive(Debug, Default)]
152pub struct HugeTlbStat {
153    pub usage: u64,
154    pub max_usage: u64,
155    pub fail_cnt: u64,
156}