From f4952f75a82fa3e3dbba9e5d63ede6565f9c37ce Mon Sep 17 00:00:00 2001
From: MrEconomical <47700125+MrEconomical@users.noreply.github.com>
Date: Thu, 25 Jul 2024 20:39:01 -0700
Subject: [PATCH] fix(ext/node): read correct CPU usage stats on Linux (#24732)
Fixes #24731
The total CPU usage row is ignored and info from `cpu0` and `cpu1` is
correctly read.
---------
Signed-off-by: MrEconomical <47700125+MrEconomical@users.noreply.github.com>
---
ext/node/ops/os/cpus.rs | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/ext/node/ops/os/cpus.rs b/ext/node/ops/os/cpus.rs
index 7515bae8b1..f57e84a1c7 100644
--- a/ext/node/ops/os/cpus.rs
+++ b/ext/node/ops/os/cpus.rs
@@ -246,12 +246,13 @@ pub fn cpu_info() -> Option> {
let reader = std::io::BufReader::new(fp);
let mut count = 0;
- for (i, line) in reader.lines().enumerate() {
+ // Skip the first line which tracks total CPU time across all cores
+ for (i, line) in reader.lines().skip(1).enumerate() {
let line = line.ok()?;
if !line.starts_with("cpu") {
break;
}
- count = i;
+ count = i + 1;
let mut fields = line.split_whitespace();
fields.next()?;
let user = fields.next()?.parse::().ok()?;