Merge branch '00.00-inst' of git://github.com/skeggsb/linux into drm-fixes
[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_start()
102 {
103         local config=$1
104         local session=$2
105
106         perf daemon start --config ${config}
107
108         # wait for the session to ping
109         local state="FAIL"
110         while [ "${state}" != "OK" ]; do
111                 state=`perf daemon ping --config ${config} --session ${session} | awk '{ print $1 }'`
112                 sleep 0.05
113         done
114 }
115
116 daemon_exit()
117 {
118         local base=$1
119         local config=$2
120
121         local line=`perf daemon --config ${config} -x: | head -1`
122         local pid=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $1 }'`
123
124         # stop daemon
125         perf daemon stop --config ${config}
126
127         # ... and wait for the pid to go away
128         tail --pid=${pid} -f /dev/null
129 }
130
131 test_list()
132 {
133         echo "test daemon list"
134
135         local config=$(mktemp /tmp/perf.daemon.config.XXX)
136         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
137
138         cat <<EOF > ${config}
139 [daemon]
140 base=BASE
141
142 [session-size]
143 run = -e cpu-clock
144
145 [session-time]
146 run = -e task-clock
147 EOF
148
149         sed -i -e "s|BASE|${base}|" ${config}
150
151         # start daemon
152         daemon_start ${config} size
153
154         # check first line
155         # pid:daemon:base:base/output:base/lock
156         local line=`perf daemon --config ${config} -x: | head -1`
157         check_line_first ${line} daemon ${base} ${base}/output ${base}/lock "0"
158
159         # check 1st session
160         # pid:size:-e cpu-clock:base/size:base/size/output:base/size/control:base/size/ack:0
161         local line=`perf daemon --config ${config} -x: | head -2 | tail -1`
162         check_line_other "${line}" size "-e cpu-clock" ${base}/session-size \
163                          ${base}/session-size/output ${base}/session-size/control \
164                          ${base}/session-size/ack "0"
165
166         # check 2nd session
167         # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0
168         local line=`perf daemon --config ${config} -x: | head -3 | tail -1`
169         check_line_other "${line}" time "-e task-clock" ${base}/session-time \
170                          ${base}/session-time/output ${base}/session-time/control \
171                          ${base}/session-time/ack "0"
172
173         # stop daemon
174         daemon_exit ${base} ${config}
175
176         rm -rf ${base}
177         rm -f ${config}
178 }
179
180 test_reconfig()
181 {
182         echo "test daemon reconfig"
183
184         local config=$(mktemp /tmp/perf.daemon.config.XXX)
185         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
186
187         # prepare config
188         cat <<EOF > ${config}
189 [daemon]
190 base=BASE
191
192 [session-size]
193 run = -e cpu-clock
194
195 [session-time]
196 run = -e task-clock
197 EOF
198
199         sed -i -e "s|BASE|${base}|" ${config}
200
201         # start daemon
202         daemon_start ${config} size
203
204         # check 2nd session
205         # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0
206         local line=`perf daemon --config ${config} -x: | head -3 | tail -1`
207         check_line_other "${line}" time "-e task-clock" ${base}/session-time \
208                          ${base}/session-time/output ${base}/session-time/control ${base}/session-time/ack "0"
209         local pid=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $1 }'`
210
211         # prepare new config
212         local config_new=${config}.new
213         cat <<EOF > ${config_new}
214 [daemon]
215 base=BASE
216
217 [session-size]
218 run = -e cpu-clock
219
220 [session-time]
221 run = -e cpu-clock
222 EOF
223
224         # TEST 1 - change config
225
226         sed -i -e "s|BASE|${base}|" ${config_new}
227         cp ${config_new} ${config}
228
229         # wait for old session to finish
230         tail --pid=${pid} -f /dev/null
231
232         # wait for new one to start
233         local state="FAIL"
234         while [ "${state}" != "OK" ]; do
235                 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
236         done
237
238         # check reconfigured 2nd session
239         # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0
240         local line=`perf daemon --config ${config} -x: | head -3 | tail -1`
241         check_line_other "${line}" time "-e cpu-clock" ${base}/session-time \
242                          ${base}/session-time/output ${base}/session-time/control ${base}/session-time/ack "0"
243
244         # TEST 2 - empty config
245
246         local config_empty=${config}.empty
247         cat <<EOF > ${config_empty}
248 [daemon]
249 base=BASE
250 EOF
251
252         # change config
253         sed -i -e "s|BASE|${base}|" ${config_empty}
254         cp ${config_empty} ${config}
255
256         # wait for sessions to finish
257         local state="OK"
258         while [ "${state}" != "FAIL" ]; do
259                 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
260         done
261
262         local state="OK"
263         while [ "${state}" != "FAIL" ]; do
264                 state=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`
265         done
266
267         local one=`perf daemon --config ${config} -x: | wc -l`
268
269         if [ ${one} -ne "1" ]; then
270                 echo "FAILED: wrong list output"
271                 error=1
272         fi
273
274         # TEST 3 - config again
275
276         cp ${config_new} ${config}
277
278         # wait for size to start
279         local state="FAIL"
280         while [ "${state}" != "OK" ]; do
281                 state=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`
282         done
283
284         # wait for time to start
285         local state="FAIL"
286         while [ "${state}" != "OK" ]; do
287                 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
288         done
289
290         # stop daemon
291         daemon_exit ${base} ${config}
292
293         rm -rf ${base}
294         rm -f ${config}
295         rm -f ${config_new}
296         rm -f ${config_empty}
297 }
298
299 test_stop()
300 {
301         echo "test daemon stop"
302
303         local config=$(mktemp /tmp/perf.daemon.config.XXX)
304         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
305
306         # prepare config
307         cat <<EOF > ${config}
308 [daemon]
309 base=BASE
310
311 [session-size]
312 run = -e cpu-clock
313
314 [session-time]
315 run = -e task-clock
316 EOF
317
318         sed -i -e "s|BASE|${base}|" ${config}
319
320         # start daemon
321         daemon_start ${config} size
322
323         local pid_size=`perf daemon --config ${config} -x: | head -2 | tail -1 | awk 'BEGIN { FS = ":" } ; { print $1 }'`
324         local pid_time=`perf daemon --config ${config} -x: | head -3 | tail -1 | awk 'BEGIN { FS = ":" } ; { print $1 }'`
325
326         # check that sessions are running
327         if [ ! -d "/proc/${pid_size}" ]; then
328                 echo "FAILED: session size not up"
329         fi
330
331         if [ ! -d "/proc/${pid_time}" ]; then
332                 echo "FAILED: session time not up"
333         fi
334
335         # stop daemon
336         daemon_exit ${base} ${config}
337
338         # check that sessions are gone
339         if [ -d "/proc/${pid_size}" ]; then
340                 echo "FAILED: session size still up"
341         fi
342
343         if [ -d "/proc/${pid_time}" ]; then
344                 echo "FAILED: session time still up"
345         fi
346
347         rm -rf ${base}
348         rm -f ${config}
349 }
350
351 test_signal()
352 {
353         echo "test daemon signal"
354
355         local config=$(mktemp /tmp/perf.daemon.config.XXX)
356         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
357
358         # prepare config
359         cat <<EOF > ${config}
360 [daemon]
361 base=BASE
362
363 [session-test]
364 run = -e cpu-clock --switch-output
365 EOF
366
367         sed -i -e "s|BASE|${base}|" ${config}
368
369         # start daemon
370         daemon_start ${config} test
371
372         # send 2 signals
373         perf daemon signal --config ${config} --session test
374         perf daemon signal --config ${config}
375
376         # stop daemon
377         daemon_exit ${base} ${config}
378
379         # count is 2 perf.data for signals and 1 for perf record finished
380         count=`ls ${base}/session-test/ | grep perf.data | wc -l`
381         if [ ${count} -ne 3 ]; then
382                 error=1
383                 echo "FAILED: perf data no generated"
384         fi
385
386         rm -rf ${base}
387         rm -f ${config}
388 }
389
390 test_ping()
391 {
392         echo "test daemon ping"
393
394         local config=$(mktemp /tmp/perf.daemon.config.XXX)
395         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
396
397         # prepare config
398         cat <<EOF > ${config}
399 [daemon]
400 base=BASE
401
402 [session-size]
403 run = -e cpu-clock
404
405 [session-time]
406 run = -e task-clock
407 EOF
408
409         sed -i -e "s|BASE|${base}|" ${config}
410
411         # start daemon
412         daemon_start ${config} size
413
414         size=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`
415         type=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`
416
417         if [ ${size} != "OK" -o ${type} != "OK" ]; then
418                 error=1
419                 echo "FAILED: daemon ping failed"
420         fi
421
422         # stop daemon
423         daemon_exit ${base} ${config}
424
425         rm -rf ${base}
426         rm -f ${config}
427 }
428
429 test_lock()
430 {
431         echo "test daemon lock"
432
433         local config=$(mktemp /tmp/perf.daemon.config.XXX)
434         local base=$(mktemp -d /tmp/perf.daemon.base.XXX)
435
436         # prepare config
437         cat <<EOF > ${config}
438 [daemon]
439 base=BASE
440
441 [session-size]
442 run = -e cpu-clock
443 EOF
444
445         sed -i -e "s|BASE|${base}|" ${config}
446
447         # start daemon
448         daemon_start ${config} size
449
450         # start second daemon over the same config/base
451         failed=`perf daemon start --config ${config} 2>&1 | awk '{ print $1 }'`
452
453         # check that we failed properly
454         if [ ${failed} != "failed:" ]; then
455                 error=1
456                 echo "FAILED: daemon lock failed"
457         fi
458
459         # stop daemon
460         daemon_exit ${base} ${config}
461
462         rm -rf ${base}
463         rm -f ${config}
464 }
465
466 error=0
467
468 test_list
469 test_reconfig
470 test_stop
471 test_signal
472 test_ping
473 test_lock
474
475 exit ${error}