Skip to content

Commit

Permalink
fix(ext/node): Fix os.cpus() on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Jan 8, 2025
1 parent 9115070 commit b6398ab
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions ext/node/ops/os/cpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,16 @@ pub fn cpu_info() -> Option<Vec<CpuInfo>> {
let nice = fields.next()?.parse::<u64>().ok()?;
let sys = fields.next()?.parse::<u64>().ok()?;
let idle = fields.next()?.parse::<u64>().ok()?;
let _iowait = fields.next()?.parse::<u64>().ok()?;
let irq = fields.next()?.parse::<u64>().ok()?;

cpus[i].times.user = user;
cpus[i].times.nice = nice;
cpus[i].times.sys = sys;
cpus[i].times.idle = idle;
cpus[i].times.irq = irq;
// sysconf(_SC_CLK_TCK) is fixed at 100 Hz, therefore the
// multiplier is always 1000/100 = 10
cpus[i].times.user = user * 10;
cpus[i].times.nice = nice * 10;
cpus[i].times.sys = sys * 10;
cpus[i].times.idle = idle * 10;
cpus[i].times.irq = irq * 10;
}

let fp = std::fs::File::open("/proc/cpuinfo").ok()?;
Expand All @@ -287,6 +290,18 @@ pub fn cpu_info() -> Option<Vec<CpuInfo>> {
let model = fields.next()?.trim();

cpus[j].model = model.to_string();

if let Ok(fp) = std::fs::File::open(format!(
"/sys/devices/system/cpu/cpu{}/cpufreq/scaling_cur_freq",
j
)) {
let mut reader = std::io::BufReader::new(fp);
let mut speed = String::new();
reader.read_line(&mut speed).ok()?;

cpus[j].speed = speed.trim().parse::<u64>().ok()? / 1000;
}

j += 1;
}

Expand Down

0 comments on commit b6398ab

Please sign in to comment.