sysutil 0.7.2

Linux system utils library
Documentation
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use std::{fs, path, thread};
use std::collections::HashMap;
use std::process::Command;
use std::time::Duration;
use crate::utils::{*};
use regex;

/// Contains the average CPU usage and the discrete usage for each processor
#[derive(Debug, Clone)]
pub struct CpuUsage {
    pub average: ProcessorUsage,
    pub processors: Vec<ProcessorUsage>,
}

/// Encloses the different parameters relative to processor usage
#[derive(Debug, Clone)]
pub struct ProcessorUsage {
    pub total: f32,
    pub user: f32,
    pub nice: f32,
    pub system: f32,
    pub idle: f32,
    pub iowait: f32,
    pub interrupt: f32,
    pub soft_interrupt: f32,
}

impl ProcessorUsage {
    fn clone(&self) -> ProcessorUsage {
        return ProcessorUsage {
            total: self.total,
            user: self.user,
            nice: self.nice,
            system: self.system,
            idle: self.idle,
            iowait: self.iowait,
            interrupt: self.interrupt,
            soft_interrupt: self.soft_interrupt,
        };
    }
}

/// Contains base information relative to the CPU
#[derive(Debug, Clone)]
pub struct CpuInfo {
    pub modelName: String,
    pub cores: usize,
    pub threads: usize,
    pub dies: usize,
    pub governors: Vec<String>,
    pub maxFrequencyMHz: f32,
    pub clockBoost: Option<bool>,
    pub architecture: String,
    pub byteOrder: String
}

/// Encloses all CPU-related data available in the library
/// ## Example
/// Once generating a `CPU` instance, usages and scheduler policies can be updated by the `update()` method
/// ```rust
/// use sysutil;
///
/// let mut cpu = sysutil::cpu::CPU::new();
/// cpu.update();
/// ```
#[derive(Debug, Clone)]
pub struct CPU {
    pub info: CpuInfo,
    pub averageUsage: ProcessorUsage,
    pub perProcessorUsage: Vec<ProcessorUsage>,
    pub schedulerPolicies: Vec<SchedulerPolicy>,
    pub averageFrequency: Frequency,
    pub perProcessorFrequency: Vec<ProcessorFrequency>
}

impl CPU {
    pub fn new() -> CPU {
        let cpuUsage = cpuUsage();
        let frequency = cpuFrequency();

        CPU {
            info: cpuInfo(),
            averageUsage: cpuUsage.average,
            perProcessorUsage: cpuUsage.processors,
            schedulerPolicies: schedulerInfo(),
            averageFrequency: frequency.average,
            perProcessorFrequency: frequency.processors
        }
    }

    pub fn update(&mut self) {
        self.schedulerPolicies = schedulerInfo();
        let cpuUsage = cpuUsage();

        self.averageUsage = cpuUsage.average;
        self.perProcessorUsage = cpuUsage.processors;
    }
}

/// Frequency data structure implements direct conversion for frequencies
/// in various size orders
/// ```rust
/// use sysutil::cpu::cpuFrequency;
/// let frequency = cpuFrequency().average;
///
/// frequency.khz(); // returns the frequency in Kilo Hertz
/// frequency.mhz(); // returns the frequency in Mega Hertz
/// frequency.ghz(); // return the frequency in Giga Hertz
/// ```
#[derive(Debug, Clone)]
pub struct Frequency {
    pub khz: usize
}

impl Frequency {
    pub fn khz(&self) -> f32 {
        return self.khz as f32;
    }

    pub fn mhz(&self) -> f32 {
        return self.khz as f32 / 1000_f32;
    }

    pub fn ghz(&self) -> f32 {
        return self.khz as f32 / 1000_000_f32;
    }
}

/// Contains processor id and its frequency
#[derive(Debug, Clone)]
pub struct ProcessorFrequency {
    pub processorID: String,
    pub frequency: Frequency
}

/// Contains cpu frequencies, both average and processor wise
#[derive(Debug, Clone)]
pub struct CpuFrequency {
    pub average: Frequency,
    pub processors: Vec<ProcessorFrequency>
}

/// Contains currently active clock source and the available ones
#[derive(Debug, Clone)]
pub struct ClockSource {
    pub current: String,
    pub available: Vec<String>
}


/// Contains scheduler information relative to a processor in the system
#[derive(Debug, Clone)]
pub struct SchedulerPolicy {
    pub name: String,
    pub scalingGovernor: String,
    pub scalingDriver: String,
    pub minimumScalingMHz: f32,
    pub maximumScalingMHz: f32,
}

