From d500d42a346d175f0f22a883dfccca3645edae3b Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 10 Jun 2023 02:23:23 +0000 Subject: [PATCH] [process][linux]: use fillFromStatusWithContext on MemoryInfo --- process/process_linux.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/process/process_linux.go b/process/process_linux.go index 29c4473..d4947d9 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -315,15 +315,14 @@ func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - meminfo, _, err := p.fillFromStatmWithContext(ctx) - if err != nil { + if err := p.fillFromStatusWithContext(ctx); err != nil { return nil, err } - return meminfo, nil + return p.memInfo, nil } func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - _, memInfoEx, err := p.fillFromStatmWithContext(ctx) + memInfoEx, err := p.fillFromStatmWithContext(ctx) if err != nil { return nil, err } @@ -738,43 +737,38 @@ func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, e } // Get memory info from /proc/(pid)/statm -func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat, *MemoryInfoExStat, error) { +func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoExStat, error) { pid := p.Pid memPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "statm") contents, err := ioutil.ReadFile(memPath) if err != nil { - return nil, nil, err + return nil, err } fields := strings.Split(string(contents), " ") vms, err := strconv.ParseUint(fields[0], 10, 64) if err != nil { - return nil, nil, err + return nil, err } rss, err := strconv.ParseUint(fields[1], 10, 64) if err != nil { - return nil, nil, err - } - memInfo := &MemoryInfoStat{ - RSS: rss * pageSize, - VMS: vms * pageSize, + return nil, err } - shared, err := strconv.ParseUint(fields[2], 10, 64) if err != nil { - return nil, nil, err + return nil, err } text, err := strconv.ParseUint(fields[3], 10, 64) if err != nil { - return nil, nil, err + return nil, err } lib, err := strconv.ParseUint(fields[4], 10, 64) if err != nil { - return nil, nil, err + return nil, err } dirty, err := strconv.ParseUint(fields[5], 10, 64) if err != nil { - return nil, nil, err + return nil, err } memInfoEx := &MemoryInfoExStat{ @@ -786,7 +780,7 @@ func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat Dirty: dirty * pageSize, } - return memInfo, memInfoEx, nil + return memInfoEx, nil } // Get name from /proc/(pid)/comm or /proc/(pid)/status