Compare commits
No commits in common. "master" and "dev" have entirely different histories.
13
pidusage.go
13
pidusage.go
|
@ -9,7 +9,6 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SysInfo will record cpu and memory data
|
// SysInfo will record cpu and memory data
|
||||||
|
@ -18,7 +17,7 @@ type SysInfo struct {
|
||||||
Memory float64
|
Memory float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stat will store CPU time struct
|
// Stat will store CUP time struct
|
||||||
type Stat struct {
|
type Stat struct {
|
||||||
utime float64
|
utime float64
|
||||||
stime float64
|
stime float64
|
||||||
|
@ -34,7 +33,6 @@ type fn func(int) (*SysInfo, error)
|
||||||
var fnMap map[string]fn
|
var fnMap map[string]fn
|
||||||
var platform string
|
var platform string
|
||||||
var history map[int]Stat
|
var history map[int]Stat
|
||||||
var historyLock sync.Mutex
|
|
||||||
var eol string
|
var eol string
|
||||||
|
|
||||||
func wrapper(statType string) func(pid int) (*SysInfo, error) {
|
func wrapper(statType string) func(pid int) (*SysInfo, error) {
|
||||||
|
@ -44,7 +42,8 @@ func wrapper(statType string) func(pid int) (*SysInfo, error) {
|
||||||
}
|
}
|
||||||
func init() {
|
func init() {
|
||||||
platform = runtime.GOOS
|
platform = runtime.GOOS
|
||||||
if eol = "\n"; strings.Index(platform, "win") == 0 {
|
eol = "\n"
|
||||||
|
if strings.Index(platform, "win") == 0 {
|
||||||
platform = "win"
|
platform = "win"
|
||||||
eol = "\r\n"
|
eol = "\r\n"
|
||||||
}
|
}
|
||||||
|
@ -80,7 +79,7 @@ func stat(pid int, statType string) (*SysInfo, error) {
|
||||||
stdout, _ := exec.Command("ps", args, strconv.Itoa(pid)).Output()
|
stdout, _ := exec.Command("ps", args, strconv.Itoa(pid)).Output()
|
||||||
ret := formatStdOut(stdout, 1)
|
ret := formatStdOut(stdout, 1)
|
||||||
if len(ret) == 0 {
|
if len(ret) == 0 {
|
||||||
return sysInfo, errors.New("Can't find process with this PID: " + strconv.Itoa(pid))
|
return sysInfo, errors.New("can not foud this pid: " + strconv.Itoa(pid))
|
||||||
}
|
}
|
||||||
sysInfo.CPU = parseFloat(ret[0])
|
sysInfo.CPU = parseFloat(ret[0])
|
||||||
sysInfo.Memory = parseFloat(ret[1]) * 1024
|
sysInfo.Memory = parseFloat(ret[1]) * 1024
|
||||||
|
@ -106,7 +105,7 @@ func stat(pid int, statType string) (*SysInfo, error) {
|
||||||
splitAfter := strings.SplitAfter(string(procStatFileBytes), ")")
|
splitAfter := strings.SplitAfter(string(procStatFileBytes), ")")
|
||||||
|
|
||||||
if len(splitAfter) == 0 || len(splitAfter) == 1 {
|
if len(splitAfter) == 0 || len(splitAfter) == 1 {
|
||||||
return sysInfo, errors.New("Can't find process with this PID: " + strconv.Itoa(pid))
|
return sysInfo, errors.New("can not foud this pid: " + strconv.Itoa(pid))
|
||||||
}
|
}
|
||||||
infos := strings.Split(splitAfter[1], " ")
|
infos := strings.Split(splitAfter[1], " ")
|
||||||
stat := &Stat{
|
stat := &Stat{
|
||||||
|
@ -141,9 +140,7 @@ func stat(pid int, statType string) (*SysInfo, error) {
|
||||||
seconds = 1
|
seconds = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
historyLock.Lock()
|
|
||||||
history[pid] = *stat
|
history[pid] = *stat
|
||||||
historyLock.Unlock()
|
|
||||||
sysInfo.CPU = (total / seconds) * 100
|
sysInfo.CPU = (total / seconds) * 100
|
||||||
sysInfo.Memory = stat.rss * pageSize
|
sysInfo.Memory = stat.rss * pageSize
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue