From 47166d6a8124547aadc9661e0b45d3d8aa4eaf88 Mon Sep 17 00:00:00 2001 From: Tagir Bakirov Date: Wed, 8 Aug 2018 09:37:17 +0200 Subject: [PATCH 1/2] implement grouped memorymaps output --- process/process_linux.go | 18 +++++++++++++++++- process/process_test.go | 13 +++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/process/process_linux.go b/process/process_linux.go index 6708173..dcaf95d 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -537,6 +537,9 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) { pid := p.Pid var ret []MemoryMapsStat + if grouped { + ret = make([]MemoryMapsStat, 1) + } smapsPath := common.HostProc(strconv.Itoa(int(pid)), "smaps") contents, err := ioutil.ReadFile(smapsPath) if err != nil { @@ -599,7 +602,20 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M if err != nil { return &ret, err } - ret = append(ret, g) + if grouped { + ret.Size += g.Size + ret.Rss += g.Rss + ret.Pss += g.Pss + ret.SharedClean += g.SharedClean + ret.SharedDirty += g.SharedDirty + ret.PrivateClean += g.PrivateClean + ret.PrivateDirty += g.PrivateDirty + ret.Referenced += g.Referenced + ret.Anonymous += g.Anonymous + ret.Swap += g.Swap + } else { + ret = append(ret, g) + } } // starts new block blocks = make([]string, 16) diff --git a/process/process_test.go b/process/process_test.go index 7cdcedb..ebf63ca 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -89,6 +89,7 @@ func Test_Process_memory_maps(t *testing.T) { t.Errorf("error %v", err) } + // ungrouped memory maps mmaps, err := ret.MemoryMaps(false) if err != nil { t.Errorf("memory map get error %v", err) @@ -99,6 +100,18 @@ func Test_Process_memory_maps(t *testing.T) { t.Errorf("memory map get error %v", m) } } + + // grouped memory maps + mmaps, err := ret.MemoryMaps(true) + if err != nil { + t.Errorf("memory map get error %v", err) + } + if len(mmaps) != 1 { + t.Errorf("grouped memory maps length (%v) is not equal to 1", len(mmaps)) + } + if mmaps[0] == empty { + t.Errorf("memory map is empty") + } } func Test_Process_MemoryInfo(t *testing.T) { p := testGetProcess() From 29b3c3719b9058eb535381e95fdc7c80e6d9e7bb Mon Sep 17 00:00:00 2001 From: Tagir Bakirov Date: Tue, 14 Aug 2018 09:12:09 +0200 Subject: [PATCH 2/2] fix slice indices --- process/process_linux.go | 20 ++++++++++---------- process/process_test.go | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/process/process_linux.go b/process/process_linux.go index dcaf95d..734a19e 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -603,16 +603,16 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M return &ret, err } if grouped { - ret.Size += g.Size - ret.Rss += g.Rss - ret.Pss += g.Pss - ret.SharedClean += g.SharedClean - ret.SharedDirty += g.SharedDirty - ret.PrivateClean += g.PrivateClean - ret.PrivateDirty += g.PrivateDirty - ret.Referenced += g.Referenced - ret.Anonymous += g.Anonymous - ret.Swap += g.Swap + ret[0].Size += g.Size + ret[0].Rss += g.Rss + ret[0].Pss += g.Pss + ret[0].SharedClean += g.SharedClean + ret[0].SharedDirty += g.SharedDirty + ret[0].PrivateClean += g.PrivateClean + ret[0].PrivateDirty += g.PrivateDirty + ret[0].Referenced += g.Referenced + ret[0].Anonymous += g.Anonymous + ret[0].Swap += g.Swap } else { ret = append(ret, g) } diff --git a/process/process_test.go b/process/process_test.go index ebf63ca..91c76ae 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -102,14 +102,14 @@ func Test_Process_memory_maps(t *testing.T) { } // grouped memory maps - mmaps, err := ret.MemoryMaps(true) + mmaps, err = ret.MemoryMaps(true) if err != nil { t.Errorf("memory map get error %v", err) } - if len(mmaps) != 1 { - t.Errorf("grouped memory maps length (%v) is not equal to 1", len(mmaps)) + if len(*mmaps) != 1 { + t.Errorf("grouped memory maps length (%v) is not equal to 1", len(*mmaps)) } - if mmaps[0] == empty { + if (*mmaps)[0] == empty { t.Errorf("memory map is empty") } }