summaryrefslogtreecommitdiff
path: root/internal/statsengine/process.go
blob: 3bfd019d545826e6e01f17c7396cf2bda0cff610 (plain)
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
181
182
183
184
package statsengine

import (
	"cmp"
	"slices"
	"time"

	"ior/internal/event"
)

const processRankTopNDefault = 20

type processAccumulator struct {
	topN    int
	maxSeen int
	byPID   map[uint32]*processStats
}

type processStats struct {
	pid          uint32
	comm         string
	count        uint64
	totalBytes   uint64
	totalLatency uint64
}

type processSnapshotInput struct {
	pid          uint32
	comm         string
	count        uint64
	totalBytes   uint64
	totalLatency uint64
}

func newProcessAccumulator() *processAccumulator {
	return newProcessAccumulatorWithConfig(processRankTopNDefault)
}

func newProcessAccumulatorWithConfig(topN int) *processAccumulator {
	if topN <= 0 {
		topN = processRankTopNDefault
	}
	return newProcessAccumulatorWithLimits(topN, topN*32)
}

func newProcessAccumulatorWithLimits(topN int, maxSeen int) *processAccumulator {
	if topN <= 0 {
		topN = processRankTopNDefault
	}
	if maxSeen < topN {
		maxSeen = topN
	}
	return &processAccumulator{
		topN:    topN,
		maxSeen: maxSeen,
		byPID:   make(map[uint32]*processStats),
	}
}

func (a *processAccumulator) Add(pair *event.Pair) {
	if a == nil || pair == nil || pair.EnterEv == nil {
		return
	}

	pid := pair.EnterEv.GetPid()
	stats := a.byPID[pid]
	if stats == nil {
		stats = &processStats{pid: pid}
		a.byPID[pid] = stats
	}
	if pair.Comm != "" && stats.comm != "" && stats.comm != pair.Comm {
		// Best-effort PID reuse handling: when command name changes for an
		// existing PID, treat it as a new process lifetime and reset counters.
		stats = &processStats{pid: pid}
		a.byPID[pid] = stats
	}

	stats.count++
	stats.totalBytes += pair.Bytes
	stats.totalLatency += pair.Duration
	if pair.Comm != "" {
		stats.comm = pair.Comm
	}
	a.compactIfNeeded()
}

func (a *processAccumulator) Snapshot(elapsed time.Duration) []ProcessSnapshot {
	if a == nil {
		return nil
	}

	return buildProcessSnapshots(a.snapshotInputs(), elapsed)
}

func (a *processAccumulator) snapshotInputs() []processSnapshotInput {
	if a == nil {
		return nil
	}

	inputs := make([]processSnapshotInput, 0, len(a.byPID))
	for _, stats := range a.byPID {
		inputs = append(inputs, processSnapshotInput{
			pid:          stats.pid,
			comm:         stats.comm,
			count:        stats.count,
			totalBytes:   stats.totalBytes,
			totalLatency: stats.totalLatency,
		})
	}
	return inputs
}

func buildProcessSnapshots(inputs []processSnapshotInput, elapsed time.Duration) []ProcessSnapshot {
	rateDiv := elapsed.Seconds()
	result := make([]ProcessSnapshot, 0, len(inputs))
	for _, in := range inputs {
		result = append(result, in.toSnapshot(rateDiv))
	}
	slices.SortFunc(result, func(a, b ProcessSnapshot) int {
		if a.Syscalls != b.Syscalls {
			return cmp.Compare(b.Syscalls, a.Syscalls)
		}
		if a.Bytes != b.Bytes {
			return cmp.Compare(b.Bytes, a.Bytes)
		}
		return cmp.Compare(a.PID, b.PID)
	})
	return result
}

func (a *processAccumulator) compactIfNeeded() {
	if len(a.byPID) <= a.maxSeen {
		return
	}

	ordered := make([]*processStats, 0, len(a.byPID))
	for _, stats := range a.byPID {
		ordered = append(ordered, stats)
	}
	slices.SortFunc(ordered, func(a, b *processStats) int {
		if betterProcessRank(a, b) {
			return -1
		}
		if betterProcessRank(b, a) {
			return 1
		}
		return 0
	})
	if len(ordered) > a.topN {
		ordered = ordered[:a.topN]
	}

	kept := make(map[uint32]*processStats, len(ordered))
	for _, stats := range ordered {
		kept[stats.pid] = stats
	}
	a.byPID = kept
}

func betterProcessRank(a, b *processStats) bool {
	if a.count != b.count {
		return a.count > b.count
	}
	if a.totalBytes != b.totalBytes {
		return a.totalBytes > b.totalBytes
	}
	return a.pid < b.pid
}

func (s processSnapshotInput) toSnapshot(rateDiv float64) ProcessSnapshot {
	avg := 0.0
	if s.count > 0 {
		avg = float64(s.totalLatency) / float64(s.count)
	}

	return ProcessSnapshot{
		PID:          s.pid,
		Comm:         s.comm,
		Syscalls:     s.count,
		RatePerSec:   safeRate(s.count, rateDiv),
		Bytes:        s.totalBytes,
		AvgLatencyNs: avg,
	}
}