[8/8] perf test: Add test case for the standard perf stat output

Message ID 20230607162700.3234712-9-kan.liang@linux.intel.com
State New
Headers
Series New metricgroup output in perf stat default mode |

Commit Message

Liang, Kan June 7, 2023, 4:27 p.m. UTC
  From: Kan Liang <kan.liang@linux.intel.com>

Add a new test case to verify the standard perf stat output with
different options.

Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
---
 tools/perf/tests/shell/stat+std_output.sh | 259 ++++++++++++++++++++++
 1 file changed, 259 insertions(+)
 create mode 100755 tools/perf/tests/shell/stat+std_output.sh
  

Comments

Ian Rogers June 13, 2023, 8:21 p.m. UTC | #1
On Wed, Jun 7, 2023 at 9:27 AM <kan.liang@linux.intel.com> wrote:
>
> From: Kan Liang <kan.liang@linux.intel.com>
>
> Add a new test case to verify the standard perf stat output with
> different options.
>
> Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
> ---
>  tools/perf/tests/shell/stat+std_output.sh | 259 ++++++++++++++++++++++
>  1 file changed, 259 insertions(+)
>  create mode 100755 tools/perf/tests/shell/stat+std_output.sh
>
> diff --git a/tools/perf/tests/shell/stat+std_output.sh b/tools/perf/tests/shell/stat+std_output.sh
> new file mode 100755
> index 000000000000..b9db0f245450
> --- /dev/null
> +++ b/tools/perf/tests/shell/stat+std_output.sh
> @@ -0,0 +1,259 @@
> +#!/bin/bash
> +# perf stat STD output linter
> +# SPDX-License-Identifier: GPL-2.0
> +# Tests various perf stat STD output commands for
> +# default event and metricgroup
> +
> +set -e
> +
> +skip_test=0
> +
> +stat_output=$(mktemp /tmp/__perf_test.stat_output.std.XXXXX)
> +
> +event_name=(cpu-clock task-clock context-switches cpu-migrations page-faults cycles instructions branches branch-misses stalled-cycles-frontend stalled-cycles-backend)
> +event_metric=("CPUs utilized" "CPUs utilized" "/sec" "/sec" "/sec" "GHz" "insn per cycle" "/sec" "of all branches" "frontend cycles idle" "backend cycles idle")
> +
> +metricgroup_name=(TopdownL1 TopdownL2)
> +
> +cleanup() {
> +  rm -f "${stat_output}"
> +
> +  trap - EXIT TERM INT
> +}
> +
> +trap_cleanup() {
> +  cleanup
> +  exit 1
> +}
> +trap trap_cleanup EXIT TERM INT
> +
> +function commachecker()
> +{
> +       local -i cnt=0
> +       local prefix=1
> +
> +       case "$1"
> +       in "--interval")        prefix=2
> +       ;; "--per-thread")      prefix=2
> +       ;; "--system-wide-no-aggr")     prefix=2
> +       ;; "--per-core")        prefix=3
> +       ;; "--per-socket")      prefix=3
> +       ;; "--per-node")        prefix=3
> +       ;; "--per-die")         prefix=3
> +       ;; "--per-cache")       prefix=3
> +       esac
> +
> +       while read line
> +       do
> +               # Ignore initial "started on" comment.
> +               x=${line:0:1}
> +               [ "$x" = "#" ] && continue
> +               # Ignore initial blank line.
> +               [ "$line" = "" ] && continue
> +               # Ignore "Performance counter stats"
> +               x=${line:0:25}
> +               [ "$x" = "Performance counter stats" ] && continue
> +               # Ignore "seconds time elapsed" and break
> +               [[ "$line" == *"time elapsed"* ]] && break
> +
> +               main_body=$(echo $line | cut -d' ' -f$prefix-)
> +               x=${main_body%#*}
> +               # Check default metricgroup
> +               y=$(echo $x | tr -d ' ')
> +               [ "$y" = "" ] && continue
> +               for i in "${!metricgroup_name[@]}"; do
> +                       [[ "$y" == *"${metricgroup_name[$i]}"* ]] && break
> +               done
> +               [[ "$y" == *"${metricgroup_name[$i]}"* ]] && continue
> +
> +               # Check default event
> +               for i in "${!event_name[@]}"; do
> +                       [[ "$x" == *"${event_name[$i]}"* ]] && break
> +               done
> +
> +               [[ ! "$x" == *"${event_name[$i]}"* ]] && {
> +                       echo "Unknown event name in $line" 1>&2
> +                       exit 1;
> +               }
> +
> +               # Check event metric if it exists
> +               [[ ! "$main_body" == *"#"* ]] && continue
> +               [[ ! "$main_body" == *"${event_metric[$i]}"* ]] && {
> +                       echo "wrong event metric. expected ${event_metric[$i]} in $line" 1>&2
> +                       exit 1;
> +               }
> +       done < "${stat_output}"
> +       return 0
> +}
> +
> +# Return true if perf_event_paranoid is > $1 and not running as root.
> +function ParanoidAndNotRoot()
> +{
> +        [ $(id -u) != 0 ] && [ $(cat /proc/sys/kernel/perf_event_paranoid) -gt $1 ]
> +}
> +
> +check_no_args()
> +{
> +       echo -n "Checking STD output: no args "
> +       perf stat -o "${stat_output}" true
> +        commachecker --no-args
> +       echo "[Success]"
> +}
> +
> +check_system_wide()
> +{
> +       echo -n "Checking STD output: system wide "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat -a -o "${stat_output}" true
> +        commachecker --system-wide
> +       echo "[Success]"
> +}
> +
> +check_system_wide_no_aggr()
> +{
> +       echo -n "Checking STD output: system wide no aggregation "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat -A -a --no-merge -o "${stat_output}" true
> +        commachecker --system-wide-no-aggr
> +       echo "[Success]"
> +}
> +
> +check_interval()
> +{
> +       echo -n "Checking STD output: interval "
> +       perf stat -I 1000 -o "${stat_output}" true
> +        commachecker --interval
> +       echo "[Success]"
> +}
> +
> +
> +check_per_core()
> +{
> +       echo -n "Checking STD output: per core "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat --per-core -a -o "${stat_output}" true
> +        commachecker --per-core
> +       echo "[Success]"
> +}
> +
> +check_per_thread()
> +{
> +       echo -n "Checking STD output: per thread "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat --per-thread -a -o "${stat_output}" true
> +        commachecker --per-thread
> +       echo "[Success]"
> +}
> +
> +check_per_cache_instance()
> +{
> +       echo -n "Checking STD output: per cache instance "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat  --per-cache -a true 2>&1 | commachecker --per-cache
> +       echo "[Success]"
> +}
> +
> +check_per_die()
> +{
> +       echo -n "Checking STD output: per die "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat --per-die -a -o "${stat_output}" true
> +        commachecker --per-die
> +       echo "[Success]"
> +}
> +
> +check_per_node()
> +{
> +       echo -n "Checking STD output: per node "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat --per-node -a -o "${stat_output}" true
> +        commachecker --per-node
> +       echo "[Success]"
> +}
> +
> +check_per_socket()
> +{
> +       echo -n "Checking STD output: per socket "
> +       if ParanoidAndNotRoot 0
> +       then
> +               echo "[Skip] paranoid and not root"
> +               return
> +       fi
> +       perf stat --per-socket -a -o "${stat_output}" true
> +        commachecker --per-socket
> +       echo "[Success]"
> +}
> +
> +# The perf stat options for per-socket, per-core, per-die
> +# and -A ( no_aggr mode ) uses the info fetched from this
> +# directory: "/sys/devices/system/cpu/cpu*/topology". For
> +# example, socket value is fetched from "physical_package_id"
> +# file in topology directory.
> +# Reference: cpu__get_topology_int in util/cpumap.c
> +# If the platform doesn't expose topology information, values
> +# will be set to -1. For example, incase of pSeries platform
> +# of powerpc, value for  "physical_package_id" is restricted
> +# and set to -1. Check here validates the socket-id read from
> +# topology file before proceeding further
> +
> +FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"
> +FILE_NAME="physical_package_id"
> +
> +check_for_topology()
> +{
> +       if ! ParanoidAndNotRoot 0
> +       then
> +               socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`
> +               [ -z $socket_file ] && return 0
> +               socket_id=`cat $socket_file`
> +               [ $socket_id == -1 ] && skip_test=1
> +               return 0
> +       fi
> +}

Tests, great! This logic is taken from
tools/perf/tests/shell/stat+csv_output.sh, could we share the
implementation between that and here by moving the code into something
in the lib directory?

Thanks,
Ian

> +
> +check_for_topology
> +check_no_args
> +check_system_wide
> +check_interval
> +check_per_thread
> +check_per_node
> +if [ $skip_test -ne 1 ]
> +then
> +       check_system_wide_no_aggr
> +       check_per_core
> +       check_per_cache_instance
> +       check_per_die
> +       check_per_socket
> +else
> +       echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
> +fi
> +cleanup
> +exit 0
> --
> 2.35.1
>
  

Patch

diff --git a/tools/perf/tests/shell/stat+std_output.sh b/tools/perf/tests/shell/stat+std_output.sh
new file mode 100755
index 000000000000..b9db0f245450
--- /dev/null
+++ b/tools/perf/tests/shell/stat+std_output.sh
@@ -0,0 +1,259 @@ 
+#!/bin/bash
+# perf stat STD output linter
+# SPDX-License-Identifier: GPL-2.0
+# Tests various perf stat STD output commands for
+# default event and metricgroup
+
+set -e
+
+skip_test=0
+
+stat_output=$(mktemp /tmp/__perf_test.stat_output.std.XXXXX)
+
+event_name=(cpu-clock task-clock context-switches cpu-migrations page-faults cycles instructions branches branch-misses stalled-cycles-frontend stalled-cycles-backend)
+event_metric=("CPUs utilized" "CPUs utilized" "/sec" "/sec" "/sec" "GHz" "insn per cycle" "/sec" "of all branches" "frontend cycles idle" "backend cycles idle")
+
+metricgroup_name=(TopdownL1 TopdownL2)
+
+cleanup() {
+  rm -f "${stat_output}"
+
+  trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+  cleanup
+  exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+function commachecker()
+{
+	local -i cnt=0
+	local prefix=1
+
+	case "$1"
+	in "--interval")	prefix=2
+	;; "--per-thread")	prefix=2
+	;; "--system-wide-no-aggr")	prefix=2
+	;; "--per-core")	prefix=3
+	;; "--per-socket")	prefix=3
+	;; "--per-node")	prefix=3
+	;; "--per-die")		prefix=3
+	;; "--per-cache")	prefix=3
+	esac
+
+	while read line
+	do
+		# Ignore initial "started on" comment.
+		x=${line:0:1}
+		[ "$x" = "#" ] && continue
+		# Ignore initial blank line.
+		[ "$line" = "" ] && continue
+		# Ignore "Performance counter stats"
+		x=${line:0:25}
+		[ "$x" = "Performance counter stats" ] && continue
+		# Ignore "seconds time elapsed" and break
+		[[ "$line" == *"time elapsed"* ]] && break
+
+		main_body=$(echo $line | cut -d' ' -f$prefix-)
+		x=${main_body%#*}
+		# Check default metricgroup
+		y=$(echo $x | tr -d ' ')
+		[ "$y" = "" ] && continue
+		for i in "${!metricgroup_name[@]}"; do
+			[[ "$y" == *"${metricgroup_name[$i]}"* ]] && break
+		done
+		[[ "$y" == *"${metricgroup_name[$i]}"* ]] && continue
+
+		# Check default event
+		for i in "${!event_name[@]}"; do
+			[[ "$x" == *"${event_name[$i]}"* ]] && break
+		done
+
+		[[ ! "$x" == *"${event_name[$i]}"* ]] && {
+			echo "Unknown event name in $line" 1>&2
+			exit 1;
+		}
+
+		# Check event metric if it exists
+		[[ ! "$main_body" == *"#"* ]] && continue
+		[[ ! "$main_body" == *"${event_metric[$i]}"* ]] && {
+			echo "wrong event metric. expected ${event_metric[$i]} in $line" 1>&2
+			exit 1;
+		}
+	done < "${stat_output}"
+	return 0
+}
+
+# Return true if perf_event_paranoid is > $1 and not running as root.
+function ParanoidAndNotRoot()
+{
+	 [ $(id -u) != 0 ] && [ $(cat /proc/sys/kernel/perf_event_paranoid) -gt $1 ]
+}
+
+check_no_args()
+{
+	echo -n "Checking STD output: no args "
+	perf stat -o "${stat_output}" true
+        commachecker --no-args
+	echo "[Success]"
+}
+
+check_system_wide()
+{
+	echo -n "Checking STD output: system wide "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat -a -o "${stat_output}" true
+        commachecker --system-wide
+	echo "[Success]"
+}
+
+check_system_wide_no_aggr()
+{
+	echo -n "Checking STD output: system wide no aggregation "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat -A -a --no-merge -o "${stat_output}" true
+        commachecker --system-wide-no-aggr
+	echo "[Success]"
+}
+
+check_interval()
+{
+	echo -n "Checking STD output: interval "
+	perf stat -I 1000 -o "${stat_output}" true
+        commachecker --interval
+	echo "[Success]"
+}
+
+
+check_per_core()
+{
+	echo -n "Checking STD output: per core "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat --per-core -a -o "${stat_output}" true
+        commachecker --per-core
+	echo "[Success]"
+}
+
+check_per_thread()
+{
+	echo -n "Checking STD output: per thread "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat --per-thread -a -o "${stat_output}" true
+        commachecker --per-thread
+	echo "[Success]"
+}
+
+check_per_cache_instance()
+{
+	echo -n "Checking STD output: per cache instance "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat  --per-cache -a true 2>&1 | commachecker --per-cache
+	echo "[Success]"
+}
+
+check_per_die()
+{
+	echo -n "Checking STD output: per die "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat --per-die -a -o "${stat_output}" true
+        commachecker --per-die
+	echo "[Success]"
+}
+
+check_per_node()
+{
+	echo -n "Checking STD output: per node "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat --per-node -a -o "${stat_output}" true
+        commachecker --per-node
+	echo "[Success]"
+}
+
+check_per_socket()
+{
+	echo -n "Checking STD output: per socket "
+	if ParanoidAndNotRoot 0
+	then
+		echo "[Skip] paranoid and not root"
+		return
+	fi
+	perf stat --per-socket -a -o "${stat_output}" true
+        commachecker --per-socket
+	echo "[Success]"
+}
+
+# The perf stat options for per-socket, per-core, per-die
+# and -A ( no_aggr mode ) uses the info fetched from this
+# directory: "/sys/devices/system/cpu/cpu*/topology". For
+# example, socket value is fetched from "physical_package_id"
+# file in topology directory.
+# Reference: cpu__get_topology_int in util/cpumap.c
+# If the platform doesn't expose topology information, values
+# will be set to -1. For example, incase of pSeries platform
+# of powerpc, value for  "physical_package_id" is restricted
+# and set to -1. Check here validates the socket-id read from
+# topology file before proceeding further
+
+FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"
+FILE_NAME="physical_package_id"
+
+check_for_topology()
+{
+	if ! ParanoidAndNotRoot 0
+	then
+		socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`
+		[ -z $socket_file ] && return 0
+		socket_id=`cat $socket_file`
+		[ $socket_id == -1 ] && skip_test=1
+		return 0
+	fi
+}
+
+check_for_topology
+check_no_args
+check_system_wide
+check_interval
+check_per_thread
+check_per_node
+if [ $skip_test -ne 1 ]
+then
+	check_system_wide_no_aggr
+	check_per_core
+	check_per_cache_instance
+	check_per_die
+	check_per_socket
+else
+	echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
+fi
+cleanup
+exit 0