Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
strucoder | 25b2fd121a | |
strucoder | 9927ebc77a | |
strucoder | 581934eda8 |
71
pidusage.go
71
pidusage.go
|
@ -9,7 +9,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SysInfo will record cpu and memory data
|
// SysInfo will record cpu and memory data
|
||||||
|
@ -20,13 +20,15 @@ type SysInfo struct {
|
||||||
|
|
||||||
// Stat will store CPU time struct
|
// Stat will store CPU time struct
|
||||||
type Stat struct {
|
type Stat struct {
|
||||||
utime float64
|
utime float64
|
||||||
stime float64
|
stime float64
|
||||||
cutime float64
|
cutime float64
|
||||||
cstime float64
|
cstime float64
|
||||||
start float64
|
start float64
|
||||||
rss float64
|
rss float64
|
||||||
uptime float64
|
uptime float64
|
||||||
|
kernelmodetime float64
|
||||||
|
usermodetime float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type fn func(int) (*SysInfo, error)
|
type fn func(int) (*SysInfo, error)
|
||||||
|
@ -34,7 +36,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,10 +45,10 @@ 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 {
|
// if eol = "\n"; strings.Index(platform, "win") == 0 {
|
||||||
platform = "win"
|
// platform = "win"
|
||||||
eol = "\r\n"
|
// eol = "\r\n"
|
||||||
}
|
// }
|
||||||
history = make(map[int]Stat)
|
history = make(map[int]Stat)
|
||||||
fnMap = make(map[string]fn)
|
fnMap = make(map[string]fn)
|
||||||
fnMap["darwin"] = wrapper("ps")
|
fnMap["darwin"] = wrapper("ps")
|
||||||
|
@ -57,6 +58,7 @@ func init() {
|
||||||
fnMap["linux"] = wrapper("proc")
|
fnMap["linux"] = wrapper("proc")
|
||||||
fnMap["netbsd"] = wrapper("proc")
|
fnMap["netbsd"] = wrapper("proc")
|
||||||
fnMap["win"] = wrapper("win")
|
fnMap["win"] = wrapper("win")
|
||||||
|
fnMap["windows"] = wrapper("windows")
|
||||||
}
|
}
|
||||||
func formatStdOut(stdout []byte, userfulIndex int) []string {
|
func formatStdOut(stdout []byte, userfulIndex int) []string {
|
||||||
infoArr := strings.Split(string(stdout), eol)[userfulIndex]
|
infoArr := strings.Split(string(stdout), eol)[userfulIndex]
|
||||||
|
@ -141,18 +143,53 @@ 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
|
||||||
|
} else if statType == "windows" {
|
||||||
|
uptime := float64(time.Now().Unix())
|
||||||
|
args := "wmic PROCESS " + strconv.Itoa(pid) + " get workingsetsize,usermodetime,kernelmodetime"
|
||||||
|
cmdArgs := strings.Fields(args)
|
||||||
|
output, _ := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...).Output()
|
||||||
|
outputStr := string(output)
|
||||||
|
strArr := strings.Fields(outputStr)[3:]
|
||||||
|
|
||||||
|
kernelmodetime := parseFloat(strArr[0])
|
||||||
|
usermodetime := parseFloat(strArr[1])
|
||||||
|
workingsetsize := parseFloat(strArr[2])
|
||||||
|
|
||||||
|
_kernelmodetime := 0.0
|
||||||
|
_usermodetime := 0.0
|
||||||
|
_uptime := 0.0
|
||||||
|
if _history.kernelmodetime != 0 {
|
||||||
|
_kernelmodetime = _history.kernelmodetime
|
||||||
|
}
|
||||||
|
|
||||||
|
if _history.usermodetime != 0 {
|
||||||
|
_usermodetime = _history.usermodetime
|
||||||
|
}
|
||||||
|
|
||||||
|
if _history.uptime != 0 {
|
||||||
|
_uptime = _history.uptime
|
||||||
|
}
|
||||||
|
total := kernelmodetime - _kernelmodetime + usermodetime - _usermodetime
|
||||||
|
total = total / 10000000
|
||||||
|
|
||||||
|
// seconds :=
|
||||||
|
stat := &Stat{
|
||||||
|
kernelmodetime: kernelmodetime,
|
||||||
|
usermodetime: usermodetime,
|
||||||
|
uptime: uptime,
|
||||||
|
}
|
||||||
|
|
||||||
|
history[pid] = *stat
|
||||||
|
|
||||||
}
|
}
|
||||||
return sysInfo, nil
|
return sysInfo, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStat will return current system CPU and memory data
|
// GetStat will return current system CPU and memory data
|
||||||
func GetStat(pid int) (*SysInfo, error) {
|
func GetStat(pid int) (*SysInfo, error) {
|
||||||
sysInfo, err := fnMap[platform](pid)
|
sysInfo, err := fnMap[platform](12320)
|
||||||
return sysInfo, err
|
return sysInfo, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue