1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# description: Test creation and deletion of trace instances while setting an event
4
5if [ ! -d instances ] ; then
6    echo "no instance directory with this kernel"
7    exit_unsupported;
8fi
9
10fail() { # mesg
11    rmdir foo 2>/dev/null
12    echo $1
13    set -e
14    exit_fail
15}
16
17cd instances
18
19# we don't want to fail on error
20set +e
21
22mkdir x
23rmdir x
24result=$?
25
26if [ $result -ne 0 ]; then
27    echo "instance rmdir not supported"
28    exit_unsupported
29fi
30
31instance_slam() {
32        while :; do
33                mkdir foo 2> /dev/null
34                rmdir foo 2> /dev/null
35        done
36}
37
38instance_read() {
39        while :; do
40                cat foo/trace 1> /dev/null 2>&1
41        done
42}
43
44instance_set() {
45        while :; do
46                echo 1 > foo/events/sched/sched_switch
47        done 2> /dev/null
48}
49
50instance_slam &
51p1=$!
52echo $p1
53
54instance_set &
55p2=$!
56echo $p2
57
58instance_read &
59p3=$!
60echo $p3
61
62sleep 1
63
64kill -1 $p3
65kill -1 $p2
66kill -1 $p1
67
68echo "Wait for processes to finish"
69wait $p1 $p2 $p3
70echo "all processes finished, wait for cleanup"
71sleep 1
72
73mkdir foo
74ls foo > /dev/null
75rmdir foo
76if [ -d foo ]; then
77        fail "foo still exists"
78fi
79
80mkdir foo
81echo "schedule:enable_event:sched:sched_switch" > foo/set_ftrace_filter
82rmdir foo
83if [ -d foo ]; then
84        fail "foo still exists"
85fi
86if grep -q "schedule:enable_event:sched:sched_switch" ../set_ftrace_filter; then
87	echo "Older kernel detected. Cleanup filter"
88	echo '!schedule:enable_event:sched:sched_switch' > ../set_ftrace_filter
89fi
90
91instance_slam() {
92    while :; do
93	mkdir x
94	mkdir y
95	mkdir z
96	rmdir x
97	rmdir y
98	rmdir z
99    done 2>/dev/null
100}
101
102instance_slam &
103p1=$!
104echo $p1
105
106instance_slam &
107p2=$!
108echo $p2
109
110instance_slam &
111p3=$!
112echo $p3
113
114instance_slam &
115p4=$!
116echo $p4
117
118instance_slam &
119p5=$!
120echo $p5
121
122ls -lR >/dev/null
123sleep 1
124
125kill -1 $p1
126kill -1 $p2
127kill -1 $p3
128kill -1 $p4
129kill -1 $p5
130
131echo "Wait for processes to finish"
132wait $p1 $p2 $p3 $p4 $p5
133echo "all processes finished, wait for cleanup"
134
135mkdir x y z
136ls x y z
137rmdir x y z
138for d in x y z; do
139        if [ -d $d ]; then
140                fail "instance $d still exists"
141        fi
142done
143
144set -e
145
146exit 0
147