pidusage/pidusage.go

159 lines
3.7 KiB
Go
Raw Permalink Normal View History

package pidusage
2017-04-06 17:13:40 +02:00
import (
2017-04-10 11:48:47 +02:00
"errors"
2017-04-06 17:13:40 +02:00
"io/ioutil"
"math"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
"sync"
2017-04-06 17:13:40 +02:00
)
// SysInfo will record cpu and memory data
type SysInfo struct {
CPU float64
Memory float64
}
2017-07-15 00:53:09 +02:00
// Stat will store CPU time struct
2017-04-06 17:13:40 +02:00
type Stat struct {
utime float64
stime float64
cutime float64
cstime float64
start float64
rss float64
uptime float64
}
2017-04-10 11:48:47 +02:00
type fn func(int) (*SysInfo, error)
2017-04-06 17:13:40 +02:00
var fnMap map[string]fn
2017-04-06 17:13:40 +02:00
var platform string
var history map[int]Stat
var historyLock sync.Mutex
var eol string
2017-04-06 17:13:40 +02:00
2017-04-10 11:48:47 +02:00
func wrapper(statType string) func(pid int) (*SysInfo, error) {
return func(pid int) (*SysInfo, error) {
return stat(pid, statType)
}
}
2017-04-06 17:13:40 +02:00
func init() {
platform = runtime.GOOS
2017-07-15 00:53:09 +02:00
if eol = "\n"; strings.Index(platform, "win") == 0 {
2017-04-06 17:13:40 +02:00
platform = "win"
eol = "\r\n"
2017-04-06 17:13:40 +02:00
}
history = make(map[int]Stat)
fnMap = make(map[string]fn)
fnMap["darwin"] = wrapper("ps")
fnMap["sunos"] = wrapper("ps")
fnMap["freebsd"] = wrapper("ps")
fnMap["aix"] = wrapper("ps")
fnMap["linux"] = wrapper("proc")
fnMap["netbsd"] = wrapper("proc")
fnMap["win"] = wrapper("win")
2017-04-06 17:13:40 +02:00
}
func formatStdOut(stdout []byte, userfulIndex int) []string {
infoArr := strings.Split(string(stdout), eol)[userfulIndex]
2017-04-06 17:13:40 +02:00
ret := strings.Fields(infoArr)
return ret
}
func parseFloat(val string) float64 {
floatVal, _ := strconv.ParseFloat(val, 64)
return floatVal
}
2017-04-10 11:48:47 +02:00
func stat(pid int, statType string) (*SysInfo, error) {
2017-04-06 17:13:40 +02:00
sysInfo := &SysInfo{}
_history := history[pid]
if statType == "ps" {
args := "-o pcpu,rss -p"
if platform == "aix" {
args = "-o pcpu,rssize -p"
}
stdout, _ := exec.Command("ps", args, strconv.Itoa(pid)).Output()
ret := formatStdOut(stdout, 1)
2017-04-10 11:48:47 +02:00
if len(ret) == 0 {
2017-07-15 00:53:09 +02:00
return sysInfo, errors.New("Can't find process with this PID: " + strconv.Itoa(pid))
2017-04-10 11:48:47 +02:00
}
2017-04-06 17:13:40 +02:00
sysInfo.CPU = parseFloat(ret[0])
sysInfo.Memory = parseFloat(ret[1]) * 1024
2017-04-06 17:13:40 +02:00
} else if statType == "proc" {
// default clkTck and pageSize
var clkTck float64 = 100
var pageSize float64 = 4096
uptimeFileBytes, err := ioutil.ReadFile(path.Join("/proc", "uptime"))
uptime := parseFloat(strings.Split(string(uptimeFileBytes), " ")[0])
clkTckStdout, err := exec.Command("getconf", "CLK_TCK").Output()
if err == nil {
clkTck = parseFloat(formatStdOut(clkTckStdout, 0)[0])
}
pageSizeStdout, err := exec.Command("getconf", "PAGESIZE").Output()
if err == nil {
pageSize = parseFloat(formatStdOut(pageSizeStdout, 0)[0])
}
procStatFileBytes, err := ioutil.ReadFile(path.Join("/proc", strconv.Itoa(pid), "stat"))
2017-04-10 11:48:47 +02:00
splitAfter := strings.SplitAfter(string(procStatFileBytes), ")")
if len(splitAfter) == 0 || len(splitAfter) == 1 {
2017-07-15 00:53:09 +02:00
return sysInfo, errors.New("Can't find process with this PID: " + strconv.Itoa(pid))
2017-04-10 11:48:47 +02:00
}
infos := strings.Split(splitAfter[1], " ")
2017-04-06 17:13:40 +02:00
stat := &Stat{
utime: parseFloat(infos[12]),
stime: parseFloat(infos[13]),
cutime: parseFloat(infos[14]),
cstime: parseFloat(infos[15]),
start: parseFloat(infos[20]) / clkTck,
rss: parseFloat(infos[22]),
uptime: uptime,
}
_stime := 0.0
_utime := 0.0
if _history.stime != 0 {
2017-04-06 17:13:40 +02:00
_stime = _history.stime
}
if _history.utime != 0 {
2017-04-06 17:13:40 +02:00
_utime = _history.utime
}
total := stat.stime - _stime + stat.utime - _utime
total = total / clkTck
seconds := stat.start - uptime
if _history.uptime != 0 {
2017-04-06 17:13:40 +02:00
seconds = uptime - _history.uptime
}
seconds = math.Abs(seconds)
if seconds == 0 {
seconds = 1
}
historyLock.Lock()
2017-04-06 17:13:40 +02:00
history[pid] = *stat
historyLock.Unlock()
2017-04-06 17:13:40 +02:00
sysInfo.CPU = (total / seconds) * 100
sysInfo.Memory = stat.rss * pageSize
}
2017-04-10 11:48:47 +02:00
return sysInfo, nil
2017-04-06 17:13:40 +02:00
}
// GetStat will return current system CPU and memory data
2017-04-10 11:48:47 +02:00
func GetStat(pid int) (*SysInfo, error) {
sysInfo, err := fnMap[platform](pid)
return sysInfo, err
2017-04-06 17:13:40 +02:00
}