xref: /linux/tools/perf/tests/shell/record.sh (revision bf5802238dc181b1f7375d358af1d01cd72d1c11)
1#!/bin/sh
2# perf record tests
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7shelldir=$(dirname "$0")
8# shellcheck source=lib/waiting.sh
9. "${shelldir}"/lib/waiting.sh
10
11# shellcheck source=lib/perf_has_symbol.sh
12. "${shelldir}"/lib/perf_has_symbol.sh
13
14testsym="test_loop"
15
16skip_test_missing_symbol ${testsym}
17
18err=0
19perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
20testprog="perf test -w thloop"
21cpu_pmu_dir="/sys/bus/event_source/devices/cpu*"
22br_cntr_file="/caps/branch_counter_nr"
23br_cntr_output="branch stack counters"
24
25cleanup() {
26  rm -rf "${perfdata}"
27  rm -rf "${perfdata}".old
28
29  trap - EXIT TERM INT
30}
31
32trap_cleanup() {
33  cleanup
34  exit 1
35}
36trap trap_cleanup EXIT TERM INT
37
38test_per_thread() {
39  echo "Basic --per-thread mode test"
40  if ! perf record -o /dev/null --quiet ${testprog} 2> /dev/null
41  then
42    echo "Per-thread record [Skipped event not supported]"
43    return
44  fi
45  if ! perf record --per-thread -o "${perfdata}" ${testprog} 2> /dev/null
46  then
47    echo "Per-thread record [Failed record]"
48    err=1
49    return
50  fi
51  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
52  then
53    echo "Per-thread record [Failed missing output]"
54    err=1
55    return
56  fi
57
58  # run the test program in background (for 30 seconds)
59  ${testprog} 30 &
60  TESTPID=$!
61
62  rm -f "${perfdata}"
63
64  wait_for_threads ${TESTPID} 2
65  perf record -p "${TESTPID}" --per-thread -o "${perfdata}" sleep 1 2> /dev/null
66  kill ${TESTPID}
67
68  if [ ! -e "${perfdata}" ]
69  then
70    echo "Per-thread record [Failed record -p]"
71    err=1
72    return
73  fi
74  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
75  then
76    echo "Per-thread record [Failed -p missing output]"
77    err=1
78    return
79  fi
80
81  echo "Basic --per-thread mode test [Success]"
82}
83
84test_register_capture() {
85  echo "Register capture test"
86  if ! perf list | grep -q 'br_inst_retired.near_call'
87  then
88    echo "Register capture test [Skipped missing event]"
89    return
90  fi
91  if ! perf record --intr-regs=\? 2>&1 | grep -q 'available registers: AX BX CX DX SI DI BP SP IP FLAGS CS SS R8 R9 R10 R11 R12 R13 R14 R15'
92  then
93    echo "Register capture test [Skipped missing registers]"
94    return
95  fi
96  if ! perf record -o - --intr-regs=di,r8,dx,cx -e br_inst_retired.near_call \
97    -c 1000 --per-thread ${testprog} 2> /dev/null \
98    | perf script -F ip,sym,iregs -i - 2> /dev/null \
99    | grep -q "DI:"
100  then
101    echo "Register capture test [Failed missing output]"
102    err=1
103    return
104  fi
105  echo "Register capture test [Success]"
106}
107
108test_system_wide() {
109  echo "Basic --system-wide mode test"
110  if ! perf record -aB --synth=no -o "${perfdata}" ${testprog} 2> /dev/null
111  then
112    echo "System-wide record [Skipped not supported]"
113    return
114  fi
115  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
116  then
117    echo "System-wide record [Failed missing output]"
118    err=1
119    return
120  fi
121  if ! perf record -aB --synth=no -e cpu-clock,cs --threads=cpu \
122    -o "${perfdata}" ${testprog} 2> /dev/null
123  then
124    echo "System-wide record [Failed record --threads option]"
125    err=1
126    return
127  fi
128  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
129  then
130    echo "System-wide record [Failed --threads missing output]"
131    err=1
132    return
133  fi
134  echo "Basic --system-wide mode test [Success]"
135}
136
137test_workload() {
138  echo "Basic target workload test"
139  if ! perf record -o "${perfdata}" ${testprog} 2> /dev/null
140  then
141    echo "Workload record [Failed record]"
142    err=1
143    return
144  fi
145  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
146  then
147    echo "Workload record [Failed missing output]"
148    err=1
149    return
150  fi
151  if ! perf record -e cpu-clock,cs --threads=package \
152    -o "${perfdata}" ${testprog} 2> /dev/null
153  then
154    echo "Workload record [Failed record --threads option]"
155    err=1
156    return
157  fi
158  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
159  then
160    echo "Workload record [Failed --threads missing output]"
161    err=1
162    return
163  fi
164  echo "Basic target workload test [Success]"
165}
166
167test_branch_counter() {
168  echo "Basic branch counter test"
169  # Check if the branch counter feature is supported
170  for dir in $cpu_pmu_dir
171  do
172    if [ ! -e "$dir$br_cntr_file" ]
173    then
174      echo "branch counter feature not supported on all core PMUs ($dir) [Skipped]"
175      return
176    fi
177  done
178  if ! perf record -o "${perfdata}" -j any,counter ${testprog} 2> /dev/null
179  then
180    echo "Basic branch counter test [Failed record]"
181    err=1
182    return
183  fi
184  if ! perf report -i "${perfdata}" -D -q | grep -q "$br_cntr_output"
185  then
186    echo "Basic branch record test [Failed missing output]"
187    err=1
188    return
189  fi
190  echo "Basic branch counter test [Success]"
191}
192
193test_per_thread
194test_register_capture
195test_system_wide
196test_workload
197test_branch_counter
198
199cleanup
200exit $err
201