539 lines · bash
1#!/bin/bash2# daemon operations3# SPDX-License-Identifier: GPL-2.04 5check_line_first()6{7 local line=$18 local name=$29 local base=$310 local output=$411 local lock=$512 local up=$613 14 local line_name15 line_name=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $2 }'`16 local line_base17 line_base=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $3 }'`18 local line_output19 line_output=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $4 }'`20 local line_lock21 line_lock=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $5 }'`22 local line_up23 line_up=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $6 }'`24 25 if [ "${name}" != "${line_name}" ]; then26 echo "FAILED: wrong name"27 error=128 fi29 30 if [ "${base}" != "${line_base}" ]; then31 echo "FAILED: wrong base"32 error=133 fi34 35 if [ "${output}" != "${line_output}" ]; then36 echo "FAILED: wrong output"37 error=138 fi39 40 if [ "${lock}" != "${line_lock}" ]; then41 echo "FAILED: wrong lock"42 error=143 fi44 45 if [ "${up}" != "${line_up}" ]; then46 echo "FAILED: wrong up"47 error=148 fi49}50 51check_line_other()52{53 local line=$154 local name=$255 local run=$356 local base=$457 local output=$558 local control=$659 local ack=$760 local up=$861 62 local line_name63 line_name=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $2 }'`64 local line_run65 line_run=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $3 }'`66 local line_base67 line_base=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $4 }'`68 local line_output69 line_output=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $5 }'`70 local line_control71 line_control=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $6 }'`72 local line_ack73 line_ack=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $7 }'`74 local line_up75 line_up=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $8 }'`76 77 if [ "${name}" != "${line_name}" ]; then78 echo "FAILED: wrong name"79 error=180 fi81 82 if [ "${run}" != "${line_run}" ]; then83 echo "FAILED: wrong run"84 error=185 fi86 87 if [ "${base}" != "${line_base}" ]; then88 echo "FAILED: wrong base"89 error=190 fi91 92 if [ "${output}" != "${line_output}" ]; then93 echo "FAILED: wrong output"94 error=195 fi96 97 if [ "${control}" != "${line_control}" ]; then98 echo "FAILED: wrong control"99 error=1100 fi101 102 if [ "${ack}" != "${line_ack}" ]; then103 echo "FAILED: wrong ack"104 error=1105 fi106 107 if [ "${up}" != "${line_up}" ]; then108 echo "FAILED: wrong up"109 error=1110 fi111}112 113daemon_exit()114{115 local config=$1116 117 local line118 line=`perf daemon --config ${config} -x: | head -1`119 local pid120 pid=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $1 }'`121 122 # Reset trap handler.123 trap - SIGINT SIGTERM124 125 # stop daemon126 perf daemon stop --config ${config}127 128 # ... and wait for the pid to go away129 tail --pid=${pid} -f /dev/null130}131 132daemon_start()133{134 local config=$1135 local session=$2136 137 perf daemon start --config ${config}138 139 # Clean up daemon if interrupted.140 trap 'echo "FAILED: Signal caught"; daemon_exit "${config}"; exit 1' SIGINT SIGTERM141 142 # wait for the session to ping143 local state="FAIL"144 local retries=0145 while [ "${state}" != "OK" ]; do146 state=`perf daemon ping --config ${config} --session ${session} | awk '{ print $1 }'`147 sleep 0.05148 retries=$((${retries} +1))149 if [ ${retries} -ge 600 ]; then150 echo "FAILED: Timeout waiting for daemon to ping"151 daemon_exit ${config}152 exit 1153 fi154 done155}156 157test_list()158{159 echo "test daemon list"160 161 local config162 config=$(mktemp /tmp/perf.daemon.config.XXX)163 local base164 base=$(mktemp -d /tmp/perf.daemon.base.XXX)165 166 cat <<EOF > ${config}167[daemon]168base=BASE169 170[session-size]171run = -e cpu-clock -m 1 sleep 10172 173[session-time]174run = -e task-clock -m 1 sleep 10175EOF176 177 sed -i -e "s|BASE|${base}|" ${config}178 179 # start daemon180 daemon_start ${config} size181 182 # check first line183 # pid:daemon:base:base/output:base/lock184 local line185 line=`perf daemon --config ${config} -x: | head -1`186 check_line_first ${line} daemon ${base} ${base}/output ${base}/lock "0"187 188 # check 1st session189 # pid:size:-e cpu-clock:base/size:base/size/output:base/size/control:base/size/ack:0190 local line191 line=`perf daemon --config ${config} -x: | head -2 | tail -1`192 check_line_other "${line}" size "-e cpu-clock -m 1 sleep 10" ${base}/session-size \193 ${base}/session-size/output ${base}/session-size/control \194 ${base}/session-size/ack "0"195 196 # check 2nd session197 # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0198 local line199 line=`perf daemon --config ${config} -x: | head -3 | tail -1`200 check_line_other "${line}" time "-e task-clock -m 1 sleep 10" ${base}/session-time \201 ${base}/session-time/output ${base}/session-time/control \202 ${base}/session-time/ack "0"203 204 # stop daemon205 daemon_exit ${config}206 207 rm -rf ${base}208 rm -f ${config}209}210 211test_reconfig()212{213 echo "test daemon reconfig"214 215 local config216 config=$(mktemp /tmp/perf.daemon.config.XXX)217 local base218 base=$(mktemp -d /tmp/perf.daemon.base.XXX)219 220 # prepare config221 cat <<EOF > ${config}222[daemon]223base=BASE224 225[session-size]226run = -e cpu-clock -m 1 sleep 10227 228[session-time]229run = -e task-clock -m 1 sleep 10230EOF231 232 sed -i -e "s|BASE|${base}|" ${config}233 234 # start daemon235 daemon_start ${config} size236 237 # check 2nd session238 # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0239 local line240 line=`perf daemon --config ${config} -x: | head -3 | tail -1`241 check_line_other "${line}" time "-e task-clock -m 1 sleep 10" ${base}/session-time \242 ${base}/session-time/output ${base}/session-time/control ${base}/session-time/ack "0"243 local pid244 pid=`echo "${line}" | awk 'BEGIN { FS = ":" } ; { print $1 }'`245 246 # prepare new config247 local config_new=${config}.new248 cat <<EOF > ${config_new}249[daemon]250base=BASE251 252[session-size]253run = -e cpu-clock -m 1 sleep 10254 255[session-time]256run = -e cpu-clock -m 1 sleep 10257EOF258 259 # TEST 1 - change config260 261 sed -i -e "s|BASE|${base}|" ${config_new}262 cp ${config_new} ${config}263 264 # wait for old session to finish265 tail --pid=${pid} -f /dev/null266 267 # wait for new one to start268 local state="FAIL"269 while [ "${state}" != "OK" ]; do270 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`271 done272 273 # check reconfigured 2nd session274 # pid:time:-e task-clock:base/time:base/time/output:base/time/control:base/time/ack:0275 local line276 line=`perf daemon --config ${config} -x: | head -3 | tail -1`277 check_line_other "${line}" time "-e cpu-clock -m 1 sleep 10" ${base}/session-time \278 ${base}/session-time/output ${base}/session-time/control ${base}/session-time/ack "0"279 280 # TEST 2 - empty config281 282 local config_empty=${config}.empty283 cat <<EOF > ${config_empty}284[daemon]285base=BASE286EOF287 288 # change config289 sed -i -e "s|BASE|${base}|" ${config_empty}290 cp ${config_empty} ${config}291 292 # wait for sessions to finish293 local state="OK"294 while [ "${state}" != "FAIL" ]; do295 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`296 done297 298 local state="OK"299 while [ "${state}" != "FAIL" ]; do300 state=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`301 done302 303 local one304 one=`perf daemon --config ${config} -x: | wc -l`305 306 if [ ${one} -ne "1" ]; then307 echo "FAILED: wrong list output"308 error=1309 fi310 311 # TEST 3 - config again312 313 cp ${config_new} ${config}314 315 # wait for size to start316 local state="FAIL"317 while [ "${state}" != "OK" ]; do318 state=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`319 done320 321 # wait for time to start322 local state="FAIL"323 while [ "${state}" != "OK" ]; do324 state=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`325 done326 327 # stop daemon328 daemon_exit ${config}329 330 rm -rf ${base}331 rm -f ${config}332 rm -f ${config_new}333 rm -f ${config_empty}334}335 336test_stop()337{338 echo "test daemon stop"339 340 local config341 config=$(mktemp /tmp/perf.daemon.config.XXX)342 local base343 base=$(mktemp -d /tmp/perf.daemon.base.XXX)344 345 # prepare config346 cat <<EOF > ${config}347[daemon]348base=BASE349 350[session-size]351run = -e cpu-clock -m 1 sleep 10352 353[session-time]354run = -e task-clock -m 1 sleep 10355EOF356 357 sed -i -e "s|BASE|${base}|" ${config}358 359 # start daemon360 daemon_start ${config} size361 362 local pid_size363 pid_size=`perf daemon --config ${config} -x: | head -2 | tail -1 |364 awk 'BEGIN { FS = ":" } ; { print $1 }'`365 local pid_time366 pid_time=`perf daemon --config ${config} -x: | head -3 | tail -1 |367 awk 'BEGIN { FS = ":" } ; { print $1 }'`368 369 # check that sessions are running370 if [ ! -d "/proc/${pid_size}" ]; then371 echo "FAILED: session size not up"372 fi373 374 if [ ! -d "/proc/${pid_time}" ]; then375 echo "FAILED: session time not up"376 fi377 378 # stop daemon379 daemon_exit ${config}380 381 # check that sessions are gone382 if [ -d "/proc/${pid_size}" ]; then383 echo "FAILED: session size still up"384 fi385 386 if [ -d "/proc/${pid_time}" ]; then387 echo "FAILED: session time still up"388 fi389 390 rm -rf ${base}391 rm -f ${config}392}393 394test_signal()395{396 echo "test daemon signal"397 398 local config399 config=$(mktemp /tmp/perf.daemon.config.XXX)400 local base401 base=$(mktemp -d /tmp/perf.daemon.base.XXX)402 403 # prepare config404 cat <<EOF > ${config}405[daemon]406base=BASE407 408[session-test]409run = -e cpu-clock --switch-output -m 1 sleep 10410EOF411 412 sed -i -e "s|BASE|${base}|" ${config}413 414 # start daemon415 daemon_start ${config} test416 417 # send 2 signals then exit. Do this in a loop watching the number of418 # files to avoid races. If the loop retries more than 600 times then419 # give up.420 local retries=0421 local signals=0422 local success=0423 while [ ${retries} -lt 600 ] && [ ${success} -eq 0 ]; do424 local files425 files=`ls ${base}/session-test/*perf.data* 2> /dev/null | wc -l`426 if [ ${signals} -eq 0 ]; then427 perf daemon signal --config ${config} --session test428 signals=1429 elif [ ${signals} -eq 1 ] && [ $files -ge 1 ]; then430 perf daemon signal --config ${config}431 signals=2432 elif [ ${signals} -eq 2 ] && [ $files -ge 2 ]; then433 daemon_exit ${config}434 signals=3435 elif [ ${signals} -eq 3 ] && [ $files -ge 3 ]; then436 success=1437 fi438 retries=$((${retries} +1))439 done440 if [ ${success} -eq 0 ]; then441 error=1442 echo "FAILED: perf data no generated"443 fi444 445 rm -rf ${base}446 rm -f ${config}447}448 449test_ping()450{451 echo "test daemon ping"452 453 local config454 config=$(mktemp /tmp/perf.daemon.config.XXX)455 local base456 base=$(mktemp -d /tmp/perf.daemon.base.XXX)457 458 # prepare config459 cat <<EOF > ${config}460[daemon]461base=BASE462 463[session-size]464run = -e cpu-clock -m 1 sleep 10465 466[session-time]467run = -e task-clock -m 1 sleep 10468EOF469 470 sed -i -e "s|BASE|${base}|" ${config}471 472 # start daemon473 daemon_start ${config} size474 475 size=`perf daemon ping --config ${config} --session size | awk '{ print $1 }'`476 type=`perf daemon ping --config ${config} --session time | awk '{ print $1 }'`477 478 if [ ${size} != "OK" ] || [ ${type} != "OK" ]; then479 error=1480 echo "FAILED: daemon ping failed"481 fi482 483 # stop daemon484 daemon_exit ${config}485 486 rm -rf ${base}487 rm -f ${config}488}489 490test_lock()491{492 echo "test daemon lock"493 494 local config495 config=$(mktemp /tmp/perf.daemon.config.XXX)496 local base497 base=$(mktemp -d /tmp/perf.daemon.base.XXX)498 499 # prepare config500 cat <<EOF > ${config}501[daemon]502base=BASE503 504[session-size]505run = -e cpu-clock -m 1 sleep 10506EOF507 508 sed -i -e "s|BASE|${base}|" ${config}509 510 # start daemon511 daemon_start ${config} size512 513 # start second daemon over the same config/base514 failed=`perf daemon start --config ${config} 2>&1 | awk '{ print $1 }'`515 516 # check that we failed properly517 if [ ${failed} != "failed:" ]; then518 error=1519 echo "FAILED: daemon lock failed"520 fi521 522 # stop daemon523 daemon_exit ${config}524 525 rm -rf ${base}526 rm -f ${config}527}528 529error=0530 531test_list532test_reconfig533test_stop534test_signal535test_ping536test_lock537 538exit ${error}539