trace_selftest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. tracing_stop();
  110. ftrace_enabled = 0;
  111. /* check the trace buffer */
  112. ret = trace_test_buffer(tr, &count);
  113. trace->reset(tr);
  114. tracing_start();
  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. tracing_stop();
  154. ftrace_enabled = 0;
  155. /* check the trace buffer */
  156. ret = trace_test_buffer(tr, &count);
  157. trace->reset(tr);
  158. tracing_start();
  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. tracing_stop();
  193. /* check both trace buffers */
  194. ret = trace_test_buffer(tr, NULL);
  195. if (!ret)
  196. ret = trace_test_buffer(&max_tr, &count);
  197. trace->reset(tr);
  198. tracing_start();
  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. tracing_stop();
  225. /* check both trace buffers */
  226. ret = trace_test_buffer(tr, NULL);
  227. if (!ret)
  228. ret = trace_test_buffer(&max_tr, &count);
  229. trace->reset(tr);
  230. tracing_start();
  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. tracing_stop();
  260. /* check both trace buffers */
  261. ret = trace_test_buffer(tr, NULL);
  262. if (ret) {
  263. tracing_start();
  264. goto out;
  265. }
  266. ret = trace_test_buffer(&max_tr, &count);
  267. if (ret) {
  268. tracing_start();
  269. goto out;
  270. }
  271. if (!ret && !count) {
  272. printk(KERN_CONT ".. no entries found ..");
  273. ret = -1;
  274. tracing_start();
  275. goto out;
  276. }
  277. /* do the test by disabling interrupts first this time */
  278. tracing_max_latency = 0;
  279. tracing_start();
  280. preempt_disable();
  281. local_irq_disable();
  282. udelay(100);
  283. preempt_enable();
  284. /* reverse the order of preempt vs irqs */
  285. local_irq_enable();
  286. /* stop the tracing. */
  287. tracing_stop();
  288. /* check both trace buffers */
  289. ret = trace_test_buffer(tr, NULL);
  290. if (ret)
  291. goto out;
  292. ret = trace_test_buffer(&max_tr, &count);
  293. if (!ret && !count) {
  294. printk(KERN_CONT ".. no entries found ..");
  295. ret = -1;
  296. goto out;
  297. }
  298. out:
  299. trace->reset(tr);
  300. tracing_start();
  301. tracing_max_latency = save_max;
  302. return ret;
  303. }
  304. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  305. #ifdef CONFIG_NOP_TRACER
  306. int
  307. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  308. {
  309. /* What could possibly go wrong? */
  310. return 0;
  311. }
  312. #endif
  313. #ifdef CONFIG_SCHED_TRACER
  314. static int trace_wakeup_test_thread(void *data)
  315. {
  316. /* Make this a RT thread, doesn't need to be too high */
  317. struct sched_param param = { .sched_priority = 5 };
  318. struct completion *x = data;
  319. sched_setscheduler(current, SCHED_FIFO, &param);
  320. /* Make it know we have a new prio */
  321. complete(x);
  322. /* now go to sleep and let the test wake us up */
  323. set_current_state(TASK_INTERRUPTIBLE);
  324. schedule();
  325. /* we are awake, now wait to disappear */
  326. while (!kthread_should_stop()) {
  327. /*
  328. * This is an RT task, do short sleeps to let
  329. * others run.
  330. */
  331. msleep(100);
  332. }
  333. return 0;
  334. }
  335. int
  336. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  337. {
  338. unsigned long save_max = tracing_max_latency;
  339. struct task_struct *p;
  340. struct completion isrt;
  341. unsigned long count;
  342. int ret;
  343. init_completion(&isrt);
  344. /* create a high prio thread */
  345. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  346. if (IS_ERR(p)) {
  347. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  348. return -1;
  349. }
  350. /* make sure the thread is running at an RT prio */
  351. wait_for_completion(&isrt);
  352. /* start the tracing */
  353. tr->ctrl = 1;
  354. trace->init(tr);
  355. /* reset the max latency */
  356. tracing_max_latency = 0;
  357. /* sleep to let the RT thread sleep too */
  358. msleep(100);
  359. /*
  360. * Yes this is slightly racy. It is possible that for some
  361. * strange reason that the RT thread we created, did not
  362. * call schedule for 100ms after doing the completion,
  363. * and we do a wakeup on a task that already is awake.
  364. * But that is extremely unlikely, and the worst thing that
  365. * happens in such a case, is that we disable tracing.
  366. * Honestly, if this race does happen something is horrible
  367. * wrong with the system.
  368. */
  369. wake_up_process(p);
  370. /* give a little time to let the thread wake up */
  371. msleep(100);
  372. /* stop the tracing. */
  373. tracing_stop();
  374. /* check both trace buffers */
  375. ret = trace_test_buffer(tr, NULL);
  376. if (!ret)
  377. ret = trace_test_buffer(&max_tr, &count);
  378. trace->reset(tr);
  379. tracing_start();
  380. tracing_max_latency = save_max;
  381. /* kill the thread */
  382. kthread_stop(p);
  383. if (!ret && !count) {
  384. printk(KERN_CONT ".. no entries found ..");
  385. ret = -1;
  386. }
  387. return ret;
  388. }
  389. #endif /* CONFIG_SCHED_TRACER */
  390. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  391. int
  392. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  393. {
  394. unsigned long count;
  395. int ret;
  396. /* start the tracing */
  397. tr->ctrl = 1;
  398. trace->init(tr);
  399. /* Sleep for a 1/10 of a second */
  400. msleep(100);
  401. /* stop the tracing. */
  402. tracing_stop();
  403. /* check the trace buffer */
  404. ret = trace_test_buffer(tr, &count);
  405. trace->reset(tr);
  406. tracing_start();
  407. if (!ret && !count) {
  408. printk(KERN_CONT ".. no entries found ..");
  409. ret = -1;
  410. }
  411. return ret;
  412. }
  413. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  414. #ifdef CONFIG_SYSPROF_TRACER
  415. int
  416. trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
  417. {
  418. unsigned long count;
  419. int ret;
  420. /* start the tracing */
  421. tr->ctrl = 1;
  422. trace->init(tr);
  423. /* Sleep for a 1/10 of a second */
  424. msleep(100);
  425. /* stop the tracing. */
  426. tracing_stop();
  427. /* check the trace buffer */
  428. ret = trace_test_buffer(tr, &count);
  429. trace->reset(tr);
  430. tracing_start();
  431. return ret;
  432. }
  433. #endif /* CONFIG_SYSPROF_TRACER */