98cc3356a04a7e27ecf3ef363d07489ccdc29755
[platform/kernel/linux-starfive.git] / tools / perf / tests / shell / stat+std_output.sh
1 #!/bin/bash
2 # perf stat STD output linter
3 # SPDX-License-Identifier: GPL-2.0
4 # Tests various perf stat STD output commands for
5 # default event and metricgroup
6
7 set -e
8
9 . $(dirname $0)/lib/stat_output.sh
10
11 stat_output=$(mktemp /tmp/__perf_test.stat_output.std.XXXXX)
12
13 event_name=(cpu-clock task-clock context-switches cpu-migrations page-faults cycles instructions branches branch-misses stalled-cycles-frontend stalled-cycles-backend)
14 event_metric=("CPUs utilized" "CPUs utilized" "/sec" "/sec" "/sec" "GHz" "insn per cycle" "/sec" "of all branches" "frontend cycles idle" "backend cycles idle")
15
16 metricgroup_name=(TopdownL1 TopdownL2)
17
18 cleanup() {
19   rm -f "${stat_output}"
20
21   trap - EXIT TERM INT
22 }
23
24 trap_cleanup() {
25   cleanup
26   exit 1
27 }
28 trap trap_cleanup EXIT TERM INT
29
30 function commachecker()
31 {
32         local -i cnt=0
33         local prefix=1
34
35         case "$1"
36         in "--interval")        prefix=2
37         ;; "--per-thread")      prefix=2
38         ;; "--system-wide-no-aggr")     prefix=2
39         ;; "--per-core")        prefix=3
40         ;; "--per-socket")      prefix=3
41         ;; "--per-node")        prefix=3
42         ;; "--per-die")         prefix=3
43         ;; "--per-cache")       prefix=3
44         esac
45
46         while read line
47         do
48                 # Ignore initial "started on" comment.
49                 x=${line:0:1}
50                 [ "$x" = "#" ] && continue
51                 # Ignore initial blank line.
52                 [ "$line" = "" ] && continue
53                 # Ignore "Performance counter stats"
54                 x=${line:0:25}
55                 [ "$x" = "Performance counter stats" ] && continue
56                 # Ignore "seconds time elapsed" and break
57                 [[ "$line" == *"time elapsed"* ]] && break
58
59                 main_body=$(echo $line | cut -d' ' -f$prefix-)
60                 x=${main_body%#*}
61                 # Check default metricgroup
62                 y=$(echo $x | tr -d ' ')
63                 [ "$y" = "" ] && continue
64                 for i in "${!metricgroup_name[@]}"; do
65                         [[ "$y" == *"${metricgroup_name[$i]}"* ]] && break
66                 done
67                 [[ "$y" == *"${metricgroup_name[$i]}"* ]] && continue
68
69                 # Check default event
70                 for i in "${!event_name[@]}"; do
71                         [[ "$x" == *"${event_name[$i]}"* ]] && break
72                 done
73
74                 [[ ! "$x" == *"${event_name[$i]}"* ]] && {
75                         echo "Unknown event name in $line" 1>&2
76                         exit 1;
77                 }
78
79                 # Check event metric if it exists
80                 [[ ! "$main_body" == *"#"* ]] && continue
81                 [[ ! "$main_body" == *"${event_metric[$i]}"* ]] && {
82                         echo "wrong event metric. expected ${event_metric[$i]} in $line" 1>&2
83                         exit 1;
84                 }
85         done < "${stat_output}"
86         return 0
87 }
88
89 perf_cmd="-o ${stat_output}"
90
91 skip_test=$(check_for_topology)
92 check_no_args "STD" "$perf_cmd"
93 check_system_wide "STD" "$perf_cmd"
94 check_interval "STD" "$perf_cmd"
95 check_per_thread "STD" "$perf_cmd"
96 check_per_node "STD" "$perf_cmd"
97 if [ $skip_test -ne 1 ]
98 then
99         check_system_wide_no_aggr "STD" "$perf_cmd"
100         check_per_core "STD" "$perf_cmd"
101         check_per_cache_instance "STD" "$perf_cmd"
102         check_per_die "STD" "$perf_cmd"
103         check_per_socket "STD" "$perf_cmd"
104 else
105         echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
106 fi
107 cleanup
108 exit 0