1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use std::fs;
use std::io::{Error, ErrorKind, Result};
use std::{thread, time};

#[derive(Debug, Clone)]
pub struct CpuTimes {
    /// Time spent by normal processes executing in user mode;
    /// on Linux this also includes guest time
    pub user: u64,

    /// Time spent by niced (prioritized) processes executing in user mode;
    /// on Linux this also includes guest_nice time
    pub nice: u64,

    /// Time spent by processes executing in kernel mode
    pub system: u64,

    /// Time spent doing nothing
    pub idle: u64,

    /// Time spent waiting for I/O to complete
    pub iowait: u64,

    /// Time spent for servicing hardware interrupts
    pub irq: u64,

    /// Time spent for servicing software interrupts
    pub softirq: u64,

    /// Time spent by other operating systems running in a virtualized environment
    pub steal: u64,

    /// Time spent running a virtual CPU for guest operating systems
    /// under the control of the Linux kernel
    pub guest: u64,

    /// Time spent running a niced guest
    /// (virtual CPU for guest operating systems
    /// under the control of the Linux kernel)
    pub guest_nice: u64,
}

impl CpuTimes {
    /// Calculate the total time of CPU utilization.
    /// guest time and guest_nice time are respectively include
    /// on user and nice times
    fn total_time(&self) -> u64 {
        self.user
            + self.nice
            + self.system
            + self.idle
            + self.iowait
            + self.irq
            + self.softirq
            + self.steal
    }

    /// Return the CpuTimesPercent object that contains the detailed
    /// CPU times percentage of CPU utilization between two instants.
    fn cpu_percent_since(&self, past_cpu_times: &CpuTimes) -> CpuTimesPercent {
        if self.total_time() > past_cpu_times.total_time() {
            let diff_total = (self.total_time() - past_cpu_times.total_time()) as f64;
            CpuTimesPercent {
                user: delta_percentage(self.user, past_cpu_times.user, diff_total),
                nice: delta_percentage(self.nice, past_cpu_times.nice, diff_total),
                system: delta_percentage(self.system, past_cpu_times.system, diff_total),
                idle: delta_percentage(self.idle, past_cpu_times.idle, diff_total),
                iowait: delta_percentage(self.iowait, past_cpu_times.iowait, diff_total),
                irq: delta_percentage(self.irq, past_cpu_times.irq, diff_total),
                softirq: delta_percentage(self.softirq, past_cpu_times.softirq, diff_total),
                steal: delta_percentage(self.steal, past_cpu_times.steal, diff_total),
                guest: delta_percentage(self.guest, past_cpu_times.guest, diff_total),
                guest_nice: delta_percentage(
                    self.guest_nice,
                    past_cpu_times.guest_nice,
                    diff_total,
                ),
            }
        } else {
            CpuTimesPercent {
                user: 0.,
                nice: 0.,
                system: 0.,
                idle: 0.,
                iowait: 0.,
                irq: 0.,
                softirq: 0.,
                steal: 0.,
                guest: 0.,
                guest_nice: 0.,
            }
        }
    }
}

#[derive(Debug)]
pub struct CpuTimesPercent {
    /// Percentage of time spent by normal processes executing in user mode
    /// between two instants;
    /// on Linux this also includes guest time
    pub user: f64,

    /// Percentage of time spent by niced (prioritized) processes
    /// executing in user mode between two instants;
    /// on Linux this also includes guest_nice time
    pub nice: f64,

    /// Percentage of time spent by processes executing in kernel
    /// mode between two instants
    pub system: f64,

    /// Percentage of time spent doing nothing between two instants
    pub idle: f64,

    /// Percentage of time spent waiting for I/O to complete between two instants
    pub iowait: f64,

    /// Percentage of time spent for servicing hardware interrupts
    /// between two instants
    pub irq: f64,

    /// Percentage of time spent for servicing software interrupts
    /// between two instants
    pub softirq: f64,

    /// Percentage of time spent by other operating systems running
    /// in a virtualized environment between two instants
    pub steal: f64,

    /// Percentage of time spent running a virtual CPU for guest operating systems
    /// under the control of the Linux kernel between two instants
    pub guest: f64,

