Describe the bug
process.Percent sometime return wrong value when have mary goroutines.
To Reproduce
I write demo code.
package main
import (
"fmt"
"os"
"time"
"github.com/shirou/gopsutil/process"
)
const MaxPercent = 800 // the machine have 8 cpu
// print process cpu usage per second
func metric() {
p, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
panic(err)
}
var lastTicker = time.Now()
ticker := time.NewTicker(time.Second)
for range ticker.C {
now := time.Now()
percent, err := p.Percent(0)
if err != nil {
panic(err)
}
if percent > MaxPercent {
fmt.Printf("process percent: %f, \tticker interval: %fs\n", percent, now.Sub(lastTicker).Seconds())
}
lastTicker = now
}
}
func main() {
for i := 0; i < 10000; i++ {
go func() {
for {
}
}()
}
go metric()
<-make(chan struct{})
}
Expected behavior
[A clear and concise description of what you expected to happen.]
The print txt "process percent" should be <= 800, because the machine have 8 cpu.
But I got this:
process percent: 168871696.801113, ticker interval: 33.316982s
process percent: 20228.992191, ticker interval: 29.470608s
process percent: 225733353.254644, ticker interval: 31.351190s
process percent: 208994766.209612, ticker interval: 31.867065s
Environment (please complete the following information):
[ ] Windows: [paste the result of ver]
[x] Linux: [paste contents of /etc/os-release and the result of uname -a]
[ ] Mac OS: [paste the result of sw_vers and uname -a
[ ] FreeBSD: [paste the result of freebsd-version -k -r -u and uname -a]
Since the ticker interval in your code is 30sec, I think the load is too high and getting the time is too slow. Now I do not know how to solve this problem.
Describe the bug process.Percent sometime return wrong value when have mary goroutines. To Reproduce I write demo code.
go.mod
Expected behavior [A clear and concise description of what you expected to happen.] The print txt "process percent" should be <= 800, because the machine have 8 cpu.
But I got this:
Environment (please complete the following information):
ver
]/etc/os-release
and the result ofuname -a
]sw_vers
anduname -a
freebsd-version -k -r -u
anduname -a
]uname -a
]cat /etc/os-release
uname -a
Additional context [Cross-compiling? Paste the command you are using to cross-compile and the result of the corresponding
go env
]