Skip to content

Commit

Permalink
Add get_vendor_id function
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Cameron <[email protected]>
  • Loading branch information
Nick Cameron committed Jan 23, 2020
1 parent b57d44d commit 1473e52
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub use self::component::Component;
pub use self::disk::{Disk, DiskType};
pub use self::network::NetworkData;
pub use self::process::{Process, ProcessStatus};
pub use self::processor::{get_avg_load, get_cpu_frequency, Processor};
pub use self::processor::{get_avg_load, get_cpu_frequency, get_vendor_id, Processor};
pub use self::system::System;
13 changes: 13 additions & 0 deletions src/linux/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,19 @@ pub fn get_cpu_frequency() -> u64 {
.unwrap_or_default()
}

/// Returns the brand/vendor string for the first CPU (which should be the same for all CPUs).
pub fn get_vendor_id() -> String {
let mut s = String::new();
if let Err(_) = File::open("/proc/cpuinfo").and_then(|mut f| f.read_to_string(&mut s)) {
return String::new();
}

s.split('\n').find(|line| line.starts_with("vendor_id\t"))
.and_then(|line| line.split(':').last())
.map(|s| s.trim().to_owned())
.unwrap_or_default()
}

/// get_avg_load returns the system load average value.
pub fn get_avg_load() -> LoadAvg {
let mut s = String::new();
Expand Down
2 changes: 1 addition & 1 deletion src/mac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ pub use self::component::Component;
pub use self::disk::{Disk, DiskType};
pub use self::network::NetworkData;
pub use self::process::{Process, ProcessStatus};
pub use self::processor::{get_avg_load, get_cpu_frequency, Processor};
pub use self::processor::{get_avg_load, get_cpu_frequency, get_vendor_id, Processor};
pub use self::system::System;
6 changes: 6 additions & 0 deletions src/mac/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ pub fn get_cpu_frequency() -> u64 {
speed
}

/// Returns the brand/vendor string for the first CPU (which should be the same for all CPUs).
pub fn get_vendor_id() -> String {
// TODO: support Mac
"".to_owned()
}

/// get_avg_load returns the system load average value.
pub fn get_avg_load() -> LoadAvg {
let loads = vec![0f64; 3];
Expand Down
8 changes: 6 additions & 2 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub use net::NICLoad;
pub use num_cpus::{get as get_logical_cores, get_physical as get_physical_cores};

pub use sys::{
get_avg_load, get_cpu_frequency, Component, Disk, DiskType, NetworkData, Process,
get_avg_load, get_cpu_frequency, get_vendor_id, Component, Disk, DiskType, NetworkData, Process,
ProcessStatus, Processor, System,
};
pub use traits::{ComponentExt, DiskExt, NetworkExt, ProcessExt, ProcessorExt, SystemExt};
Expand Down Expand Up @@ -264,7 +264,7 @@ pub fn get_sysctl_list() -> HashMap<String, String> {

#[cfg(test)]
mod test {
use traits::{ProcessExt, SystemExt};
use super::*;

#[test]
fn check_memory_usage() {
Expand Down Expand Up @@ -307,6 +307,10 @@ mod test {

#[test]
fn test_cache_size() {
if get_vendor_id() == "AuthenticAMD" {
return;
}

let caches = vec![
("l1-cache-size", ::cache_size::l1_cache_size()),
("l1-cache-line-size", ::cache_size::l1_cache_line_size()),
Expand Down
2 changes: 1 addition & 1 deletion src/unknown/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub use self::component::Component;
pub use self::disk::{Disk, DiskType};
pub use self::network::NetworkData;
pub use self::process::{Process, ProcessStatus};
pub use self::processor::{get_avg_load, get_cpu_frequency, Processor};
pub use self::processor::{get_avg_load, get_cpu_frequency, get_vendor_id, Processor};
pub use self::system::System;
5 changes: 5 additions & 0 deletions src/unknown/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ pub fn get_cpu_frequency() -> u64 {
0
}

/// Returns the brand/vendor string for the first CPU (which should be the same for all CPUs).
pub fn get_vendor_id() -> String {
"".to_owned()
}

/// get_avg_load returns the system load average value.
pub fn get_avg_load() -> LoadAvg {
LoadAvg::default()
Expand Down
2 changes: 1 addition & 1 deletion src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ pub use self::component::Component;
pub use self::disk::{Disk, DiskType};
pub use self::network::NetworkData;
pub use self::process::{Process, ProcessStatus};
pub use self::processor::{get_avg_load, get_cpu_frequency, Processor};
pub use self::processor::{get_avg_load, get_cpu_frequency, get_vendor_id, Processor};
pub use self::system::System;
6 changes: 6 additions & 0 deletions src/windows/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ pub fn get_cpu_frequency() -> u64 {
0
}

/// Returns the brand/vendor string for the first CPU (which should be the same for all CPUs).
pub fn get_vendor_id() -> String {
// TODO: support windows
"".to_owned()
}

/// get_avg_load returns the system load average value.
pub fn get_avg_load() -> LoadAvg {
LoadAvg::default()
Expand Down

0 comments on commit 1473e52

Please sign in to comment.