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