Skip to content

Commit

Permalink
fix: getting core - socket mapping from apic_id
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetit committed Nov 14, 2023
1 parent 197c84f commit 9f8381e
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 146 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ windows = { version = "0.27.0", features = ["alloc","Win32_Storage_FileSystem","
windows-service = { version = "0.6.0" }
raw-cpuid = { version = "10.5.0" }
core_affinity = { version = "0.8.1"}
x86 = { version = "0.52.0" }

[features]
default = ["prometheus", "riemann", "warpten", "json", "containers", "prometheuspush"]
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ pub fn get_default_sensor() -> impl sensors::Sensor {
);

#[cfg(target_os = "windows")]
return msr_rapl::MsrRAPLSensor::new(
1
);
return msr_rapl::MsrRAPLSensor::new();
}

fn current_system_time_since_epoch() -> Duration {
Expand Down
9 changes: 1 addition & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ struct Cli {
#[cfg(target_os = "linux")]
#[arg(long, default_value_t = powercap_rapl::DEFAULT_BUFFER_PER_SOCKET_MAX_KBYTES)]
sensor_buffer_per_socket_max_kb: u16,

/// Number of physical CPU packages/sockets enabled on the host
#[cfg(target_os = "windows")]
#[arg(long, default_value_t = 1)]
sensor_nb_cpu_sockets: u16,
}

/// Defines the possible subcommands, one per exporter.
Expand Down Expand Up @@ -286,9 +281,7 @@ fn build_sensor(cli: &Cli) -> impl Sensor {

#[cfg(target_os = "windows")]
let msr_sensor_win = || {
msr_rapl::MsrRAPLSensor::new(
cli.sensor_nb_cpu_sockets
)
msr_rapl::MsrRAPLSensor::new()
};

match cli.sensor.as_deref() {
Expand Down
62 changes: 40 additions & 22 deletions src/sensors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl Topology {

let sysinfo_system = System::new_all();
let sysinfo_cores = sysinfo_system.cpus();
warn!("Sysinfo sees {}", sysinfo_cores.len());
#[cfg(target_os = "linux")]
let cpuinfo = CpuInfo::new().unwrap();
for (id, c) in (0_u16..).zip(sysinfo_cores.iter()) {
Expand Down Expand Up @@ -206,7 +207,7 @@ impl Topology {
counter_uj_path: String,
buffer_max_kbytes: u16,
sensor_data: HashMap<String, String>,
) {
) -> Option<CPUSocket> {
if !self.sockets.iter().any(|s| s.id == socket_id) {
let socket = CPUSocket::new(
socket_id,
Expand All @@ -216,10 +217,23 @@ impl Topology {
buffer_max_kbytes,
sensor_data,
);
let res = socket.clone();
self.sockets.push(socket);
Some(res)
} else {
None
}
}

pub fn safe_insert_socket(
&mut self,
socket: CPUSocket
) {
if !self.sockets.iter().any(|s| s.id == socket.id) {
self.sockets.push(socket);
}
}

/// Returns a immutable reference to self.proc_tracker
pub fn get_proc_tracker(&self) -> &ProcessTracker {
&self.proc_tracker
Expand Down Expand Up @@ -302,24 +316,28 @@ impl Topology {
}
}
}
#[cfg(target_os = "windows")]
{
let nb_cores_per_socket = &cores.len() / &self.sockets.len();
warn!("nb_cores_per_socket: {} cores_len: {} sockets_len: {}", nb_cores_per_socket, &cores.len(), &self.sockets.len());
for s in self.sockets.iter_mut().rev() {
for c in 0..nb_cores_per_socket {
match cores.pop() {
Some(core) => {
warn!("adding core {} to socket {}", core.id, s.id);
s.add_cpu_core(core);
},
None => {
error!("Uneven number of CPU cores !");
}
}
}
}
}
//#[cfg(target_os = "windows")]
//{
//TODO: fix
//let nb_sockets = &self.sockets.len();
//let mut socket_counter = 0;
//let nb_cores_per_socket = &cores.len() / nb_sockets;
//warn!("nb_cores_per_socket: {} cores_len: {} sockets_len: {}", nb_cores_per_socket, &cores.len(), &self.sockets.len());
//for s in self.sockets.iter_mut() {
// for c in (socket_counter * nb_cores_per_socket)..((socket_counter+1) * nb_cores_per_socket) {
// match cores.pop() {
// Some(core) => {
// warn!("adding core {} to socket {}", core.id, s.id);
// s.add_cpu_core(core);
// },
// None => {
// error!("Uneven number of CPU cores !");
// }
// }
// }
// socket_counter = socket_counter + 1;
//}
//}
} else {
panic!("Couldn't retrieve any CPU Core from the topology. (generate_cpu_cores)");
}
Expand Down Expand Up @@ -1553,7 +1571,7 @@ mod tests {
#[cfg(target_os = "linux")]
let sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false);
#[cfg(not(target_os = "linux"))]
let sensor = msr_rapl::MsrRAPLSensor::new(1);
let sensor = msr_rapl::MsrRAPLSensor::new();
let topo = (*sensor.get_topology()).unwrap();
println!("{:?}", topo.read_stats());
}
Expand All @@ -1563,7 +1581,7 @@ mod tests {
#[cfg(target_os = "linux")]
let sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false);
#[cfg(not(target_os = "linux"))]
let sensor = msr_rapl::MsrRAPLSensor::new(1);
let sensor = msr_rapl::MsrRAPLSensor::new();
let mut topo = (*sensor.get_topology()).unwrap();
for s in topo.get_sockets() {
for c in s.get_cores() {
Expand All @@ -1577,7 +1595,7 @@ mod tests {
#[cfg(target_os = "linux")]
let sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false);
#[cfg(not(target_os = "linux"))]
let sensor = msr_rapl::MsrRAPLSensor::new(1);
let sensor = msr_rapl::MsrRAPLSensor::new();
let mut topo = (*sensor.get_topology()).unwrap();
for s in topo.get_sockets() {
println!("{:?}", s.read_stats());
Expand Down
Loading

0 comments on commit 9f8381e

Please sign in to comment.