Debian Codegraph @

src/collection/memory/sysinfo.rs

raw symbols SCIP
1 //! Collecting memory data using sysinfo.
2
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::numrust-analyzer cargo std https://github.com/rust-lang/rust/library/std num/::NonZeroU64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroU64#;
4
5 #[cfg(target_os = "linux")]
6 use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::linuxrust-analyzer cargo bottom 0.14.9 collection/linux/::cgroupsrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/::CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#;
7 use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::{DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector#, memoryrust-analyzer cargo bottom 0.14.9 collection/memory/::MemDatarust-analyzer cargo bottom 0.14.9 collection/memory/MemData#};
8
9 #[inline]
10 fn get_usagerust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/get_usage().(used: u64, total: u64) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<MemDatarust-analyzer cargo bottom 0.14.9 collection/memory/MemData#> {
11 NonZeroU64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroU64#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[`NonZero<T>`]new().(total).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|total_bytes| MemDatarust-analyzer cargo bottom 0.14.9 collection/memory/MemData# {
12 total_bytesrust-analyzer cargo bottom 0.14.9 collection/memory/MemData#total_bytes.,
13 used_bytesrust-analyzer cargo bottom 0.14.9 collection/memory/MemData#used_bytes.: used,
14 })
15 }
16
17 /// Resolves the total memory to report given an optional cgroup limit and the physical total.
18 ///
19 /// cgroup v1 reports an "unlimited" limit as a very large value, which causes problems if taken
20 /// literally. This function caps it to the minimum of the cgroup total or the actual total to
21 /// avoid this problem.
22 #[cfg(target_os = "linux")]
23 #[inline]
24 fn resolve_cgroup_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/resolve_cgroup_total().(limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#>, base_total: u64) -> u64 {
25 match limit {
26 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#(bytes)) => (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().bytes).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(base_total),
27 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Maxrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Max#) | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => base_total,
28 }
29 }
30
31 /// Returns memory (RAM) usage using sysinfo.
32 ///
33 /// On Linux, this will take cgroup usage/limits into account.
34 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn get_ram_usagerust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/get_ram_usage().(collector: &DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<MemDatarust-analyzer cargo bottom 0.14.9 collection/memory/MemData#> {
35 let sys = &collector.sysrust-analyzer cargo bottom 0.14.9 collection/DataCollector#sys..systemrust-analyzer cargo bottom 0.14.9 collection/SysinfoSource#system.;
36
37 cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! {
38 target_os = "linux" => {
39 let base_used = sys.used_memoryrust-analyzer cargo sysinfo 0.39.6 common/system/impl#[System]used_memory().();
40 let base_total = sys.total_memoryrust-analyzer cargo sysinfo 0.39.6 common/system/impl#[System]total_memory().();
41
42 let (used, total) = match &collector.cgroup_memory_datarust-analyzer cargo bottom 0.14.9 collection/DataCollector#cgroup_memory_data..ramrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#ram. {
43 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cgroup_data) => {
44 let used = cgroup_data.used_bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#used_bytes.;
45 let total = resolve_cgroup_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/resolve_cgroup_total().(cgroup_data.limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#limit..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().(), base_total);
46
47 (used, total)
48 }
49 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => (base_used, base_total),
50 };
51
52 get_usagerust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/get_usage().(used, total)
53 }
54 _ => {
55 get_usage(sys.used_memory(), sys.total_memory())
56 }
57 }
58 }
59
60 /// Returns SWAP usage using sysinfo.
61 ///
62 /// On Linux, this will take cgroup usage/limits into account.
63 #[cfg(not(target_os = "windows"))]
64 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn get_swap_usagerust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/get_swap_usage().(collector: &DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<MemDatarust-analyzer cargo bottom 0.14.9 collection/memory/MemData#> {
65 let sys = &collector.sysrust-analyzer cargo bottom 0.14.9 collection/DataCollector#sys..systemrust-analyzer cargo bottom 0.14.9 collection/SysinfoSource#system.;
66
67 cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! {
68 target_os = "linux" => {
69 let base_used = sys.used_swaprust-analyzer cargo sysinfo 0.39.6 common/system/impl#[System]used_swap().();
70 let base_total = sys.total_swaprust-analyzer cargo sysinfo 0.39.6 common/system/impl#[System]total_swap().();
71
72 let (used, total) = match &collector.cgroup_memory_datarust-analyzer cargo bottom 0.14.9 collection/DataCollector#cgroup_memory_data..swaprust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemCollector#swap. {
73 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cgroup_data) => {
74 let used = cgroup_data.used_bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#used_bytes.;
75 let total = resolve_cgroup_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/resolve_cgroup_total().(cgroup_data.limitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemData#limit..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().(), base_total);
76
77 (used, total)
78 }
79 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => (base_used, base_total),
80 };
81
82 get_usagerust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/get_usage().(used, total)
83 }
84 _ => {
85 get_usage(sys.used_swap(), sys.total_swap())
86 }
87 }
88 }
89
90 /// Returns cache usage using sysinfo.
91 ///
92 /// sysinfo has no way to do this directly but it should equal the difference
93 /// between the available and free memory. Free memory is defined as memory
94 /// not containing any data, which means cache and buffer memory are not
95 /// "free". Available memory is defined as memory able to be allocated by
96 /// processes, which includes cache and buffer memory. On Windows, this will
97 /// always be 0.
98 ///
99 /// For more information, see [sysinfo docs](https://docs.rs/sysinfo/latest/sysinfo/struct.System.html#method.available_memory)
100 /// and [this explanation on memory](https://askubuntu.com/questions/867068/what-is-available-memory-while-using-free-command)
101 #[cfg(not(target_os = "windows"))]
102 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn get_cache_usagerust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/get_cache_usage().(sys: &sysinforust-analyzer cargo sysinfo 0.39.6 crate/::Systemrust-analyzer cargo sysinfo 0.39.6 common/system/System#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<MemDatarust-analyzer cargo bottom 0.14.9 collection/memory/MemData#> {
103 let mem_used = sys.available_memoryrust-analyzer cargo sysinfo 0.39.6 common/system/impl#[System]available_memory().().saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(sys.free_memoryrust-analyzer cargo sysinfo 0.39.6 common/system/impl#[System]free_memory().());
104 let mem_total = sys.total_memoryrust-analyzer cargo sysinfo 0.39.6 common/system/impl#[System]total_memory().();
105
106 get_usagerust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/get_usage().(mem_used, mem_total)
107 }
108
109 #[cfg(test)]
110 #[cfg(target_os = "linux")]
111 mod linux_testsrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/ {
112 use superrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/::*;
113
114 const BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.: u64 = 16 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().1024 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().1024 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().1024; // 16 GiB
115
116 /// Regression test for <https://github.com/ClementTsang/bottom/issues/2092>.
117 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
118 fn cap_cgroup_v1_limitsrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/cap_cgroup_v1_limits().() {
119 let sentinel = u64::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]MAX.;
120 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
121 resolve_cgroup_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/resolve_cgroup_total().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#(sentinel)), BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.),
122 BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.
123 );
124 }
125
126 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
127 fn legit_cgroup_limit_worksrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/legit_cgroup_limit_works().() {
128 let limit = 4 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().1024 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().1024 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().1024; // 4 GiB
129 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
130 resolve_cgroup_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/resolve_cgroup_total().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Bytesrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Bytes#(limit)), BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.),
131 limit
132 );
133 }
134
135 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
136 fn max_and_missing_limit_use_physical_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/max_and_missing_limit_use_physical_total().() {
137 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
138 resolve_cgroup_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/resolve_cgroup_total().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&CgroupMemLimitrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#::Maxrust-analyzer cargo bottom 0.14.9 collection/linux/cgroups/CgroupMemLimit#Max#), BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.),
139 BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.
140 );
141 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(resolve_cgroup_totalrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/resolve_cgroup_total().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.), BASE_TOTALrust-analyzer cargo bottom 0.14.9 collection/memory/sysinfo/linux_tests/BASE_TOTAL.);
142 }
143 }