Merge pull request #21 from elementai/swap-from-proc-pid-status

Add MemoryInfoStat to Process and fill it while reading /proc/<pid>/status
pull/23/head
shirou 11 years ago
commit 4e06bc6ad3

@ -12,6 +12,7 @@ type Process struct {
uids []int32
gids []int32
numThreads int32
memInfo *MemoryInfoStat
}
type OpenFilesStat struct {
@ -20,8 +21,9 @@ type OpenFilesStat struct {
}
type MemoryInfoStat struct {
RSS uint64 `json:"rss"` // bytes
VMS uint64 `json:"vms"` // bytes
RSS uint64 `json:"rss"` // bytes
VMS uint64 `json:"vms"` // bytes
Swap uint64 `json:"swap"` // bytes
}
type RlimitStat struct {

@ -150,11 +150,7 @@ func (p *Process) CPUAffinity() ([]int32, error) {
return nil, NotImplementedError
}
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
memInfo, _, err := p.fillFromStatm()
if err != nil {
return nil, err
}
return memInfo, nil
return p.memInfo, nil
}
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
_, memInfoEx, err := p.fillFromStatm()
@ -436,6 +432,7 @@ func (p *Process) fillFromStatus() error {
}
lines := strings.Split(string(contents), "\n")
p.numCtxSwitches = &NumCtxSwitchesStat{}
p.memInfo = &MemoryInfoStat{}
for _, line := range lines {
tabParts := strings.SplitN(line, "\t", 2)
if len(tabParts) < 2 {
@ -486,7 +483,29 @@ func (p *Process) fillFromStatus() error {
return err
}
p.numCtxSwitches.Involuntary = v
case "VmRSS":
value := strings.Trim(value, " kB") // remove last "kB"
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
p.memInfo.RSS = v * 1024
case "VmSize":
value := strings.Trim(value, " kB") // remove last "kB"
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
p.memInfo.VMS = v * 1024
case "VmSwap":
value := strings.Trim(value, " kB") // remove last "kB"
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
p.memInfo.Swap = v * 1024
}
}
return nil
}

Loading…
Cancel
Save