2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
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
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>
32 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly tracing_thresh;
35 static int tracing_disabled = 1;
38 ns2usecs(cycle_t nsec)
45 static const int time_sync_freq_max = 128;
46 static const cycle_t time_sync_thresh = 100000;
48 static DEFINE_PER_CPU(cycle_t, time_offset);
49 static DEFINE_PER_CPU(cycle_t, prev_cpu_time);
50 static DEFINE_PER_CPU(int, time_sync_count);
51 static DEFINE_PER_CPU(int, time_sync_freq);
54 * Global lock which we take every now and then to synchronize
55 * the CPUs time. This method is not warp-safe, but it's good
56 * enough to synchronize slowly diverging time sources and thus
57 * it's good enough for tracing:
59 static DEFINE_SPINLOCK(time_sync_lock);
60 static cycle_t prev_global_time;
62 static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
66 spin_lock_irqsave(&time_sync_lock, flags);
69 * Update the synchronization frequency:
71 if (per_cpu(time_sync_freq, cpu) < time_sync_freq_max)
72 per_cpu(time_sync_freq, cpu) *= 2;
73 per_cpu(time_sync_count, cpu) = per_cpu(time_sync_freq, cpu);
75 if (time < prev_global_time) {
76 per_cpu(time_offset, cpu) += prev_global_time - time;
77 time = prev_global_time;
79 prev_global_time = time;
82 spin_unlock_irqrestore(&time_sync_lock, flags);
87 notrace cycle_t ftrace_now(int cpu)
89 cycle_t prev_cpu_time, time, delta_time;
91 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
92 time = sched_clock() + per_cpu(time_offset, cpu);
93 delta_time = time-prev_cpu_time;
95 if (unlikely(delta_time > time_sync_thresh ||
96 --per_cpu(time_sync_count, cpu) <= 0))
97 time = __ftrace_now_sync(time, cpu);
102 static struct trace_array global_trace;
104 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
106 static struct trace_array max_tr;
108 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
110 static int tracer_enabled;
111 static unsigned long trace_nr_entries = 16384UL;
113 static struct tracer *trace_types __read_mostly;
114 static struct tracer *current_trace __read_mostly;
115 static int max_tracer_type_len;
117 static DEFINE_MUTEX(trace_types_lock);
119 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
121 static int __init set_nr_entries(char *str)
125 trace_nr_entries = simple_strtoul(str, &str, 0);
128 __setup("trace_entries=", set_nr_entries);
130 unsigned long nsecs_to_usecs(unsigned long nsecs)
136 __TRACE_FIRST_TYPE = 0,
144 enum trace_flag_type {
145 TRACE_FLAG_IRQS_OFF = 0x01,
146 TRACE_FLAG_NEED_RESCHED = 0x02,
147 TRACE_FLAG_HARDIRQ = 0x04,
148 TRACE_FLAG_SOFTIRQ = 0x08,
151 enum trace_iterator_flags {
152 TRACE_ITER_PRINT_PARENT = 0x01,
153 TRACE_ITER_SYM_OFFSET = 0x02,
154 TRACE_ITER_SYM_ADDR = 0x04,
155 TRACE_ITER_VERBOSE = 0x08,
156 TRACE_ITER_RAW = 0x10,
157 TRACE_ITER_BIN = 0x20,
160 #define TRACE_ITER_SYM_MASK \
161 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
163 /* These must match the bit postions above */
164 static const char *trace_options[] = {
174 static unsigned trace_flags;
176 static DEFINE_SPINLOCK(ftrace_max_lock);
179 * Copy the new maximum trace into the separate maximum-trace
180 * structure. (this way the maximum trace is permanently saved,
181 * for later retrieval via /debugfs/tracing/latency_trace)
184 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
186 struct trace_array_cpu *data = tr->data[cpu];
189 max_tr.time_start = data->preempt_timestamp;
191 data = max_tr.data[cpu];
192 data->saved_latency = tracing_max_latency;
194 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
195 data->pid = tsk->pid;
196 data->uid = tsk->uid;
197 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
198 data->policy = tsk->policy;
199 data->rt_priority = tsk->rt_priority;
201 /* record this tasks comm */
202 tracing_record_cmdline(current);
205 void check_pages(struct trace_array_cpu *data)
207 struct page *page, *tmp;
209 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
210 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
212 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
213 BUG_ON(page->lru.next->prev != &page->lru);
214 BUG_ON(page->lru.prev->next != &page->lru);
218 void *head_page(struct trace_array_cpu *data)
223 if (list_empty(&data->trace_pages))
226 page = list_entry(data->trace_pages.next, struct page, lru);
227 BUG_ON(&page->lru == &data->trace_pages);
229 return page_address(page);
233 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
235 int len = (PAGE_SIZE - 1) - s->len;
243 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
246 /* If we can't write it all, don't bother writing anything */
256 trace_seq_puts(struct trace_seq *s, const char *str)
258 int len = strlen(str);
260 if (len > ((PAGE_SIZE - 1) - s->len))
263 memcpy(s->buffer + s->len, str, len);
270 trace_seq_putc(struct trace_seq *s, unsigned char c)
272 if (s->len >= (PAGE_SIZE - 1))
275 s->buffer[s->len++] = c;
281 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
283 if (len > ((PAGE_SIZE - 1) - s->len))
286 memcpy(s->buffer + s->len, mem, len);
293 trace_seq_reset(struct trace_seq *s)
299 trace_print_seq(struct seq_file *m, struct trace_seq *s)
301 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
304 seq_puts(m, s->buffer);
310 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
312 struct list_head flip_pages;
314 INIT_LIST_HEAD(&flip_pages);
316 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
317 sizeof(struct trace_array_cpu) -
318 offsetof(struct trace_array_cpu, trace_head_idx));
322 list_splice_init(&tr1->trace_pages, &flip_pages);
323 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
324 list_splice_init(&flip_pages, &tr2->trace_pages);
325 BUG_ON(!list_empty(&flip_pages));
331 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
333 struct trace_array_cpu *data;
336 WARN_ON_ONCE(!irqs_disabled());
337 spin_lock(&ftrace_max_lock);
338 /* clear out all the previous traces */
339 for_each_possible_cpu(i) {
341 flip_trace(max_tr.data[i], data);
345 __update_max_tr(tr, tsk, cpu);
346 spin_unlock(&ftrace_max_lock);
350 * update_max_tr_single - only copy one trace over, and reset the rest
352 * @tsk - task with the latency
353 * @cpu - the cpu of the buffer to copy.
356 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
358 struct trace_array_cpu *data = tr->data[cpu];
361 WARN_ON_ONCE(!irqs_disabled());
362 spin_lock(&ftrace_max_lock);
363 for_each_possible_cpu(i)
364 tracing_reset(max_tr.data[i]);
366 flip_trace(max_tr.data[cpu], data);
369 __update_max_tr(tr, tsk, cpu);
370 spin_unlock(&ftrace_max_lock);
373 int register_tracer(struct tracer *type)
380 pr_info("Tracer must have a name\n");
384 mutex_lock(&trace_types_lock);
385 for (t = trace_types; t; t = t->next) {
386 if (strcmp(type->name, t->name) == 0) {
388 pr_info("Trace %s already registered\n",
395 #ifdef CONFIG_FTRACE_STARTUP_TEST
396 if (type->selftest) {
397 struct tracer *saved_tracer = current_trace;
398 struct trace_array_cpu *data;
399 struct trace_array *tr = &global_trace;
400 int saved_ctrl = tr->ctrl;
403 * Run a selftest on this tracer.
404 * Here we reset the trace buffer, and set the current
405 * tracer to be this tracer. The tracer can then run some
406 * internal tracing to verify that everything is in order.
407 * If we fail, we do not register this tracer.
409 for_each_possible_cpu(i) {
411 if (!head_page(data))
415 current_trace = type;
417 /* the test is responsible for initializing and enabling */
418 pr_info("Testing tracer %s: ", type->name);
419 ret = type->selftest(type, tr);
420 /* the test is responsible for resetting too */
421 current_trace = saved_tracer;
422 tr->ctrl = saved_ctrl;
424 printk(KERN_CONT "FAILED!\n");
427 /* Only reset on passing, to avoid touching corrupted buffers */
428 for_each_possible_cpu(i) {
430 if (!head_page(data))
434 printk(KERN_CONT "PASSED\n");
438 type->next = trace_types;
440 len = strlen(type->name);
441 if (len > max_tracer_type_len)
442 max_tracer_type_len = len;
445 mutex_unlock(&trace_types_lock);
450 void unregister_tracer(struct tracer *type)
455 mutex_lock(&trace_types_lock);
456 for (t = &trace_types; *t; t = &(*t)->next) {
460 pr_info("Trace %s not registered\n", type->name);
465 if (strlen(type->name) != max_tracer_type_len)
468 max_tracer_type_len = 0;
469 for (t = &trace_types; *t; t = &(*t)->next) {
470 len = strlen((*t)->name);
471 if (len > max_tracer_type_len)
472 max_tracer_type_len = len;
475 mutex_unlock(&trace_types_lock);
478 notrace void tracing_reset(struct trace_array_cpu *data)
481 data->trace_head = data->trace_tail = head_page(data);
482 data->trace_head_idx = 0;
483 data->trace_tail_idx = 0;
488 function_trace_call(unsigned long ip, unsigned long parent_ip)
490 struct trace_array *tr = &global_trace;
491 struct trace_array_cpu *data;
496 if (unlikely(!tracer_enabled))
499 local_irq_save(flags);
500 cpu = raw_smp_processor_id();
501 data = tr->data[cpu];
502 disabled = atomic_inc_return(&data->disabled);
504 if (likely(disabled == 1))
505 ftrace(tr, data, ip, parent_ip, flags);
507 atomic_dec(&data->disabled);
508 local_irq_restore(flags);
511 static struct ftrace_ops trace_ops __read_mostly =
513 .func = function_trace_call,
517 notrace void tracing_start_function_trace(void)
519 register_ftrace_function(&trace_ops);
522 notrace void tracing_stop_function_trace(void)
524 unregister_ftrace_function(&trace_ops);
527 #define SAVED_CMDLINES 128
528 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
529 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
530 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
531 static int cmdline_idx;
532 static DEFINE_SPINLOCK(trace_cmdline_lock);
533 atomic_t trace_record_cmdline_disabled;
535 static void trace_init_cmdlines(void)
537 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
538 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
542 notrace void trace_stop_cmdline_recording(void);
544 static notrace void trace_save_cmdline(struct task_struct *tsk)
549 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
553 * It's not the end of the world if we don't get
554 * the lock, but we also don't want to spin
555 * nor do we want to disable interrupts,
556 * so if we miss here, then better luck next time.
558 if (!spin_trylock(&trace_cmdline_lock))
561 idx = map_pid_to_cmdline[tsk->pid];
562 if (idx >= SAVED_CMDLINES) {
563 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
565 map = map_cmdline_to_pid[idx];
566 if (map <= PID_MAX_DEFAULT)
567 map_pid_to_cmdline[map] = (unsigned)-1;
569 map_pid_to_cmdline[tsk->pid] = idx;
574 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
576 spin_unlock(&trace_cmdline_lock);
579 static notrace char *trace_find_cmdline(int pid)
581 char *cmdline = "<...>";
587 if (pid > PID_MAX_DEFAULT)
590 map = map_pid_to_cmdline[pid];
591 if (map >= SAVED_CMDLINES)
594 cmdline = saved_cmdlines[map];
600 notrace void tracing_record_cmdline(struct task_struct *tsk)
602 if (atomic_read(&trace_record_cmdline_disabled))
605 trace_save_cmdline(tsk);
608 static inline notrace struct list_head *
609 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
612 * Roundrobin - but skip the head (which is not a real page):
615 if (unlikely(next == &data->trace_pages))
617 BUG_ON(next == &data->trace_pages);
622 static inline notrace void *
623 trace_next_page(struct trace_array_cpu *data, void *addr)
625 struct list_head *next;
628 page = virt_to_page(addr);
630 next = trace_next_list(data, &page->lru);
631 page = list_entry(next, struct page, lru);
633 return page_address(page);
636 static inline notrace struct trace_entry *
637 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
639 unsigned long idx, idx_next;
640 struct trace_entry *entry;
643 idx = data->trace_head_idx;
646 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
648 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
650 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
651 data->trace_head = trace_next_page(data, data->trace_head);
655 if (data->trace_head == data->trace_tail &&
656 idx_next == data->trace_tail_idx) {
658 data->trace_tail_idx++;
659 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
661 trace_next_page(data, data->trace_tail);
662 data->trace_tail_idx = 0;
666 data->trace_head_idx = idx_next;
671 static inline notrace void
672 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
674 struct task_struct *tsk = current;
677 pc = preempt_count();
679 entry->preempt_count = pc & 0xff;
680 entry->pid = tsk->pid;
681 entry->t = ftrace_now(raw_smp_processor_id());
682 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
683 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
684 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
685 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
689 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
690 unsigned long ip, unsigned long parent_ip, unsigned long flags)
692 struct trace_entry *entry;
694 spin_lock(&data->lock);
695 entry = tracing_get_trace_entry(tr, data);
696 tracing_generic_entry_update(entry, flags);
697 entry->type = TRACE_FN;
699 entry->fn.parent_ip = parent_ip;
700 spin_unlock(&data->lock);
704 tracing_sched_switch_trace(struct trace_array *tr,
705 struct trace_array_cpu *data,
706 struct task_struct *prev, struct task_struct *next,
709 struct trace_entry *entry;
711 spin_lock(&data->lock);
712 entry = tracing_get_trace_entry(tr, data);
713 tracing_generic_entry_update(entry, flags);
714 entry->type = TRACE_CTX;
715 entry->ctx.prev_pid = prev->pid;
716 entry->ctx.prev_prio = prev->prio;
717 entry->ctx.prev_state = prev->state;
718 entry->ctx.next_pid = next->pid;
719 entry->ctx.next_prio = next->prio;
720 spin_unlock(&data->lock);
723 enum trace_file_type {
724 TRACE_FILE_LAT_FMT = 1,
727 static struct trace_entry *
728 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
729 struct trace_iterator *iter, int cpu)
732 struct trace_entry *array;
734 if (iter->next_idx[cpu] >= tr->entries ||
735 iter->next_idx[cpu] >= data->trace_idx ||
736 (data->trace_head == data->trace_tail &&
737 data->trace_head_idx == data->trace_tail_idx))
740 if (!iter->next_page[cpu]) {
741 /* Initialize the iterator for this cpu trace buffer */
742 WARN_ON(!data->trace_tail);
743 page = virt_to_page(data->trace_tail);
744 iter->next_page[cpu] = &page->lru;
745 iter->next_page_idx[cpu] = data->trace_tail_idx;
748 page = list_entry(iter->next_page[cpu], struct page, lru);
749 BUG_ON(&data->trace_pages == &page->lru);
751 array = page_address(page);
753 /* Still possible to catch up to the tail */
754 if (iter->next_idx[cpu] && array == data->trace_tail &&
755 iter->next_page_idx[cpu] == data->trace_tail_idx)
758 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
759 return &array[iter->next_page_idx[cpu]];
762 static struct notrace trace_entry *
763 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
765 struct trace_array *tr = iter->tr;
766 struct trace_entry *ent, *next = NULL;
770 for_each_possible_cpu(cpu) {
771 if (!head_page(tr->data[cpu]))
773 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
775 * Pick the entry with the smallest timestamp:
777 if (ent && (!next || ent->t < next->t)) {
789 static notrace void trace_iterator_increment(struct trace_iterator *iter)
792 iter->next_idx[iter->cpu]++;
793 iter->next_page_idx[iter->cpu]++;
795 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
796 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
798 iter->next_page_idx[iter->cpu] = 0;
799 iter->next_page[iter->cpu] =
800 trace_next_list(data, iter->next_page[iter->cpu]);
804 static notrace void trace_consume(struct trace_iterator *iter)
806 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
808 data->trace_tail_idx++;
809 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
810 data->trace_tail = trace_next_page(data, data->trace_tail);
811 data->trace_tail_idx = 0;
814 /* Check if we empty it, then reset the index */
815 if (data->trace_head == data->trace_tail &&
816 data->trace_head_idx == data->trace_tail_idx)
820 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
822 struct trace_entry *next;
825 next = find_next_entry(iter, &next_cpu);
827 iter->prev_ent = iter->ent;
828 iter->prev_cpu = iter->cpu;
831 iter->cpu = next_cpu;
834 trace_iterator_increment(iter);
836 return next ? iter : NULL;
839 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
841 struct trace_iterator *iter = m->private;
842 void *last_ent = iter->ent;
848 /* can't go backwards */
853 ent = find_next_entry_inc(iter);
857 while (ent && iter->idx < i)
858 ent = find_next_entry_inc(iter);
862 if (last_ent && !ent)
863 seq_puts(m, "\n\nvim:ft=help\n");
868 static void *s_start(struct seq_file *m, loff_t *pos)
870 struct trace_iterator *iter = m->private;
875 mutex_lock(&trace_types_lock);
877 if (!current_trace || current_trace != iter->trace)
880 atomic_inc(&trace_record_cmdline_disabled);
882 /* let the tracer grab locks here if needed */
883 if (current_trace->start)
884 current_trace->start(iter);
886 if (*pos != iter->pos) {
890 iter->prev_ent = NULL;
893 for_each_possible_cpu(i) {
894 iter->next_idx[i] = 0;
895 iter->next_page[i] = NULL;
898 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
903 p = s_next(m, p, &l);
909 static void s_stop(struct seq_file *m, void *p)
911 struct trace_iterator *iter = m->private;
913 atomic_dec(&trace_record_cmdline_disabled);
915 /* let the tracer release locks here if needed */
916 if (current_trace && current_trace == iter->trace && iter->trace->stop)
917 iter->trace->stop(iter);
919 mutex_unlock(&trace_types_lock);
923 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
925 #ifdef CONFIG_KALLSYMS
926 char str[KSYM_SYMBOL_LEN];
928 kallsyms_lookup(address, NULL, NULL, NULL, str);
930 return trace_seq_printf(s, fmt, str);
936 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
937 unsigned long address)
939 #ifdef CONFIG_KALLSYMS
940 char str[KSYM_SYMBOL_LEN];
942 sprint_symbol(str, address);
943 return trace_seq_printf(s, fmt, str);
949 # define IP_FMT "%08lx"
951 # define IP_FMT "%016lx"
955 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
960 return trace_seq_printf(s, "0");
962 if (sym_flags & TRACE_ITER_SYM_OFFSET)
963 ret = seq_print_sym_offset(s, "%s", ip);
965 ret = seq_print_sym_short(s, "%s", ip);
970 if (sym_flags & TRACE_ITER_SYM_ADDR)
971 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
975 static notrace void print_lat_help_header(struct seq_file *m)
977 seq_puts(m, "# _------=> CPU# \n");
978 seq_puts(m, "# / _-----=> irqs-off \n");
979 seq_puts(m, "# | / _----=> need-resched \n");
980 seq_puts(m, "# || / _---=> hardirq/softirq \n");
981 seq_puts(m, "# ||| / _--=> preempt-depth \n");
982 seq_puts(m, "# |||| / \n");
983 seq_puts(m, "# ||||| delay \n");
984 seq_puts(m, "# cmd pid ||||| time | caller \n");
985 seq_puts(m, "# \\ / ||||| \\ | / \n");
988 static notrace void print_func_help_header(struct seq_file *m)
990 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
991 seq_puts(m, "# | | | | |\n");
996 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
998 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
999 struct trace_array *tr = iter->tr;
1000 struct trace_array_cpu *data = tr->data[tr->cpu];
1001 struct tracer *type = current_trace;
1002 unsigned long total = 0;
1003 unsigned long entries = 0;
1005 const char *name = "preemption";
1010 for_each_possible_cpu(cpu) {
1011 if (head_page(tr->data[cpu])) {
1012 total += tr->data[cpu]->trace_idx;
1013 if (tr->data[cpu]->trace_idx > tr->entries)
1014 entries += tr->entries;
1016 entries += tr->data[cpu]->trace_idx;
1020 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1022 seq_puts(m, "-----------------------------------"
1023 "---------------------------------\n");
1024 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1025 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1026 nsecs_to_usecs(data->saved_latency),
1030 #if defined(CONFIG_PREEMPT_NONE)
1032 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1034 #elif defined(CONFIG_PREEMPT_DESKTOP)
1039 /* These are reserved for later use */
1042 seq_printf(m, " #P:%d)\n", num_online_cpus());
1046 seq_puts(m, " -----------------\n");
1047 seq_printf(m, " | task: %.16s-%d "
1048 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1049 data->comm, data->pid, data->uid, data->nice,
1050 data->policy, data->rt_priority);
1051 seq_puts(m, " -----------------\n");
1053 if (data->critical_start) {
1054 seq_puts(m, " => started at: ");
1055 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1056 trace_print_seq(m, &iter->seq);
1057 seq_puts(m, "\n => ended at: ");
1058 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1059 trace_print_seq(m, &iter->seq);
1067 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1069 int hardirq, softirq;
1072 comm = trace_find_cmdline(entry->pid);
1074 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1075 trace_seq_printf(s, "%d", cpu);
1076 trace_seq_printf(s, "%c%c",
1077 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1078 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1080 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1081 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1082 if (hardirq && softirq)
1083 trace_seq_putc(s, 'H');
1086 trace_seq_putc(s, 'h');
1089 trace_seq_putc(s, 's');
1091 trace_seq_putc(s, '.');
1095 if (entry->preempt_count)
1096 trace_seq_printf(s, "%x", entry->preempt_count);
1098 trace_seq_puts(s, ".");
1101 unsigned long preempt_mark_thresh = 100;
1104 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1105 unsigned long rel_usecs)
1107 trace_seq_printf(s, " %4lldus", abs_usecs);
1108 if (rel_usecs > preempt_mark_thresh)
1109 trace_seq_puts(s, "!: ");
1110 else if (rel_usecs > 1)
1111 trace_seq_puts(s, "+: ");
1113 trace_seq_puts(s, " : ");
1116 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1119 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1121 struct trace_seq *s = &iter->seq;
1122 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1123 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1124 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1125 struct trace_entry *entry = iter->ent;
1126 unsigned long abs_usecs;
1127 unsigned long rel_usecs;
1133 rel_usecs = ns2usecs(next_entry->t - entry->t);
1134 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1137 comm = trace_find_cmdline(entry->pid);
1138 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1139 " %ld.%03ldms (+%ld.%03ldms): ",
1141 entry->pid, cpu, entry->flags,
1142 entry->preempt_count, trace_idx,
1145 abs_usecs % 1000, rel_usecs/1000,
1148 lat_print_generic(s, entry, cpu);
1149 lat_print_timestamp(s, abs_usecs, rel_usecs);
1151 switch (entry->type) {
1153 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1154 trace_seq_puts(s, " (");
1155 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1156 trace_seq_puts(s, ")\n");
1159 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1160 state_to_char[entry->ctx.prev_state] : 'X';
1161 comm = trace_find_cmdline(entry->ctx.next_pid);
1162 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1163 entry->ctx.prev_pid,
1164 entry->ctx.prev_prio,
1166 entry->ctx.next_pid,
1167 entry->ctx.next_prio,
1171 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1176 static notrace int print_trace_fmt(struct trace_iterator *iter)
1178 struct trace_seq *s = &iter->seq;
1179 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1180 struct trace_entry *entry;
1181 unsigned long usec_rem;
1182 unsigned long long t;
1190 comm = trace_find_cmdline(iter->ent->pid);
1192 t = ns2usecs(entry->t);
1193 usec_rem = do_div(t, 1000000ULL);
1194 secs = (unsigned long)t;
1196 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1199 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1202 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1206 switch (entry->type) {
1208 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1211 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1212 entry->fn.parent_ip) {
1213 ret = trace_seq_printf(s, " <-");
1216 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1221 ret = trace_seq_printf(s, "\n");
1226 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1227 state_to_char[entry->ctx.prev_state] : 'X';
1228 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1229 entry->ctx.prev_pid,
1230 entry->ctx.prev_prio,
1232 entry->ctx.next_pid,
1233 entry->ctx.next_prio);
1241 static notrace int print_raw_fmt(struct trace_iterator *iter)
1243 struct trace_seq *s = &iter->seq;
1244 struct trace_entry *entry;
1250 ret = trace_seq_printf(s, "%d %d %llu ",
1251 entry->pid, iter->cpu, entry->t);
1255 switch (entry->type) {
1257 ret = trace_seq_printf(s, "%x %x\n",
1258 entry->fn.ip, entry->fn.parent_ip);
1263 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1264 state_to_char[entry->ctx.prev_state] : 'X';
1265 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1266 entry->ctx.prev_pid,
1267 entry->ctx.prev_prio,
1269 entry->ctx.next_pid,
1270 entry->ctx.next_prio);
1278 #define SEQ_PUT_FIELD_RET(s, x) \
1280 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1284 static notrace int print_bin_fmt(struct trace_iterator *iter)
1286 struct trace_seq *s = &iter->seq;
1287 struct trace_entry *entry;
1291 SEQ_PUT_FIELD_RET(s, entry->pid);
1292 SEQ_PUT_FIELD_RET(s, entry->cpu);
1293 SEQ_PUT_FIELD_RET(s, entry->t);
1295 switch (entry->type) {
1297 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1298 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1301 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1302 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1303 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1304 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1305 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1311 static int trace_empty(struct trace_iterator *iter)
1313 struct trace_array_cpu *data;
1316 for_each_possible_cpu(cpu) {
1317 data = iter->tr->data[cpu];
1319 if (head_page(data) && data->trace_idx &&
1320 (data->trace_tail != data->trace_head ||
1321 data->trace_tail_idx != data->trace_head_idx))
1327 static int print_trace_line(struct trace_iterator *iter)
1329 if (trace_flags & TRACE_ITER_BIN)
1330 return print_bin_fmt(iter);
1332 if (trace_flags & TRACE_ITER_RAW)
1333 return print_raw_fmt(iter);
1335 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1336 return print_lat_fmt(iter, iter->idx, iter->cpu);
1338 return print_trace_fmt(iter);
1341 static int s_show(struct seq_file *m, void *v)
1343 struct trace_iterator *iter = v;
1345 if (iter->ent == NULL) {
1347 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1350 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1351 /* print nothing if the buffers are empty */
1352 if (trace_empty(iter))
1354 print_trace_header(m, iter);
1355 if (!(trace_flags & TRACE_ITER_VERBOSE))
1356 print_lat_help_header(m);
1358 if (!(trace_flags & TRACE_ITER_VERBOSE))
1359 print_func_help_header(m);
1362 print_trace_line(iter);
1363 trace_print_seq(m, &iter->seq);
1369 static struct seq_operations tracer_seq_ops = {
1376 static struct trace_iterator notrace *
1377 __tracing_open(struct inode *inode, struct file *file, int *ret)
1379 struct trace_iterator *iter;
1381 if (tracing_disabled) {
1386 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1392 mutex_lock(&trace_types_lock);
1393 if (current_trace && current_trace->print_max)
1396 iter->tr = inode->i_private;
1397 iter->trace = current_trace;
1400 /* TODO stop tracer */
1401 *ret = seq_open(file, &tracer_seq_ops);
1403 struct seq_file *m = file->private_data;
1406 /* stop the trace while dumping */
1410 if (iter->trace && iter->trace->open)
1411 iter->trace->open(iter);
1416 mutex_unlock(&trace_types_lock);
1422 int tracing_open_generic(struct inode *inode, struct file *filp)
1424 if (tracing_disabled)
1427 filp->private_data = inode->i_private;
1431 int tracing_release(struct inode *inode, struct file *file)
1433 struct seq_file *m = (struct seq_file *)file->private_data;
1434 struct trace_iterator *iter = m->private;
1436 mutex_lock(&trace_types_lock);
1437 if (iter->trace && iter->trace->close)
1438 iter->trace->close(iter);
1440 /* reenable tracing if it was previously enabled */
1443 mutex_unlock(&trace_types_lock);
1445 seq_release(inode, file);
1450 static int tracing_open(struct inode *inode, struct file *file)
1454 __tracing_open(inode, file, &ret);
1459 static int tracing_lt_open(struct inode *inode, struct file *file)
1461 struct trace_iterator *iter;
1464 iter = __tracing_open(inode, file, &ret);
1467 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1473 static notrace void *
1474 t_next(struct seq_file *m, void *v, loff_t *pos)
1476 struct tracer *t = m->private;
1488 static void *t_start(struct seq_file *m, loff_t *pos)
1490 struct tracer *t = m->private;
1493 mutex_lock(&trace_types_lock);
1494 for (; t && l < *pos; t = t_next(m, t, &l))
1500 static void t_stop(struct seq_file *m, void *p)
1502 mutex_unlock(&trace_types_lock);
1505 static int t_show(struct seq_file *m, void *v)
1507 struct tracer *t = v;
1512 seq_printf(m, "%s", t->name);
1521 static struct seq_operations show_traces_seq_ops = {
1528 static int show_traces_open(struct inode *inode, struct file *file)
1532 if (tracing_disabled)
1535 ret = seq_open(file, &show_traces_seq_ops);
1537 struct seq_file *m = file->private_data;
1538 m->private = trace_types;
1544 static struct file_operations tracing_fops = {
1545 .open = tracing_open,
1547 .llseek = seq_lseek,
1548 .release = tracing_release,
1551 static struct file_operations tracing_lt_fops = {
1552 .open = tracing_lt_open,
1554 .llseek = seq_lseek,
1555 .release = tracing_release,
1558 static struct file_operations show_traces_fops = {
1559 .open = show_traces_open,
1561 .release = seq_release,
1565 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1566 size_t cnt, loff_t *ppos)
1573 /* calulate max size */
1574 for (i = 0; trace_options[i]; i++) {
1575 len += strlen(trace_options[i]);
1576 len += 3; /* "no" and space */
1579 /* +2 for \n and \0 */
1580 buf = kmalloc(len + 2, GFP_KERNEL);
1584 for (i = 0; trace_options[i]; i++) {
1585 if (trace_flags & (1 << i))
1586 r += sprintf(buf + r, "%s ", trace_options[i]);
1588 r += sprintf(buf + r, "no%s ", trace_options[i]);
1591 r += sprintf(buf + r, "\n");
1592 WARN_ON(r >= len + 2);
1594 r = simple_read_from_buffer(ubuf, cnt, ppos,
1603 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1604 size_t cnt, loff_t *ppos)
1614 if (copy_from_user(&buf, ubuf, cnt))
1619 if (strncmp(buf, "no", 2) == 0) {
1624 for (i = 0; trace_options[i]; i++) {
1625 int len = strlen(trace_options[i]);
1627 if (strncmp(cmp, trace_options[i], len) == 0) {
1629 trace_flags &= ~(1 << i);
1631 trace_flags |= (1 << i);
1641 static struct file_operations tracing_iter_fops = {
1642 .open = tracing_open_generic,
1643 .read = tracing_iter_ctrl_read,
1644 .write = tracing_iter_ctrl_write,
1647 static const char readme_msg[] =
1648 "tracing mini-HOWTO:\n\n"
1650 "# mount -t debugfs nodev /debug\n\n"
1651 "# cat /debug/tracing/available_tracers\n"
1652 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1653 "# cat /debug/tracing/current_tracer\n"
1655 "# echo sched_switch > /debug/tracing/current_tracer\n"
1656 "# cat /debug/tracing/current_tracer\n"
1658 "# cat /debug/tracing/iter_ctrl\n"
1659 "noprint-parent nosym-offset nosym-addr noverbose\n"
1660 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1661 "# echo 1 > /debug/tracing/tracing_enabled\n"
1662 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1663 "echo 0 > /debug/tracing/tracing_enabled\n"
1667 tracing_readme_read(struct file *filp, char __user *ubuf,
1668 size_t cnt, loff_t *ppos)
1670 return simple_read_from_buffer(ubuf, cnt, ppos,
1671 readme_msg, strlen(readme_msg));
1674 static struct file_operations tracing_readme_fops = {
1675 .open = tracing_open_generic,
1676 .read = tracing_readme_read,
1681 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1682 size_t cnt, loff_t *ppos)
1684 struct trace_array *tr = filp->private_data;
1688 r = sprintf(buf, "%ld\n", tr->ctrl);
1689 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1693 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1694 size_t cnt, loff_t *ppos)
1696 struct trace_array *tr = filp->private_data;
1703 if (copy_from_user(&buf, ubuf, cnt))
1708 val = simple_strtoul(buf, NULL, 10);
1712 mutex_lock(&trace_types_lock);
1713 if (tr->ctrl ^ val) {
1721 if (current_trace && current_trace->ctrl_update)
1722 current_trace->ctrl_update(tr);
1724 mutex_unlock(&trace_types_lock);
1732 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1733 size_t cnt, loff_t *ppos)
1735 char buf[max_tracer_type_len+2];
1738 mutex_lock(&trace_types_lock);
1740 r = sprintf(buf, "%s\n", current_trace->name);
1742 r = sprintf(buf, "\n");
1743 mutex_unlock(&trace_types_lock);
1745 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1749 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1750 size_t cnt, loff_t *ppos)
1752 struct trace_array *tr = &global_trace;
1754 char buf[max_tracer_type_len+1];
1757 if (cnt > max_tracer_type_len)
1758 cnt = max_tracer_type_len;
1760 if (copy_from_user(&buf, ubuf, cnt))
1765 /* strip ending whitespace. */
1766 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1769 mutex_lock(&trace_types_lock);
1770 for (t = trace_types; t; t = t->next) {
1771 if (strcmp(t->name, buf) == 0)
1774 if (!t || t == current_trace)
1777 if (current_trace && current_trace->reset)
1778 current_trace->reset(tr);
1785 mutex_unlock(&trace_types_lock);
1793 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1794 size_t cnt, loff_t *ppos)
1796 unsigned long *ptr = filp->private_data;
1800 r = snprintf(buf, 64, "%ld\n",
1801 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1804 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1808 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1809 size_t cnt, loff_t *ppos)
1811 long *ptr = filp->private_data;
1818 if (copy_from_user(&buf, ubuf, cnt))
1823 val = simple_strtoul(buf, NULL, 10);
1830 static atomic_t tracing_reader;
1832 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1834 struct trace_iterator *iter;
1836 if (tracing_disabled)
1839 /* We only allow for reader of the pipe */
1840 if (atomic_inc_return(&tracing_reader) != 1) {
1841 atomic_dec(&tracing_reader);
1845 /* create a buffer to store the information to pass to userspace */
1846 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1850 iter->tr = &global_trace;
1852 filp->private_data = iter;
1857 static int tracing_release_pipe(struct inode *inode, struct file *file)
1859 struct trace_iterator *iter = file->private_data;
1862 atomic_dec(&tracing_reader);
1871 tracing_read_pipe(struct file *filp, char __user *ubuf,
1872 size_t cnt, loff_t *ppos)
1874 struct trace_iterator *iter = filp->private_data;
1875 struct trace_array_cpu *data;
1876 static cpumask_t mask;
1877 struct trace_entry *entry;
1879 unsigned long flags;
1885 /* return any leftover data */
1886 if (iter->seq.len > start) {
1887 len = iter->seq.len - start;
1890 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1899 trace_seq_reset(&iter->seq);
1902 while (trace_empty(iter)) {
1904 * This is a make-shift waitqueue. The reason we don't use
1905 * an actual wait queue is because:
1906 * 1) we only ever have one waiter
1907 * 2) the tracing, traces all functions, we don't want
1908 * the overhead of calling wake_up and friends
1909 * (and tracing them too)
1910 * Anyway, this is really very primitive wakeup.
1912 set_current_state(TASK_INTERRUPTIBLE);
1913 iter->tr->waiter = current;
1915 /* sleep for one second, and try again. */
1916 schedule_timeout(HZ);
1918 iter->tr->waiter = NULL;
1920 if (signal_pending(current))
1924 * We block until we read something and tracing is disabled.
1925 * We still block if tracing is disabled, but we have never
1926 * read anything. This allows a user to cat this file, and
1927 * then enable tracing. But after we have read something,
1928 * we give an EOF when tracing is again disabled.
1930 * iter->pos will be 0 if we haven't read anything.
1932 if (!tracer_enabled && iter->pos)
1938 /* stop when tracing is finished */
1939 if (trace_empty(iter))
1942 if (cnt >= PAGE_SIZE)
1943 cnt = PAGE_SIZE - 1;
1945 memset(iter, 0, sizeof(*iter));
1946 iter->tr = &global_trace;
1950 * We need to stop all tracing on all CPUS to read the
1951 * the next buffer. This is a bit expensive, but is
1952 * not done often. We fill all what we can read,
1953 * and then release the locks again.
1957 local_irq_save(flags);
1958 for_each_possible_cpu(cpu) {
1959 data = iter->tr->data[cpu];
1961 if (!head_page(data) || !data->trace_idx)
1964 atomic_inc(&data->disabled);
1965 spin_lock(&data->lock);
1969 while ((entry = find_next_entry_inc(iter)) != NULL) {
1970 ret = print_trace_line(iter);
1974 trace_consume(iter);
1976 if (iter->seq.len >= cnt)
1980 for_each_cpu_mask(cpu, mask) {
1981 data = iter->tr->data[cpu];
1982 spin_unlock(&data->lock);
1983 atomic_dec(&data->disabled);
1985 local_irq_restore(flags);
1987 /* Now copy what we have to the user */
1988 read = iter->seq.len;
1992 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1994 if (read < iter->seq.len)
1997 trace_seq_reset(&iter->seq);
2005 static struct file_operations tracing_max_lat_fops = {
2006 .open = tracing_open_generic,
2007 .read = tracing_max_lat_read,
2008 .write = tracing_max_lat_write,
2011 static struct file_operations tracing_ctrl_fops = {
2012 .open = tracing_open_generic,
2013 .read = tracing_ctrl_read,
2014 .write = tracing_ctrl_write,
2017 static struct file_operations set_tracer_fops = {
2018 .open = tracing_open_generic,
2019 .read = tracing_set_trace_read,
2020 .write = tracing_set_trace_write,
2023 static struct file_operations tracing_pipe_fops = {
2024 .open = tracing_open_pipe,
2025 .read = tracing_read_pipe,
2026 .release = tracing_release_pipe,
2029 #ifdef CONFIG_DYNAMIC_FTRACE
2032 tracing_read_long(struct file *filp, char __user *ubuf,
2033 size_t cnt, loff_t *ppos)
2035 unsigned long *p = filp->private_data;
2039 r = sprintf(buf, "%ld\n", *p);
2041 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2044 static struct file_operations tracing_read_long_fops = {
2045 .open = tracing_open_generic,
2046 .read = tracing_read_long,
2050 static struct dentry *d_tracer;
2052 struct dentry *tracing_init_dentry(void)
2059 d_tracer = debugfs_create_dir("tracing", NULL);
2061 if (!d_tracer && !once) {
2063 pr_warning("Could not create debugfs directory 'tracing'\n");
2070 #ifdef CONFIG_FTRACE_SELFTEST
2071 /* Let selftest have access to static functions in this file */
2072 #include "trace_selftest.c"
2075 static __init void tracer_init_debugfs(void)
2077 struct dentry *d_tracer;
2078 struct dentry *entry;
2080 d_tracer = tracing_init_dentry();
2082 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2083 &global_trace, &tracing_ctrl_fops);
2085 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2087 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2088 NULL, &tracing_iter_fops);
2090 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2092 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2093 &global_trace, &tracing_lt_fops);
2095 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2097 entry = debugfs_create_file("trace", 0444, d_tracer,
2098 &global_trace, &tracing_fops);
2100 pr_warning("Could not create debugfs 'trace' entry\n");
2102 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2103 &global_trace, &show_traces_fops);
2105 pr_warning("Could not create debugfs 'trace' entry\n");
2107 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2108 &global_trace, &set_tracer_fops);
2110 pr_warning("Could not create debugfs 'trace' entry\n");
2112 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2113 &tracing_max_latency,
2114 &tracing_max_lat_fops);
2116 pr_warning("Could not create debugfs "
2117 "'tracing_max_latency' entry\n");
2119 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2120 &tracing_thresh, &tracing_max_lat_fops);
2122 pr_warning("Could not create debugfs "
2123 "'tracing_threash' entry\n");
2124 entry = debugfs_create_file("README", 0644, d_tracer,
2125 NULL, &tracing_readme_fops);
2127 pr_warning("Could not create debugfs 'README' entry\n");
2129 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2130 NULL, &tracing_pipe_fops);
2132 pr_warning("Could not create debugfs "
2133 "'tracing_threash' entry\n");
2135 #ifdef CONFIG_DYNAMIC_FTRACE
2136 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2137 &ftrace_update_tot_cnt,
2138 &tracing_read_long_fops);
2140 pr_warning("Could not create debugfs "
2141 "'dyn_ftrace_total_info' entry\n");
2145 /* dummy trace to disable tracing */
2146 static struct tracer no_tracer __read_mostly =
2151 static int trace_alloc_page(void)
2153 struct trace_array_cpu *data;
2154 struct page *page, *tmp;
2159 /* first allocate a page for each CPU */
2160 for_each_possible_cpu(i) {
2161 array = (void *)__get_free_page(GFP_KERNEL);
2162 if (array == NULL) {
2163 printk(KERN_ERR "tracer: failed to allocate page"
2164 "for trace buffer!\n");
2168 page = virt_to_page(array);
2169 list_add(&page->lru, &pages);
2171 /* Only allocate if we are actually using the max trace */
2172 #ifdef CONFIG_TRACER_MAX_TRACE
2173 array = (void *)__get_free_page(GFP_KERNEL);
2174 if (array == NULL) {
2175 printk(KERN_ERR "tracer: failed to allocate page"
2176 "for trace buffer!\n");
2179 page = virt_to_page(array);
2180 list_add(&page->lru, &pages);
2184 /* Now that we successfully allocate a page per CPU, add them */
2185 for_each_possible_cpu(i) {
2186 data = global_trace.data[i];
2187 spin_lock_init(&data->lock);
2188 lockdep_set_class(&data->lock, &data->lock_key);
2189 page = list_entry(pages.next, struct page, lru);
2190 list_del_init(&page->lru);
2191 list_add_tail(&page->lru, &data->trace_pages);
2194 #ifdef CONFIG_TRACER_MAX_TRACE
2195 data = max_tr.data[i];
2196 spin_lock_init(&data->lock);
2197 lockdep_set_class(&data->lock, &data->lock_key);
2198 page = list_entry(pages.next, struct page, lru);
2199 list_del_init(&page->lru);
2200 list_add_tail(&page->lru, &data->trace_pages);
2204 global_trace.entries += ENTRIES_PER_PAGE;
2209 list_for_each_entry_safe(page, tmp, &pages, lru) {
2210 list_del_init(&page->lru);
2216 __init static int tracer_alloc_buffers(void)
2218 struct trace_array_cpu *data;
2225 /* Allocate the first page for all buffers */
2226 for_each_possible_cpu(i) {
2227 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2228 max_tr.data[i] = &per_cpu(max_data, i);
2230 array = (void *)__get_free_page(GFP_KERNEL);
2231 if (array == NULL) {
2232 printk(KERN_ERR "tracer: failed to allocate page"
2233 "for trace buffer!\n");
2237 /* set the array to the list */
2238 INIT_LIST_HEAD(&data->trace_pages);
2239 page = virt_to_page(array);
2240 list_add(&page->lru, &data->trace_pages);
2241 /* use the LRU flag to differentiate the two buffers */
2244 /* Only allocate if we are actually using the max trace */
2245 #ifdef CONFIG_TRACER_MAX_TRACE
2246 array = (void *)__get_free_page(GFP_KERNEL);
2247 if (array == NULL) {
2248 printk(KERN_ERR "tracer: failed to allocate page"
2249 "for trace buffer!\n");
2253 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2254 page = virt_to_page(array);
2255 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2261 * Since we allocate by orders of pages, we may be able to
2264 global_trace.entries = ENTRIES_PER_PAGE;
2267 while (global_trace.entries < trace_nr_entries) {
2268 if (trace_alloc_page())
2272 max_tr.entries = global_trace.entries;
2274 pr_info("tracer: %d pages allocated for %ld",
2275 pages, trace_nr_entries);
2276 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2277 pr_info(" actual entries %ld\n", global_trace.entries);
2279 tracer_init_debugfs();
2281 trace_init_cmdlines();
2283 register_tracer(&no_tracer);
2284 current_trace = &no_tracer;
2286 /* All seems OK, enable tracing */
2287 tracing_disabled = 0;
2292 for (i-- ; i >= 0; i--) {
2293 struct page *page, *tmp;
2294 struct trace_array_cpu *data = global_trace.data[i];
2297 list_for_each_entry_safe(page, tmp,
2298 &data->trace_pages, lru) {
2299 list_del_init(&page->lru);
2304 #ifdef CONFIG_TRACER_MAX_TRACE
2305 data = max_tr.data[i];
2307 list_for_each_entry_safe(page, tmp,
2308 &data->trace_pages, lru) {
2309 list_del_init(&page->lru);
2317 fs_initcall(tracer_alloc_buffers);