Skip to content

Commit

Permalink
fix(ext/node): Fix os.cpus() on Linux (#27592)
Browse files Browse the repository at this point in the history
Populate `speed` using current scaling frequency and fix times
multiplier.

Fixes #27555

<table>
<tr>
<th>Node.js</th>
<th>Deno</th>
</tr>
<tr>
<td>

```
> os.cpus()
[
  {
    model: 'AMD Ryzen 5 7530U with Radeon Graphics',
    speed: 1396,
    times: {
      user: 1769930,
      nice: 20,
      sys: 525630,
      idle: 41325700,
      irq: 110060
    }
  },
```

</td>
<td>

```
> os.cpus()
[
  {
    model: "AMD Ryzen 5 7530U with Radeon Graphics",
    speed: 1630,
    times: [Object: null prototype] {
      user: 1795620,
      nice: 20,
      sys: 537840,
      idle: 41589390,
      irq: 111230
    }
  },
```

</td>
</tr>
</table>
  • Loading branch information
littledivy authored Jan 8, 2025
1 parent e233173 commit fffa380
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 fffa380

Please sign in to comment.