Skip to content

Commit 72cbb2f

Browse files
committed
process_metrics_linux.go: avoid precision loss when calculating process_cpu_seconds_total metric value
Previously the sum of two floating-point numbers - utime and stime - could lead to precision loss, which is usually visible as too many digits after the floating point such as 1.29999999999999 instead of 1.3 or 2.00000000000001 instead of 2. This should improve the compression rate at VictoriaMetrics side for this metric.
1 parent 80e6b1a commit 72cbb2f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

process_metrics_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ func writeProcessMetrics(w io.Writer) {
7979

8080
utime := float64(p.Utime) / userHZ
8181
stime := float64(p.Stime) / userHZ
82+
83+
// Calculate totalTime by dividing the sum of p.Utime and p.Stime by userHZ.
84+
// This reduces possible floating-point precision loss
85+
totalTime := float64(p.Utime+p.Stime) / userHZ
86+
8287
WriteCounterFloat64(w, "process_cpu_seconds_system_total", stime)
83-
WriteCounterFloat64(w, "process_cpu_seconds_total", utime+stime)
88+
WriteCounterFloat64(w, "process_cpu_seconds_total", totalTime)
8489
WriteCounterFloat64(w, "process_cpu_seconds_user_total", utime)
8590
WriteCounterUint64(w, "process_major_pagefaults_total", uint64(p.Majflt))
8691
WriteCounterUint64(w, "process_minor_pagefaults_total", uint64(p.Minflt))

0 commit comments

Comments
 (0)