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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
package flamegraph
import (
"bytes"
"errors"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"ior/internal/types"
)
func counterAt(iod iorData, path pathType, traceID traceIdType, comm commType, pid pidType, tid tidType, flags flagsType) (Counter, bool) {
key := recordKey{
Path: path,
TraceID: traceID,
Comm: comm,
Pid: pid,
Tid: tid,
Flags: flags,
}
cnt, ok := iod.records[key]
return cnt, ok
}
func TestAddPath(t *testing.T) {
iod := newIorData()
path := pathType("testPath")
traceId := types.SYS_ENTER_OPENAT
comm := commType("testComm")
pid := pidType(1234)
tid := tidType(5678)
flags := flagsType(syscall.O_RDONLY)
cnt1 := Counter{Count: 1, Duration: 1000, DurationToPrev: 100, Bytes: 64}
iod.add(path, traceId, comm, pid, tid, flags, cnt1)
gotCnt, ok := counterAt(iod, path, traceId, comm, pid, tid, flags)
if !ok || gotCnt != cnt1 {
t.Errorf("Expected counter %v, got %v (ok=%v)", cnt1, gotCnt, ok)
}
cnt2 := Counter{Count: 2, Duration: 2000, DurationToPrev: 200, Bytes: 128}
iod.add(path, traceId, comm, pid, tid, flags, cnt2)
resultCnt := cnt1.add(cnt2)
gotCnt, ok = counterAt(iod, path, traceId, comm, pid, tid, flags)
if !ok || gotCnt != resultCnt {
t.Errorf("Expected counter %v, got %v (ok=%v)", resultCnt, gotCnt, ok)
}
}
func TestMerge(t *testing.T) {
rdwrFlag := flagsType(syscall.O_RDWR)
roFlag := flagsType(syscall.O_RDONLY)
traceId := types.SYS_ENTER_OPENAT
// Initialize iorData instances with sample data
iod1 := newIorData()
iod1.add("path1", traceId, "comm1", 100, 1000, rdwrFlag, Counter{
Count: 10,
Duration: 1000,
DurationToPrev: 100,
Bytes: 64,
})
iod2 := newIorData()
iod2.add("path1", traceId, "comm1", 100, 1000, roFlag, Counter{
Count: 20,
Duration: 2000,
DurationToPrev: 200,
Bytes: 128,
})
iod3 := newIorData()
iod3.add("path2", traceId, "comm2", 101, 1000, roFlag, Counter{
Count: 20,
Duration: 2000,
DurationToPrev: 200,
Bytes: 128,
})
iod4 := newIorData()
iod4.add("path2", traceId, "comm2", 101, 1000, roFlag, Counter{
Count: 40,
Duration: 4000,
DurationToPrev: 400,
Bytes: 256,
})
t.Log("iod1", iod1)
t.Log("iod2", iod2)
t.Log("iod3", iod3)
t.Log("iod4", iod4)
merged := *iod1.merge(iod2).merge(iod3).merge(iod4)
t.Log("merged", merged)
t.Run("Merged correctly", func(t *testing.T) {
if len(merged.records) != 3 {
t.Errorf("Expected 3 aggregated records, got %d", len(merged.records))
}
if cnt, _ := counterAt(merged, "path1", traceId, "comm1", 100, 1000, rdwrFlag); cnt.Count != 10 {
t.Errorf("Expected counter 10, got %d", cnt.Count)
}
if cnt, _ := counterAt(merged, "path2", traceId, "comm2", 101, 1000, roFlag); cnt.Count != 60 {
t.Errorf("Expected counter 60, got %d", cnt.Count)
}
if cnt, _ := counterAt(merged, "path2", traceId, "comm2", 101, 1000, roFlag); cnt.Bytes != 384 {
t.Errorf("Expected bytes 384, got %d", cnt.Bytes)
}
})
// t.Run("Iterate over lines", func(t *testing.T) {
// expectedLines := []string{
// "path1 ␞ enter_openat ␞ comm1 ␞ 100 ␞ 1000 ␞ O_RDWR ␞ 10 1000 100 0",
// "path1 ␞ enter_openat ␞ comm1 ␞ 100 ␞ 1000 ␞ O_RDONLY ␞ 20 2000 200 0",
// "path2 ␞ enter_openat ␞ comm2 ␞ 101 ␞ 1000 ␞ O_RDONLY ␞ 60 6000 600 0",
// }
// var lines []string
// for line := range merged.lines() {
// lines = append(lines, line)
// }
// if len(lines) != len(expectedLines) {
// t.Errorf("Expected %d lines, got %d", len(expectedLines), len(lines))
// }
// if !bothArraysHaveSameElements(lines, expectedLines) {
// t.Errorf("Expected lines %v, got %v", expectedLines, lines)
// }
// })
}
func TestStringByNameUnknownField(t *testing.T) {
ir := IterRecord{
Path: "/tmp/test",
TraceID: types.SYS_ENTER_OPENAT,
Comm: "testComm",
Pid: 1234,
Tid: 5678,
Flags: flagsType(syscall.O_RDONLY),
Cnt: Counter{Count: 1},
}
_, err := ir.StringByName("nonexistent")
if err == nil {
t.Error("Expected error for unknown field name, got nil")
}
}
func TestStringByNameValidFields(t *testing.T) {
ir := IterRecord{
Path: "/tmp/test",
TraceID: types.SYS_ENTER_OPENAT,
Comm: "testComm",
Pid: 1234,
Tid: 5678,
Flags: flagsType(syscall.O_RDONLY),
Cnt: Counter{Count: 1},
}
validFields := []string{"path", "comm", "tracepoint", "pid", "tid", "flags"}
for _, name := range validFields {
t.Run(name, func(t *testing.T) {
val, err := ir.StringByName(name)
if err != nil {
t.Errorf("Expected no error for field %q, got %v", name, err)
}
if val == "" {
t.Errorf("Expected non-empty string for field %q", name)
}
})
}
}
func TestCounterValueByNameUnknownField(t *testing.T) {
c := Counter{Count: 1, Duration: 100, DurationToPrev: 10, Bytes: 64}
_, err := c.ValueByName("nonexistent")
if err == nil {
t.Error("Expected error for unknown counter name, got nil")
}
}
func TestCounterValueByNameValidFields(t *testing.T) {
c := Counter{Count: 1, Duration: 100, DurationToPrev: 10, Bytes: 64}
tests := map[string]uint64{
"count": c.Count,
"duration": c.Duration,
"durationToPrev": c.DurationToPrev,
"bytes": c.Bytes,
}
for field, want := range tests {
t.Run(field, func(t *testing.T) {
got, err := c.ValueByName(field)
if err != nil {
t.Fatalf("Expected no error for field %q, got %v", field, err)
}
if got != want {
t.Fatalf("Expected %d for field %q, got %d", want, field, got)
}
})
}
}
func TestMergeEmpty(t *testing.T) {
traceId := types.SYS_ENTER_OPENAT
roFlag := flagsType(syscall.O_RDONLY)
iod := newIorData()
iod.add("path1", traceId, "comm1", 100, 1000, roFlag, Counter{
Count: 10,
Duration: 1000,
DurationToPrev: 100,
Bytes: 64,
})
empty := newIorData()
merged := *iod.merge(empty)
if len(merged.records) != 1 {
t.Errorf("Expected 1 record, got %d", len(merged.records))
}
cnt, ok := counterAt(merged, "path1", traceId, "comm1", 100, 1000, roFlag)
if !ok {
t.Fatal("Expected merged counter to exist")
}
if cnt.Count != 10 || cnt.Duration != 1000 || cnt.DurationToPrev != 100 || cnt.Bytes != 64 {
t.Errorf("Expected original counter preserved, got %v", cnt)
}
}
func TestAddZeroCounter(t *testing.T) {
iod := newIorData()
path := pathType("testPath")
traceId := types.SYS_ENTER_OPENAT
comm := commType("testComm")
pid := pidType(1234)
tid := tidType(5678)
flags := flagsType(syscall.O_RDONLY)
zero := Counter{}
iod.add(path, traceId, comm, pid, tid, flags, zero)
cnt, ok := counterAt(iod, path, traceId, comm, pid, tid, flags)
if !ok {
t.Fatal("Expected entry to exist for zero counter")
}
if cnt != zero {
t.Errorf("Expected zero counter %v, got %v", zero, cnt)
}
}
func TestSerializeDeserializeRoundTrip(t *testing.T) {
traceId := types.SYS_ENTER_OPENAT
rdwrFlag := flagsType(syscall.O_RDWR)
roFlag := flagsType(syscall.O_RDONLY)
original := newIorData()
original.add("path1", traceId, "comm1", 100, 1000, rdwrFlag, Counter{
Count: 10,
Duration: 1000,
DurationToPrev: 100,
Bytes: 64,
})
original.add("path2", traceId, "comm2", 200, 2000, roFlag, Counter{
Count: 20,
Duration: 2000,
DurationToPrev: 200,
Bytes: 128,
})
data, err := original.serialize()
if err != nil {
t.Fatalf("serialize failed: %v", err)
}
restored := newIorData()
if err := restored.deserialize(bytes.NewBuffer(data)); err != nil {
t.Fatalf("deserialize failed: %v", err)
}
if len(restored.records) != len(original.records) {
t.Fatalf("Expected %d records, got %d", len(original.records), len(restored.records))
}
cnt1, ok := counterAt(restored, "path1", traceId, "comm1", 100, 1000, rdwrFlag)
if !ok {
t.Fatal("Expected path1 counter to exist")
}
if cnt1.Count != 10 || cnt1.Duration != 1000 || cnt1.DurationToPrev != 100 || cnt1.Bytes != 64 {
t.Errorf("path1 counter mismatch: %v", cnt1)
}
cnt2, ok := counterAt(restored, "path2", traceId, "comm2", 200, 2000, roFlag)
if !ok {
t.Fatal("Expected path2 counter to exist")
}
if cnt2.Count != 20 || cnt2.Duration != 2000 || cnt2.DurationToPrev != 200 || cnt2.Bytes != 128 {
t.Errorf("path2 counter mismatch: %v", cnt2)
}
}
func TestDeserializeInvalidData(t *testing.T) {
iod := newIorData()
var buf bytes.Buffer
buf.WriteString("this is not valid gob data")
err := iod.deserialize(&buf)
if err == nil {
t.Error("Expected error when deserializing invalid data, got nil")
}
}
func TestSerializeToFileHostnameErrorReturnsError(t *testing.T) {
origHostnameFn := hostnameFn
t.Cleanup(func() { hostnameFn = origHostnameFn })
hostnameFn = func() (string, error) {
return "", errors.New("hostname unavailable")
}
iod := newIorData()
err := iod.serializeToFile("test")
if err == nil {
t.Fatal("Expected error when hostname lookup fails, got nil")
}
if !strings.Contains(err.Error(), "get hostname") {
t.Fatalf("Expected get hostname context, got %v", err)
}
}
func TestLoadFromFileCorruptDataReturnsContext(t *testing.T) {
path := filepath.Join(t.TempDir(), "corrupt.ior.zst")
if err := os.WriteFile(path, []byte("not-a-valid-zstd-stream"), 0o600); err != nil {
t.Fatalf("write corrupt file: %v", err)
}
iod := newIorData()
err := iod.loadFromFile(path)
if err == nil {
t.Fatal("Expected corrupt file to return an error")
}
if !strings.Contains(err.Error(), "decode ior records from") {
t.Fatalf("Expected decode context, got %v", err)
}
}
func bothArraysHaveSameElements(a, b []string) bool {
if len(a) != len(b) {
return false
}
for _, v1 := range a {
found := false
for _, v2 := range b {
if v1 == v2 {
found = true
break
}
}
if !found {
return false
}
}
return true
}
|