    /// Percentage of time spent running a niced guest
    /// (virtual CPU for guest operating systems
    /// under the control of the Linux kernel) between two instants
    pub guest_nice: f64,
}

impl CpuTimesPercent {
    /// Caculculate the busy time in percent of a CPU
    /// Guest and guest_nice are count in user and nice.
    /// We ignore the CPU time in idle and iowait.
    fn busy_times(&self) -> f64 {
        self.user + self.nice + self.system + self.irq + self.softirq + self.steal
    }
}

/// To get a CpuPercent struct in non-blocking mode.
///
/// Example :
///
///     let mut cpu_percent_collector = match psutil::system::CpuPercentCollector::new() {
///         Ok(cpu_percent_collector) => cpu_percent_collector,
///         Err(_) => {
///             println!("Could not initialize cpu_percent_collector");
///             return;
///         },
///     };
///
///     // {... Your programme ...}
///
///     let cpu_times_percent = cpu_percent_collector.cpu_times_percent();
///     let cpu_times_percent_percpu = cpu_percent_collector.cpu_times_percent_percpu();
///
/// See an other example in examples/cpu_percent.
#[derive(Clone, Debug)]
pub struct CpuPercentCollector {
    /// Store the CPU times informations of the last call
    /// of class method cpu_times_percent or cpu_times_percent_percpu
    /// for the global CPU
    last_statement_cpu: CpuTimes,

    /// Store the CPU times informations of the last call
    /// of class method cpu_times_percent or cpu_times_percent_percpu per CPU.
    last_statement_percpu: Vec<CpuTimes>,
}

impl CpuPercentCollector {
    /// Initialize the CpuPercentCollector struct with the cpu_times informations.
    pub fn new() -> Result<CpuPercentCollector> {
        let last_statement_cpu = cpu_times()?;
        let last_statement_percpu = cpu_times_percpu()?;
        Ok(CpuPercentCollector {
            last_statement_cpu,
            last_statement_percpu,
        })
    }

    /// Returns a Result of CpuTimesPercent calculate
    /// since the last call of the method.
    /// For the first call it is since the object creation.
    ///
    /// The CpuTimesPercent object contains the detailed CPU utilization
    /// as percentage.
    pub fn cpu_times_percent(&mut self) -> Result<CpuTimesPercent> {
        let current_cpu_times = cpu_times()?;

        let cpu_percent_since = current_cpu_times.cpu_percent_since(&self.last_statement_cpu);

        self.last_statement_cpu = current_cpu_times;

        Ok(cpu_percent_since)
    }

    /// Returns a Result of vector of CpuTimesPercent
    /// calculate since the last call of the method.
    /// For the first call it is since the object creation.
    ///
    /// Each element of the vector reprensents the detailed
    /// CPU utilization as percentage per CPU.
    pub fn cpu_times_percent_percpu(&mut self) -> Result<Vec<CpuTimesPercent>> {
        let current_cpu_times_percpu = cpu_times_percpu()?;

        let mut cpu_times_percent_vector: Vec<CpuTimesPercent> = Vec::new();
        let current_cpu_times_percpu_copy = current_cpu_times_percpu.clone();
        for (iter, cpu_times) in current_cpu_times_percpu.iter().enumerate() {
            cpu_times_percent_vector
                .push(cpu_times.cpu_percent_since(&self.last_statement_percpu[iter]));
        }
        self.last_statement_percpu = current_cpu_times_percpu_copy;

        Ok(cpu_times_percent_vector)
    }
}

/// Convert a cpu line from /proc/stat into a Vec<u64>.
fn info_cpu_line(cpu_line: &str) -> Result<Vec<u64>> {
    let mut fields: Vec<&str> = cpu_line.split_whitespace().collect();
    if fields.len() < 2 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("Wrong line use from /proc/stat : {}", cpu_line),
        ));
    }
    if fields.len() < 10 {
        return Err(Error::new(
                ErrorKind::InvalidData,
                format!("Wrong format of /proc/stat line : {}, Maybe the kernel version is too old (Linux 2.6.33)", cpu_line),
        ));
    }
    // The first element of the line contains "cpux", we remove it.
    fields.remove(0);
    let mut values: Vec<u64> = Vec::new();
    for elt in fields {
        let value = match elt.parse::<u64>() {
            Ok(v) => v,
            Err(_) => {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("failed to parse {}", elt),
                ));
            }
        };
        values.push(value);
    }
    Ok(values)
}

