1#!/bin/bash
2# perf script task-analyzer tests
3# SPDX-License-Identifier: GPL-2.0
4
5tmpdir=$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX)
6err=0
7
8# set PERF_EXEC_PATH to find scripts in the source directory
9perfdir=$(dirname "$0")/../..
10if [ -e "$perfdir/scripts/python/Perf-Trace-Util" ]; then
11  export PERF_EXEC_PATH=$perfdir
12fi
13
14cleanup() {
15  rm -f perf.data
16  rm -f perf.data.old
17  rm -f csv
18  rm -f csvsummary
19  rm -rf "$tmpdir"
20  trap - exit term int
21}
22
23trap_cleanup() {
24  cleanup
25  exit 1
26}
27trap trap_cleanup exit term int
28
29report() {
30	if [ "$1" = 0 ]; then
31		echo "PASS: \"$2\""
32	else
33		echo "FAIL: \"$2\" Error message: \"$3\""
34		err=1
35	fi
36}
37
38check_exec_0() {
39	if [ $? != 0 ]; then
40		report 1 "invocation of $1 command failed"
41	fi
42}
43
44find_str_or_fail() {
45	grep -q "$1" "$2"
46	if [ "$?" != 0 ]; then
47		report 1 "$3" "Failed to find required string:'${1}'."
48	else
49		report 0 "$3"
50	fi
51}
52
53# check if perf is compiled with libtraceevent support
54skip_no_probe_record_support() {
55	perf record -e "sched:sched_switch" -a -- sleep 1 2>&1 | grep "libtraceevent is necessary for tracepoint support" && return 2
56	return 0
57}
58
59prepare_perf_data() {
60	# 1s should be sufficient to catch at least some switches
61	perf record -e sched:sched_switch -a -- sleep 1 > /dev/null 2>&1
62	# check if perf data file got created in above step.
63	if [ ! -e "perf.data" ]; then
64		printf "FAIL: perf record failed to create \"perf.data\" \n"
65		return 1
66	fi
67}
68
69# check standard inkvokation with no arguments
70test_basic() {
71	out="$tmpdir/perf.out"
72	perf script report task-analyzer > "$out"
73	check_exec_0 "perf script report task-analyzer"
74	find_str_or_fail "Comm" "$out" "${FUNCNAME[0]}"
75}
76
77test_ns_rename(){
78	out="$tmpdir/perf.out"
79	perf script report task-analyzer --ns --rename-comms-by-tids 0:random > "$out"
80	check_exec_0 "perf script report task-analyzer --ns --rename-comms-by-tids 0:random"
81	find_str_or_fail "Comm" "$out" "${FUNCNAME[0]}"
82}
83
84test_ms_filtertasks_highlight(){
85	out="$tmpdir/perf.out"
86	perf script report task-analyzer --ms --filter-tasks perf --highlight-tasks perf \
87	> "$out"
88	check_exec_0 "perf script report task-analyzer --ms --filter-tasks perf --highlight-tasks perf"
89	find_str_or_fail "Comm" "$out" "${FUNCNAME[0]}"
90}
91
92test_extended_times_timelimit_limittasks() {
93	out="$tmpdir/perf.out"
94	perf script report task-analyzer --extended-times --time-limit :99999 \
95	--limit-to-tasks perf > "$out"
96	check_exec_0 "perf script report task-analyzer --extended-times --time-limit :99999 --limit-to-tasks perf"
97	find_str_or_fail "Out-Out" "$out" "${FUNCNAME[0]}"
98}
99
100test_summary() {
101	out="$tmpdir/perf.out"
102	perf script report task-analyzer --summary > "$out"
103	check_exec_0 "perf script report task-analyzer --summary"
104	find_str_or_fail "Summary" "$out" "${FUNCNAME[0]}"
105}
106
107test_summaryextended() {
108	out="$tmpdir/perf.out"
109	perf script report task-analyzer --summary-extended > "$out"
110	check_exec_0 "perf script report task-analyzer --summary-extended"
111	find_str_or_fail "Inter Task Times" "$out" "${FUNCNAME[0]}"
112}
113
114test_summaryonly() {
115	out="$tmpdir/perf.out"
116	perf script report task-analyzer --summary-only > "$out"
117	check_exec_0 "perf script report task-analyzer --summary-only"
118	find_str_or_fail "Summary" "$out" "${FUNCNAME[0]}"
119}
120
121test_extended_times_summary_ns() {
122	out="$tmpdir/perf.out"
123	perf script report task-analyzer --extended-times --summary --ns > "$out"
124	check_exec_0 "perf script report task-analyzer --extended-times --summary --ns"
125	find_str_or_fail "Out-Out" "$out" "${FUNCNAME[0]}"
126	find_str_or_fail "Summary" "$out" "${FUNCNAME[0]}"
127}
128
129test_csv() {
130	perf script report task-analyzer --csv csv > /dev/null
131	check_exec_0 "perf script report task-analyzer --csv csv"
132	find_str_or_fail "Comm;" csv "${FUNCNAME[0]}"
133}
134
135test_csv_extended_times() {
136	perf script report task-analyzer --csv csv --extended-times > /dev/null
137	check_exec_0 "perf script report task-analyzer --csv csv --extended-times"
138	find_str_or_fail "Out-Out;" csv "${FUNCNAME[0]}"
139}
140
141test_csvsummary() {
142	perf script report task-analyzer --csv-summary csvsummary > /dev/null
143	check_exec_0 "perf script report task-analyzer --csv-summary csvsummary"
144	find_str_or_fail "Comm;" csvsummary "${FUNCNAME[0]}"
145}
146
147test_csvsummary_extended() {
148	perf script report task-analyzer --csv-summary csvsummary --summary-extended \
149	>/dev/null
150	check_exec_0 "perf script report task-analyzer --csv-summary csvsummary --summary-extended"
151	find_str_or_fail "Out-Out;" csvsummary "${FUNCNAME[0]}"
152}
153
154skip_no_probe_record_support
155err=$?
156if [ $err -ne 0 ]; then
157	echo "WARN: Skipping tests. No libtraceevent support"
158	cleanup
159	exit $err
160fi
161prepare_perf_data
162test_basic
163test_ns_rename
164test_ms_filtertasks_highlight
165test_extended_times_timelimit_limittasks
166test_summary
167test_summaryextended
168test_summaryonly
169test_extended_times_summary_ns
170test_csv
171test_csvsummary
172test_csv_extended_times
173test_csvsummary_extended
174cleanup
175exit $err
176