-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_metrics_linux.go
94 lines (85 loc) · 1.64 KB
/
process_metrics_linux.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//go:build linux
package metrics
import (
"bytes"
"fmt"
"os"
)
// 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
type procStat struct {
State byte
Ppid int
Pgrp int
Session int
TtyNr int
Tpgid int
Flags uint
Minflt uint
Cminflt uint
Majflt uint
Cmajflt uint
Utime uint
Stime uint
Cutime int
Cstime int
Priority int
Nice int
NumThreads int
ItrealValue int
Starttime uint64
Vsize uint
Rss int
}
func (c *processMetricsCollector) Collect(w ExpfmtWriter) {
collectUnix(w)
collectStatMetrics(w)
}
func collectStatMetrics(w ExpfmtWriter) {
data, err := os.ReadFile("/proc/self/stat")
if err != nil {
return
}
// Search for the end of command.
n := bytes.LastIndex(data, []byte(") "))
if n < 0 {
return
}
data = data[n+2:]
var p procStat
bb := bytes.NewBuffer(data)
_, err = fmt.Fscanf(
bb,
"%c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&p.State,
&p.Ppid,
&p.Pgrp,
&p.Session,
&p.TtyNr,
&p.Tpgid,
&p.Flags,
&p.Minflt,
&p.Cminflt,
&p.Majflt,
&p.Cmajflt,
&p.Utime,
&p.Stime,
&p.Cutime,
&p.Cstime,
&p.Priority,
&p.Nice,
&p.NumThreads,
&p.ItrealValue,
&p.Starttime,
&p.Vsize,
&p.Rss,
)
if err != nil {
return
}
w.WriteLazyMetricUint64("process_resident_memory_bytes", uint64(p.Rss)*pageSizeBytes)
w.WriteLazyMetricUint64("process_virtual_memory_bytes", uint64(p.Vsize))
}