-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinfo.rs
More file actions
148 lines (133 loc) · 4.17 KB
/
info.rs
File metadata and controls
148 lines (133 loc) · 4.17 KB
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
use std::process::Command;
use serde::{Deserialize, Serialize};
use sysinfo::{CpuRefreshKind, MemoryRefreshKind, RefreshKind, System};
use crate::prelude::*;
fn get_user() -> Result<String> {
let user_output = Command::new("whoami")
.output()
.map_err(|_| anyhow!("Failed to get user info"))?;
if !user_output.status.success() {
bail!("Failed to get user info");
}
let output_str =
String::from_utf8(user_output.stdout).map_err(|_| anyhow!("Failed to parse user info"))?;
Ok(output_str.trim().to_string())
}
#[derive(Eq, PartialEq, Hash, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SystemInfo {
pub os: String,
pub os_version: String,
pub arch: String,
pub host: String,
pub user: String,
pub cpu_brand: String,
pub cpu_name: String,
pub cpu_vendor_id: String,
pub cpu_cores: usize,
pub total_memory_gb: u64,
pub cpu_flags: Vec<String>,
}
#[cfg(test)]
impl SystemInfo {
pub fn test() -> Self {
SystemInfo {
os: "ubuntu".to_string(),
os_version: "20.04".to_string(),
arch: "x86_64".to_string(),
host: "host".to_string(),
user: "user".to_string(),
cpu_brand: "Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz".to_string(),
cpu_name: "cpu0".to_string(),
cpu_vendor_id: "GenuineIntel".to_string(),
cpu_cores: 2,
total_memory_gb: 8,
cpu_flags: vec![
"sse2".to_string(),
"avx".to_string(),
"avx2".to_string(),
"erms".to_string(),
],
}
}
}
#[cfg(target_os = "linux")]
fn get_cpu_flags() -> Vec<String> {
use procfs::Current;
let cpuinfo = match procfs::CpuInfo::current() {
Ok(cpuinfo) => cpuinfo,
Err(e) => {
warn!("Failed to read /proc/cpuinfo: {e}");
return Vec::new();
}
};
// /proc/cpuinfo uses "flags" on x86_64 and "Features" on aarch64
let field_name = if cfg!(target_arch = "x86_64") {
"flags"
} else if cfg!(target_arch = "aarch64") {
"Features"
} else {
return Vec::new();
};
let mut flags: Vec<String> = match cpuinfo.get_field(0, field_name) {
Some(value) => value.split_whitespace().map(|s| s.to_string()).collect(),
None => {
warn!("No CPU flags found in /proc/cpuinfo (field: {field_name})");
return Vec::new();
}
};
flags.sort();
flags
}
#[cfg(not(target_os = "linux"))]
fn get_cpu_flags() -> Vec<String> {
Vec::new()
}
impl SystemInfo {
pub fn new() -> Result<Self> {
let os = System::distribution_id();
let os_version = System::os_version().ok_or(anyhow!("Failed to get OS version"))?;
let arch = System::cpu_arch();
let user = get_user()?;
let host = System::host_name().ok_or(anyhow!("Failed to get host name"))?;
let s = System::new_with_specifics(
RefreshKind::nothing()
.with_cpu(CpuRefreshKind::everything())
.with_memory(MemoryRefreshKind::everything()),
);
let cpu_cores = s
.physical_core_count()
.ok_or(anyhow!("Failed to get CPU core count"))?;
let total_memory_gb = s.total_memory().div_ceil(1024_u64.pow(3));
// take the first CPU to get the brand, name and vendor id
let cpu = s
.cpus()
.iter()
.next()
.ok_or(anyhow!("Failed to get CPU info"))?;
let cpu_brand = {
let brand = cpu.brand().to_string();
if brand.is_empty() {
"unknown".to_string()
} else {
brand
}
};
let cpu_name = cpu.name().to_string();
let cpu_vendor_id = cpu.vendor_id().to_string();
let cpu_flags = get_cpu_flags();
Ok(SystemInfo {
os,
os_version,
arch,
host,
user,
cpu_brand,
cpu_name,
cpu_vendor_id,
cpu_cores,
total_memory_gb,
cpu_flags,
})
}
}