summaryrefslogtreecommitdiff
path: root/internal/syscall_aggregate_consumer_test.go
blob: a78899bb0d28713a318bb06634183036b6622146 (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
package internal

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"testing"
	"unsafe"

	"ior/internal/flags"
	"ior/internal/statsengine"
	"ior/internal/types"
)

func TestBuildSyscallSamplingRatesFamilyAndSyscallOverride(t *testing.T) {
	cfg := flags.NewFlags()
	cfg.SyscallFamilySamplingRates[types.FamilyTime] = 100
	cfg.SyscallSamplingRates["clock_gettime"] = 3

	rates := buildSyscallSamplingRates(cfg)
	if got := rates[types.SYS_ENTER_NANOSLEEP]; got != 100 {
		t.Fatalf("nanosleep rate = %d, want 100", got)
	}
	if got := rates[types.SYS_ENTER_CLOCK_GETTIME]; got != 3 {
		t.Fatalf("clock_gettime rate = %d, want 3", got)
	}
}

func TestBuildAggregateOnlyTraceIDs(t *testing.T) {
	cfg := flags.NewFlags()
	cfg.SyscallFamilySamplingRates[types.FamilyTime] = 10
	cfg.SyscallSamplingRates["futex"] = 0
	cfg.SyscallSamplingRates["clock_gettime"] = 0

	ids := buildAggregateOnlyTraceIDs(cfg)
	if _, ok := ids[types.SYS_ENTER_FUTEX]; !ok {
		t.Fatal("expected futex in aggregate-only set")
	}
	if _, ok := ids[types.SYS_ENTER_CLOCK_GETTIME]; !ok {
		t.Fatal("expected clock_gettime in aggregate-only set")
	}
	if _, ok := ids[types.SYS_ENTER_NANOSLEEP]; ok {
		t.Fatal("did not expect nanosleep in aggregate-only set")
	}
}

func TestDecodeRawSyscallAggregate(t *testing.T) {
	want := rawSyscallAggregate{
		Count:         7,
		Errors:        2,
		TotalDuration: 1234,
		MinDuration:   12,
		MaxDuration:   456,
		Histogram:     [8]uint64{1, 2, 3, 4, 5, 6, 7, 8},
	}
	var buf bytes.Buffer
	if err := binary.Write(&buf, binary.LittleEndian, want); err != nil {
		t.Fatalf("binary write: %v", err)
	}

	got, err := decodeRawSyscallAggregate(buf.Bytes())
	if err != nil {
		t.Fatalf("decodeRawSyscallAggregate error: %v", err)
	}
	if got != want {
		t.Fatalf("decoded aggregate = %+v, want %+v", got, want)
	}
}

func TestDecodeRawSyscallAggregateRejectsBadSize(t *testing.T) {
	if _, err := decodeRawSyscallAggregate([]byte{1, 2, 3}); err == nil {
		t.Fatal("expected error for short value")
	}
}

func TestDecodeRawSyscallAggregatePerCPUSumsActiveCPUs(t *testing.T) {
	raw := encodeRawAggregates(t,
		rawSyscallAggregate{
			Count:         2,
			Errors:        1,
			TotalDuration: 30,
			MinDuration:   10,
			MaxDuration:   20,
			Histogram:     [8]uint64{1, 0, 1},
		},
		rawSyscallAggregate{},
		rawSyscallAggregate{
			Count:         3,
			Errors:        0,
			TotalDuration: 90,
			MinDuration:   5,
			MaxDuration:   50,
			Histogram:     [8]uint64{0, 2, 1},
		},
	)

	got, err := decodeRawSyscallAggregatePerCPU(raw)
	if err != nil {
		t.Fatalf("decodeRawSyscallAggregatePerCPU error: %v", err)
	}

	want := rawSyscallAggregate{
		Count:         5,
		Errors:        1,
		TotalDuration: 120,
		MinDuration:   5,
		MaxDuration:   50,
		Histogram:     [8]uint64{1, 2, 2},
	}
	if got != want {
		t.Fatalf("per-cpu aggregate = %+v, want %+v", got, want)
	}
}

