diff --git a/process/process_linux.go b/process/process_linux.go index a5dc602..f52b700 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -1032,9 +1032,14 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui // There is no such thing as iotime in stat file. As an approximation, we // will use delayacct_blkio_ticks (aggregated block I/O delays, as per Linux // docs). Note: I am assuming at least Linux 2.6.18 - iotime, err := strconv.ParseFloat(fields[42], 64) - if err != nil { - iotime = 0 // Ancient linux version, most likely + var iotime float64 + if len(fields) > 42 { + iotime, err = strconv.ParseFloat(fields[42], 64) + if err != nil { + iotime = 0 // Ancient linux version, most likely + } + } else { + iotime = 0 // e.g. SmartOS containers } cpuTimes := &cpu.TimesStat{ diff --git a/process/process_linux_test.go b/process/process_linux_test.go index e0ff628..9a3dfaa 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -115,3 +115,27 @@ func Test_fillFromStatusWithContext(t *testing.T) { } } } + +func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) { + pids, err := ioutil.ReadDir("testdata/lx_brandz/") + if err != nil { + t.Error(err) + } + f := common.MockEnv("HOST_PROC", "testdata/lx_brandz") + defer f() + for _, pid := range pids { + pid, err := strconv.ParseInt(pid.Name(), 0, 32) + if err != nil { + continue + } + if _, err := os.Stat(fmt.Sprintf("testdata/lx_brandz/%d/stat", pid)); err != nil { + continue + } + p, _ := NewProcess(int32(pid)) + _, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStatWithContext(context.Background(), -1) + if err != nil { + t.Error(err) + } + assert.Equal(t, float64(0), cpuTimes.Iowait) + } +} diff --git a/process/testdata/lx_brandz/1/stat b/process/testdata/lx_brandz/1/stat new file mode 100644 index 0000000..82f6062 --- /dev/null +++ b/process/testdata/lx_brandz/1/stat @@ -0,0 +1 @@ +1 (systemd) S 0 0 0 0 -1 0 0 0 0 0 8 15 48 52 1 0 0 0 25 31883264 1413 18446744073709551615 0 0 140737487261696 0 0 0 0 0 0 18446741901776689794 0 0 17 0 diff --git a/v3/process/process_linux.go b/v3/process/process_linux.go index 74549b1..5bf3515 100644 --- a/v3/process/process_linux.go +++ b/v3/process/process_linux.go @@ -1027,9 +1027,14 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui // There is no such thing as iotime in stat file. As an approximation, we // will use delayacct_blkio_ticks (aggregated block I/O delays, as per Linux // docs). Note: I am assuming at least Linux 2.6.18 - iotime, err := strconv.ParseFloat(fields[42], 64) - if err != nil { - iotime = 0 // Ancient linux version, most likely + var iotime float64 + if len(fields) > 42 { + iotime, err = strconv.ParseFloat(fields[42], 64) + if err != nil { + iotime = 0 // Ancient linux version, most likely + } + } else { + iotime = 0 // e.g. SmartOS containers } cpuTimes := &cpu.TimesStat{ diff --git a/v3/process/process_linux_test.go b/v3/process/process_linux_test.go index 2bc65d6..7519a4f 100644 --- a/v3/process/process_linux_test.go +++ b/v3/process/process_linux_test.go @@ -114,4 +114,28 @@ func Test_fillFromStatusWithContext(t *testing.T) { t.Error(err) } } -} \ No newline at end of file +} + +func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) { + pids, err := ioutil.ReadDir("testdata/lx_brandz/") + if err != nil { + t.Error(err) + } + f := common.MockEnv("HOST_PROC", "testdata/lx_brandz") + defer f() + for _, pid := range pids { + pid, err := strconv.ParseInt(pid.Name(), 0, 32) + if err != nil { + continue + } + if _, err := os.Stat(fmt.Sprintf("testdata/lx_brandz/%d/stat", pid)); err != nil { + continue + } + p, _ := NewProcess(int32(pid)) + _, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStatWithContext(context.Background(), -1) + if err != nil { + t.Error(err) + } + assert.Equal(t, float64(0), cpuTimes.Iowait) + } +} diff --git a/v3/process/testdata/lx_brandz/1/stat b/v3/process/testdata/lx_brandz/1/stat new file mode 100644 index 0000000..82f6062 --- /dev/null +++ b/v3/process/testdata/lx_brandz/1/stat @@ -0,0 +1 @@ +1 (systemd) S 0 0 0 0 -1 0 0 0 0 0 8 15 48 52 1 0 0 0 25 31883264 1413 18446744073709551615 0 0 140737487261696 0 0 0 0 0 0 18446741901776689794 0 0 17 0