Skip to content

Commit

Permalink
Merge pull request #40 from fearful-symmetry/process-bugs
Browse files Browse the repository at this point in the history
Add IsZero to CPU Totals, report value for monitoring
  • Loading branch information
fearful-symmetry authored Jul 12, 2022
2 parents 8bd788c + d4f2387 commit 5f48574
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

## [0.4.3]

## Fixed

- Add IsZero to CPU Totals, report value for monitoring #40

## [0.4.2]

## Fixed
Expand Down
1 change: 0 additions & 1 deletion metric/system/process/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func GetProcCPUPercentage(s0, s1 ProcState) ProcState {

s1.CPU.Total.Norm.Pct = opt.FloatWith(metric.Round(normalizedPct))
s1.CPU.Total.Pct = opt.FloatWith(metric.Round(pct))
s1.CPU.Total.Value = opt.FloatWith(metric.Round(float64(s1.CPU.Total.Ticks.ValueOr(0))))

return s1

Expand Down
6 changes: 5 additions & 1 deletion metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/opt"
"github.com/elastic/elastic-agent-libs/transform/typeconv"
"github.com/elastic/elastic-agent-system-metrics/metric"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
)

Expand Down Expand Up @@ -133,7 +134,7 @@ func (procStats *Stats) GetSelf() (ProcState, error) {
if err != nil {
return ProcState{}, fmt.Errorf("error fetching PID %d: %w", self, err)
}

procStats.ProcsMap[self] = pidStat
return pidStat, nil
}

Expand Down Expand Up @@ -202,6 +203,9 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
}
} // end cgroups processor

if status.CPU.Total.Ticks.Exists() {
status.CPU.Total.Value = opt.FloatWith(metric.Round(float64(status.CPU.Total.Ticks.ValueOr(0))))
}
if ok {
status = GetProcCPUPercentage(last, status)
}
Expand Down
19 changes: 18 additions & 1 deletion metric/system/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
Expand Down Expand Up @@ -118,6 +119,23 @@ func TestProcessList(t *testing.T) {
}
}

func TestSelfPersist(t *testing.T) {
stat, err := initTestResolver()
require.NoError(t, err, "Init()")
first, err := stat.GetSelf()
require.NoError(t, err, "First GetSelf()")

// The first process fetch shouldn't have percentages, since we don't have >1 procs to compare
assert.False(t, first.CPU.Total.Pct.Exists(), "total.pct should not exist")
// Create a proper time delay so the CPU percentage delta calculations don't fail
time.Sleep(time.Millisecond * 5)
second, err := stat.GetSelf()
require.NoError(t, err, "Second GetSelf()")

// now it should exist
assert.True(t, second.CPU.Total.Pct.Exists(), "total.pct should exist")
}

func TestGetProcess(t *testing.T) {
stat, err := initTestResolver()
assert.NoError(t, err, "Init()")
Expand Down Expand Up @@ -227,7 +245,6 @@ func TestProcCpuPercentage(t *testing.T) {

assert.EqualValues(t, 0.0721, normalizedTest)
assert.EqualValues(t, 3.459, newState.CPU.Total.Pct.ValueOr(0))
assert.EqualValues(t, 14841, newState.CPU.Total.Value.ValueOr(0))
}

// BenchmarkGetProcess runs a benchmark of the GetProcess method with caching
Expand Down
4 changes: 4 additions & 0 deletions metric/system/process/process_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ type ProcLimits struct {

// Implementations

func (t CPUTotal) IsZero() bool {
return t.Value.IsZero() && t.Ticks.IsZero() && t.Pct.IsZero() && t.Norm.IsZero()
}

// IsZero returns true if the underlying value nil
func (t CPUTicks) IsZero() bool {
return t.Ticks.IsZero()
Expand Down

0 comments on commit 5f48574

Please sign in to comment.