Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data types for CUTime and CSTime stat fields #403 #404

Merged
merged 4 commits into from Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
43 changes: 24 additions & 19 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 @@ -128,8 +128,8 @@ func (p Proc) Stat() (ProcStat, error) {
}

var (
ignoreInt64 int64
ignoreUint64 uint64
ignoreInt int
ignoreUint uint
SuperQ marked this conversation as resolved.
Show resolved Hide resolved

s = ProcStat{PID: p.PID, proc: p.fs}
l = bytes.Index(data, []byte("("))
Expand All @@ -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 All @@ -161,25 +166,25 @@ func (p Proc) Stat() (ProcStat, error) {
&s.Priority,
&s.Nice,
&s.NumThreads,
&ignoreInt64,
&ignoreInt,
&s.Starttime,
&s.VSize,
&s.RSS,
&s.RSSLimit,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreUint64,
&ignoreInt64,
&ignoreInt64,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreUint,
&ignoreInt,
&ignoreInt,
&s.RTPriority,
&s.Policy,
&s.DelayAcctBlkIOTicks,
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},
Comment on lines +97 to +98
Copy link
Contributor

@dswarbrick dswarbrick Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using MinInt64 / MaxInt64 here causes tests to fail on 32-bit archs, since these are untyped constants, being assigned to machine-native ints (in struct a few lines up).

Additionally, the supplied test data will simply overflow the ProcStat.CUTime and ProcStat.CSTime members, since they are also declared as machine-native ints. Either the struct needs to explicitly use int64 types, or you need to use 32-bit safe test data.

Incidentally, this is not the first time that there has been a test regression for 32-bit arch in this package. It would be nice if the CI pipeline would also test on 32-bit archs, as I presume they are still officially supported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, do you want to open an issue for testing on 32 bit archs? We need to see how to make that possible (let alone who has time to work on it) but agreed that we should track that at least.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to reproduce the 32-bit test failure simply by exporting GOARCH=386 before running the tests.

} {
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