Skip to content

Commit 0e88176

Browse files
AIX: Fix physical cpu usage calculation
Signed-off-by: Johannes Ziemke <[email protected]>
1 parent 940e73b commit 0e88176

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

collector/cpu_aix.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,44 @@ type cpuCollector struct {
5959
cpuFlags typedDesc
6060
cpuContextSwitch typedDesc
6161

62-
logger *slog.Logger
63-
tickPerSecond int64
62+
logger *slog.Logger
63+
tickPerSecond float64
64+
purrTicksPerSecond float64
6465
}
6566

6667
func init() {
6768
registerCollector("cpu", defaultEnabled, NewCpuCollector)
6869
}
6970

70-
func tickPerSecond() (int64, error) {
71+
func tickPerSecond() (float64, error) {
7172
ticks, err := C.sysconf(C._SC_CLK_TCK)
7273
if ticks == -1 || err != nil {
7374
return 0, fmt.Errorf("failed to get clock ticks per second: %v", err)
7475
}
75-
return int64(ticks), nil
76+
return float64(ticks), nil
7677
}
7778

7879
func NewCpuCollector(logger *slog.Logger) (Collector, error) {
7980
ticks, err := tickPerSecond()
8081
if err != nil {
8182
return nil, err
8283
}
84+
85+
pconfig, err := perfstat.PartitionStat()
86+
87+
if err != nil {
88+
return nil, err
89+
}
90+
8391
return &cpuCollector{
84-
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
85-
cpuPhysical: typedDesc{nodeCPUPhysicalSecondsDesc, prometheus.CounterValue},
86-
cpuRunQueue: typedDesc{nodeCPUSRunQueueDesc, prometheus.GaugeValue},
87-
cpuFlags: typedDesc{nodeCPUFlagsDesc, prometheus.GaugeValue},
88-
cpuContextSwitch: typedDesc{nodeCPUContextSwitchDesc, prometheus.CounterValue},
89-
logger: logger,
90-
tickPerSecond: ticks,
92+
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
93+
cpuPhysical: typedDesc{nodeCPUPhysicalSecondsDesc, prometheus.CounterValue},
94+
cpuRunQueue: typedDesc{nodeCPUSRunQueueDesc, prometheus.GaugeValue},
95+
cpuFlags: typedDesc{nodeCPUFlagsDesc, prometheus.GaugeValue},
96+
cpuContextSwitch: typedDesc{nodeCPUContextSwitchDesc, prometheus.CounterValue},
97+
logger: logger,
98+
tickPerSecond: ticks,
99+
purrTicksPerSecond: float64(pconfig.ProcessorMhz * 1e6),
91100
}, nil
92101
}
93102

@@ -99,16 +108,16 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
99108

100109
for n, stat := range stats {
101110
// LPAR metrics
102-
ch <- c.cpu.mustNewConstMetric(float64(stat.User/c.tickPerSecond), strconv.Itoa(n), "user")
103-
ch <- c.cpu.mustNewConstMetric(float64(stat.Sys/c.tickPerSecond), strconv.Itoa(n), "system")
104-
ch <- c.cpu.mustNewConstMetric(float64(stat.Idle/c.tickPerSecond), strconv.Itoa(n), "idle")
105-
ch <- c.cpu.mustNewConstMetric(float64(stat.Wait/c.tickPerSecond), strconv.Itoa(n), "wait")
111+
ch <- c.cpu.mustNewConstMetric(float64(stat.User)/c.tickPerSecond, strconv.Itoa(n), "user")
112+
ch <- c.cpu.mustNewConstMetric(float64(stat.Sys)/c.tickPerSecond, strconv.Itoa(n), "system")
113+
ch <- c.cpu.mustNewConstMetric(float64(stat.Idle)/c.tickPerSecond, strconv.Itoa(n), "idle")
114+
ch <- c.cpu.mustNewConstMetric(float64(stat.Wait)/c.tickPerSecond, strconv.Itoa(n), "wait")
106115

107116
// Physical CPU metrics
108-
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PIdle/c.tickPerSecond), strconv.Itoa(n), "pidle")
109-
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PUser/c.tickPerSecond), strconv.Itoa(n), "puser")
110-
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PSys/c.tickPerSecond), strconv.Itoa(n), "psys")
111-
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PWait/c.tickPerSecond), strconv.Itoa(n), "pwait")
117+
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PIdle)/c.purrTicksPerSecond, strconv.Itoa(n), "pidle")
118+
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PUser)/c.purrTicksPerSecond, strconv.Itoa(n), "puser")
119+
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PSys)/c.purrTicksPerSecond, strconv.Itoa(n), "psys")
120+
ch <- c.cpuPhysical.mustNewConstMetric(float64(stat.PWait)/c.purrTicksPerSecond, strconv.Itoa(n), "pwait")
112121

113122
// Run queue length
114123
ch <- c.cpuRunQueue.mustNewConstMetric(float64(stat.RunQueue), strconv.Itoa(n))

collector/diskstats_aix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type diskstatsCollector struct {
4545
deviceFilter deviceFilter
4646
logger *slog.Logger
4747

48-
tickPerSecond int64
48+
tickPerSecond float64
4949
}
5050

5151
func init() {
@@ -151,14 +151,14 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
151151
}
152152
ch <- c.rbytes.mustNewConstMetric(float64(stat.Rblks*512), stat.Name)
153153
ch <- c.wbytes.mustNewConstMetric(float64(stat.Wblks*512), stat.Name)
154-
ch <- c.time.mustNewConstMetric(float64(stat.Time/c.tickPerSecond), stat.Name)
154+
ch <- c.time.mustNewConstMetric(float64(stat.Time)/c.tickPerSecond, stat.Name)
155155

156156
ch <- c.bsize.mustNewConstMetric(float64(stat.BSize), stat.Name)
157157
ch <- c.qdepth.mustNewConstMetric(float64(stat.QDepth), stat.Name)
158158
ch <- c.rblks.mustNewConstMetric(float64(stat.Rblks), stat.Name)
159159
ch <- c.wblks.mustNewConstMetric(float64(stat.Wblks), stat.Name)
160-
ch <- c.rserv.mustNewConstMetric(float64(stat.Rserv/c.tickPerSecond), stat.Name)
161-
ch <- c.wserv.mustNewConstMetric(float64(stat.Wserv/c.tickPerSecond), stat.Name)
160+
ch <- c.rserv.mustNewConstMetric(float64(stat.Rserv)/c.tickPerSecond, stat.Name)
161+
ch <- c.wserv.mustNewConstMetric(float64(stat.Wserv)/c.tickPerSecond, stat.Name)
162162
ch <- c.xfers.mustNewConstMetric(float64(stat.Xfers), stat.Name)
163163
ch <- c.xrate.mustNewConstMetric(float64(stat.XRate), stat.Name)
164164
}

0 commit comments

Comments
 (0)