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)
21 static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
23 struct ring_buffer_event *event;
24 struct trace_entry *entry;
26 while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
27 entry = ring_buffer_event_data(event);
29 if (!trace_valid_entry(entry)) {
30 printk(KERN_CONT ".. invalid entry %d ",
40 printk(KERN_CONT ".. corrupted trace buffer .. ");
45 * Test the trace buffer to see if all the elements
48 static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
50 unsigned long flags, cnt = 0;
53 /* Don't allow flipping of max traces now */
54 raw_local_irq_save(flags);
55 __raw_spin_lock(&ftrace_max_lock);
57 cnt = ring_buffer_entries(tr->buffer);
59 for_each_possible_cpu(cpu) {
60 ret = trace_test_buffer_cpu(tr, cpu);
64 __raw_spin_unlock(&ftrace_max_lock);
65 raw_local_irq_restore(flags);
73 #ifdef CONFIG_FUNCTION_TRACER
75 #ifdef CONFIG_DYNAMIC_FTRACE
78 #define STR(x) __STR(x)
80 /* Test dynamic code modification and ftrace filters */
81 int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
82 struct trace_array *tr,
85 int save_ftrace_enabled = ftrace_enabled;
86 int save_tracer_enabled = tracer_enabled;
91 /* The ftrace test PASSED */
92 printk(KERN_CONT "PASSED\n");
93 pr_info("Testing dynamic ftrace: ");
95 /* enable tracing, and record the filter function */
99 /* passed in by parameter to fool gcc from optimizing */
103 * Some archs *cough*PowerPC*cough* add charachters to the
104 * start of the function names. We simply put a '*' to
107 func_name = "*" STR(DYN_FTRACE_TEST_NAME);
109 /* filter only on our function */
110 ftrace_set_filter(func_name, strlen(func_name), 1);
115 /* Sleep for a 1/10 of a second */
118 /* we should have nothing in the buffer */
119 ret = trace_test_buffer(tr, &count);
125 printk(KERN_CONT ".. filter did not filter .. ");
129 /* call our function again */
135 /* stop the tracing. */
139 /* check the trace buffer */
140 ret = trace_test_buffer(tr, &count);
144 /* we should only have one item */
145 if (!ret && count != 1) {
146 printk(KERN_CONT ".. filter failed count=%ld ..", count);
152 ftrace_enabled = save_ftrace_enabled;
153 tracer_enabled = save_tracer_enabled;
155 /* Enable tracing on all functions again */
156 ftrace_set_filter(NULL, 0, 1);
161 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
162 #endif /* CONFIG_DYNAMIC_FTRACE */
164 * Simple verification test of ftrace function tracer.
165 * Enable ftrace, sleep 1/10 second, and then read the trace
166 * buffer to see if all is in order.
169 trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
171 int save_ftrace_enabled = ftrace_enabled;
172 int save_tracer_enabled = tracer_enabled;
176 /* make sure msleep has been recorded */
179 /* start the tracing */
184 /* Sleep for a 1/10 of a second */
186 /* stop the tracing. */
190 /* check the trace buffer */
191 ret = trace_test_buffer(tr, &count);
195 if (!ret && !count) {
196 printk(KERN_CONT ".. no entries found ..");
201 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
202 DYN_FTRACE_TEST_NAME);
205 ftrace_enabled = save_ftrace_enabled;
206 tracer_enabled = save_tracer_enabled;
208 /* kill ftrace totally if we failed */
214 #endif /* CONFIG_FUNCTION_TRACER */
216 #ifdef CONFIG_IRQSOFF_TRACER
218 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
220 unsigned long save_max = tracing_max_latency;
224 /* start the tracing */
226 /* reset the max latency */
227 tracing_max_latency = 0;
228 /* disable interrupts for a bit */
232 /* stop the tracing. */
234 /* check both trace buffers */
235 ret = trace_test_buffer(tr, NULL);
237 ret = trace_test_buffer(&max_tr, &count);
241 if (!ret && !count) {
242 printk(KERN_CONT ".. no entries found ..");
246 tracing_max_latency = save_max;
250 #endif /* CONFIG_IRQSOFF_TRACER */
252 #ifdef CONFIG_PREEMPT_TRACER
254 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
256 unsigned long save_max = tracing_max_latency;
261 * Now that the big kernel lock is no longer preemptable,
262 * and this is called with the BKL held, it will always
263 * fail. If preemption is already disabled, simply
264 * pass the test. When the BKL is removed, or becomes
265 * preemptible again, we will once again test this,
268 if (preempt_count()) {
269 printk(KERN_CONT "can not test ... force ");
273 /* start the tracing */
275 /* reset the max latency */
276 tracing_max_latency = 0;
277 /* disable preemption for a bit */
281 /* stop the tracing. */
283 /* check both trace buffers */
284 ret = trace_test_buffer(tr, NULL);
286 ret = trace_test_buffer(&max_tr, &count);
290 if (!ret && !count) {
291 printk(KERN_CONT ".. no entries found ..");
295 tracing_max_latency = save_max;
299 #endif /* CONFIG_PREEMPT_TRACER */
301 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
303 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
305 unsigned long save_max = tracing_max_latency;
310 * Now that the big kernel lock is no longer preemptable,
311 * and this is called with the BKL held, it will always
312 * fail. If preemption is already disabled, simply
313 * pass the test. When the BKL is removed, or becomes
314 * preemptible again, we will once again test this,
317 if (preempt_count()) {
318 printk(KERN_CONT "can not test ... force ");
322 /* start the tracing */
325 /* reset the max latency */
326 tracing_max_latency = 0;
328 /* disable preemption and interrupts for a bit */
333 /* reverse the order of preempt vs irqs */
336 /* stop the tracing. */
338 /* check both trace buffers */
339 ret = trace_test_buffer(tr, NULL);
345 ret = trace_test_buffer(&max_tr, &count);
351 if (!ret && !count) {
352 printk(KERN_CONT ".. no entries found ..");
358 /* do the test by disabling interrupts first this time */
359 tracing_max_latency = 0;
365 /* reverse the order of preempt vs irqs */
368 /* stop the tracing. */
370 /* check both trace buffers */
371 ret = trace_test_buffer(tr, NULL);
375 ret = trace_test_buffer(&max_tr, &count);
377 if (!ret && !count) {
378 printk(KERN_CONT ".. no entries found ..");
386 tracing_max_latency = save_max;
390 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
392 #ifdef CONFIG_NOP_TRACER
394 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
396 /* What could possibly go wrong? */
401 #ifdef CONFIG_SCHED_TRACER
402 static int trace_wakeup_test_thread(void *data)
404 /* Make this a RT thread, doesn't need to be too high */
405 struct sched_param param = { .sched_priority = 5 };
406 struct completion *x = data;
408 sched_setscheduler(current, SCHED_FIFO, ¶m);
410 /* Make it know we have a new prio */
413 /* now go to sleep and let the test wake us up */
414 set_current_state(TASK_INTERRUPTIBLE);
417 /* we are awake, now wait to disappear */
418 while (!kthread_should_stop()) {
420 * This is an RT task, do short sleeps to let
430 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
432 unsigned long save_max = tracing_max_latency;
433 struct task_struct *p;
434 struct completion isrt;
438 init_completion(&isrt);
440 /* create a high prio thread */
441 p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
443 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
447 /* make sure the thread is running at an RT prio */
448 wait_for_completion(&isrt);
450 /* start the tracing */
452 /* reset the max latency */
453 tracing_max_latency = 0;
455 /* sleep to let the RT thread sleep too */
459 * Yes this is slightly racy. It is possible that for some
460 * strange reason that the RT thread we created, did not
461 * call schedule for 100ms after doing the completion,
462 * and we do a wakeup on a task that already is awake.
463 * But that is extremely unlikely, and the worst thing that
464 * happens in such a case, is that we disable tracing.
465 * Honestly, if this race does happen something is horrible
466 * wrong with the system.
471 /* give a little time to let the thread wake up */
474 /* stop the tracing. */
476 /* check both trace buffers */
477 ret = trace_test_buffer(tr, NULL);
479 ret = trace_test_buffer(&max_tr, &count);
485 tracing_max_latency = save_max;
487 /* kill the thread */
490 if (!ret && !count) {
491 printk(KERN_CONT ".. no entries found ..");
497 #endif /* CONFIG_SCHED_TRACER */
499 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
501 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
506 /* start the tracing */
508 /* Sleep for a 1/10 of a second */
510 /* stop the tracing. */
512 /* check the trace buffer */
513 ret = trace_test_buffer(tr, &count);
517 if (!ret && !count) {
518 printk(KERN_CONT ".. no entries found ..");
524 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
526 #ifdef CONFIG_SYSPROF_TRACER
528 trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
533 /* start the tracing */
535 /* Sleep for a 1/10 of a second */
537 /* stop the tracing. */
539 /* check the trace buffer */
540 ret = trace_test_buffer(tr, &count);
546 #endif /* CONFIG_SYSPROF_TRACER */