服务器配置: Ubuntu18.04 4核16g
有一个Java web应用,想通过gopsutil获取该进程的CPU使用率, 默认不访问,通过top查看,CPU使用率浮动0.0~0.3
```
$ top -p 2439
top - 08:28:20 up 28 days, 19:40, 7 users, load average: 2.67, 1.89, 1.88
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 25.0 us, 11.4 sy, 0.0 ni, 62.9 id, 0.2 wa, 0.0 hi, 0.6 si, 0.0 st
KiB Mem : 16342280 total, 1349456 free, 7832528 used, 7160296 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 9406588 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2439 jiangyd 20 0 7909440 557120 19156 S 0.3 3.4 26:56.50 java
```
go 代码
```
package main
import (
"fmt"
"time"
"github.com/shirou/gopsutil/process"
)
func main() {
proce, err := process.NewProcess(2439)
if err != nil {
fmt.Println(err)
}
for {
t, err := proce.CPUPercent()
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
time.Sleep(time.Second * 3)
}
}
```
通过程序运行获取 (不知道为什么,获取的值都几乎一致)
```
$ go run process.go
0.38034758398948637
0.38034488210257517
0.3803421970541274
0.38033949538827044
0.3803391630394106
0.380338813923693
0.3803361117440858
0.3803334268939177
```
以上在Java应用,没有请求的时候,看不出top获取的CPU,与gopsutil 获取进程的CPU有什么区别
现在给Java应用一些压测,例如使用jmeter 100个并发请求,再次通过top查看,cpu使用率的波动在30~50差不多
```
$ top -p 2439
top - 08:34:42 up 28 days, 19:46, 7 users, load average: 2.14, 2.62, 2.27
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 31.7 us, 13.5 sy, 0.0 ni, 53.0 id, 0.2 wa, 0.0 hi, 1.6 si, 0.0 st
KiB Mem : 16342280 total, 1308800 free, 7845908 used, 7187572 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 9384580 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2439 jiangyd 20 0 7909440 555664 19156 S 45.3 3.4 27:00.33 java
```
但是再次,通过go 程序去查看,发现与之前一样的值,这里就搞不清了,难道这个CPUPercent 方法错误?
https://godoc.org/github.com/shirou/gopsutil/process#Process.CPUPercent
```
$ go run process.go
0.38471095821959816
0.3849856685762762
0.3853356280277827
0.3856385425690914
0.38586386464412914
0.3860844987718504
0.3863779971000666
0.3866973701931095
```
请各位大佬指点迷津啥情况
运行以下代码与top查看结果是一样的
```
package main
import (
"fmt"
"time"
"github.com/shirou/gopsutil/process"
)
func main() {
proce, err := process.NewProcess(2439)
if err != nil {
fmt.Println(err)
}
for {
fmt.Println(proce.Percent(3*time.Second))
}
}
```
但提的问题,还是同问,那个CPUPercent 是啥意思
#1