From 63381dc92ef8cff0686564c988abec06c4679358 Mon Sep 17 00:00:00 2001 From: abdulfatah mohammed sheikh Date: Mon, 9 Dec 2024 23:36:49 +0300 Subject: [PATCH 1/4] added: extra inforation for linux os --- src/benchmark/mod.rs | 17 +++++++++ src/benchmark/os_info.rs | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/benchmark/os_info.rs diff --git a/src/benchmark/mod.rs b/src/benchmark/mod.rs index 2cca85736..61db25fb9 100644 --- a/src/benchmark/mod.rs +++ b/src/benchmark/mod.rs @@ -1,5 +1,6 @@ pub mod benchmark_result; pub mod executor; +pub mod os_info; pub mod relative_speed; pub mod scheduler; pub mod timing_result; @@ -20,6 +21,7 @@ use crate::util::exit_code::extract_exit_code; use crate::util::min_max::{max, min}; use crate::util::units::Second; use benchmark_result::BenchmarkResult; +use os_info::OsInfo; use timing_result::TimingResult; use anyhow::{anyhow, Result}; @@ -385,6 +387,21 @@ impl<'a> Benchmark<'a> { max_str.purple(), num_str.dimmed() ); + + let os_name = std::env::consts::OS; + + println!(" OS: {}", os_name.green()); + + if cfg!(target_os = "linux") { + let kernal_version = OsInfo::kernal_version(); + let (disro_name, disro_version) = OsInfo::distro_info(); + let number_of_cores = OsInfo::number_of_cores(); + + println!(" Kernal version: {}", kernal_version.green()); + println!(" Distro: {}", disro_name.green()); + println!(" Distro Version: {}", disro_version.green()); + println!(" Cores: {}", number_of_cores.to_string().green()); + } } } diff --git a/src/benchmark/os_info.rs b/src/benchmark/os_info.rs new file mode 100644 index 000000000..0938c307a --- /dev/null +++ b/src/benchmark/os_info.rs @@ -0,0 +1,77 @@ +#![cfg(unix)] +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::str::from_utf8; + +pub struct OsInfo(); +const PROC: &str = "proc"; + +impl OsInfo { + /// The first element is the name of the distro and the second is the version + pub fn distro_info() -> (String, String) { + let file = File::open("/etc/os-release").expect("could not open version dir"); + let reader = BufReader::new(file); + let mut counter = 0; + let mut name = String::from(""); + let mut version = String::from(""); + + for line in reader.lines() { + if counter == 2 { + break; + } + + match line { + Ok(data) => { + let text: Vec<_> = data.split('=').collect(); + + if text[0].to_lowercase().trim() == "name" { + name = text[1].to_string(); + counter += 1; + } else if text[0].to_lowercase().trim() == "version" { + version = text[1][1..text[1].len() - 1].to_string(); + counter += 1; + } + } + Err(_) => {} + } + } + + (name, version) + } + + pub fn kernal_version() -> String { + let dir = format!("/{}/version", PROC); + + let file = File::open(dir).expect("could not open version dir"); + let mut reader = BufReader::new(file); + let mut buf = vec![]; + let _ = reader.read_until(b'(', &mut buf); + + let start = "Linux version ".len(); + let end = buf.len() - 2; + let version = from_utf8(&buf[start..end]).expect("Failed to convert bytes to string"); + + version.to_string() + } + + pub fn number_of_cores() -> u8 { + let dir = format!("/{}/cpuinfo", PROC); + let file = File::open(dir).expect("could not open version dir"); + let reader = BufReader::new(file); + let mut counter: u8 = 0; + + for line in reader.lines() { + match line { + Ok(data) => { + let text: Vec<_> = data.split(":").collect(); + + if text[0].trim() == "processor" { + counter += 1; + } + } + Err(_) => {} + } + } + counter + } +} From c181c681aa8bbe783774465af35c610dbbc0fbf3 Mon Sep 17 00:00:00 2001 From: abdulfatah mohammed sheikh Date: Mon, 9 Dec 2024 23:47:19 +0300 Subject: [PATCH 2/4] fixed: unresolved import `os_info` in windows --- src/benchmark/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/benchmark/mod.rs b/src/benchmark/mod.rs index 61db25fb9..cfa636e46 100644 --- a/src/benchmark/mod.rs +++ b/src/benchmark/mod.rs @@ -1,5 +1,6 @@ pub mod benchmark_result; pub mod executor; +#[cfg(unix)] pub mod os_info; pub mod relative_speed; pub mod scheduler; @@ -21,6 +22,7 @@ use crate::util::exit_code::extract_exit_code; use crate::util::min_max::{max, min}; use crate::util::units::Second; use benchmark_result::BenchmarkResult; +#[cfg(unix)] use os_info::OsInfo; use timing_result::TimingResult; From 519bda57bf6d9ea743f5a41bd78ca9e078650279 Mon Sep 17 00:00:00 2001 From: abdulfatah mohammed sheikh Date: Tue, 10 Dec 2024 00:12:51 +0300 Subject: [PATCH 3/4] changed: target from unix to linux --- src/benchmark/mod.rs | 30 ++++++++++++++++++------------ src/benchmark/os_info.rs | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/benchmark/mod.rs b/src/benchmark/mod.rs index cfa636e46..8915c6ad2 100644 --- a/src/benchmark/mod.rs +++ b/src/benchmark/mod.rs @@ -1,6 +1,6 @@ pub mod benchmark_result; pub mod executor; -#[cfg(unix)] +#[cfg(target_os = "linux")] pub mod os_info; pub mod relative_speed; pub mod scheduler; @@ -22,7 +22,7 @@ use crate::util::exit_code::extract_exit_code; use crate::util::min_max::{max, min}; use crate::util::units::Second; use benchmark_result::BenchmarkResult; -#[cfg(unix)] +#[cfg(target_os = "linux")] use os_info::OsInfo; use timing_result::TimingResult; @@ -394,16 +394,8 @@ impl<'a> Benchmark<'a> { println!(" OS: {}", os_name.green()); - if cfg!(target_os = "linux") { - let kernal_version = OsInfo::kernal_version(); - let (disro_name, disro_version) = OsInfo::distro_info(); - let number_of_cores = OsInfo::number_of_cores(); - - println!(" Kernal version: {}", kernal_version.green()); - println!(" Distro: {}", disro_name.green()); - println!(" Distro Version: {}", disro_version.green()); - println!(" Cores: {}", number_of_cores.to_string().green()); - } + #[cfg(unix)] + self.linux_extra_inforamtion() } } @@ -479,4 +471,18 @@ impl<'a> Benchmark<'a> { .collect(), }) } + + #[cfg(target_os = "linux")] + fn linux_extra_inforamtion(&self) { + if cfg!(target_os = "linux") { + let kernal_version = OsInfo::kernal_version(); + let (disro_name, disro_version) = OsInfo::distro_info(); + let number_of_cores = OsInfo::number_of_cores(); + + println!(" Kernal version: {}", kernal_version.green()); + println!(" Distro: {}", disro_name.green()); + println!(" Distro Version: {}", disro_version.green()); + println!(" Cores: {}", number_of_cores.to_string().green()); + } + } } diff --git a/src/benchmark/os_info.rs b/src/benchmark/os_info.rs index 0938c307a..c9128d03c 100644 --- a/src/benchmark/os_info.rs +++ b/src/benchmark/os_info.rs @@ -1,4 +1,4 @@ -#![cfg(unix)] +#![cfg(target_os = "linux")] use std::fs::File; use std::io::{BufRead, BufReader}; use std::str::from_utf8; From 1038f437e1b601944d92f249debecff4433233d0 Mon Sep 17 00:00:00 2001 From: abdulfatah mohammed sheikh Date: Tue, 10 Dec 2024 00:28:20 +0300 Subject: [PATCH 4/4] added: target linux to function difination --- src/benchmark/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/benchmark/mod.rs b/src/benchmark/mod.rs index 8915c6ad2..d7026c439 100644 --- a/src/benchmark/mod.rs +++ b/src/benchmark/mod.rs @@ -394,7 +394,7 @@ impl<'a> Benchmark<'a> { println!(" OS: {}", os_name.green()); - #[cfg(unix)] + #[cfg(target_os = "linux")] self.linux_extra_inforamtion() } }