xref: /linux/tools/perf/tests/shell/script_perl.sh (revision 2273697781d27c6ac033cdca7b5f5f5ad12e28f9)
1*22736977SIan Rogers#!/bin/bash
2*22736977SIan Rogers# perf script perl tests
3*22736977SIan Rogers# SPDX-License-Identifier: GPL-2.0
4*22736977SIan Rogers
5*22736977SIan Rogersset -e
6*22736977SIan Rogers
7*22736977SIan Rogers# set PERF_EXEC_PATH to find scripts in the source directory
8*22736977SIan Rogersperfdir=$(dirname "$0")/../..
9*22736977SIan Rogersif [ -e "$perfdir/scripts/perl/Perf-Trace-Util" ]; then
10*22736977SIan Rogers  export PERF_EXEC_PATH=$perfdir
11*22736977SIan Rogersfi
12*22736977SIan Rogers
13*22736977SIan Rogers
14*22736977SIan Rogersperfdata=$(mktemp /tmp/__perf_test_script_perl.perf.data.XXXXX)
15*22736977SIan Rogersgenerated_script=$(mktemp /tmp/__perf_test_script.XXXXX.pl)
16*22736977SIan Rogers
17*22736977SIan Rogerscleanup() {
18*22736977SIan Rogers  rm -f "${perfdata}"
19*22736977SIan Rogers  rm -f "${generated_script}"
20*22736977SIan Rogers  trap - EXIT TERM INT
21*22736977SIan Rogers}
22*22736977SIan Rogers
23*22736977SIan Rogerstrap_cleanup() {
24*22736977SIan Rogers  echo "Unexpected signal in ${FUNCNAME[1]}"
25*22736977SIan Rogers  cleanup
26*22736977SIan Rogers  exit 1
27*22736977SIan Rogers}
28*22736977SIan Rogerstrap trap_cleanup TERM INT
29*22736977SIan Rogerstrap cleanup EXIT
30*22736977SIan Rogers
31*22736977SIan Rogerscheck_perl_support() {
32*22736977SIan Rogers	if perf check feature -q libperl; then
33*22736977SIan Rogers		return 0
34*22736977SIan Rogers	fi
35*22736977SIan Rogers	echo "perf script perl test [Skipped: no libperl support]"
36*22736977SIan Rogers	return 2
37*22736977SIan Rogers}
38*22736977SIan Rogers
39*22736977SIan Rogerstest_script() {
40*22736977SIan Rogers	local event_name=$1
41*22736977SIan Rogers	local expected_output=$2
42*22736977SIan Rogers	local record_opts=$3
43*22736977SIan Rogers
44*22736977SIan Rogers	echo "Testing event: $event_name"
45*22736977SIan Rogers
46*22736977SIan Rogers	# Try to record. If this fails, it might be permissions or lack of support.
47*22736977SIan Rogers	# We return 2 to indicate "skip this event" rather than "fail test".
48*22736977SIan Rogers	if ! perf record -o "${perfdata}" -e "$event_name" $record_opts -- perf test -w thloop > /dev/null 2>&1; then
49*22736977SIan Rogers		echo "perf script perl test [Skipped: failed to record $event_name]"
50*22736977SIan Rogers		return 2
51*22736977SIan Rogers	fi
52*22736977SIan Rogers
53*22736977SIan Rogers	echo "Generating perl script..."
54*22736977SIan Rogers	if ! perf script -i "${perfdata}" -g "${generated_script}"; then
55*22736977SIan Rogers		echo "perf script perl test [Failed: script generation for $event_name]"
56*22736977SIan Rogers		return 1
57*22736977SIan Rogers	fi
58*22736977SIan Rogers
59*22736977SIan Rogers	if [ ! -f "${generated_script}" ]; then
60*22736977SIan Rogers		echo "perf script perl test [Failed: script not generated for $event_name]"
61*22736977SIan Rogers		return 1
62*22736977SIan Rogers	fi
63*22736977SIan Rogers
64*22736977SIan Rogers	echo "Executing perl script..."
65*22736977SIan Rogers	output=$(perf script -i "${perfdata}" -s "${generated_script}" 2>&1)
66*22736977SIan Rogers
67*22736977SIan Rogers	if echo "$output" | grep -q "$expected_output"; then
68*22736977SIan Rogers		echo "perf script perl test [Success: $event_name triggered $expected_output]"
69*22736977SIan Rogers		return 0
70*22736977SIan Rogers	else
71*22736977SIan Rogers		echo "perf script perl test [Failed: $event_name did not trigger $expected_output]"
72*22736977SIan Rogers		echo "Output was:"
73*22736977SIan Rogers		echo "$output" | head -n 20
74*22736977SIan Rogers		return 1
75*22736977SIan Rogers	fi
76*22736977SIan Rogers}
77*22736977SIan Rogers
78*22736977SIan Rogerscheck_perl_support || exit 2
79*22736977SIan Rogers
80*22736977SIan Rogers# Try tracepoint first
81*22736977SIan Rogerstest_script "sched:sched_switch" "sched::sched_switch" "-c 1" && res=0 || res=$?
82*22736977SIan Rogers
83*22736977SIan Rogersif [ $res -eq 0 ]; then
84*22736977SIan Rogers	exit 0
85*22736977SIan Rogerselif [ $res -eq 1 ]; then
86*22736977SIan Rogers	exit 1
87*22736977SIan Rogersfi
88*22736977SIan Rogers
89*22736977SIan Rogers# If tracepoint skipped (res=2), try task-clock
90*22736977SIan Rogers# For generic events like task-clock, the generated script uses process_event()
91*22736977SIan Rogers# which dumps data using Data::Dumper. We check for "$VAR1" which is standard Dumper output.
92*22736977SIan Rogerstest_script "task-clock" "\$VAR1" "-c 100" && res=0 || res=$?
93*22736977SIan Rogers
94*22736977SIan Rogersif [ $res -eq 0 ]; then
95*22736977SIan Rogers	exit 0
96*22736977SIan Rogerselif [ $res -eq 1 ]; then
97*22736977SIan Rogers	exit 1
98*22736977SIan Rogersfi
99*22736977SIan Rogers
100*22736977SIan Rogers# If both skipped
101*22736977SIan Rogersecho "perf script perl test [Skipped: Could not record tracepoint or task-clock]"
102*22736977SIan Rogersexit 2
103