process_metrics_linux.go: avoid calling os.Getpagesize() with every writeProcessMetrics() call

It is enough to detect OS page size at the start and then use the detected value.
This removes syscall overhead from every writeProcessMetrics() call.

This is a follow-up for 7b14975dbb
Updates https://github.com/VictoriaMetrics/metrics/pull/76
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6457
This commit is contained in:
Aliaksandr Valialkin 2024-07-16 13:09:14 +02:00
parent 7a44715774
commit eba0da061f
No known key found for this signature in database
GPG Key ID: 52C003EE2BCDB9EB

View File

@ -16,6 +16,11 @@ import (
// See https://github.com/prometheus/procfs/blob/a4ac0826abceb44c40fc71daed2b301db498b93e/proc_stat.go#L40 . // See https://github.com/prometheus/procfs/blob/a4ac0826abceb44c40fc71daed2b301db498b93e/proc_stat.go#L40 .
const userHZ = 100 const userHZ = 100
// Different environments may have different page size.
//
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6457
var pageSizeBytes = uint64(os.Getpagesize())
// See http://man7.org/linux/man-pages/man5/proc.5.html // See http://man7.org/linux/man-pages/man5/proc.5.html
type procStat struct { type procStat struct {
State byte State byte
@ -80,7 +85,7 @@ func writeProcessMetrics(w io.Writer) {
WriteCounterUint64(w, "process_major_pagefaults_total", uint64(p.Majflt)) WriteCounterUint64(w, "process_major_pagefaults_total", uint64(p.Majflt))
WriteCounterUint64(w, "process_minor_pagefaults_total", uint64(p.Minflt)) WriteCounterUint64(w, "process_minor_pagefaults_total", uint64(p.Minflt))
WriteGaugeUint64(w, "process_num_threads", uint64(p.NumThreads)) WriteGaugeUint64(w, "process_num_threads", uint64(p.NumThreads))
WriteGaugeUint64(w, "process_resident_memory_bytes", uint64(p.Rss)*uint64(os.Getpagesize())) WriteGaugeUint64(w, "process_resident_memory_bytes", uint64(p.Rss)*pageSizeBytes)
WriteGaugeUint64(w, "process_start_time_seconds", uint64(startTimeSeconds)) WriteGaugeUint64(w, "process_start_time_seconds", uint64(startTimeSeconds))
WriteGaugeUint64(w, "process_virtual_memory_bytes", uint64(p.Vsize)) WriteGaugeUint64(w, "process_virtual_memory_bytes", uint64(p.Vsize))
writeProcessMemMetrics(w) writeProcessMemMetrics(w)