xref: /openbmc/linux/tools/perf/tests/shell/record.sh (revision 4321ad4ee98b7325d6133e1d5b7fa25bcbdeb57e)
1#!/bin/sh
2# perf record tests
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7err=0
8perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
9testprog=$(mktemp /tmp/__perf_test.prog.XXXXXX)
10testsym="test_loop"
11
12cleanup() {
13  rm -f "${perfdata}"
14  rm -f "${perfdata}".old
15
16  if [ "${testprog}" != "true" ]; then
17    rm -f "${testprog}"
18  fi
19
20  trap - EXIT TERM INT
21}
22
23trap_cleanup() {
24  cleanup
25  exit 1
26}
27trap trap_cleanup EXIT TERM INT
28
29build_test_program() {
30  if ! [ -x "$(command -v cc)" ]; then
31    # No CC found. Fall back to 'true'
32    testprog=true
33    testsym=true
34    return
35  fi
36
37  echo "Build a test program"
38  cat <<EOF | cc -o ${testprog} -xc - -pthread
39#include <stdio.h>
40#include <stdlib.h>
41#include <pthread.h>
42
43void test_loop(void) {
44  volatile int count = 1000000;
45
46  while (count--)
47    continue;
48}
49
50void *thfunc(void *arg) {
51  int forever = *(int *)arg;
52
53  do {
54    test_loop();
55  } while (forever);
56
57  return NULL;
58}
59
60int main(int argc, char *argv[]) {
61  pthread_t th;
62  int forever = 0;
63
64  if (argc > 1)
65    forever = atoi(argv[1]);
66
67  pthread_create(&th, NULL, thfunc, &forever);
68  test_loop();
69  pthread_join(th, NULL);
70
71  return 0;
72}
73EOF
74}
75
76test_per_thread() {
77  echo "Basic --per-thread mode test"
78  if ! perf record -o /dev/null --quiet ${testprog} 2> /dev/null
79  then
80    echo "Per-thread record [Skipped event not supported]"
81    if [ $err -ne 1 ]
82    then
83      err=2
84    fi
85    return
86  fi
87  if ! perf record --per-thread -o "${perfdata}" ${testprog} 2> /dev/null
88  then
89    echo "Per-thread record [Failed record]"
90    err=1
91    return
92  fi
93  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
94  then
95    echo "Per-thread record [Failed missing output]"
96    err=1
97    return
98  fi
99  echo "Basic --per-thread mode test [Success]"
100}
101
102test_register_capture() {
103  echo "Register capture test"
104  if ! perf list | grep -q 'br_inst_retired.near_call'
105  then
106    echo "Register capture test [Skipped missing event]"
107    if [ $err -ne 1 ]
108    then
109      err=2
110    fi
111    return
112  fi
113  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'
114  then
115    echo "Register capture test [Skipped missing registers]"
116    return
117  fi
118  if ! perf record -o - --intr-regs=di,r8,dx,cx -e br_inst_retired.near_call:p \
119    -c 1000 --per-thread ${testprog} 2> /dev/null \
120    | perf script -F ip,sym,iregs -i - 2> /dev/null \
121    | grep -q "DI:"
122  then
123    echo "Register capture test [Failed missing output]"
124    err=1
125    return
126  fi
127  echo "Register capture test [Success]"
128}
129
130build_test_program
131
132test_per_thread
133test_register_capture
134
135cleanup
136exit $err
137