xref: /linux/tools/tracing/rtla/tests/timerlat.t (revision 244d0cbff2efa13931115784e5dc4d1270a04ec7)
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# Histogram tests
59check "hist with -b/--bucket-size" \
60	"timerlat hist -b 1 -d 1s"
61check "hist with -E/--entries" \
62	"timerlat hist -E 10 -d 1s"
63check "hist with -E/--entries out of range" \
64	"timerlat hist -E 1 -d 1s" 1 "^Entries must be > 10 and < 10000000$"
65check "hist with --no-header" \
66	"timerlat hist --no-header -d 1s" 0 "" "RTLA timerlat histogram"
67check "hist with --with-zeros" \
68	"timerlat hist --with-zeros -b 100000 -E 21 -d 1s" 0 '^2000000\s+0\s+'
69check "hist with --no-index" \
70	"timerlat hist --no-index --with-zeros -d 1s" 0 "" "^count:"
71check "hist with --no-summary" \
72	"timerlat hist --no-summary -d 1s" 0 "" "^ALL:"
73check "hist with --no-irq" \
74	"timerlat hist --no-irq -d 1s" 0 "" "IRQ-"
75check "hist with --no-thread" \
76	"timerlat hist --no-thread -d 1s" 0 "" "Thr-"
77
78# Actions tests
79check_top_q_hist "trace output through -t" \
80	"timerlat TOOL -T 2 -t" 2 "^  Saving trace to timerlat_trace.txt$"
81check_top_q_hist "trace output through -t with custom filename" \
82	"timerlat TOOL -T 2 -t custom_filename.txt" 2 "^  Saving trace to custom_filename.txt$"
83check_top_q_hist "trace output through --on-threshold trace" \
84	"timerlat TOOL -T 2 --on-threshold trace" 2 "^  Saving trace to timerlat_trace.txt$"
85check_top_q_hist "trace output through --on-threshold trace with custom filename" \
86	"timerlat TOOL -T 2 --on-threshold trace,file=custom_filename.txt" 2 "^  Saving trace to custom_filename.txt$"
87check_top_q_hist "exec command" \
88	"timerlat TOOL -T 2 --on-threshold shell,command='echo TestOutput'" 2 "^TestOutput$"
89check_top_q_hist "multiple actions" \
90	"timerlat TOOL -T 2 --on-threshold shell,command='echo -n 1' --on-threshold shell,command='echo 2'" 2 "^12$"
91check "hist stop at failed action" \
92	"timerlat hist -T 2 --on-threshold shell,command='echo -n 1; false' --on-threshold shell,command='echo -n 2'" 2 "^1# RTLA timerlat histogram$"
93check "top stop at failed action" \
94	"timerlat top -T 2 --on-threshold shell,command='echo -n abc; false' --on-threshold shell,command='echo -n defgh'" 2 "^abc" "defgh"
95check_top_q_hist "with continue" \
96	"timerlat TOOL -T 2 -d 5s --on-threshold shell,command='echo TestOutput' --on-threshold continue" 0 "^TestOutput$"
97check_top_q_hist "with conditional continue" \
98	"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$"
99check_top_hist "with trace output at end" \
100	"timerlat TOOL -d 1s --on-end trace" 0 "^  Saving trace to timerlat_trace.txt$"
101
102# BPF action program tests
103if [ "$option" -eq 0 ]
104then
105	# Test BPF action program properly in BPF mode
106	[ -z "$BPFTOOL" ] && BPFTOOL=bpftool
107	check_top_q_hist "with BPF action program (BPF mode)" \
108		"timerlat TOOL -T 2 --bpf-action $testdir/bpf/bpf_action_map.o --on-threshold shell,command='$BPFTOOL map dump name rtla_test_map'" \
109		2 '"value": 42'
110else
111	# Test BPF action program failure in non-BPF mode
112	check_top_q_hist "with BPF action program (non-BPF mode)" \
113		"timerlat TOOL -T 2 --bpf-action $testdir/bpf/bpf_action_map.o" \
114		1 "BPF actions are not supported in tracefs-only mode"
115fi
116done
117
118test_end
119