-
Notifications
You must be signed in to change notification settings - Fork 5
/
analytics.go
138 lines (123 loc) · 3.85 KB
/
analytics.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"os"
)
const ANALYTICS_LIMIT = 1000
type Analytics struct {
OpenFiles []int
Threads []uint
Vsize []uint64
ResidentMem []uint64
SharedMem []uint64
TextMem []uint64
DataMem []uint64
PID int
}
func NewAnalytics() Analytics {
return Analytics{
OpenFiles: make([]int, 0, ANALYTICS_LIMIT),
Threads: make([]uint, 0, ANALYTICS_LIMIT),
Vsize: make([]uint64, 0, ANALYTICS_LIMIT),
ResidentMem: make([]uint64, 0, ANALYTICS_LIMIT),
SharedMem: make([]uint64, 0, ANALYTICS_LIMIT),
TextMem: make([]uint64, 0, ANALYTICS_LIMIT),
DataMem: make([]uint64, 0, ANALYTICS_LIMIT),
}
}
func (a *Analytics) Collect(pid int) {
if pid != a.PID {
a.PID = pid
a.Clear()
}
a.openFiles(pid)
a.stat(pid)
a.statm(pid)
}
func (a *Analytics) Clear() {
a.OpenFiles = make([]int, 0, ANALYTICS_LIMIT)
a.Threads = make([]uint, 0, ANALYTICS_LIMIT)
a.Vsize = make([]uint64, 0, ANALYTICS_LIMIT)
a.ResidentMem = make([]uint64, 0, ANALYTICS_LIMIT)
a.SharedMem = make([]uint64, 0, ANALYTICS_LIMIT)
a.TextMem = make([]uint64, 0, ANALYTICS_LIMIT)
a.DataMem = make([]uint64, 0, ANALYTICS_LIMIT)
}
func (a *Analytics) openFiles(pid int) {
f, err := os.Open(fmt.Sprintf("/proc/%d/fd", pid))
if err != nil {
return
}
defer f.Close()
dirs, _ := f.ReadDir(-1)
limitedAppend(&a.OpenFiles, len(dirs))
}
// https://man.archlinux.org/man/proc.5.en
func (a *Analytics) stat(pid int) {
s, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
return
}
var (
comm string
state rune
ppid, pgrp, session, tty_nr, tpgid int
flags uint
minflt, cminflt, majflt, cmajflt, utime, stime uint64
cutime, cstime, priority, nice, num_threads, itrealvalue uint
starttime, vsize, rss uint64
rsslim, startcode, endcode, startstack, kstkesp uint64
kstkeip, signal, blocked, sigignore, sigcatch uint64
wchan, nswap, cnswap uint64
exit_signal, processor int
rt_priority, policy uint
delayacct_blkio_ticks, guest_time, cguest_time uint64
start_data, end_data, start_brk, arg_start uint64
arg_end, env_start, env_end uint64
exit_code int
)
fmt.Sscanf(
string(s),
""+
"%d %s %c %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d %d %d %d "+
"%d %d",
&pid, &comm, &state, &ppid, &pgrp,
&session, &tty_nr, &tpgid, &flags, &minflt,
&cminflt, &majflt, &cmajflt, &utime, &stime,
&cutime, &cstime, &priority, &nice, &num_threads,
&itrealvalue, &starttime, &vsize, &rss, &rsslim,
&startcode, &endcode, &startstack, &kstkesp, &kstkeip,
&signal, &blocked, &sigignore, &sigcatch, &wchan,
&nswap, &cnswap, &exit_signal, &processor, &rt_priority,
&policy, &delayacct_blkio_ticks, &guest_time, &cguest_time, &start_data,
&end_data, &start_brk, &arg_start, &arg_end, &env_start,
&env_end, &exit_code,
)
limitedAppend(&a.Threads, num_threads)
limitedAppend(&a.Vsize, vsize)
}
func (a *Analytics) statm(pid int) {
s, err := os.ReadFile(fmt.Sprintf("/proc/%d/statm", pid))
if err != nil {
return
}
var size, resident, shared, text, lib, data, dt uint64
fmt.Sscanf(
string(s),
"%d %d %d %d %d %d %d",
&size, &resident, &shared, &text, &lib, &data, &dt,
)
limitedAppend(&a.ResidentMem, resident)
limitedAppend(&a.SharedMem, shared)
limitedAppend(&a.TextMem, text)
limitedAppend(&a.DataMem, data)
}