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 sysinfo::ProcessStatus;
7
8 cfg_select! {
9 target_os = "linux" => {
10 pub mod linux;
11 pub use self::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_select! {
33 unix => {
34 pub mod unix;
35 pub use self::unix::*;
36 }
37 _ => {}
38 }
39
40 use std::{sync::Arc, time::Duration};
41
42 use super::{DataCollector, error::CollectionResult};
43
44 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 Pid = libc::pid_t;
52 }
53 _ => {}
54 }
55
56 pub type Bytes = u64;
57
58 #[cfg(target_os = "linux")]
59 /// The process entry "type".
60 #[derive(Debug, Clone, Copy, Default)]
61 pub enum ProcessType {
62 /// A regular user process.
63 #[default]
64 Regular,
65
66 /// A kernel process.
67 Kernel,
68
69 /// A thread spawned by a regular user process.
70 ProcessThread,
71 }
72
73 #[cfg(target_os = "linux")]
74 impl ProcessType {
75 /// Returns `true` if this is a thread.
76 pub fn is_thread(&self) -> bool {
77 matches!(self, Self::ProcessThread)
78 }
79
80 /// Returns `true` if this is a kernel process.
81 pub fn is_kernel(&self) -> bool {
82 matches!(self, Self::Kernel)
83 }
84 }
85
86 #[derive(Debug, Clone, Default)]
87 pub struct ProcessHarvest {
88 /// The pid of the process.
89 pub pid: Pid,
90
91 /// The parent PID of the process. A `parent_pid` of 0 is usually the root.
92 pub parent_pid: Option<Pid>,
93
94 /// CPU usage as a percentage.
95 pub 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_percent: f32,
102
103 /// Memory usage as bytes.
104 pub mem_usage: Bytes,
105
106 /// Virtual memory.
107 pub virtual_mem: Bytes,
108
109 /// The name of the process.
110 pub name: String,
111
112 /// The exact command for the process.
113 pub command: String,
114
115 /// Bytes read per second.
116 pub read_per_sec: Bytes,
117
118 /// Bytes written per second.
119 pub write_per_sec: Bytes,
120
121 /// The total number of bytes read by the process.
122 pub total_read: Bytes,
123
124 /// The total number of bytes written by the process.
125 pub total_write: Bytes,
126
127 /// The current state of the process (e.g. zombie, asleep).
128 pub process_state: (&'static str, char),
129
130 /// Cumulative process uptime.
131 pub 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 uid: Option<libc::uid_t>,
137
138 /// This is the process' user.
139 pub user: Option<Arc<str>>,
140
141 /// Gpu memory usage as bytes.
142 #[cfg(feature = "gpu")]
143 pub 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_percent: f32,
151
152 /// Gpu utilization as a percentage.
153 #[cfg(feature = "gpu")]
154 pub gpu_util: u32,
155
156 /// The process entry "type".
157 #[cfg(target_os = "linux")]
158 pub process_type: ProcessType,
159
160 /// The nice value (user-settable scheduling hint).
161 #[cfg(unix)]
162 pub nice: i32,
163
164 /// The kernel scheduling priority.
165 pub priority: i32,
166 // TODO: Additional fields
167 // pub rss_kb: u64,
168 // pub virt_kb: u64,
169 }
170
171 impl DataCollector {
172 pub(crate) fn get_processes(&mut self) -> CollectionResult<Vec<ProcessHarvest>> {
173 cfg_select! {
174 target_os = "linux" => {
175 let time_diff = self.data.collection_time
176 .duration_since(self.last_collection_time)
177 .as_secs();
178
179 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(super) fn process_status_str(status: ProcessStatus) -> &'static str {
196 cfg_select! {
197 target_os = "linux" => {
198 match status {
199 ProcessStatus::Idle => "Idle",
200 ProcessStatus::Run => "Runnable",
201 ProcessStatus::Sleep => "Sleeping",
202 ProcessStatus::Stop => "Stopped",
203 ProcessStatus::Zombie => "Zombie",
204 ProcessStatus::Tracing => "Tracing",
205 ProcessStatus::Dead => "Dead",
206 ProcessStatus::Wakekill => "Wakekill",
207 ProcessStatus::Waking => "Waking",
208 ProcessStatus::Parked => "Parked",
209 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 }