Skip to content

Commit

Permalink
Merge pull request #1691 from pgimalac/pgimalac/fix-proc-smaps-parsing
Browse files Browse the repository at this point in the history
Fix parsing of /proc/pid/smaps when path is empty
  • Loading branch information
shirou committed Aug 24, 2024
2 parents 2c51005 + d4ac231 commit 2a37a1d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
4 changes: 3 additions & 1 deletion process/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M
// function of parsing a block
getBlock := func(firstLine []string, block []string) (MemoryMapsStat, error) {
m := MemoryMapsStat{}
m.Path = firstLine[len(firstLine)-1]
if len(firstLine) >= 6 {
m.Path = strings.Join(firstLine[5:], " ")
}

for _, line := range block {
if strings.Contains(line, "VmFlags") {
Expand Down
67 changes: 67 additions & 0 deletions process/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSplitProcStat(t *testing.T) {
Expand Down Expand Up @@ -174,3 +175,69 @@ func TestFillFromTIDStatWithContext_lx_brandz(t *testing.T) {
assert.Equal(t, float64(0), cpuTimes.Iowait)
}
}

func TestProcessMemoryMaps(t *testing.T) {
t.Setenv("HOST_PROC", "testdata/linux")
pid := 1
p, err := NewProcess(int32(pid))
require.NoError(t, err)
maps, err := p.MemoryMaps(false)
require.NoError(t, err)

expected := &[]MemoryMapsStat{
{
"[vvar]",
0,
1,
0,
3,
4,
5,
6,
7,
8,
9,
},
{
"",
0,
1,
2,
3,
4,
0,
6,
7,
8,
9,
},
{
"[vdso]",
0,
1,
2,
3,
4,
5,
0,
7,
8,
9,
},
{
"/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1",
0,
1,
2,
3,
4,
5,
6,
7,
0,
9,
},
}

require.Equal(t, expected, maps)
}
44 changes: 44 additions & 0 deletions process/testdata/linux/1/smaps
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ffffb5ecc000-ffffb5ece000 r--p 00000000 00:00 0 [vvar]
Rss: 0 kB
KernelPageSize: 4 kB
Size: 1 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Clean: 5 kB
Private_Dirty: 6 kB
Referenced: 7 kB
Anonymous: 8 kB
Swap: 9 kB
ffffb5eca000-ffffb5ecc000 rw-p 00000000 00:00 0
Rss: 0 kB
Size: 1 kB
Pss: 2 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Dirty: 6 kB
LazyFree: 0 kB
Referenced: 7 kB
Anonymous: 8 kB
Swap: 9 kB
ffffb5ece000-ffffb5ecf000 r-xp 00000000 00:00 0 [vdso]
Rss: 0 kB
Size: 1 kB
Pss: 2 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Clean: 5 kB
Private_Hugetlb: 0 kB
Referenced: 7 kB
Anonymous: 8 kB
Swap: 9 kB
ffffb5ecf000-ffffb5ed1000 r--p 0002a000 00:3d 2238525 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1
Rss: 0 kB
Size: 1 kB
Pss: 2 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Clean: 5 kB
Private_Dirty: 6 kB
Referenced: 7 kB
THPeligible: 0
Swap: 9 kB

0 comments on commit 2a37a1d

Please sign in to comment.