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,
159 #define TRACE_ITER_SYM_MASK \
160 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
162 /* These must match the bit postions above */
163 static const char *trace_options[] = {
172 static unsigned trace_flags;
174 static DEFINE_SPINLOCK(ftrace_max_lock);
177 * Copy the new maximum trace into the separate maximum-trace
178 * structure. (this way the maximum trace is permanently saved,
179 * for later retrieval via /debugfs/tracing/latency_trace)
182 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
184 struct trace_array_cpu *data = tr->data[cpu];
187 max_tr.time_start = data->preempt_timestamp;
189 data = max_tr.data[cpu];
190 data->saved_latency = tracing_max_latency;
192 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
193 data->pid = tsk->pid;
194 data->uid = tsk->uid;
195 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
196 data->policy = tsk->policy;
197 data->rt_priority = tsk->rt_priority;
199 /* record this tasks comm */
200 tracing_record_cmdline(current);
203 void check_pages(struct trace_array_cpu *data)
205 struct page *page, *tmp;
207 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
208 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
210 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
211 BUG_ON(page->lru.next->prev != &page->lru);
212 BUG_ON(page->lru.prev->next != &page->lru);
216 void *head_page(struct trace_array_cpu *data)
221 if (list_empty(&data->trace_pages))
224 page = list_entry(data->trace_pages.next, struct page, lru);
225 BUG_ON(&page->lru == &data->trace_pages);
227 return page_address(page);
231 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
233 int len = (PAGE_SIZE - 1) - s->len;
241 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
244 /* If we can't write it all, don't bother writing anything */
254 trace_seq_puts(struct trace_seq *s, const char *str)
256 int len = strlen(str);
258 if (len > ((PAGE_SIZE - 1) - s->len))
261 memcpy(s->buffer + s->len, str, len);
268 trace_seq_putc(struct trace_seq *s, unsigned char c)
270 if (s->len >= (PAGE_SIZE - 1))
273 s->buffer[s->len++] = c;
279 trace_seq_reset(struct trace_seq *s)
285 trace_print_seq(struct seq_file *m, struct trace_seq *s)
287 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
290 seq_puts(m, s->buffer);
296 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
298 struct list_head flip_pages;
300 INIT_LIST_HEAD(&flip_pages);
302 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
303 sizeof(struct trace_array_cpu) -
304 offsetof(struct trace_array_cpu, trace_head_idx));
308 list_splice_init(&tr1->trace_pages, &flip_pages);
309 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
310 list_splice_init(&flip_pages, &tr2->trace_pages);
311 BUG_ON(!list_empty(&flip_pages));
317 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
319 struct trace_array_cpu *data;
322 WARN_ON_ONCE(!irqs_disabled());
323 spin_lock(&ftrace_max_lock);
324 /* clear out all the previous traces */
325 for_each_possible_cpu(i) {
327 flip_trace(max_tr.data[i], data);
331 __update_max_tr(tr, tsk, cpu);
332 spin_unlock(&ftrace_max_lock);
336 * update_max_tr_single - only copy one trace over, and reset the rest
338 * @tsk - task with the latency
339 * @cpu - the cpu of the buffer to copy.
342 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
344 struct trace_array_cpu *data = tr->data[cpu];
347 WARN_ON_ONCE(!irqs_disabled());
348 spin_lock(&ftrace_max_lock);
349 for_each_possible_cpu(i)
350 tracing_reset(max_tr.data[i]);
352 flip_trace(max_tr.data[cpu], data);
355 __update_max_tr(tr, tsk, cpu);
356 spin_unlock(&ftrace_max_lock);
359 int register_tracer(struct tracer *type)
366 pr_info("Tracer must have a name\n");
370 mutex_lock(&trace_types_lock);
371 for (t = trace_types; t; t = t->next) {
372 if (strcmp(type->name, t->name) == 0) {
374 pr_info("Trace %s already registered\n",
381 #ifdef CONFIG_FTRACE_STARTUP_TEST
382 if (type->selftest) {
383 struct tracer *saved_tracer = current_trace;
384 struct trace_array_cpu *data;
385 struct trace_array *tr = &global_trace;
386 int saved_ctrl = tr->ctrl;
389 * Run a selftest on this tracer.
390 * Here we reset the trace buffer, and set the current
391 * tracer to be this tracer. The tracer can then run some
392 * internal tracing to verify that everything is in order.
393 * If we fail, we do not register this tracer.
395 for_each_possible_cpu(i) {
397 if (!head_page(data))
401 current_trace = type;
403 /* the test is responsible for initializing and enabling */
404 pr_info("Testing tracer %s: ", type->name);
405 ret = type->selftest(type, tr);
406 /* the test is responsible for resetting too */
407 current_trace = saved_tracer;
408 tr->ctrl = saved_ctrl;
410 printk(KERN_CONT "FAILED!\n");
413 /* Only reset on passing, to avoid touching corrupted buffers */
414 for_each_possible_cpu(i) {
416 if (!head_page(data))
420 printk(KERN_CONT "PASSED\n");
424 type->next = trace_types;
426 len = strlen(type->name);
427 if (len > max_tracer_type_len)
428 max_tracer_type_len = len;
431 mutex_unlock(&trace_types_lock);
436 void unregister_tracer(struct tracer *type)
441 mutex_lock(&trace_types_lock);
442 for (t = &trace_types; *t; t = &(*t)->next) {
446 pr_info("Trace %s not registered\n", type->name);
451 if (strlen(type->name) != max_tracer_type_len)
454 max_tracer_type_len = 0;
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 len = strlen((*t)->name);
457 if (len > max_tracer_type_len)
458 max_tracer_type_len = len;
461 mutex_unlock(&trace_types_lock);
464 notrace void tracing_reset(struct trace_array_cpu *data)
467 data->trace_head = data->trace_tail = head_page(data);
468 data->trace_head_idx = 0;
469 data->trace_tail_idx = 0;
474 function_trace_call(unsigned long ip, unsigned long parent_ip)
476 struct trace_array *tr = &global_trace;
477 struct trace_array_cpu *data;
482 if (unlikely(!tracer_enabled))
485 local_irq_save(flags);
486 cpu = raw_smp_processor_id();
487 data = tr->data[cpu];
488 disabled = atomic_inc_return(&data->disabled);
490 if (likely(disabled == 1))
491 ftrace(tr, data, ip, parent_ip, flags);
493 atomic_dec(&data->disabled);
494 local_irq_restore(flags);
497 static struct ftrace_ops trace_ops __read_mostly =
499 .func = function_trace_call,
503 notrace void tracing_start_function_trace(void)
505 register_ftrace_function(&trace_ops);
508 notrace void tracing_stop_function_trace(void)
510 unregister_ftrace_function(&trace_ops);
513 #define SAVED_CMDLINES 128
514 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
515 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
516 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
517 static int cmdline_idx;
518 static DEFINE_SPINLOCK(trace_cmdline_lock);
519 atomic_t trace_record_cmdline_disabled;
521 static void trace_init_cmdlines(void)
523 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
524 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
528 notrace void trace_stop_cmdline_recording(void);
530 static notrace void trace_save_cmdline(struct task_struct *tsk)
535 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
539 * It's not the end of the world if we don't get
540 * the lock, but we also don't want to spin
541 * nor do we want to disable interrupts,
542 * so if we miss here, then better luck next time.
544 if (!spin_trylock(&trace_cmdline_lock))
547 idx = map_pid_to_cmdline[tsk->pid];
548 if (idx >= SAVED_CMDLINES) {
549 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
551 map = map_cmdline_to_pid[idx];
552 if (map <= PID_MAX_DEFAULT)
553 map_pid_to_cmdline[map] = (unsigned)-1;
555 map_pid_to_cmdline[tsk->pid] = idx;
560 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
562 spin_unlock(&trace_cmdline_lock);
565 static notrace char *trace_find_cmdline(int pid)
567 char *cmdline = "<...>";
573 if (pid > PID_MAX_DEFAULT)
576 map = map_pid_to_cmdline[pid];
577 if (map >= SAVED_CMDLINES)
580 cmdline = saved_cmdlines[map];
586 notrace void tracing_record_cmdline(struct task_struct *tsk)
588 if (atomic_read(&trace_record_cmdline_disabled))
591 trace_save_cmdline(tsk);
594 static inline notrace struct list_head *
595 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
598 * Roundrobin - but skip the head (which is not a real page):
601 if (unlikely(next == &data->trace_pages))
603 BUG_ON(next == &data->trace_pages);
608 static inline notrace void *
609 trace_next_page(struct trace_array_cpu *data, void *addr)
611 struct list_head *next;
614 page = virt_to_page(addr);
616 next = trace_next_list(data, &page->lru);
617 page = list_entry(next, struct page, lru);
619 return page_address(page);
622 static inline notrace struct trace_entry *
623 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
625 unsigned long idx, idx_next;
626 struct trace_entry *entry;
629 idx = data->trace_head_idx;
632 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
634 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
636 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
637 data->trace_head = trace_next_page(data, data->trace_head);
641 if (data->trace_head == data->trace_tail &&
642 idx_next == data->trace_tail_idx) {
644 data->trace_tail_idx++;
645 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
647 trace_next_page(data, data->trace_tail);
648 data->trace_tail_idx = 0;
652 data->trace_head_idx = idx_next;
657 static inline notrace void
658 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
660 struct task_struct *tsk = current;
663 pc = preempt_count();
665 entry->preempt_count = pc & 0xff;
666 entry->pid = tsk->pid;
667 entry->t = ftrace_now(raw_smp_processor_id());
668 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
669 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
670 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
671 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
675 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
676 unsigned long ip, unsigned long parent_ip, unsigned long flags)
678 struct trace_entry *entry;
680 spin_lock(&data->lock);
681 entry = tracing_get_trace_entry(tr, data);
682 tracing_generic_entry_update(entry, flags);
683 entry->type = TRACE_FN;
685 entry->fn.parent_ip = parent_ip;
686 spin_unlock(&data->lock);
690 tracing_sched_switch_trace(struct trace_array *tr,
691 struct trace_array_cpu *data,
692 struct task_struct *prev, struct task_struct *next,
695 struct trace_entry *entry;
697 spin_lock(&data->lock);
698 entry = tracing_get_trace_entry(tr, data);
699 tracing_generic_entry_update(entry, flags);
700 entry->type = TRACE_CTX;
701 entry->ctx.prev_pid = prev->pid;
702 entry->ctx.prev_prio = prev->prio;
703 entry->ctx.prev_state = prev->state;
704 entry->ctx.next_pid = next->pid;
705 entry->ctx.next_prio = next->prio;
706 spin_unlock(&data->lock);
709 enum trace_file_type {
710 TRACE_FILE_LAT_FMT = 1,
713 static struct trace_entry *
714 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
715 struct trace_iterator *iter, int cpu)
718 struct trace_entry *array;
720 if (iter->next_idx[cpu] >= tr->entries ||
721 iter->next_idx[cpu] >= data->trace_idx ||
722 (data->trace_head == data->trace_tail &&
723 data->trace_head_idx == data->trace_tail_idx))
726 if (!iter->next_page[cpu]) {
727 /* Initialize the iterator for this cpu trace buffer */
728 WARN_ON(!data->trace_tail);
729 page = virt_to_page(data->trace_tail);
730 iter->next_page[cpu] = &page->lru;
731 iter->next_page_idx[cpu] = data->trace_tail_idx;
734 page = list_entry(iter->next_page[cpu], struct page, lru);
735 BUG_ON(&data->trace_pages == &page->lru);
737 array = page_address(page);
739 /* Still possible to catch up to the tail */
740 if (iter->next_idx[cpu] && array == data->trace_tail &&
741 iter->next_page_idx[cpu] == data->trace_tail_idx)
744 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
745 return &array[iter->next_page_idx[cpu]];
748 static struct notrace trace_entry *
749 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
751 struct trace_array *tr = iter->tr;
752 struct trace_entry *ent, *next = NULL;
756 for_each_possible_cpu(cpu) {
757 if (!head_page(tr->data[cpu]))
759 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
761 * Pick the entry with the smallest timestamp:
763 if (ent && (!next || ent->t < next->t)) {
775 static notrace void trace_iterator_increment(struct trace_iterator *iter)
778 iter->next_idx[iter->cpu]++;
779 iter->next_page_idx[iter->cpu]++;
781 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
782 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
784 iter->next_page_idx[iter->cpu] = 0;
785 iter->next_page[iter->cpu] =
786 trace_next_list(data, iter->next_page[iter->cpu]);
790 static notrace void trace_consume(struct trace_iterator *iter)
792 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
794 data->trace_tail_idx++;
795 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
796 data->trace_tail = trace_next_page(data, data->trace_tail);
797 data->trace_tail_idx = 0;
800 /* Check if we empty it, then reset the index */
801 if (data->trace_head == data->trace_tail &&
802 data->trace_head_idx == data->trace_tail_idx)
806 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
808 struct trace_entry *next;
811 next = find_next_entry(iter, &next_cpu);
813 iter->prev_ent = iter->ent;
814 iter->prev_cpu = iter->cpu;
817 iter->cpu = next_cpu;
820 trace_iterator_increment(iter);
822 return next ? iter : NULL;
825 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
827 struct trace_iterator *iter = m->private;
828 void *last_ent = iter->ent;
834 /* can't go backwards */
839 ent = find_next_entry_inc(iter);
843 while (ent && iter->idx < i)
844 ent = find_next_entry_inc(iter);
848 if (last_ent && !ent)
849 seq_puts(m, "\n\nvim:ft=help\n");
854 static void *s_start(struct seq_file *m, loff_t *pos)
856 struct trace_iterator *iter = m->private;
861 mutex_lock(&trace_types_lock);
863 if (!current_trace || current_trace != iter->trace)
866 atomic_inc(&trace_record_cmdline_disabled);
868 /* let the tracer grab locks here if needed */
869 if (current_trace->start)
870 current_trace->start(iter);
872 if (*pos != iter->pos) {
876 iter->prev_ent = NULL;
879 for_each_possible_cpu(i) {
880 iter->next_idx[i] = 0;
881 iter->next_page[i] = NULL;
884 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
889 p = s_next(m, p, &l);
895 static void s_stop(struct seq_file *m, void *p)
897 struct trace_iterator *iter = m->private;
899 atomic_dec(&trace_record_cmdline_disabled);
901 /* let the tracer release locks here if needed */
902 if (current_trace && current_trace == iter->trace && iter->trace->stop)
903 iter->trace->stop(iter);
905 mutex_unlock(&trace_types_lock);
909 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
911 #ifdef CONFIG_KALLSYMS
912 char str[KSYM_SYMBOL_LEN];
914 kallsyms_lookup(address, NULL, NULL, NULL, str);
916 return trace_seq_printf(s, fmt, str);
922 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
923 unsigned long address)
925 #ifdef CONFIG_KALLSYMS
926 char str[KSYM_SYMBOL_LEN];
928 sprint_symbol(str, address);
929 return trace_seq_printf(s, fmt, str);
935 # define IP_FMT "%08lx"
937 # define IP_FMT "%016lx"
941 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
946 return trace_seq_printf(s, "0");
948 if (sym_flags & TRACE_ITER_SYM_OFFSET)
949 ret = seq_print_sym_offset(s, "%s", ip);
951 ret = seq_print_sym_short(s, "%s", ip);
956 if (sym_flags & TRACE_ITER_SYM_ADDR)
957 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
961 static notrace void print_lat_help_header(struct seq_file *m)
963 seq_puts(m, "# _------=> CPU# \n");
964 seq_puts(m, "# / _-----=> irqs-off \n");
965 seq_puts(m, "# | / _----=> need-resched \n");
966 seq_puts(m, "# || / _---=> hardirq/softirq \n");
967 seq_puts(m, "# ||| / _--=> preempt-depth \n");
968 seq_puts(m, "# |||| / \n");
969 seq_puts(m, "# ||||| delay \n");
970 seq_puts(m, "# cmd pid ||||| time | caller \n");
971 seq_puts(m, "# \\ / ||||| \\ | / \n");
974 static notrace void print_func_help_header(struct seq_file *m)
976 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
977 seq_puts(m, "# | | | | |\n");
982 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
984 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
985 struct trace_array *tr = iter->tr;
986 struct trace_array_cpu *data = tr->data[tr->cpu];
987 struct tracer *type = current_trace;
988 unsigned long total = 0;
989 unsigned long entries = 0;
991 const char *name = "preemption";
996 for_each_possible_cpu(cpu) {
997 if (head_page(tr->data[cpu])) {
998 total += tr->data[cpu]->trace_idx;
999 if (tr->data[cpu]->trace_idx > tr->entries)
1000 entries += tr->entries;
1002 entries += tr->data[cpu]->trace_idx;
1006 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1008 seq_puts(m, "-----------------------------------"
1009 "---------------------------------\n");
1010 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1011 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1012 nsecs_to_usecs(data->saved_latency),
1016 #if defined(CONFIG_PREEMPT_NONE)
1018 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1020 #elif defined(CONFIG_PREEMPT_DESKTOP)
1025 /* These are reserved for later use */
1028 seq_printf(m, " #P:%d)\n", num_online_cpus());
1032 seq_puts(m, " -----------------\n");
1033 seq_printf(m, " | task: %.16s-%d "
1034 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1035 data->comm, data->pid, data->uid, data->nice,
1036 data->policy, data->rt_priority);
1037 seq_puts(m, " -----------------\n");
1039 if (data->critical_start) {
1040 seq_puts(m, " => started at: ");
1041 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1042 trace_print_seq(m, &iter->seq);
1043 seq_puts(m, "\n => ended at: ");
1044 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1045 trace_print_seq(m, &iter->seq);
1053 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1055 int hardirq, softirq;
1058 comm = trace_find_cmdline(entry->pid);
1060 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1061 trace_seq_printf(s, "%d", cpu);
1062 trace_seq_printf(s, "%c%c",
1063 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1064 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1066 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1067 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1068 if (hardirq && softirq)
1069 trace_seq_putc(s, 'H');
1072 trace_seq_putc(s, 'h');
1075 trace_seq_putc(s, 's');
1077 trace_seq_putc(s, '.');
1081 if (entry->preempt_count)
1082 trace_seq_printf(s, "%x", entry->preempt_count);
1084 trace_seq_puts(s, ".");
1087 unsigned long preempt_mark_thresh = 100;
1090 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1091 unsigned long rel_usecs)
1093 trace_seq_printf(s, " %4lldus", abs_usecs);
1094 if (rel_usecs > preempt_mark_thresh)
1095 trace_seq_puts(s, "!: ");
1096 else if (rel_usecs > 1)
1097 trace_seq_puts(s, "+: ");
1099 trace_seq_puts(s, " : ");
1102 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1105 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1107 struct trace_seq *s = &iter->seq;
1108 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1109 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1110 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1111 struct trace_entry *entry = iter->ent;
1112 unsigned long abs_usecs;
1113 unsigned long rel_usecs;
1119 rel_usecs = ns2usecs(next_entry->t - entry->t);
1120 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1123 comm = trace_find_cmdline(entry->pid);
1124 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1125 " %ld.%03ldms (+%ld.%03ldms): ",
1127 entry->pid, cpu, entry->flags,
1128 entry->preempt_count, trace_idx,
1131 abs_usecs % 1000, rel_usecs/1000,
1134 lat_print_generic(s, entry, cpu);
1135 lat_print_timestamp(s, abs_usecs, rel_usecs);
1137 switch (entry->type) {
1139 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1140 trace_seq_puts(s, " (");
1141 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1142 trace_seq_puts(s, ")\n");
1145 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1146 state_to_char[entry->ctx.prev_state] : 'X';
1147 comm = trace_find_cmdline(entry->ctx.next_pid);
1148 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1149 entry->ctx.prev_pid,
1150 entry->ctx.prev_prio,
1152 entry->ctx.next_pid,
1153 entry->ctx.next_prio,
1157 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1162 static notrace int print_trace_fmt(struct trace_iterator *iter)
1164 struct trace_seq *s = &iter->seq;
1165 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1166 struct trace_entry *entry;
1167 unsigned long usec_rem;
1168 unsigned long long t;
1176 comm = trace_find_cmdline(iter->ent->pid);
1178 t = ns2usecs(entry->t);
1179 usec_rem = do_div(t, 1000000ULL);
1180 secs = (unsigned long)t;
1182 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1185 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1188 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1192 switch (entry->type) {
1194 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1197 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1198 entry->fn.parent_ip) {
1199 ret = trace_seq_printf(s, " <-");
1202 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1207 ret = trace_seq_printf(s, "\n");
1212 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1213 state_to_char[entry->ctx.prev_state] : 'X';
1214 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1215 entry->ctx.prev_pid,
1216 entry->ctx.prev_prio,
1218 entry->ctx.next_pid,
1219 entry->ctx.next_prio);
1227 static notrace int print_raw_fmt(struct trace_iterator *iter)
1229 struct trace_seq *s = &iter->seq;
1230 struct trace_entry *entry;
1236 ret = trace_seq_printf(s, "%d %d %llu ",
1237 entry->pid, iter->cpu, entry->t);
1241 switch (entry->type) {
1243 ret = trace_seq_printf(s, "%x %x\n",
1244 entry->fn.ip, entry->fn.parent_ip);
1249 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1250 state_to_char[entry->ctx.prev_state] : 'X';
1251 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1252 entry->ctx.prev_pid,
1253 entry->ctx.prev_prio,
1255 entry->ctx.next_pid,
1256 entry->ctx.next_prio);
1264 static int trace_empty(struct trace_iterator *iter)
1266 struct trace_array_cpu *data;
1269 for_each_possible_cpu(cpu) {
1270 data = iter->tr->data[cpu];
1272 if (head_page(data) && data->trace_idx &&
1273 (data->trace_tail != data->trace_head ||
1274 data->trace_tail_idx != data->trace_head_idx))
1280 static int print_trace_line(struct trace_iterator *iter)
1282 if (trace_flags & TRACE_ITER_RAW)
1283 return print_raw_fmt(iter);
1285 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1286 return print_lat_fmt(iter, iter->idx, iter->cpu);
1288 return print_trace_fmt(iter);
1291 static int s_show(struct seq_file *m, void *v)
1293 struct trace_iterator *iter = v;
1295 if (iter->ent == NULL) {
1297 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1300 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1301 /* print nothing if the buffers are empty */
1302 if (trace_empty(iter))
1304 print_trace_header(m, iter);
1305 if (!(trace_flags & TRACE_ITER_VERBOSE))
1306 print_lat_help_header(m);
1308 if (!(trace_flags & TRACE_ITER_VERBOSE))
1309 print_func_help_header(m);
1312 print_trace_line(iter);
1313 trace_print_seq(m, &iter->seq);
1319 static struct seq_operations tracer_seq_ops = {
1326 static struct trace_iterator notrace *
1327 __tracing_open(struct inode *inode, struct file *file, int *ret)
1329 struct trace_iterator *iter;
1331 if (tracing_disabled) {
1336 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1342 mutex_lock(&trace_types_lock);
1343 if (current_trace && current_trace->print_max)
1346 iter->tr = inode->i_private;
1347 iter->trace = current_trace;
1350 /* TODO stop tracer */
1351 *ret = seq_open(file, &tracer_seq_ops);
1353 struct seq_file *m = file->private_data;
1356 /* stop the trace while dumping */
1360 if (iter->trace && iter->trace->open)
1361 iter->trace->open(iter);
1366 mutex_unlock(&trace_types_lock);
1372 int tracing_open_generic(struct inode *inode, struct file *filp)
1374 if (tracing_disabled)
1377 filp->private_data = inode->i_private;
1381 int tracing_release(struct inode *inode, struct file *file)
1383 struct seq_file *m = (struct seq_file *)file->private_data;
1384 struct trace_iterator *iter = m->private;
1386 mutex_lock(&trace_types_lock);
1387 if (iter->trace && iter->trace->close)
1388 iter->trace->close(iter);
1390 /* reenable tracing if it was previously enabled */
1393 mutex_unlock(&trace_types_lock);
1395 seq_release(inode, file);
1400 static int tracing_open(struct inode *inode, struct file *file)
1404 __tracing_open(inode, file, &ret);
1409 static int tracing_lt_open(struct inode *inode, struct file *file)
1411 struct trace_iterator *iter;
1414 iter = __tracing_open(inode, file, &ret);
1417 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1423 static notrace void *
1424 t_next(struct seq_file *m, void *v, loff_t *pos)
1426 struct tracer *t = m->private;
1438 static void *t_start(struct seq_file *m, loff_t *pos)
1440 struct tracer *t = m->private;
1443 mutex_lock(&trace_types_lock);
1444 for (; t && l < *pos; t = t_next(m, t, &l))
1450 static void t_stop(struct seq_file *m, void *p)
1452 mutex_unlock(&trace_types_lock);
1455 static int t_show(struct seq_file *m, void *v)
1457 struct tracer *t = v;
1462 seq_printf(m, "%s", t->name);
1471 static struct seq_operations show_traces_seq_ops = {
1478 static int show_traces_open(struct inode *inode, struct file *file)
1482 if (tracing_disabled)
1485 ret = seq_open(file, &show_traces_seq_ops);
1487 struct seq_file *m = file->private_data;
1488 m->private = trace_types;
1494 static struct file_operations tracing_fops = {
1495 .open = tracing_open,
1497 .llseek = seq_lseek,
1498 .release = tracing_release,
1501 static struct file_operations tracing_lt_fops = {
1502 .open = tracing_lt_open,
1504 .llseek = seq_lseek,
1505 .release = tracing_release,
1508 static struct file_operations show_traces_fops = {
1509 .open = show_traces_open,
1511 .release = seq_release,
1515 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1516 size_t cnt, loff_t *ppos)
1523 /* calulate max size */
1524 for (i = 0; trace_options[i]; i++) {
1525 len += strlen(trace_options[i]);
1526 len += 3; /* "no" and space */
1529 /* +2 for \n and \0 */
1530 buf = kmalloc(len + 2, GFP_KERNEL);
1534 for (i = 0; trace_options[i]; i++) {
1535 if (trace_flags & (1 << i))
1536 r += sprintf(buf + r, "%s ", trace_options[i]);
1538 r += sprintf(buf + r, "no%s ", trace_options[i]);
1541 r += sprintf(buf + r, "\n");
1542 WARN_ON(r >= len + 2);
1544 r = simple_read_from_buffer(ubuf, cnt, ppos,
1553 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1554 size_t cnt, loff_t *ppos)
1564 if (copy_from_user(&buf, ubuf, cnt))
1569 if (strncmp(buf, "no", 2) == 0) {
1574 for (i = 0; trace_options[i]; i++) {
1575 int len = strlen(trace_options[i]);
1577 if (strncmp(cmp, trace_options[i], len) == 0) {
1579 trace_flags &= ~(1 << i);
1581 trace_flags |= (1 << i);
1591 static struct file_operations tracing_iter_fops = {
1592 .open = tracing_open_generic,
1593 .read = tracing_iter_ctrl_read,
1594 .write = tracing_iter_ctrl_write,
1597 static const char readme_msg[] =
1598 "tracing mini-HOWTO:\n\n"
1600 "# mount -t debugfs nodev /debug\n\n"
1601 "# cat /debug/tracing/available_tracers\n"
1602 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1603 "# cat /debug/tracing/current_tracer\n"
1605 "# echo sched_switch > /debug/tracing/current_tracer\n"
1606 "# cat /debug/tracing/current_tracer\n"
1608 "# cat /debug/tracing/iter_ctrl\n"
1609 "noprint-parent nosym-offset nosym-addr noverbose\n"
1610 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1611 "# echo 1 > /debug/tracing/tracing_enabled\n"
1612 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1613 "echo 0 > /debug/tracing/tracing_enabled\n"
1617 tracing_readme_read(struct file *filp, char __user *ubuf,
1618 size_t cnt, loff_t *ppos)
1620 return simple_read_from_buffer(ubuf, cnt, ppos,
1621 readme_msg, strlen(readme_msg));
1624 static struct file_operations tracing_readme_fops = {
1625 .open = tracing_open_generic,
1626 .read = tracing_readme_read,
1631 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1632 size_t cnt, loff_t *ppos)
1634 struct trace_array *tr = filp->private_data;
1638 r = sprintf(buf, "%ld\n", tr->ctrl);
1639 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1643 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1644 size_t cnt, loff_t *ppos)
1646 struct trace_array *tr = filp->private_data;
1653 if (copy_from_user(&buf, ubuf, cnt))
1658 val = simple_strtoul(buf, NULL, 10);
1662 mutex_lock(&trace_types_lock);
1663 if (tr->ctrl ^ val) {
1671 if (current_trace && current_trace->ctrl_update)
1672 current_trace->ctrl_update(tr);
1674 mutex_unlock(&trace_types_lock);
1682 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1683 size_t cnt, loff_t *ppos)
1685 char buf[max_tracer_type_len+2];
1688 mutex_lock(&trace_types_lock);
1690 r = sprintf(buf, "%s\n", current_trace->name);
1692 r = sprintf(buf, "\n");
1693 mutex_unlock(&trace_types_lock);
1695 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1699 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1700 size_t cnt, loff_t *ppos)
1702 struct trace_array *tr = &global_trace;
1704 char buf[max_tracer_type_len+1];
1707 if (cnt > max_tracer_type_len)
1708 cnt = max_tracer_type_len;
1710 if (copy_from_user(&buf, ubuf, cnt))
1715 /* strip ending whitespace. */
1716 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1719 mutex_lock(&trace_types_lock);
1720 for (t = trace_types; t; t = t->next) {
1721 if (strcmp(t->name, buf) == 0)
1724 if (!t || t == current_trace)
1727 if (current_trace && current_trace->reset)
1728 current_trace->reset(tr);
1735 mutex_unlock(&trace_types_lock);
1743 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1744 size_t cnt, loff_t *ppos)
1746 unsigned long *ptr = filp->private_data;
1750 r = snprintf(buf, 64, "%ld\n",
1751 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1754 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1758 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1759 size_t cnt, loff_t *ppos)
1761 long *ptr = filp->private_data;
1768 if (copy_from_user(&buf, ubuf, cnt))
1773 val = simple_strtoul(buf, NULL, 10);
1780 static atomic_t tracing_reader;
1782 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1784 struct trace_iterator *iter;
1786 if (tracing_disabled)
1789 /* We only allow for reader of the pipe */
1790 if (atomic_inc_return(&tracing_reader) != 1) {
1791 atomic_dec(&tracing_reader);
1795 /* create a buffer to store the information to pass to userspace */
1796 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1800 iter->tr = &global_trace;
1802 filp->private_data = iter;
1807 static int tracing_release_pipe(struct inode *inode, struct file *file)
1809 struct trace_iterator *iter = file->private_data;
1812 atomic_dec(&tracing_reader);
1821 tracing_read_pipe(struct file *filp, char __user *ubuf,
1822 size_t cnt, loff_t *ppos)
1824 struct trace_iterator *iter = filp->private_data;
1825 struct trace_array_cpu *data;
1826 static cpumask_t mask;
1827 struct trace_entry *entry;
1829 unsigned long flags;
1835 /* return any leftover data */
1836 if (iter->seq.len > start) {
1837 len = iter->seq.len - start;
1840 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1849 trace_seq_reset(&iter->seq);
1852 while (trace_empty(iter)) {
1854 * This is a make-shift waitqueue. The reason we don't use
1855 * an actual wait queue is because:
1856 * 1) we only ever have one waiter
1857 * 2) the tracing, traces all functions, we don't want
1858 * the overhead of calling wake_up and friends
1859 * (and tracing them too)
1860 * Anyway, this is really very primitive wakeup.
1862 set_current_state(TASK_INTERRUPTIBLE);
1863 iter->tr->waiter = current;
1865 /* sleep for one second, and try again. */
1866 schedule_timeout(HZ);
1868 iter->tr->waiter = NULL;
1870 if (signal_pending(current))
1874 * We block until we read something and tracing is disabled.
1875 * We still block if tracing is disabled, but we have never
1876 * read anything. This allows a user to cat this file, and
1877 * then enable tracing. But after we have read something,
1878 * we give an EOF when tracing is again disabled.
1880 * iter->pos will be 0 if we haven't read anything.
1882 if (!tracer_enabled && iter->pos)
1888 /* stop when tracing is finished */
1889 if (trace_empty(iter))
1892 if (cnt >= PAGE_SIZE)
1893 cnt = PAGE_SIZE - 1;
1895 memset(iter, 0, sizeof(*iter));
1896 iter->tr = &global_trace;
1900 * We need to stop all tracing on all CPUS to read the
1901 * the next buffer. This is a bit expensive, but is
1902 * not done often. We fill all what we can read,
1903 * and then release the locks again.
1907 local_irq_save(flags);
1908 for_each_possible_cpu(cpu) {
1909 data = iter->tr->data[cpu];
1911 if (!head_page(data) || !data->trace_idx)
1914 atomic_inc(&data->disabled);
1915 spin_lock(&data->lock);
1919 while ((entry = find_next_entry_inc(iter)) != NULL) {
1920 ret = print_trace_line(iter);
1924 trace_consume(iter);
1926 if (iter->seq.len >= cnt)
1930 for_each_cpu_mask(cpu, mask) {
1931 data = iter->tr->data[cpu];
1932 spin_unlock(&data->lock);
1933 atomic_dec(&data->disabled);
1935 local_irq_restore(flags);
1937 /* Now copy what we have to the user */
1938 read = iter->seq.len;
1942 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1944 if (read < iter->seq.len)
1947 trace_seq_reset(&iter->seq);
1955 static struct file_operations tracing_max_lat_fops = {
1956 .open = tracing_open_generic,
1957 .read = tracing_max_lat_read,
1958 .write = tracing_max_lat_write,
1961 static struct file_operations tracing_ctrl_fops = {
1962 .open = tracing_open_generic,
1963 .read = tracing_ctrl_read,
1964 .write = tracing_ctrl_write,
1967 static struct file_operations set_tracer_fops = {
1968 .open = tracing_open_generic,
1969 .read = tracing_set_trace_read,
1970 .write = tracing_set_trace_write,
1973 static struct file_operations tracing_pipe_fops = {
1974 .open = tracing_open_pipe,
1975 .read = tracing_read_pipe,
1976 .release = tracing_release_pipe,
1979 #ifdef CONFIG_DYNAMIC_FTRACE
1982 tracing_read_long(struct file *filp, char __user *ubuf,
1983 size_t cnt, loff_t *ppos)
1985 unsigned long *p = filp->private_data;
1989 r = sprintf(buf, "%ld\n", *p);
1991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1994 static struct file_operations tracing_read_long_fops = {
1995 .open = tracing_open_generic,
1996 .read = tracing_read_long,
2000 static struct dentry *d_tracer;
2002 struct dentry *tracing_init_dentry(void)
2009 d_tracer = debugfs_create_dir("tracing", NULL);
2011 if (!d_tracer && !once) {
2013 pr_warning("Could not create debugfs directory 'tracing'\n");
2020 #ifdef CONFIG_FTRACE_SELFTEST
2021 /* Let selftest have access to static functions in this file */
2022 #include "trace_selftest.c"
2025 static __init void tracer_init_debugfs(void)
2027 struct dentry *d_tracer;
2028 struct dentry *entry;
2030 d_tracer = tracing_init_dentry();
2032 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2033 &global_trace, &tracing_ctrl_fops);
2035 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2037 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2038 NULL, &tracing_iter_fops);
2040 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2042 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2043 &global_trace, &tracing_lt_fops);
2045 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2047 entry = debugfs_create_file("trace", 0444, d_tracer,
2048 &global_trace, &tracing_fops);
2050 pr_warning("Could not create debugfs 'trace' entry\n");
2052 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2053 &global_trace, &show_traces_fops);
2055 pr_warning("Could not create debugfs 'trace' entry\n");
2057 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2058 &global_trace, &set_tracer_fops);
2060 pr_warning("Could not create debugfs 'trace' entry\n");
2062 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2063 &tracing_max_latency,
2064 &tracing_max_lat_fops);
2066 pr_warning("Could not create debugfs "
2067 "'tracing_max_latency' entry\n");
2069 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2070 &tracing_thresh, &tracing_max_lat_fops);
2072 pr_warning("Could not create debugfs "
2073 "'tracing_threash' entry\n");
2074 entry = debugfs_create_file("README", 0644, d_tracer,
2075 NULL, &tracing_readme_fops);
2077 pr_warning("Could not create debugfs 'README' entry\n");
2079 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2080 NULL, &tracing_pipe_fops);
2082 pr_warning("Could not create debugfs "
2083 "'tracing_threash' entry\n");
2085 #ifdef CONFIG_DYNAMIC_FTRACE
2086 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2087 &ftrace_update_tot_cnt,
2088 &tracing_read_long_fops);
2090 pr_warning("Could not create debugfs "
2091 "'dyn_ftrace_total_info' entry\n");
2095 /* dummy trace to disable tracing */
2096 static struct tracer no_tracer __read_mostly =
2101 static int trace_alloc_page(void)
2103 struct trace_array_cpu *data;
2104 struct page *page, *tmp;
2109 /* first allocate a page for each CPU */
2110 for_each_possible_cpu(i) {
2111 array = (void *)__get_free_page(GFP_KERNEL);
2112 if (array == NULL) {
2113 printk(KERN_ERR "tracer: failed to allocate page"
2114 "for trace buffer!\n");
2118 page = virt_to_page(array);
2119 list_add(&page->lru, &pages);
2121 /* Only allocate if we are actually using the max trace */
2122 #ifdef CONFIG_TRACER_MAX_TRACE
2123 array = (void *)__get_free_page(GFP_KERNEL);
2124 if (array == NULL) {
2125 printk(KERN_ERR "tracer: failed to allocate page"
2126 "for trace buffer!\n");
2129 page = virt_to_page(array);
2130 list_add(&page->lru, &pages);
2134 /* Now that we successfully allocate a page per CPU, add them */
2135 for_each_possible_cpu(i) {
2136 data = global_trace.data[i];
2137 spin_lock_init(&data->lock);
2138 lockdep_set_class(&data->lock, &data->lock_key);
2139 page = list_entry(pages.next, struct page, lru);
2140 list_del_init(&page->lru);
2141 list_add_tail(&page->lru, &data->trace_pages);
2144 #ifdef CONFIG_TRACER_MAX_TRACE
2145 data = max_tr.data[i];
2146 spin_lock_init(&data->lock);
2147 lockdep_set_class(&data->lock, &data->lock_key);
2148 page = list_entry(pages.next, struct page, lru);
2149 list_del_init(&page->lru);
2150 list_add_tail(&page->lru, &data->trace_pages);
2154 global_trace.entries += ENTRIES_PER_PAGE;
2159 list_for_each_entry_safe(page, tmp, &pages, lru) {
2160 list_del_init(&page->lru);
2166 __init static int tracer_alloc_buffers(void)
2168 struct trace_array_cpu *data;
2175 /* Allocate the first page for all buffers */
2176 for_each_possible_cpu(i) {
2177 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2178 max_tr.data[i] = &per_cpu(max_data, i);
2180 array = (void *)__get_free_page(GFP_KERNEL);
2181 if (array == NULL) {
2182 printk(KERN_ERR "tracer: failed to allocate page"
2183 "for trace buffer!\n");
2187 /* set the array to the list */
2188 INIT_LIST_HEAD(&data->trace_pages);
2189 page = virt_to_page(array);
2190 list_add(&page->lru, &data->trace_pages);
2191 /* use the LRU flag to differentiate the two buffers */
2194 /* Only allocate if we are actually using the max trace */
2195 #ifdef CONFIG_TRACER_MAX_TRACE
2196 array = (void *)__get_free_page(GFP_KERNEL);
2197 if (array == NULL) {
2198 printk(KERN_ERR "tracer: failed to allocate page"
2199 "for trace buffer!\n");
2203 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2204 page = virt_to_page(array);
2205 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2211 * Since we allocate by orders of pages, we may be able to
2214 global_trace.entries = ENTRIES_PER_PAGE;
2217 while (global_trace.entries < trace_nr_entries) {
2218 if (trace_alloc_page())
2222 max_tr.entries = global_trace.entries;
2224 pr_info("tracer: %d pages allocated for %ld",
2225 pages, trace_nr_entries);
2226 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2227 pr_info(" actual entries %ld\n", global_trace.entries);
2229 tracer_init_debugfs();
2231 trace_init_cmdlines();
2233 register_tracer(&no_tracer);
2234 current_trace = &no_tracer;
2236 /* All seems OK, enable tracing */
2237 tracing_disabled = 0;
2242 for (i-- ; i >= 0; i--) {
2243 struct page *page, *tmp;
2244 struct trace_array_cpu *data = global_trace.data[i];
2247 list_for_each_entry_safe(page, tmp,
2248 &data->trace_pages, lru) {
2249 list_del_init(&page->lru);
2254 #ifdef CONFIG_TRACER_MAX_TRACE
2255 data = max_tr.data[i];
2257 list_for_each_entry_safe(page, tmp,
2258 &data->trace_pages, lru) {
2259 list_del_init(&page->lru);
2267 fs_initcall(tracer_alloc_buffers);