Compare commits

...

7 Commits
dev ... master

Author SHA1 Message Date
Dan Ballard 0b9266ba83 make writes to shared global hashmap threadsafe 2018-11-21 13:42:58 -08:00
David 大伟 f39c7cf5e2 Merge pull request #5 from xNevo/master
Fixed english typos
2017-07-17 09:56:50 +08:00
xNevo 8d5783c9c4 FIxed typos 2017-07-15 00:53:09 +02:00
David 大伟 beb3d83057 Merge pull request #4 from struCoder/dev
feat(dev): fix proc type when there is no pid
2017-04-10 18:01:52 +08:00
David 大伟 8b46c6a37a Merge pull request #3 from struCoder/dev
feat(dev): add error and update readme
2017-04-10 17:49:41 +08:00
David 大伟 d654f15dd0 Merge pull request #2 from struCoder/dev
docs(dev): update readme.md
2017-04-07 23:11:26 +08:00
David 大伟 f84800df51 Merge pull request #1 from struCoder/dev
feat(dev): support os sys except windows
2017-04-07 22:57:43 +08:00
1 changed files with 8 additions and 5 deletions

View File

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