Add ExLinux to cpu package to expose raw tick counters from /proc/stat
Is your feature request related to a problem? Please describe.
TimesWithContext on Linux reads /proc/stat and immediately converts the integer tick counters to float64 seconds by dividing by ClocksPerSec. This means callers that want to compute utilization ratios from integer deltas have no way to do so, the original uint64 values are gone before the function returns.
This matters when computing delta / total for CPU utilization: if the subtraction happens on the already-scaled float64 values, the result varies slightly depending on the absolute counter position even when the tick delta is identical. Computing the ratio from integer deltas (uint64 - uint64) avoids that position-dependent noise.
This would be very helpful for an upstream consumer (open-telemetry/opentelemetry-collector-contrib#47972), as we ended up shipping our own /proc/stat parser inside the receiver because there is no raw-ticks API here.
Describe the solution you'd like
Add cpu/ex_linux.go following the same pattern as mem/ex_linux.go and sensors/ex_linux.go:
type TimesStatEx struct {
CPU string `json:"cpu"`
User uint64 `json:"user"`
// ... same fields as TimesStat but uint64
}
type ExLinux struct{}
func NewExLinux() *ExLinux { return &ExLinux{} }
func (*ExLinux) TimesWithContext(ctx context.Context, percpu bool) ([]TimesStatEx, error) {
// reads /proc/stat with strconv.ParseUint, no ClocksPerSec division
}ClocksPerSec is already exported so consumers can do their own unit conversion when needed.
Additional context
See open-telemetry/opentelemetry-collector-contrib#47972
Source: shirou/gopsutil