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. */
140 /* check the trace buffer */
141 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);
153 ftrace_enabled = save_ftrace_enabled;
154 tracer_enabled = save_tracer_enabled;
156 /* Enable tracing on all functions again */
157 ftrace_set_filter(NULL, 0, 1);
162 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
163 #endif /* CONFIG_DYNAMIC_FTRACE */
165 * Simple verification test of ftrace function tracer.
166 * Enable ftrace, sleep 1/10 second, and then read the trace
167 * buffer to see if all is in order.
170 trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
172 int save_ftrace_enabled = ftrace_enabled;
173 int save_tracer_enabled = tracer_enabled;
177 /* make sure msleep has been recorded */
180 /* start the tracing */
186 /* Sleep for a 1/10 of a second */
188 /* stop the tracing. */
192 /* check the trace buffer */
193 ret = trace_test_buffer(tr, &count);
197 if (!ret && !count) {
198 printk(KERN_CONT ".. no entries found ..");
203 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
204 DYN_FTRACE_TEST_NAME);
207 ftrace_enabled = save_ftrace_enabled;
208 tracer_enabled = save_tracer_enabled;
210 /* kill ftrace totally if we failed */
216 #endif /* CONFIG_FUNCTION_TRACER */
218 #ifdef CONFIG_IRQSOFF_TRACER
220 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
222 unsigned long save_max = tracing_max_latency;
226 /* start the tracing */
229 /* reset the max latency */
230 tracing_max_latency = 0;
231 /* disable interrupts for a bit */
235 /* stop the tracing. */
237 /* check both trace buffers */
238 ret = trace_test_buffer(tr, NULL);
240 ret = trace_test_buffer(&max_tr, &count);
244 if (!ret && !count) {
245 printk(KERN_CONT ".. no entries found ..");
249 tracing_max_latency = save_max;
253 #endif /* CONFIG_IRQSOFF_TRACER */
255 #ifdef CONFIG_PREEMPT_TRACER
257 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
259 unsigned long save_max = tracing_max_latency;
263 /* start the tracing */
266 /* reset the max latency */
267 tracing_max_latency = 0;
268 /* disable preemption for a bit */
272 /* stop the tracing. */
274 /* check both trace buffers */
275 ret = trace_test_buffer(tr, NULL);
277 ret = trace_test_buffer(&max_tr, &count);
281 if (!ret && !count) {
282 printk(KERN_CONT ".. no entries found ..");
286 tracing_max_latency = save_max;
290 #endif /* CONFIG_PREEMPT_TRACER */
292 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
294 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
296 unsigned long save_max = tracing_max_latency;
300 /* start the tracing */
304 /* reset the max latency */
305 tracing_max_latency = 0;
307 /* disable preemption and interrupts for a bit */
312 /* reverse the order of preempt vs irqs */
315 /* stop the tracing. */
317 /* check both trace buffers */
318 ret = trace_test_buffer(tr, NULL);
324 ret = trace_test_buffer(&max_tr, &count);
330 if (!ret && !count) {
331 printk(KERN_CONT ".. no entries found ..");
337 /* do the test by disabling interrupts first this time */
338 tracing_max_latency = 0;
344 /* reverse the order of preempt vs irqs */
347 /* stop the tracing. */
349 /* check both trace buffers */
350 ret = trace_test_buffer(tr, NULL);
354 ret = trace_test_buffer(&max_tr, &count);
356 if (!ret && !count) {
357 printk(KERN_CONT ".. no entries found ..");
365 tracing_max_latency = save_max;
369 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
371 #ifdef CONFIG_NOP_TRACER
373 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
375 /* What could possibly go wrong? */
380 #ifdef CONFIG_SCHED_TRACER
381 static int trace_wakeup_test_thread(void *data)
383 /* Make this a RT thread, doesn't need to be too high */
384 struct sched_param param = { .sched_priority = 5 };
385 struct completion *x = data;
387 sched_setscheduler(current, SCHED_FIFO, ¶m);
389 /* Make it know we have a new prio */
392 /* now go to sleep and let the test wake us up */
393 set_current_state(TASK_INTERRUPTIBLE);
396 /* we are awake, now wait to disappear */
397 while (!kthread_should_stop()) {
399 * This is an RT task, do short sleeps to let
409 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
411 unsigned long save_max = tracing_max_latency;
412 struct task_struct *p;
413 struct completion isrt;
417 init_completion(&isrt);
419 /* create a high prio thread */
420 p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
422 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
426 /* make sure the thread is running at an RT prio */
427 wait_for_completion(&isrt);
429 /* start the tracing */
432 /* reset the max latency */
433 tracing_max_latency = 0;
435 /* sleep to let the RT thread sleep too */
439 * Yes this is slightly racy. It is possible that for some
440 * strange reason that the RT thread we created, did not
441 * call schedule for 100ms after doing the completion,
442 * and we do a wakeup on a task that already is awake.
443 * But that is extremely unlikely, and the worst thing that
444 * happens in such a case, is that we disable tracing.
445 * Honestly, if this race does happen something is horrible
446 * wrong with the system.
451 /* give a little time to let the thread wake up */
454 /* stop the tracing. */
456 /* check both trace buffers */
457 ret = trace_test_buffer(tr, NULL);
459 ret = trace_test_buffer(&max_tr, &count);
465 tracing_max_latency = save_max;
467 /* kill the thread */
470 if (!ret && !count) {
471 printk(KERN_CONT ".. no entries found ..");
477 #endif /* CONFIG_SCHED_TRACER */
479 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
481 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
486 /* start the tracing */
489 /* Sleep for a 1/10 of a second */
491 /* stop the tracing. */
493 /* check the trace buffer */
494 ret = trace_test_buffer(tr, &count);
498 if (!ret && !count) {
499 printk(KERN_CONT ".. no entries found ..");
505 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
507 #ifdef CONFIG_SYSPROF_TRACER
509 trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
514 /* start the tracing */
517 /* Sleep for a 1/10 of a second */
519 /* stop the tracing. */
521 /* check the trace buffer */
522 ret = trace_test_buffer(tr, &count);
528 #endif /* CONFIG_SYSPROF_TRACER */