shirou / gopsutil

psutil for golang
Other
10.36k stars 1.57k forks source link

Total Processes in `MiscStat` Corrected #1612

Closed eric1234 closed 4 months ago

eric1234 commented 4 months ago

The ProcsTotal in the MiscStat structure was very inaccurate. It was reading a value which is the total number of kernel scheduling entities. This includes both processes and threads significantly overcounting.

This instead uses an existing method already in common to count the number of processes via the /proc filesystem where any directory is a number. This should still be a single syscall to read that directory entry.

I don't see any existing automated test with this functionality so I did not add any but I did do my own manual testing by writing the following program:

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v3/load"
)

func main() {
    stats, _ := load.Misc()
    fmt.Printf("Count: %v\n", stats.ProcsTotal)
}

With the previous code it was returning a value of over 1200. After the change the count was around 320 and matched ps -A | wc -l.

This fixes #1606.