selftests/ftrace: Add kernel cmdline tracing testcases

Add test cases for kernel command-line tracing options. It verifies
various parameters like trace_buf_size and trace_options by reading
the output of tracing files in sysfs.

Link: https://lore.kernel.org/all/178649543741.438282.2638887907664217934.stgit@devnote2/

Assisted-by: LLM
Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
This commit is contained in:
Masami Hiramatsu (Google)
2026-10-03 09:27:54 +09:00
parent 50cc9e8fe8
commit 8dd296e4ec
12 changed files with 146 additions and 0 deletions
@@ -0,0 +1 @@
ftrace=function
@@ -0,0 +1 @@
trace_event=sched:sched_switch,kmem:kmalloc
@@ -0,0 +1 @@
trace_buf_size=2048K
@@ -0,0 +1 @@
trace_options=sym-addr,verbose
@@ -0,0 +1 @@
trace_clock=global
@@ -0,0 +1 @@
trace_instance=bar,sched:sched_switch
@@ -0,0 +1,19 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2026, Google LLC.
# Check ftrace= kernel command-line tracer setting
TRACEDIR="/sys/kernel/tracing"
if [ ! -f "$TRACEDIR/current_tracer" ]; then
echo "FAIL: current_tracer does not exist"
exit 1
fi
read -r TRACER _ < "$TRACEDIR/current_tracer"
if [ "$TRACER" != "function" ]; then
echo "FAIL: current_tracer is '$TRACER', expected 'function'"
exit 1
fi
echo "PASS: cmdline-01-ftrace"
exit 0
@@ -0,0 +1,26 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2026, Google LLC.
# Check trace_event= kernel command-line setting
TRACEDIR="/sys/kernel/tracing"
if [ ! -d "$TRACEDIR/events/sched/sched_switch" ]; then
echo "FAIL: event sched:sched_switch does not exist"
exit 1
fi
if [ ! -d "$TRACEDIR/events/kmem/kmalloc" ]; then
echo "FAIL: event kmem:kmalloc does not exist"
exit 1
fi
ENABLE1=$(cat "$TRACEDIR/events/sched/sched_switch/enable")
ENABLE2=$(cat "$TRACEDIR/events/kmem/kmalloc/enable")
if [ "$ENABLE1" != "1" ] || [ "$ENABLE2" != "1" ]; then
echo "FAIL: events not enabled (sched_switch=$ENABLE1, kmalloc=$ENABLE2)"
exit 1
fi
echo "PASS: cmdline-02-trace-event"
exit 0
@@ -0,0 +1,29 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2026, Google LLC.
# APPLETS: awk
# Check trace_buf_size= kernel command-line setting
TRACEDIR="/sys/kernel/tracing"
if [ ! -f "$TRACEDIR/buffer_size_kb" ]; then
echo "FAIL: buffer_size_kb does not exist"
exit 1
fi
BUF_RAW=$(cat "$TRACEDIR/buffer_size_kb")
case "$BUF_RAW" in
*"expanded:"*)
BUFSIZE=$(echo "$BUF_RAW" | sed -n 's/.*expanded: *\([0-9]*\).*/\1/p')
;;
*)
BUFSIZE=$(echo "$BUF_RAW" | awk '{print $1}')
;;
esac
if [ -z "$BUFSIZE" ] || [ "$BUFSIZE" -lt 2048 ]; then
echo "FAIL: buffer_size_kb is '$BUF_RAW', expected >= 2048"
exit 1
fi
echo "PASS: cmdline-03-trace-buf-size"
exit 0
@@ -0,0 +1,23 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2026, Google LLC.
# Check trace_options= kernel command-line setting
TRACEDIR="/sys/kernel/tracing"
if [ ! -f "$TRACEDIR/trace_options" ]; then
echo "FAIL: trace_options file does not exist"
exit 1
fi
if ! grep -qw "sym-addr" "$TRACEDIR/trace_options"; then
echo "FAIL: sym-addr option is not set in trace_options"
exit 1
fi
if ! grep -qw "verbose" "$TRACEDIR/trace_options"; then
echo "FAIL: verbose option is not set in trace_options"
exit 1
fi
echo "PASS: cmdline-04-trace-options"
exit 0
@@ -0,0 +1,19 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2026, Google LLC.
# Check trace_clock= kernel command-line setting
TRACEDIR="/sys/kernel/tracing"
if [ ! -f "$TRACEDIR/trace_clock" ]; then
echo "FAIL: trace_clock file does not exist"
exit 1
fi
if ! grep -q '\[global\]' "$TRACEDIR/trace_clock"; then
CLOCK=$(cat "$TRACEDIR/trace_clock")
echo "FAIL: trace_clock is not set to global ($CLOCK)"
exit 1
fi
echo "PASS: cmdline-05-trace-clock"
exit 0
@@ -0,0 +1,24 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2026, Google LLC.
# Check trace_instance= kernel command-line setting
TRACEDIR="/sys/kernel/tracing"
if [ ! -d "$TRACEDIR/instances/bar" ]; then
echo "FAIL: trace instance bar does not exist"
exit 1
fi
if [ ! -d "$TRACEDIR/instances/bar/events/sched/sched_switch" ]; then
echo "FAIL: event sched_switch does not exist in instance bar"
exit 1
fi
ENABLE=$(cat "$TRACEDIR/instances/bar/events/sched/sched_switch/enable")
if [ "$ENABLE" != "1" ]; then
echo "FAIL: event sched_switch is not enabled in instance bar ($ENABLE)"
exit 1
fi
echo "PASS: cmdline-06-trace-instance"
exit 0