From 8a735c78dc68aa8054454772b34620a46fdd0ba7 Mon Sep 17 00:00:00 2001 From: slava Date: Tue, 31 Aug 2021 00:06:23 +0300 Subject: [PATCH] Fix data types for CUTime and CSTime stat fields #403 (#404) * Fix data types for CUTime and CSTime stat fields #403 These two stat fields (CUTime and CSTime) in the /proc/[pid]/stat file should have the signed long data type according to the documentation. But currently in the code their data type is just unsigned int. This commit fixes it and adds more tests. See for details: * https://man7.org/linux/man-pages/man5/proc.5.html * https://man7.org/linux/man-pages/man3/scanf.3.html Signed-off-by: Vyacheslav Kulakov --- fixtures.ttar | 2 +- proc_stat.go | 9 +++++++-- proc_stat_test.go | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/fixtures.ttar b/fixtures.ttar index 5e7eeef4a..30df09144 100644 --- a/fixtures.ttar +++ b/fixtures.ttar @@ -589,7 +589,7 @@ SymlinkTo: /does/not/exist # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: fixtures/proc/26232/stat Lines: 1 -33 (ata_sff) S 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 5 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744073709551615 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0 +33 (ata_sff) S 2 0 0 0 -1 69238880 0 0 0 0 0 0 -9223372036854775808 9223372036854775807 0 -20 1 0 5 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744073709551615 0 0 -9223372036854775808 9223372036854775807 0 0 0 0 0 0 0 0 0 0 0 0 0 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: fixtures/proc/26232/wchan diff --git a/proc_stat.go b/proc_stat.go index 8c7b6e80a..ad03d85ef 100644 --- a/proc_stat.go +++ b/proc_stat.go @@ -81,10 +81,10 @@ type ProcStat struct { STime uint // Amount of time that this process's waited-for children have been // scheduled in user mode, measured in clock ticks. - CUTime uint + CUTime int // Amount of time that this process's waited-for children have been // scheduled in kernel mode, measured in clock ticks. - CSTime uint + CSTime int // For processes running a real-time scheduling policy, this is the negated // scheduling priority, minus one. Priority int @@ -141,6 +141,11 @@ func (p Proc) Stat() (ProcStat, error) { } s.Comm = string(data[l+1 : r]) + + // Check the following resources for the details about the particular stat + // fields and their data types: + // * https://man7.org/linux/man-pages/man5/proc.5.html + // * https://man7.org/linux/man-pages/man3/scanf.3.html _, err = fmt.Fscan( bytes.NewBuffer(data[r+2:]), &s.State, diff --git a/proc_stat_test.go b/proc_stat_test.go index 53491050e..8be1e1318 100644 --- a/proc_stat_test.go +++ b/proc_stat_test.go @@ -14,6 +14,7 @@ package procfs import ( + "math" "os" "testing" ) @@ -76,16 +77,30 @@ func TestProcStat(t *testing.T) { } } -func TestProcStatIgnored(t *testing.T) { +func TestProcStatLimits(t *testing.T) { p, err := getProcFixtures(t).Proc(26232) if err != nil { t.Fatal(err) } - _, err = p.Stat() + s, err := p.Stat() if err != nil { t.Errorf("want not error, have %s", err) } + + // max values of stat int fields + for _, test := range []struct { + name string + want int + have int + }{ + {name: "waited for children user time", want: math.MinInt64, have: s.CUTime}, + {name: "waited for children system time", want: math.MaxInt64, have: s.CSTime}, + } { + if test.want != test.have { + t.Errorf("want %s %d, have %d", test.name, test.want, test.have) + } + } } func TestProcStatComm(t *testing.T) {