2025-06-23 10:00:12 +01:00
|
|
|
#!/bin/bash
|
2024-10-25 12:21:04 -07:00
|
|
|
# perf stat --bpf-counters test (exclusive)
|
2021-03-23 17:42:47 -03:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2024-10-21 11:02:01 +00:00
|
|
|
workload="perf test -w sqrtloop"
|
2024-04-16 10:00:14 -07:00
|
|
|
|
2024-01-08 09:40:09 +01:00
|
|
|
# check whether $2 is within +/- 20% of $1
|
2021-03-23 17:42:47 -03:00
|
|
|
compare_number()
|
|
|
|
{
|
2024-04-16 10:00:14 -07:00
|
|
|
first_num=$1
|
|
|
|
second_num=$2
|
|
|
|
|
|
|
|
# upper bound is first_num * 120%
|
|
|
|
upper=$(expr $first_num + $first_num / 5 )
|
|
|
|
# lower bound is first_num * 80%
|
|
|
|
lower=$(expr $first_num - $first_num / 5 )
|
|
|
|
|
|
|
|
if [ $second_num -gt $upper ] || [ $second_num -lt $lower ]; then
|
|
|
|
echo "The difference between $first_num and $second_num are greater than 20%."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_counts()
|
|
|
|
{
|
2024-06-25 11:20:01 +02:00
|
|
|
base_instructions=$1
|
|
|
|
bpf_instructions=$2
|
2024-04-16 10:00:14 -07:00
|
|
|
|
2024-06-25 11:20:01 +02:00
|
|
|
if [ "$base_instructions" = "<not" ]; then
|
|
|
|
echo "Skipping: instructions event not counted"
|
2024-04-16 10:00:14 -07:00
|
|
|
exit 2
|
|
|
|
fi
|
2024-06-25 11:20:01 +02:00
|
|
|
if [ "$bpf_instructions" = "<not" ]; then
|
|
|
|
echo "Failed: instructions not counted with --bpf-counters"
|
2024-04-16 10:00:14 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_bpf_counters()
|
|
|
|
{
|
|
|
|
printf "Testing --bpf-counters "
|
2024-06-25 11:20:01 +02:00
|
|
|
base_instructions=$(perf stat --no-big-num -e instructions -- $workload 2>&1 | awk '/instructions/ {print $1}')
|
|
|
|
bpf_instructions=$(perf stat --no-big-num --bpf-counters -e instructions -- $workload 2>&1 | awk '/instructions/ {print $1}')
|
|
|
|
check_counts $base_instructions $bpf_instructions
|
|
|
|
compare_number $base_instructions $bpf_instructions
|
2024-04-16 10:00:14 -07:00
|
|
|
echo "[Success]"
|
|
|
|
}
|
|
|
|
|
|
|
|
test_bpf_modifier()
|
|
|
|
{
|
|
|
|
printf "Testing bpf event modifier "
|
2024-06-25 11:20:01 +02:00
|
|
|
stat_output=$(perf stat --no-big-num -e instructions/name=base_instructions/,instructions/name=bpf_instructions/b -- $workload 2>&1)
|
|
|
|
base_instructions=$(echo "$stat_output"| awk '/base_instructions/ {print $1}')
|
|
|
|
bpf_instructions=$(echo "$stat_output"| awk '/bpf_instructions/ {print $1}')
|
|
|
|
check_counts $base_instructions $bpf_instructions
|
|
|
|
compare_number $base_instructions $bpf_instructions
|
2024-04-16 10:00:14 -07:00
|
|
|
echo "[Success]"
|
2021-03-23 17:42:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
# skip if --bpf-counters is not supported
|
2024-06-25 11:20:01 +02:00
|
|
|
if ! perf stat -e instructions --bpf-counters true > /dev/null 2>&1; then
|
2021-10-28 14:48:25 +01:00
|
|
|
if [ "$1" = "-v" ]; then
|
2021-06-17 11:42:15 -07:00
|
|
|
echo "Skipping: --bpf-counters not supported"
|
2024-06-25 11:20:01 +02:00
|
|
|
perf --no-pager stat -e instructions --bpf-counters true || true
|
2021-06-17 11:42:15 -07:00
|
|
|
fi
|
|
|
|
exit 2
|
|
|
|
fi
|
2021-03-23 17:42:47 -03:00
|
|
|
|
2024-04-16 10:00:14 -07:00
|
|
|
test_bpf_counters
|
|
|
|
test_bpf_modifier
|
2021-03-23 17:42:47 -03:00
|
|
|
|
|
|
|
exit 0
|