Linux 3.7.1
[linux/fpc-iii.git] / kernel / trace / trace_selftest.c
blob2c00a691a54068ec9db750f2be4053701bd5be65
1 /* Include in trace.c */
3 #include <linux/stringify.h>
4 #include <linux/kthread.h>
5 #include <linux/delay.h>
6 #include <linux/slab.h>
8 static inline int trace_valid_entry(struct trace_entry *entry)
10 switch (entry->type) {
11 case TRACE_FN:
12 case TRACE_CTX:
13 case TRACE_WAKE:
14 case TRACE_STACK:
15 case TRACE_PRINT:
16 case TRACE_BRANCH:
17 case TRACE_GRAPH_ENT:
18 case TRACE_GRAPH_RET:
19 return 1;
21 return 0;
24 static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
26 struct ring_buffer_event *event;
27 struct trace_entry *entry;
28 unsigned int loops = 0;
30 while ((event = ring_buffer_consume(tr->buffer, cpu, NULL, NULL))) {
31 entry = ring_buffer_event_data(event);
34 * The ring buffer is a size of trace_buf_size, if
35 * we loop more than the size, there's something wrong
36 * with the ring buffer.
38 if (loops++ > trace_buf_size) {
39 printk(KERN_CONT ".. bad ring buffer ");
40 goto failed;
42 if (!trace_valid_entry(entry)) {
43 printk(KERN_CONT ".. invalid entry %d ",
44 entry->type);
45 goto failed;
48 return 0;
50 failed:
51 /* disable tracing */
52 tracing_disabled = 1;
53 printk(KERN_CONT ".. corrupted trace buffer .. ");
54 return -1;
58 * Test the trace buffer to see if all the elements
59 * are still sane.
61 static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
63 unsigned long flags, cnt = 0;
64 int cpu, ret = 0;
66 /* Don't allow flipping of max traces now */
67 local_irq_save(flags);
68 arch_spin_lock(&ftrace_max_lock);
70 cnt = ring_buffer_entries(tr->buffer);
73 * The trace_test_buffer_cpu runs a while loop to consume all data.
74 * If the calling tracer is broken, and is constantly filling
75 * the buffer, this will run forever, and hard lock the box.
76 * We disable the ring buffer while we do this test to prevent
77 * a hard lock up.
79 tracing_off();
80 for_each_possible_cpu(cpu) {
81 ret = trace_test_buffer_cpu(tr, cpu);
82 if (ret)
83 break;
85 tracing_on();
86 arch_spin_unlock(&ftrace_max_lock);
87 local_irq_restore(flags);
89 if (count)
90 *count = cnt;
92 return ret;
95 static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
97 printk(KERN_WARNING "Failed to init %s tracer, init returned %d\n",
98 trace->name, init_ret);
100 #ifdef CONFIG_FUNCTION_TRACER
102 #ifdef CONFIG_DYNAMIC_FTRACE
104 static int trace_selftest_test_probe1_cnt;
105 static void trace_selftest_test_probe1_func(unsigned long ip,
106 unsigned long pip,
107 struct ftrace_ops *op,
108 struct pt_regs *pt_regs)
110 trace_selftest_test_probe1_cnt++;
113 static int trace_selftest_test_probe2_cnt;
114 static void trace_selftest_test_probe2_func(unsigned long ip,
115 unsigned long pip,
116 struct ftrace_ops *op,
117 struct pt_regs *pt_regs)
119 trace_selftest_test_probe2_cnt++;
122 static int trace_selftest_test_probe3_cnt;
123 static void trace_selftest_test_probe3_func(unsigned long ip,
124 unsigned long pip,
125 struct ftrace_ops *op,
126 struct pt_regs *pt_regs)
128 trace_selftest_test_probe3_cnt++;
131 static int trace_selftest_test_global_cnt;
132 static void trace_selftest_test_global_func(unsigned long ip,
133 unsigned long pip,
134 struct ftrace_ops *op,
135 struct pt_regs *pt_regs)
137 trace_selftest_test_global_cnt++;
140 static int trace_selftest_test_dyn_cnt;
141 static void trace_selftest_test_dyn_func(unsigned long ip,
142 unsigned long pip,
143 struct ftrace_ops *op,
144 struct pt_regs *pt_regs)
146 trace_selftest_test_dyn_cnt++;
149 static struct ftrace_ops test_probe1 = {
150 .func = trace_selftest_test_probe1_func,
151 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
154 static struct ftrace_ops test_probe2 = {
155 .func = trace_selftest_test_probe2_func,
156 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
159 static struct ftrace_ops test_probe3 = {
160 .func = trace_selftest_test_probe3_func,
161 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
164 static struct ftrace_ops test_global = {
165 .func = trace_selftest_test_global_func,
166 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
169 static void print_counts(void)
171 printk("(%d %d %d %d %d) ",
172 trace_selftest_test_probe1_cnt,
173 trace_selftest_test_probe2_cnt,
174 trace_selftest_test_probe3_cnt,
175 trace_selftest_test_global_cnt,
176 trace_selftest_test_dyn_cnt);
179 static void reset_counts(void)
181 trace_selftest_test_probe1_cnt = 0;
182 trace_selftest_test_probe2_cnt = 0;
183 trace_selftest_test_probe3_cnt = 0;
184 trace_selftest_test_global_cnt = 0;
185 trace_selftest_test_dyn_cnt = 0;
188 static int trace_selftest_ops(int cnt)
190 int save_ftrace_enabled = ftrace_enabled;
191 struct ftrace_ops *dyn_ops;
192 char *func1_name;
193 char *func2_name;
194 int len1;
195 int len2;
196 int ret = -1;
198 printk(KERN_CONT "PASSED\n");
199 pr_info("Testing dynamic ftrace ops #%d: ", cnt);
201 ftrace_enabled = 1;
202 reset_counts();
204 /* Handle PPC64 '.' name */
205 func1_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
206 func2_name = "*" __stringify(DYN_FTRACE_TEST_NAME2);
207 len1 = strlen(func1_name);
208 len2 = strlen(func2_name);
211 * Probe 1 will trace function 1.
212 * Probe 2 will trace function 2.
213 * Probe 3 will trace functions 1 and 2.
215 ftrace_set_filter(&test_probe1, func1_name, len1, 1);
216 ftrace_set_filter(&test_probe2, func2_name, len2, 1);
217 ftrace_set_filter(&test_probe3, func1_name, len1, 1);
218 ftrace_set_filter(&test_probe3, func2_name, len2, 0);
220 register_ftrace_function(&test_probe1);
221 register_ftrace_function(&test_probe2);
222 register_ftrace_function(&test_probe3);
223 register_ftrace_function(&test_global);
225 DYN_FTRACE_TEST_NAME();
227 print_counts();
229 if (trace_selftest_test_probe1_cnt != 1)
230 goto out;
231 if (trace_selftest_test_probe2_cnt != 0)
232 goto out;
233 if (trace_selftest_test_probe3_cnt != 1)
234 goto out;
235 if (trace_selftest_test_global_cnt == 0)
236 goto out;
238 DYN_FTRACE_TEST_NAME2();
240 print_counts();
242 if (trace_selftest_test_probe1_cnt != 1)
243 goto out;
244 if (trace_selftest_test_probe2_cnt != 1)
245 goto out;
246 if (trace_selftest_test_probe3_cnt != 2)
247 goto out;
249 /* Add a dynamic probe */
250 dyn_ops = kzalloc(sizeof(*dyn_ops), GFP_KERNEL);
251 if (!dyn_ops) {
252 printk("MEMORY ERROR ");
253 goto out;
256 dyn_ops->func = trace_selftest_test_dyn_func;
258 register_ftrace_function(dyn_ops);
260 trace_selftest_test_global_cnt = 0;
262 DYN_FTRACE_TEST_NAME();
264 print_counts();
266 if (trace_selftest_test_probe1_cnt != 2)
267 goto out_free;
268 if (trace_selftest_test_probe2_cnt != 1)
269 goto out_free;
270 if (trace_selftest_test_probe3_cnt != 3)
271 goto out_free;
272 if (trace_selftest_test_global_cnt == 0)
273 goto out;
274 if (trace_selftest_test_dyn_cnt == 0)
275 goto out_free;
277 DYN_FTRACE_TEST_NAME2();
279 print_counts();
281 if (trace_selftest_test_probe1_cnt != 2)
282 goto out_free;
283 if (trace_selftest_test_probe2_cnt != 2)
284 goto out_free;
285 if (trace_selftest_test_probe3_cnt != 4)
286 goto out_free;
288 ret = 0;
289 out_free:
290 unregister_ftrace_function(dyn_ops);
291 kfree(dyn_ops);
293 out:
294 /* Purposely unregister in the same order */
295 unregister_ftrace_function(&test_probe1);
296 unregister_ftrace_function(&test_probe2);
297 unregister_ftrace_function(&test_probe3);
298 unregister_ftrace_function(&test_global);
300 /* Make sure everything is off */
301 reset_counts();
302 DYN_FTRACE_TEST_NAME();
303 DYN_FTRACE_TEST_NAME();
305 if (trace_selftest_test_probe1_cnt ||
306 trace_selftest_test_probe2_cnt ||
307 trace_selftest_test_probe3_cnt ||
308 trace_selftest_test_global_cnt ||
309 trace_selftest_test_dyn_cnt)
310 ret = -1;
312 ftrace_enabled = save_ftrace_enabled;
314 return ret;
317 /* Test dynamic code modification and ftrace filters */
318 int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
319 struct trace_array *tr,
320 int (*func)(void))
322 int save_ftrace_enabled = ftrace_enabled;
323 int save_tracer_enabled = tracer_enabled;
324 unsigned long count;
325 char *func_name;
326 int ret;
328 /* The ftrace test PASSED */
329 printk(KERN_CONT "PASSED\n");
330 pr_info("Testing dynamic ftrace: ");
332 /* enable tracing, and record the filter function */
333 ftrace_enabled = 1;
334 tracer_enabled = 1;
336 /* passed in by parameter to fool gcc from optimizing */
337 func();
340 * Some archs *cough*PowerPC*cough* add characters to the
341 * start of the function names. We simply put a '*' to
342 * accommodate them.
344 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
346 /* filter only on our function */
347 ftrace_set_global_filter(func_name, strlen(func_name), 1);
349 /* enable tracing */
350 ret = tracer_init(trace, tr);
351 if (ret) {
352 warn_failed_init_tracer(trace, ret);
353 goto out;
356 /* Sleep for a 1/10 of a second */
357 msleep(100);
359 /* we should have nothing in the buffer */
360 ret = trace_test_buffer(tr, &count);
361 if (ret)
362 goto out;
364 if (count) {
365 ret = -1;
366 printk(KERN_CONT ".. filter did not filter .. ");
367 goto out;
370 /* call our function again */
371 func();
373 /* sleep again */
374 msleep(100);
376 /* stop the tracing. */
377 tracing_stop();
378 ftrace_enabled = 0;
380 /* check the trace buffer */
381 ret = trace_test_buffer(tr, &count);
382 tracing_start();
384 /* we should only have one item */
385 if (!ret && count != 1) {
386 trace->reset(tr);
387 printk(KERN_CONT ".. filter failed count=%ld ..", count);
388 ret = -1;
389 goto out;
392 /* Test the ops with global tracing running */
393 ret = trace_selftest_ops(1);
394 trace->reset(tr);
396 out:
397 ftrace_enabled = save_ftrace_enabled;
398 tracer_enabled = save_tracer_enabled;
400 /* Enable tracing on all functions again */
401 ftrace_set_global_filter(NULL, 0, 1);
403 /* Test the ops with global tracing off */
404 if (!ret)
405 ret = trace_selftest_ops(2);
407 return ret;
410 static int trace_selftest_recursion_cnt;
411 static void trace_selftest_test_recursion_func(unsigned long ip,
412 unsigned long pip,
413 struct ftrace_ops *op,
414 struct pt_regs *pt_regs)
417 * This function is registered without the recursion safe flag.
418 * The ftrace infrastructure should provide the recursion
419 * protection. If not, this will crash the kernel!
421 trace_selftest_recursion_cnt++;
422 DYN_FTRACE_TEST_NAME();
425 static void trace_selftest_test_recursion_safe_func(unsigned long ip,
426 unsigned long pip,
427 struct ftrace_ops *op,
428 struct pt_regs *pt_regs)
431 * We said we would provide our own recursion. By calling
432 * this function again, we should recurse back into this function
433 * and count again. But this only happens if the arch supports
434 * all of ftrace features and nothing else is using the function
435 * tracing utility.
437 if (trace_selftest_recursion_cnt++)
438 return;
439 DYN_FTRACE_TEST_NAME();
442 static struct ftrace_ops test_rec_probe = {
443 .func = trace_selftest_test_recursion_func,
446 static struct ftrace_ops test_recsafe_probe = {
447 .func = trace_selftest_test_recursion_safe_func,
448 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
451 static int
452 trace_selftest_function_recursion(void)
454 int save_ftrace_enabled = ftrace_enabled;
455 int save_tracer_enabled = tracer_enabled;
456 char *func_name;
457 int len;
458 int ret;
459 int cnt;
461 /* The previous test PASSED */
462 pr_cont("PASSED\n");
463 pr_info("Testing ftrace recursion: ");
466 /* enable tracing, and record the filter function */
467 ftrace_enabled = 1;
468 tracer_enabled = 1;
470 /* Handle PPC64 '.' name */
471 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
472 len = strlen(func_name);
474 ret = ftrace_set_filter(&test_rec_probe, func_name, len, 1);
475 if (ret) {
476 pr_cont("*Could not set filter* ");
477 goto out;
480 ret = register_ftrace_function(&test_rec_probe);
481 if (ret) {
482 pr_cont("*could not register callback* ");
483 goto out;
486 DYN_FTRACE_TEST_NAME();
488 unregister_ftrace_function(&test_rec_probe);
490 ret = -1;
491 if (trace_selftest_recursion_cnt != 1) {
492 pr_cont("*callback not called once (%d)* ",
493 trace_selftest_recursion_cnt);
494 goto out;
497 trace_selftest_recursion_cnt = 1;
499 pr_cont("PASSED\n");
500 pr_info("Testing ftrace recursion safe: ");
502 ret = ftrace_set_filter(&test_recsafe_probe, func_name, len, 1);
503 if (ret) {
504 pr_cont("*Could not set filter* ");
505 goto out;
508 ret = register_ftrace_function(&test_recsafe_probe);
509 if (ret) {
510 pr_cont("*could not register callback* ");
511 goto out;
514 DYN_FTRACE_TEST_NAME();
516 unregister_ftrace_function(&test_recsafe_probe);
519 * If arch supports all ftrace features, and no other task
520 * was on the list, we should be fine.
522 if (!ftrace_nr_registered_ops() && !FTRACE_FORCE_LIST_FUNC)
523 cnt = 2; /* Should have recursed */
524 else
525 cnt = 1;
527 ret = -1;
528 if (trace_selftest_recursion_cnt != cnt) {
529 pr_cont("*callback not called expected %d times (%d)* ",
530 cnt, trace_selftest_recursion_cnt);
531 goto out;
534 ret = 0;
535 out:
536 ftrace_enabled = save_ftrace_enabled;
537 tracer_enabled = save_tracer_enabled;
539 return ret;
541 #else
542 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
543 # define trace_selftest_function_recursion() ({ 0; })
544 #endif /* CONFIG_DYNAMIC_FTRACE */
546 static enum {
547 TRACE_SELFTEST_REGS_START,
548 TRACE_SELFTEST_REGS_FOUND,
549 TRACE_SELFTEST_REGS_NOT_FOUND,
550 } trace_selftest_regs_stat;
552 static void trace_selftest_test_regs_func(unsigned long ip,
553 unsigned long pip,
554 struct ftrace_ops *op,
555 struct pt_regs *pt_regs)
557 if (pt_regs)
558 trace_selftest_regs_stat = TRACE_SELFTEST_REGS_FOUND;
559 else
560 trace_selftest_regs_stat = TRACE_SELFTEST_REGS_NOT_FOUND;
563 static struct ftrace_ops test_regs_probe = {
564 .func = trace_selftest_test_regs_func,
565 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_SAVE_REGS,
568 static int
569 trace_selftest_function_regs(void)
571 int save_ftrace_enabled = ftrace_enabled;
572 int save_tracer_enabled = tracer_enabled;
573 char *func_name;
574 int len;
575 int ret;
576 int supported = 0;
578 #ifdef ARCH_SUPPORTS_FTRACE_SAVE_REGS
579 supported = 1;
580 #endif
582 /* The previous test PASSED */
583 pr_cont("PASSED\n");
584 pr_info("Testing ftrace regs%s: ",
585 !supported ? "(no arch support)" : "");
587 /* enable tracing, and record the filter function */
588 ftrace_enabled = 1;
589 tracer_enabled = 1;
591 /* Handle PPC64 '.' name */
592 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME);
593 len = strlen(func_name);
595 ret = ftrace_set_filter(&test_regs_probe, func_name, len, 1);
597 * If DYNAMIC_FTRACE is not set, then we just trace all functions.
598 * This test really doesn't care.
600 if (ret && ret != -ENODEV) {
601 pr_cont("*Could not set filter* ");
602 goto out;
605 ret = register_ftrace_function(&test_regs_probe);
607 * Now if the arch does not support passing regs, then this should
608 * have failed.
610 if (!supported) {
611 if (!ret) {
612 pr_cont("*registered save-regs without arch support* ");
613 goto out;
615 test_regs_probe.flags |= FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
616 ret = register_ftrace_function(&test_regs_probe);
618 if (ret) {
619 pr_cont("*could not register callback* ");
620 goto out;
624 DYN_FTRACE_TEST_NAME();
626 unregister_ftrace_function(&test_regs_probe);
628 ret = -1;
630 switch (trace_selftest_regs_stat) {
631 case TRACE_SELFTEST_REGS_START:
632 pr_cont("*callback never called* ");
633 goto out;
635 case TRACE_SELFTEST_REGS_FOUND:
636 if (supported)
637 break;
638 pr_cont("*callback received regs without arch support* ");
639 goto out;
641 case TRACE_SELFTEST_REGS_NOT_FOUND:
642 if (!supported)
643 break;
644 pr_cont("*callback received NULL regs* ");
645 goto out;
648 ret = 0;
649 out:
650 ftrace_enabled = save_ftrace_enabled;
651 tracer_enabled = save_tracer_enabled;
653 return ret;
657 * Simple verification test of ftrace function tracer.
658 * Enable ftrace, sleep 1/10 second, and then read the trace
659 * buffer to see if all is in order.
662 trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
664 int save_ftrace_enabled = ftrace_enabled;
665 int save_tracer_enabled = tracer_enabled;
666 unsigned long count;
667 int ret;
669 /* make sure msleep has been recorded */
670 msleep(1);
672 /* start the tracing */
673 ftrace_enabled = 1;
674 tracer_enabled = 1;
676 ret = tracer_init(trace, tr);
677 if (ret) {
678 warn_failed_init_tracer(trace, ret);
679 goto out;
682 /* Sleep for a 1/10 of a second */
683 msleep(100);
684 /* stop the tracing. */
685 tracing_stop();
686 ftrace_enabled = 0;
688 /* check the trace buffer */
689 ret = trace_test_buffer(tr, &count);
690 trace->reset(tr);
691 tracing_start();
693 if (!ret && !count) {
694 printk(KERN_CONT ".. no entries found ..");
695 ret = -1;
696 goto out;
699 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
700 DYN_FTRACE_TEST_NAME);
701 if (ret)
702 goto out;
704 ret = trace_selftest_function_recursion();
705 if (ret)
706 goto out;
708 ret = trace_selftest_function_regs();
709 out:
710 ftrace_enabled = save_ftrace_enabled;
711 tracer_enabled = save_tracer_enabled;
713 /* kill ftrace totally if we failed */
714 if (ret)
715 ftrace_kill();
717 return ret;
719 #endif /* CONFIG_FUNCTION_TRACER */
722 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
724 /* Maximum number of functions to trace before diagnosing a hang */
725 #define GRAPH_MAX_FUNC_TEST 100000000
727 static void
728 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode);
729 static unsigned int graph_hang_thresh;
731 /* Wrap the real function entry probe to avoid possible hanging */
732 static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)
734 /* This is harmlessly racy, we want to approximately detect a hang */
735 if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) {
736 ftrace_graph_stop();
737 printk(KERN_WARNING "BUG: Function graph tracer hang!\n");
738 if (ftrace_dump_on_oops)
739 __ftrace_dump(false, DUMP_ALL);
740 return 0;
743 return trace_graph_entry(trace);
747 * Pretty much the same than for the function tracer from which the selftest
748 * has been borrowed.
751 trace_selftest_startup_function_graph(struct tracer *trace,
752 struct trace_array *tr)
754 int ret;
755 unsigned long count;
758 * Simulate the init() callback but we attach a watchdog callback
759 * to detect and recover from possible hangs
761 tracing_reset_online_cpus(tr);
762 set_graph_array(tr);
763 ret = register_ftrace_graph(&trace_graph_return,
764 &trace_graph_entry_watchdog);
765 if (ret) {
766 warn_failed_init_tracer(trace, ret);
767 goto out;
769 tracing_start_cmdline_record();
771 /* Sleep for a 1/10 of a second */
772 msleep(100);
774 /* Have we just recovered from a hang? */
775 if (graph_hang_thresh > GRAPH_MAX_FUNC_TEST) {
776 tracing_selftest_disabled = true;
777 ret = -1;
778 goto out;
781 tracing_stop();
783 /* check the trace buffer */
784 ret = trace_test_buffer(tr, &count);
786 trace->reset(tr);
787 tracing_start();
789 if (!ret && !count) {
790 printk(KERN_CONT ".. no entries found ..");
791 ret = -1;
792 goto out;
795 /* Don't test dynamic tracing, the function tracer already did */
797 out:
798 /* Stop it if we failed */
799 if (ret)
800 ftrace_graph_stop();
802 return ret;
804 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
807 #ifdef CONFIG_IRQSOFF_TRACER
809 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
811 unsigned long save_max = tracing_max_latency;
812 unsigned long count;
813 int ret;
815 /* start the tracing */
816 ret = tracer_init(trace, tr);
817 if (ret) {
818 warn_failed_init_tracer(trace, ret);
819 return ret;
822 /* reset the max latency */
823 tracing_max_latency = 0;
824 /* disable interrupts for a bit */
825 local_irq_disable();
826 udelay(100);
827 local_irq_enable();
830 * Stop the tracer to avoid a warning subsequent
831 * to buffer flipping failure because tracing_stop()
832 * disables the tr and max buffers, making flipping impossible
833 * in case of parallels max irqs off latencies.
835 trace->stop(tr);
836 /* stop the tracing. */
837 tracing_stop();
838 /* check both trace buffers */
839 ret = trace_test_buffer(tr, NULL);
840 if (!ret)
841 ret = trace_test_buffer(&max_tr, &count);
842 trace->reset(tr);
843 tracing_start();
845 if (!ret && !count) {
846 printk(KERN_CONT ".. no entries found ..");
847 ret = -1;
850 tracing_max_latency = save_max;
852 return ret;
854 #endif /* CONFIG_IRQSOFF_TRACER */
856 #ifdef CONFIG_PREEMPT_TRACER
858 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
860 unsigned long save_max = tracing_max_latency;
861 unsigned long count;
862 int ret;
865 * Now that the big kernel lock is no longer preemptable,
866 * and this is called with the BKL held, it will always
867 * fail. If preemption is already disabled, simply
868 * pass the test. When the BKL is removed, or becomes
869 * preemptible again, we will once again test this,
870 * so keep it in.
872 if (preempt_count()) {
873 printk(KERN_CONT "can not test ... force ");
874 return 0;
877 /* start the tracing */
878 ret = tracer_init(trace, tr);
879 if (ret) {
880 warn_failed_init_tracer(trace, ret);
881 return ret;
884 /* reset the max latency */
885 tracing_max_latency = 0;
886 /* disable preemption for a bit */
887 preempt_disable();
888 udelay(100);
889 preempt_enable();
892 * Stop the tracer to avoid a warning subsequent
893 * to buffer flipping failure because tracing_stop()
894 * disables the tr and max buffers, making flipping impossible
895 * in case of parallels max preempt off latencies.
897 trace->stop(tr);
898 /* stop the tracing. */
899 tracing_stop();
900 /* check both trace buffers */
901 ret = trace_test_buffer(tr, NULL);
902 if (!ret)
903 ret = trace_test_buffer(&max_tr, &count);
904 trace->reset(tr);
905 tracing_start();
907 if (!ret && !count) {
908 printk(KERN_CONT ".. no entries found ..");
909 ret = -1;
912 tracing_max_latency = save_max;
914 return ret;
916 #endif /* CONFIG_PREEMPT_TRACER */
918 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
920 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
922 unsigned long save_max = tracing_max_latency;
923 unsigned long count;
924 int ret;
927 * Now that the big kernel lock is no longer preemptable,
928 * and this is called with the BKL held, it will always
929 * fail. If preemption is already disabled, simply
930 * pass the test. When the BKL is removed, or becomes
931 * preemptible again, we will once again test this,
932 * so keep it in.
934 if (preempt_count()) {
935 printk(KERN_CONT "can not test ... force ");
936 return 0;
939 /* start the tracing */
940 ret = tracer_init(trace, tr);
941 if (ret) {
942 warn_failed_init_tracer(trace, ret);
943 goto out_no_start;
946 /* reset the max latency */
947 tracing_max_latency = 0;
949 /* disable preemption and interrupts for a bit */
950 preempt_disable();
951 local_irq_disable();
952 udelay(100);
953 preempt_enable();
954 /* reverse the order of preempt vs irqs */
955 local_irq_enable();
958 * Stop the tracer to avoid a warning subsequent
959 * to buffer flipping failure because tracing_stop()
960 * disables the tr and max buffers, making flipping impossible
961 * in case of parallels max irqs/preempt off latencies.
963 trace->stop(tr);
964 /* stop the tracing. */
965 tracing_stop();
966 /* check both trace buffers */
967 ret = trace_test_buffer(tr, NULL);
968 if (ret)
969 goto out;
971 ret = trace_test_buffer(&max_tr, &count);
972 if (ret)
973 goto out;
975 if (!ret && !count) {
976 printk(KERN_CONT ".. no entries found ..");
977 ret = -1;
978 goto out;
981 /* do the test by disabling interrupts first this time */
982 tracing_max_latency = 0;
983 tracing_start();
984 trace->start(tr);
986 preempt_disable();
987 local_irq_disable();
988 udelay(100);
989 preempt_enable();
990 /* reverse the order of preempt vs irqs */
991 local_irq_enable();
993 trace->stop(tr);
994 /* stop the tracing. */
995 tracing_stop();
996 /* check both trace buffers */
997 ret = trace_test_buffer(tr, NULL);
998 if (ret)
999 goto out;
1001 ret = trace_test_buffer(&max_tr, &count);
1003 if (!ret && !count) {
1004 printk(KERN_CONT ".. no entries found ..");
1005 ret = -1;
1006 goto out;
1009 out:
1010 tracing_start();
1011 out_no_start:
1012 trace->reset(tr);
1013 tracing_max_latency = save_max;
1015 return ret;
1017 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
1019 #ifdef CONFIG_NOP_TRACER
1021 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
1023 /* What could possibly go wrong? */
1024 return 0;
1026 #endif
1028 #ifdef CONFIG_SCHED_TRACER
1029 static int trace_wakeup_test_thread(void *data)
1031 /* Make this a RT thread, doesn't need to be too high */
1032 static const struct sched_param param = { .sched_priority = 5 };
1033 struct completion *x = data;
1035 sched_setscheduler(current, SCHED_FIFO, &param);
1037 /* Make it know we have a new prio */
1038 complete(x);
1040 /* now go to sleep and let the test wake us up */
1041 set_current_state(TASK_INTERRUPTIBLE);
1042 schedule();
1044 complete(x);
1046 /* we are awake, now wait to disappear */
1047 while (!kthread_should_stop()) {
1049 * This is an RT task, do short sleeps to let
1050 * others run.
1052 msleep(100);
1055 return 0;
1059 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
1061 unsigned long save_max = tracing_max_latency;
1062 struct task_struct *p;
1063 struct completion isrt;
1064 unsigned long count;
1065 int ret;
1067 init_completion(&isrt);
1069 /* create a high prio thread */
1070 p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
1071 if (IS_ERR(p)) {
1072 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
1073 return -1;
1076 /* make sure the thread is running at an RT prio */
1077 wait_for_completion(&isrt);
1079 /* start the tracing */
1080 ret = tracer_init(trace, tr);
1081 if (ret) {
1082 warn_failed_init_tracer(trace, ret);
1083 return ret;
1086 /* reset the max latency */
1087 tracing_max_latency = 0;
1089 while (p->on_rq) {
1091 * Sleep to make sure the RT thread is asleep too.
1092 * On virtual machines we can't rely on timings,
1093 * but we want to make sure this test still works.
1095 msleep(100);
1098 init_completion(&isrt);
1100 wake_up_process(p);
1102 /* Wait for the task to wake up */
1103 wait_for_completion(&isrt);
1105 /* stop the tracing. */
1106 tracing_stop();
1107 /* check both trace buffers */
1108 ret = trace_test_buffer(tr, NULL);
1109 if (!ret)
1110 ret = trace_test_buffer(&max_tr, &count);
1113 trace->reset(tr);
1114 tracing_start();
1116 tracing_max_latency = save_max;
1118 /* kill the thread */
1119 kthread_stop(p);
1121 if (!ret && !count) {
1122 printk(KERN_CONT ".. no entries found ..");
1123 ret = -1;
1126 return ret;
1128 #endif /* CONFIG_SCHED_TRACER */
1130 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
1132 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
1134 unsigned long count;
1135 int ret;
1137 /* start the tracing */
1138 ret = tracer_init(trace, tr);
1139 if (ret) {
1140 warn_failed_init_tracer(trace, ret);
1141 return ret;
1144 /* Sleep for a 1/10 of a second */
1145 msleep(100);
1146 /* stop the tracing. */
1147 tracing_stop();
1148 /* check the trace buffer */
1149 ret = trace_test_buffer(tr, &count);
1150 trace->reset(tr);
1151 tracing_start();
1153 if (!ret && !count) {
1154 printk(KERN_CONT ".. no entries found ..");
1155 ret = -1;
1158 return ret;
1160 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
1162 #ifdef CONFIG_BRANCH_TRACER
1164 trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
1166 unsigned long count;
1167 int ret;
1169 /* start the tracing */
1170 ret = tracer_init(trace, tr);
1171 if (ret) {
1172 warn_failed_init_tracer(trace, ret);
1173 return ret;
1176 /* Sleep for a 1/10 of a second */
1177 msleep(100);
1178 /* stop the tracing. */
1179 tracing_stop();
1180 /* check the trace buffer */
1181 ret = trace_test_buffer(tr, &count);
1182 trace->reset(tr);
1183 tracing_start();
1185 if (!ret && !count) {
1186 printk(KERN_CONT ".. no entries found ..");
1187 ret = -1;
1190 return ret;
1192 #endif /* CONFIG_BRANCH_TRACER */