Debian Codegraph @

src/collection/processes.rs

raw symbols SCIP
1 //! Data collection for processes.
2 //!
3 //! For Linux, this is handled by a custom set of functions.
4 //! For Windows, macOS, FreeBSD, Android, and Linux, this is handled by sysinfo.
5
6 use sysinforust-analyzer cargo sysinfo 0.39.6 crate/::ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#;
7
8 cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! {
9 target_os = "linux" => {
10 pub mod linuxrust-analyzer cargo bottom 0.14.9 collection/processes/linux/;
11 pub use selfrust-analyzer cargo bottom 0.14.9 collection/processes/::linuxrust-analyzer cargo bottom 0.14.9 collection/processes/linux/::*;
12 }
13 target_os = "macos" => {
14 pub mod macos;
15 pub(crate) use self::macos::*;
16 }
17 target_os = "windows" => {
18 pub mod windows;
19 pub use self::windows::*;
20 }
21 target_os = "freebsd" => {
22 pub mod freebsd;
23 pub(crate) use self::freebsd::*;
24 }
25 unix => {
26 pub(crate) struct GenericProcessExt;
27 impl UnixProcessExt for GenericProcessExt {}
28 }
29 _ => {}
30 }
31
32 cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! {
33 unix => {
34 pub mod unixrust-analyzer cargo bottom 0.14.9 collection/processes/unix/;
35 pub use selfrust-analyzer cargo bottom 0.14.9 collection/processes/::unixrust-analyzer cargo bottom 0.14.9 collection/processes/unix/::*;
36 }
37 _ => {}
38 }
39
40 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#, timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#};
41
42 use superrust-analyzer cargo bottom 0.14.9 collection/::{DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector#, errorrust-analyzer cargo bottom 0.14.9 collection/error/::CollectionResultrust-analyzer cargo bottom 0.14.9 collection/error/CollectionResult#};
43
44 cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! {
45 target_family = "windows" => {
46 /// A Windows process ID.
47 pub type Pid = usize;
48 }
49 unix => {
50 /// A UNIX process ID.
51 pub type Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid# = libcrust-analyzer cargo libc 0.2.189 crate/::pid_trust-analyzer cargo libc 0.2.189 unix/pid_t#;
52 }
53 _ => {}
54 }
55
56 pub type Bytesrust-analyzer cargo bottom 0.14.9 collection/processes/Bytes# = u64;
57
58 #[cfg(target_os = "linux")]
59 /// The process entry "type".
60 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
61 pub enum ProcessTyperust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType# {
62 /// A regular user process.
63 #[defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/default:]
64 Regularrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType#Regular#,
65
66 /// A kernel process.
67 Kernelrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType#Kernel#,
68
69 /// A thread spawned by a regular user process.
70 ProcessThreadrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType#ProcessThread#,
71 }
72
73 #[cfg(target_os = "linux")]
74 impl ProcessTyperust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType# {
75 /// Returns `true` if this is a thread.
76 pub fn is_threadrust-analyzer cargo bottom 0.14.9 collection/processes/impl#[ProcessType]is_thread().(&self) -> bool {
77 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Selfrust-analyzer cargo bottom 0.14.9 collection/processes/impl#[ProcessType]::ProcessThreadrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType#ProcessThread#)
78 }
79
80 /// Returns `true` if this is a kernel process.
81 pub fn is_kernelrust-analyzer cargo bottom 0.14.9 collection/processes/impl#[ProcessType]is_kernel().(&self) -> bool {
82 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Selfrust-analyzer cargo bottom 0.14.9 collection/processes/impl#[ProcessType]::Kernelrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType#Kernel#)
83 }
84 }
85
86 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
87 pub struct ProcessHarvestrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest# {
88 /// The pid of the process.
89 pub pidrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#pid.: Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#,
90
91 /// The parent PID of the process. A `parent_pid` of 0 is usually the root.
92 pub parent_pidrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#parent_pid.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#>,
93
94 /// CPU usage as a percentage.
95 pub cpu_usage_percentrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#cpu_usage_percent.: f32,
96
97 /// Memory usage as a percentage.
98 ///
99 /// TODO: Maybe calculate this on usage? Store the total mem along with the
100 /// vector of results.
101 pub mem_usage_percentrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#mem_usage_percent.: f32,
102
103 /// Memory usage as bytes.
104 pub mem_usagerust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#mem_usage.: Bytesrust-analyzer cargo bottom 0.14.9 collection/processes/Bytes#,
105
106 /// Virtual memory.
107 pub virtual_memrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#virtual_mem.: Bytesrust-analyzer cargo bottom 0.14.9 collection/processes/Bytes#,
108
109 /// The name of the process.
110 pub namerust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
111
112 /// The exact command for the process.
113 pub commandrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#command.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
114
115 /// Bytes read per second.
116 pub read_per_secrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#read_per_sec.: Bytesrust-analyzer cargo bottom 0.14.9 collection/processes/Bytes#,
117
118 /// Bytes written per second.
119 pub write_per_secrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#write_per_sec.: Bytesrust-analyzer cargo bottom 0.14.9 collection/processes/Bytes#,
120
121 /// The total number of bytes read by the process.
122 pub total_readrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#total_read.: Bytesrust-analyzer cargo bottom 0.14.9 collection/processes/Bytes#,
123
124 /// The total number of bytes written by the process.
125 pub total_writerust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#total_write.: Bytesrust-analyzer cargo bottom 0.14.9 collection/processes/Bytes#,
126
127 /// The current state of the process (e.g. zombie, asleep).
128 pub process_staterust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#process_state.: (&'static str, char),
129
130 /// Cumulative process uptime.
131 pub timerust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#time.: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#,
132
133 /// This is the *effective* user ID of the process. This is only used on
134 /// Unix platforms.
135 #[cfg(unix)]
136 pub uidrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#uid.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<libcrust-analyzer cargo libc 0.2.189 crate/::uid_trust-analyzer cargo libc 0.2.189 unix/uid_t#>,
137
138 /// This is the process' user.
139 pub userrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#user.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<str>>,
140
141 /// Gpu memory usage as bytes.
142 #[cfg(feature = "gpu")]
143 pub gpu_memrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#gpu_mem.: u64,
144
145 /// Gpu memory usage as percentage.
146 ///
147 /// TODO: Maybe calculate this on usage? Store the total GPU mem along with
148 /// the vector of results.
149 #[cfg(feature = "gpu")]
150 pub gpu_mem_percentrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#gpu_mem_percent.: f32,
151
152 /// Gpu utilization as a percentage.
153 #[cfg(feature = "gpu")]
154 pub gpu_utilrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#gpu_util.: u32,
155
156 /// The process entry "type".
157 #[cfg(target_os = "linux")]
158 pub process_typerust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#process_type.: ProcessTyperust-analyzer cargo bottom 0.14.9 collection/processes/ProcessType#,
159
160 /// The nice value (user-settable scheduling hint).
161 #[cfg(unix)]
162 pub nicerust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#nice.: i32,
163
164 /// The kernel scheduling priority.
165 pub priorityrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#priority.: i32,
166 // TODO: Additional fields
167 // pub rss_kb: u64,
168 // pub virt_kb: u64,
169 }
170
171 impl DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector# {
172 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn get_processesrust-analyzer cargo bottom 0.14.9 collection/processes/impl#[DataCollector]get_processes().(&mut self) -> CollectionResultrust-analyzer cargo bottom 0.14.9 collection/error/CollectionResult#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ProcessHarvestrust-analyzer cargo bottom 0.14.9 collection/processes/ProcessHarvest#>> {
173 cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! {
174 target_os = "linux" => {
175 let time_diff = self.datarust-analyzer cargo bottom 0.14.9 collection/DataCollector#data..collection_timerust-analyzer cargo bottom 0.14.9 collection/Data#collection_time.
176 .duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]duration_since().(self.last_collection_timerust-analyzer cargo bottom 0.14.9 collection/DataCollector#last_collection_time.)
177 .as_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs().();
178
179 linux_process_datarust-analyzer cargo bottom 0.14.9 collection/processes/linux/linux_process_data().(
180 self,
181 time_diff,
182 )
183 }
184 any(target_os = "freebsd", target_os = "macos", target_os = "windows", target_os = "android", target_os = "ios") => {
185 sysinfo_process_data(self)
186 }
187 _ => {
188 Err(crate::collection::error::CollectionError::Unsupported)
189 }
190 }
191 }
192 }
193
194 /// Pulled from [`ProcessStatus::to_string`] to avoid an alloc.
195 pub(superrust-analyzer cargo bottom 0.14.9 collection/) fn process_status_strrust-analyzer cargo bottom 0.14.9 collection/processes/process_status_str().(status: ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#) -> &'static str {
196 cfg_selectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/cfg_select!! {
197 target_os = "linux" => {
198 match status {
199 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Idlerust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Idle# => "Idle",
200 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Runrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Run# => "Runnable",
201 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Sleeprust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Sleep# => "Sleeping",
202 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Stoprust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Stop# => "Stopped",
203 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Zombierust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Zombie# => "Zombie",
204 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Tracingrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Tracing# => "Tracing",
205 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Deadrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Dead# => "Dead",
206 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Wakekillrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Wakekill# => "Wakekill",
207 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Wakingrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Waking# => "Waking",
208 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::Parkedrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#Parked# => "Parked",
209 ProcessStatusrust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#::UninterruptibleDiskSleeprust-analyzer cargo sysinfo 0.39.6 common/system/ProcessStatus#UninterruptibleDiskSleep# => "UninterruptibleDiskSleep",
210 _ => "Unknown",
211 }
212 }
213 target_os = "windows" => {
214 match status {
215 ProcessStatus::Run => "Runnable",
216 _ => "Unknown",
217 }
218 }
219 target_os = "macos" => {
220 match status {
221 ProcessStatus::Idle => "Idle",
222 ProcessStatus::Run => "Runnable",
223 ProcessStatus::Sleep => "Sleeping",
224 ProcessStatus::Stop => "Stopped",
225 ProcessStatus::Zombie => "Zombie",
226 _ => "Unknown",
227 }
228 }
229 target_os = "freebsd" => {
230 match status {
231 ProcessStatus::Idle => "Idle",
232 ProcessStatus::Run => "Runnable",
233 ProcessStatus::Sleep => "Sleeping",
234 ProcessStatus::Stop => "Stopped",
235 ProcessStatus::Zombie => "Zombie",
236 ProcessStatus::Dead => "Dead",
237 ProcessStatus::LockBlocked => "LockBlocked",
238 _ => "Unknown",
239 }
240 }
241 _ => {
242 "Unknown"
243 }
244 }
245 }