From e04e78eda2107f5bffed2559f2141ec7d566208a Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Wed, 1 Apr 2020 17:42:05 +0200 Subject: [PATCH] psgo: add RSS format Signed-off-by: Leah Neukirchen --- psgo.go | 15 +++++++++++++++ test/format.bats | 9 ++++++++- test/list.bats | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/psgo.go b/psgo.go index 30b8b74..8cd56f3 100644 --- a/psgo.go +++ b/psgo.go @@ -305,6 +305,11 @@ var ( onHost: true, procFn: processHGROUP, }, + { + normal: "rss", + header: "RSS", + procFn: processRSS, + }, { normal: "state", header: "STATE", @@ -867,6 +872,16 @@ func processHGROUP(p *process.Process, ctx *psContext) (string, error) { return "?", nil } +// processRSS returns the resident set size of process p in KiB (1024-byte +// units). +func processRSS(p *process.Process, ctx *psContext) (string, error) { + if p.Status.VMRSS == "" { + // probably a kernel thread + return "0", nil + } + return p.Status.VMRSS, nil +} + // processState returns the process state of process p. func processState(p *process.Process, ctx *psContext) (string, error) { return p.Status.State, nil diff --git a/test/format.bats b/test/format.bats index d60522c..c399704 100644 --- a/test/format.bats +++ b/test/format.bats @@ -231,6 +231,12 @@ function is_labeling_enabled() { [[ ${lines[0]} =~ "LABEL" ]] } +@test "RSS header" { + run ./bin/psgo -format "rss" + [ "$status" -eq 0 ] + [[ ${lines[0]} =~ "RSS" ]] +} + @test "STATE header" { run ./bin/psgo -format "state" [ "$status" -eq 0 ] @@ -244,7 +250,7 @@ function is_labeling_enabled() { } @test "ALL header" { - run ./bin/psgo -format "pcpu, group, ppid, user, args, comm, rgroup, nice, pid, pgid, etime, ruser, time, tty, vsz, capamb, capinh, capprm, capeff, capbnd, seccomp, hpid, huser, hgroup, state" + run ./bin/psgo -format "pcpu, group, ppid, user, args, comm, rgroup, nice, pid, pgid, etime, ruser, time, tty, vsz, capamb, capinh, capprm, capeff, capbnd, seccomp, hpid, huser, hgroup, rss, state" [ "$status" -eq 0 ] [[ ${lines[0]} =~ "%CPU" ]] @@ -270,5 +276,6 @@ function is_labeling_enabled() { [[ ${lines[0]} =~ "HPID" ]] [[ ${lines[0]} =~ "HUSER" ]] [[ ${lines[0]} =~ "HGROUP" ]] + [[ ${lines[0]} =~ "RSS" ]] [[ ${lines[0]} =~ "STATE" ]] } diff --git a/test/list.bats b/test/list.bats index eb059a2..57c7a99 100644 --- a/test/list.bats +++ b/test/list.bats @@ -3,5 +3,5 @@ @test "List descriptors" { run ./bin/psgo -list [ "$status" -eq 0 ] - [[ ${lines[0]} =~ "args, capamb, capbnd, capeff, capinh, capprm, comm, etime, group, hgroup, hpid, huser, label, nice, pcpu, pgid, pid, ppid, rgroup, ruser, seccomp, state, stime, time, tty, user, vsz" ]] + [[ ${lines[0]} =~ "args, capamb, capbnd, capeff, capinh, capprm, comm, etime, group, hgroup, hpid, huser, label, nice, pcpu, pgid, pid, ppid, rgroup, rss, ruser, seccomp, state, stime, time, tty, user, vsz" ]] }