Skip to content

Commit 2871d29

Browse files
First refactor of the system module - system/cpu and system/core (#25771)
* initial commit * finux linux refactor * fix up freebsd * port main metricset, start openbsd * start work on openbsd vagrantfile * refactors of API, add darwin support * fix darwin implementation * refactor API, move tests, remove old code, take a crack at AIX * fix aix init func * fix tests * regenerate core data.json * small fixes, fix host field * update tests * run correct mage commands * try to fix system tests * more fixes for windows python tests * refactor CPU struct, use reflection * refactor reflection code, add validation * move metrics to its own internal folder * move directories, again * move directories, again * use optional Uint type * cleanup opt files * move around naming of opt types * fix up if block * change opt names * move around opt methods, cpu stat reader refactor * fix IsZero usage * add changelog
1 parent cefaeaf commit 2871d29

28 files changed

+1312
-545
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
499499
- Change vsphere.datastore.capacity.used.pct value to betweeen 0 and 1. {pull}23148[23148]
500500
- Update config in `windows.yml` file. {issue}23027[23027]{pull}23327[23327]
501501
- Fix metric grouping for windows/perfmon module {issue}23489[23489] {pull}23505[23505]
502+
- Major refactor of system/cpu and system/core metrics. {pull}25771[25771]
502503

503504
*Packetbeat*
504505

Vagrantfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ Vagrant.configure("2") do |config|
307307
c.vm.provision "shell", inline: $unixProvision, privileged: false
308308
c.vm.provision "shell", inline: $freebsdShellUpdate, privileged: true
309309
c.vm.provision "shell", inline: gvmProvision(arch="amd64", os="freebsd"), privileged: false
310+
c.vm.provision "shell", inline: "sudo mount -t linprocfs /dev/null /proc", privileged: false
310311
end
311312

312313
# OpenBSD 6.0

libbeat/metric/system/cpu/cpu.go

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -26,165 +26,6 @@ import (
2626
sigar "github.com/elastic/gosigar"
2727
)
2828

29-
// CPU Monitor
30-
31-
// Monitor is used to monitor the overall CPU usage of the system.
32-
type Monitor struct {
33-
lastSample *sigar.Cpu
34-
}
35-
36-
// Sample collects a new sample of the CPU usage metrics.
37-
func (m *Monitor) Sample() (*Metrics, error) {
38-
cpuSample := &sigar.Cpu{}
39-
if err := cpuSample.Get(); err != nil {
40-
return nil, err
41-
}
42-
43-
oldLastSample := m.lastSample
44-
m.lastSample = cpuSample
45-
return &Metrics{oldLastSample, cpuSample}, nil
46-
}
47-
48-
// Percentages stores all CPU values in percentages collected by a Beat.
49-
type Percentages struct {
50-
User float64
51-
System float64
52-
Idle float64
53-
IOWait float64
54-
IRQ float64
55-
Nice float64
56-
SoftIRQ float64
57-
Steal float64
58-
Total float64
59-
}
60-
61-
// Ticks stores all CPU values in number of tick collected by a Beat.
62-
type Ticks struct {
63-
User uint64
64-
System uint64
65-
Idle uint64
66-
IOWait uint64
67-
IRQ uint64
68-
Nice uint64
69-
SoftIRQ uint64
70-
Steal uint64
71-
}
72-
73-
// Metrics stores the current and the last sample collected by a Beat.
74-
type Metrics struct {
75-
previousSample *sigar.Cpu
76-
currentSample *sigar.Cpu
77-
}
78-
79-
// NormalizedPercentages returns CPU percentage usage information that is
80-
// normalized by the number of CPU cores. The values will range from
81-
// 0 to 100%.
82-
func (m *Metrics) NormalizedPercentages() Percentages {
83-
return cpuPercentages(m.previousSample, m.currentSample, 1)
84-
}
85-
86-
// Percentages returns CPU percentage usage information. The values range from
87-
// 0 to 100% * NumCPU.
88-
func (m *Metrics) Percentages() Percentages {
89-
return cpuPercentages(m.previousSample, m.currentSample, runtime.NumCPU())
90-
}
91-
92-
// cpuPercentages calculates the amount of CPU time used between the two given
93-
// samples. The CPU percentages are divided by given numCPU value and rounded
94-
// using Round.
95-
func cpuPercentages(s0, s1 *sigar.Cpu, numCPU int) Percentages {
96-
if s0 == nil || s1 == nil {
97-
return Percentages{}
98-
}
99-
100-
// timeDelta is the total amount of CPU time available across all CPU cores.
101-
timeDelta := s1.Total() - s0.Total()
102-
if timeDelta <= 0 {
103-
return Percentages{}
104-
}
105-
106-
calculatePct := func(v0, v1 uint64) float64 {
107-
cpuDelta := int64(v1 - v0)
108-
pct := float64(cpuDelta) / float64(timeDelta)
109-
return common.Round(pct*float64(numCPU), common.DefaultDecimalPlacesCount)
110-
}
111-
112-
calculateTotalPct := func() float64 {
113-
// IOWait time is excluded from the total as per #7627.
114-
idle := calculatePct(s0.Idle, s1.Idle) + calculatePct(s0.Wait, s1.Wait)
115-
return common.Round(float64(numCPU)-idle, common.DefaultDecimalPlacesCount)
116-
}
117-
118-
return Percentages{
119-
User: calculatePct(s0.User, s1.User),
120-
System: calculatePct(s0.Sys, s1.Sys),
121-
Idle: calculatePct(s0.Idle, s1.Idle),
122-
IOWait: calculatePct(s0.Wait, s1.Wait),
123-
IRQ: calculatePct(s0.Irq, s1.Irq),
124-
Nice: calculatePct(s0.Nice, s1.Nice),
125-
SoftIRQ: calculatePct(s0.SoftIrq, s1.SoftIrq),
126-
Steal: calculatePct(s0.Stolen, s1.Stolen),
127-
Total: calculateTotalPct(),
128-
}
129-
}
130-
131-
// Ticks returns the number of CPU ticks from the last collected sample.
132-
func (m *Metrics) Ticks() Ticks {
133-
return Ticks{
134-
User: m.currentSample.User,
135-
System: m.currentSample.Sys,
136-
Idle: m.currentSample.Idle,
137-
IOWait: m.currentSample.Wait,
138-
IRQ: m.currentSample.Irq,
139-
Nice: m.currentSample.Nice,
140-
SoftIRQ: m.currentSample.SoftIrq,
141-
Steal: m.currentSample.Stolen,
142-
}
143-
}
144-
145-
// CPU Core Monitor
146-
147-
// CoreMetrics is used to monitor the usage of individual CPU cores.
148-
type CoreMetrics Metrics
149-
150-
// Percentages returns CPU percentage usage information for the core. The values
151-
// range from [0, 100%].
152-
func (m *CoreMetrics) Percentages() Percentages { return (*Metrics)(m).NormalizedPercentages() }
153-
154-
// Ticks returns the raw number of "ticks". The value is a counter (though it
155-
// may roll overfunc (m *CoreMetrics) Ticks() Ticks { return (*Metrics)(m).Ticks() }
156-
func (m *CoreMetrics) Ticks() Ticks { return (*Metrics)(m).Ticks() }
157-
158-
// CoresMonitor is used to monitor the usage information of all the CPU
159-
// cores in the system.
160-
type CoresMonitor struct {
161-
lastSample []sigar.Cpu
162-
}
163-
164-
// Sample collects a new sample of the metrics from all CPU cores.
165-
func (m *CoresMonitor) Sample() ([]CoreMetrics, error) {
166-
var cores sigar.CpuList
167-
if err := cores.Get(); err != nil {
168-
return nil, err
169-
}
170-
171-
lastSample := m.lastSample
172-
m.lastSample = cores.List
173-
174-
cpuMetrics := make([]CoreMetrics, len(cores.List))
175-
for i := 0; i < len(cores.List); i++ {
176-
if len(lastSample) > i {
177-
cpuMetrics[i] = CoreMetrics{&lastSample[i], &cores.List[i]}
178-
} else {
179-
cpuMetrics[i] = CoreMetrics{nil, &cores.List[i]}
180-
}
181-
}
182-
183-
return cpuMetrics, nil
184-
}
185-
186-
// CPU Load
187-
18829
// Load returns CPU load information for the previous 1, 5, and 15 minute
18930
// periods.
19031
func Load() (*LoadMetrics, error) {

libbeat/metric/system/cpu/cpu_test.go

Lines changed: 0 additions & 147 deletions
This file was deleted.

metricbeat/docs/fields.asciidoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49313,6 +49313,18 @@ type: long
4931349313

4931449314
--
4931549315

49316+
*`system.core.total.pct`*::
49317+
+
49318+
--
49319+
Total active time spent by the core
49320+
49321+
49322+
type: scaled_float
49323+
49324+
format: percent
49325+
49326+
--
49327+
4931649328
*`system.core.user.pct`*::
4931749329
+
4931849330
--
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cpu

0 commit comments

Comments
 (0)