Skip to content

Commit

Permalink
Use sysconf to get the total online CPUs
Browse files Browse the repository at this point in the history
num_cpus does not return the total number of CPUs if aperf is taskset.
Use sysconf to get the total number of online CPUs.
  • Loading branch information
janaknat committed Jun 3, 2024
1 parent ac94cf1 commit 49b553b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ strum = "0.24"
strum_macros = "0.24"
sysctl = "*"
perf-event2 = "0.7.2"
num_cpus = "1.16"
libc = "0.2"
flate2 = "1.0.30"
tar = "0.4.40"
Expand Down
12 changes: 9 additions & 3 deletions src/data/perf_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate ctor;

use crate::data::{CollectData, CollectorParams, Data, DataType, ProcessedData, TimeEnum};
use crate::visualizer::{DataVisualizer, GetData, GraphLimitType, GraphMetadata};
use crate::{PERFORMANCE_DATA, VISUALIZATION_DATA};
use crate::{PDError, PERFORMANCE_DATA, VISUALIZATION_DATA};
use anyhow::Result;
use chrono::prelude::*;
use ctor::ctor;
Expand All @@ -19,7 +19,7 @@ use crate::data::grv_perf_events;
use {
crate::data::intel_icelake_perf_events::ICX_CTRS, crate::data::intel_perf_events,
crate::data::intel_sapphire_rapids_perf_events::SPR_CTRS, crate::data::utils::get_cpu_info,
crate::PDError, indexmap::IndexMap,
indexmap::IndexMap,
};

pub static PERF_STAT_FILE_NAME: &str = "perf_stat";
Expand Down Expand Up @@ -113,7 +113,13 @@ impl PerfStatRaw {

impl CollectData for PerfStatRaw {
fn prepare_data_collector(&mut self, _params: CollectorParams) -> Result<()> {
let num_cpus = num_cpus::get();
let num_cpus = match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN as libc::c_int) } {
-1 => {
warn!("Could not get the number of cpus in the system with sysconf.");
return Err(PDError::CollectorPMUCPUError.into());
}
ret => ret as usize,
};
let mut cpu_groups: Vec<CpuCtrGroup> = Vec::new();

cfg_if::cfg_if! {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub enum PDError {
#[error("All processes collection error")]
CollectorAllProcessError,

#[error("Could not get the total number of online CPUs with sysconf")]
CollectorPMUCPUError,

#[error("Generating report from other reports. Name must be given.")]
VisualizerReportFromReportNoNameError,

Expand Down

0 comments on commit 49b553b

Please sign in to comment.