Skip to content

Commit

Permalink
added: extra inforation for linux os
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulfatahMohammedSheikh committed Dec 9, 2024
1 parent f78f482 commit 63381dc
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/benchmark/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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());
}
}
}

Expand Down
77 changes: 77 additions & 0 deletions src/benchmark/os_info.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 63381dc

Please sign in to comment.