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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// const CPUINFO: &str = "/proc/cpuinfo";
// const MEMINFO: &str = "/proc/meminfo";
// freq 频率
// freq_unit 单位
// export LC_ALL=C; lscpu; unset LC_ALL
// top -bn 1 -i -c // https://www.cnblogs.com/gongchixin/articles/7998054.html
// fn get_num_physical_cpus() -> usize {
// use std::io::BufReader;
// use std::io::BufRead;
// use std::fs::File;
// use std::collections::HashSet;
// let file = match File::open("/proc/cpuinfo") {
// Ok(val) => val,
// Err(_) => {return get_num_cpus()},
// };
// let reader = BufReader::new(file);
// let mut set = HashSet::new();
// let mut coreid: u32 = 0;
// let mut physid: u32 = 0;
// let mut chgcount = 0;
// for line in reader.lines().filter_map(|result| result.ok()) {
// let parts: Vec<&str> = line.split(':').map(|s| s.trim()).collect();
// if parts.len() != 2 {
// continue
// }
// if parts[0] == "core id" || parts[0] == "physical id" {
// let value = match parts[1].trim().parse() {
// Ok(val) => val,
// Err(_) => break,
// };
// match parts[0] {
// "core id" => coreid = value,
// "physical id" => physid = value,
// _ => {},
// }
// chgcount += 1;
// }
// if chgcount == 2 {
// set.insert((physid, coreid));
// chgcount = 0;
// }
// }
// let count = set.len();
// if count == 0 { get_num_cpus() } else { count }
// }
// fn cpuinfo() -> io::Result<(String, usize, f64)> {
// let mut file = File::open(CPUINFO)?;
// let mut contents = String::new();
// file.read_to_string(&mut contents)?;
// contents = contents.replace("\t", "");
// let parts: Vec<&str> = contents.split("\n\n").filter(|p| p.len() != 0).collect();
// let cpu_num = parts.len();
// let mut cpu_name = "";
// let mut mhz = 0f64;
// for part in parts {
// let lines: Vec<&str> = part.split("\n").collect();
// // let temp =
// for line in lines {
// let value = line.split(':').map(|s| s.trim()).collect::<Vec<&str>>();
// // println!("{:?}", value);
// if value[0] == "cpu MHz" {
// mhz += value[1].parse::<f64>().unwrap();
// // println!("{:?}", mhz);
// }
// if value[0] == "model name" {
// cpu_name = value[1];
// }
// }
// }
// println!("{:?}", mhz / cpu_num as f64);
// println!("{:?}", cpu_name);
// return Ok((cpu_name.to_string(), cpu_num, mhz / cpu_num as f64))
// }
/*
use std::io;
#[derive(Debug, Default)]
pub struct CpuInfo {
pub architecture: String,
pub cpu_op_modes: String,
pub byte_order: String,
pub cpus: String,
pub on_line: String,
pub threads_per_core: String,
pub cores_per_socket: String,
pub sockets: String,
pub numa_nodes: String,
pub verdor_id: String,
pub cpu_family: String,
pub model: String,
pub model_name: String,
pub stepping: String,
pub cpu_mhz: String,
pub cpu_max_mhz: String,
pub cpu_min_mhz: String,
pub bogo_mips: String,
pub virtualization: String,
pub l1d_cache: String,
pub l1i_cache: String,
pub l2_cache: String,
pub l3_cache: String,
pub numa_node0_cpus: String,
pub flags: String
}
pub fn cpuinfo() -> io::Result<Option<CpuInfo>>{
use std::process::Command;
let lscpu = Command::new("lscpu").env("LC_ALL", "C").output();
let lscpu = match lscpu {
Ok(l) => l,
Err(_) => return Ok(None)
};
if !lscpu.status.success() {
return Ok(None)
}
let lscpu = String::from_utf8_lossy(&lscpu.stdout).to_string();
let lines: Vec<&str> = lscpu.split("\n").collect();
let mut cpuinfo = CpuInfo::default();
for line in lines {
let parts = line.split(':').map(|s| s.trim()).collect::<Vec<&str>>();
if parts.len() < 2 {
continue;
}
match parts[0] {
"Architecture" => cpuinfo.architecture = parts[1].to_string(),
"CPU op-mode(s)" => cpuinfo.cpu_op_modes = parts[1].to_string(),
"Byte Order" => cpuinfo.byte_order = parts[1].to_string(),
"CPU(s)" => cpuinfo.cpus = parts[1].to_string(),
"On-line CPU(s) list" => cpuinfo.on_line = parts[1].to_string(),
"Thread(s) per core" => cpuinfo.threads_per_core = parts[1].to_string(),
"Core(s) per socket" => cpuinfo.cores_per_socket = parts[1].to_string(),
"Socket(s)" => cpuinfo.sockets = parts[1].to_string(),
"NUMA node(s)" => cpuinfo.numa_nodes = parts[1].to_string(),
"Vendor ID" => cpuinfo.verdor_id = parts[1].to_string(),
"CPU family" => cpuinfo.cpu_family = parts[1].to_string(),
"Model" => cpuinfo.model = parts[1].to_string(),
"Model name" => cpuinfo.model_name = parts[1].to_string(),
"Stepping" => cpuinfo.model_name = parts[1].to_string(),
"CPU MHz" => cpuinfo.cpu_mhz = parts[1].to_string(),
"CPU max MHz" => cpuinfo.cpu_max_mhz = parts[1].to_string(),
"CPU min MHz" => cpuinfo.cpu_min_mhz = parts[1].to_string(),
"BogoMIPS" => cpuinfo.bogo_mips = parts[1].to_string(),
"Virtualization" => cpuinfo.virtualization = parts[1].to_string(),
"L1d cache" => cpuinfo.l1d_cache = parts[1].to_string(),
"L1i cache" => cpuinfo.l1i_cache = parts[1].to_string(),
"L2 cache" => cpuinfo.l2_cache = parts[1].to_string(),
"L3 cache" => cpuinfo.l3_cache = parts[1].to_string(),
"NUMA node0 CPU(s)" => cpuinfo.numa_node0_cpus = parts[1].to_string(),
"Flags" => cpuinfo.flags = parts[1].to_string(),
_ => ()
}
}
return Ok(Some(cpuinfo))
}
pub fn sysinfo() {
let mut sysinfo: libc::sysinfo = unsafe { std::mem::zeroed() };
unsafe { libc::sysinfo(&mut sysinfo as *mut _); }
}
// cat /proc/loadavg
pub fn loadavg() {
}
pub fn uptime() {
}
*/