From 2e50131db8e5bd51fd068cc138835239124a5d7a Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Fri, 25 Apr 2014 15:09:38 +0900 Subject: [PATCH] implement Process.Memory_info on linux. --- README.rst | 4 ++-- process.go | 16 ++++++++-------- process_linux.go | 41 ++++++++++++++++++++++++++++++++++++++++- process_linux_amd64.go | 3 ++- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index bf96e8d..f2f6a12 100644 --- a/README.rst +++ b/README.rst @@ -86,6 +86,8 @@ Current Status - nice (linux) - num_fds (linux) - cpu_times (linux) + - memory_info (linux) + - memory_info_ex (linux) - not yet @@ -107,8 +109,6 @@ Current Status - threads - cpu_percent - cpu_affinity - - memory_info - - memory_info_ex - memory_percent - memory_maps - children diff --git a/process.go b/process.go index a5ddae4..fd703b0 100644 --- a/process.go +++ b/process.go @@ -25,11 +25,11 @@ type Process struct { // Threads map[string]string `json:"threads"` Cpu_times CPU_TimesStat `json:"cpu_times"` // Cpu_percent `json:"cpu_percent"` - Cpu_affinity []int32 `json:"cpu_affinity"` - Memory_info Memory_infoStat `json:"memory_info"` - Memory_info_ex map[string]string `json:"memori_info_ex"` - Memory_percent float32 `json:"memory_percent"` - Memory_maps []Memory_mapsStat `json:"memory_maps"` + Cpu_affinity []int32 `json:"cpu_affinity"` + Memory_info Memory_infoStat `json:"memory_info"` + Memory_info_ex Memory_info_exStat `json:"memori_info_ex"` + Memory_percent float32 `json:"memory_percent"` + Memory_maps []Memory_mapsStat `json:"memory_maps"` // Children []Process // FIXME: recursive `json:"children"` Open_files []Open_filesStat `json:"open_files"` Connections []Net_connectionStat `json:"connections"` @@ -38,12 +38,12 @@ type Process struct { type Open_filesStat struct { Path string `json:"path"` - Fd uint32 `json:"fd"` + Fd uint64 `json:"fd"` } type Memory_infoStat struct { - RSS int32 `json:"rss"` // bytes - VMS int32 `json:"vms"` // bytes + RSS uint64 `json:"rss"` // bytes + VMS uint64 `json:"vms"` // bytes } type Memory_mapsStat struct { diff --git a/process_linux.go b/process_linux.go index 52746b3..0428554 100644 --- a/process_linux.go +++ b/process_linux.go @@ -16,6 +16,17 @@ const ( PRIO_PROCESS = 0 // linux/resource.h ) +// Memory_info_ex is different between OSes +type Memory_info_exStat struct { + RSS uint64 `json:"rss"` // bytes + VMS uint64 `json:"vms"` // bytes + Shared uint64 `json:"shared"` // bytes + Text uint64 `json:"text"` // bytes + Lib uint64 `json:"lib"` // bytes + Data uint64 `json:"data"` // bytes + Dirty uint64 `json:"dirty"` // bytes +} + type fillFunc func(pid int32, p *Process) error func NewProcess(pid int32) (*Process, error) { @@ -25,7 +36,8 @@ func NewProcess(pid int32) (*Process, error) { // Fill Process information from fillFuncs var wg sync.WaitGroup - funcs := []fillFunc{fillFromStat, fillFromStatus, fillFromfd, fillFromCmdline} + funcs := []fillFunc{fillFromStat, fillFromStatus, fillFromfd, + fillFromCmdline, fillFromStatm} wg.Add(len(funcs)) for _, f := range funcs { @@ -83,6 +95,33 @@ func fillFromCmdline(pid int32, p *Process) error { return nil } +// Get memory info from /proc/(pid)/statm +func fillFromStatm(pid int32, p *Process) error { + memPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "statm") + contents, err := ioutil.ReadFile(memPath) + if err != nil { + return err + } + fields := strings.Split(string(contents), " ") + + rss := parseUint64(fields[0]) * PAGESIZE + vms := parseUint64(fields[1]) * PAGESIZE + p.Memory_info = Memory_infoStat{ + RSS: rss, + VMS: vms, + } + p.Memory_info_ex = Memory_info_exStat{ + RSS: rss, + VMS: vms, + Shared: parseUint64(fields[2]) * PAGESIZE, + Text: parseUint64(fields[3]) * PAGESIZE, + Lib: parseUint64(fields[4]) * PAGESIZE, + Dirty: parseUint64(fields[5]) * PAGESIZE, + } + + return nil +} + // get various status from /proc/(pid)/status func fillFromStatus(pid int32, p *Process) error { statPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "status") diff --git a/process_linux_amd64.go b/process_linux_amd64.go index 498f9dd..726e1b6 100644 --- a/process_linux_amd64.go +++ b/process_linux_amd64.go @@ -4,5 +4,6 @@ package gopsutil const ( - CLOCK_TICKS = 100 // C.sysconf(C._SC_CLK_TCK) + CLOCK_TICKS = 100 // C.sysconf(C._SC_CLK_TCK) + PAGESIZE = 4096 // C.sysconf(C._SC_PAGE_SIZE) )