/// Return the CpuTimes object from the Vec<u64> obtain by info_cpu_line.
fn cpu_line_to_cpu_times(cpu_info: &[u64]) -> CpuTimes {
    let user = cpu_info[0];
    let nice = cpu_info[1];
    let system = cpu_info[2];
    let idle = cpu_info[3];
    let iowait = cpu_info[4];
    let irq = cpu_info[5];
    let softirq = cpu_info[6];
    let steal = cpu_info[7];
    let guest = cpu_info[8];
    let guest_nice = cpu_info[9];

    CpuTimes {
        user,
        nice,
        system,
        idle,
        iowait,
        irq,
        softirq,
        steal,
        guest,
        guest_nice,
    }
}

/// Returns information about cpu times usage.
///
/// `/proc/stat` contains the cpu times statistics
pub fn cpu_times() -> Result<CpuTimes> {
    let data = fs::read_to_string("/proc/stat")?;
    let lines: Vec<&str> = data.lines().collect();
    let cpu_info = match info_cpu_line(lines[0]) {
        Ok(cpu_info) => cpu_info,
        Err(error) => return Err(error),
    };
    Ok(cpu_line_to_cpu_times(&cpu_info))
}

/// Returns information about cpu time usage on a Vec
/// that contains information per cpu
///
/// '/proc/stat' contains the cpu times statistics
pub fn cpu_times_percpu() -> Result<Vec<CpuTimes>> {
    let data = fs::read_to_string("/proc/stat")?;
    let mut lines: Vec<&str> = data.lines().collect();
    let mut cpu_times_vector: Vec<CpuTimes> = Vec::new();
    // Remove the first line that contain the total cpu: "cpu"
    lines.remove(0);
    for line in lines {
        if line.starts_with("cpu") {
            let cpu_info = match info_cpu_line(line) {
                Ok(cpu_info) => cpu_info,
                Err(error) => return Err(error),
            };
            let cpu_time = cpu_line_to_cpu_times(&cpu_info);
            cpu_times_vector.push(cpu_time);
        }
    }
    Ok(cpu_times_vector)
}

/// Return a float representing the current system-wide
/// CPU utilization as percentage.
///
/// Interval must be > 0 seconds.
/// If interval < 0.1, the result of this function will be meaningless.
/// The function compares system CPU times elapsed before and after
/// the interval (blocking).
///
/// Use information contains in '/proc/stat'
pub fn cpu_percent(interval: f64) -> Result<f64> {
    Ok(cpu_times_percent(interval)?.busy_times())
}

/// Return a vector of floats representing the current system-wide
/// CPU utilization as percentage.
///
/// Interval must be > 0.0 seconds.
/// If interval < 0.1, the result of this function will be meaningless.
/// The function compares system CPU times per CPU elapsed before and after
/// the interval (blocking).
///
/// Use information contains in '/proc/stat'
pub fn cpu_percent_percpu(interval: f64) -> Result<Vec<f64>> {
    let cpu_percent_percpu = cpu_times_percent_percpu(interval)?;
    let mut cpu_percents: Vec<f64> = Vec::new();

    for cpu_percent in cpu_percent_percpu {
        cpu_percents.push(cpu_percent.busy_times());
    }
    Ok(cpu_percents)
}

/// Return a CpuTimesPercent representing the current detailed
/// CPU utilization as a percentage.
///
/// Interval must be > 0.0 seconds.
/// If interval is < 0.1, the result of this function will be meaningless.
/// The function compares all CPU times elapsed before and after
/// the interval (blocking).
///
/// Use informations contains in '/proc/stat'
pub fn cpu_times_percent(interval: f64) -> Result<CpuTimesPercent> {
    let mut cpu_percent_last_call = CpuPercentCollector::new()?;

    let interval = (test_interval(interval)? * 1000.) as u64;
    let block_time = time::Duration::from_millis(interval);
    thread::sleep(block_time);

    cpu_percent_last_call.cpu_times_percent()
}

