feat(4windows): just for windows

This commit is contained in:
strucoder 2018-02-05 23:37:37 +08:00
parent f39c7cf5e2
commit 581934eda8
1 changed files with 15 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"fmt"
) )
// SysInfo will record cpu and memory data // SysInfo will record cpu and memory data
@ -42,10 +43,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")
@ -55,6 +56,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]
@ -142,13 +144,20 @@ func stat(pid int, statType string) (*SysInfo, error) {
history[pid] = *stat history[pid] = *stat
sysInfo.CPU = (total / seconds) * 100 sysInfo.CPU = (total / seconds) * 100
sysInfo.Memory = stat.rss * pageSize sysInfo.Memory = stat.rss * pageSize
} else if statType == "windows" {
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)
fmt.Println(strings.Fields(outputStr)[3:], "===sd")
} }
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
} }