Skip to contentSkip to conversation

perf trace: Add more tests for BTF-augmented perf trace

PATCH Superseded Howard Chu 2 participants 5 messages Dec 11, 2024 – Dec 15, 2024 perf events

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Howard Chu

author
#0 lore raw

v3 cover perf trace: Add more tests for BTF-augmented perf trace

Changes in v3:
- Add vmlinux BTF check, and skip the tests if it doesn't exist

v1, v2:

The previous version of the perf trace BTF general augmentation tests didn't pass Shellcheck (thanks to Arnaldo Carvalho de Melo <[email protected]> for pointing this out), this version uses bash instead of POSIX shell to pass Shellcheck.

This patch series also adds documentation for the new option --force-btf, which is used in the tests.

Howard Chu (2):
perf trace: Add tests for BTF general augmentation perf docs: Add documentation for --force-btf option

 tools/perf/Documentation/perf-trace.txt     |  5 ++
 tools/perf/tests/shell/trace_btf_general.sh | 93 +++++++++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100755 tools/perf/tests/shell/trace_btf_general.sh
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/2 perf trace: Add tests for BTF general augmentation

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 <[email protected]>

---
 tools/perf/tests/shell/trace_btf_general.sh | 93 +++++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100755 tools/perf/tests/shell/trace_btf_general.sh
tools/perf/tests/shell/trace_btf_general.sh new file +93 -0
new file mode 100755
index 000000000000..bef07bad42bb
@@ -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 ! ls /sys/kernel/btf/vmlinux 1>/dev/null 2>&1
+  then
+    echo "Skipped due to missing vmlinux BTF"
+    err=2
+  fi
+}
+
+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
+
+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
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH v3 1/2] perf trace: Add tests for BTF general augmentation

Hi Howard,

Quoted 30 lines · Howard Chu

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 <[email protected]>

---
 tools/perf/tests/shell/trace_btf_general.sh | 93 +++++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100755 tools/perf/tests/shell/trace_btf_general.sh
tools/perf/tests/shell/trace_btf_general.sh new file +10 -0
new file mode 100755
index 000000000000..bef07bad42bb
@@ -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)

tools/perf/tests/shell/trace_btf_general.sh line 20

Quoted 10 lines
tools/perf/tests/shell/trace_btf_general.sh new file +10 -0
+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 ! ls /sys/kernel/btf/vmlinux 1>/dev/null 2>&1

Normally we do

if [ ! -f /sys/kernel/btf/vmlinux ]

tools/perf/tests/shell/trace_btf_general.sh line 23

Quoted 3 lines
tools/perf/tests/shell/trace_btf_general.sh new file +3 -0
+  then
+    echo "Skipped due to missing vmlinux BTF"
+    err=2

This can be overwritten by trace_test_string.

tools/perf/tests/shell/trace_btf_general.sh line 30

Quoted 7 lines
tools/perf/tests/shell/trace_btf_general.sh new file +7 -0
+  fi
+}
+
+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]+$"

Does this work without BTF support?

Thanks
Namhyung

Quoted 66 lines
tools/perf/tests/shell/trace_btf_general.sh new file +63 -0
+  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
+
+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
Signature · 1 line
2.43.0
author
#3 lore raw

Re: [PATCH v3 1/2] perf trace: Add tests for BTF general augmentation

Hello Namhyung,

Quoted 47 lines · Namhyung Kim

Hi Howard,

Quoted 30 lines · Howard Chu

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 <[email protected]>

---
 tools/perf/tests/shell/trace_btf_general.sh | 93 +++++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100755 tools/perf/tests/shell/trace_btf_general.sh
tools/perf/tests/shell/trace_btf_general.sh new file +10 -0
new file mode 100755
index 000000000000..bef07bad42bb
@@ -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)

tools/perf/tests/shell/trace_btf_general.sh line 20

Quoted 10 lines
tools/perf/tests/shell/trace_btf_general.sh new file +10 -0
+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 ! ls /sys/kernel/btf/vmlinux 1>/dev/null 2>&1

Normally we do

Quoted 1 line

if [ ! -f /sys/kernel/btf/vmlinux ]

Sure, I'll change the check to this.

Quoted 6 lines

tools/perf/tests/shell/trace_btf_general.sh line 23

Quoted 4 lines
tools/perf/tests/shell/trace_btf_general.sh new file +3 -0
+  then
+    echo "Skipped due to missing vmlinux BTF"
+    err=2

This can be overwritten by trace_test_string.

Oops my bad sorry.

Quoted 11 lines

tools/perf/tests/shell/trace_btf_general.sh line 30

Quoted 9 lines
tools/perf/tests/shell/trace_btf_general.sh new file +7 -0
+  fi
+}
+
+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]+$"

Does this work without BTF support?

Yes, this works without BTF with Arnaldo's patch adding sys_enter_renameat2. Before the patch, it wouldn't work because the second filename would not be displayed properly. I understand this may seem redundant, but now in perf trace all the data is collected through the BTF general collector (if BTF is present), so this trace_test_string() tests the new code path that uses BTF information to pretty-print string arguments, since all data collection goes through it.

Thanks,
Howard

author
#4 lore raw

PATCH v3 2/2 perf docs: Add documentation for --force-btf option

In reply to Howard Chu

The --force-btf option is intended for debugging purposes and is currently undocumented. Add documentation for it.

---
 tools/perf/Documentation/perf-trace.txt | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/Documentation/perf-trace.txt +5 -0
index 6e0cc50bbc13..fb3d2af33844 100644
@@ -241,6 +241,11 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs.
 	printing using the existing 'perf trace' syscall arg beautifiers to map integer
 	arguments to strings (pid to comm, syscall id to syscall name, etc).
 
+--force-btf::
+	Use btf_dump to pretty print syscall argument data, instead of using hand-crafted pretty
+	printers. This option is intended for testing BTF integration in perf trace. btf_dump-based
+	pretty-printing serves as a fallback to hand-crafted pretty printers, as the latter can
+	better pretty-print integer flags and struct pointers.
 
 PAGEFAULTS
 ----------
Signature · 1 line
2.43.0