1#!/bin/bash
2# test Intel TPEBS counting mode (exclusive)
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7ParanoidAndNotRoot() {
8  [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]
9}
10
11if ! grep -q GenuineIntel /proc/cpuinfo
12then
13  echo "Skipping non-Intel"
14  exit 2
15fi
16
17if ParanoidAndNotRoot 0
18then
19  echo "Skipping paranoid >0 and not root"
20  exit 2
21fi
22
23stat_output=$(mktemp /tmp/__perf_stat_tpebs_output.XXXXX)
24
25cleanup() {
26  rm -rf "${stat_output}"
27  trap - EXIT TERM INT
28}
29
30trap_cleanup() {
31  echo "Unexpected signal in ${FUNCNAME[1]}"
32  cat "${stat_output}"
33  cleanup
34  exit 1
35}
36trap trap_cleanup EXIT TERM INT
37
38# Event to be used in tests
39event=cache-misses
40
41if ! perf record -e "${event}:p" -a -o /dev/null sleep 0.01 > "${stat_output}" 2>&1
42then
43  echo "Missing ${event} support"
44  cleanup
45  exit 2
46fi
47
48test_with_record_tpebs() {
49  echo "Testing with --record-tpebs"
50  if ! perf stat -e "${event}:R" --record-tpebs -a sleep 0.01 > "${stat_output}" 2>&1
51  then
52    echo "Testing with --record-tpebs [Failed perf stat]"
53    cat "${stat_output}"
54    exit 1
55  fi
56
57  # Expected output:
58  # $ perf stat --record-tpebs -e cache-misses:R -a sleep 0.01
59  # Events enabled
60  # [ perf record: Woken up 2 times to write data ]
61  # [ perf record: Captured and wrote 0.056 MB - ]
62  #
63  #  Performance counter stats for 'system wide':
64  #
65  #                  0      cache-misses:R
66  #
67  #        0.013963299 seconds time elapsed
68  if ! grep "perf record" "${stat_output}"
69  then
70    echo "Testing with --record-tpebs [Failed missing perf record]"
71    cat "${stat_output}"
72    exit 1
73  fi
74  if ! grep "${event}:R" "${stat_output}" && ! grep "/${event}/R" "${stat_output}"
75  then
76    echo "Testing with --record-tpebs [Failed missing event name]"
77    cat "${stat_output}"
78    exit 1
79  fi
80  echo "Testing with --record-tpebs [Success]"
81}
82
83test_with_record_tpebs
84cleanup
85exit 0
86