-
Notifications
You must be signed in to change notification settings - Fork 460
/
cpu.go
58 lines (50 loc) · 1.42 KB
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package perf
import (
"github.com/prometheus/procfs"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/perf"
)
// CPU adapter provides conversion from procfs.
//
//nolint:revive,golint
func CPU(r *perf.CPU) cpu {
return cpu{
CPU: r,
}
}
type cpu struct {
*perf.CPU
}
// Update current CPU snapshot.
func (a cpu) Update(stat *procfs.Stat) {
translateCPUStat := func(in procfs.CPUStat) perf.CPUStat {
return perf.CPUStat{
User: in.User,
Nice: in.Nice,
System: in.System,
Idle: in.Idle,
Iowait: in.Iowait,
Irq: in.IRQ,
SoftIrq: in.SoftIRQ,
Steal: in.Steal,
Guest: in.Guest,
GuestNice: in.GuestNice,
}
}
translateListOfCPUStat := func(in []procfs.CPUStat) []perf.CPUStat {
return slices.Map(in, translateCPUStat)
}
*a.CPU.TypedSpec() = perf.CPUSpec{
CPUTotal: translateCPUStat(stat.CPUTotal),
CPU: translateListOfCPUStat(stat.CPU),
IRQTotal: stat.IRQTotal,
ContextSwitches: stat.ContextSwitches,
ProcessCreated: stat.ProcessCreated,
ProcessRunning: stat.ProcessesRunning,
ProcessBlocked: stat.ProcessesBlocked,
SoftIrqTotal: stat.SoftIRQTotal,
}
}