func TestDecodeRawSyscallAggregatePerCPURejectsBadSize(t *testing.T) {
	raw := encodeRawAggregates(t, rawSyscallAggregate{Count: 1})
	raw = append(raw, 0)

	if _, err := decodeRawSyscallAggregatePerCPU(raw); err == nil {
		t.Fatal("expected error for non-stride-aligned value")
	}
}

func TestDecodeRawSyscallAggregatePerCPURejectsEmptyValue(t *testing.T) {
	if _, err := decodeRawSyscallAggregatePerCPU(nil); err == nil {
		t.Fatal("expected error for empty per-cpu value")
	}
}

func TestSyscallAggregateConsumerDrainEmitsDeltas(t *testing.T) {
	const traceID = uint32(types.SYS_ENTER_FUTEX)
	fakeMap := newFakeSyscallAggregateMap(traceID, encodeRawAggregates(t,
		rawSyscallAggregate{
			Count:         2,
			Errors:        1,
			TotalDuration: 30,
			MinDuration:   10,
			MaxDuration:   20,
			Histogram:     [8]uint64{1, 0, 1},
		},
		rawSyscallAggregate{
			Count:         3,
			TotalDuration: 90,
			MinDuration:   5,
			MaxDuration:   50,
			Histogram:     [8]uint64{0, 2, 1},
		},
	))
	consumer := &syscallAggregateConsumer{
		aggregateMap: fakeMap,
		last:         make(map[types.TraceId]rawSyscallAggregate),
	}

	rows, err := consumer.Drain()
	if err != nil {
		t.Fatalf("first Drain error: %v", err)
	}
	assertAggregateRows(t, rows, statsengine.SyscallAggregate{
		TraceID:        types.TraceId(traceID),
		Count:          5,
		Errors:         1,
		TotalLatencyNs: 120,
		MinLatencyNs:   5,
		MaxLatencyNs:   50,
		LatencyHistogramNs: [8]uint64{
			1, 2, 2,
		},
	})

	fakeMap.values[traceID] = encodeRawAggregates(t,
		rawSyscallAggregate{
			Count:         4,
			Errors:        2,
			TotalDuration: 80,
			MinDuration:   4,
			MaxDuration:   40,
			Histogram:     [8]uint64{2, 1, 1},
		},
		rawSyscallAggregate{
			Count:         3,
			TotalDuration: 110,
			MinDuration:   5,
			MaxDuration:   70,
			Histogram:     [8]uint64{0, 2, 1, 1},
		},
	)
	rows, err = consumer.Drain()
	if err != nil {
		t.Fatalf("second Drain error: %v", err)
	}
	assertAggregateRows(t, rows, statsengine.SyscallAggregate{
		TraceID:        types.TraceId(traceID),
		Count:          2,
		Errors:         1,
		TotalLatencyNs: 70,
		MinLatencyNs:   4,
		MaxLatencyNs:   70,
		LatencyHistogramNs: [8]uint64{
			1, 1, 0, 1,
		},
	})

	fakeMap.values[traceID] = encodeRawAggregates(t,
		rawSyscallAggregate{
			Count:         5,
			Errors:        2,
			TotalDuration: 100,
			MinDuration:   4,
			MaxDuration:   40,
			Histogram:     [8]uint64{3, 1, 1},
		},
		rawSyscallAggregate{
			Count:         3,
			TotalDuration: 110,
			MinDuration:   5,
			MaxDuration:   70,
			Histogram:     [8]uint64{0, 2, 1, 1},
		},
	)
	rows, err = consumer.Drain()
	if err != nil {
		t.Fatalf("third Drain error: %v", err)
	}
	// Unchanged cumulative extrema use delta bucket sentinels: min 0 means no new lower min, max 999 caps bucket 0.
	assertAggregateRows(t, rows, statsengine.SyscallAggregate{
		TraceID:        types.TraceId(traceID),
		Count:          1,
		Errors:         0,
		TotalLatencyNs: 20,
		MinLatencyNs:   0,
		MaxLatencyNs:   999,
		LatencyHistogramNs: [8]uint64{
			1,
		},
	})

	rows, err = consumer.Drain()
	if err != nil {
		t.Fatalf("fourth Drain error: %v", err)
	}
	if len(rows) != 0 {
		t.Fatalf("fourth Drain rows = %+v, want none for zero delta", rows)
	}
}

