summaryrefslogtreecommitdiff
path: root/internal/flags/sampling_test.go
blob: 82a43ecfbe8a406c3c19777f59d49863e3a7045d (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
package flags

import (
	"strings"
	"testing"

	"ior/internal/types"
)

func TestParseSamplingRates(t *testing.T) {
	cfg, err := parseForTest(t,
		"-syscall-sampling-families", "Time=100,misc=0",
		"-syscall-sampling-syscalls", "futex=0,clock_gettime=7",
	)
	if err != nil {
		t.Fatalf("parse returned error: %v", err)
	}

	if got := cfg.SyscallFamilySamplingRates[types.FamilyTime]; got != 100 {
		t.Fatalf("Time family rate = %d, want 100", got)
	}
	if got := cfg.SyscallFamilySamplingRates[types.FamilyMisc]; got != 0 {
		t.Fatalf("Misc family rate = %d, want 0", got)
	}
	if got := cfg.SyscallSamplingRates["futex"]; got != 0 {
		t.Fatalf("futex rate = %d, want 0", got)
	}
	if got := cfg.SyscallSamplingRates["clock_gettime"]; got != 7 {
		t.Fatalf("clock_gettime rate = %d, want 7", got)
	}
}

func TestParseSamplingFamilyRejectsUnknown(t *testing.T) {
	_, err := parseForTest(t, "-syscall-sampling-families", "Nope=4")
	if err == nil {
		t.Fatal("expected parse error")
	}
	if !strings.Contains(err.Error(), "invalid syscall family") {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestParseSamplingSyscallRejectsMalformedEntry(t *testing.T) {
	_, err := parseForTest(t, "-syscall-sampling-syscalls", "futex")
	if err == nil {
		t.Fatal("expected parse error")
	}
	if !strings.Contains(err.Error(), "expected name=rate") {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestParseSamplingSyscallRejectsUnknownName(t *testing.T) {
	_, err := parseForTest(t, "-syscall-sampling-syscalls", "not_a_syscall=2")
	if err == nil {
		t.Fatal("expected parse error")
	}
	if !strings.Contains(err.Error(), "invalid syscall in sampling map") {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestCloneDeepCopiesSamplingMaps(t *testing.T) {
	cfg := NewFlags()
	cfg.SyscallFamilySamplingRates[types.FamilyTime] = 5
	cfg.SyscallSamplingRates["futex"] = 9

	cloned := cfg.Clone()
	cloned.SyscallFamilySamplingRates[types.FamilyTime] = 100
	cloned.SyscallSamplingRates["futex"] = 1

	if got := cfg.SyscallFamilySamplingRates[types.FamilyTime]; got != 5 {
		t.Fatalf("original family rate mutated: got %d, want 5", got)
	}
	if got := cfg.SyscallSamplingRates["futex"]; got != 9 {
		t.Fatalf("original syscall rate mutated: got %d, want 9", got)
	}
}

func TestDefaultSamplingRatesIncludeFutexAggregateOnly(t *testing.T) {
	cfg, err := parseForTest(t)
	if err != nil {
		t.Fatalf("parse returned error: %v", err)
	}
	for _, syscall := range []string{"futex", "futex_wait", "futex_wake", "futex_requeue", "futex_waitv"} {
		rate, ok := cfg.SyscallSamplingRates[syscall]
		if !ok {
			t.Fatalf("expected default sampling entry for %s", syscall)
		}
		if rate != 0 {
			t.Fatalf("%s default rate = %d, want 0 (aggregate-only)", syscall, rate)
		}
	}
}

func TestParseSamplingRatesOverrideDefaultFutexRate(t *testing.T) {
	cfg, err := parseForTest(t, "-syscall-sampling-syscalls", "futex=7")
	if err != nil {
		t.Fatalf("parse returned error: %v", err)
	}
	if got := cfg.SyscallSamplingRates["futex"]; got != 7 {
		t.Fatalf("futex rate = %d, want 7", got)
	}
}