Skip to content

Commit c1467f0

Browse files
committed
fix NaN issue for HostCPU
1 parent 6b11a5c commit c1467f0

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

cli/clistat/stat.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ func New(opts ...Option) (*Statter, error) {
178178
// Units are in "cores".
179179
func (s *Statter) HostCPU(m Prefix) (*Result, error) {
180180
r := &Result{
181-
Unit: "cores",
182-
Total: ptr.To(float64(s.nproc)),
181+
Unit: "cores",
182+
Total: ptr.To(float64(s.nproc)),
183+
Prefix: m,
183184
}
184185
c1, err := s.hi.CPUTime()
185186
if err != nil {
@@ -191,11 +192,13 @@ func (s *Statter) HostCPU(m Prefix) (*Result, error) {
191192
return nil, xerrors.Errorf("get second cpu sample: %w", err)
192193
}
193194
total := c2.Total() - c1.Total()
195+
if total == 0 {
196+
return r, nil // no change
197+
}
194198
idle := c2.Idle - c1.Idle
195199
used := total - idle
196200
scaleFactor := float64(s.nproc) / total.Seconds()
197201
r.Used = used.Seconds() * scaleFactor
198-
r.Prefix = m
199202
return r, nil
200203
}
201204

0 commit comments

Comments
 (0)