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);
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 */
102 /* update the records */
103 ret = ftrace_force_update();
105 printk(KERN_CONT ".. ftraced failed .. ");
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);
123 /* Sleep for a 1/10 of a second */
126 /* we should have nothing in the buffer */
127 ret = trace_test_buffer(tr, &count);
133 printk(KERN_CONT ".. filter did not filter .. ");
137 /* call our function again */
143 /* stop the tracing. */
145 trace->ctrl_update(tr);
148 /* check the trace buffer */
149 ret = trace_test_buffer(tr, &count);
152 /* we should only have one item */
153 if (!ret && count != 1) {
154 printk(KERN_CONT ".. filter failed count=%ld ..", count);
159 ftrace_enabled = save_ftrace_enabled;
160 tracer_enabled = save_tracer_enabled;
162 /* Enable tracing on all functions again */
163 ftrace_set_filter(NULL, 0, 1);
168 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
169 #endif /* CONFIG_DYNAMIC_FTRACE */
171 * Simple verification test of ftrace function tracer.
172 * Enable ftrace, sleep 1/10 second, and then read the trace
173 * buffer to see if all is in order.
176 trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
178 int save_ftrace_enabled = ftrace_enabled;
179 int save_tracer_enabled = tracer_enabled;
183 /* make sure msleep has been recorded */
186 /* force the recorded functions to be traced */
187 ret = ftrace_force_update();
189 printk(KERN_CONT ".. ftraced failed .. ");
193 /* start the tracing */
199 /* Sleep for a 1/10 of a second */
201 /* stop the tracing. */
203 trace->ctrl_update(tr);
206 /* check the trace buffer */
207 ret = trace_test_buffer(tr, &count);
210 if (!ret && !count) {
211 printk(KERN_CONT ".. no entries found ..");
216 ret = trace_selftest_startup_dynamic_tracing(trace, tr,
217 DYN_FTRACE_TEST_NAME);
220 ftrace_enabled = save_ftrace_enabled;
221 tracer_enabled = save_tracer_enabled;
223 /* kill ftrace totally if we failed */
229 #endif /* CONFIG_FTRACE */
231 #ifdef CONFIG_IRQSOFF_TRACER
233 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
235 unsigned long save_max = tracing_max_latency;
239 /* start the tracing */
242 /* reset the max latency */
243 tracing_max_latency = 0;
244 /* disable interrupts for a bit */
248 /* stop the tracing. */
250 trace->ctrl_update(tr);
251 /* check both trace buffers */
252 ret = trace_test_buffer(tr, NULL);
254 ret = trace_test_buffer(&max_tr, &count);
257 if (!ret && !count) {
258 printk(KERN_CONT ".. no entries found ..");
262 tracing_max_latency = save_max;
266 #endif /* CONFIG_IRQSOFF_TRACER */
268 #ifdef CONFIG_PREEMPT_TRACER
270 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
272 unsigned long save_max = tracing_max_latency;
276 /* start the tracing */
279 /* reset the max latency */
280 tracing_max_latency = 0;
281 /* disable preemption for a bit */
285 /* stop the tracing. */
287 trace->ctrl_update(tr);
288 /* check both trace buffers */
289 ret = trace_test_buffer(tr, NULL);
291 ret = trace_test_buffer(&max_tr, &count);
294 if (!ret && !count) {
295 printk(KERN_CONT ".. no entries found ..");
299 tracing_max_latency = save_max;
303 #endif /* CONFIG_PREEMPT_TRACER */
305 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
307 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
309 unsigned long save_max = tracing_max_latency;
313 /* start the tracing */
317 /* reset the max latency */
318 tracing_max_latency = 0;
320 /* disable preemption and interrupts for a bit */
325 /* reverse the order of preempt vs irqs */
328 /* stop the tracing. */
330 trace->ctrl_update(tr);
331 /* check both trace buffers */
332 ret = trace_test_buffer(tr, NULL);
336 ret = trace_test_buffer(&max_tr, &count);
340 if (!ret && !count) {
341 printk(KERN_CONT ".. no entries found ..");
346 /* do the test by disabling interrupts first this time */
347 tracing_max_latency = 0;
349 trace->ctrl_update(tr);
354 /* reverse the order of preempt vs irqs */
357 /* stop the tracing. */
359 trace->ctrl_update(tr);
360 /* check both trace buffers */
361 ret = trace_test_buffer(tr, NULL);
365 ret = trace_test_buffer(&max_tr, &count);
367 if (!ret && !count) {
368 printk(KERN_CONT ".. no entries found ..");
375 tracing_max_latency = save_max;
379 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
381 #ifdef CONFIG_NOP_TRACER
383 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
385 /* What could possibly go wrong? */
390 #ifdef CONFIG_SCHED_TRACER
391 static int trace_wakeup_test_thread(void *data)
393 /* Make this a RT thread, doesn't need to be too high */
394 struct sched_param param = { .sched_priority = 5 };
395 struct completion *x = data;
397 sched_setscheduler(current, SCHED_FIFO, ¶m);
399 /* Make it know we have a new prio */
402 /* now go to sleep and let the test wake us up */
403 set_current_state(TASK_INTERRUPTIBLE);
406 /* we are awake, now wait to disappear */
407 while (!kthread_should_stop()) {
409 * This is an RT task, do short sleeps to let
419 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
421 unsigned long save_max = tracing_max_latency;
422 struct task_struct *p;
423 struct completion isrt;
427 init_completion(&isrt);
429 /* create a high prio thread */
430 p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
432 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
436 /* make sure the thread is running at an RT prio */
437 wait_for_completion(&isrt);
439 /* start the tracing */
442 /* reset the max latency */
443 tracing_max_latency = 0;
445 /* sleep to let the RT thread sleep too */
449 * Yes this is slightly racy. It is possible that for some
450 * strange reason that the RT thread we created, did not
451 * call schedule for 100ms after doing the completion,
452 * and we do a wakeup on a task that already is awake.
453 * But that is extremely unlikely, and the worst thing that
454 * happens in such a case, is that we disable tracing.
455 * Honestly, if this race does happen something is horrible
456 * wrong with the system.
461 /* give a little time to let the thread wake up */
464 /* stop the tracing. */
466 trace->ctrl_update(tr);
467 /* check both trace buffers */
468 ret = trace_test_buffer(tr, NULL);
470 ret = trace_test_buffer(&max_tr, &count);
475 tracing_max_latency = save_max;
477 /* kill the thread */
480 if (!ret && !count) {
481 printk(KERN_CONT ".. no entries found ..");
487 #endif /* CONFIG_SCHED_TRACER */
489 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
491 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
496 /* start the tracing */
499 /* Sleep for a 1/10 of a second */
501 /* stop the tracing. */
503 trace->ctrl_update(tr);
504 /* check the trace buffer */
505 ret = trace_test_buffer(tr, &count);
508 if (!ret && !count) {
509 printk(KERN_CONT ".. no entries found ..");
515 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
517 #ifdef CONFIG_SYSPROF_TRACER
519 trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
524 /* start the tracing */
527 /* Sleep for a 1/10 of a second */
529 /* stop the tracing. */
531 trace->ctrl_update(tr);
532 /* check the trace buffer */
533 ret = trace_test_buffer(tr, &count);
538 #endif /* CONFIG_SYSPROF_TRACER */