/// Return a vector of CpuTimesPercent representing the current detailed
/// CPU utilization as percentage per cpu.
///
/// Interval must be > 0.0 seconds.
/// If interval is < 0.1, the result of this function will be meaningless.
/// The function compares all  CPU times per CPU elapsed before and after
/// the interval(blocking).
///
/// Use informations contains in '/proc/stat'
pub fn cpu_times_percent_percpu(interval: f64) -> Result<Vec<CpuTimesPercent>> {
    let mut cpu_percent_last_call = CpuPercentCollector::new()?;

    let interval = (test_interval(interval)? * 1000.) as u64;
    let block_time = time::Duration::from_millis(interval);
    thread::sleep(block_time);

    cpu_percent_last_call.cpu_times_percent_percpu()
}

/// Calculate a percentage from two values and a diff_total
///
/// Use in cpu_percent_since method
fn delta_percentage(current_value: u64, past_value: u64, total_diff: f64) -> f64 {
    if past_value <= current_value {
        let percentage = (100. * (current_value - past_value) as f64) / total_diff;
        if percentage > 100. {
            100.
        } else {
            percentage
        }
    } else {
        0.
    }
}

/// Test interval value validity
fn test_interval(interval: f64) -> Result<f64> {
    if interval <= 0. {
        Err(Error::new(
            ErrorKind::InvalidData,
            format!("Interval must be greater than 0 : {}", interval),
        ))
    } else {
        Ok(interval)
    }
}

#[cfg(test)]
mod unit_tests {
    use super::*;

    #[test]
    fn info_cpu_line_test() {
        let input = "cpu0 61286 322 19182 1708940 323 0 322 0 0 0 ";
        let result = match info_cpu_line(input) {
            Ok(r) => r,
            Err(e) => panic!("{}", e),
        };
        assert_eq!(
            result,
            vec![61286, 322, 19182, 1_708_940, 323, 0, 322, 0, 0, 0]
        );
    }

    #[test]
    fn cpu_line_to_cpu_times_test() {
        let input = vec![62972, 178, 18296, 349_198, 163, 0, 493, 0, 0, 0];
        let result = cpu_line_to_cpu_times(&input);
        assert_eq!(result.user, 62972);
        assert_eq!(result.nice, 178);
        assert_eq!(result.system, 18296);
        assert_eq!(result.idle, 349_198);
        assert_eq!(result.iowait, 163);
        assert_eq!(result.irq, 0);
        assert_eq!(result.softirq, 493);
        assert_eq!(result.steal, 0);
        assert_eq!(result.guest, 0);
        assert_eq!(result.guest_nice, 0);
    }

    #[test]
    fn cpu_time_percent_test() {
        let input1 = vec![62972, 178, 18296, 349_198, 163, 0, 493, 0, 0, 0];
        let input2 = vec![61286, 322, 19182, 1_708_940, 323, 0, 322, 0, 0, 0];
        let result1 = cpu_line_to_cpu_times(&input1);
        let result2 = cpu_line_to_cpu_times(&input2);
        let percent = result2.cpu_percent_since(&result1);
        assert!(percent.user >= 0.);
        assert!(percent.user <= 100.);
        assert!(percent.nice >= 0.);
        assert!(percent.nice <= 100.);
        assert!(percent.system >= 0.);
        assert!(percent.system <= 100.);
        assert!(percent.idle >= 0.);
        assert!(percent.idle <= 100.);
        assert!(percent.iowait >= 0.);
        assert!(percent.iowait <= 100.);
        assert!(percent.irq >= 0.);
        assert!(percent.irq <= 100.);
        assert!(percent.softirq >= 0.);
        assert!(percent.softirq <= 100.);
        assert!(percent.guest >= 0.);
        assert!(percent.guest <= 100.);
        assert!(percent.guest_nice >= 0.);
        assert!(percent.guest_nice <= 100.);
        assert!(percent.steal >= 0.);
        assert!(percent.steal <= 100.);
    }
}