trace_selftest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* Include in trace.c */
  2. #include <linux/kthread.h>
  3. #include <linux/delay.h>
  4. static inline int trace_valid_entry(struct trace_entry *entry)
  5. {
  6. switch (entry->type) {
  7. case TRACE_FN:
  8. case TRACE_CTX:
  9. case TRACE_WAKE:
  10. case TRACE_CONT:
  11. case TRACE_STACK:
  12. case TRACE_PRINT:
  13. case TRACE_SPECIAL:
  14. return 1;
  15. }
  16. return 0;
  17. }
  18. static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
  19. {
  20. struct ring_buffer_event *event;
  21. struct trace_entry *entry;
  22. while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
  23. entry = ring_buffer_event_data(event);
  24. if (!trace_valid_entry(entry)) {
  25. printk(KERN_CONT ".. invalid entry %d ",
  26. entry->type);
  27. goto failed;
  28. }
  29. }
  30. return 0;
  31. failed:
  32. /* disable tracing */
  33. tracing_disabled = 1;
  34. printk(KERN_CONT ".. corrupted trace buffer .. ");
  35. return -1;
  36. }
  37. /*
  38. * Test the trace buffer to see if all the elements
  39. * are still sane.
  40. */
  41. static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
  42. {
  43. unsigned long flags, cnt = 0;
  44. int cpu, ret = 0;
  45. /* Don't allow flipping of max traces now */
  46. raw_local_irq_save(flags);
  47. __raw_spin_lock(&ftrace_max_lock);
  48. cnt = ring_buffer_entries(tr->buffer);
  49. for_each_possible_cpu(cpu) {
  50. ret = trace_test_buffer_cpu(tr, cpu);
  51. if (ret)
  52. break;
  53. }
  54. __raw_spin_unlock(&ftrace_max_lock);
  55. raw_local_irq_restore(flags);
  56. if (count)
  57. *count = cnt;
  58. return ret;
  59. }
  60. #ifdef CONFIG_FUNCTION_TRACER
  61. #ifdef CONFIG_DYNAMIC_FTRACE
  62. #define __STR(x) #x
  63. #define STR(x) __STR(x)
  64. /* Test dynamic code modification and ftrace filters */
  65. int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
  66. struct trace_array *tr,
  67. int (*func)(void))
  68. {
  69. int save_ftrace_enabled = ftrace_enabled;
  70. int save_tracer_enabled = tracer_enabled;
  71. unsigned long count;
  72. char *func_name;
  73. int ret;
  74. /* The ftrace test PASSED */
  75. printk(KERN_CONT "PASSED\n");
  76. pr_info("Testing dynamic ftrace: ");
  77. /* enable tracing, and record the filter function */
  78. ftrace_enabled = 1;
  79. tracer_enabled = 1;
  80. /* passed in by parameter to fool gcc from optimizing */
  81. func();
  82. /*
  83. * Some archs *cough*PowerPC*cough* add charachters to the
  84. * start of the function names. We simply put a '*' to
  85. * accomodate them.
  86. */
  87. func_name = "*" STR(DYN_FTRACE_TEST_NAME);
  88. /* filter only on our function */
  89. ftrace_set_filter(func_name, strlen(func_name), 1);
  90. /* enable tracing */
  91. tr->ctrl = 1;
  92. trace->init(tr);
  93. /* Sleep for a 1/10 of a second */
  94. msleep(100);
  95. /* we should have nothing in the buffer */
  96. ret = trace_test_buffer(tr, &count);
  97. if (ret)
  98. goto out;
  99. if (count) {
  100. ret = -1;
  101. printk(KERN_CONT ".. filter did not filter .. ");
  102. goto out;
  103. }
  104. /* call our function again */
  105. func();
  106. /* sleep again */
  107. msleep(100);
  108. /* stop the tracing. */
  109. tr->ctrl = 0;
  110. trace->ctrl_update(tr);
  111. ftrace_enabled = 0;
  112. /* check the trace buffer */
  113. ret = trace_test_buffer(tr, &count);
  114. trace->reset(tr);
  115. /* we should only have one item */
  116. if (!ret && count != 1) {
  117. printk(KERN_CONT ".. filter failed count=%ld ..", count);
  118. ret = -1;
  119. goto out;
  120. }
  121. out:
  122. ftrace_enabled = save_ftrace_enabled;
  123. tracer_enabled = save_tracer_enabled;
  124. /* Enable tracing on all functions again */
  125. ftrace_set_filter(NULL, 0, 1);
  126. return ret;
  127. }
  128. #else
  129. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  130. #endif /* CONFIG_DYNAMIC_FTRACE */
  131. /*
  132. * Simple verification test of ftrace function tracer.
  133. * Enable ftrace, sleep 1/10 second, and then read the trace
  134. * buffer to see if all is in order.
  135. */
  136. int
  137. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  138. {
  139. int save_ftrace_enabled = ftrace_enabled;
  140. int save_tracer_enabled = tracer_enabled;
  141. unsigned long count;
  142. int ret;
  143. /* make sure msleep has been recorded */
  144. msleep(1);
  145. /* start the tracing */
  146. ftrace_enabled = 1;
  147. tracer_enabled = 1;
  148. tr->ctrl = 1;
  149. trace->init(tr);
  150. /* Sleep for a 1/10 of a second */
  151. msleep(100);
  152. /* stop the tracing. */
  153. tr->ctrl = 0;
  154. trace->ctrl_update(tr);
  155. ftrace_enabled = 0;
  156. /* check the trace buffer */
  157. ret = trace_test_buffer(tr, &count);
  158. trace->reset(tr);
  159. if (!ret && !count) {
  160. printk(KERN_CONT ".. no entries found ..");
  161. ret = -1;
  162. goto out;
  163. }
  164. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  165. DYN_FTRACE_TEST_NAME);
  166. out:
  167. ftrace_enabled = save_ftrace_enabled;
  168. tracer_enabled = save_tracer_enabled;
  169. /* kill ftrace totally if we failed */
  170. if (ret)
  171. ftrace_kill();
  172. return ret;
  173. }
  174. #endif /* CONFIG_FUNCTION_TRACER */
  175. #ifdef CONFIG_IRQSOFF_TRACER
  176. int
  177. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  178. {
  179. unsigned long save_max = tracing_max_latency;
  180. unsigned long count;
  181. int ret;
  182. /* start the tracing */
  183. tr->ctrl = 1;
  184. trace->init(tr);
  185. /* reset the max latency */
  186. tracing_max_latency = 0;
  187. /* disable interrupts for a bit */
  188. local_irq_disable();
  189. udelay(100);
  190. local_irq_enable();
  191. /* stop the tracing. */
  192. tr->ctrl = 0;
  193. trace->ctrl_update(tr);
  194. /* check both trace buffers */
  195. ret = trace_test_buffer(tr, NULL);
  196. if (!ret)
  197. ret = trace_test_buffer(&max_tr, &count);
  198. trace->reset(tr);
  199. if (!ret && !count) {
  200. printk(KERN_CONT ".. no entries found ..");
  201. ret = -1;
  202. }
  203. tracing_max_latency = save_max;
  204. return ret;
  205. }
  206. #endif /* CONFIG_IRQSOFF_TRACER */
  207. #ifdef CONFIG_PREEMPT_TRACER
  208. int
  209. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  210. {
  211. unsigned long save_max = tracing_max_latency;
  212. unsigned long count;
  213. int ret;
  214. /* start the tracing */
  215. tr->ctrl = 1;
  216. trace->init(tr);
  217. /* reset the max latency */
  218. tracing_max_latency = 0;
  219. /* disable preemption for a bit */
  220. preempt_disable();
  221. udelay(100);
  222. preempt_enable();
  223. /* stop the tracing. */
  224. tr->ctrl = 0;
  225. trace->ctrl_update(tr);
  226. /* check both trace buffers */
  227. ret = trace_test_buffer(tr, NULL);
  228. if (!ret)
  229. ret = trace_test_buffer(&max_tr, &count);
  230. trace->reset(tr);
  231. if (!ret && !count) {
  232. printk(KERN_CONT ".. no entries found ..");
  233. ret = -1;
  234. }
  235. tracing_max_latency = save_max;
  236. return ret;
  237. }
  238. #endif /* CONFIG_PREEMPT_TRACER */
  239. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  240. int
  241. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  242. {
  243. unsigned long save_max = tracing_max_latency;
  244. unsigned long count;
  245. int ret;
  246. /* start the tracing */
  247. tr->ctrl = 1;
  248. trace->init(tr);
  249. /* reset the max latency */
  250. tracing_max_latency = 0;
  251. /* disable preemption and interrupts for a bit */
  252. preempt_disable();
  253. local_irq_disable();
  254. udelay(100);
  255. preempt_enable();
  256. /* reverse the order of preempt vs irqs */
  257. local_irq_enable();
  258. /* stop the tracing. */
  259. tr->ctrl = 0;
  260. trace->ctrl_update(tr);
  261. /* check both trace buffers */
  262. ret = trace_test_buffer(tr, NULL);
  263. if (ret)
  264. goto out;
  265. ret = trace_test_buffer(&max_tr, &count);
  266. if (ret)
  267. goto out;
  268. if (!ret && !count) {
  269. printk(KERN_CONT ".. no entries found ..");
  270. ret = -1;
  271. goto out;
  272. }
  273. /* do the test by disabling interrupts first this time */
  274. tracing_max_latency = 0;
  275. tr->ctrl = 1;
  276. trace->ctrl_update(tr);
  277. preempt_disable();
  278. local_irq_disable();
  279. udelay(100);
  280. preempt_enable();
  281. /* reverse the order of preempt vs irqs */
  282. local_irq_enable();
  283. /* stop the tracing. */
  284. tr->ctrl = 0;
  285. trace->ctrl_update(tr);
  286. /* check both trace buffers */
  287. ret = trace_test_buffer(tr, NULL);
  288. if (ret)
  289. goto out;
  290. ret = trace_test_buffer(&max_tr, &count);
  291. if (!ret && !count) {
  292. printk(KERN_CONT ".. no entries found ..");
  293. ret = -1;
  294. goto out;
  295. }
  296. out:
  297. trace->reset(tr);
  298. tracing_max_latency = save_max;
  299. return ret;
  300. }
  301. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  302. #ifdef CONFIG_NOP_TRACER
  303. int
  304. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  305. {
  306. /* What could possibly go wrong? */
  307. return 0;
  308. }
  309. #endif
  310. #ifdef CONFIG_SCHED_TRACER
  311. static int trace_wakeup_test_thread(void *data)
  312. {
  313. /* Make this a RT thread, doesn't need to be too high */
  314. struct sched_param param = { .sched_priority = 5 };
  315. struct completion *x = data;
  316. sched_setscheduler(current, SCHED_FIFO, &param);
  317. /* Make it know we have a new prio */
  318. complete(x);
  319. /* now go to sleep and let the test wake us up */
  320. set_current_state(TASK_INTERRUPTIBLE);
  321. schedule();
  322. /* we are awake, now wait to disappear */
  323. while (!kthread_should_stop()) {
  324. /*
  325. * This is an RT task, do short sleeps to let
  326. * others run.
  327. */
  328. msleep(100);
  329. }
  330. return 0;
  331. }
  332. int
  333. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  334. {
  335. unsigned long save_max = tracing_max_latency;
  336. struct task_struct *p;
  337. struct completion isrt;
  338. unsigned long count;
  339. int ret;
  340. init_completion(&isrt);
  341. /* create a high prio thread */
  342. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  343. if (IS_ERR(p)) {
  344. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  345. return -1;
  346. }
  347. /* make sure the thread is running at an RT prio */
  348. wait_for_completion(&isrt);
  349. /* start the tracing */
  350. tr->ctrl = 1;
  351. trace->init(tr);
  352. /* reset the max latency */
  353. tracing_max_latency = 0;
  354. /* sleep to let the RT thread sleep too */
  355. msleep(100);
  356. /*
  357. * Yes this is slightly racy. It is possible that for some
  358. * strange reason that the RT thread we created, did not
  359. * call schedule for 100ms after doing the completion,
  360. * and we do a wakeup on a task that already is awake.
  361. * But that is extremely unlikely, and the worst thing that
  362. * happens in such a case, is that we disable tracing.
  363. * Honestly, if this race does happen something is horrible
  364. * wrong with the system.
  365. */
  366. wake_up_process(p);
  367. /* give a little time to let the thread wake up */
  368. msleep(100);
  369. /* stop the tracing. */
  370. tr->ctrl = 0;
  371. trace->ctrl_update(tr);
  372. /* check both trace buffers */
  373. ret = trace_test_buffer(tr, NULL);
  374. if (!ret)
  375. ret = trace_test_buffer(&max_tr, &count);
  376. trace->reset(tr);
  377. tracing_max_latency = save_max;
  378. /* kill the thread */
  379. kthread_stop(p);
  380. if (!ret && !count) {
  381. printk(KERN_CONT ".. no entries found ..");
  382. ret = -1;
  383. }
  384. return ret;
  385. }
  386. #endif /* CONFIG_SCHED_TRACER */
  387. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  388. int
  389. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  390. {
  391. unsigned long count;
  392. int ret;
  393. /* start the tracing */
  394. tr->ctrl = 1;
  395. trace->init(tr);
  396. /* Sleep for a 1/10 of a second */
  397. msleep(100);
  398. /* stop the tracing. */
  399. tr->ctrl = 0;
  400. trace->ctrl_update(tr);
  401. /* check the trace buffer */
  402. ret = trace_test_buffer(tr, &count);
  403. trace->reset(tr);
  404. if (!ret && !count) {
  405. printk(KERN_CONT ".. no entries found ..");
  406. ret = -1;
  407. }
  408. return ret;
  409. }
  410. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  411. #ifdef CONFIG_SYSPROF_TRACER
  412. int
  413. trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
  414. {
  415. unsigned long count;
  416. int ret;
  417. /* start the tracing */
  418. tr->ctrl = 1;
  419. trace->init(tr);
  420. /* Sleep for a 1/10 of a second */
  421. msleep(100);
  422. /* stop the tracing. */
  423. tr->ctrl = 0;
  424. trace->ctrl_update(tr);
  425. /* check the trace buffer */
  426. ret = trace_test_buffer(tr, &count);
  427. trace->reset(tr);
  428. return ret;
  429. }
  430. #endif /* CONFIG_SYSPROF_TRACER */