-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.go
180 lines (150 loc) · 3.3 KB
/
cmdline.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"log/slog"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
)
type Process struct {
// /proc/pid/cmdline
Cmdline []string
// /proc/pid/comm
Comm string
PID int64
PPID int64
}
type ProcessCache struct {
c map[int64]*Process
mu *sync.RWMutex
}
func NewProcessCache() *ProcessCache {
c := &ProcessCache{
c: map[int64]*Process{},
mu: &sync.RWMutex{},
}
go func() {
for {
cmdline := listProcesses(maxPid)
c.mu.Lock()
c.c = cmdline
c.mu.Unlock()
time.Sleep(time.Duration(listProcessInterval) * time.Second)
}
}()
return c
}
func (c *ProcessCache) Insert(p *Process) {
c.mu.Lock()
defer c.mu.Unlock()
c.c[p.PID] = p
}
// Lookup retturns nil if not found.
func (c *ProcessCache) Lookup(pid int64) *Process {
c.mu.RLock()
defer c.mu.RUnlock()
return c.c[pid]
}
func listProcesses(maxpid int64) map[int64]*Process {
ret := map[int64]*Process{}
mu := &sync.Mutex{}
stime := time.Now()
defer func() {
diff := time.Since(stime).Milliseconds()
slog.Debug("listed process", "msElapsed", diff, "items", len(ret))
}()
getProcess := func(pid int64) {
proc := getProcessFromProc(pid)
if proc == nil {
return
}
mu.Lock()
ret[pid] = proc
mu.Unlock()
}
pids := listPids(maxpid)
// We want to do parallel listing to speed up.
// So we split the pids into maxListProcessThreads groups, and spawn maxListProcessThreads goroutines to get them.
var perGoroutinePids [][]int64
for range maxListProcessThreads {
perGoroutinePids = append(perGoroutinePids, []int64{})
}
for i, pid := range pids {
whichGoroutine := i % maxListProcessThreads
perGoroutinePids[whichGoroutine] = append(perGoroutinePids[whichGoroutine], pid)
}
// Let them go!
wg := sync.WaitGroup{}
wg.Add(len(perGoroutinePids))
for _, gPids := range perGoroutinePids {
go func() {
defer wg.Done()
for _, pid := range gPids {
getProcess(pid)
}
}()
}
wg.Wait()
return ret
}
func listPids(maxpid int64) []int64 {
proc, err := os.ReadDir(path.Join(root, "proc"))
if err != nil {
return nil
}
var pids []int64
for _, d := range proc {
if !d.IsDir() {
continue
}
isPid := true
for _, i := range d.Name() {
if i < '0' || i > '9' {
isPid = false
break
}
}
if !isPid {
continue
}
pid, err := strconv.ParseInt(d.Name(), 10, 32)
if err != nil || pid <= 0 { // no pid 0 in proc
continue
}
if pid > maxpid {
continue
}
pids = append(pids, pid)
}
return pids
}
func getProcessFromProc(pid int64) *Process {
cmdlineBytes, err := os.ReadFile(path.Join(root, "proc", strconv.FormatInt(pid, 10), "cmdline"))
if err != nil || len(cmdlineBytes) == 0 {
return nil
}
cmdline := cmdlineConv(cmdlineBytes)
statBytes, err := os.ReadFile(path.Join(root, "proc", strconv.FormatInt(pid, 10), "stat"))
if err != nil || len(statBytes) == 0 {
return nil
}
stat := string(statBytes)
binStart := strings.IndexRune(stat, '(') + 1
binEnd := strings.IndexRune(stat[binStart:], ')')
binary := stat[binStart : binStart+binEnd]
// Move past the image name and start parsing the rest
stat = stat[binStart+binEnd+4:]
ppidStr := stat[:strings.IndexRune(stat, ' ')]
ppid, err := strconv.ParseInt(ppidStr, 10, 64)
if err != nil {
return nil
}
return &Process{
Cmdline: cmdline,
Comm: binary,
PPID: ppid,
PID: pid,
}
}