daemon.sh 11 KB

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