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
|
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# compaction-times python test
set -e
shelldir=$(dirname "$0")
# shellcheck source=lib/setup_python.sh
. "${shelldir}"/lib/setup_python.sh
# If we don't have the perf python module, we can't test
if ! "$PYTHON" -c 'import perf' > /dev/null 2>&1; then
echo "Skipping test, perf python module not found"
exit 2
fi
if ! perf check feature -q libtraceevent; then
echo "Skipping test, perf built without libtraceevent"
exit 2
fi
script_dir="$(dirname "$0")/../../python"
script_path="${script_dir}/compaction-times.py"
if [ ! -f "$script_path" ]; then
echo "Skipping test, compaction-times.py not found at $script_path"
exit 2
fi
err=0
temp_data=""
temp_out=""
cleanup() {
rm -f "${temp_data}" "${temp_out}"
}
trap 'cleanup' EXIT TERM INT
temp_data=$(mktemp /tmp/perf.data.XXXXXX)
temp_out=$(mktemp /tmp/perf.out.XXXXXX)
test_file_mode() {
echo "Testing compaction-times.py..."
# Check for any compaction events to see if kernel supports it
if ! perf list tracepoint | grep -q "compaction:mm_compaction_begin"; then
echo "Skipping test, compaction tracepoints not found"
exit 2
fi
# Generate some events
# We might not naturally trigger compaction in 0.1s sleep, but the script
# should parse the empty or sparse file correctly without crashing.
if ! perf record -B -N --no-bpf-event \
-e "compaction:mm_compaction_begin,compaction:mm_compaction_end" \
-a -o "${temp_data}" -- sleep 0.1 >/dev/null 2>&1; then
echo "Skipping test, perf record failed"
exit 2
fi
# Run the script with some filters to validate filtering logic
if ! perf script compaction-times -i "${temp_data}" > "${temp_out}"; then
echo "File mode default test failed."
err=1
elif ! grep -q "total:" "${temp_out}"; then
echo "File mode default test missing 'total:' output."
err=1
fi
if ! perf script compaction-times -i "${temp_data}" "0-0" > "${temp_out}"; then
echo "File mode strict PID filter test failed."
err=1
elif ! grep -q "total:" "${temp_out}"; then
echo "File mode strict PID filter test missing 'total:' output."
err=1
fi
if ! perf script compaction-times -i "${temp_data}" "sleep" > "${temp_out}"; then
echo "File mode comm filter test failed."
err=1
elif ! grep -q "total:" "${temp_out}"; then
echo "File mode comm filter test missing 'total:' output."
err=1
fi
# Deterministic unit test of comm filtering and non-zero duration aggregation
if ! "$PYTHON" -c "
import re, sys, importlib.util
spec = importlib.util.spec_from_file_location('compaction_times', sys.argv[1])
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
class DummyThread:
def comm(self): return 'sleep'
class DummySession:
def find_thread(self, pid, tid): return DummyThread()
class DummySample:
def __init__(self, evsel, time, **kw):
self.evsel = evsel
self.sample_time = time
self.sample_pid = 100
self.sample_tid = 100
for k, v in kw.items():
setattr(self, k, v)
mod.Chead.add_filter(mod.get_comm_filter(re.compile('sleep')))
mod.session = DummySession()
mod.process_event(DummySample('evsel(compaction:mm_compaction_begin)', 1000))
mod.process_event(DummySample('evsel(compaction:mm_compaction_end)', 2500))
mod.trace_end()
" "$script_path" > "${temp_out}"; then
echo "Deterministic comm filter unit test failed."
err=1
elif ! grep -q "total: 1500ns" "${temp_out}"; then
echo "Deterministic comm filter unit test did not report expected 1500ns total."
err=1
fi
if [ $err -eq 0 ]; then
echo "File mode test passed."
fi
}
test_file_mode
exit $err
|