/// Holds data structure for average load
#[derive(Debug)]
pub struct Load {
    pub oneMinute: f32,
    pub fiveMinutes: f32,
    pub fifteenMinutes: f32
}

fn getStats() -> Vec<Vec<usize>> {
    linuxCheck();

    let fileContent = readFile("/proc/stat");

    let lines = fileContent.split("\n");
    let mut strLines = Vec::<String>::new();

    for currentLine in lines {
        if currentLine.contains("cpu") {
            strLines.push(currentLine.to_string());
        }
    }

    let mut uLines = Vec::<Vec<usize>>::new();
    for line in strLines {
        let splittedLine = line.split(' ');
        let mut fixedLine = Vec::<usize>::new();

        for chunk in splittedLine {
            if !chunk.is_empty() && !chunk.contains("cpu") {
                fixedLine.push(chunk.parse().unwrap());
            }
        }
        uLines.push(fixedLine);
    }

    return uLines;
}

/// Returns CPU usage, both average and processor-wise, each value is in percentage
pub fn cpuUsage() -> CpuUsage {
    linuxCheck();

    let before = getStats();
    thread::sleep(Duration::from_millis(250));
    let after = getStats();

    let mut processors = Vec::<ProcessorUsage>::new();
    for i in 0..before.len() {
        let beforeLine = &before[i];
        let beforeSum = {
            let mut sum = 0;

            for element in beforeLine {
                sum += element;
            }
            sum
        };

        let afterLine = &after[i];
        let afterSum = {
            let mut sum = 0;

            for element in afterLine {
                sum += element;
            }
            sum
        };

        let delta: f32 = (afterSum - beforeSum) as f32;

        processors.push(ProcessorUsage {
            total: {
                100_f32 - (afterLine[3] - beforeLine[3]) as f32 * 100_f32 / delta
            },
            user: {
                (afterLine[0] - beforeLine[0]) as f32 * 100_f32 / delta
            },
            nice: {
                (afterLine[1] - beforeLine[1]) as f32 * 100_f32 / delta
            },
            system: {
                (afterLine[2] - beforeLine[2]) as f32 * 100_f32 / delta
            },
            idle: {
                (afterLine[3] - beforeLine[3]) as f32 * 100_f32 / delta
            },
            iowait: {
                (afterLine[4] - beforeLine[4]) as f32 * 100_f32 / delta
            },
            interrupt: {
                (afterLine[5] - beforeLine[5]) as f32 * 100_f32 / delta
            },
            soft_interrupt: {
                (afterLine[6] - beforeLine[6]) as f32 * 100_f32 / delta
            },
        });
    }

    return CpuUsage {
        average: processors[0].clone(),
        processors: processors[1..].to_vec(),
    };
}

/// Returns CPU base information, enclosed in the `CpuInfo` data structure

