xref: /linux/tools/perf/tests/shell/record.sh (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
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
11err=0
12perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
13testprog="perf test -w thloop"
14testsym="test_loop"
15
16cleanup() {
17  rm -rf "${perfdata}"
18  rm -rf "${perfdata}".old
19
20  trap - EXIT TERM INT
21}
22
23trap_cleanup() {
24  cleanup
25  exit 1
26}
27trap trap_cleanup EXIT TERM INT
28
29test_per_thread() {
30  echo "Basic --per-thread mode test"
31  if ! perf record -o /dev/null --quiet ${testprog} 2> /dev/null
32  then
33    echo "Per-thread record [Skipped event not supported]"
34    return
35  fi
36  if ! perf record --per-thread -o "${perfdata}" ${testprog} 2> /dev/null
37  then
38    echo "Per-thread record [Failed record]"
39    err=1
40    return
41  fi
42  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
43  then
44    echo "Per-thread record [Failed missing output]"
45    err=1
46    return
47  fi
48
49  # run the test program in background (for 30 seconds)
50  ${testprog} 30 &
51  TESTPID=$!
52
53  rm -f "${perfdata}"
54
55  wait_for_threads ${TESTPID} 2
56  perf record -p "${TESTPID}" --per-thread -o "${perfdata}" sleep 1 2> /dev/null
57  kill ${TESTPID}
58
59  if [ ! -e "${perfdata}" ]
60  then
61    echo "Per-thread record [Failed record -p]"
62    err=1
63    return
64  fi
65  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
66  then
67    echo "Per-thread record [Failed -p missing output]"
68    err=1
69    return
70  fi
71
72  echo "Basic --per-thread mode test [Success]"
73}
74
75test_register_capture() {
76  echo "Register capture test"
77  if ! perf list | grep -q 'br_inst_retired.near_call'
78  then
79    echo "Register capture test [Skipped missing event]"
80    return
81  fi
82  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'
83  then
84    echo "Register capture test [Skipped missing registers]"
85    return
86  fi
87  if ! perf record -o - --intr-regs=di,r8,dx,cx -e br_inst_retired.near_call \
88    -c 1000 --per-thread ${testprog} 2> /dev/null \
89    | perf script -F ip,sym,iregs -i - 2> /dev/null \
90    | grep -q "DI:"
91  then
92    echo "Register capture test [Failed missing output]"
93    err=1
94    return
95  fi
96  echo "Register capture test [Success]"
97}
98
99test_system_wide() {
100  echo "Basic --system-wide mode test"
101  if ! perf record -aB --synth=no -o "${perfdata}" ${testprog} 2> /dev/null
102  then
103    echo "System-wide record [Skipped not supported]"
104    return
105  fi
106  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
107  then
108    echo "System-wide record [Failed missing output]"
109    err=1
110    return
111  fi
112  if ! perf record -aB --synth=no -e cpu-clock,cs --threads=cpu \
113    -o "${perfdata}" ${testprog} 2> /dev/null
114  then
115    echo "System-wide record [Failed record --threads option]"
116    err=1
117    return
118  fi
119  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
120  then
121    echo "System-wide record [Failed --threads missing output]"
122    err=1
123    return
124  fi
125  echo "Basic --system-wide mode test [Success]"
126}
127
128test_workload() {
129  echo "Basic target workload test"
130  if ! perf record -o "${perfdata}" ${testprog} 2> /dev/null
131  then
132    echo "Workload record [Failed record]"
133    err=1
134    return
135  fi
136  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
137  then
138    echo "Workload record [Failed missing output]"
139    err=1
140    return
141  fi
142  if ! perf record -e cpu-clock,cs --threads=package \
143    -o "${perfdata}" ${testprog} 2> /dev/null
144  then
145    echo "Workload record [Failed record --threads option]"
146    err=1
147    return
148  fi
149  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
150  then
151    echo "Workload record [Failed --threads missing output]"
152    err=1
153    return
154  fi
155  echo "Basic target workload test [Success]"
156}
157
158test_per_thread
159test_register_capture
160test_system_wide
161test_workload
162
163cleanup
164exit $err
165