projects/linux·blob master · refs

94 lines · 2.7 KiB

1#!/bin/bash
2# perf stat --bpf-counters test (exclusive)
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7# Get the first allowed CPU
8CPU=$(taskset -c -p $$ | awk -F': ' '{print $2}' | awk -F'[,-]' '{print $1}')
9if [ -z "$CPU" ]; then
10 CPU=0
11fi
12workload=(taskset -c "$CPU" awk 'BEGIN { for (i=0; i<10000000; i++) sum+=i }')
13
14# check whether $2 is within +/- 15% of $1
15compare_number()
16{
17 first_num=$1
18 second_num=$2
19
20 # upper bound is first_num * 115%
21 upper=$(expr $first_num + $first_num / 20 \* 3 )
22 # lower bound is first_num * 85%
23 lower=$(expr $first_num - $first_num / 20 \* 3 )
24
25 if [ $second_num -gt $upper ] || [ $second_num -lt $lower ]; then
26 echo "The difference between $first_num and $second_num are greater than 15%."
27 exit 1
28 fi
29}
30
31check_counts()
32{
33 base_instructions=$1
34 bpf_instructions=$2
35
36 if [ "$base_instructions" = "<not" ]; then
37 echo "Skipping: instructions event not counted"
38 exit 2
39 fi
40 if [ "$bpf_instructions" = "<not" ]; then
41 echo "Failed: instructions not counted with --bpf-counters"
42 exit 1
43 fi
44}
45
46test_bpf_counters()
47{
48 printf "Testing --bpf-counters "
49 base_instructions=$(perf stat --no-big-num -e instructions:u -- "${workload[@]}" 2>&1 | \
50 awk -v i=0 -v c=0 '/instructions/ { \
51 if ($1 != "<not") { i++; c += $1 } \
52 } END { if (i > 0) printf "%.0f", c; else print "<not" }')
53 bpf_instructions=$(perf stat --no-big-num --bpf-counters -e instructions:u \
54 -- "${workload[@]}" 2>&1 | \
55 awk -v i=0 -v c=0 '/instructions/ { \
56 if ($1 != "<not") { i++; c += $1 } \
57 } END { if (i > 0) printf "%.0f", c; else print "<not" }')
58 check_counts $base_instructions $bpf_instructions
59 compare_number $base_instructions $bpf_instructions
60 echo "[Success]"
61}
62
63test_bpf_modifier()
64{
65 printf "Testing bpf event modifier "
66 stat_output=$(perf stat --no-big-num \
67 -e instructions/name=base_instructions/u,instructions/name=bpf_instructions/bu \
68 -- "${workload[@]}" 2>&1)
69 base_instructions=$(echo "$stat_output"| \
70 awk -v i=0 -v c=0 '/base_instructions/ { \
71 if ($1 != "<not") { i++; c += $1 } \
72 } END { if (i > 0) printf "%.0f", c; else print "<not" }')
73 bpf_instructions=$(echo "$stat_output"| \
74 awk -v i=0 -v c=0 '/bpf_instructions/ { \
75 if ($1 != "<not") { i++; c += $1 } \
76 } END { if (i > 0) printf "%.0f", c; else print "<not" }')
77 check_counts $base_instructions $bpf_instructions
78 compare_number $base_instructions $bpf_instructions
79 echo "[Success]"
80}
81
82# skip if --bpf-counters is not supported
83if ! perf stat -e instructions --bpf-counters true > /dev/null 2>&1; then
84 if [ "$1" = "-v" ]; then
85 echo "Skipping: --bpf-counters not supported"
86 perf --no-pager stat -e instructions --bpf-counters true || true
87 fi
88 exit 2
89fi
90
91test_bpf_counters
92test_bpf_modifier
93
94exit 0