]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/trace/trace_selftest.c
c72e749bcbefc80701b7c5c7053cc454d18b5be8
1 /* Include in trace.c */
3 #include <linux/kthread.h>
4 #include <linux/delay.h>
6 static inline int trace_valid_entry(struct trace_entry
*entry
)
23 static int trace_test_buffer_cpu(struct trace_array
*tr
, int cpu
)
25 struct ring_buffer_event
*event
;
26 struct trace_entry
*entry
;
28 while ((event
= ring_buffer_consume(tr
->buffer
, cpu
, NULL
))) {
29 entry
= ring_buffer_event_data(event
);
31 if (!trace_valid_entry(entry
)) {
32 printk(KERN_CONT
".. invalid entry %d ",
42 printk(KERN_CONT
".. corrupted trace buffer .. ");
47 * Test the trace buffer to see if all the elements
50 static int trace_test_buffer(struct trace_array
*tr
, unsigned long *count
)
52 unsigned long flags
, cnt
= 0;
55 /* Don't allow flipping of max traces now */
56 local_irq_save(flags
);
57 __raw_spin_lock(&ftrace_max_lock
);
59 cnt
= ring_buffer_entries(tr
->buffer
);
61 for_each_possible_cpu(cpu
) {
62 ret
= trace_test_buffer_cpu(tr
, cpu
);
66 __raw_spin_unlock(&ftrace_max_lock
);
67 local_irq_restore(flags
);
75 static inline void warn_failed_init_tracer(struct tracer
*trace
, int init_ret
)
77 printk(KERN_WARNING
"Failed to init %s tracer, init returned %d\n",
78 trace
->name
, init_ret
);
80 #ifdef CONFIG_FUNCTION_TRACER
82 #ifdef CONFIG_DYNAMIC_FTRACE
85 #define STR(x) __STR(x)
87 /* Test dynamic code modification and ftrace filters */
88 int trace_selftest_startup_dynamic_tracing(struct tracer
*trace
,
89 struct trace_array
*tr
,
92 int save_ftrace_enabled
= ftrace_enabled
;
93 int save_tracer_enabled
= tracer_enabled
;
98 /* The ftrace test PASSED */
99 printk(KERN_CONT
"PASSED\n");
100 pr_info("Testing dynamic ftrace: ");
102 /* enable tracing, and record the filter function */
106 /* passed in by parameter to fool gcc from optimizing */
110 * Some archs *cough*PowerPC*cough* add charachters to the
111 * start of the function names. We simply put a '*' to
114 func_name
= "*" STR(DYN_FTRACE_TEST_NAME
);
116 /* filter only on our function */
117 ftrace_set_filter(func_name
, strlen(func_name
), 1);
120 ret
= tracer_init(trace
, tr
);
122 warn_failed_init_tracer(trace
, ret
);
126 /* Sleep for a 1/10 of a second */
129 /* we should have nothing in the buffer */
130 ret
= trace_test_buffer(tr
, &count
);
136 printk(KERN_CONT
".. filter did not filter .. ");
140 /* call our function again */
146 /* stop the tracing. */
150 /* check the trace buffer */
151 ret
= trace_test_buffer(tr
, &count
);
155 /* we should only have one item */
156 if (!ret
&& count
!= 1) {
157 printk(KERN_CONT
".. filter failed count=%ld ..", count
);
163 ftrace_enabled
= save_ftrace_enabled
;
164 tracer_enabled
= save_tracer_enabled
;
166 /* Enable tracing on all functions again */
167 ftrace_set_filter(NULL
, 0, 1);
172 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
173 #endif /* CONFIG_DYNAMIC_FTRACE */
175 * Simple verification test of ftrace function tracer.
176 * Enable ftrace, sleep 1/10 second, and then read the trace
177 * buffer to see if all is in order.
180 trace_selftest_startup_function(struct tracer
*trace
, struct trace_array
*tr
)
182 int save_ftrace_enabled
= ftrace_enabled
;
183 int save_tracer_enabled
= tracer_enabled
;
187 /* make sure msleep has been recorded */
190 /* start the tracing */
194 ret
= tracer_init(trace
, tr
);
196 warn_failed_init_tracer(trace
, ret
);
200 /* Sleep for a 1/10 of a second */
202 /* stop the tracing. */
206 /* check the trace buffer */
207 ret
= trace_test_buffer(tr
, &count
);
211 if (!ret
&& !count
) {
212 printk(KERN_CONT
".. no entries found ..");
217 ret
= trace_selftest_startup_dynamic_tracing(trace
, tr
,
218 DYN_FTRACE_TEST_NAME
);
221 ftrace_enabled
= save_ftrace_enabled
;
222 tracer_enabled
= save_tracer_enabled
;
224 /* kill ftrace totally if we failed */
230 #endif /* CONFIG_FUNCTION_TRACER */
233 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
235 * Pretty much the same than for the function tracer from which the selftest
239 trace_selftest_startup_function_graph(struct tracer
*trace
,
240 struct trace_array
*tr
)
245 ret
= tracer_init(trace
, tr
);
247 warn_failed_init_tracer(trace
, ret
);
251 /* Sleep for a 1/10 of a second */
256 /* check the trace buffer */
257 ret
= trace_test_buffer(tr
, &count
);
262 if (!ret
&& !count
) {
263 printk(KERN_CONT
".. no entries found ..");
268 /* Don't test dynamic tracing, the function tracer already did */
271 /* Stop it if we failed */
277 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
280 #ifdef CONFIG_IRQSOFF_TRACER
282 trace_selftest_startup_irqsoff(struct tracer
*trace
, struct trace_array
*tr
)
284 unsigned long save_max
= tracing_max_latency
;
288 /* start the tracing */
289 ret
= tracer_init(trace
, tr
);
291 warn_failed_init_tracer(trace
, ret
);
295 /* reset the max latency */
296 tracing_max_latency
= 0;
297 /* disable interrupts for a bit */
301 /* stop the tracing. */
303 /* check both trace buffers */
304 ret
= trace_test_buffer(tr
, NULL
);
306 ret
= trace_test_buffer(&max_tr
, &count
);
310 if (!ret
&& !count
) {
311 printk(KERN_CONT
".. no entries found ..");
315 tracing_max_latency
= save_max
;
319 #endif /* CONFIG_IRQSOFF_TRACER */
321 #ifdef CONFIG_PREEMPT_TRACER
323 trace_selftest_startup_preemptoff(struct tracer
*trace
, struct trace_array
*tr
)
325 unsigned long save_max
= tracing_max_latency
;
330 * Now that the big kernel lock is no longer preemptable,
331 * and this is called with the BKL held, it will always
332 * fail. If preemption is already disabled, simply
333 * pass the test. When the BKL is removed, or becomes
334 * preemptible again, we will once again test this,
337 if (preempt_count()) {
338 printk(KERN_CONT
"can not test ... force ");
342 /* start the tracing */
343 ret
= tracer_init(trace
, tr
);
345 warn_failed_init_tracer(trace
, ret
);
349 /* reset the max latency */
350 tracing_max_latency
= 0;
351 /* disable preemption for a bit */
355 /* stop the tracing. */
357 /* check both trace buffers */
358 ret
= trace_test_buffer(tr
, NULL
);
360 ret
= trace_test_buffer(&max_tr
, &count
);
364 if (!ret
&& !count
) {
365 printk(KERN_CONT
".. no entries found ..");
369 tracing_max_latency
= save_max
;
373 #endif /* CONFIG_PREEMPT_TRACER */
375 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
377 trace_selftest_startup_preemptirqsoff(struct tracer
*trace
, struct trace_array
*tr
)
379 unsigned long save_max
= tracing_max_latency
;
384 * Now that the big kernel lock is no longer preemptable,
385 * and this is called with the BKL held, it will always
386 * fail. If preemption is already disabled, simply
387 * pass the test. When the BKL is removed, or becomes
388 * preemptible again, we will once again test this,
391 if (preempt_count()) {
392 printk(KERN_CONT
"can not test ... force ");
396 /* start the tracing */
397 ret
= tracer_init(trace
, tr
);
399 warn_failed_init_tracer(trace
, ret
);
403 /* reset the max latency */
404 tracing_max_latency
= 0;
406 /* disable preemption and interrupts for a bit */
411 /* reverse the order of preempt vs irqs */
414 /* stop the tracing. */
416 /* check both trace buffers */
417 ret
= trace_test_buffer(tr
, NULL
);
423 ret
= trace_test_buffer(&max_tr
, &count
);
429 if (!ret
&& !count
) {
430 printk(KERN_CONT
".. no entries found ..");
436 /* do the test by disabling interrupts first this time */
437 tracing_max_latency
= 0;
443 /* reverse the order of preempt vs irqs */
446 /* stop the tracing. */
448 /* check both trace buffers */
449 ret
= trace_test_buffer(tr
, NULL
);
453 ret
= trace_test_buffer(&max_tr
, &count
);
455 if (!ret
&& !count
) {
456 printk(KERN_CONT
".. no entries found ..");
464 tracing_max_latency
= save_max
;
468 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
470 #ifdef CONFIG_NOP_TRACER
472 trace_selftest_startup_nop(struct tracer
*trace
, struct trace_array
*tr
)
474 /* What could possibly go wrong? */
479 #ifdef CONFIG_SCHED_TRACER
480 static int trace_wakeup_test_thread(void *data
)
482 /* Make this a RT thread, doesn't need to be too high */
483 struct sched_param param
= { .sched_priority
= 5 };
484 struct completion
*x
= data
;
486 sched_setscheduler(current
, SCHED_FIFO
, ¶m
);
488 /* Make it know we have a new prio */
491 /* now go to sleep and let the test wake us up */
492 set_current_state(TASK_INTERRUPTIBLE
);
495 /* we are awake, now wait to disappear */
496 while (!kthread_should_stop()) {
498 * This is an RT task, do short sleeps to let
508 trace_selftest_startup_wakeup(struct tracer
*trace
, struct trace_array
*tr
)
510 unsigned long save_max
= tracing_max_latency
;
511 struct task_struct
*p
;
512 struct completion isrt
;
516 init_completion(&isrt
);
518 /* create a high prio thread */
519 p
= kthread_run(trace_wakeup_test_thread
, &isrt
, "ftrace-test");
521 printk(KERN_CONT
"Failed to create ftrace wakeup test thread ");
525 /* make sure the thread is running at an RT prio */
526 wait_for_completion(&isrt
);
528 /* start the tracing */
529 ret
= tracer_init(trace
, tr
);
531 warn_failed_init_tracer(trace
, ret
);
535 /* reset the max latency */
536 tracing_max_latency
= 0;
538 /* sleep to let the RT thread sleep too */
542 * Yes this is slightly racy. It is possible that for some
543 * strange reason that the RT thread we created, did not
544 * call schedule for 100ms after doing the completion,
545 * and we do a wakeup on a task that already is awake.
546 * But that is extremely unlikely, and the worst thing that
547 * happens in such a case, is that we disable tracing.
548 * Honestly, if this race does happen something is horrible
549 * wrong with the system.
554 /* give a little time to let the thread wake up */
557 /* stop the tracing. */
559 /* check both trace buffers */
560 ret
= trace_test_buffer(tr
, NULL
);
562 ret
= trace_test_buffer(&max_tr
, &count
);
568 tracing_max_latency
= save_max
;
570 /* kill the thread */
573 if (!ret
&& !count
) {
574 printk(KERN_CONT
".. no entries found ..");
580 #endif /* CONFIG_SCHED_TRACER */
582 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
584 trace_selftest_startup_sched_switch(struct tracer
*trace
, struct trace_array
*tr
)
589 /* start the tracing */
590 ret
= tracer_init(trace
, tr
);
592 warn_failed_init_tracer(trace
, ret
);
596 /* Sleep for a 1/10 of a second */
598 /* stop the tracing. */
600 /* check the trace buffer */
601 ret
= trace_test_buffer(tr
, &count
);
605 if (!ret
&& !count
) {
606 printk(KERN_CONT
".. no entries found ..");
612 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
614 #ifdef CONFIG_SYSPROF_TRACER
616 trace_selftest_startup_sysprof(struct tracer
*trace
, struct trace_array
*tr
)
621 /* start the tracing */
622 ret
= tracer_init(trace
, tr
);
624 warn_failed_init_tracer(trace
, ret
);
628 /* Sleep for a 1/10 of a second */
630 /* stop the tracing. */
632 /* check the trace buffer */
633 ret
= trace_test_buffer(tr
, &count
);
637 if (!ret
&& !count
) {
638 printk(KERN_CONT
".. no entries found ..");
644 #endif /* CONFIG_SYSPROF_TRACER */
646 #ifdef CONFIG_BRANCH_TRACER
648 trace_selftest_startup_branch(struct tracer
*trace
, struct trace_array
*tr
)
653 /* start the tracing */
654 ret
= tracer_init(trace
, tr
);
656 warn_failed_init_tracer(trace
, ret
);
660 /* Sleep for a 1/10 of a second */
662 /* stop the tracing. */
664 /* check the trace buffer */
665 ret
= trace_test_buffer(tr
, &count
);
669 if (!ret
&& !count
) {
670 printk(KERN_CONT
".. no entries found ..");
676 #endif /* CONFIG_BRANCH_TRACER */