12c0cb9f5SSong Liu#!/bin/sh
22c0cb9f5SSong Liu# perf stat --bpf-counters test
32c0cb9f5SSong Liu# SPDX-License-Identifier: GPL-2.0
42c0cb9f5SSong Liu
52c0cb9f5SSong Liuset -e
62c0cb9f5SSong Liu
72c0cb9f5SSong Liu# check whether $2 is within +/- 10% of $1
82c0cb9f5SSong Liucompare_number()
92c0cb9f5SSong Liu{
102c0cb9f5SSong Liu       first_num=$1
112c0cb9f5SSong Liu       second_num=$2
122c0cb9f5SSong Liu
132c0cb9f5SSong Liu       # upper bound is first_num * 110%
14482698c2SIan Rogers       upper=$(expr $first_num + $first_num / 10 )
152c0cb9f5SSong Liu       # lower bound is first_num * 90%
16482698c2SIan Rogers       lower=$(expr $first_num - $first_num / 10 )
172c0cb9f5SSong Liu
182c0cb9f5SSong Liu       if [ $second_num -gt $upper ] || [ $second_num -lt $lower ]; then
192c0cb9f5SSong Liu               echo "The difference between $first_num and $second_num are greater than 10%."
202c0cb9f5SSong Liu               exit 1
212c0cb9f5SSong Liu       fi
222c0cb9f5SSong Liu}
232c0cb9f5SSong Liu
242c0cb9f5SSong Liu# skip if --bpf-counters is not supported
25*68ca249cSNamhyung Kimif ! perf stat -e cycles --bpf-counters true > /dev/null 2>&1; then
26c8b94764SJames Clark	if [ "$1" = "-v" ]; then
272638fbd3SIan Rogers		echo "Skipping: --bpf-counters not supported"
28*68ca249cSNamhyung Kim		perf --no-pager stat -e cycles --bpf-counters true || true
292638fbd3SIan Rogers	fi
302638fbd3SIan Rogers	exit 2
312638fbd3SIan Rogersfi
322c0cb9f5SSong Liu
332c0cb9f5SSong Liubase_cycles=$(perf stat --no-big-num -e cycles -- perf bench sched messaging -g 1 -l 100 -t 2>&1 | awk '/cycles/ {print $1}')
34d10eedd8SAthira Rajeevif [ "$base_cycles" = "<not" ]; then
35a49ed2b4SIan Rogers	echo "Skipping: cycles event not counted"
36a49ed2b4SIan Rogers	exit 2
37a49ed2b4SIan Rogersfi
382c0cb9f5SSong Liubpf_cycles=$(perf stat --no-big-num --bpf-counters -e cycles -- perf bench sched messaging -g 1 -l 100 -t 2>&1 | awk '/cycles/ {print $1}')
39d10eedd8SAthira Rajeevif [ "$bpf_cycles" = "<not" ]; then
40a49ed2b4SIan Rogers	echo "Failed: cycles not counted with --bpf-counters"
41a49ed2b4SIan Rogers	exit 1
42a49ed2b4SIan Rogersfi
432c0cb9f5SSong Liu
442c0cb9f5SSong Liucompare_number $base_cycles $bpf_cycles
452c0cb9f5SSong Liuexit 0
46