|
| 1 | +use std::io; |
| 2 | + |
| 3 | +pub struct State(imp::State); |
| 4 | + |
| 5 | +impl State { |
| 6 | + /// Captures the current state of all CPUs on the system. |
| 7 | + /// |
| 8 | + /// The `State` returned here isn't too meaningful in terms of |
| 9 | + /// interpretation across platforms, but it can be compared to previous |
| 10 | + /// states to get a meaningful cross-platform number. |
| 11 | + pub fn current() -> io::Result<State> { |
| 12 | + imp::current().map(State) |
| 13 | + } |
| 14 | + |
| 15 | + /// Returns the percentage of time CPUs were idle from the current state |
| 16 | + /// relative to the previous state, as a percentage from 0.0 to 100.0. |
| 17 | + /// |
| 18 | + /// This function will return, as a percentage, the amount of time that the |
| 19 | + /// entire system was idle between the `previous` state and this own state. |
| 20 | + /// This can be useful to compare two snapshots in time of CPU usage to see |
| 21 | + /// how the CPU usage compares between the two. |
| 22 | + pub fn idle_since(&self, previous: &State) -> f64 { |
| 23 | + imp::pct_idle(&previous.0, &self.0) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[cfg(target_os = "linux")] |
| 28 | +mod imp { |
| 29 | + use std::fs::File; |
| 30 | + use std::io::{self, Read}; |
| 31 | + |
| 32 | + pub struct State { |
| 33 | + user: u64, |
| 34 | + nice: u64, |
| 35 | + system: u64, |
| 36 | + idle: u64, |
| 37 | + iowait: u64, |
| 38 | + irq: u64, |
| 39 | + softirq: u64, |
| 40 | + steal: u64, |
| 41 | + guest: u64, |
| 42 | + guest_nice: u64, |
| 43 | + } |
| 44 | + |
| 45 | + pub fn current() -> io::Result<State> { |
| 46 | + let mut state = String::new(); |
| 47 | + File::open("/proc/stat")?.read_to_string(&mut state)?; |
| 48 | + let mut parts = state.lines().next().unwrap().split_whitespace(); |
| 49 | + if parts.next() != Some("cpu") { |
| 50 | + return Err(io::Error::new( |
| 51 | + io::ErrorKind::Other, |
| 52 | + "cannot parse /proc/stat", |
| 53 | + )); |
| 54 | + } |
| 55 | + |
| 56 | + Ok(State { |
| 57 | + user: parts.next().unwrap().parse::<u64>().unwrap(), |
| 58 | + nice: parts.next().unwrap().parse::<u64>().unwrap(), |
| 59 | + system: parts.next().unwrap().parse::<u64>().unwrap(), |
| 60 | + idle: parts.next().unwrap().parse::<u64>().unwrap(), |
| 61 | + iowait: parts.next().unwrap().parse::<u64>().unwrap(), |
| 62 | + irq: parts.next().unwrap().parse::<u64>().unwrap(), |
| 63 | + softirq: parts.next().unwrap().parse::<u64>().unwrap(), |
| 64 | + steal: parts.next().unwrap().parse::<u64>().unwrap(), |
| 65 | + guest: parts.next().unwrap().parse::<u64>().unwrap(), |
| 66 | + guest_nice: parts.next().unwrap().parse::<u64>().unwrap(), |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + pub fn pct_idle(prev: &State, next: &State) -> f64 { |
| 71 | + let user = next.user - prev.user; |
| 72 | + let nice = next.nice - prev.nice; |
| 73 | + let system = next.system - prev.system; |
| 74 | + let idle = next.idle - prev.idle; |
| 75 | + let iowait = next.iowait - prev.iowait; |
| 76 | + let irq = next.irq - prev.irq; |
| 77 | + let softirq = next.softirq - prev.softirq; |
| 78 | + let steal = next.steal - prev.steal; |
| 79 | + let guest = next.guest - prev.guest; |
| 80 | + let guest_nice = next.guest_nice - prev.guest_nice; |
| 81 | + let total = |
| 82 | + user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice; |
| 83 | + |
| 84 | + (idle as f64) / (total as f64) * 100.0 |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(target_os = "macos")] |
| 89 | +#[allow(bad_style)] |
| 90 | +mod imp { |
| 91 | + use std::io; |
| 92 | + use std::ptr; |
| 93 | + use std::slice; |
| 94 | + |
| 95 | + type host_t = u32; |
| 96 | + type mach_port_t = u32; |
| 97 | + type processor_flavor_t = i32; |
| 98 | + type natural_t = u32; |
| 99 | + type processor_info_array_t = *mut i32; |
| 100 | + type mach_msg_type_number_t = i32; |
| 101 | + type kern_return_t = i32; |
| 102 | + |
| 103 | + const PROESSOR_CPU_LOAD_INFO: processor_flavor_t = 2; |
| 104 | + const CPU_STATE_USER: usize = 0; |
| 105 | + const CPU_STATE_SYSTEM: usize = 1; |
| 106 | + const CPU_STATE_IDLE: usize = 2; |
| 107 | + const CPU_STATE_NICE: usize = 3; |
| 108 | + |
| 109 | + extern "C" { |
| 110 | + fn mach_host_self() -> mach_port_t; |
| 111 | + fn host_processor_info( |
| 112 | + host: host_t, |
| 113 | + flavor: processor_flavor_t, |
| 114 | + out_processor_count: *mut natural_t, |
| 115 | + out_processor_info: *mut processor_info_array_t, |
| 116 | + out_processor_infoCnt: *mut mach_msg_type_number_t, |
| 117 | + ) -> kern_return_t; |
| 118 | + } |
| 119 | + |
| 120 | + pub struct State { |
| 121 | + user: u64, |
| 122 | + system: u64, |
| 123 | + idle: u64, |
| 124 | + nice: u64, |
| 125 | + } |
| 126 | + |
| 127 | + pub fn current() -> io::Result<State> { |
| 128 | + unsafe { |
| 129 | + let mut num_cpus_u = 0; |
| 130 | + let mut cpu_info = ptr::null_mut(); |
| 131 | + let mut cpu_info_cnt = 0; |
| 132 | + let err = host_processor_info( |
| 133 | + mach_host_self(), |
| 134 | + PROESSOR_CPU_LOAD_INFO, |
| 135 | + &mut num_cpus_u, |
| 136 | + &mut cpu_info, |
| 137 | + &mut cpu_info_cnt, |
| 138 | + ); |
| 139 | + if err != 0 { |
| 140 | + return Err(io::Error::last_os_error()); |
| 141 | + } |
| 142 | + let cpu_info = slice::from_raw_parts(cpu_info, cpu_info_cnt as usize); |
| 143 | + let mut ret = State { |
| 144 | + user: 0, |
| 145 | + system: 0, |
| 146 | + idle: 0, |
| 147 | + nice: 0, |
| 148 | + }; |
| 149 | + for chunk in cpu_info.chunks(num_cpus_u as usize) { |
| 150 | + ret.user += chunk[CPU_STATE_USER] as u64; |
| 151 | + ret.system += chunk[CPU_STATE_SYSTEM] as u64; |
| 152 | + ret.idle += chunk[CPU_STATE_IDLE] as u64; |
| 153 | + ret.nice += chunk[CPU_STATE_NICE] as u64; |
| 154 | + } |
| 155 | + Ok(ret) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + pub fn pct_idle(prev: &State, next: &State) -> f64 { |
| 160 | + let user = next.user - prev.user; |
| 161 | + let system = next.system - prev.system; |
| 162 | + let idle = next.idle - prev.idle; |
| 163 | + let nice = next.nice - prev.nice; |
| 164 | + let total = user + system + idle + nice; |
| 165 | + (idle as f64) / (total as f64) * 100.0 |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +#[cfg(windows)] |
| 170 | +mod imp { |
| 171 | + use std::io; |
| 172 | + use std::mem; |
| 173 | + use winapi::shared::minwindef::*; |
| 174 | + use winapi::um::processthreadsapi::*; |
| 175 | + |
| 176 | + pub struct State { |
| 177 | + idle: FILETIME, |
| 178 | + kernel: FILETIME, |
| 179 | + user: FILETIME, |
| 180 | + } |
| 181 | + |
| 182 | + pub fn current() -> io::Result<State> { |
| 183 | + unsafe { |
| 184 | + let mut ret = mem::zeroed::<State>(); |
| 185 | + let r = GetSystemTimes(&mut ret.idle, &mut ret.kernel, &mut ret.user); |
| 186 | + if r != 0 { |
| 187 | + Ok(ret) |
| 188 | + } else { |
| 189 | + Err(io::Error::last_os_error()) |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + pub fn pct_idle(prev: &State, next: &State) -> f64 { |
| 195 | + fn to_u64(a: &FILETIME) -> u64 { |
| 196 | + ((a.dwHighDateTime as u64) << 32) | (a.dwLowDateTime as u64) |
| 197 | + } |
| 198 | + |
| 199 | + let idle = to_u64(&next.idle) - to_u64(&prev.idle); |
| 200 | + let kernel = to_u64(&next.kernel) - to_u64(&prev.kernel); |
| 201 | + let user = to_u64(&next.user) - to_u64(&prev.user); |
| 202 | + let total = user + kernel; |
| 203 | + (idle as f64) / (total as f64) * 100.0 |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))] |
| 208 | +mod imp { |
| 209 | + use std::io; |
| 210 | + |
| 211 | + pub struct State; |
| 212 | + |
| 213 | + pub fn current() -> io::Result<State> { |
| 214 | + Err(io::Error::new( |
| 215 | + io::ErrorKind::Other, |
| 216 | + "unsupported platform to learn CPU state", |
| 217 | + )) |
| 218 | + } |
| 219 | + |
| 220 | + pub fn pct_idle(_prev: &State, _next: &State) -> f64 { |
| 221 | + unimplemented!() |
| 222 | + } |
| 223 | +} |
0 commit comments