ftrace: fix time offset
[deliverable/linux.git] / kernel / trace / trace.c
1 /*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/gfp.h>
28 #include <linux/fs.h>
29
30 #include "trace.h"
31
32 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly tracing_thresh;
34
35 static int tracing_disabled = 1;
36
37 static long notrace
38 ns2usecs(cycle_t nsec)
39 {
40 nsec += 500;
41 do_div(nsec, 1000);
42 return nsec;
43 }
44
45 static atomic_t tracer_counter;
46 static struct trace_array global_trace;
47
48 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
49
50 static struct trace_array max_tr;
51
52 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
53
54 static int tracer_enabled;
55 static unsigned long trace_nr_entries = 16384UL;
56
57 static struct tracer *trace_types __read_mostly;
58 static struct tracer *current_trace __read_mostly;
59 static int max_tracer_type_len;
60
61 static DEFINE_MUTEX(trace_types_lock);
62
63 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
64
65 static int __init set_nr_entries(char *str)
66 {
67 if (!str)
68 return 0;
69 trace_nr_entries = simple_strtoul(str, &str, 0);
70 return 1;
71 }
72 __setup("trace_entries=", set_nr_entries);
73
74 unsigned long nsecs_to_usecs(unsigned long nsecs)
75 {
76 return nsecs / 1000;
77 }
78
79 enum trace_type {
80 __TRACE_FIRST_TYPE = 0,
81
82 TRACE_FN,
83 TRACE_CTX,
84
85 __TRACE_LAST_TYPE
86 };
87
88 enum trace_flag_type {
89 TRACE_FLAG_IRQS_OFF = 0x01,
90 TRACE_FLAG_NEED_RESCHED = 0x02,
91 TRACE_FLAG_HARDIRQ = 0x04,
92 TRACE_FLAG_SOFTIRQ = 0x08,
93 };
94
95 enum trace_iterator_flags {
96 TRACE_ITER_PRINT_PARENT = 0x01,
97 TRACE_ITER_SYM_OFFSET = 0x02,
98 TRACE_ITER_SYM_ADDR = 0x04,
99 TRACE_ITER_VERBOSE = 0x08,
100 };
101
102 #define TRACE_ITER_SYM_MASK \
103 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
104
105 /* These must match the bit postions above */
106 static const char *trace_options[] = {
107 "print-parent",
108 "sym-offset",
109 "sym-addr",
110 "verbose",
111 NULL
112 };
113
114 static unsigned trace_flags;
115
116 static DEFINE_SPINLOCK(ftrace_max_lock);
117
118 /*
119 * Copy the new maximum trace into the separate maximum-trace
120 * structure. (this way the maximum trace is permanently saved,
121 * for later retrieval via /debugfs/tracing/latency_trace)
122 */
123 static notrace void
124 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
125 {
126 struct trace_array_cpu *data = tr->data[cpu];
127
128 max_tr.cpu = cpu;
129 max_tr.time_start = data->preempt_timestamp;
130
131 data = max_tr.data[cpu];
132 data->saved_latency = tracing_max_latency;
133
134 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
135 data->pid = tsk->pid;
136 data->uid = tsk->uid;
137 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
138 data->policy = tsk->policy;
139 data->rt_priority = tsk->rt_priority;
140
141 /* record this tasks comm */
142 tracing_record_cmdline(current);
143 }
144
145 void check_pages(struct trace_array_cpu *data)
146 {
147 struct page *page, *tmp;
148
149 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
150 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
151
152 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
153 BUG_ON(page->lru.next->prev != &page->lru);
154 BUG_ON(page->lru.prev->next != &page->lru);
155 }
156 }
157
158 void *head_page(struct trace_array_cpu *data)
159 {
160 struct page *page;
161
162 check_pages(data);
163 if (list_empty(&data->trace_pages))
164 return NULL;
165
166 page = list_entry(data->trace_pages.next, struct page, lru);
167 BUG_ON(&page->lru == &data->trace_pages);
168
169 return page_address(page);
170 }
171
172 notrace static void
173 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
174 {
175 struct list_head flip_pages;
176
177 INIT_LIST_HEAD(&flip_pages);
178
179 tr1->trace_current = NULL;
180 memcpy(&tr1->trace_current_idx, &tr2->trace_current_idx,
181 sizeof(struct trace_array_cpu) -
182 offsetof(struct trace_array_cpu, trace_current_idx));
183
184 check_pages(tr1);
185 check_pages(tr2);
186 list_splice_init(&tr1->trace_pages, &flip_pages);
187 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
188 list_splice_init(&flip_pages, &tr2->trace_pages);
189 BUG_ON(!list_empty(&flip_pages));
190 check_pages(tr1);
191 check_pages(tr2);
192 }
193
194 notrace void
195 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
196 {
197 struct trace_array_cpu *data;
198 int i;
199
200 WARN_ON_ONCE(!irqs_disabled());
201 spin_lock(&ftrace_max_lock);
202 /* clear out all the previous traces */
203 for_each_possible_cpu(i) {
204 data = tr->data[i];
205 flip_trace(max_tr.data[i], data);
206 tracing_reset(data);
207 }
208
209 __update_max_tr(tr, tsk, cpu);
210 spin_unlock(&ftrace_max_lock);
211 }
212
213 /**
214 * update_max_tr_single - only copy one trace over, and reset the rest
215 * @tr - tracer
216 * @tsk - task with the latency
217 * @cpu - the cpu of the buffer to copy.
218 */
219 notrace void
220 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
221 {
222 struct trace_array_cpu *data = tr->data[cpu];
223 int i;
224
225 WARN_ON_ONCE(!irqs_disabled());
226 spin_lock(&ftrace_max_lock);
227 for_each_possible_cpu(i)
228 tracing_reset(max_tr.data[i]);
229
230 flip_trace(max_tr.data[cpu], data);
231
232 tracing_reset(data);
233
234 __update_max_tr(tr, tsk, cpu);
235 spin_unlock(&ftrace_max_lock);
236 }
237
238 int register_tracer(struct tracer *type)
239 {
240 struct tracer *t;
241 int len;
242 int ret = 0;
243
244 if (!type->name) {
245 pr_info("Tracer must have a name\n");
246 return -1;
247 }
248
249 mutex_lock(&trace_types_lock);
250 for (t = trace_types; t; t = t->next) {
251 if (strcmp(type->name, t->name) == 0) {
252 /* already found */
253 pr_info("Trace %s already registered\n",
254 type->name);
255 ret = -1;
256 goto out;
257 }
258 }
259
260 #ifdef CONFIG_FTRACE_STARTUP_TEST
261 if (type->selftest) {
262 struct tracer *saved_tracer = current_trace;
263 struct trace_array_cpu *data;
264 struct trace_array *tr = &global_trace;
265 int saved_ctrl = tr->ctrl;
266 int i;
267 /*
268 * Run a selftest on this tracer.
269 * Here we reset the trace buffer, and set the current
270 * tracer to be this tracer. The tracer can then run some
271 * internal tracing to verify that everything is in order.
272 * If we fail, we do not register this tracer.
273 */
274 for_each_possible_cpu(i) {
275 data = tr->data[i];
276 if (!head_page(data))
277 continue;
278 tracing_reset(data);
279 }
280 current_trace = type;
281 tr->ctrl = 0;
282 /* the test is responsible for initializing and enabling */
283 pr_info("Testing tracer %s: ", type->name);
284 ret = type->selftest(type, tr);
285 /* the test is responsible for resetting too */
286 current_trace = saved_tracer;
287 tr->ctrl = saved_ctrl;
288 if (ret) {
289 printk(KERN_CONT "FAILED!\n");
290 goto out;
291 }
292 printk(KERN_CONT "PASSED\n");
293 }
294 #endif
295
296 type->next = trace_types;
297 trace_types = type;
298 len = strlen(type->name);
299 if (len > max_tracer_type_len)
300 max_tracer_type_len = len;
301
302 out:
303 mutex_unlock(&trace_types_lock);
304
305 return ret;
306 }
307
308 void unregister_tracer(struct tracer *type)
309 {
310 struct tracer **t;
311 int len;
312
313 mutex_lock(&trace_types_lock);
314 for (t = &trace_types; *t; t = &(*t)->next) {
315 if (*t == type)
316 goto found;
317 }
318 pr_info("Trace %s not registered\n", type->name);
319 goto out;
320
321 found:
322 *t = (*t)->next;
323 if (strlen(type->name) != max_tracer_type_len)
324 goto out;
325
326 max_tracer_type_len = 0;
327 for (t = &trace_types; *t; t = &(*t)->next) {
328 len = strlen((*t)->name);
329 if (len > max_tracer_type_len)
330 max_tracer_type_len = len;
331 }
332 out:
333 mutex_unlock(&trace_types_lock);
334 }
335
336 notrace void tracing_reset(struct trace_array_cpu *data)
337 {
338 data->trace_idx = 0;
339 data->trace_current = head_page(data);
340 data->trace_current_idx = 0;
341 data->time_offset = 0;
342 }
343
344 #ifdef CONFIG_FTRACE
345 static notrace void
346 function_trace_call(unsigned long ip, unsigned long parent_ip)
347 {
348 struct trace_array *tr = &global_trace;
349 struct trace_array_cpu *data;
350 unsigned long flags;
351 long disabled;
352 int cpu;
353
354 if (unlikely(!tracer_enabled))
355 return;
356
357 local_irq_save(flags);
358 cpu = raw_smp_processor_id();
359 data = tr->data[cpu];
360 disabled = atomic_inc_return(&data->disabled);
361
362 if (likely(disabled == 1))
363 ftrace(tr, data, ip, parent_ip, flags);
364
365 atomic_dec(&data->disabled);
366 local_irq_restore(flags);
367 }
368
369 static struct ftrace_ops trace_ops __read_mostly =
370 {
371 .func = function_trace_call,
372 };
373 #endif
374
375 notrace void tracing_start_function_trace(void)
376 {
377 register_ftrace_function(&trace_ops);
378 }
379
380 notrace void tracing_stop_function_trace(void)
381 {
382 unregister_ftrace_function(&trace_ops);
383 }
384
385 #define SAVED_CMDLINES 128
386 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
387 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
388 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
389 static int cmdline_idx;
390 static DEFINE_SPINLOCK(trace_cmdline_lock);
391 atomic_t trace_record_cmdline_disabled;
392
393 static void trace_init_cmdlines(void)
394 {
395 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
396 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
397 cmdline_idx = 0;
398 }
399
400 notrace void trace_stop_cmdline_recording(void);
401
402 static notrace void trace_save_cmdline(struct task_struct *tsk)
403 {
404 unsigned map;
405 unsigned idx;
406
407 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
408 return;
409
410 /*
411 * It's not the end of the world if we don't get
412 * the lock, but we also don't want to spin
413 * nor do we want to disable interrupts,
414 * so if we miss here, then better luck next time.
415 */
416 if (!spin_trylock(&trace_cmdline_lock))
417 return;
418
419 idx = map_pid_to_cmdline[tsk->pid];
420 if (idx >= SAVED_CMDLINES) {
421 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
422
423 map = map_cmdline_to_pid[idx];
424 if (map <= PID_MAX_DEFAULT)
425 map_pid_to_cmdline[map] = (unsigned)-1;
426
427 map_pid_to_cmdline[tsk->pid] = idx;
428
429 cmdline_idx = idx;
430 }
431
432 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
433
434 spin_unlock(&trace_cmdline_lock);
435 }
436
437 static notrace char *trace_find_cmdline(int pid)
438 {
439 char *cmdline = "<...>";
440 unsigned map;
441
442 if (!pid)
443 return "<idle>";
444
445 if (pid > PID_MAX_DEFAULT)
446 goto out;
447
448 map = map_pid_to_cmdline[pid];
449 if (map >= SAVED_CMDLINES)
450 goto out;
451
452 cmdline = saved_cmdlines[map];
453
454 out:
455 return cmdline;
456 }
457
458 notrace void tracing_record_cmdline(struct task_struct *tsk)
459 {
460 if (atomic_read(&trace_record_cmdline_disabled))
461 return;
462
463 trace_save_cmdline(tsk);
464 }
465
466 static inline notrace struct trace_entry *
467 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
468 {
469 unsigned long idx, idx_next;
470 struct trace_entry *entry;
471 struct list_head *next;
472 struct page *page;
473
474 data->trace_idx++;
475 idx = data->trace_current_idx;
476 idx_next = idx + 1;
477
478 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
479
480 entry = data->trace_current + idx * TRACE_ENTRY_SIZE;
481
482 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
483 page = virt_to_page(data->trace_current);
484 /*
485 * Roundrobin - but skip the head (which is not a real page):
486 */
487 next = page->lru.next;
488 if (unlikely(next == &data->trace_pages))
489 next = next->next;
490 BUG_ON(next == &data->trace_pages);
491
492 page = list_entry(next, struct page, lru);
493 data->trace_current = page_address(page);
494 idx_next = 0;
495 }
496
497 data->trace_current_idx = idx_next;
498
499 return entry;
500 }
501
502 static inline notrace void
503 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
504 {
505 struct task_struct *tsk = current;
506 unsigned long pc;
507
508 pc = preempt_count();
509
510 entry->idx = atomic_inc_return(&tracer_counter);
511 entry->preempt_count = pc & 0xff;
512 entry->pid = tsk->pid;
513 entry->t = now(raw_smp_processor_id());
514 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
515 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
516 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
517 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
518 }
519
520 notrace void
521 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
522 unsigned long ip, unsigned long parent_ip, unsigned long flags)
523 {
524 struct trace_entry *entry;
525
526 entry = tracing_get_trace_entry(tr, data);
527 tracing_generic_entry_update(entry, flags);
528 entry->type = TRACE_FN;
529 entry->fn.ip = ip;
530 entry->fn.parent_ip = parent_ip;
531 }
532
533 notrace void
534 tracing_sched_switch_trace(struct trace_array *tr,
535 struct trace_array_cpu *data,
536 struct task_struct *prev, struct task_struct *next,
537 unsigned long flags)
538 {
539 struct trace_entry *entry;
540
541 entry = tracing_get_trace_entry(tr, data);
542 tracing_generic_entry_update(entry, flags);
543 entry->type = TRACE_CTX;
544 entry->ctx.prev_pid = prev->pid;
545 entry->ctx.prev_prio = prev->prio;
546 entry->ctx.prev_state = prev->state;
547 entry->ctx.next_pid = next->pid;
548 entry->ctx.next_prio = next->prio;
549 }
550
551 enum trace_file_type {
552 TRACE_FILE_LAT_FMT = 1,
553 };
554
555 static struct trace_entry *
556 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
557 struct trace_iterator *iter, int cpu)
558 {
559 struct page *page;
560 struct trace_entry *array;
561
562 if (iter->next_idx[cpu] >= tr->entries ||
563 iter->next_idx[cpu] >= data->trace_idx)
564 return NULL;
565
566 if (!iter->next_page[cpu]) {
567 /*
568 * Initialize. If the count of elements in
569 * this buffer is greater than the max entries
570 * we had an underrun. Which means we looped around.
571 * We can simply use the current pointer as our
572 * starting point.
573 */
574 if (data->trace_idx >= tr->entries) {
575 page = virt_to_page(data->trace_current);
576 iter->next_page[cpu] = &page->lru;
577 iter->next_page_idx[cpu] = data->trace_current_idx;
578 } else {
579 iter->next_page[cpu] = data->trace_pages.next;
580 iter->next_page_idx[cpu] = 0;
581 }
582 }
583
584 page = list_entry(iter->next_page[cpu], struct page, lru);
585 BUG_ON(&data->trace_pages == &page->lru);
586
587 array = page_address(page);
588
589 return &array[iter->next_page_idx[cpu]];
590 }
591
592 static struct notrace trace_entry *
593 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
594 {
595 struct trace_array *tr = iter->tr;
596 struct trace_entry *ent, *next = NULL;
597 int next_cpu = -1;
598 int cpu;
599
600 for_each_possible_cpu(cpu) {
601 if (!head_page(tr->data[cpu]))
602 continue;
603 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
604 if (ent &&
605 (!next || (long)(next->idx - ent->idx) > 0)) {
606 next = ent;
607 next_cpu = cpu;
608 }
609 }
610
611 if (ent_cpu)
612 *ent_cpu = next_cpu;
613
614 return next;
615 }
616
617 static void *find_next_entry_inc(struct trace_iterator *iter)
618 {
619 struct trace_entry *next;
620 int next_cpu = -1;
621
622 next = find_next_entry(iter, &next_cpu);
623
624 if (next) {
625 iter->idx++;
626 iter->next_idx[next_cpu]++;
627 iter->next_page_idx[next_cpu]++;
628
629 if (iter->next_page_idx[next_cpu] >= ENTRIES_PER_PAGE) {
630 struct trace_array_cpu *data = iter->tr->data[next_cpu];
631
632 iter->next_page_idx[next_cpu] = 0;
633 iter->next_page[next_cpu] =
634 iter->next_page[next_cpu]->next;
635 if (iter->next_page[next_cpu] == &data->trace_pages)
636 iter->next_page[next_cpu] =
637 data->trace_pages.next;
638 }
639 }
640 iter->prev_ent = iter->ent;
641 iter->prev_cpu = iter->cpu;
642
643 iter->ent = next;
644 iter->cpu = next_cpu;
645
646 return next ? iter : NULL;
647 }
648
649 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
650 {
651 struct trace_iterator *iter = m->private;
652 void *last_ent = iter->ent;
653 int i = (int)*pos;
654 void *ent;
655
656 (*pos)++;
657
658 /* can't go backwards */
659 if (iter->idx > i)
660 return NULL;
661
662 if (iter->idx < 0)
663 ent = find_next_entry_inc(iter);
664 else
665 ent = iter;
666
667 while (ent && iter->idx < i)
668 ent = find_next_entry_inc(iter);
669
670 iter->pos = *pos;
671
672 if (last_ent && !ent)
673 seq_puts(m, "\n\nvim:ft=help\n");
674
675 return ent;
676 }
677
678 static void *s_start(struct seq_file *m, loff_t *pos)
679 {
680 struct trace_iterator *iter = m->private;
681 void *p = NULL;
682 loff_t l = 0;
683 int i;
684
685 mutex_lock(&trace_types_lock);
686
687 if (!current_trace || current_trace != iter->trace)
688 return NULL;
689
690 atomic_inc(&trace_record_cmdline_disabled);
691
692 /* let the tracer grab locks here if needed */
693 if (current_trace->start)
694 current_trace->start(iter);
695
696 if (*pos != iter->pos) {
697 iter->ent = NULL;
698 iter->cpu = 0;
699 iter->idx = -1;
700 iter->prev_ent = NULL;
701 iter->prev_cpu = -1;
702
703 for_each_possible_cpu(i) {
704 iter->next_idx[i] = 0;
705 iter->next_page[i] = NULL;
706 }
707
708 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
709 ;
710
711 } else {
712 l = *pos - 1;
713 p = s_next(m, p, &l);
714 }
715
716 return p;
717 }
718
719 static void s_stop(struct seq_file *m, void *p)
720 {
721 struct trace_iterator *iter = m->private;
722
723 atomic_dec(&trace_record_cmdline_disabled);
724
725 /* let the tracer release locks here if needed */
726 if (current_trace && current_trace == iter->trace && iter->trace->stop)
727 iter->trace->stop(iter);
728
729 mutex_unlock(&trace_types_lock);
730 }
731
732 static void
733 seq_print_sym_short(struct seq_file *m, const char *fmt, unsigned long address)
734 {
735 #ifdef CONFIG_KALLSYMS
736 char str[KSYM_SYMBOL_LEN];
737
738 kallsyms_lookup(address, NULL, NULL, NULL, str);
739
740 seq_printf(m, fmt, str);
741 #endif
742 }
743
744 static void
745 seq_print_sym_offset(struct seq_file *m, const char *fmt, unsigned long address)
746 {
747 #ifdef CONFIG_KALLSYMS
748 char str[KSYM_SYMBOL_LEN];
749
750 sprint_symbol(str, address);
751 seq_printf(m, fmt, str);
752 #endif
753 }
754
755 #ifndef CONFIG_64BIT
756 # define IP_FMT "%08lx"
757 #else
758 # define IP_FMT "%016lx"
759 #endif
760
761 static notrace void
762 seq_print_ip_sym(struct seq_file *m, unsigned long ip, unsigned long sym_flags)
763 {
764 if (!ip) {
765 seq_printf(m, "0");
766 return;
767 }
768
769 if (sym_flags & TRACE_ITER_SYM_OFFSET)
770 seq_print_sym_offset(m, "%s", ip);
771 else
772 seq_print_sym_short(m, "%s", ip);
773
774 if (sym_flags & TRACE_ITER_SYM_ADDR)
775 seq_printf(m, " <" IP_FMT ">", ip);
776 }
777
778 static notrace void print_lat_help_header(struct seq_file *m)
779 {
780 seq_puts(m, "# _------=> CPU# \n");
781 seq_puts(m, "# / _-----=> irqs-off \n");
782 seq_puts(m, "# | / _----=> need-resched \n");
783 seq_puts(m, "# || / _---=> hardirq/softirq \n");
784 seq_puts(m, "# ||| / _--=> preempt-depth \n");
785 seq_puts(m, "# |||| / \n");
786 seq_puts(m, "# ||||| delay \n");
787 seq_puts(m, "# cmd pid ||||| time | caller \n");
788 seq_puts(m, "# \\ / ||||| \\ | / \n");
789 }
790
791 static notrace void print_func_help_header(struct seq_file *m)
792 {
793 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
794 seq_puts(m, "# | | | | |\n");
795 }
796
797
798 static notrace void
799 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
800 {
801 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
802 struct trace_array *tr = iter->tr;
803 struct trace_array_cpu *data = tr->data[tr->cpu];
804 struct tracer *type = current_trace;
805 unsigned long total = 0;
806 unsigned long entries = 0;
807 int cpu;
808 const char *name = "preemption";
809
810 if (type)
811 name = type->name;
812
813 for_each_possible_cpu(cpu) {
814 if (head_page(tr->data[cpu])) {
815 total += tr->data[cpu]->trace_idx;
816 if (tr->data[cpu]->trace_idx > tr->entries)
817 entries += tr->entries;
818 else
819 entries += tr->data[cpu]->trace_idx;
820 }
821 }
822
823 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
824 name, UTS_RELEASE);
825 seq_puts(m, "-----------------------------------"
826 "---------------------------------\n");
827 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
828 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
829 nsecs_to_usecs(data->saved_latency),
830 entries,
831 total,
832 tr->cpu,
833 #if defined(CONFIG_PREEMPT_NONE)
834 "server",
835 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
836 "desktop",
837 #elif defined(CONFIG_PREEMPT_DESKTOP)
838 "preempt",
839 #else
840 "unknown",
841 #endif
842 /* These are reserved for later use */
843 0, 0, 0, 0);
844 #ifdef CONFIG_SMP
845 seq_printf(m, " #P:%d)\n", num_online_cpus());
846 #else
847 seq_puts(m, ")\n");
848 #endif
849 seq_puts(m, " -----------------\n");
850 seq_printf(m, " | task: %.16s-%d "
851 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
852 data->comm, data->pid, data->uid, data->nice,
853 data->policy, data->rt_priority);
854 seq_puts(m, " -----------------\n");
855
856 if (data->critical_start) {
857 seq_puts(m, " => started at: ");
858 seq_print_ip_sym(m, data->critical_start, sym_flags);
859 seq_puts(m, "\n => ended at: ");
860 seq_print_ip_sym(m, data->critical_end, sym_flags);
861 seq_puts(m, "\n");
862 }
863
864 seq_puts(m, "\n");
865 }
866
867 static notrace void
868 lat_print_generic(struct seq_file *m, struct trace_entry *entry, int cpu)
869 {
870 int hardirq, softirq;
871 char *comm;
872
873 comm = trace_find_cmdline(entry->pid);
874
875 seq_printf(m, "%8.8s-%-5d ", comm, entry->pid);
876 seq_printf(m, "%d", cpu);
877 seq_printf(m, "%c%c",
878 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
879 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
880
881 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
882 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
883 if (hardirq && softirq)
884 seq_putc(m, 'H');
885 else {
886 if (hardirq)
887 seq_putc(m, 'h');
888 else {
889 if (softirq)
890 seq_putc(m, 's');
891 else
892 seq_putc(m, '.');
893 }
894 }
895
896 if (entry->preempt_count)
897 seq_printf(m, "%x", entry->preempt_count);
898 else
899 seq_puts(m, ".");
900 }
901
902 unsigned long preempt_mark_thresh = 100;
903
904 static notrace void
905 lat_print_timestamp(struct seq_file *m, unsigned long long abs_usecs,
906 unsigned long rel_usecs)
907 {
908 seq_printf(m, " %4lldus", abs_usecs);
909 if (rel_usecs > preempt_mark_thresh)
910 seq_puts(m, "!: ");
911 else if (rel_usecs > 1)
912 seq_puts(m, "+: ");
913 else
914 seq_puts(m, " : ");
915 }
916
917 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
918
919 static notrace void
920 print_lat_fmt(struct seq_file *m, struct trace_iterator *iter,
921 unsigned int trace_idx, int cpu)
922 {
923 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
924 struct trace_entry *next_entry = find_next_entry(iter, NULL);
925 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
926 struct trace_entry *entry = iter->ent;
927 unsigned long abs_usecs;
928 unsigned long rel_usecs;
929 char *comm;
930 int S;
931
932 if (!next_entry)
933 next_entry = entry;
934 rel_usecs = ns2usecs(next_entry->t - entry->t);
935 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
936
937 if (verbose) {
938 comm = trace_find_cmdline(entry->pid);
939 seq_printf(m, "%16s %5d %d %d %08x %08x [%08lx]"
940 " %ld.%03ldms (+%ld.%03ldms): ",
941 comm,
942 entry->pid, cpu, entry->flags,
943 entry->preempt_count, trace_idx,
944 ns2usecs(entry->t),
945 abs_usecs/1000,
946 abs_usecs % 1000, rel_usecs/1000, rel_usecs % 1000);
947 } else {
948 lat_print_generic(m, entry, cpu);
949 lat_print_timestamp(m, abs_usecs, rel_usecs);
950 }
951 switch (entry->type) {
952 case TRACE_FN:
953 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
954 seq_puts(m, " (");
955 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
956 seq_puts(m, ")\n");
957 break;
958 case TRACE_CTX:
959 S = entry->ctx.prev_state < sizeof(state_to_char) ?
960 state_to_char[entry->ctx.prev_state] : 'X';
961 comm = trace_find_cmdline(entry->ctx.next_pid);
962 seq_printf(m, " %d:%d:%c --> %d:%d %s\n",
963 entry->ctx.prev_pid,
964 entry->ctx.prev_prio,
965 S,
966 entry->ctx.next_pid,
967 entry->ctx.next_prio,
968 comm);
969 break;
970 default:
971 seq_printf(m, "Unknown type %d\n", entry->type);
972 }
973 }
974
975 static notrace void sync_time_offset(struct trace_iterator *iter)
976 {
977 struct trace_array_cpu *prev_array, *array;
978 struct trace_entry *prev_entry, *entry;
979 cycle_t prev_t, t;
980
981 entry = iter->ent;
982 prev_entry = iter->prev_ent;
983 if (!prev_entry)
984 return;
985
986 prev_array = iter->tr->data[iter->prev_cpu];
987 array = iter->tr->data[iter->cpu];
988
989 prev_t = prev_entry->t + prev_array->time_offset;
990 t = entry->t + array->time_offset;
991
992 /*
993 * If time goes backwards we increase the offset of
994 * the current array, to not have observable time warps.
995 * This will quickly synchronize the time offsets of
996 * multiple CPUs:
997 */
998 if (t < prev_t)
999 array->time_offset += prev_t - t;
1000 }
1001
1002 static notrace void
1003 print_trace_fmt(struct seq_file *m, struct trace_iterator *iter)
1004 {
1005 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1006 struct trace_entry *entry;
1007 unsigned long usec_rem;
1008 unsigned long long t;
1009 unsigned long secs;
1010 char *comm;
1011 int S;
1012
1013 sync_time_offset(iter);
1014 entry = iter->ent;
1015
1016 comm = trace_find_cmdline(iter->ent->pid);
1017
1018 t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1019 usec_rem = do_div(t, 1000000ULL);
1020 secs = (unsigned long)t;
1021
1022 seq_printf(m, "%16s-%-5d ", comm, entry->pid);
1023 seq_printf(m, "[%02d] ", iter->cpu);
1024 seq_printf(m, "%5lu.%06lu: ", secs, usec_rem);
1025
1026 switch (entry->type) {
1027 case TRACE_FN:
1028 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
1029 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1030 entry->fn.parent_ip) {
1031 seq_printf(m, " <-");
1032 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
1033 }
1034 seq_printf(m, "\n");
1035 break;
1036 case TRACE_CTX:
1037 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1038 state_to_char[entry->ctx.prev_state] : 'X';
1039 seq_printf(m, " %d:%d:%c ==> %d:%d\n",
1040 entry->ctx.prev_pid,
1041 entry->ctx.prev_prio,
1042 S,
1043 entry->ctx.next_pid,
1044 entry->ctx.next_prio);
1045 break;
1046 }
1047 }
1048
1049 static int trace_empty(struct trace_iterator *iter)
1050 {
1051 struct trace_array_cpu *data;
1052 int cpu;
1053
1054 for_each_possible_cpu(cpu) {
1055 data = iter->tr->data[cpu];
1056
1057 if (head_page(data) && data->trace_idx)
1058 return 0;
1059 }
1060 return 1;
1061 }
1062
1063 static int s_show(struct seq_file *m, void *v)
1064 {
1065 struct trace_iterator *iter = v;
1066
1067 if (iter->ent == NULL) {
1068 if (iter->tr) {
1069 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1070 seq_puts(m, "#\n");
1071 }
1072 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1073 /* print nothing if the buffers are empty */
1074 if (trace_empty(iter))
1075 return 0;
1076 print_trace_header(m, iter);
1077 if (!(trace_flags & TRACE_ITER_VERBOSE))
1078 print_lat_help_header(m);
1079 } else {
1080 if (!(trace_flags & TRACE_ITER_VERBOSE))
1081 print_func_help_header(m);
1082 }
1083 } else {
1084 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1085 print_lat_fmt(m, iter, iter->idx, iter->cpu);
1086 else
1087 print_trace_fmt(m, iter);
1088 }
1089
1090 return 0;
1091 }
1092
1093 static struct seq_operations tracer_seq_ops = {
1094 .start = s_start,
1095 .next = s_next,
1096 .stop = s_stop,
1097 .show = s_show,
1098 };
1099
1100 static struct trace_iterator notrace *
1101 __tracing_open(struct inode *inode, struct file *file, int *ret)
1102 {
1103 struct trace_iterator *iter;
1104
1105 if (tracing_disabled) {
1106 *ret = -ENODEV;
1107 return NULL;
1108 }
1109
1110 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1111 if (!iter) {
1112 *ret = -ENOMEM;
1113 goto out;
1114 }
1115
1116 mutex_lock(&trace_types_lock);
1117 if (current_trace && current_trace->print_max)
1118 iter->tr = &max_tr;
1119 else
1120 iter->tr = inode->i_private;
1121 iter->trace = current_trace;
1122 iter->pos = -1;
1123
1124 /* TODO stop tracer */
1125 *ret = seq_open(file, &tracer_seq_ops);
1126 if (!*ret) {
1127 struct seq_file *m = file->private_data;
1128 m->private = iter;
1129
1130 /* stop the trace while dumping */
1131 if (iter->tr->ctrl)
1132 tracer_enabled = 0;
1133
1134 if (iter->trace && iter->trace->open)
1135 iter->trace->open(iter);
1136 } else {
1137 kfree(iter);
1138 iter = NULL;
1139 }
1140 mutex_unlock(&trace_types_lock);
1141
1142 out:
1143 return iter;
1144 }
1145
1146 int tracing_open_generic(struct inode *inode, struct file *filp)
1147 {
1148 if (tracing_disabled)
1149 return -ENODEV;
1150
1151 filp->private_data = inode->i_private;
1152 return 0;
1153 }
1154
1155 int tracing_release(struct inode *inode, struct file *file)
1156 {
1157 struct seq_file *m = (struct seq_file *)file->private_data;
1158 struct trace_iterator *iter = m->private;
1159
1160 mutex_lock(&trace_types_lock);
1161 if (iter->trace && iter->trace->close)
1162 iter->trace->close(iter);
1163
1164 /* reenable tracing if it was previously enabled */
1165 if (iter->tr->ctrl)
1166 tracer_enabled = 1;
1167 mutex_unlock(&trace_types_lock);
1168
1169 seq_release(inode, file);
1170 kfree(iter);
1171 return 0;
1172 }
1173
1174 static int tracing_open(struct inode *inode, struct file *file)
1175 {
1176 int ret;
1177
1178 __tracing_open(inode, file, &ret);
1179
1180 return ret;
1181 }
1182
1183 static int tracing_lt_open(struct inode *inode, struct file *file)
1184 {
1185 struct trace_iterator *iter;
1186 int ret;
1187
1188 iter = __tracing_open(inode, file, &ret);
1189
1190 if (!ret)
1191 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1192
1193 return ret;
1194 }
1195
1196
1197 static notrace void *
1198 t_next(struct seq_file *m, void *v, loff_t *pos)
1199 {
1200 struct tracer *t = m->private;
1201
1202 (*pos)++;
1203
1204 if (t)
1205 t = t->next;
1206
1207 m->private = t;
1208
1209 return t;
1210 }
1211
1212 static void *t_start(struct seq_file *m, loff_t *pos)
1213 {
1214 struct tracer *t = m->private;
1215 loff_t l = 0;
1216
1217 mutex_lock(&trace_types_lock);
1218 for (; t && l < *pos; t = t_next(m, t, &l))
1219 ;
1220
1221 return t;
1222 }
1223
1224 static void t_stop(struct seq_file *m, void *p)
1225 {
1226 mutex_unlock(&trace_types_lock);
1227 }
1228
1229 static int t_show(struct seq_file *m, void *v)
1230 {
1231 struct tracer *t = v;
1232
1233 if (!t)
1234 return 0;
1235
1236 seq_printf(m, "%s", t->name);
1237 if (t->next)
1238 seq_putc(m, ' ');
1239 else
1240 seq_putc(m, '\n');
1241
1242 return 0;
1243 }
1244
1245 static struct seq_operations show_traces_seq_ops = {
1246 .start = t_start,
1247 .next = t_next,
1248 .stop = t_stop,
1249 .show = t_show,
1250 };
1251
1252 static int show_traces_open(struct inode *inode, struct file *file)
1253 {
1254 int ret;
1255
1256 if (tracing_disabled)
1257 return -ENODEV;
1258
1259 ret = seq_open(file, &show_traces_seq_ops);
1260 if (!ret) {
1261 struct seq_file *m = file->private_data;
1262 m->private = trace_types;
1263 }
1264
1265 return ret;
1266 }
1267
1268 static struct file_operations tracing_fops = {
1269 .open = tracing_open,
1270 .read = seq_read,
1271 .llseek = seq_lseek,
1272 .release = tracing_release,
1273 };
1274
1275 static struct file_operations tracing_lt_fops = {
1276 .open = tracing_lt_open,
1277 .read = seq_read,
1278 .llseek = seq_lseek,
1279 .release = tracing_release,
1280 };
1281
1282 static struct file_operations show_traces_fops = {
1283 .open = show_traces_open,
1284 .read = seq_read,
1285 .release = seq_release,
1286 };
1287
1288 static ssize_t
1289 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1290 size_t cnt, loff_t *ppos)
1291 {
1292 char *buf;
1293 int r = 0;
1294 int len = 0;
1295 int i;
1296
1297 /* calulate max size */
1298 for (i = 0; trace_options[i]; i++) {
1299 len += strlen(trace_options[i]);
1300 len += 3; /* "no" and space */
1301 }
1302
1303 /* +2 for \n and \0 */
1304 buf = kmalloc(len + 2, GFP_KERNEL);
1305 if (!buf)
1306 return -ENOMEM;
1307
1308 for (i = 0; trace_options[i]; i++) {
1309 if (trace_flags & (1 << i))
1310 r += sprintf(buf + r, "%s ", trace_options[i]);
1311 else
1312 r += sprintf(buf + r, "no%s ", trace_options[i]);
1313 }
1314
1315 r += sprintf(buf + r, "\n");
1316 WARN_ON(r >= len + 2);
1317
1318 r = simple_read_from_buffer(ubuf, cnt, ppos,
1319 buf, r);
1320
1321 kfree(buf);
1322
1323 return r;
1324 }
1325
1326 static ssize_t
1327 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1328 size_t cnt, loff_t *ppos)
1329 {
1330 char buf[64];
1331 char *cmp = buf;
1332 int neg = 0;
1333 int i;
1334
1335 if (cnt > 63)
1336 cnt = 63;
1337
1338 if (copy_from_user(&buf, ubuf, cnt))
1339 return -EFAULT;
1340
1341 buf[cnt] = 0;
1342
1343 if (strncmp(buf, "no", 2) == 0) {
1344 neg = 1;
1345 cmp += 2;
1346 }
1347
1348 for (i = 0; trace_options[i]; i++) {
1349 int len = strlen(trace_options[i]);
1350
1351 if (strncmp(cmp, trace_options[i], len) == 0) {
1352 if (neg)
1353 trace_flags &= ~(1 << i);
1354 else
1355 trace_flags |= (1 << i);
1356 break;
1357 }
1358 }
1359
1360 filp->f_pos += cnt;
1361
1362 return cnt;
1363 }
1364
1365 static struct file_operations tracing_iter_fops = {
1366 .open = tracing_open_generic,
1367 .read = tracing_iter_ctrl_read,
1368 .write = tracing_iter_ctrl_write,
1369 };
1370
1371 static const char readme_msg[] =
1372 "tracing mini-HOWTO:\n\n"
1373 "# mkdir /debug\n"
1374 "# mount -t debugfs nodev /debug\n\n"
1375 "# cat /debug/tracing/available_tracers\n"
1376 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1377 "# cat /debug/tracing/current_tracer\n"
1378 "none\n"
1379 "# echo sched_switch > /debug/tracing/current_tracer\n"
1380 "# cat /debug/tracing/current_tracer\n"
1381 "sched_switch\n"
1382 "# cat /debug/tracing/iter_ctrl\n"
1383 "noprint-parent nosym-offset nosym-addr noverbose\n"
1384 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1385 "# echo 1 > /debug/tracing/tracing_enabled\n"
1386 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1387 "echo 0 > /debug/tracing/tracing_enabled\n"
1388 ;
1389
1390 static ssize_t
1391 tracing_readme_read(struct file *filp, char __user *ubuf,
1392 size_t cnt, loff_t *ppos)
1393 {
1394 return simple_read_from_buffer(ubuf, cnt, ppos,
1395 readme_msg, strlen(readme_msg));
1396 }
1397
1398 static struct file_operations tracing_readme_fops = {
1399 .open = tracing_open_generic,
1400 .read = tracing_readme_read,
1401 };
1402
1403
1404 static ssize_t
1405 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1406 size_t cnt, loff_t *ppos)
1407 {
1408 struct trace_array *tr = filp->private_data;
1409 char buf[64];
1410 int r;
1411
1412 r = sprintf(buf, "%ld\n", tr->ctrl);
1413 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1414 }
1415
1416 static ssize_t
1417 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1418 size_t cnt, loff_t *ppos)
1419 {
1420 struct trace_array *tr = filp->private_data;
1421 long val;
1422 char buf[64];
1423
1424 if (cnt > 63)
1425 cnt = 63;
1426
1427 if (copy_from_user(&buf, ubuf, cnt))
1428 return -EFAULT;
1429
1430 buf[cnt] = 0;
1431
1432 val = simple_strtoul(buf, NULL, 10);
1433
1434 val = !!val;
1435
1436 mutex_lock(&trace_types_lock);
1437 if (tr->ctrl ^ val) {
1438 if (val)
1439 tracer_enabled = 1;
1440 else
1441 tracer_enabled = 0;
1442
1443 tr->ctrl = val;
1444
1445 if (current_trace && current_trace->ctrl_update)
1446 current_trace->ctrl_update(tr);
1447 }
1448 mutex_unlock(&trace_types_lock);
1449
1450 filp->f_pos += cnt;
1451
1452 return cnt;
1453 }
1454
1455 static ssize_t
1456 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1457 size_t cnt, loff_t *ppos)
1458 {
1459 char buf[max_tracer_type_len+2];
1460 int r;
1461
1462 mutex_lock(&trace_types_lock);
1463 if (current_trace)
1464 r = sprintf(buf, "%s\n", current_trace->name);
1465 else
1466 r = sprintf(buf, "\n");
1467 mutex_unlock(&trace_types_lock);
1468
1469 return simple_read_from_buffer(ubuf, cnt, ppos,
1470 buf, r);
1471 }
1472
1473 static ssize_t
1474 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1475 size_t cnt, loff_t *ppos)
1476 {
1477 struct trace_array *tr = &global_trace;
1478 struct tracer *t;
1479 char buf[max_tracer_type_len+1];
1480 int i;
1481
1482 if (cnt > max_tracer_type_len)
1483 cnt = max_tracer_type_len;
1484
1485 if (copy_from_user(&buf, ubuf, cnt))
1486 return -EFAULT;
1487
1488 buf[cnt] = 0;
1489
1490 /* strip ending whitespace. */
1491 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1492 buf[i] = 0;
1493
1494 mutex_lock(&trace_types_lock);
1495 for (t = trace_types; t; t = t->next) {
1496 if (strcmp(t->name, buf) == 0)
1497 break;
1498 }
1499 if (!t || t == current_trace)
1500 goto out;
1501
1502 if (current_trace && current_trace->reset)
1503 current_trace->reset(tr);
1504
1505 current_trace = t;
1506 if (t->init)
1507 t->init(tr);
1508
1509 out:
1510 mutex_unlock(&trace_types_lock);
1511
1512 filp->f_pos += cnt;
1513
1514 return cnt;
1515 }
1516
1517 static ssize_t
1518 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1519 size_t cnt, loff_t *ppos)
1520 {
1521 unsigned long *ptr = filp->private_data;
1522 char buf[64];
1523 int r;
1524
1525 r = snprintf(buf, 64, "%ld\n",
1526 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1527 if (r > 64)
1528 r = 64;
1529 return simple_read_from_buffer(ubuf, cnt, ppos,
1530 buf, r);
1531 }
1532
1533 static ssize_t
1534 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1535 size_t cnt, loff_t *ppos)
1536 {
1537 long *ptr = filp->private_data;
1538 long val;
1539 char buf[64];
1540
1541 if (cnt > 63)
1542 cnt = 63;
1543
1544 if (copy_from_user(&buf, ubuf, cnt))
1545 return -EFAULT;
1546
1547 buf[cnt] = 0;
1548
1549 val = simple_strtoul(buf, NULL, 10);
1550
1551 *ptr = val * 1000;
1552
1553 return cnt;
1554 }
1555
1556 static struct file_operations tracing_max_lat_fops = {
1557 .open = tracing_open_generic,
1558 .read = tracing_max_lat_read,
1559 .write = tracing_max_lat_write,
1560 };
1561
1562 static struct file_operations tracing_ctrl_fops = {
1563 .open = tracing_open_generic,
1564 .read = tracing_ctrl_read,
1565 .write = tracing_ctrl_write,
1566 };
1567
1568 static struct file_operations set_tracer_fops = {
1569 .open = tracing_open_generic,
1570 .read = tracing_set_trace_read,
1571 .write = tracing_set_trace_write,
1572 };
1573
1574 #ifdef CONFIG_DYNAMIC_FTRACE
1575
1576 static ssize_t
1577 tracing_read_long(struct file *filp, char __user *ubuf,
1578 size_t cnt, loff_t *ppos)
1579 {
1580 unsigned long *p = filp->private_data;
1581 char buf[64];
1582 int r;
1583
1584 r = sprintf(buf, "%ld\n", *p);
1585 return simple_read_from_buffer(ubuf, cnt, ppos,
1586 buf, r);
1587 }
1588
1589 static struct file_operations tracing_read_long_fops = {
1590 .open = tracing_open_generic,
1591 .read = tracing_read_long,
1592 };
1593 #endif
1594
1595 static struct dentry *d_tracer;
1596
1597 struct dentry *tracing_init_dentry(void)
1598 {
1599 static int once;
1600
1601 if (d_tracer)
1602 return d_tracer;
1603
1604 d_tracer = debugfs_create_dir("tracing", NULL);
1605
1606 if (!d_tracer && !once) {
1607 once = 1;
1608 pr_warning("Could not create debugfs directory 'tracing'\n");
1609 return NULL;
1610 }
1611
1612 return d_tracer;
1613 }
1614
1615 #ifdef CONFIG_FTRACE_SELFTEST
1616 /* Let selftest have access to static functions in this file */
1617 #include "trace_selftest.c"
1618 #endif
1619
1620 static __init void tracer_init_debugfs(void)
1621 {
1622 struct dentry *d_tracer;
1623 struct dentry *entry;
1624
1625 d_tracer = tracing_init_dentry();
1626
1627 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1628 &global_trace, &tracing_ctrl_fops);
1629 if (!entry)
1630 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1631
1632 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1633 NULL, &tracing_iter_fops);
1634 if (!entry)
1635 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1636
1637 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1638 &global_trace, &tracing_lt_fops);
1639 if (!entry)
1640 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1641
1642 entry = debugfs_create_file("trace", 0444, d_tracer,
1643 &global_trace, &tracing_fops);
1644 if (!entry)
1645 pr_warning("Could not create debugfs 'trace' entry\n");
1646
1647 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
1648 &global_trace, &show_traces_fops);
1649 if (!entry)
1650 pr_warning("Could not create debugfs 'trace' entry\n");
1651
1652 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
1653 &global_trace, &set_tracer_fops);
1654 if (!entry)
1655 pr_warning("Could not create debugfs 'trace' entry\n");
1656
1657 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
1658 &tracing_max_latency,
1659 &tracing_max_lat_fops);
1660 if (!entry)
1661 pr_warning("Could not create debugfs "
1662 "'tracing_max_latency' entry\n");
1663
1664 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
1665 &tracing_thresh, &tracing_max_lat_fops);
1666 if (!entry)
1667 pr_warning("Could not create debugfs "
1668 "'tracing_threash' entry\n");
1669 entry = debugfs_create_file("README", 0644, d_tracer,
1670 NULL, &tracing_readme_fops);
1671 if (!entry)
1672 pr_warning("Could not create debugfs 'README' entry\n");
1673
1674
1675 #ifdef CONFIG_DYNAMIC_FTRACE
1676 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
1677 &ftrace_update_tot_cnt,
1678 &tracing_read_long_fops);
1679 if (!entry)
1680 pr_warning("Could not create debugfs "
1681 "'dyn_ftrace_total_info' entry\n");
1682 #endif
1683 }
1684
1685 /* dummy trace to disable tracing */
1686 static struct tracer no_tracer __read_mostly =
1687 {
1688 .name = "none",
1689 };
1690
1691 static int trace_alloc_page(void)
1692 {
1693 struct trace_array_cpu *data;
1694 struct page *page, *tmp;
1695 LIST_HEAD(pages);
1696 void *array;
1697 int i;
1698
1699 /* first allocate a page for each CPU */
1700 for_each_possible_cpu(i) {
1701 array = (void *)__get_free_page(GFP_KERNEL);
1702 if (array == NULL) {
1703 printk(KERN_ERR "tracer: failed to allocate page"
1704 "for trace buffer!\n");
1705 goto free_pages;
1706 }
1707
1708 page = virt_to_page(array);
1709 list_add(&page->lru, &pages);
1710
1711 /* Only allocate if we are actually using the max trace */
1712 #ifdef CONFIG_TRACER_MAX_TRACE
1713 array = (void *)__get_free_page(GFP_KERNEL);
1714 if (array == NULL) {
1715 printk(KERN_ERR "tracer: failed to allocate page"
1716 "for trace buffer!\n");
1717 goto free_pages;
1718 }
1719 page = virt_to_page(array);
1720 list_add(&page->lru, &pages);
1721 #endif
1722 }
1723
1724 /* Now that we successfully allocate a page per CPU, add them */
1725 for_each_possible_cpu(i) {
1726 data = global_trace.data[i];
1727 page = list_entry(pages.next, struct page, lru);
1728 list_del_init(&page->lru);
1729 list_add_tail(&page->lru, &data->trace_pages);
1730 ClearPageLRU(page);
1731
1732 #ifdef CONFIG_TRACER_MAX_TRACE
1733 data = max_tr.data[i];
1734 page = list_entry(pages.next, struct page, lru);
1735 list_del_init(&page->lru);
1736 list_add_tail(&page->lru, &data->trace_pages);
1737 SetPageLRU(page);
1738 #endif
1739 }
1740 global_trace.entries += ENTRIES_PER_PAGE;
1741
1742 return 0;
1743
1744 free_pages:
1745 list_for_each_entry_safe(page, tmp, &pages, lru) {
1746 list_del_init(&page->lru);
1747 __free_page(page);
1748 }
1749 return -ENOMEM;
1750 }
1751
1752 __init static int tracer_alloc_buffers(void)
1753 {
1754 struct trace_array_cpu *data;
1755 void *array;
1756 struct page *page;
1757 int pages = 0;
1758 int ret = -ENOMEM;
1759 int i;
1760
1761 /* Allocate the first page for all buffers */
1762 for_each_possible_cpu(i) {
1763 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
1764 max_tr.data[i] = &per_cpu(max_data, i);
1765
1766 array = (void *)__get_free_page(GFP_KERNEL);
1767 if (array == NULL) {
1768 printk(KERN_ERR "tracer: failed to allocate page"
1769 "for trace buffer!\n");
1770 goto free_buffers;
1771 }
1772
1773 /* set the array to the list */
1774 INIT_LIST_HEAD(&data->trace_pages);
1775 page = virt_to_page(array);
1776 list_add(&page->lru, &data->trace_pages);
1777 /* use the LRU flag to differentiate the two buffers */
1778 ClearPageLRU(page);
1779
1780 /* Only allocate if we are actually using the max trace */
1781 #ifdef CONFIG_TRACER_MAX_TRACE
1782 array = (void *)__get_free_page(GFP_KERNEL);
1783 if (array == NULL) {
1784 printk(KERN_ERR "tracer: failed to allocate page"
1785 "for trace buffer!\n");
1786 goto free_buffers;
1787 }
1788
1789 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
1790 page = virt_to_page(array);
1791 list_add(&page->lru, &max_tr.data[i]->trace_pages);
1792 SetPageLRU(page);
1793 #endif
1794 }
1795
1796 /*
1797 * Since we allocate by orders of pages, we may be able to
1798 * round up a bit.
1799 */
1800 global_trace.entries = ENTRIES_PER_PAGE;
1801 pages++;
1802
1803 while (global_trace.entries < trace_nr_entries) {
1804 if (trace_alloc_page())
1805 break;
1806 pages++;
1807 }
1808 max_tr.entries = global_trace.entries;
1809
1810 pr_info("tracer: %d pages allocated for %ld",
1811 pages, trace_nr_entries);
1812 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
1813 pr_info(" actual entries %ld\n", global_trace.entries);
1814
1815 tracer_init_debugfs();
1816
1817 trace_init_cmdlines();
1818
1819 register_tracer(&no_tracer);
1820 current_trace = &no_tracer;
1821
1822 /* All seems OK, enable tracing */
1823 tracing_disabled = 0;
1824
1825 return 0;
1826
1827 free_buffers:
1828 for (i-- ; i >= 0; i--) {
1829 struct page *page, *tmp;
1830 struct trace_array_cpu *data = global_trace.data[i];
1831
1832 if (data) {
1833 list_for_each_entry_safe(page, tmp,
1834 &data->trace_pages, lru) {
1835 list_del_init(&page->lru);
1836 __free_page(page);
1837 }
1838 }
1839
1840 #ifdef CONFIG_TRACER_MAX_TRACE
1841 data = max_tr.data[i];
1842 if (data) {
1843 list_for_each_entry_safe(page, tmp,
1844 &data->trace_pages, lru) {
1845 list_del_init(&page->lru);
1846 __free_page(page);
1847 }
1848 }
1849 #endif
1850 }
1851 return ret;
1852 }
1853
1854 fs_initcall(tracer_alloc_buffers);
This page took 0.068567 seconds and 6 git commands to generate.