| 1 | |
| 2 | |
| 3 | |
| 4 |
|
| 5 | set -e
|
| 6 |
|
| 7 | |
| 8 | CPU=$(taskset -c -p $$ | awk -F': ' '{print $2}' | awk -F'[,-]' '{print $1}')
|
| 9 | if [ -z "$CPU" ]; then
|
| 10 | CPU=0
|
| 11 | fi
|
| 12 | workload=(taskset -c "$CPU" awk 'BEGIN { for (i=0; i<10000000; i++) sum+=i }')
|
| 13 |
|
| 14 | |
| 15 | compare_number()
|
| 16 | {
|
| 17 | first_num=$1
|
| 18 | second_num=$2
|
| 19 |
|
| 20 | |
| 21 | upper=$(expr $first_num + $first_num / 20 \* 3 )
|
| 22 | |
| 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 |
|
| 31 | check_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 |
|
| 46 | test_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 |
|
| 63 | test_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 | |
| 83 | if ! 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
|
| 89 | fi
|
| 90 |
|
| 91 | test_bpf_counters
|
| 92 | test_bpf_modifier
|
| 93 |
|
| 94 | exit 0
|