trace_selftest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. trace->init(tr);
  92. /* Sleep for a 1/10 of a second */
  93. msleep(100);
  94. /* we should have nothing in the buffer */
  95. ret = trace_test_buffer(tr, &count);
  96. if (ret)
  97. goto out;
  98. if (count) {
  99. ret = -1;
  100. printk(KERN_CONT ".. filter did not filter .. ");
  101. goto out;
  102. }
  103. /* call our function again */
  104. func();
  105. /* sleep again */
  106. msleep(100);
  107. /* stop the tracing. */
  108. tracing_stop();
  109. ftrace_enabled = 0;
  110. /* check the trace buffer */
  111. ret = trace_test_buffer(tr, &count);
  112. trace->reset(tr);
  113. tracing_start();
  114. /* we should only have one item */
  115. if (!ret && count != 1) {
  116. printk(KERN_CONT ".. filter failed count=%ld ..", count);
  117. ret = -1;
  118. goto out;
  119. }
  120. out:
  121. ftrace_enabled = save_ftrace_enabled;
  122. tracer_enabled = save_tracer_enabled;
  123. /* Enable tracing on all functions again */
  124. ftrace_set_filter(NULL, 0, 1);
  125. return ret;
  126. }
  127. #else
  128. # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
  129. #endif /* CONFIG_DYNAMIC_FTRACE */
  130. /*
  131. * Simple verification test of ftrace function tracer.
  132. * Enable ftrace, sleep 1/10 second, and then read the trace
  133. * buffer to see if all is in order.
  134. */
  135. int
  136. trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
  137. {
  138. int save_ftrace_enabled = ftrace_enabled;
  139. int save_tracer_enabled = tracer_enabled;
  140. unsigned long count;
  141. int ret;
  142. /* make sure msleep has been recorded */
  143. msleep(1);
  144. /* start the tracing */
  145. ftrace_enabled = 1;
  146. tracer_enabled = 1;
  147. trace->init(tr);
  148. /* Sleep for a 1/10 of a second */
  149. msleep(100);
  150. /* stop the tracing. */
  151. tracing_stop();
  152. ftrace_enabled = 0;
  153. /* check the trace buffer */
  154. ret = trace_test_buffer(tr, &count);
  155. trace->reset(tr);
  156. tracing_start();
  157. if (!ret && !count) {
  158. printk(KERN_CONT ".. no entries found ..");
  159. ret = -1;
  160. goto out;
  161. }
  162. ret = trace_selftest_startup_dynamic_tracing(trace, tr,
  163. DYN_FTRACE_TEST_NAME);
  164. out:
  165. ftrace_enabled = save_ftrace_enabled;
  166. tracer_enabled = save_tracer_enabled;
  167. /* kill ftrace totally if we failed */
  168. if (ret)
  169. ftrace_kill();
  170. return ret;
  171. }
  172. #endif /* CONFIG_FUNCTION_TRACER */
  173. #ifdef CONFIG_IRQSOFF_TRACER
  174. int
  175. trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
  176. {
  177. unsigned long save_max = tracing_max_latency;
  178. unsigned long count;
  179. int ret;
  180. /* start the tracing */
  181. trace->init(tr);
  182. /* reset the max latency */
  183. tracing_max_latency = 0;
  184. /* disable interrupts for a bit */
  185. local_irq_disable();
  186. udelay(100);
  187. local_irq_enable();
  188. /* stop the tracing. */
  189. tracing_stop();
  190. /* check both trace buffers */
  191. ret = trace_test_buffer(tr, NULL);
  192. if (!ret)
  193. ret = trace_test_buffer(&max_tr, &count);
  194. trace->reset(tr);
  195. tracing_start();
  196. if (!ret && !count) {
  197. printk(KERN_CONT ".. no entries found ..");
  198. ret = -1;
  199. }
  200. tracing_max_latency = save_max;
  201. return ret;
  202. }
  203. #endif /* CONFIG_IRQSOFF_TRACER */
  204. #ifdef CONFIG_PREEMPT_TRACER
  205. int
  206. trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
  207. {
  208. unsigned long save_max = tracing_max_latency;
  209. unsigned long count;
  210. int ret;
  211. /* start the tracing */
  212. trace->init(tr);
  213. /* reset the max latency */
  214. tracing_max_latency = 0;
  215. /* disable preemption for a bit */
  216. preempt_disable();
  217. udelay(100);
  218. preempt_enable();
  219. /* stop the tracing. */
  220. tracing_stop();
  221. /* check both trace buffers */
  222. ret = trace_test_buffer(tr, NULL);
  223. if (!ret)
  224. ret = trace_test_buffer(&max_tr, &count);
  225. trace->reset(tr);
  226. tracing_start();
  227. if (!ret && !count) {
  228. printk(KERN_CONT ".. no entries found ..");
  229. ret = -1;
  230. }
  231. tracing_max_latency = save_max;
  232. return ret;
  233. }
  234. #endif /* CONFIG_PREEMPT_TRACER */
  235. #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
  236. int
  237. trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
  238. {
  239. unsigned long save_max = tracing_max_latency;
  240. unsigned long count;
  241. int ret;
  242. /* start the tracing */
  243. trace->init(tr);
  244. /* reset the max latency */
  245. tracing_max_latency = 0;
  246. /* disable preemption and interrupts for a bit */
  247. preempt_disable();
  248. local_irq_disable();
  249. udelay(100);
  250. preempt_enable();
  251. /* reverse the order of preempt vs irqs */
  252. local_irq_enable();
  253. /* stop the tracing. */
  254. tracing_stop();
  255. /* check both trace buffers */
  256. ret = trace_test_buffer(tr, NULL);
  257. if (ret) {
  258. tracing_start();
  259. goto out;
  260. }
  261. ret = trace_test_buffer(&max_tr, &count);
  262. if (ret) {
  263. tracing_start();
  264. goto out;
  265. }
  266. if (!ret && !count) {
  267. printk(KERN_CONT ".. no entries found ..");
  268. ret = -1;
  269. tracing_start();
  270. goto out;
  271. }
  272. /* do the test by disabling interrupts first this time */
  273. tracing_max_latency = 0;
  274. tracing_start();
  275. preempt_disable();
  276. local_irq_disable();
  277. udelay(100);
  278. preempt_enable();
  279. /* reverse the order of preempt vs irqs */
  280. local_irq_enable();
  281. /* stop the tracing. */
  282. tracing_stop();
  283. /* check both trace buffers */
  284. ret = trace_test_buffer(tr, NULL);
  285. if (ret)
  286. goto out;
  287. ret = trace_test_buffer(&max_tr, &count);
  288. if (!ret && !count) {
  289. printk(KERN_CONT ".. no entries found ..");
  290. ret = -1;
  291. goto out;
  292. }
  293. out:
  294. trace->reset(tr);
  295. tracing_start();
  296. tracing_max_latency = save_max;
  297. return ret;
  298. }
  299. #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
  300. #ifdef CONFIG_NOP_TRACER
  301. int
  302. trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
  303. {
  304. /* What could possibly go wrong? */
  305. return 0;
  306. }
  307. #endif
  308. #ifdef CONFIG_SCHED_TRACER
  309. static int trace_wakeup_test_thread(void *data)
  310. {
  311. /* Make this a RT thread, doesn't need to be too high */
  312. struct sched_param param = { .sched_priority = 5 };
  313. struct completion *x = data;
  314. sched_setscheduler(current, SCHED_FIFO, &param);
  315. /* Make it know we have a new prio */
  316. complete(x);
  317. /* now go to sleep and let the test wake us up */
  318. set_current_state(TASK_INTERRUPTIBLE);
  319. schedule();
  320. /* we are awake, now wait to disappear */
  321. while (!kthread_should_stop()) {
  322. /*
  323. * This is an RT task, do short sleeps to let
  324. * others run.
  325. */
  326. msleep(100);
  327. }
  328. return 0;
  329. }
  330. int
  331. trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
  332. {
  333. unsigned long save_max = tracing_max_latency;
  334. struct task_struct *p;
  335. struct completion isrt;
  336. unsigned long count;
  337. int ret;
  338. init_completion(&isrt);
  339. /* create a high prio thread */
  340. p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
  341. if (IS_ERR(p)) {
  342. printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
  343. return -1;
  344. }
  345. /* make sure the thread is running at an RT prio */
  346. wait_for_completion(&isrt);
  347. /* start the tracing */
  348. trace->init(tr);
  349. /* reset the max latency */
  350. tracing_max_latency = 0;
  351. /* sleep to let the RT thread sleep too */
  352. msleep(100);
  353. /*
  354. * Yes this is slightly racy. It is possible that for some
  355. * strange reason that the RT thread we created, did not
  356. * call schedule for 100ms after doing the completion,
  357. * and we do a wakeup on a task that already is awake.
  358. * But that is extremely unlikely, and the worst thing that
  359. * happens in such a case, is that we disable tracing.
  360. * Honestly, if this race does happen something is horrible
  361. * wrong with the system.
  362. */
  363. wake_up_process(p);
  364. /* give a little time to let the thread wake up */
  365. msleep(100);
  366. /* stop the tracing. */
  367. tracing_stop();
  368. /* check both trace buffers */
  369. ret = trace_test_buffer(tr, NULL);
  370. if (!ret)
  371. ret = trace_test_buffer(&max_tr, &count);
  372. trace->reset(tr);
  373. tracing_start();
  374. tracing_max_latency = save_max;
  375. /* kill the thread */
  376. kthread_stop(p);
  377. if (!ret && !count) {
  378. printk(KERN_CONT ".. no entries found ..");
  379. ret = -1;
  380. }
  381. return ret;
  382. }
  383. #endif /* CONFIG_SCHED_TRACER */
  384. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  385. int
  386. trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
  387. {
  388. unsigned long count;
  389. int ret;
  390. /* start the tracing */
  391. trace->init(tr);
  392. /* Sleep for a 1/10 of a second */
  393. msleep(100);
  394. /* stop the tracing. */
  395. tracing_stop();
  396. /* check the trace buffer */
  397. ret = trace_test_buffer(tr, &count);
  398. trace->reset(tr);
  399. tracing_start();
  400. if (!ret && !count) {
  401. printk(KERN_CONT ".. no entries found ..");
  402. ret = -1;
  403. }
  404. return ret;
  405. }
  406. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  407. #ifdef CONFIG_SYSPROF_TRACER
  408. int
  409. trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
  410. {
  411. unsigned long count;
  412. int ret;
  413. /* start the tracing */
  414. trace->init(tr);
  415. /* Sleep for a 1/10 of a second */
  416. msleep(100);
  417. /* stop the tracing. */
  418. tracing_stop();
  419. /* check the trace buffer */
  420. ret = trace_test_buffer(tr, &count);
  421. trace->reset(tr);
  422. tracing_start();
  423. return ret;
  424. }
  425. #endif /* CONFIG_SYSPROF_TRACER */