1#!/bin/sh
2# perf stat metrics (shadow stat) test
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7# skip if system-wide mode is forbidden
8perf stat -a true > /dev/null 2>&1 || exit 2
9
10test_global_aggr()
11{
12	perf stat -a --no-big-num -e cycles,instructions sleep 1  2>&1 | \
13	grep -e cycles -e instructions | \
14	while read num evt hash ipc rest
15	do
16		# skip not counted events
17		if [ "$num" = "<not" ]; then
18			continue
19		fi
20
21		# save cycles count
22		if [ "$evt" = "cycles" ]; then
23			cyc=$num
24			continue
25		fi
26
27		# skip if no cycles
28		if [ -z "$cyc" ]; then
29			continue
30		fi
31
32		# use printf for rounding and a leading zero
33		res=`printf "%.2f" $(echo "scale=6; $num / $cyc" | bc -q)`
34		if [ "$ipc" != "$res" ]; then
35			echo "IPC is different: $res != $ipc  ($num / $cyc)"
36			exit 1
37		fi
38	done
39}
40
41test_no_aggr()
42{
43	perf stat -a -A --no-big-num -e cycles,instructions sleep 1  2>&1 | \
44	grep ^CPU | \
45	while read cpu num evt hash ipc rest
46	do
47		# skip not counted events
48		if [ "$num" = "<not" ]; then
49			continue
50		fi
51
52		# save cycles count
53		if [ "$evt" = "cycles" ]; then
54			results="$results $cpu:$num"
55			continue
56		fi
57
58		cyc=${results##* $cpu:}
59		cyc=${cyc%% *}
60
61		# skip if no cycles
62		if [ -z "$cyc" ]; then
63			continue
64		fi
65
66		# use printf for rounding and a leading zero
67		res=`printf "%.2f" $(echo "scale=6; $num / $cyc" | bc -q)`
68		if [ "$ipc" != "$res" ]; then
69			echo "IPC is different for $cpu: $res != $ipc  ($num / $cyc)"
70			exit 1
71		fi
72	done
73}
74
75test_global_aggr
76test_no_aggr
77
78exit 0
79