1#!/bin/sh
2# perf pipe recording and injection test
3# SPDX-License-Identifier: GPL-2.0
4
5# skip if there's no compiler
6if ! [ -x "$(command -v cc)" ]; then
7	echo "failed: no compiler, install gcc"
8	exit 2
9fi
10
11file=$(mktemp /tmp/test.file.XXXXXX)
12data=$(mktemp /tmp/perf.data.XXXXXX)
13
14cat <<EOF | cc -o ${file} -x c -
15#include <signal.h>
16#include <stdlib.h>
17#include <unistd.h>
18
19volatile int done;
20
21void sigalrm(int sig) {
22	done = 1;
23}
24
25__attribute__((noinline)) void noploop(void) {
26	while (!done)
27		continue;
28}
29
30int main(int argc, char *argv[]) {
31	int sec = 1;
32
33	if (argc > 1)
34		sec = atoi(argv[1]);
35
36	signal(SIGALRM, sigalrm);
37	alarm(sec);
38
39	noploop();
40	return 0;
41}
42EOF
43
44
45if ! perf record -e task-clock:u -o - ${file} | perf report -i - --task | grep test.file; then
46	echo "cannot find the test file in the perf report"
47	exit 1
48fi
49
50if ! perf record -e task-clock:u -o - ${file} | perf inject -b | perf report -i - | grep noploop; then
51	echo "cannot find noploop function in pipe #1"
52	exit 1
53fi
54
55perf record -e task-clock:u -o - ${file} | perf inject -b -o ${data}
56if ! perf report -i ${data} | grep noploop; then
57	echo "cannot find noploop function in pipe #2"
58	exit 1
59fi
60
61perf record -e task-clock:u -o ${data} ${file}
62if ! perf inject -b -i ${data} | perf report -i - | grep noploop; then
63	echo "cannot find noploop function in pipe #3"
64	exit 1
65fi
66
67
68rm -f ${file} ${data} ${data}.old
69exit 0
70