Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: more information about the system in linux #783

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/benchmark/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod benchmark_result;
pub mod executor;
#[cfg(target_os = "linux")]
pub mod os_info;
pub mod relative_speed;
pub mod scheduler;
pub mod timing_result;
Expand All @@ -20,6 +22,8 @@ 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(target_os = "linux")]
use os_info::OsInfo;
use timing_result::TimingResult;

use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -385,6 +389,13 @@ impl<'a> Benchmark<'a> {
max_str.purple(),
num_str.dimmed()
);

let os_name = std::env::consts::OS;

println!(" OS: {}", os_name.green());

#[cfg(target_os = "linux")]
self.linux_extra_inforamtion()
}
}

Expand Down Expand Up @@ -460,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());
}
}
}
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(target_os = "linux")]
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
}
}
Loading