Skip to content

Commit 67e2064

Browse files
andrewkrohtsg
authored andcommitted
Scale system.cpu.*.pct metrics by the number of cores (#4544)
Change all `system.cpu.*.pct` metrics to be scaled by the number of CPU cores such that the values range on `[0, 100% * number_of_cores]`. This will make the CPU usage percentages from the system cpu metricset consistent with the system process metricset. The documentation for these metrics already stated that on multi-core systems the percentages could be greater than 100%. This makes the code match the docs, but does cause a change in behavior to the user.
1 parent 10556fb commit 67e2064

6 files changed

Lines changed: 27 additions & 14 deletions

File tree

CHANGELOG.asciidoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Template, add newest changes here
99

1010
=== Beats version HEAD
11-
https://github.com/elastic/beats/compare/v5.4.1...master[Check the HEAD diff]
11+
https://github.com/elastic/beats/compare/v5.5.0...master[Check the HEAD diff]
1212

1313
==== Breaking changes
1414

@@ -92,6 +92,12 @@ https://github.com/elastic/beats/compare/v5.4.2...v5.5.0[View commits]
9292
9393
- Usage of field `_type` is now ignored and hardcoded to `doc`. {pull}3757[3757]
9494
95+
*Metricbeat*
96+
- Change all `system.cpu.*.pct` metrics to be scaled by the number of CPU cores.
97+
This will make the CPU usage percentages from the system cpu metricset consistent
98+
with the system process metricset. The documentation for these metrics already
99+
stated that on multi-core systems the percentages could be greater than 100%. {pull}4544[4544]
100+
95101
==== Bugfixes
96102
97103
*Affecting all Beats*

metricbeat/docs/fields.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5601,7 +5601,7 @@ The amount of CPU time spent in involuntary wait by the virtual CPU while the hy
56015601
56025602
type: long
56035603
5604-
The number of CPU cores.
5604+
The number of CPU cores. The CPU percentages can range from `[0, 100% * cores]`.
56055605
56065606
56075607
[float]
@@ -5611,7 +5611,7 @@ type: scaled_float
56115611
56125612
format: percent
56135613
5614-
The percentage of CPU time spent in user space. On multi-core systems, you can have percentages that are greater than 100%. For example, if 3 cores are at 60% use, then the `cpu.user_p` will be 180%.
5614+
The percentage of CPU time spent in user space. On multi-core systems, you can have percentages that are greater than 100%. For example, if 3 cores are at 60% use, then the `system.cpu.user.pct` will be 180%.
56155615
56165616
56175617
[float]

metricbeat/module/system/cpu/_meta/data.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
},
1212
"system": {
1313
"cpu": {
14+
"cores": 8,
1415
"idle": {
15-
"pct": 0.852,
16-
"ticks": 44421033
16+
"pct": 7.0854,
17+
"ticks": 1617015818
1718
},
1819
"iowait": {
1920
"pct": 0,
20-
"ticks": 159735
21+
"ticks": 0
2122
},
2223
"irq": {
2324
"pct": 0,
@@ -29,19 +30,19 @@
2930
},
3031
"softirq": {
3132
"pct": 0,
32-
"ticks": 14070
33+
"ticks": 0
3334
},
3435
"steal": {
3536
"pct": 0,
3637
"ticks": 0
3738
},
3839
"system": {
39-
"pct": 0.0408,
40-
"ticks": 305704
40+
"pct": 0.3317,
41+
"ticks": 40488863
4142
},
4243
"user": {
43-
"pct": 0.1071,
44-
"ticks": 841974
44+
"pct": 0.5829,
45+
"ticks": 48194733
4546
}
4647
}
4748
},

metricbeat/module/system/cpu/_meta/fields.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
- name: cores
77
type: long
88
description: >
9-
The number of CPU cores.
9+
The number of CPU cores. The CPU percentages can range from `[0, 100% * cores]`.
1010
1111
- name: user.pct
1212
type: scaled_float
1313
format: percent
1414
description: >
1515
The percentage of CPU time spent in user space. On multi-core systems, you can have percentages that are greater than 100%.
16-
For example, if 3 cores are at 60% use, then the `cpu.user_p` will be 180%.
16+
For example, if 3 cores are at 60% use, then the `system.cpu.user.pct` will be 180%.
1717
1818
- name: system.pct
1919
type: scaled_float

metricbeat/module/system/cpu/helper.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
sigar "github.com/elastic/gosigar"
1010
)
1111

12+
// NumCPU is the number of CPU cores the system has.
13+
var NumCPU = runtime.NumCPU()
14+
1215
type CPU struct {
1316
CpuPerCore bool
1417
LastCpuTimes *CpuTimes
@@ -72,7 +75,7 @@ func GetCpuPercentage(last *CpuTimes, current *CpuTimes) *CpuTimes {
7275
perc := 0.0
7376
delta := int64(field2 - field1)
7477
perc = float64(delta) / float64(allDelta)
75-
return system.Round(perc, .5, 4)
78+
return system.Round(perc*float64(NumCPU), .5, 4)
7679
}
7780

7881
current.UserPercent = calculate(current.Cpu.User, last.Cpu.User)

metricbeat/module/system/cpu/helper_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cpu
55

66
import (
7+
"runtime"
78
"testing"
89

910
"github.com/elastic/gosigar"
@@ -21,6 +22,8 @@ func TestGetCpuTimes(t *testing.T) {
2122
}
2223

2324
func TestCpuPercentage(t *testing.T) {
25+
NumCPU = 1
26+
defer func() { NumCPU = runtime.NumCPU() }()
2427

2528
cpu := CPU{}
2629

0 commit comments

Comments
 (0)