ftrace: remove ctrl_update method
[linux-2.6] / kernel / trace / trace_selftest.c
1 /* Include in trace.c */
2
3 #include <linux/kthread.h>
4 #include <linux/delay.h>
5
6 static inline int trace_valid_entry(struct trace_entry *entry)
7 {
8         switch (entry->type) {
9         case TRACE_FN:
10         case TRACE_CTX:
11         case TRACE_WAKE:
12         case TRACE_CONT:
13         case TRACE_STACK:
14         case TRACE_PRINT:
15         case TRACE_SPECIAL:
16                 return 1;
17         }
18         return 0;
19 }
20
21 static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
22 {
23         struct ring_buffer_event *event;
24         struct trace_entry *entry;
25
26         while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
27                 entry = ring_buffer_event_data(event);
28
29                 if (!trace_valid_entry(entry)) {
30                         printk(KERN_CONT ".. invalid entry %d ",
31                                 entry->type);
32                         goto failed;
33                 }
34         }
35         return 0;
36
37  failed:
38         /* disable tracing */
39         tracing_disabled = 1;
40         printk(KERN_CONT ".. corrupted trace buffer .. ");
41         return -1;
42 }
43
44 /*
45  * Test the trace buffer to see if all the elements
46  * are still sane.
47  */
48 static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
49 {
50         unsigned long flags, cnt = 0;
51         int cpu, ret = 0;
52
53         /* Don't allow flipping of max traces now */
54         raw_local_irq_save(flags);
55         __raw_spin_lock(&ftrace_max_lock);
56
57         cnt = ring_buffer_entries(tr->buffer);
58
59         for_each_possible_cpu(cpu) {
60                 ret = trace_test_buffer_cpu(tr, cpu);
61                 if (ret)
62                         break;
63         }
64         __raw_spin_unlock(&ftrace_max_lock);
65         raw_local_irq_restore(flags);
66
67         if (count)
68                 *count = cnt;
69
70         return ret;
71 }
72
73 #ifdef CONFIG_FUNCTION_TRACER
74
75 #ifdef CONFIG_DYNAMIC_FTRACE
76
77 #define __STR(x) #x
78 #define STR(x) __STR(x)
79
80 /* Test dynamic code modification and ftrace filters */
81 int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
82                                            struct trace_array *tr,
83                                            int (*func)(void))
84 {
85         int save_ftrace_enabled = ftrace_enabled;
86         int save_tracer_enabled = tracer_enabled;
87         unsigned long count;
88         char *func_name;
89         int ret;
90
91         /* The ftrace test PASSED */
92         printk(KERN_CONT "PASSED\n");
93         pr_info("Testing dynamic ftrace: ");
94
95         /* enable tracing, and record the filter function */
96         ftrace_enabled = 1;
97         tracer_enabled = 1;
98
99         /* passed in by parameter to fool gcc from optimizing */
100         func();
101
102         /*
103          * Some archs *cough*PowerPC*cough* add charachters to the
104          * start of the function names. We simply put a '*' to
105          * accomodate them.
106          */
107         func_name = "*" STR(DYN_FTRACE_TEST_NAME);
108
109         /* filter only on our function */
110         ftrace_set_filter(func_name, strlen(func_name), 1);
111
112         /* enable tracing */
113         tr->ctrl = 1;
114         trace->init(tr);
115
116         /* Sleep for a 1/10 of a second */
117         msleep(100);
118
119         /* we should have nothing in the buffer */
120         ret = trace_test_buffer(tr, &count);
121         if (ret)
122                 goto out;
123
124         if (count) {
125                 ret = -1;
126                 printk(KERN_CONT ".. filter did not filter .. ");
127                 goto out;
128         }
129
130         /* call our function again */
131         func();
132
133         /* sleep again */
134         msleep(100);
135
136         /* stop the tracing. */
137         tracing_stop();
138         ftrace_enabled = 0;
139
140         /* check the trace buffer */
141         ret = trace_test_buffer(tr, &count);
142         trace->reset(tr);
143         tracing_start();
144
145         /* we should only have one item */
146         if (!ret && count != 1) {
147                 printk(KERN_CONT ".. filter failed count=%ld ..", count);
148                 ret = -1;
149                 goto out;
150         }
151
152  out:
153         ftrace_enabled = save_ftrace_enabled;
154         tracer_enabled = save_tracer_enabled;
155
156         /* Enable tracing on all functions again */
157         ftrace_set_filter(NULL, 0, 1);
158
159         return ret;
160 }
161 #else
162 # define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
163 #endif /* CONFIG_DYNAMIC_FTRACE */
164 /*
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.
168  */
169 int
170 trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
171 {
172         int save_ftrace_enabled = ftrace_enabled;
173         int save_tracer_enabled = tracer_enabled;
174         unsigned long count;
175         int ret;
176
177         /* make sure msleep has been recorded */
178         msleep(1);
179
180         /* start the tracing */
181         ftrace_enabled = 1;
182         tracer_enabled = 1;
183
184         tr->ctrl = 1;
185         trace->init(tr);
186         /* Sleep for a 1/10 of a second */
187         msleep(100);
188         /* stop the tracing. */
189         tracing_stop();
190         ftrace_enabled = 0;
191
192         /* check the trace buffer */
193         ret = trace_test_buffer(tr, &count);
194         trace->reset(tr);
195         tracing_start();
196
197         if (!ret && !count) {
198                 printk(KERN_CONT ".. no entries found ..");
199                 ret = -1;
200                 goto out;
201         }
202
203         ret = trace_selftest_startup_dynamic_tracing(trace, tr,
204                                                      DYN_FTRACE_TEST_NAME);
205
206  out:
207         ftrace_enabled = save_ftrace_enabled;
208         tracer_enabled = save_tracer_enabled;
209
210         /* kill ftrace totally if we failed */
211         if (ret)
212                 ftrace_kill();
213
214         return ret;
215 }
216 #endif /* CONFIG_FUNCTION_TRACER */
217
218 #ifdef CONFIG_IRQSOFF_TRACER
219 int
220 trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
221 {
222         unsigned long save_max = tracing_max_latency;
223         unsigned long count;
224         int ret;
225
226         /* start the tracing */
227         tr->ctrl = 1;
228         trace->init(tr);
229         /* reset the max latency */
230         tracing_max_latency = 0;
231         /* disable interrupts for a bit */
232         local_irq_disable();
233         udelay(100);
234         local_irq_enable();
235         /* stop the tracing. */
236         tracing_stop();
237         /* check both trace buffers */
238         ret = trace_test_buffer(tr, NULL);
239         if (!ret)
240                 ret = trace_test_buffer(&max_tr, &count);
241         trace->reset(tr);
242         tracing_start();
243
244         if (!ret && !count) {
245                 printk(KERN_CONT ".. no entries found ..");
246                 ret = -1;
247         }
248
249         tracing_max_latency = save_max;
250
251         return ret;
252 }
253 #endif /* CONFIG_IRQSOFF_TRACER */
254
255 #ifdef CONFIG_PREEMPT_TRACER
256 int
257 trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
258 {
259         unsigned long save_max = tracing_max_latency;
260         unsigned long count;
261         int ret;
262
263         /* start the tracing */
264         tr->ctrl = 1;
265         trace->init(tr);
266         /* reset the max latency */
267         tracing_max_latency = 0;
268         /* disable preemption for a bit */
269         preempt_disable();
270         udelay(100);
271         preempt_enable();
272         /* stop the tracing. */
273         tracing_stop();
274         /* check both trace buffers */
275         ret = trace_test_buffer(tr, NULL);
276         if (!ret)
277                 ret = trace_test_buffer(&max_tr, &count);
278         trace->reset(tr);
279         tracing_start();
280
281         if (!ret && !count) {
282                 printk(KERN_CONT ".. no entries found ..");
283                 ret = -1;
284         }
285
286         tracing_max_latency = save_max;
287
288         return ret;
289 }
290 #endif /* CONFIG_PREEMPT_TRACER */
291
292 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
293 int
294 trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
295 {
296         unsigned long save_max = tracing_max_latency;
297         unsigned long count;
298         int ret;
299
300         /* start the tracing */
301         tr->ctrl = 1;
302         trace->init(tr);
303
304         /* reset the max latency */
305         tracing_max_latency = 0;
306
307         /* disable preemption and interrupts for a bit */
308         preempt_disable();
309         local_irq_disable();
310         udelay(100);
311         preempt_enable();
312         /* reverse the order of preempt vs irqs */
313         local_irq_enable();
314
315         /* stop the tracing. */
316         tracing_stop();
317         /* check both trace buffers */
318         ret = trace_test_buffer(tr, NULL);
319         if (ret) {
320                 tracing_start();
321                 goto out;
322         }
323
324         ret = trace_test_buffer(&max_tr, &count);
325         if (ret) {
326                 tracing_start();
327                 goto out;
328         }
329
330         if (!ret && !count) {
331                 printk(KERN_CONT ".. no entries found ..");
332                 ret = -1;
333                 tracing_start();
334                 goto out;
335         }
336
337         /* do the test by disabling interrupts first this time */
338         tracing_max_latency = 0;
339         tracing_start();
340         preempt_disable();
341         local_irq_disable();
342         udelay(100);
343         preempt_enable();
344         /* reverse the order of preempt vs irqs */
345         local_irq_enable();
346
347         /* stop the tracing. */
348         tracing_stop();
349         /* check both trace buffers */
350         ret = trace_test_buffer(tr, NULL);
351         if (ret)
352                 goto out;
353
354         ret = trace_test_buffer(&max_tr, &count);
355
356         if (!ret && !count) {
357                 printk(KERN_CONT ".. no entries found ..");
358                 ret = -1;
359                 goto out;
360         }
361
362  out:
363         trace->reset(tr);
364         tracing_start();
365         tracing_max_latency = save_max;
366
367         return ret;
368 }
369 #endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */
370
371 #ifdef CONFIG_NOP_TRACER
372 int
373 trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
374 {
375         /* What could possibly go wrong? */
376         return 0;
377 }
378 #endif
379
380 #ifdef CONFIG_SCHED_TRACER
381 static int trace_wakeup_test_thread(void *data)
382 {
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;
386
387         sched_setscheduler(current, SCHED_FIFO, &param);
388
389         /* Make it know we have a new prio */
390         complete(x);
391
392         /* now go to sleep and let the test wake us up */
393         set_current_state(TASK_INTERRUPTIBLE);
394         schedule();
395
396         /* we are awake, now wait to disappear */
397         while (!kthread_should_stop()) {
398                 /*
399                  * This is an RT task, do short sleeps to let
400                  * others run.
401                  */
402                 msleep(100);
403         }
404
405         return 0;
406 }
407
408 int
409 trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
410 {
411         unsigned long save_max = tracing_max_latency;
412         struct task_struct *p;
413         struct completion isrt;
414         unsigned long count;
415         int ret;
416
417         init_completion(&isrt);
418
419         /* create a high prio thread */
420         p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
421         if (IS_ERR(p)) {
422                 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
423                 return -1;
424         }
425
426         /* make sure the thread is running at an RT prio */
427         wait_for_completion(&isrt);
428
429         /* start the tracing */
430         tr->ctrl = 1;
431         trace->init(tr);
432         /* reset the max latency */
433         tracing_max_latency = 0;
434
435         /* sleep to let the RT thread sleep too */
436         msleep(100);
437
438         /*
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.
447          */
448
449         wake_up_process(p);
450
451         /* give a little time to let the thread wake up */
452         msleep(100);
453
454         /* stop the tracing. */
455         tracing_stop();
456         /* check both trace buffers */
457         ret = trace_test_buffer(tr, NULL);
458         if (!ret)
459                 ret = trace_test_buffer(&max_tr, &count);
460
461
462         trace->reset(tr);
463         tracing_start();
464
465         tracing_max_latency = save_max;
466
467         /* kill the thread */
468         kthread_stop(p);
469
470         if (!ret && !count) {
471                 printk(KERN_CONT ".. no entries found ..");
472                 ret = -1;
473         }
474
475         return ret;
476 }
477 #endif /* CONFIG_SCHED_TRACER */
478
479 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
480 int
481 trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
482 {
483         unsigned long count;
484         int ret;
485
486         /* start the tracing */
487         tr->ctrl = 1;
488         trace->init(tr);
489         /* Sleep for a 1/10 of a second */
490         msleep(100);
491         /* stop the tracing. */
492         tracing_stop();
493         /* check the trace buffer */
494         ret = trace_test_buffer(tr, &count);
495         trace->reset(tr);
496         tracing_start();
497
498         if (!ret && !count) {
499                 printk(KERN_CONT ".. no entries found ..");
500                 ret = -1;
501         }
502
503         return ret;
504 }
505 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
506
507 #ifdef CONFIG_SYSPROF_TRACER
508 int
509 trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
510 {
511         unsigned long count;
512         int ret;
513
514         /* start the tracing */
515         tr->ctrl = 1;
516         trace->init(tr);
517         /* Sleep for a 1/10 of a second */
518         msleep(100);
519         /* stop the tracing. */
520         tracing_stop();
521         /* check the trace buffer */
522         ret = trace_test_buffer(tr, &count);
523         trace->reset(tr);
524         tracing_start();
525
526         return ret;
527 }
528 #endif /* CONFIG_SYSPROF_TRACER */