func TestRawSyscallAggregateDiffReturnsOnlyNewCounts(t *testing.T) {
	prev := rawSyscallAggregate{
		Count:         5,
		Errors:        1,
		TotalDuration: 100,
		MinDuration:   10,
		MaxDuration:   40,
		Histogram:     [8]uint64{1, 2, 2},
	}
	current := rawSyscallAggregate{
		Count:         9,
		Errors:        3,
		TotalDuration: 190,
		MinDuration:   5,
		MaxDuration:   80,
		Histogram:     [8]uint64{2, 5, 2, 1},
	}

	got := current.diff(prev)
	want := rawSyscallAggregate{
		Count:         4,
		Errors:        2,
		TotalDuration: 90,
		MinDuration:   5,
		MaxDuration:   80,
		Histogram:     [8]uint64{1, 3, 0, 1},
	}
	if got != want {
		t.Fatalf("aggregate diff = %+v, want %+v", got, want)
	}
}

func encodeRawAggregates(t *testing.T, values ...rawSyscallAggregate) []byte {
	t.Helper()

	var buf bytes.Buffer
	for _, value := range values {
		if err := binary.Write(&buf, binary.LittleEndian, value); err != nil {
			t.Fatalf("binary write: %v", err)
		}
	}
	return buf.Bytes()
}

func assertAggregateRows(t *testing.T, got []statsengine.SyscallAggregate, want statsengine.SyscallAggregate) {
	t.Helper()

	if len(got) != 1 {
		t.Fatalf("Drain rows = %+v, want one row", got)
	}
	if got[0] != want {
		t.Fatalf("Drain row = %+v, want %+v", got[0], want)
	}
}

type fakeSyscallAggregateMap struct {
	keys   [][]byte
	values map[uint32][]byte
}

func newFakeSyscallAggregateMap(traceID uint32, value []byte) *fakeSyscallAggregateMap {
	key := make([]byte, 4)
	binary.LittleEndian.PutUint32(key, traceID)
	return &fakeSyscallAggregateMap{
		keys: [][]byte{key},
		values: map[uint32][]byte{
			traceID: value,
		},
	}
}

func (m *fakeSyscallAggregateMap) Iterator() syscallAggregateIterator {
	return &fakeSyscallAggregateIterator{keys: m.keys}
}

func (m *fakeSyscallAggregateMap) GetValue(keyPtr unsafe.Pointer) ([]byte, error) {
	key := *(*uint32)(keyPtr)
	value, ok := m.values[key]
	if !ok {
		return nil, fmt.Errorf("missing value for key %d", key)
	}
	return append([]byte(nil), value...), nil
}

type fakeSyscallAggregateIterator struct {
	keys [][]byte
	next int
}

func (i *fakeSyscallAggregateIterator) Next() bool {
	if i.next >= len(i.keys) {
		return false
	}
	i.next++
	return i.next <= len(i.keys)
}

func (i *fakeSyscallAggregateIterator) Key() []byte {
	if i.next == 0 || i.next > len(i.keys) {
		return nil
	}
	return i.keys[i.next-1]
}

func (i *fakeSyscallAggregateIterator) Err() error {
	return nil
}