fn cpuInfo() -> CpuInfo {
    linuxCheck();

    let infoFile = readFile("/proc/cpuinfo");
    let modelName = {
        let mut name = String::new();
        for line in infoFile.split("\n") {
            if line.contains("model name") {
                name = line.split(":").last().unwrap().to_string();
                break;
            }
        }
        name.trim().to_string()
    };

    let baseDir = path::Path::new("/sys/devices/system/cpu");

    let mut coreCount: usize = 0;
    let mut dies = Vec::<usize>::new();

    for processor in fs::read_dir(baseDir).unwrap() {
        let processorPath = processor.unwrap().path();
        let path = processorPath.to_str().unwrap();

        if path.contains("cpu") && !path.contains("cpufreq") && !path.contains("cpuidle")
        {
            let coreId = readFile(format!("{path}/topology/core_id").as_str());
            let dieId = readFile(format!("{path}/topology/die_id").as_str());

            if !coreId.is_empty() {
                match coreId.parse::<usize>() {
                    Err(_) => (),
                    Ok(uCoreId) => {
                        if uCoreId > coreCount {
                            coreCount = uCoreId;
                        }
                    }
                }
            }

            if !dieId.is_empty() {
                match dieId.parse::<usize>() {
                    Err(_) => (),
                    Ok(uDieId) => {
                        if !dies.contains(&uDieId) {
                            dies.push(uDieId);
                        }
                    }
                }
            }
        }
    }

    let dieCount = dies.len();

    if coreCount % 2 == 1 {
        coreCount += 1;
    }

    let cpuInfoFile = readFile("/proc/cpuinfo");
    let threadCount = cpuInfoFile.matches("processor").count();

    let mut governors = Vec::<String>::new();
    let policiesPath = path::Path::new("/sys/devices/system/cpu/cpufreq/");

    let mut maxFrequency: usize = 0;
    let mut clockBoost: Option<bool> = None;

    for dir in fs::read_dir(policiesPath).unwrap() {
        let path = dir.unwrap().path();
        let sPath = path.to_str().unwrap();

        if sPath.contains("policy") {
            let localGovernors = readFile(format!("{sPath}/scaling_available_governors").as_str());
            let maxFreq = readFile(format!("{sPath}/cpuinfo_max_freq").as_str());

            if !maxFreq.is_empty() {
                match maxFreq.parse::<usize>() {
                    Err(_) => (),
                    Ok(freq) => {
                        if freq > maxFrequency {
                            maxFrequency = freq;
                        }
                    }
                }
            }

            for governor in localGovernors.split(" ") {
                if !governors.contains(&governor.to_string()) {
                    governors.push(governor.to_string());
                }
            }
        } else if sPath.contains("boost") {
            let content = readFile(sPath);

            if content.trim() == "1" {
                clockBoost = Some(true);
            } else {
                clockBoost = Some(false);
            }
        }
    }

    let freqMHz = maxFrequency as f32 / 1000_f32;
    let maxInteger: usize = usize::MAX;

    let arch = {
        if maxInteger as u128 == 2_u128.pow(64) - 1 {
            String::from("64 bit")
        } else if maxInteger as u128 == 2_u128.pow(32) - 1 {
            String::from("32 bit")
        } else {
            String::new()
        }
    };

    let pipe = Command::new("sh").arg("-c").args(
        ["echo -n I | od -t o2 | head -n 1 | cut -f 2 -d \" \" | cut -c 6"]
    ).output().unwrap().stdout;

    let byteOrder = match String::from_utf8(pipe).unwrap().trim() {
        "1" => String::from("Little Endian"),
        "0" => String::from("Big Endian"),
        _ => String::new(),
    };

    return CpuInfo {
        modelName: modelName,
        cores: coreCount,
        threads: threadCount,
        dies: dieCount,
        governors: governors,
        maxFrequencyMHz: freqMHz,
        clockBoost: clockBoost,
        architecture: arch,
        byteOrder: byteOrder,
    };
}

/// Returns scheduler information for each processor
pub fn schedulerInfo() -> Vec<SchedulerPolicy> {
    linuxCheck();

    let schedulerDir = path::Path::new("/sys/devices/system/cpu/cpufreq/");
    let mut policies = Vec::<SchedulerPolicy>::new();

    for dir in fs::read_dir(schedulerDir).unwrap() {
        let path = dir.unwrap().path();
        let sPath = path.to_str().unwrap();

        if sPath.contains("policy") {
            let policyName = sPath.split("/").last().unwrap().to_string();

            let scalingGovernor = readFile(format!("{sPath}/scaling_governor").as_str());
            let scalingDriver = readFile(format!("{sPath}/scaling_driver").as_str());

            let maxScalingFrequency = readFile(
                format!("{sPath}/scaling_max_freq").as_str()
            ).parse::<f32>().unwrap() / 1000_f32;

            let minScalingFrequency = readFile(
                format!("{sPath}/scaling_min_freq").as_str()
            ).parse::<f32>().unwrap() / 1000_f32;

            policies.push(SchedulerPolicy {
                name: policyName,
                scalingGovernor: scalingGovernor,
                scalingDriver: scalingDriver,
                minimumScalingMHz: minScalingFrequency,
                maximumScalingMHz: maxScalingFrequency,
            });
        }
    }

    return policies;
}

/// Returns the currently active clock source and the different ones available, enclosed in `ClockSource` struct
pub fn clockSource() -> ClockSource {
    linuxCheck();

    let currentClockSource = readFile(
        "/sys/devices/system/clocksource/clocksource0/current_clocksource"
    );

    let availableClockSources = readFile(
        "/sys/devices/system/clocksource/clocksource0/available_clocksource"
    );

    let mut sources = Vec::<String>::new();
    for source in availableClockSources.split(" ") {
        sources.push(String::from(source));
    }

    ClockSource {
        current: currentClockSource,
        available: sources,
    }
}

