summaryrefslogtreecommitdiff
path: root/integrationtests/readwrite_test.go
blob: 636cc5c64493db5e8224384f6c743011a430796c (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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package integrationtests

import "testing"

func TestReadwriteBasic(t *testing.T) {
	const payloadLen = uint64(len("hello from ioworkload"))
	result, _ := runScenarioResult(t, "readwrite-basic", []ExpectedEvent{
		{
			PathContains: "rwfile.txt",
			Tracepoint:   "enter_write",
			Comm:         "ioworkload",
			MinCount:     1,
		},
		{
			PathContains: "rwfile.txt",
			Tracepoint:   "enter_read",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "rwfile.txt",
		Tracepoint:   "enter_write",
		Comm:         "ioworkload",
	}, payloadLen)
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "rwfile.txt",
		Tracepoint:   "enter_read",
		Comm:         "ioworkload",
	}, payloadLen)
}

func TestReadwritePread(t *testing.T) {
	// pread64 returns the number of bytes read (READ_CLASSIFIED), so a
	// successful positional read must attribute the payload byte count
	// end-to-end, mirroring the pwrite64 byte-count assertion below.
	const payloadLen = uint64(len("pread test data"))
	result, _ := runScenarioResult(t, "readwrite-pread", []ExpectedEvent{
		{
			PathContains: "preadfile.txt",
			Tracepoint:   "enter_pread64",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "preadfile.txt",
		Tracepoint:   "enter_pread64",
		Comm:         "ioworkload",
	}, payloadLen)
}

func TestReadwritePwrite(t *testing.T) {
	result, _ := runScenarioResult(t, "readwrite-pwrite", []ExpectedEvent{
		{
			PathContains: "pwritefile.txt",
			Tracepoint:   "enter_pwrite64",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "pwritefile.txt",
		Tracepoint:   "enter_pwrite64",
		Comm:         "ioworkload",
	}, 1)
}

func TestReadwritePwritev(t *testing.T) {
	// pwritev is the positional vectored write sibling of pwrite64/writev and is
	// WRITE_CLASSIFIED, so a successful write must attribute the iovec total
	// (sum of both buffers) end-to-end.
	const payloadLen = uint64(len("pwritev ") + len("test data"))
	result, _ := runScenarioResult(t, "readwrite-pwritev", []ExpectedEvent{
		{
			PathContains: "pwritevfile.txt",
			Tracepoint:   "enter_pwritev",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "pwritevfile.txt",
		Tracepoint:   "enter_pwritev",
		Comm:         "ioworkload",
	}, payloadLen)
}

func TestReadwritePwritev2(t *testing.T) {
	// pwritev2 is the positional vectored write variant with flags; like pwritev
	// it is WRITE_CLASSIFIED and must attribute the iovec total end-to-end.
	const payloadLen = uint64(len("pwritev2 ") + len("test data"))
	result, _ := runScenarioResult(t, "readwrite-pwritev2", []ExpectedEvent{
		{
			PathContains: "pwritev2file.txt",
			Tracepoint:   "enter_pwritev2",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "pwritev2file.txt",
		Tracepoint:   "enter_pwritev2",
		Comm:         "ioworkload",
	}, payloadLen)
}

func TestReadwritePreadv(t *testing.T) {
	// preadv is the positional vectored read sibling of pread64/readv and is
	// READ_CLASSIFIED, so a successful read must attribute the payload bytes
	// end-to-end.
	const payloadLen = uint64(len("preadv test data"))
	result, _ := runScenarioResult(t, "readwrite-preadv", []ExpectedEvent{
		{
			PathContains: "preadvfile.txt",
			Tracepoint:   "enter_preadv",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "preadvfile.txt",
		Tracepoint:   "enter_preadv",
		Comm:         "ioworkload",
	}, payloadLen)
}

func TestReadwritePreadv2(t *testing.T) {
	// preadv2 is the positional vectored read variant with flags; like preadv
	// it is READ_CLASSIFIED and must attribute the payload bytes end-to-end.
	const payloadLen = uint64(len("preadv2 test data"))
	result, _ := runScenarioResult(t, "readwrite-preadv2", []ExpectedEvent{
		{
			PathContains: "preadv2file.txt",
			Tracepoint:   "enter_preadv2",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "preadv2file.txt",
		Tracepoint:   "enter_preadv2",
		Comm:         "ioworkload",
	}, payloadLen)
}

func TestReadwriteReadv(t *testing.T) {
	result, _ := runScenarioResult(t, "readwrite-readv", []ExpectedEvent{
		{
			PathContains: "readvfile.txt",
			Tracepoint:   "enter_readv",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "readvfile.txt",
		Tracepoint:   "enter_readv",
		Comm:         "ioworkload",
	}, 1)
}

func TestReadwriteWritev(t *testing.T) {
	result, _ := runScenarioResult(t, "readwrite-writev", []ExpectedEvent{
		{
			PathContains: "writevfile.txt",
			Tracepoint:   "enter_writev",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesAtLeast(t, result, ExpectedEvent{
		PathContains: "writevfile.txt",
		Tracepoint:   "enter_writev",
		Comm:         "ioworkload",
	}, 1)
}

func TestReadwriteWronlyRead(t *testing.T) {
	result, _ := runScenarioResult(t, "readwrite-wronly-read", []ExpectedEvent{
		{
			PathContains: "wronlyfile.txt",
			Tracepoint:   "enter_read",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesEqual(t, result, ExpectedEvent{
		PathContains: "wronlyfile.txt",
		Tracepoint:   "enter_read",
		Comm:         "ioworkload",
	}, 0)
	assertEventBytesReasonable(t, result, ExpectedEvent{
		PathContains: "wronlyfile.txt",
		Tracepoint:   "enter_read",
		Comm:         "ioworkload",
	})
}

func TestReadwriteRdonlyWrite(t *testing.T) {
	result, _ := runScenarioResult(t, "readwrite-rdonly-write", []ExpectedEvent{
		{
			PathContains: "rdonlywritefile.txt",
			Tracepoint:   "enter_write",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesEqual(t, result, ExpectedEvent{
		PathContains: "rdonlywritefile.txt",
		Tracepoint:   "enter_write",
		Comm:         "ioworkload",
	}, 0)
	assertEventBytesReasonable(t, result, ExpectedEvent{
		PathContains: "rdonlywritefile.txt",
		Tracepoint:   "enter_write",
		Comm:         "ioworkload",
	})
}

func TestReadwritePreadInvalid(t *testing.T) {
	result, _ := runScenarioResult(t, "readwrite-pread-invalid", []ExpectedEvent{
		{
			PathContains: "preadinvalid.txt",
			Tracepoint:   "enter_pread64",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesEqual(t, result, ExpectedEvent{
		PathContains: "preadinvalid.txt",
		Tracepoint:   "enter_pread64",
		Comm:         "ioworkload",
	}, 0)
	assertEventBytesReasonable(t, result, ExpectedEvent{
		PathContains: "preadinvalid.txt",
		Tracepoint:   "enter_pread64",
		Comm:         "ioworkload",
	})
}

func TestReadwritePwriteInvalid(t *testing.T) {
	result, _ := runScenarioResult(t, "readwrite-pwrite-invalid", []ExpectedEvent{
		{
			PathContains: "pwriteinvalid.txt",
			Tracepoint:   "enter_pwrite64",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	assertEventBytesEqual(t, result, ExpectedEvent{
		PathContains: "pwriteinvalid.txt",
		Tracepoint:   "enter_pwrite64",
		Comm:         "ioworkload",
	}, 0)
	assertEventBytesReasonable(t, result, ExpectedEvent{
		PathContains: "pwriteinvalid.txt",
		Tracepoint:   "enter_pwrite64",
		Comm:         "ioworkload",
	})
}

func TestReadwriteReadahead(t *testing.T) {
	// readahead(2) is KindFd / UNCLASSIFIED: despite its ssize_t prototype it
	// returns 0/-1 and transfers no bytes to userspace, so the tracer must
	// attribute zero bytes (not misread the 0/-1 return as a byte count) while
	// still capturing the fd (args[0]) on enter and timing the syscall.
	result, _ := runScenarioResult(t, "readwrite-readahead", []ExpectedEvent{
		{
			PathContains: "readaheadfile.txt",
			Tracepoint:   "enter_readahead",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	exp := ExpectedEvent{
		PathContains: "readaheadfile.txt",
		Tracepoint:   "enter_readahead",
		Comm:         "ioworkload",
	}
	// UNCLASSIFIED: no byte count is attributed for a successful readahead.
	assertEventBytesEqual(t, result, exp, 0)
	// Timing is captured end-to-end (enter/exit paired into a duration).
	assertEventDurationPositive(t, result, exp)
}

func TestReadwriteReadaheadEbadf(t *testing.T) {
	// readahead on an invalid fd fails with EBADF, but ior still captures the
	// enter_readahead tracepoint because arguments are read on syscall entry
	// before the kernel returns the error. The UNCLASSIFIED -1 return must not
	// be attributed as bytes.
	result, _ := runScenarioResult(t, "readwrite-readahead-ebadf", []ExpectedEvent{
		{
			Tracepoint: "enter_readahead",
			Comm:       "ioworkload",
			MinCount:   1,
		},
	})
	assertEventBytesEqual(t, result, ExpectedEvent{
		Tracepoint: "enter_readahead",
		Comm:       "ioworkload",
	}, 0)
}

func TestReadwriteFadvise64(t *testing.T) {
	// fadvise64(2) is KindFd / UNCLASSIFIED: it declares a page-cache access hint
	// and returns 0/-1 (no byte count, transfers no bytes to userspace), so the
	// tracer must attribute zero bytes (not misread the 0/-1 return or the
	// offset/len hint parameters as a byte count) while still capturing the fd
	// (args[0]) on enter and timing the syscall.
	result, _ := runScenarioResult(t, "readwrite-fadvise64", []ExpectedEvent{
		{
			PathContains: "fadvise64file.txt",
			Tracepoint:   "enter_fadvise64",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	exp := ExpectedEvent{
		PathContains: "fadvise64file.txt",
		Tracepoint:   "enter_fadvise64",
		Comm:         "ioworkload",
	}
	// UNCLASSIFIED: no byte count is attributed for a successful fadvise64.
	assertEventBytesEqual(t, result, exp, 0)
	// Timing is captured end-to-end (enter/exit paired into a duration).
	assertEventDurationPositive(t, result, exp)
}

func TestReadwriteFadvise64Ebadf(t *testing.T) {
	// fadvise64 on an invalid fd fails with EBADF, but ior still captures the
	// enter_fadvise64 tracepoint because arguments are read on syscall entry
	// before the kernel returns the error. The UNCLASSIFIED -1 return must not
	// be attributed as bytes.
	result, _ := runScenarioResult(t, "readwrite-fadvise64-ebadf", []ExpectedEvent{
		{
			Tracepoint: "enter_fadvise64",
			Comm:       "ioworkload",
			MinCount:   1,
		},
	})
	assertEventBytesEqual(t, result, ExpectedEvent{
		Tracepoint: "enter_fadvise64",
		Comm:       "ioworkload",
	}, 0)
}

func TestReadwriteCachestat(t *testing.T) {
	// cachestat(2) is KindFd / UNCLASSIFIED: it queries page-cache residency for
	// a file and returns 0/-1 (no byte count, no I/O bytes to userspace), so the
	// tracer must attribute zero bytes while still capturing the fd (args[0]) on
	// enter and timing the syscall. cachestat is Linux 6.5+; the enter tracepoint
	// fires before the kernel checks availability, so even an ENOSYS kernel would
	// still record enter_cachestat, but this dev kernel (7.0.9) supports it fully.
	result, _ := runScenarioResult(t, "readwrite-cachestat", []ExpectedEvent{
		{
			PathContains: "cachestatfile.txt",
			Tracepoint:   "enter_cachestat",
			Comm:         "ioworkload",
			MinCount:     1,
		},
	})
	exp := ExpectedEvent{
		PathContains: "cachestatfile.txt",
		Tracepoint:   "enter_cachestat",
		Comm:         "ioworkload",
	}
	// UNCLASSIFIED: no byte count is attributed for a successful cachestat.
	assertEventBytesEqual(t, result, exp, 0)
	// Timing is captured end-to-end (enter/exit paired into a duration).
	assertEventDurationPositive(t, result, exp)
}

func assertEventBytesAtLeast(t *testing.T, result TestResult, exp ExpectedEvent, minBytes uint64) {
	t.Helper()
	var matched bool
	var totalBytes uint64
	for _, rec := range result.Records {
		if !matchesExpectation(rec, exp) {
			continue
		}
		matched = true
		totalBytes += rec.Cnt.Bytes
	}
	if !matched {
		t.Fatalf("expected event not found while asserting bytes: %+v", exp)
	}
	if totalBytes < minBytes {
		t.Fatalf("bytes for %+v too small: got=%d want>=%d", exp, totalBytes, minBytes)
	}
}

func assertEventBytesEqual(t *testing.T, result TestResult, exp ExpectedEvent, wantBytes uint64) {
	t.Helper()
	var matched bool
	var totalBytes uint64
	for _, rec := range result.Records {
		if !matchesExpectation(rec, exp) {
			continue
		}
		matched = true
		totalBytes += rec.Cnt.Bytes
	}
	if !matched {
		t.Fatalf("expected event not found while asserting bytes: %+v", exp)
	}
	if totalBytes != wantBytes {
		t.Fatalf("bytes for %+v mismatch: got=%d want=%d", exp, totalBytes, wantBytes)
	}
}

func assertEventBytesReasonable(t *testing.T, result TestResult, exp ExpectedEvent) {
	t.Helper()
	const maxReasonableBytes = 1 << 20 // 1MiB is far above any bytes used in these scenarios.
	var matched bool
	for _, rec := range result.Records {
		if !matchesExpectation(rec, exp) {
			continue
		}
		matched = true
		if rec.Cnt.Bytes > maxReasonableBytes {
			t.Fatalf("unreasonable bytes for %+v: got=%d (possible underflow/sign issue)", exp, rec.Cnt.Bytes)
		}
	}
	if !matched {
		t.Fatalf("expected event not found while asserting byte range: %+v", exp)
	}
}

func assertEventDurationPositive(t *testing.T, result TestResult, exp ExpectedEvent) {
	t.Helper()
	var matched bool
	var totalDuration uint64
	for _, rec := range result.Records {
		if !matchesExpectation(rec, exp) {
			continue
		}
		matched = true
		totalDuration += rec.Cnt.Duration
	}
	if !matched {
		t.Fatalf("expected event not found while asserting duration: %+v", exp)
	}
	if totalDuration == 0 {
		t.Fatalf("duration for %+v is zero", exp)
	}
}