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 atomic_t tracer_counter;
103 static struct trace_array global_trace;
105 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
107 static struct trace_array max_tr;
109 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
111 static int tracer_enabled;
112 static unsigned long trace_nr_entries = 16384UL;
114 static struct tracer *trace_types __read_mostly;
115 static struct tracer *current_trace __read_mostly;
116 static int max_tracer_type_len;
118 static DEFINE_MUTEX(trace_types_lock);
120 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
122 static int __init set_nr_entries(char *str)
126 trace_nr_entries = simple_strtoul(str, &str, 0);
129 __setup("trace_entries=", set_nr_entries);
131 unsigned long nsecs_to_usecs(unsigned long nsecs)
137 __TRACE_FIRST_TYPE = 0,
145 enum trace_flag_type {
146 TRACE_FLAG_IRQS_OFF = 0x01,
147 TRACE_FLAG_NEED_RESCHED = 0x02,
148 TRACE_FLAG_HARDIRQ = 0x04,
149 TRACE_FLAG_SOFTIRQ = 0x08,
152 enum trace_iterator_flags {
153 TRACE_ITER_PRINT_PARENT = 0x01,
154 TRACE_ITER_SYM_OFFSET = 0x02,
155 TRACE_ITER_SYM_ADDR = 0x04,
156 TRACE_ITER_VERBOSE = 0x08,
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[] = {
171 static unsigned trace_flags;
173 static DEFINE_SPINLOCK(ftrace_max_lock);
176 * Copy the new maximum trace into the separate maximum-trace
177 * structure. (this way the maximum trace is permanently saved,
178 * for later retrieval via /debugfs/tracing/latency_trace)
181 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
183 struct trace_array_cpu *data = tr->data[cpu];
186 max_tr.time_start = data->preempt_timestamp;
188 data = max_tr.data[cpu];
189 data->saved_latency = tracing_max_latency;
191 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
192 data->pid = tsk->pid;
193 data->uid = tsk->uid;
194 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
195 data->policy = tsk->policy;
196 data->rt_priority = tsk->rt_priority;
198 /* record this tasks comm */
199 tracing_record_cmdline(current);
202 void check_pages(struct trace_array_cpu *data)
204 struct page *page, *tmp;
206 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
207 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
209 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
210 BUG_ON(page->lru.next->prev != &page->lru);
211 BUG_ON(page->lru.prev->next != &page->lru);
215 void *head_page(struct trace_array_cpu *data)
220 if (list_empty(&data->trace_pages))
223 page = list_entry(data->trace_pages.next, struct page, lru);
224 BUG_ON(&page->lru == &data->trace_pages);
226 return page_address(page);
230 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
232 int len = (PAGE_SIZE - 1) - s->len;
240 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
243 /* If we can't write it all, don't bother writing anything */
253 trace_seq_puts(struct trace_seq *s, const char *str)
255 int len = strlen(str);
257 if (len > ((PAGE_SIZE - 1) - s->len))
260 memcpy(s->buffer + s->len, str, len);
267 trace_seq_putc(struct trace_seq *s, unsigned char c)
269 if (s->len >= (PAGE_SIZE - 1))
272 s->buffer[s->len++] = c;
278 trace_seq_reset(struct trace_seq *s)
284 trace_print_seq(struct seq_file *m, struct trace_seq *s)
286 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
289 seq_puts(m, s->buffer);
295 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
297 struct list_head flip_pages;
299 INIT_LIST_HEAD(&flip_pages);
301 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
302 sizeof(struct trace_array_cpu) -
303 offsetof(struct trace_array_cpu, trace_head_idx));
307 list_splice_init(&tr1->trace_pages, &flip_pages);
308 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
309 list_splice_init(&flip_pages, &tr2->trace_pages);
310 BUG_ON(!list_empty(&flip_pages));
316 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
318 struct trace_array_cpu *data;
321 WARN_ON_ONCE(!irqs_disabled());
322 spin_lock(&ftrace_max_lock);
323 /* clear out all the previous traces */
324 for_each_possible_cpu(i) {
326 flip_trace(max_tr.data[i], data);
330 __update_max_tr(tr, tsk, cpu);
331 spin_unlock(&ftrace_max_lock);
335 * update_max_tr_single - only copy one trace over, and reset the rest
337 * @tsk - task with the latency
338 * @cpu - the cpu of the buffer to copy.
341 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
343 struct trace_array_cpu *data = tr->data[cpu];
346 WARN_ON_ONCE(!irqs_disabled());
347 spin_lock(&ftrace_max_lock);
348 for_each_possible_cpu(i)
349 tracing_reset(max_tr.data[i]);
351 flip_trace(max_tr.data[cpu], data);
354 __update_max_tr(tr, tsk, cpu);
355 spin_unlock(&ftrace_max_lock);
358 int register_tracer(struct tracer *type)
365 pr_info("Tracer must have a name\n");
369 mutex_lock(&trace_types_lock);
370 for (t = trace_types; t; t = t->next) {
371 if (strcmp(type->name, t->name) == 0) {
373 pr_info("Trace %s already registered\n",
380 #ifdef CONFIG_FTRACE_STARTUP_TEST
381 if (type->selftest) {
382 struct tracer *saved_tracer = current_trace;
383 struct trace_array_cpu *data;
384 struct trace_array *tr = &global_trace;
385 int saved_ctrl = tr->ctrl;
388 * Run a selftest on this tracer.
389 * Here we reset the trace buffer, and set the current
390 * tracer to be this tracer. The tracer can then run some
391 * internal tracing to verify that everything is in order.
392 * If we fail, we do not register this tracer.
394 for_each_possible_cpu(i) {
396 if (!head_page(data))
400 current_trace = type;
402 /* the test is responsible for initializing and enabling */
403 pr_info("Testing tracer %s: ", type->name);
404 ret = type->selftest(type, tr);
405 /* the test is responsible for resetting too */
406 current_trace = saved_tracer;
407 tr->ctrl = saved_ctrl;
409 printk(KERN_CONT "FAILED!\n");
412 /* Only reset on passing, to avoid touching corrupted buffers */
413 for_each_possible_cpu(i) {
415 if (!head_page(data))
419 printk(KERN_CONT "PASSED\n");
423 type->next = trace_types;
425 len = strlen(type->name);
426 if (len > max_tracer_type_len)
427 max_tracer_type_len = len;
430 mutex_unlock(&trace_types_lock);
435 void unregister_tracer(struct tracer *type)
440 mutex_lock(&trace_types_lock);
441 for (t = &trace_types; *t; t = &(*t)->next) {
445 pr_info("Trace %s not registered\n", type->name);
450 if (strlen(type->name) != max_tracer_type_len)
453 max_tracer_type_len = 0;
454 for (t = &trace_types; *t; t = &(*t)->next) {
455 len = strlen((*t)->name);
456 if (len > max_tracer_type_len)
457 max_tracer_type_len = len;
460 mutex_unlock(&trace_types_lock);
463 notrace void tracing_reset(struct trace_array_cpu *data)
466 data->trace_head = data->trace_tail = head_page(data);
467 data->trace_head_idx = 0;
468 data->trace_tail_idx = 0;
473 function_trace_call(unsigned long ip, unsigned long parent_ip)
475 struct trace_array *tr = &global_trace;
476 struct trace_array_cpu *data;
481 if (unlikely(!tracer_enabled))
484 local_irq_save(flags);
485 cpu = raw_smp_processor_id();
486 data = tr->data[cpu];
487 disabled = atomic_inc_return(&data->disabled);
489 if (likely(disabled == 1))
490 ftrace(tr, data, ip, parent_ip, flags);
492 atomic_dec(&data->disabled);
493 local_irq_restore(flags);
496 static struct ftrace_ops trace_ops __read_mostly =
498 .func = function_trace_call,
502 notrace void tracing_start_function_trace(void)
504 register_ftrace_function(&trace_ops);
507 notrace void tracing_stop_function_trace(void)
509 unregister_ftrace_function(&trace_ops);
512 #define SAVED_CMDLINES 128
513 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
514 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
515 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
516 static int cmdline_idx;
517 static DEFINE_SPINLOCK(trace_cmdline_lock);
518 atomic_t trace_record_cmdline_disabled;
520 static void trace_init_cmdlines(void)
522 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
523 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
527 notrace void trace_stop_cmdline_recording(void);
529 static notrace void trace_save_cmdline(struct task_struct *tsk)
534 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
538 * It's not the end of the world if we don't get
539 * the lock, but we also don't want to spin
540 * nor do we want to disable interrupts,
541 * so if we miss here, then better luck next time.
543 if (!spin_trylock(&trace_cmdline_lock))
546 idx = map_pid_to_cmdline[tsk->pid];
547 if (idx >= SAVED_CMDLINES) {
548 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
550 map = map_cmdline_to_pid[idx];
551 if (map <= PID_MAX_DEFAULT)
552 map_pid_to_cmdline[map] = (unsigned)-1;
554 map_pid_to_cmdline[tsk->pid] = idx;
559 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
561 spin_unlock(&trace_cmdline_lock);
564 static notrace char *trace_find_cmdline(int pid)
566 char *cmdline = "<...>";
572 if (pid > PID_MAX_DEFAULT)
575 map = map_pid_to_cmdline[pid];
576 if (map >= SAVED_CMDLINES)
579 cmdline = saved_cmdlines[map];
585 notrace void tracing_record_cmdline(struct task_struct *tsk)
587 if (atomic_read(&trace_record_cmdline_disabled))
590 trace_save_cmdline(tsk);
593 static inline notrace struct list_head *
594 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
597 * Roundrobin - but skip the head (which is not a real page):
600 if (unlikely(next == &data->trace_pages))
602 BUG_ON(next == &data->trace_pages);
607 static inline notrace void *
608 trace_next_page(struct trace_array_cpu *data, void *addr)
610 struct list_head *next;
613 page = virt_to_page(addr);
615 next = trace_next_list(data, &page->lru);
616 page = list_entry(next, struct page, lru);
618 return page_address(page);
621 static inline notrace struct trace_entry *
622 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
624 unsigned long idx, idx_next;
625 struct trace_entry *entry;
628 idx = data->trace_head_idx;
631 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
633 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
635 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
636 data->trace_head = trace_next_page(data, data->trace_head);
640 if (data->trace_head == data->trace_tail &&
641 idx_next == data->trace_tail_idx) {
643 data->trace_tail_idx++;
644 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
646 trace_next_page(data, data->trace_tail);
647 data->trace_tail_idx = 0;
651 data->trace_head_idx = idx_next;
656 static inline notrace void
657 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
659 struct task_struct *tsk = current;
662 pc = preempt_count();
664 entry->idx = atomic_inc_return(&tracer_counter);
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 (!next || (long)(next->idx - ent->idx) > 0)) {
774 trace_iterator_increment(struct trace_iterator *iter)
777 iter->next_idx[iter->cpu]++;
778 iter->next_page_idx[iter->cpu]++;
779 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
780 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
782 iter->next_page_idx[iter->cpu] = 0;
783 iter->next_page[iter->cpu] =
784 trace_next_list(data, iter->next_page[iter->cpu]);
789 trace_consume(struct trace_iterator *iter)
791 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
793 data->trace_tail_idx++;
794 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
795 data->trace_tail = trace_next_page(data, data->trace_tail);
796 data->trace_tail_idx = 0;
799 /* Check if we empty it, then reset the index */
800 if (data->trace_head == data->trace_tail &&
801 data->trace_head_idx == data->trace_tail_idx)
804 trace_iterator_increment(iter);
807 static notrace void *
808 find_next_entry_inc(struct trace_iterator *iter)
810 struct trace_entry *next;
813 next = find_next_entry(iter, &next_cpu);
815 iter->prev_ent = iter->ent;
816 iter->prev_cpu = iter->cpu;
819 iter->cpu = next_cpu;
822 trace_iterator_increment(iter);
824 return next ? iter : NULL;
827 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
829 struct trace_iterator *iter = m->private;
830 void *last_ent = iter->ent;
836 /* can't go backwards */
841 ent = find_next_entry_inc(iter);
845 while (ent && iter->idx < i)
846 ent = find_next_entry_inc(iter);
850 if (last_ent && !ent)
851 seq_puts(m, "\n\nvim:ft=help\n");
856 static void *s_start(struct seq_file *m, loff_t *pos)
858 struct trace_iterator *iter = m->private;
863 mutex_lock(&trace_types_lock);
865 if (!current_trace || current_trace != iter->trace)
868 atomic_inc(&trace_record_cmdline_disabled);
870 /* let the tracer grab locks here if needed */
871 if (current_trace->start)
872 current_trace->start(iter);
874 if (*pos != iter->pos) {
878 iter->prev_ent = NULL;
881 for_each_possible_cpu(i) {
882 iter->next_idx[i] = 0;
883 iter->next_page[i] = NULL;
886 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
891 p = s_next(m, p, &l);
897 static void s_stop(struct seq_file *m, void *p)
899 struct trace_iterator *iter = m->private;
901 atomic_dec(&trace_record_cmdline_disabled);
903 /* let the tracer release locks here if needed */
904 if (current_trace && current_trace == iter->trace && iter->trace->stop)
905 iter->trace->stop(iter);
907 mutex_unlock(&trace_types_lock);
911 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
913 #ifdef CONFIG_KALLSYMS
914 char str[KSYM_SYMBOL_LEN];
916 kallsyms_lookup(address, NULL, NULL, NULL, str);
918 return trace_seq_printf(s, fmt, str);
924 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
925 unsigned long address)
927 #ifdef CONFIG_KALLSYMS
928 char str[KSYM_SYMBOL_LEN];
930 sprint_symbol(str, address);
931 return trace_seq_printf(s, fmt, str);
937 # define IP_FMT "%08lx"
939 # define IP_FMT "%016lx"
943 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
948 return trace_seq_printf(s, "0");
950 if (sym_flags & TRACE_ITER_SYM_OFFSET)
951 ret = seq_print_sym_offset(s, "%s", ip);
953 ret = seq_print_sym_short(s, "%s", ip);
958 if (sym_flags & TRACE_ITER_SYM_ADDR)
959 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
963 static notrace void print_lat_help_header(struct seq_file *m)
965 seq_puts(m, "# _------=> CPU# \n");
966 seq_puts(m, "# / _-----=> irqs-off \n");
967 seq_puts(m, "# | / _----=> need-resched \n");
968 seq_puts(m, "# || / _---=> hardirq/softirq \n");
969 seq_puts(m, "# ||| / _--=> preempt-depth \n");
970 seq_puts(m, "# |||| / \n");
971 seq_puts(m, "# ||||| delay \n");
972 seq_puts(m, "# cmd pid ||||| time | caller \n");
973 seq_puts(m, "# \\ / ||||| \\ | / \n");
976 static notrace void print_func_help_header(struct seq_file *m)
978 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
979 seq_puts(m, "# | | | | |\n");
984 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
986 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
987 struct trace_array *tr = iter->tr;
988 struct trace_array_cpu *data = tr->data[tr->cpu];
989 struct tracer *type = current_trace;
990 unsigned long total = 0;
991 unsigned long entries = 0;
993 const char *name = "preemption";
998 for_each_possible_cpu(cpu) {
999 if (head_page(tr->data[cpu])) {
1000 total += tr->data[cpu]->trace_idx;
1001 if (tr->data[cpu]->trace_idx > tr->entries)
1002 entries += tr->entries;
1004 entries += tr->data[cpu]->trace_idx;
1008 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1010 seq_puts(m, "-----------------------------------"
1011 "---------------------------------\n");
1012 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1013 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1014 nsecs_to_usecs(data->saved_latency),
1018 #if defined(CONFIG_PREEMPT_NONE)
1020 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1022 #elif defined(CONFIG_PREEMPT_DESKTOP)
1027 /* These are reserved for later use */
1030 seq_printf(m, " #P:%d)\n", num_online_cpus());
1034 seq_puts(m, " -----------------\n");
1035 seq_printf(m, " | task: %.16s-%d "
1036 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1037 data->comm, data->pid, data->uid, data->nice,
1038 data->policy, data->rt_priority);
1039 seq_puts(m, " -----------------\n");
1041 if (data->critical_start) {
1042 seq_puts(m, " => started at: ");
1043 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1044 trace_print_seq(m, &iter->seq);
1045 seq_puts(m, "\n => ended at: ");
1046 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1047 trace_print_seq(m, &iter->seq);
1055 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1057 int hardirq, softirq;
1060 comm = trace_find_cmdline(entry->pid);
1062 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1063 trace_seq_printf(s, "%d", cpu);
1064 trace_seq_printf(s, "%c%c",
1065 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1066 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1068 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1069 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1070 if (hardirq && softirq)
1071 trace_seq_putc(s, 'H');
1074 trace_seq_putc(s, 'h');
1077 trace_seq_putc(s, 's');
1079 trace_seq_putc(s, '.');
1083 if (entry->preempt_count)
1084 trace_seq_printf(s, "%x", entry->preempt_count);
1086 trace_seq_puts(s, ".");
1089 unsigned long preempt_mark_thresh = 100;
1092 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1093 unsigned long rel_usecs)
1095 trace_seq_printf(s, " %4lldus", abs_usecs);
1096 if (rel_usecs > preempt_mark_thresh)
1097 trace_seq_puts(s, "!: ");
1098 else if (rel_usecs > 1)
1099 trace_seq_puts(s, "+: ");
1101 trace_seq_puts(s, " : ");
1104 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1107 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1109 struct trace_seq *s = &iter->seq;
1110 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1111 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1112 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1113 struct trace_entry *entry = iter->ent;
1114 unsigned long abs_usecs;
1115 unsigned long rel_usecs;
1121 rel_usecs = ns2usecs(next_entry->t - entry->t);
1122 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1125 comm = trace_find_cmdline(entry->pid);
1126 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1127 " %ld.%03ldms (+%ld.%03ldms): ",
1129 entry->pid, cpu, entry->flags,
1130 entry->preempt_count, trace_idx,
1133 abs_usecs % 1000, rel_usecs/1000,
1136 lat_print_generic(s, entry, cpu);
1137 lat_print_timestamp(s, abs_usecs, rel_usecs);
1139 switch (entry->type) {
1141 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1142 trace_seq_puts(s, " (");
1143 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1144 trace_seq_puts(s, ")\n");
1147 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1148 state_to_char[entry->ctx.prev_state] : 'X';
1149 comm = trace_find_cmdline(entry->ctx.next_pid);
1150 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1151 entry->ctx.prev_pid,
1152 entry->ctx.prev_prio,
1154 entry->ctx.next_pid,
1155 entry->ctx.next_prio,
1159 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1163 static notrace void sync_time_offset(struct trace_iterator *iter)
1165 struct trace_array_cpu *prev_array, *array;
1166 struct trace_entry *prev_entry, *entry;
1170 prev_entry = iter->prev_ent;
1174 prev_array = iter->tr->data[iter->prev_cpu];
1175 array = iter->tr->data[iter->cpu];
1177 prev_t = prev_entry->t + prev_array->time_offset;
1178 t = entry->t + array->time_offset;
1181 * If time goes backwards we increase the offset of
1182 * the current array, to not have observable time warps.
1183 * This will quickly synchronize the time offsets of
1187 array->time_offset += prev_t - t;
1191 print_trace_fmt(struct trace_iterator *iter)
1193 struct trace_seq *s = &iter->seq;
1194 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1195 struct trace_entry *entry;
1196 unsigned long usec_rem;
1197 unsigned long long t;
1203 sync_time_offset(iter);
1206 comm = trace_find_cmdline(iter->ent->pid);
1208 t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1209 usec_rem = do_div(t, 1000000ULL);
1210 secs = (unsigned long)t;
1212 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1215 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1218 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1222 switch (entry->type) {
1224 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1227 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1228 entry->fn.parent_ip) {
1229 ret = trace_seq_printf(s, " <-");
1232 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1237 ret = trace_seq_printf(s, "\n");
1242 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1243 state_to_char[entry->ctx.prev_state] : 'X';
1244 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1245 entry->ctx.prev_pid,
1246 entry->ctx.prev_prio,
1248 entry->ctx.next_pid,
1249 entry->ctx.next_prio);
1257 static int trace_empty(struct trace_iterator *iter)
1259 struct trace_array_cpu *data;
1262 for_each_possible_cpu(cpu) {
1263 data = iter->tr->data[cpu];
1265 if (head_page(data) && data->trace_idx &&
1266 (data->trace_tail != data->trace_head ||
1267 data->trace_tail_idx != data->trace_head_idx))
1273 static int s_show(struct seq_file *m, void *v)
1275 struct trace_iterator *iter = v;
1277 if (iter->ent == NULL) {
1279 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1282 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1283 /* print nothing if the buffers are empty */
1284 if (trace_empty(iter))
1286 print_trace_header(m, iter);
1287 if (!(trace_flags & TRACE_ITER_VERBOSE))
1288 print_lat_help_header(m);
1290 if (!(trace_flags & TRACE_ITER_VERBOSE))
1291 print_func_help_header(m);
1294 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1295 print_lat_fmt(iter, iter->idx, iter->cpu);
1297 print_trace_fmt(iter);
1298 trace_print_seq(m, &iter->seq);
1304 static struct seq_operations tracer_seq_ops = {
1311 static struct trace_iterator notrace *
1312 __tracing_open(struct inode *inode, struct file *file, int *ret)
1314 struct trace_iterator *iter;
1316 if (tracing_disabled) {
1321 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1327 mutex_lock(&trace_types_lock);
1328 if (current_trace && current_trace->print_max)
1331 iter->tr = inode->i_private;
1332 iter->trace = current_trace;
1335 /* TODO stop tracer */
1336 *ret = seq_open(file, &tracer_seq_ops);
1338 struct seq_file *m = file->private_data;
1341 /* stop the trace while dumping */
1345 if (iter->trace && iter->trace->open)
1346 iter->trace->open(iter);
1351 mutex_unlock(&trace_types_lock);
1357 int tracing_open_generic(struct inode *inode, struct file *filp)
1359 if (tracing_disabled)
1362 filp->private_data = inode->i_private;
1366 int tracing_release(struct inode *inode, struct file *file)
1368 struct seq_file *m = (struct seq_file *)file->private_data;
1369 struct trace_iterator *iter = m->private;
1371 mutex_lock(&trace_types_lock);
1372 if (iter->trace && iter->trace->close)
1373 iter->trace->close(iter);
1375 /* reenable tracing if it was previously enabled */
1378 mutex_unlock(&trace_types_lock);
1380 seq_release(inode, file);
1385 static int tracing_open(struct inode *inode, struct file *file)
1389 __tracing_open(inode, file, &ret);
1394 static int tracing_lt_open(struct inode *inode, struct file *file)
1396 struct trace_iterator *iter;
1399 iter = __tracing_open(inode, file, &ret);
1402 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1408 static notrace void *
1409 t_next(struct seq_file *m, void *v, loff_t *pos)
1411 struct tracer *t = m->private;
1423 static void *t_start(struct seq_file *m, loff_t *pos)
1425 struct tracer *t = m->private;
1428 mutex_lock(&trace_types_lock);
1429 for (; t && l < *pos; t = t_next(m, t, &l))
1435 static void t_stop(struct seq_file *m, void *p)
1437 mutex_unlock(&trace_types_lock);
1440 static int t_show(struct seq_file *m, void *v)
1442 struct tracer *t = v;
1447 seq_printf(m, "%s", t->name);
1456 static struct seq_operations show_traces_seq_ops = {
1463 static int show_traces_open(struct inode *inode, struct file *file)
1467 if (tracing_disabled)
1470 ret = seq_open(file, &show_traces_seq_ops);
1472 struct seq_file *m = file->private_data;
1473 m->private = trace_types;
1479 static struct file_operations tracing_fops = {
1480 .open = tracing_open,
1482 .llseek = seq_lseek,
1483 .release = tracing_release,
1486 static struct file_operations tracing_lt_fops = {
1487 .open = tracing_lt_open,
1489 .llseek = seq_lseek,
1490 .release = tracing_release,
1493 static struct file_operations show_traces_fops = {
1494 .open = show_traces_open,
1496 .release = seq_release,
1500 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1501 size_t cnt, loff_t *ppos)
1508 /* calulate max size */
1509 for (i = 0; trace_options[i]; i++) {
1510 len += strlen(trace_options[i]);
1511 len += 3; /* "no" and space */
1514 /* +2 for \n and \0 */
1515 buf = kmalloc(len + 2, GFP_KERNEL);
1519 for (i = 0; trace_options[i]; i++) {
1520 if (trace_flags & (1 << i))
1521 r += sprintf(buf + r, "%s ", trace_options[i]);
1523 r += sprintf(buf + r, "no%s ", trace_options[i]);
1526 r += sprintf(buf + r, "\n");
1527 WARN_ON(r >= len + 2);
1529 r = simple_read_from_buffer(ubuf, cnt, ppos,
1538 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1539 size_t cnt, loff_t *ppos)
1549 if (copy_from_user(&buf, ubuf, cnt))
1554 if (strncmp(buf, "no", 2) == 0) {
1559 for (i = 0; trace_options[i]; i++) {
1560 int len = strlen(trace_options[i]);
1562 if (strncmp(cmp, trace_options[i], len) == 0) {
1564 trace_flags &= ~(1 << i);
1566 trace_flags |= (1 << i);
1576 static struct file_operations tracing_iter_fops = {
1577 .open = tracing_open_generic,
1578 .read = tracing_iter_ctrl_read,
1579 .write = tracing_iter_ctrl_write,
1582 static const char readme_msg[] =
1583 "tracing mini-HOWTO:\n\n"
1585 "# mount -t debugfs nodev /debug\n\n"
1586 "# cat /debug/tracing/available_tracers\n"
1587 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1588 "# cat /debug/tracing/current_tracer\n"
1590 "# echo sched_switch > /debug/tracing/current_tracer\n"
1591 "# cat /debug/tracing/current_tracer\n"
1593 "# cat /debug/tracing/iter_ctrl\n"
1594 "noprint-parent nosym-offset nosym-addr noverbose\n"
1595 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1596 "# echo 1 > /debug/tracing/tracing_enabled\n"
1597 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1598 "echo 0 > /debug/tracing/tracing_enabled\n"
1602 tracing_readme_read(struct file *filp, char __user *ubuf,
1603 size_t cnt, loff_t *ppos)
1605 return simple_read_from_buffer(ubuf, cnt, ppos,
1606 readme_msg, strlen(readme_msg));
1609 static struct file_operations tracing_readme_fops = {
1610 .open = tracing_open_generic,
1611 .read = tracing_readme_read,
1616 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1617 size_t cnt, loff_t *ppos)
1619 struct trace_array *tr = filp->private_data;
1623 r = sprintf(buf, "%ld\n", tr->ctrl);
1624 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1628 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1629 size_t cnt, loff_t *ppos)
1631 struct trace_array *tr = filp->private_data;
1638 if (copy_from_user(&buf, ubuf, cnt))
1643 val = simple_strtoul(buf, NULL, 10);
1647 mutex_lock(&trace_types_lock);
1648 if (tr->ctrl ^ val) {
1656 if (current_trace && current_trace->ctrl_update)
1657 current_trace->ctrl_update(tr);
1659 mutex_unlock(&trace_types_lock);
1667 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1668 size_t cnt, loff_t *ppos)
1670 char buf[max_tracer_type_len+2];
1673 mutex_lock(&trace_types_lock);
1675 r = sprintf(buf, "%s\n", current_trace->name);
1677 r = sprintf(buf, "\n");
1678 mutex_unlock(&trace_types_lock);
1680 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1684 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1685 size_t cnt, loff_t *ppos)
1687 struct trace_array *tr = &global_trace;
1689 char buf[max_tracer_type_len+1];
1692 if (cnt > max_tracer_type_len)
1693 cnt = max_tracer_type_len;
1695 if (copy_from_user(&buf, ubuf, cnt))
1700 /* strip ending whitespace. */
1701 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1704 mutex_lock(&trace_types_lock);
1705 for (t = trace_types; t; t = t->next) {
1706 if (strcmp(t->name, buf) == 0)
1709 if (!t || t == current_trace)
1712 if (current_trace && current_trace->reset)
1713 current_trace->reset(tr);
1720 mutex_unlock(&trace_types_lock);
1728 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1729 size_t cnt, loff_t *ppos)
1731 unsigned long *ptr = filp->private_data;
1735 r = snprintf(buf, 64, "%ld\n",
1736 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1739 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1743 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1744 size_t cnt, loff_t *ppos)
1746 long *ptr = filp->private_data;
1753 if (copy_from_user(&buf, ubuf, cnt))
1758 val = simple_strtoul(buf, NULL, 10);
1765 static atomic_t tracing_reader;
1767 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1769 struct trace_iterator *iter;
1771 if (tracing_disabled)
1774 /* We only allow for reader of the pipe */
1775 if (atomic_inc_return(&tracing_reader) != 1) {
1776 atomic_dec(&tracing_reader);
1780 /* create a buffer to store the information to pass to userspace */
1781 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1785 iter->tr = &global_trace;
1787 filp->private_data = iter;
1792 static int tracing_release_pipe(struct inode *inode, struct file *file)
1794 struct trace_iterator *iter = file->private_data;
1797 atomic_dec(&tracing_reader);
1806 tracing_read_pipe(struct file *filp, char __user *ubuf,
1807 size_t cnt, loff_t *ppos)
1809 struct trace_iterator *iter = filp->private_data;
1810 struct trace_array_cpu *data;
1811 static cpumask_t mask;
1812 struct trace_entry *entry;
1814 unsigned long flags;
1820 /* return any leftover data */
1821 if (iter->seq.len > start) {
1822 len = iter->seq.len - start;
1825 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1834 trace_seq_reset(&iter->seq);
1837 while (trace_empty(iter)) {
1839 * This is a make-shift waitqueue. The reason we don't use
1840 * an actual wait queue is because:
1841 * 1) we only ever have one waiter
1842 * 2) the tracing, traces all functions, we don't want
1843 * the overhead of calling wake_up and friends
1844 * (and tracing them too)
1845 * Anyway, this is really very primitive wakeup.
1847 set_current_state(TASK_INTERRUPTIBLE);
1848 iter->tr->waiter = current;
1850 /* sleep for one second, and try again. */
1851 schedule_timeout(HZ);
1853 iter->tr->waiter = NULL;
1855 if (signal_pending(current))
1859 * We block until we read something and tracing is disabled.
1860 * We still block if tracing is disabled, but we have never
1861 * read anything. This allows a user to cat this file, and
1862 * then enable tracing. But after we have read something,
1863 * we give an EOF when tracing is again disabled.
1865 * iter->pos will be 0 if we haven't read anything.
1867 if (!tracer_enabled && iter->pos)
1873 /* stop when tracing is finished */
1874 if (trace_empty(iter))
1877 if (cnt >= PAGE_SIZE)
1878 cnt = PAGE_SIZE - 1;
1880 memset(iter, 0, sizeof(*iter));
1881 iter->tr = &global_trace;
1885 * We need to stop all tracing on all CPUS to read the
1886 * the next buffer. This is a bit expensive, but is
1887 * not done often. We fill all what we can read,
1888 * and then release the locks again.
1892 local_irq_save(flags);
1893 for_each_possible_cpu(cpu) {
1894 data = iter->tr->data[cpu];
1896 if (!head_page(data) || !data->trace_idx)
1899 atomic_inc(&data->disabled);
1900 spin_lock(&data->lock);
1904 while ((entry = find_next_entry(iter, &cpu))) {
1912 ret = print_trace_fmt(iter);
1916 trace_consume(iter);
1918 if (iter->seq.len >= cnt)
1923 for_each_cpu_mask(cpu, mask) {
1924 data = iter->tr->data[cpu];
1925 spin_unlock(&data->lock);
1926 atomic_dec(&data->disabled);
1928 local_irq_restore(flags);
1930 /* Now copy what we have to the user */
1931 read = iter->seq.len;
1935 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1937 if (read < iter->seq.len)
1940 trace_seq_reset(&iter->seq);
1948 static struct file_operations tracing_max_lat_fops = {
1949 .open = tracing_open_generic,
1950 .read = tracing_max_lat_read,
1951 .write = tracing_max_lat_write,
1954 static struct file_operations tracing_ctrl_fops = {
1955 .open = tracing_open_generic,
1956 .read = tracing_ctrl_read,
1957 .write = tracing_ctrl_write,
1960 static struct file_operations set_tracer_fops = {
1961 .open = tracing_open_generic,
1962 .read = tracing_set_trace_read,
1963 .write = tracing_set_trace_write,
1966 static struct file_operations tracing_pipe_fops = {
1967 .open = tracing_open_pipe,
1968 .read = tracing_read_pipe,
1969 .release = tracing_release_pipe,
1972 #ifdef CONFIG_DYNAMIC_FTRACE
1975 tracing_read_long(struct file *filp, char __user *ubuf,
1976 size_t cnt, loff_t *ppos)
1978 unsigned long *p = filp->private_data;
1982 r = sprintf(buf, "%ld\n", *p);
1984 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1987 static struct file_operations tracing_read_long_fops = {
1988 .open = tracing_open_generic,
1989 .read = tracing_read_long,
1993 static struct dentry *d_tracer;
1995 struct dentry *tracing_init_dentry(void)
2002 d_tracer = debugfs_create_dir("tracing", NULL);
2004 if (!d_tracer && !once) {
2006 pr_warning("Could not create debugfs directory 'tracing'\n");
2013 #ifdef CONFIG_FTRACE_SELFTEST
2014 /* Let selftest have access to static functions in this file */
2015 #include "trace_selftest.c"
2018 static __init void tracer_init_debugfs(void)
2020 struct dentry *d_tracer;
2021 struct dentry *entry;
2023 d_tracer = tracing_init_dentry();
2025 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2026 &global_trace, &tracing_ctrl_fops);
2028 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2030 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2031 NULL, &tracing_iter_fops);
2033 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2035 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2036 &global_trace, &tracing_lt_fops);
2038 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2040 entry = debugfs_create_file("trace", 0444, d_tracer,
2041 &global_trace, &tracing_fops);
2043 pr_warning("Could not create debugfs 'trace' entry\n");
2045 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2046 &global_trace, &show_traces_fops);
2048 pr_warning("Could not create debugfs 'trace' entry\n");
2050 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2051 &global_trace, &set_tracer_fops);
2053 pr_warning("Could not create debugfs 'trace' entry\n");
2055 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2056 &tracing_max_latency,
2057 &tracing_max_lat_fops);
2059 pr_warning("Could not create debugfs "
2060 "'tracing_max_latency' entry\n");
2062 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2063 &tracing_thresh, &tracing_max_lat_fops);
2065 pr_warning("Could not create debugfs "
2066 "'tracing_threash' entry\n");
2067 entry = debugfs_create_file("README", 0644, d_tracer,
2068 NULL, &tracing_readme_fops);
2070 pr_warning("Could not create debugfs 'README' entry\n");
2072 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2073 NULL, &tracing_pipe_fops);
2075 pr_warning("Could not create debugfs "
2076 "'tracing_threash' entry\n");
2078 #ifdef CONFIG_DYNAMIC_FTRACE
2079 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2080 &ftrace_update_tot_cnt,
2081 &tracing_read_long_fops);
2083 pr_warning("Could not create debugfs "
2084 "'dyn_ftrace_total_info' entry\n");
2088 /* dummy trace to disable tracing */
2089 static struct tracer no_tracer __read_mostly =
2094 static int trace_alloc_page(void)
2096 struct trace_array_cpu *data;
2097 struct page *page, *tmp;
2102 /* first allocate a page for each CPU */
2103 for_each_possible_cpu(i) {
2104 array = (void *)__get_free_page(GFP_KERNEL);
2105 if (array == NULL) {
2106 printk(KERN_ERR "tracer: failed to allocate page"
2107 "for trace buffer!\n");
2111 page = virt_to_page(array);
2112 list_add(&page->lru, &pages);
2114 /* Only allocate if we are actually using the max trace */
2115 #ifdef CONFIG_TRACER_MAX_TRACE
2116 array = (void *)__get_free_page(GFP_KERNEL);
2117 if (array == NULL) {
2118 printk(KERN_ERR "tracer: failed to allocate page"
2119 "for trace buffer!\n");
2122 page = virt_to_page(array);
2123 list_add(&page->lru, &pages);
2127 /* Now that we successfully allocate a page per CPU, add them */
2128 for_each_possible_cpu(i) {
2129 data = global_trace.data[i];
2130 spin_lock_init(&data->lock);
2131 lockdep_set_class(&data->lock, &data->lock_key);
2132 page = list_entry(pages.next, struct page, lru);
2133 list_del_init(&page->lru);
2134 list_add_tail(&page->lru, &data->trace_pages);
2137 #ifdef CONFIG_TRACER_MAX_TRACE
2138 data = max_tr.data[i];
2139 spin_lock_init(&data->lock);
2140 lockdep_set_class(&data->lock, &data->lock_key);
2141 page = list_entry(pages.next, struct page, lru);
2142 list_del_init(&page->lru);
2143 list_add_tail(&page->lru, &data->trace_pages);
2147 global_trace.entries += ENTRIES_PER_PAGE;
2152 list_for_each_entry_safe(page, tmp, &pages, lru) {
2153 list_del_init(&page->lru);
2159 __init static int tracer_alloc_buffers(void)
2161 struct trace_array_cpu *data;
2168 /* Allocate the first page for all buffers */
2169 for_each_possible_cpu(i) {
2170 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2171 max_tr.data[i] = &per_cpu(max_data, i);
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");
2180 /* set the array to the list */
2181 INIT_LIST_HEAD(&data->trace_pages);
2182 page = virt_to_page(array);
2183 list_add(&page->lru, &data->trace_pages);
2184 /* use the LRU flag to differentiate the two buffers */
2187 /* Only allocate if we are actually using the max trace */
2188 #ifdef CONFIG_TRACER_MAX_TRACE
2189 array = (void *)__get_free_page(GFP_KERNEL);
2190 if (array == NULL) {
2191 printk(KERN_ERR "tracer: failed to allocate page"
2192 "for trace buffer!\n");
2196 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2197 page = virt_to_page(array);
2198 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2204 * Since we allocate by orders of pages, we may be able to
2207 global_trace.entries = ENTRIES_PER_PAGE;
2210 while (global_trace.entries < trace_nr_entries) {
2211 if (trace_alloc_page())
2215 max_tr.entries = global_trace.entries;
2217 pr_info("tracer: %d pages allocated for %ld",
2218 pages, trace_nr_entries);
2219 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2220 pr_info(" actual entries %ld\n", global_trace.entries);
2222 tracer_init_debugfs();
2224 trace_init_cmdlines();
2226 register_tracer(&no_tracer);
2227 current_trace = &no_tracer;
2229 /* All seems OK, enable tracing */
2230 tracing_disabled = 0;
2235 for (i-- ; i >= 0; i--) {
2236 struct page *page, *tmp;
2237 struct trace_array_cpu *data = global_trace.data[i];
2240 list_for_each_entry_safe(page, tmp,
2241 &data->trace_pages, lru) {
2242 list_del_init(&page->lru);
2247 #ifdef CONFIG_TRACER_MAX_TRACE
2248 data = max_tr.data[i];
2250 list_for_each_entry_safe(page, tmp,
2251 &data->trace_pages, lru) {
2252 list_del_init(&page->lru);
2260 fs_initcall(tracer_alloc_buffers);