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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
package statsengine
import (
"cmp"
"container/heap"
"slices"
"ior/internal/event"
"ior/internal/types"
)
const fileRankTopNDefault = 20
type fileRanker struct {
topN int
maxSeen int
byPath map[string]*fileRankStats
topHeap fileRankHeap
}
type fileRankStats struct {
path string
accesses uint64
bytesRead uint64
bytesWritten uint64
totalLatency uint64
maxLatency uint64
heapIndex int
}
type fileSnapshotInput struct {
path string
accesses uint64
bytesRead uint64
bytesWritten uint64
totalLatency uint64
maxLatency uint64
}
type fileRankHeap []*fileRankStats
func newFileRanker() *fileRanker {
return newFileRankerWithConfig(fileRankTopNDefault)
}
func newFileRankerWithConfig(topN int) *fileRanker {
if topN <= 0 {
topN = fileRankTopNDefault
}
return newFileRankerWithLimits(topN, topN*32)
}
func newFileRankerWithLimits(topN int, maxSeen int) *fileRanker {
if topN <= 0 {
topN = fileRankTopNDefault
}
if maxSeen < topN {
maxSeen = topN
}
r := &fileRanker{
topN: topN,
maxSeen: maxSeen,
byPath: make(map[string]*fileRankStats),
}
heap.Init(&r.topHeap)
return r
}
func (r *fileRanker) Add(pair *event.Pair) {
if r == nil || pair == nil || pair.File == nil {
return
}
path := pair.File.Name()
if path == "" || path == "N:file" {
return
}
stats := r.byPath[path]
if stats == nil {
stats = &fileRankStats{path: path, heapIndex: -1}
r.byPath[path] = stats
}
stats.accesses++
stats.totalLatency += pair.Duration
if pair.Duration > stats.maxLatency {
stats.maxLatency = pair.Duration
}
r.addBytes(stats, pair)
r.updateHeap(stats)
r.compactIfNeeded()
}
func (r *fileRanker) Snapshot() []FileSnapshot {
if r == nil {
return nil
}
return buildFileSnapshots(r.snapshotInputs())
}
func (r *fileRanker) snapshotInputs() []fileSnapshotInput {
if r == nil {
return nil
}
inputs := make([]fileSnapshotInput, 0, len(r.topHeap))
for _, stats := range r.topHeap {
inputs = append(inputs, fileSnapshotInput{
path: stats.path,
accesses: stats.accesses,
bytesRead: stats.bytesRead,
bytesWritten: stats.bytesWritten,
totalLatency: stats.totalLatency,
maxLatency: stats.maxLatency,
})
}
return inputs
}
func buildFileSnapshots(inputs []fileSnapshotInput) []FileSnapshot {
out := make([]FileSnapshot, 0, len(inputs))
for _, in := range inputs {
out = append(out, in.toSnapshot())
}
slices.SortFunc(out, func(a, b FileSnapshot) int {
if a.Accesses != b.Accesses {
return cmp.Compare(b.Accesses, a.Accesses)
}
return cmp.Compare(a.Path, b.Path)
})
return out
}
func (r *fileRanker) addBytes(stats *fileRankStats, pair *event.Pair) {
retEv, ok := pair.ExitEv.(*types.RetEvent)
if !ok {
return
}
switch retEv.RetType {
case types.READ_CLASSIFIED:
stats.bytesRead += pair.Bytes
case types.WRITE_CLASSIFIED:
stats.bytesWritten += pair.Bytes
case types.TRANSFER_CLASSIFIED:
// Transfer syscalls move bytes in both directions from a file-centric view.
stats.bytesRead += pair.Bytes
stats.bytesWritten += pair.Bytes
}
}
func (r *fileRanker) updateHeap(stats *fileRankStats) {
if stats.heapIndex >= 0 {
heap.Fix(&r.topHeap, stats.heapIndex)
return
}
if len(r.topHeap) < r.topN {
heap.Push(&r.topHeap, stats)
return
}
worst := r.topHeap[0]
if !betterFileRank(stats, worst) {
return
}
heap.Pop(&r.topHeap)
heap.Push(&r.topHeap, stats)
}
func (r *fileRanker) compactIfNeeded() {
if len(r.byPath) <= r.maxSeen {
return
}
// Keep only currently top-ranked paths once cardinality crosses the guard.
kept := make(map[string]*fileRankStats, len(r.topHeap))
for _, stats := range r.topHeap {
kept[stats.path] = stats
}
r.byPath = kept
}
func (s fileSnapshotInput) toSnapshot() FileSnapshot {
avg := 0.0
if s.accesses > 0 {
avg = float64(s.totalLatency) / float64(s.accesses)
}
return FileSnapshot{
Path: s.path,
Accesses: s.accesses,
BytesRead: s.bytesRead,
BytesWritten: s.bytesWritten,
AvgLatencyNs: avg,
MaxLatencyNs: s.maxLatency,
}
}
func betterFileRank(a, b *fileRankStats) bool {
if a.accesses != b.accesses {
return a.accesses > b.accesses
}
return a.path < b.path
}
func (h fileRankHeap) Len() int {
return len(h)
}
func (h fileRankHeap) Less(i, j int) bool {
// Keep the worst-ranked item at root for O(log N) eviction.
return betterFileRank(h[j], h[i])
}
func (h fileRankHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
h[i].heapIndex = i
h[j].heapIndex = j
}
func (h *fileRankHeap) Push(x any) {
stats := x.(*fileRankStats)
stats.heapIndex = len(*h)
*h = append(*h, stats)
}
func (h *fileRankHeap) Pop() any {
old := *h
n := len(old)
stats := old[n-1]
stats.heapIndex = -1
*h = old[:n-1]
return stats
}
|