]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf trace: Add tests for BTF general augmentation
authorHoward Chu <howardchu95@gmail.com>
Sun, 15 Dec 2024 19:07:10 +0000 (11:07 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 26 Dec 2024 15:18:11 +0000 (12:18 -0300)
Currently, we only have 'perf trace' augmentation tests for enum
arguments. This patch adds tests for more general syscall arguments,
such as struct pointers, strings, and buffers.

These tests utilize the 'perf config' system to configure 'the perf trace'
output, as suggested by Arnaldo Carvalho de Melo <acme@kernel.org>.

Committer testing:

  root@number:~# perf test "BTF general"
  109: perf trace BTF general tests                                    : Ok
  root@number:~# perf test -v "BTF general"
  109: perf trace BTF general tests                                    : Ok
  root@number:~# perf test -vv "BTF general"
  109: perf trace BTF general tests:
  --- start ---
  test child forked, pid 1410451
  Checking if vmlinux BTF exists
  Testing perf trace's string augmentation
  Testing perf trace's buffer augmentation
  Testing perf trace's struct augmentation
  ---- end(0) ----
  109: perf trace BTF general tests                                    : Ok
  root@number:~#

It still fails sometimes, for instance when tested with:

  root@number:~# perf stat --null -r 10 perf test "BTF general"
  109: perf trace BTF general tests                                    : Ok
  109: perf trace BTF general tests                                    : Ok
  109: perf trace BTF general tests                                    : Ok
  109: perf trace BTF general tests                                    : Ok
  109: perf trace BTF general tests                                    : FAILED!
  109: perf trace BTF general tests                                    : Ok
  109: perf trace BTF general tests                                    : Ok
  109: perf trace BTF general tests                                    : FAILED!
  109: perf trace BTF general tests                                    : Ok
  109: perf trace BTF general tests                                    : Ok

   Performance counter stats for 'perf test BTF general' (10 runs):

               2.148 +- 0.293 seconds time elapsed  ( +- 13.63% )

  root@number:~#

But we can go on from here and fix things up with followup patches.

Suggested-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Signed-off-by: Howard Chu <howardchu95@gmail.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Link: https://lore.kernel.org/r/20241215190712.787847-2-howardchu95@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/shell/trace_btf_general.sh [new file with mode: 0755]

diff --git a/tools/perf/tests/shell/trace_btf_general.sh b/tools/perf/tests/shell/trace_btf_general.sh
new file mode 100755 (executable)
index 0000000..005c93e
--- /dev/null
@@ -0,0 +1,93 @@
+#!/bin/bash
+# perf trace BTF general tests
+# SPDX-License-Identifier: GPL-2.0
+
+err=0
+set -e
+
+. "$(dirname $0)"/lib/probe.sh
+
+file1=$(mktemp /tmp/file1_XXXX)
+file2=$(echo $file1 | sed 's/file1/file2/g')
+
+buffer="buffer content"
+perf_config_tmp=$(mktemp /tmp/.perfconfig_XXXXX)
+
+trap cleanup EXIT TERM INT HUP
+
+check_vmlinux() {
+  echo "Checking if vmlinux BTF exists"
+  if [ ! -f /sys/kernel/btf/vmlinux ]
+  then
+    echo "Skipped due to missing vmlinux BTF"
+    return 2
+  fi
+  return 0
+}
+
+trace_test_string() {
+  echo "Testing perf trace's string augmentation"
+  if ! perf trace -e renameat* --max-events=1 -- mv ${file1} ${file2} 2>&1 | \
+    grep -q -E "^mv/[0-9]+ renameat(2)?\(.*, \"${file1}\", .*, \"${file2}\", .*\) += +[0-9]+$"
+  then
+    echo "String augmentation test failed"
+    err=1
+  fi
+}
+
+trace_test_buffer() {
+  echo "Testing perf trace's buffer augmentation"
+  # echo will insert a newline (\10) at the end of the buffer
+  if ! perf trace -e write --max-events=1 -- echo "${buffer}" 2>&1 | \
+    grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.*, [0-9]+\) += +[0-9]+$"
+  then
+    echo "Buffer augmentation test failed"
+    err=1
+  fi
+}
+
+trace_test_struct_btf() {
+  echo "Testing perf trace's struct augmentation"
+  if ! perf trace -e clock_nanosleep --force-btf --max-events=1 -- sleep 1 2>&1 | \
+    grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \{1,\}, 0x[0-9a-f]+\) += +[0-9]+$"
+  then
+    echo "BTF struct augmentation test failed"
+    err=1
+  fi
+}
+
+cleanup() {
+  rm -rf ${file1} ${file2} ${perf_config_tmp}
+}
+
+trap_cleanup() {
+  echo "Unexpected signal in ${FUNCNAME[1]}"
+  cleanup
+  exit 1
+}
+
+# don't overwrite user's perf config
+trace_config() {
+  export PERF_CONFIG=${perf_config_tmp}
+  perf config trace.show_arg_names=false trace.show_duration=false \
+    trace.show_timestamp=false trace.args_alignment=0
+}
+
+skip_if_no_perf_trace || exit 2
+check_vmlinux || exit 2
+
+trace_config
+
+trace_test_string
+
+if [ $err = 0 ]; then
+  trace_test_buffer
+fi
+
+if [ $err = 0 ]; then
+  trace_test_struct_btf
+fi
+
+cleanup
+
+exit $err