/// Returns cpu frequency, both average and processor wise
pub fn cpuFrequency() -> CpuFrequency {
    linuxCheck();
    let mut totalFreq: f32 = 0_f32;
    let mut frequencies: Vec<ProcessorFrequency> = Vec::new();

    let fileContent = readFile("/proc/cpuinfo");
    for chunk in fileContent.split("\n\n") {

        if chunk.is_empty() {
            continue
        }

        let mut id = String::new();
        let mut freq: f32 = 0_f32;

        for line in chunk.split("\n") {
            if line.contains("processor") {
                id = line.trim().split(":").last().unwrap().trim().to_string();

            } else if line.contains("cpu MHz") {
                freq = line.trim().split(":").last().unwrap().trim().parse::<f32>().unwrap();
            }
        }

        if id.is_empty() || freq == 0_f32 {
            continue
        }

        totalFreq += freq;
        frequencies.push(ProcessorFrequency{
            processorID: id,
            frequency: Frequency {
                khz: (freq * 1000.0) as usize
            }
        });
    }

    CpuFrequency {
        average: Frequency {
            khz: (totalFreq * 1000.0) as usize / frequencies.len()
        },
        processors: frequencies
    }
}

/// Returns the average load for the last one, five and fifteen minutes
pub fn getLoad() -> Load {
    let fileContent = readFile("/proc/loadavg");
    let binding = fileContent.split(" ").collect::<Vec<&str>>();

    Load {
        oneMinute: binding.get(0).unwrap().parse::<f32>().unwrap(),
        fiveMinutes: binding.get(1).unwrap().parse::<f32>().unwrap(),
        fifteenMinutes: binding.get(2).unwrap().parse::<f32>().unwrap()
    }
}

/// Returns a HashMap containing the size (in ByteSize) for each cache level
pub fn cacheLevels() -> HashMap<String, ByteSize> {
    let baseDir = "/sys/devices/system/cpu";
    let mut cpus = Vec::<String>::new();

    let pattern = regex::Regex::new(r"cpu[0-9]{1,3}").unwrap();

    for element in std::fs::read_dir(baseDir).unwrap() {
        let name = element.unwrap().file_name().to_str().unwrap().to_string();
        match pattern.captures(&name) {
            Some(_) => {
                cpus.push(name);
            },

            None => {

            }
        }
    }

    let mut cacheLevels = HashMap::<String, Vec<(String, String)>>::new();

    for cpu in cpus {
        let mut cacheDirs = Vec::<String>::new();

        let pattern = regex::Regex::new(r"index[0-9]{1}").unwrap();

        for element in std::fs::read_dir(format!("{}/{}/cache/", baseDir, cpu)).unwrap() {
            let name = element.unwrap().file_name().to_str().unwrap().to_string();
            match pattern.captures(&name) {
                Some(_) => {
                    cacheDirs.push(name);
                },
                None => {}
            }
        }

        for directory in cacheDirs {
            let level = readFile(format!("{}/{}/cache/{}/level", baseDir, cpu, directory));
            let size = readFile(format!("{}/{}/cache/{}/size", baseDir, cpu, directory));
            let shared = readFile(format!("{}/{}/cache/{}/shared_cpu_list", baseDir, cpu, directory));

            if !cacheLevels.contains_key(&level) {
                cacheLevels.insert(level, vec![(size, shared)]);

            } else {
                let mut savedCacheLevel = cacheLevels.get(&level).unwrap().clone();

                let mut found = false;

                for savedLevel in &savedCacheLevel {
                    let (savedSize, savedShared) = savedLevel;
                    if savedShared == &shared {
                        found = true;
                    }
                }

                if !found {
                    cacheLevels.remove(&level);
                    savedCacheLevel.push((size, shared));

                    cacheLevels.insert(level, savedCacheLevel);
                }
            }
        }
    }

    println!("{:?}", cacheLevels);

    let mut levels = HashMap::<String, ByteSize>::new();
    for level in cacheLevels.keys() {
        let mut totalSize: usize = 0;

        let chunks = cacheLevels.get(level);
        for chunk in chunks {
            let (size, _) = chunk.first().unwrap();

            let intSize = {
                if size.contains("K") {
                    size.replace("K", "").parse::<usize>().unwrap() * 1024

                } else if size.contains("M") {
                    size.replace("M", "").parse::<usize>().unwrap() * 1024
                } else {
                    size.parse::<usize>().unwrap()
                }
            };

            totalSize += intSize;
        }

        levels.insert(level.to_string(), ByteSize::fromBytes(totalSize));
    }

    return levels;
}