From c81b5a744769f85c01ffce75407d60696623b203 Mon Sep 17 00:00:00 2001 From: Nir Levy Date: Sun, 20 Aug 2023 08:09:10 +0000 Subject: [PATCH] Support list of nspids Signed-off-by: Nir Levy --- proc_status.go | 27 ++++++++++++++++++--------- proc_status_test.go | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/proc_status.go b/proc_status.go index da6555c1..529b882e 100644 --- a/proc_status.go +++ b/proc_status.go @@ -32,8 +32,8 @@ type ProcStatus struct { // Thread group ID. TGID int - // Pid namespace. - NSpid int + // List of Pid namespace. + NSpids []uint64 // Peak virtual memory size. VmPeak uint64 // nolint:revive @@ -130,13 +130,7 @@ func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintByt case "Gid": copy(s.GIDs[:], strings.Split(vString, "\t")) case "NSpid": - if int(vUint) == 0 { - // In case where NSpid has 2 values - nspid, _ := strconv.ParseUint(strings.Split(vString, " ")[0], 10, 64) - s.NSpid = int(nspid) - } else { - s.NSpid = int(vUint) - } + s.NSpids = calcNSPidsList(vString) case "VmPeak": s.VmPeak = vUintBytes case "VmSize": @@ -210,3 +204,18 @@ func calcCpusAllowedList(cpuString string) []uint64 { sort.Slice(g, func(i, j int) bool { return g[i] < g[j] }) return g } + +func calcNSPidsList(nspidsString string) []uint64 { + s := strings.Split(nspidsString, " ") + var nspids []uint64 + + for _, nspid := range s { + nspid, _ := strconv.ParseUint(nspid, 10, 64) + if nspid == 0 { + continue + } + nspids = append(nspids, nspid) + } + + return nspids +} diff --git a/proc_status_test.go b/proc_status_test.go index 383c1653..9d22a48e 100644 --- a/proc_status_test.go +++ b/proc_status_test.go @@ -36,7 +36,7 @@ func TestProcStatus(t *testing.T) { }{ {name: "Pid", want: 26231, have: s.PID}, {name: "Tgid", want: 26231, have: s.TGID}, - {name: "NSpid", want: 1, have: s.NSpid}, + {name: "NSpid", want: 1, have: int(s.NSpids[0])}, {name: "VmPeak", want: 58472 * 1024, have: int(s.VmPeak)}, {name: "VmSize", want: 58440 * 1024, have: int(s.VmSize)}, {name: "VmLck", want: 0 * 1024, have: int(s.VmLck)},