summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-02-06 16:27:42 -0700
committerGitHub <noreply@github.com>2024-02-06 16:27:42 -0700
commit33d12c1d5774afc575b371083c1e72e8db03edea (patch)
tree10e8a54eacf99f6403acd85c74e71f8aeeed37fb
parent9955cbdb56d5aa7882048e06709695a137d2e3a5 (diff)
fix(ext/node): Ensure os.cpus() works on arm64 linux (#22302)
`/proc/cpuinfo` on ARM doesn't have the model name per CPU, so we leave those as "unknown".
-rw-r--r--ext/node/ops/os/cpus.rs15
1 files changed, 11 insertions, 4 deletions
diff --git a/ext/node/ops/os/cpus.rs b/ext/node/ops/os/cpus.rs
index 6c852dce7..bf83f7e7d 100644
--- a/ext/node/ops/os/cpus.rs
+++ b/ext/node/ops/os/cpus.rs
@@ -245,11 +245,13 @@ pub fn cpu_info() -> Option<Vec<CpuInfo>> {
let fp = std::fs::File::open("/proc/stat").ok()?;
let reader = std::io::BufReader::new(fp);
+ let mut count = 0;
for (i, line) in reader.lines().enumerate() {
let line = line.ok()?;
if !line.starts_with("cpu") {
break;
}
+ count = i;
let mut fields = line.split_whitespace();
fields.next()?;
let user = fields.next()?.parse::<u64>().ok()?;
@@ -268,7 +270,7 @@ pub fn cpu_info() -> Option<Vec<CpuInfo>> {
let fp = std::fs::File::open("/proc/cpuinfo").ok()?;
let reader = std::io::BufReader::new(fp);
- let mut i = 0;
+ let mut j = 0;
for line in reader.lines() {
let line = line.ok()?;
if !line.starts_with("model name") {
@@ -278,11 +280,16 @@ pub fn cpu_info() -> Option<Vec<CpuInfo>> {
fields.next()?;
let model = fields.next()?.trim();
- cpus[i].model = model.to_string();
- i += 1;
+ cpus[j].model = model.to_string();
+ j += 1;
}
- cpus.truncate(i);
+ while j < count {
+ cpus[j].model = "unknown".to_string();
+ j += 1;
+ }
+
+ cpus.truncate(count);
Some(cpus)
}