Skip to content

Commit

Permalink
Use strings.Cut in getNprocsProcStat
Browse files Browse the repository at this point in the history
Also make the skip condition on the first line explicit.
  • Loading branch information
tklauser committed Dec 1, 2023
1 parent fd2d5e3 commit 32abaa6
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sysconf_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ func getNprocsProcStat() (int64, error) {
s := bufio.NewScanner(f)
for s.Scan() {
if line := strings.TrimSpace(s.Text()); strings.HasPrefix(line, "cpu") {
l := strings.SplitN(line, " ", 2)
_, err := strconv.ParseInt(l[0][3:], 10, 64)
if err == nil {
count++
cpu, _, found := strings.Cut(line, " ")
if found {
// skip first line with accumulated values
if cpu == "cpu" {
continue
}
_, err := strconv.ParseInt(cpu[len("cpu"):], 10, 64)
if err == nil {
count++
}
}
} else {
// The current format of /proc/stat has all the
Expand Down

0 comments on commit 32abaa6

Please sign in to comment.