Skip to content

Commit

Permalink
Fix data types for CUTime and CSTime stat fields #403 (#404)
Browse files Browse the repository at this point in the history
* 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 <kulakov.home@gmail.com>
  • Loading branch information
vykulakov committed Aug 30, 2021
1 parent 4d08435 commit 21d221e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fixtures.ttar
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions proc_stat.go
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 17 additions & 2 deletions proc_stat_test.go
Expand Up @@ -14,6 +14,7 @@
package procfs

import (
"math"
"os"
"testing"
)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 21d221e

Please sign in to comment.