lscpu
: Calculate core/socket count
This commit is contained in:
parent
41bff4fbd6
commit
bc44b86fe7
@ -122,6 +122,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||||||
model_name_info.add_child(CpuInfo::new("Model", &model, None));
|
model_name_info.add_child(CpuInfo::new("Model", &model, None));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let socket_count = &cpu_topology.socket_count();
|
||||||
|
let core_count = &cpu_topology.core_count();
|
||||||
|
model_name_info.add_child(CpuInfo::new(
|
||||||
|
"Core(s) per socket",
|
||||||
|
&(core_count / socket_count).to_string(),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
model_name_info.add_child(CpuInfo::new("Socket(s)", &socket_count.to_string(), None));
|
||||||
|
|
||||||
if let Some(freq_boost_enabled) = sysfs::read_freq_boost_state() {
|
if let Some(freq_boost_enabled) = sysfs::read_freq_boost_state() {
|
||||||
let s = if freq_boost_enabled {
|
let s = if freq_boost_enabled {
|
||||||
"enabled"
|
"enabled"
|
||||||
@ -131,12 +140,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||||||
model_name_info.add_child(CpuInfo::new("Frequency boost", s, None));
|
model_name_info.add_child(CpuInfo::new("Frequency boost", s, None));
|
||||||
}
|
}
|
||||||
|
|
||||||
model_name_info.add_child(CpuInfo::new(
|
|
||||||
"Socket(s)",
|
|
||||||
&cpu_topology.socket_count().to_string(),
|
|
||||||
None,
|
|
||||||
));
|
|
||||||
|
|
||||||
vendor_info.add_child(model_name_info);
|
vendor_info.add_child(model_name_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ pub struct CpuTopology {
|
|||||||
pub struct Cpu {
|
pub struct Cpu {
|
||||||
_index: usize,
|
_index: usize,
|
||||||
pub pkg_id: usize,
|
pub pkg_id: usize,
|
||||||
|
pub core_id: usize,
|
||||||
pub caches: Vec<CpuCache>,
|
pub caches: Vec<CpuCache>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +42,13 @@ impl CpuTopology {
|
|||||||
for cpu_index in online_cpus {
|
for cpu_index in online_cpus {
|
||||||
let cpu_dir = PathBuf::from(format!("/sys/devices/system/cpu/cpu{}/", cpu_index));
|
let cpu_dir = PathBuf::from(format!("/sys/devices/system/cpu/cpu{}/", cpu_index));
|
||||||
|
|
||||||
let physical_pkg_id = fs::read_to_string(cpu_dir.join("topology/physical_package_id"))
|
let pkg_id = fs::read_to_string(cpu_dir.join("topology/physical_package_id"))
|
||||||
|
.unwrap()
|
||||||
|
.trim()
|
||||||
|
.parse::<usize>()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let core_id = fs::read_to_string(cpu_dir.join("topology/core_id"))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.trim()
|
.trim()
|
||||||
.parse::<usize>()
|
.parse::<usize>()
|
||||||
@ -51,7 +58,8 @@ impl CpuTopology {
|
|||||||
|
|
||||||
out.push(Cpu {
|
out.push(Cpu {
|
||||||
_index: cpu_index,
|
_index: cpu_index,
|
||||||
pkg_id: physical_pkg_id,
|
pkg_id,
|
||||||
|
core_id,
|
||||||
caches,
|
caches,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -61,10 +69,15 @@ impl CpuTopology {
|
|||||||
pub fn socket_count(&self) -> usize {
|
pub fn socket_count(&self) -> usize {
|
||||||
// Each physical socket is represented as its own package_id, so amount of unique pkg_ids = sockets
|
// Each physical socket is represented as its own package_id, so amount of unique pkg_ids = sockets
|
||||||
// https://www.kernel.org/doc/html/latest/admin-guide/abi-stable.html#abi-sys-devices-system-cpu-cpux-topology-physical-package-id
|
// https://www.kernel.org/doc/html/latest/admin-guide/abi-stable.html#abi-sys-devices-system-cpu-cpux-topology-physical-package-id
|
||||||
let physical_sockets: HashSet<usize> = self.cpus.iter().map(|cpu| cpu.pkg_id).collect();
|
let physical_sockets: HashSet<_> = self.cpus.iter().map(|cpu| cpu.pkg_id).collect();
|
||||||
|
|
||||||
physical_sockets.len()
|
physical_sockets.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn core_count(&self) -> usize {
|
||||||
|
let core_ids: HashSet<_> = self.cpus.iter().map(|cpu| cpu.core_id).collect();
|
||||||
|
core_ids.len()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: respect `--hex` option and output the bitmask instead of human-readable range
|
// TODO: respect `--hex` option and output the bitmask instead of human-readable range
|
||||||
|
Loading…
x
Reference in New Issue
Block a user