func StupidGetMemory(proc *process.Process) (ms *MemoryStat, err error) { ms = &MemoryStat{} memStat, err := proc.MemoryInfoEx() if err != nil { return nil, err } ms.VSS = memStat.VMS ms.RSS = memStat.RSS // PSS from dumpsys appname, _ := proc.Cmdline() cmd := exec.Command("/system/bin/dumpsys", "meminfo", appname) if data, er := cmd.CombinedOutput(); er == nil { res1 := ptn1.FindStringSubmatch(string(data)) if len(res1) != 0 { fmt.Sscanf(res1[1], "%d", &ms.PSS) } else { res2 := ptn2.FindStringSubmatch(string(data)) if len(res2) == 0 { return ms, nil } fmt.Sscanf(res2[1], "%d", &ms.PSS) } ms.PSS <<= 10 } return ms, nil }
func ProcessMemory(proc *process.Process) (ms *MemoryStat, err error) { return StupidGetMemory(proc) // Not working in some machines if !isRootUser() { return StupidGetMemory(proc) } mapsStat, err := proc.MemoryMaps(true) // need root here if err != nil { return nil, err } ms = &MemoryStat{} for _, mstat := range *mapsStat { ms.VSS += (mstat.Size << 10) ms.RSS += (mstat.Rss << 10) ms.PSS += (mstat.Pss << 10) if mstat.Rss == mstat.Pss { ms.USS += (mstat.Rss << 10) } else { ms.USS += ((mstat.PrivateClean + mstat.PrivateDirty) << 10) } } return }
func NewProcCollectCPU(proc *process.Process) CollectFunc { return func() (*Data, error) { percent, err := proc.CPUPercent(0) if err != nil { return nil, err } return &Data{ Name: fmt.Sprintf("proc:%d:cpu", proc.Pid), Data: map[string]interface{}{ "total": percent, "average": percent / float64(cpu.CPUCount), }, }, nil } }
func NewProcCollectTraffic(proc *process.Process) CollectFunc { return func() (*Data, error) { uids, err := proc.Uids() if err != nil { return nil, err } uid := uids[0] // there are four, first is real_uid rcv, snd, err := ReadTrafix(uid) if err != nil { return nil, err } return &Data{ Name: fmt.Sprintf("proc:%d:traffic", proc.Pid), Data: map[string]interface{}{ "recv": rcv, "send": snd, }, }, nil } }
func isMatch(proc *process.Process, search string) bool { cmdline, _ := proc.Cmdline() if fmt.Sprintf("pid:%d", proc.Pid) == search { return true } if "cmdline:"+cmdline == search { return true } exe, _ := proc.Exe() if "exe:"+exe == search { return true } name, _ := proc.Name() if "name:"+name == search { return true } return false }