xref: /openbmc/linux/tools/perf/tests/shell/record.sh (revision 8b380e6afd124d18cd51a43d2505e4eb05e2ba09)
1#!/bin/sh
2# perf record tests
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7shelldir=$(dirname "$0")
8. "${shelldir}"/lib/waiting.sh
9
10err=0
11perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
12testprog=$(mktemp /tmp/__perf_test.prog.XXXXXX)
13testsym="test_loop"
14
15cleanup() {
16  rm -rf "${perfdata}"
17  rm -rf "${perfdata}".old
18
19  if [ "${testprog}" != "true" ]; then
20    rm -f "${testprog}"
21  fi
22
23  trap - EXIT TERM INT
24}
25
26trap_cleanup() {
27  cleanup
28  exit 1
29}
30trap trap_cleanup EXIT TERM INT
31
32build_test_program() {
33  if ! [ -x "$(command -v cc)" ]; then
34    # No CC found. Fall back to 'true'
35    testprog=true
36    testsym=true
37    return
38  fi
39
40  echo "Build a test program"
41  cat <<EOF | cc -o ${testprog} -xc - -pthread
42#include <stdio.h>
43#include <stdlib.h>
44#include <pthread.h>
45
46void test_loop(void) {
47  volatile int count = 1000000;
48
49  while (count--)
50    continue;
51}
52
53void *thfunc(void *arg) {
54  int forever = *(int *)arg;
55
56  do {
57    test_loop();
58  } while (forever);
59
60  return NULL;
61}
62
63int main(int argc, char *argv[]) {
64  pthread_t th;
65  int forever = 0;
66
67  if (argc > 1)
68    forever = atoi(argv[1]);
69
70  pthread_create(&th, NULL, thfunc, &forever);
71  test_loop();
72  pthread_join(th, NULL);
73
74  return 0;
75}
76EOF
77}
78
79test_per_thread() {
80  echo "Basic --per-thread mode test"
81  if ! perf record -o /dev/null --quiet ${testprog} 2> /dev/null
82  then
83    echo "Per-thread record [Skipped event not supported]"
84    return
85  fi
86  if ! perf record --per-thread -o "${perfdata}" ${testprog} 2> /dev/null
87  then
88    echo "Per-thread record [Failed record]"
89    err=1
90    return
91  fi
92  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
93  then
94    echo "Per-thread record [Failed missing output]"
95    err=1
96    return
97  fi
98
99  # run the test program in background (forever)
100  ${testprog} 1 &
101  TESTPID=$!
102
103  rm -f "${perfdata}"
104
105  wait_for_threads ${TESTPID} 2
106  perf record -p "${TESTPID}" --per-thread -o "${perfdata}" sleep 1 2> /dev/null
107  kill ${TESTPID}
108
109  if [ ! -e "${perfdata}" ]
110  then
111    echo "Per-thread record [Failed record -p]"
112    err=1
113    return
114  fi
115  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
116  then
117    echo "Per-thread record [Failed -p missing output]"
118    err=1
119    return
120  fi
121
122  echo "Basic --per-thread mode test [Success]"
123}
124
125test_register_capture() {
126  echo "Register capture test"
127  if ! perf list | grep -q 'br_inst_retired.near_call'
128  then
129    echo "Register capture test [Skipped missing event]"
130    return
131  fi
132  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'
133  then
134    echo "Register capture test [Skipped missing registers]"
135    return
136  fi
137  if ! perf record -o - --intr-regs=di,r8,dx,cx -e br_inst_retired.near_call:p \
138    -c 1000 --per-thread ${testprog} 2> /dev/null \
139    | perf script -F ip,sym,iregs -i - 2> /dev/null \
140    | grep -q "DI:"
141  then
142    echo "Register capture test [Failed missing output]"
143    err=1
144    return
145  fi
146  echo "Register capture test [Success]"
147}
148
149test_system_wide() {
150  echo "Basic --system-wide mode test"
151  if ! perf record -aB --synth=no -o "${perfdata}" ${testprog} 2> /dev/null
152  then
153    echo "System-wide record [Skipped not supported]"
154    return
155  fi
156  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
157  then
158    echo "System-wide record [Failed missing output]"
159    err=1
160    return
161  fi
162  if ! perf record -aB --synth=no -e cpu-clock,cs --threads=cpu \
163    -o "${perfdata}" ${testprog} 2> /dev/null
164  then
165    echo "System-wide record [Failed record --threads option]"
166    err=1
167    return
168  fi
169  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
170  then
171    echo "System-wide record [Failed --threads missing output]"
172    err=1
173    return
174  fi
175  echo "Basic --system-wide mode test [Success]"
176}
177
178test_workload() {
179  echo "Basic target workload test"
180  if ! perf record -o "${perfdata}" ${testprog} 2> /dev/null
181  then
182    echo "Workload record [Failed record]"
183    err=1
184    return
185  fi
186  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
187  then
188    echo "Workload record [Failed missing output]"
189    err=1
190    return
191  fi
192  if ! perf record -e cpu-clock,cs --threads=package \
193    -o "${perfdata}" ${testprog} 2> /dev/null
194  then
195    echo "Workload record [Failed record --threads option]"
196    err=1
197    return
198  fi
199  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
200  then
201    echo "Workload record [Failed --threads missing output]"
202    err=1
203    return
204  fi
205  echo "Basic target workload test [Success]"
206}
207
208build_test_program
209
210test_per_thread
211test_register_capture
212test_system_wide
213test_workload
214
215cleanup
216exit $err
217