feat(4windows): save data

This commit is contained in:
strucoder 2018-02-06 17:51:00 +08:00
parent 9927ebc77a
commit 25b2fd121a
1 changed files with 38 additions and 17 deletions

View File

@ -1,7 +1,6 @@
package pidusage package pidusage
import ( import (
"os"
"errors" "errors"
"io/ioutil" "io/ioutil"
"math" "math"
@ -10,7 +9,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"fmt" "time"
) )
// SysInfo will record cpu and memory data // SysInfo will record cpu and memory data
@ -148,20 +147,42 @@ func stat(pid int, statType string) (*SysInfo, error) {
sysInfo.CPU = (total / seconds) * 100 sysInfo.CPU = (total / seconds) * 100
sysInfo.Memory = stat.rss * pageSize sysInfo.Memory = stat.rss * pageSize
} else if statType == "windows" { } else if statType == "windows" {
uptime := float64(time.Now().Unix())
args := "wmic PROCESS " + strconv.Itoa(pid) + " get workingsetsize,usermodetime,kernelmodetime" args := "wmic PROCESS " + strconv.Itoa(pid) + " get workingsetsize,usermodetime,kernelmodetime"
cmdArgs := strings.Fields(args) cmdArgs := strings.Fields(args)
output, _ := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...).Output() output, _ := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...).Output()
outputStr := string(output) outputStr := string(output)
strArr = strings.Fields(outputStr)[3:] strArr := strings.Fields(outputStr)[3:]
kernelmodetime := strArr[0] kernelmodetime := parseFloat(strArr[0])
usermodetime := strArr[1] usermodetime := parseFloat(strArr[1])
workingsetsize := strArr[2] workingsetsize := parseFloat(strArr[2])
total := kernelmodetime - (_history.kernelmodetime || 0) + usermodetime - (_history.usermodetime || 0) _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 total = total / 10000000
// seconds :=
stat := &Stat{
kernelmodetime: kernelmodetime,
usermodetime: usermodetime,
uptime: uptime,
}
history[pid] = *stat
} }
return sysInfo, nil return sysInfo, nil