xref: /linux/tools/tracing/rtla/tests/timerlat.t (revision 92a33d5bad7485b39c7403ad1741b3b2b35a0131)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3source tests/engine.sh
4test_begin
5
6set_timeout 2m
7timerlat_sample_event='/sys/kernel/tracing/events/osnoise/timerlat_sample'
8
9if ldd $RTLA | grep libbpf >/dev/null && [ -d "$timerlat_sample_event" ]
10then
11	# rtla build with BPF and system supports BPF mode
12	no_bpf_options='0 1'
13else
14	no_bpf_options='1'
15fi
16
17# Do every test with and without BPF
18for option in $no_bpf_options
19do
20export RTLA_NO_BPF=$option
21
22# Basic tests
23check "verify help page" \
24	"timerlat --help" 129 "timerlat version"
25check_top_hist "verify help page" \
26	"timerlat TOOL --help" 129 "rtla timerlat"
27check_top_hist "verify -s/--stack" \
28	"timerlat TOOL -s 3 -T 10 -t" 2 "Blocking thread stack trace"
29check_top_hist "test in nanoseconds" \
30	"timerlat TOOL -i 2 -c 0 -n -d 10s" 2 "ns"
31check_top_hist "set the automatic trace mode" \
32	"timerlat TOOL -a 5" 2 "analyzing it"
33check_top_hist "dump tasks" \
34	"timerlat TOOL -a 5 --dump-tasks" 2 "Printing CPU tasks"
35check "verify --aa-only stop on threshold" \
36	"timerlat top --aa-only 5" 2 "analyzing it" "Timer Latency"
37check "verify --aa-only max latency" \
38	"timerlat top --aa-only 2000000 -d 1s" 0 "^  Max latency was" "Timer Latency"
39check_top_hist "disable auto-analysis" \
40	"timerlat TOOL -s 3 -T 10 -t --no-aa" 2 "" "analyzing it"
41
42# Thread tests
43check_top_hist "verify -P/--priority" \
44	"timerlat TOOL -P F:1 -c 0 -d 10s -T 1 --on-threshold shell,command=\"$testdir/scripts/check-priority.sh SCHED_FIFO 1\"" \
45	2 "Priorities are set correctly"
46check_top_hist "verify -C/--cgroup" \
47	"timerlat TOOL -k -C -c 0 -d 10s -T 1 --on-threshold shell,command=\"$testdir/scripts/check-cgroup-match.sh\"" \
48	2 "cgroup matches for all workload PIDs"
49check_top_q_hist "verify -c/--cpus" \
50	"timerlat TOOL -c 0 -d 10s -T 1 --on-threshold shell,command=$testdir/scripts/check-cpus.sh" 2 "^Affinity of threads: 0$"
51check_top_q_hist "verify -H/--house-keeping" \
52	"timerlat TOOL -H 0 -d 10s -T 1 --on-threshold shell,command=$testdir/scripts/check-housekeeping-cpus.sh" 2 "^Affinity of threads: 0$"
53check_top_q_hist "verify -k/--kernel-threads" \
54	"timerlat TOOL -k -c 0 -d 10s -T 1 --on-threshold shell,command=$testdir/scripts/check-user-kernel-threads.sh" 2 "1 kernel threads, 0 user threads"
55check_top_q_hist "verify -u/--user-threads" \
56	"timerlat TOOL -u -c 0 -d 10s -T 1 --on-threshold shell,command=$testdir/scripts/check-user-kernel-threads.sh" 2 "0 kernel threads, 1 user threads"
57
58# Tracer option tests - verify that rtla correctly sets tracefs options
59# Default tests: poison tracefs with wrong values, verify rtla resets to defaults
60check_with_osnoise_options "apply default timerlat_period_us" \
61	"timerlat top -q -T 1 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/timerlat_period_us\"" \
62	2 "^osnoise/timerlat_period_us=1000$" osnoise/timerlat_period_us=999999
63check_with_osnoise_options "apply default print_stack" \
64	"timerlat top -q -T 1 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/print_stack\"" \
65	2 "^osnoise/print_stack=0$" osnoise/print_stack=999999
66check_with_osnoise_options "apply default stop_tracing_us" \
67	"timerlat top -q -T 1 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/stop_tracing_us\"" \
68	2 "^osnoise/stop_tracing_us=0$" osnoise/stop_tracing_us=999999
69check_with_osnoise_options "apply default stop_tracing_total_us" \
70	"timerlat top -q -i 1 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/stop_tracing_total_us\"" \
71	2 "^osnoise/stop_tracing_total_us=0$" osnoise/stop_tracing_total_us=999999
72
73# Non-default tracer option tests: verify CLI options correctly set tracefs values
74check_top_q_hist "verify -p sets timerlat_period_us" \
75	"timerlat TOOL -p 2000 -T 1 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/timerlat_period_us\"" \
76	2 "^osnoise/timerlat_period_us=2000$"
77check_top_q_hist "verify -s sets print_stack" \
78	"timerlat TOOL -s 5 -T 1 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/print_stack\"" \
79	2 "^osnoise/print_stack=5$"
80check_top_q_hist "verify -i sets stop_tracing_us" \
81	"timerlat TOOL -i 2 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/stop_tracing_us\"" \
82	2 "^osnoise/stop_tracing_us=2$"
83check_top_q_hist "verify -T sets stop_tracing_total_us" \
84	"timerlat TOOL -T 2 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/stop_tracing_total_us\"" \
85	2 "^osnoise/stop_tracing_total_us=2$"
86check_with_osnoise_options "apply default TIMERLAT_ALIGN" \
87	"timerlat top -q -T 1 --on-threshold shell,command=\"$testdir/scripts/check-osnoise-option.sh TIMERLAT_ALIGN\"" \
88	2 "^TIMERLAT_ALIGN=disabled$" osnoise/options=TIMERLAT_ALIGN
89check_top_q_hist "verify -A sets TIMERLAT_ALIGN" \
90	"timerlat TOOL -A 100 -T 1 --on-threshold shell,command=\"$testdir/scripts/check-osnoise-option.sh TIMERLAT_ALIGN\"" \
91	2 "^TIMERLAT_ALIGN=enabled$"
92check_top_q_hist "verify -A sets timerlat_align_us" \
93	"timerlat TOOL -A 100 -T 1 --on-threshold shell,command=\"$testdir/scripts/check-tracefs-value.sh osnoise/timerlat_align_us\"" \
94	2 "^osnoise/timerlat_align_us=100$"
95
96# Histogram tests
97check "hist with -b/--bucket-size" \
98	"timerlat hist -b 1 -d 1s"
99check "hist with -E/--entries" \
100	"timerlat hist -E 10 -d 1s"
101check "hist with -E/--entries out of range" \
102	"timerlat hist -E 1 -d 1s" 129 "out of range \[10, 9999999\]"
103check "hist with --no-header" \
104	"timerlat hist --no-header -d 1s" 0 "" "RTLA timerlat histogram"
105check "hist with --with-zeros" \
106	"timerlat hist --with-zeros -b 100000 -E 21 -d 1s" 0 '^2000000\s+0\s+'
107check "hist with --no-index" \
108	"timerlat hist --no-index --with-zeros -d 1s" 0 "" "^count:"
109check "hist with --no-summary" \
110	"timerlat hist --no-summary -d 1s" 0 "" "^ALL:"
111check "hist with --no-irq" \
112	"timerlat hist --no-irq -d 1s" 0 "" "IRQ-"
113check "hist with --no-thread" \
114	"timerlat hist --no-thread -d 1s" 0 "" "Thr-"
115
116# Actions tests
117check_top_q_hist "trace output through -t" \
118	"timerlat TOOL -T 2 -t" 2 "^  Saving trace to timerlat_trace.txt$"
119check_top_q_hist "trace output through -t with custom filename" \
120	"timerlat TOOL -T 2 -t custom_filename.txt" 2 "^  Saving trace to custom_filename.txt$"
121check_top_q_hist "trace output through --on-threshold trace" \
122	"timerlat TOOL -T 2 --on-threshold trace" 2 "^  Saving trace to timerlat_trace.txt$"
123check_top_q_hist "trace output through --on-threshold trace with custom filename" \
124	"timerlat TOOL -T 2 --on-threshold trace,file=custom_filename.txt" 2 "^  Saving trace to custom_filename.txt$"
125check_top_q_hist "exec command" \
126	"timerlat TOOL -T 2 --on-threshold shell,command='echo TestOutput'" 2 "^TestOutput$"
127check_top_q_hist "multiple actions" \
128	"timerlat TOOL -T 2 --on-threshold shell,command='echo -n 1' --on-threshold shell,command='echo 2'" 2 "^12$"
129check "hist stop at failed action" \
130	"timerlat hist -T 2 --on-threshold shell,command='echo -n 1; false' --on-threshold shell,command='echo -n 2'" 2 "^1# RTLA timerlat histogram$"
131check "top stop at failed action" \
132	"timerlat top -T 2 --on-threshold shell,command='echo -n abc; false' --on-threshold shell,command='echo -n defgh'" 2 "^abc" "defgh"
133check_top_q_hist "with continue" \
134	"timerlat TOOL -T 2 -d 5s --on-threshold shell,command='echo TestOutput' --on-threshold continue" 0 "^TestOutput$"
135check_top_q_hist "with conditional continue" \
136	"timerlat TOOL -T 2 --on-threshold shell,command='if [ -f a ]; then echo 2; exit 1; else echo -n 1; touch a; fi' --on-threshold continue" 2 "^12$" "^2$"
137check_top_hist "with trace output at end" \
138	"timerlat TOOL -d 1s --on-end trace" 0 "^  Saving trace to timerlat_trace.txt$"
139
140# BPF action program tests
141if [ "$option" -eq 0 ]
142then
143	# Test BPF action program properly in BPF mode
144	[ -z "$BPFTOOL" ] && BPFTOOL=bpftool
145	check_top_q_hist "with BPF action program (BPF mode)" \
146		"timerlat TOOL -T 2 --bpf-action $testdir/bpf/bpf_action_map.o --on-threshold shell,command='$BPFTOOL map dump name rtla_test_map'" \
147		2 '"value": 42'
148else
149	# Test BPF action program failure in non-BPF mode
150	check_top_q_hist "with BPF action program (non-BPF mode)" \
151		"timerlat TOOL -T 2 --bpf-action $testdir/bpf/bpf_action_map.o" \
152		1 "BPF actions are not supported in tracefs-only mode"
153fi
154done
155
156test_end
157