1#!/bin/sh
2# perf record offcpu profiling tests
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7err=0
8perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
9
10cleanup() {
11  rm -f ${perfdata}
12  rm -f ${perfdata}.old
13  trap - exit term int
14}
15
16trap_cleanup() {
17  cleanup
18  exit 1
19}
20trap trap_cleanup exit term int
21
22test_offcpu_priv() {
23  echo "Checking off-cpu privilege"
24
25  if [ `id -u` != 0 ]
26  then
27    echo "off-cpu test [Skipped permission]"
28    err=2
29    return
30  fi
31  if perf record --off-cpu -o /dev/null --quiet true 2>&1 | grep BUILD_BPF_SKEL
32  then
33    echo "off-cpu test [Skipped missing BPF support]"
34    err=2
35    return
36  fi
37}
38
39test_offcpu_basic() {
40  echo "Basic off-cpu test"
41
42  if ! perf record --off-cpu -e dummy -o ${perfdata} sleep 1 2> /dev/null
43  then
44    echo "Basic off-cpu test [Failed record]"
45    err=1
46    return
47  fi
48  if ! perf evlist -i ${perfdata} | grep -q "offcpu-time"
49  then
50    echo "Basic off-cpu test [Failed no event]"
51    err=1
52    return
53  fi
54  if ! perf report -i ${perfdata} -q --percent-limit=90 | egrep -q sleep
55  then
56    echo "Basic off-cpu test [Failed missing output]"
57    err=1
58    return
59  fi
60  echo "Basic off-cpu test [Success]"
61}
62
63test_offcpu_child() {
64  echo "Child task off-cpu test"
65
66  # perf bench sched messaging creates 400 processes
67  if ! perf record --off-cpu -e dummy -o ${perfdata} -- \
68    perf bench sched messaging -g 10 > /dev/null 2&>1
69  then
70    echo "Child task off-cpu test [Failed record]"
71    err=1
72    return
73  fi
74  if ! perf evlist -i ${perfdata} | grep -q "offcpu-time"
75  then
76    echo "Child task off-cpu test [Failed no event]"
77    err=1
78    return
79  fi
80  # each process waits for read and write, so it should be more than 800 events
81  if ! perf report -i ${perfdata} -s comm -q -n -t ';' --percent-limit=90 | \
82    awk -F ";" '{ if (NF > 3 && int($3) < 800) exit 1; }'
83  then
84    echo "Child task off-cpu test [Failed invalid output]"
85    err=1
86    return
87  fi
88  echo "Child task off-cpu test [Success]"
89}
90
91
92test_offcpu_priv
93
94if [ $err = 0 ]; then
95  test_offcpu_basic
96fi
97
98if [ $err = 0 ]; then
99  test_offcpu_child
100fi
101
102cleanup
103exit $err
104