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);
116 /* Sleep for a 1/10 of a second */
119 /* we should have nothing in the buffer */
120 ret = trace_test_buffer(tr, &count);
126 printk(KERN_CONT ".. filter did not filter .. ");
130 /* call our function again */
136 /* stop the tracing. */
138 trace->ctrl_update(tr);
141 /* check the trace buffer */
142 ret = trace_test_buffer(tr, &count);
145 /* we should only have one item */
146 if (!ret && count != 1) {
147 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 */
185 /* Sleep for a 1/10 of a second */
187 /* stop the tracing. */
189 trace->ctrl_update(tr);
192 /* check the trace buffer */
193 ret = trace_test_buffer(tr, &count);
196 if (!ret && !count) {
197 printk(KERN_CONT ".. no entries found ..");
202 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
203 DYN_FTRACE_TEST_NAME);
206 ftrace_enabled = save_ftrace_enabled;
207 tracer_enabled = save_tracer_enabled;
209 /* kill ftrace totally if we failed */
215 #endif /* CONFIG_FUNCTION_TRACER */
217 #ifdef CONFIG_IRQSOFF_TRACER
219 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
221 unsigned long save_max = tracing_max_latency;
225 /* start the tracing */
228 /* reset the max latency */
229 tracing_max_latency = 0;
230 /* disable interrupts for a bit */
234 /* stop the tracing. */
236 trace->ctrl_update(tr);
237 /* check both trace buffers */
238 ret = trace_test_buffer(tr, NULL);
240 ret = trace_test_buffer(&max_tr, &count);
243 if (!ret && !count) {
244 printk(KERN_CONT ".. no entries found ..");
248 tracing_max_latency = save_max;
252 #endif /* CONFIG_IRQSOFF_TRACER */
254 #ifdef CONFIG_PREEMPT_TRACER
256 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
258 unsigned long save_max = tracing_max_latency;
262 /* start the tracing */
265 /* reset the max latency */
266 tracing_max_latency = 0;
267 /* disable preemption for a bit */
271 /* stop the tracing. */
273 trace->ctrl_update(tr);
274 /* check both trace buffers */
275 ret = trace_test_buffer(tr, NULL);
277 ret = trace_test_buffer(&max_tr, &count);
280 if (!ret && !count) {
281 printk(KERN_CONT ".. no entries found ..");
285 tracing_max_latency = save_max;
289 #endif /* CONFIG_PREEMPT_TRACER */
291 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
293 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
295 unsigned long save_max = tracing_max_latency;
299 /* start the tracing */
303 /* reset the max latency */
304 tracing_max_latency = 0;
306 /* disable preemption and interrupts for a bit */
311 /* reverse the order of preempt vs irqs */
314 /* stop the tracing. */
316 trace->ctrl_update(tr);
317 /* check both trace buffers */
318 ret = trace_test_buffer(tr, NULL);
322 ret = trace_test_buffer(&max_tr, &count);
326 if (!ret && !count) {
327 printk(KERN_CONT ".. no entries found ..");
332 /* do the test by disabling interrupts first this time */
333 tracing_max_latency = 0;
335 trace->ctrl_update(tr);
340 /* reverse the order of preempt vs irqs */
343 /* stop the tracing. */
345 trace->ctrl_update(tr);
346 /* check both trace buffers */
347 ret = trace_test_buffer(tr, NULL);
351 ret = trace_test_buffer(&max_tr, &count);
353 if (!ret && !count) {
354 printk(KERN_CONT ".. no entries found ..");
361 tracing_max_latency = save_max;
365 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
367 #ifdef CONFIG_NOP_TRACER
369 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
371 /* What could possibly go wrong? */
376 #ifdef CONFIG_SCHED_TRACER
377 static int trace_wakeup_test_thread(void *data)
379 /* Make this a RT thread, doesn't need to be too high */
380 struct sched_param param = { .sched_priority = 5 };
381 struct completion *x = data;
383 sched_setscheduler(current, SCHED_FIFO, ¶m);
385 /* Make it know we have a new prio */
388 /* now go to sleep and let the test wake us up */
389 set_current_state(TASK_INTERRUPTIBLE);
392 /* we are awake, now wait to disappear */
393 while (!kthread_should_stop()) {
395 * This is an RT task, do short sleeps to let
405 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
407 unsigned long save_max = tracing_max_latency;
408 struct task_struct *p;
409 struct completion isrt;
413 init_completion(&isrt);
415 /* create a high prio thread */
416 p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
418 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
422 /* make sure the thread is running at an RT prio */
423 wait_for_completion(&isrt);
425 /* start the tracing */
428 /* reset the max latency */
429 tracing_max_latency = 0;
431 /* sleep to let the RT thread sleep too */
435 * Yes this is slightly racy. It is possible that for some
436 * strange reason that the RT thread we created, did not
437 * call schedule for 100ms after doing the completion,
438 * and we do a wakeup on a task that already is awake.
439 * But that is extremely unlikely, and the worst thing that
440 * happens in such a case, is that we disable tracing.
441 * Honestly, if this race does happen something is horrible
442 * wrong with the system.
447 /* give a little time to let the thread wake up */
450 /* stop the tracing. */
452 trace->ctrl_update(tr);
453 /* check both trace buffers */
454 ret = trace_test_buffer(tr, NULL);
456 ret = trace_test_buffer(&max_tr, &count);
461 tracing_max_latency = save_max;
463 /* kill the thread */
466 if (!ret && !count) {
467 printk(KERN_CONT ".. no entries found ..");
473 #endif /* CONFIG_SCHED_TRACER */
475 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
477 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
482 /* start the tracing */
485 /* Sleep for a 1/10 of a second */
487 /* stop the tracing. */
489 trace->ctrl_update(tr);
490 /* check the trace buffer */
491 ret = trace_test_buffer(tr, &count);
494 if (!ret && !count) {
495 printk(KERN_CONT ".. no entries found ..");
501 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
503 #ifdef CONFIG_SYSPROF_TRACER
505 trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
510 /* start the tracing */
513 /* Sleep for a 1/10 of a second */
515 /* stop the tracing. */
517 trace->ctrl_update(tr);
518 /* check the trace buffer */
519 ret = trace_test_buffer(tr, &count);
524 #endif /* CONFIG_SYSPROF_TRACER */