1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

fix(ext/node): Fix os.cpus() on Linux (#27592)

Populate `speed` using current scaling frequency and fix times
multiplier.

Fixes https://github.com/denoland/deno/issues/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>
This commit is contained in:
Divy Srivastava 2025-01-08 22:09:55 +05:30 committed by GitHub
parent e233173653
commit fffa3804aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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()?;
@ -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;
}