61d13c4c64b8d724545447a882e1b628d014cc75
[linux-2.6-microblaze.git] / tools / perf / tests / shell / daemon.sh
1 #!/bin/sh
2 # daemon operations
3 # SPDX-License-Identifier: GPL-2.0
4
5 check_line_first()
6 {
7         local line=$1
8         local name=$2
9         local base=$3
10         local output=$4
11         local lock=$5
12         local up=$6
13
14         local line_name=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $2 }'`
15         local line_base=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $3 }'`
16         local line_output=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $4 }'`
17         local line_lock=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $5 }'`
18         local line_up=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $6 }'`
19
20         if [ "${name}" != "${line_name}" ]; then
21                 echo "FAILED: wrong name"
22                 error=1
23         fi
24
25         if [ "${base}" != "${line_base}" ]; then
26                 echo "FAILED: wrong base"
27                 error=1
28         fi
29
30         if [ "${output}" != "${line_output}" ]; then
31                 echo "FAILED: wrong output"
32                 error=1
33         fi
34
35         if [ "${lock}" != "${line_lock}" ]; then
36                 echo "FAILED: wrong lock"
37                 error=1
38         fi
39
40         if [ "${up}" != "${line_up}" ]; then
41                 echo "FAILED: wrong up"
42                 error=1
43         fi
44 }
45
46 check_line_other()
47 {
48         local line=$1
49         local name=$2
50         local run=$3
51         local base=$4
52         local output=$5
53         local control=$6
54         local ack=$7
55         local up=$8
56
57         local line_name=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $2 }'`
58         local line_run=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $3 }'`
59         local line_base=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $4 }'`
60         local line_output=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $5 }'`
61         local line_control=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $6 }'`
62         local line_ack=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $7 }'`
63         local line_up=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $8 }'`
64
65         if [ "${name}" != "${line_name}" ]; then
66                 echo "FAILED: wrong name"
67                 error=1
68         fi
69
70         if [ "${run}" != "${line_run}" ]; then
71                 echo "FAILED: wrong run"
72                 error=1
73         fi
74
75         if [ "${base}" != "${line_base}" ]; then
76                 echo "FAILED: wrong base"
77                 error=1
78         fi
79
80         if [ "${output}" != "${line_output}" ]; then
81                 echo "FAILED: wrong output"
82                 error=1
83         fi
84
85         if [ "${control}" != "${line_control}" ]; then
86                 echo "FAILED: wrong control"
87                 error=1
88         fi
89
90         if [ "${ack}" != "${line_ack}" ]; then
91                 echo "FAILED: wrong ack"
92                 error=1
93         fi
94
95         if [ "${up}" != "${line_up}" ]; then
96                 echo "FAILED: wrong up"
97                 error=1
98         fi
99 }
100
101 daemon_exit()
102 {
103         local config=$1
104
105         local line=`perf daemon --config ${config} -x: | head -1`
106         local pid=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $1 }'`
107
108         # Reset trap handler.
109         trap - SIGINT SIGTERM
110
111         # stop daemon
112         perf daemon stop --config ${config}
113
114         # ... and wait for the pid to go away
115         tail --pid=${pid} -f /dev/null
116 }
117
118 daemon_start()
119 {
120         local config=$1
121         local session=$2
122
123         perf daemon start --config ${config}
124
125         # Clean up daemon if interrupted.
126         trap "echo 'FAILED: Signal caught'; daemon_exit ${config}; exit 1" SIGINT SIGTERM
127
128         # wait for the session to ping
129         local state="FAIL"
130         while [ "${state}" != "OK" ]; do
131                 state=`perf daemon ping --config ${config} --session ${session} | awk '{ print $1 }'`
132                 sleep 0.05
133         done
134 }
135
136 test_list()
137 {
138         echo "test daemon list"
139
140         local config=$(mktemp /tmp/perf.daemon.config.XXX)
141         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
142
143         cat <<EOF > ${config}
144 [daemon]
145 base=BASE
146
147 [session-size]
148 run = -e cpu-clock -m 1 sleep 10
149
150 [session-time]
151 run = -e task-clock -m 1 sleep 10
152 EOF
153
154         sed -i -e "s|BASE|${base}|" ${config}
155
156         # start daemon
157         daemon_start ${config} size
158
159         # check first line
160         # pid:daemon:base:base/output:base/lock
161         local line=`perf daemon --config ${config} -x: | head -1`
162         check_line_first ${line} daemon ${base} ${base}/output ${base}/lock "0"
163
164         # check 1st session
165         # pid:size:-e cpu-clock:base/size:base/size/output:base/size/control:base/size/ack:0
166         local line=`perf daemon --config ${config} -x: | head -2 | tail -1`
167         check_line_other "${line}" size "-e cpu-clock -m 1 sleep 10" ${base}/session-size \
168                          ${base}/session-size/output ${base}/session-size/control \
169                          ${base}/session-size/ack "0"
170
171         # check 2nd session
172         # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0
173         local line=`perf daemon --config ${config} -x: | head -3 | tail -1`
174         check_line_other "${line}" time "-e task-clock -m 1 sleep 10" ${base}/session-time \
175                          ${base}/session-time/output ${base}/session-time/control \
176                          ${base}/session-time/ack "0"
177
178         # stop daemon
179         daemon_exit ${config}
180
181         rm -rf ${base}
182         rm -f ${config}
183 }
184
185 test_reconfig()
186 {
187         echo "test daemon reconfig"
188
189         local config=$(mktemp /tmp/perf.daemon.config.XXX)
190         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
191
192         # prepare config
193         cat <<EOF > ${config}
194 [daemon]
195 base=BASE
196
197 [session-size]
198 run = -e cpu-clock -m 1 sleep 10
199
200 [session-time]
201 run = -e task-clock -m 1 sleep 10
202 EOF
203
204         sed -i -e "s|BASE|${base}|" ${config}
205
206         # start daemon
207         daemon_start ${config} size
208
209         # check 2nd session
210         # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0
211         local line=`perf daemon --config ${config} -x: | head -3 | tail -1`
212         check_line_other "${line}" time "-e task-clock -m 1 sleep 10" ${base}/session-time \
213                          ${base}/session-time/output ${base}/session-time/control ${base}/session-time/ack "0"
214         local pid=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $1 }'`
215
216         # prepare new config
217         local config_new=${config}.new
218         cat <<EOF > ${config_new}
219 [daemon]
220 base=BASE
221
222 [session-size]
223 run = -e cpu-clock -m 1 sleep 10
224
225 [session-time]
226 run = -e cpu-clock -m 1 sleep 10
227 EOF
228
229         # TEST 1 - change config
230
231         sed -i -e "s|BASE|${base}|" ${config_new}
232         cp ${config_new} ${config}
233
234         # wait for old session to finish
235         tail --pid=${pid} -f /dev/null
236
237         # wait for new one to start
238         local state="FAIL"
239         while [ "${state}" != "OK" ]; do
240                 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
241         done
242
243         # check reconfigured 2nd session
244         # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0
245         local line=`perf daemon --config ${config} -x: | head -3 | tail -1`
246         check_line_other "${line}" time "-e cpu-clock -m 1 sleep 10" ${base}/session-time \
247                          ${base}/session-time/output ${base}/session-time/control ${base}/session-time/ack "0"
248
249         # TEST 2 - empty config
250
251         local config_empty=${config}.empty
252         cat <<EOF > ${config_empty}
253 [daemon]
254 base=BASE
255 EOF
256
257         # change config
258         sed -i -e "s|BASE|${base}|" ${config_empty}
259         cp ${config_empty} ${config}
260
261         # wait for sessions to finish
262         local state="OK"
263         while [ "${state}" != "FAIL" ]; do
264                 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
265         done
266
267         local state="OK"
268         while [ "${state}" != "FAIL" ]; do
269                 state=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`
270         done
271
272         local one=`perf daemon --config ${config} -x: | wc -l`
273
274         if [ ${one} -ne "1" ]; then
275                 echo "FAILED: wrong list output"
276                 error=1
277         fi
278
279         # TEST 3 - config again
280
281         cp ${config_new} ${config}
282
283         # wait for size to start
284         local state="FAIL"
285         while [ "${state}" != "OK" ]; do
286                 state=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`
287         done
288
289         # wait for time to start
290         local state="FAIL"
291         while [ "${state}" != "OK" ]; do
292                 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
293         done
294
295         # stop daemon
296         daemon_exit ${config}
297
298         rm -rf ${base}
299         rm -f ${config}
300         rm -f ${config_new}
301         rm -f ${config_empty}
302 }
303
304 test_stop()
305 {
306         echo "test daemon stop"
307
308         local config=$(mktemp /tmp/perf.daemon.config.XXX)
309         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
310
311         # prepare config
312         cat <<EOF > ${config}
313 [daemon]
314 base=BASE
315
316 [session-size]
317 run = -e cpu-clock -m 1 sleep 10
318
319 [session-time]
320 run = -e task-clock -m 1 sleep 10
321 EOF
322
323         sed -i -e "s|BASE|${base}|" ${config}
324
325         # start daemon
326         daemon_start ${config} size
327
328         local pid_size=`perf daemon --config ${config} -x: | head -2 | tail -1 | awk 'BEGIN { FS = ":" } ; { print $1 }'`
329         local pid_time=`perf daemon --config ${config} -x: | head -3 | tail -1 | awk 'BEGIN { FS = ":" } ; { print $1 }'`
330
331         # check that sessions are running
332         if [ ! -d "/proc/${pid_size}" ]; then
333                 echo "FAILED: session size not up"
334         fi
335
336         if [ ! -d "/proc/${pid_time}" ]; then
337                 echo "FAILED: session time not up"
338         fi
339
340         # stop daemon
341         daemon_exit ${config}
342
343         # check that sessions are gone
344         if [ -d "/proc/${pid_size}" ]; then
345                 echo "FAILED: session size still up"
346         fi
347
348         if [ -d "/proc/${pid_time}" ]; then
349                 echo "FAILED: session time still up"
350         fi
351
352         rm -rf ${base}
353         rm -f ${config}
354 }
355
356 test_signal()
357 {
358         echo "test daemon signal"
359
360         local config=$(mktemp /tmp/perf.daemon.config.XXX)
361         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
362
363         # prepare config
364         cat <<EOF > ${config}
365 [daemon]
366 base=BASE
367
368 [session-test]
369 run = -e cpu-clock --switch-output -m 1 sleep 10
370 EOF
371
372         sed -i -e "s|BASE|${base}|" ${config}
373
374         # start daemon
375         daemon_start ${config} test
376
377         # send 2 signals
378         perf daemon signal --config ${config} --session test
379         perf daemon signal --config ${config}
380
381         # stop daemon
382         daemon_exit ${config}
383
384         # count is 2 perf.data for signals and 1 for perf record finished
385         count=`ls ${base}/session-test/ | grep perf.data | wc -l`
386         if [ ${count} -ne 3 ]; then
387                 error=1
388                 echo "FAILED: perf data no generated"
389         fi
390
391         rm -rf ${base}
392         rm -f ${config}
393 }
394
395 test_ping()
396 {
397         echo "test daemon ping"
398
399         local config=$(mktemp /tmp/perf.daemon.config.XXX)
400         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
401
402         # prepare config
403         cat <<EOF > ${config}
404 [daemon]
405 base=BASE
406
407 [session-size]
408 run = -e cpu-clock -m 1 sleep 10
409
410 [session-time]
411 run = -e task-clock -m 1 sleep 10
412 EOF
413
414         sed -i -e "s|BASE|${base}|" ${config}
415
416         # start daemon
417         daemon_start ${config} size
418
419         size=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`
420         type=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
421
422         if [ ${size} != "OK" -o ${type} != "OK" ]; then
423                 error=1
424                 echo "FAILED: daemon ping failed"
425         fi
426
427         # stop daemon
428         daemon_exit ${config}
429
430         rm -rf ${base}
431         rm -f ${config}
432 }
433
434 test_lock()
435 {
436         echo "test daemon lock"
437
438         local config=$(mktemp /tmp/perf.daemon.config.XXX)
439         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
440
441         # prepare config
442         cat <<EOF > ${config}
443 [daemon]
444 base=BASE
445
446 [session-size]
447 run = -e cpu-clock -m 1 sleep 10
448 EOF
449
450         sed -i -e "s|BASE|${base}|" ${config}
451
452         # start daemon
453         daemon_start ${config} size
454
455         # start second daemon over the same config/base
456         failed=`perf daemon start --config ${config} 2>&1 | awk '{ print $1 }'`
457
458         # check that we failed properly
459         if [ ${failed} != "failed:" ]; then
460                 error=1
461                 echo "FAILED: daemon lock failed"
462         fi
463
464         # stop daemon
465         daemon_exit ${config}
466
467         rm -rf ${base}
468         rm -f ${config}
469 }
470
471 error=0
472
473 test_list
474 test_reconfig
475 test_stop
476 test_signal
477 test_ping
478 test_lock
479
480 exit ${error}