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 uids []int32
gids []int32 gids []int32
numThreads int32 numThreads int32
memInfo *MemoryInfoStat
} }
type OpenFilesStat struct { type OpenFilesStat struct {
@ -22,6 +23,7 @@ type OpenFilesStat struct {
type MemoryInfoStat struct { type MemoryInfoStat struct {
RSS uint64 `json:"rss"` // bytes RSS uint64 `json:"rss"` // bytes
VMS uint64 `json:"vms"` // bytes VMS uint64 `json:"vms"` // bytes
Swap uint64 `json:"swap"` // bytes
} }
type RlimitStat struct { type RlimitStat struct {

@ -150,11 +150,7 @@ func (p *Process) CPUAffinity() ([]int32, error) {
return nil, NotImplementedError return nil, NotImplementedError
} }
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
memInfo, _, err := p.fillFromStatm() return p.memInfo, nil
if err != nil {
return nil, err
}
return memInfo, nil
} }
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
_, memInfoEx, err := p.fillFromStatm() _, memInfoEx, err := p.fillFromStatm()
@ -436,6 +432,7 @@ func (p *Process) fillFromStatus() error {
} }
lines := strings.Split(string(contents), "\n") lines := strings.Split(string(contents), "\n")
p.numCtxSwitches = &NumCtxSwitchesStat{} p.numCtxSwitches = &NumCtxSwitchesStat{}
p.memInfo = &MemoryInfoStat{}
for _, line := range lines { for _, line := range lines {
tabParts := strings.SplitN(line, "\t", 2) tabParts := strings.SplitN(line, "\t", 2)
if len(tabParts) < 2 { if len(tabParts) < 2 {
@ -486,7 +483,29 @@ func (p *Process) fillFromStatus() error {
return err return err
} }
p.numCtxSwitches.Involuntary = v 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 return nil
} }

Loading…
Cancel
Save