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,
158 #define TRACE_ITER_SYM_MASK \
159 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
161 /* These must match the bit postions above */
162 static const char *trace_options[] = {
170 static unsigned trace_flags;
172 static DEFINE_SPINLOCK(ftrace_max_lock);
175 * Copy the new maximum trace into the separate maximum-trace
176 * structure. (this way the maximum trace is permanently saved,
177 * for later retrieval via /debugfs/tracing/latency_trace)
180 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
182 struct trace_array_cpu *data = tr->data[cpu];
185 max_tr.time_start = data->preempt_timestamp;
187 data = max_tr.data[cpu];
188 data->saved_latency = tracing_max_latency;
190 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
191 data->pid = tsk->pid;
192 data->uid = tsk->uid;
193 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
194 data->policy = tsk->policy;
195 data->rt_priority = tsk->rt_priority;
197 /* record this tasks comm */
198 tracing_record_cmdline(current);
201 void check_pages(struct trace_array_cpu *data)
203 struct page *page, *tmp;
205 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
206 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
208 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
209 BUG_ON(page->lru.next->prev != &page->lru);
210 BUG_ON(page->lru.prev->next != &page->lru);
214 void *head_page(struct trace_array_cpu *data)
219 if (list_empty(&data->trace_pages))
222 page = list_entry(data->trace_pages.next, struct page, lru);
223 BUG_ON(&page->lru == &data->trace_pages);
225 return page_address(page);
229 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
231 int len = (PAGE_SIZE - 1) - s->len;
239 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
242 /* If we can't write it all, don't bother writing anything */
252 trace_seq_puts(struct trace_seq *s, const char *str)
254 int len = strlen(str);
256 if (len > ((PAGE_SIZE - 1) - s->len))
259 memcpy(s->buffer + s->len, str, len);
266 trace_seq_putc(struct trace_seq *s, unsigned char c)
268 if (s->len >= (PAGE_SIZE - 1))
271 s->buffer[s->len++] = c;
277 trace_seq_reset(struct trace_seq *s)
283 trace_print_seq(struct seq_file *m, struct trace_seq *s)
285 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
288 seq_puts(m, s->buffer);
294 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
296 struct list_head flip_pages;
298 INIT_LIST_HEAD(&flip_pages);
300 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
301 sizeof(struct trace_array_cpu) -
302 offsetof(struct trace_array_cpu, trace_head_idx));
306 list_splice_init(&tr1->trace_pages, &flip_pages);
307 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
308 list_splice_init(&flip_pages, &tr2->trace_pages);
309 BUG_ON(!list_empty(&flip_pages));
315 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
317 struct trace_array_cpu *data;
320 WARN_ON_ONCE(!irqs_disabled());
321 spin_lock(&ftrace_max_lock);
322 /* clear out all the previous traces */
323 for_each_possible_cpu(i) {
325 flip_trace(max_tr.data[i], data);
329 __update_max_tr(tr, tsk, cpu);
330 spin_unlock(&ftrace_max_lock);
334 * update_max_tr_single - only copy one trace over, and reset the rest
336 * @tsk - task with the latency
337 * @cpu - the cpu of the buffer to copy.
340 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
342 struct trace_array_cpu *data = tr->data[cpu];
345 WARN_ON_ONCE(!irqs_disabled());
346 spin_lock(&ftrace_max_lock);
347 for_each_possible_cpu(i)
348 tracing_reset(max_tr.data[i]);
350 flip_trace(max_tr.data[cpu], data);
353 __update_max_tr(tr, tsk, cpu);
354 spin_unlock(&ftrace_max_lock);
357 int register_tracer(struct tracer *type)
364 pr_info("Tracer must have a name\n");
368 mutex_lock(&trace_types_lock);
369 for (t = trace_types; t; t = t->next) {
370 if (strcmp(type->name, t->name) == 0) {
372 pr_info("Trace %s already registered\n",
379 #ifdef CONFIG_FTRACE_STARTUP_TEST
380 if (type->selftest) {
381 struct tracer *saved_tracer = current_trace;
382 struct trace_array_cpu *data;
383 struct trace_array *tr = &global_trace;
384 int saved_ctrl = tr->ctrl;
387 * Run a selftest on this tracer.
388 * Here we reset the trace buffer, and set the current
389 * tracer to be this tracer. The tracer can then run some
390 * internal tracing to verify that everything is in order.
391 * If we fail, we do not register this tracer.
393 for_each_possible_cpu(i) {
395 if (!head_page(data))
399 current_trace = type;
401 /* the test is responsible for initializing and enabling */
402 pr_info("Testing tracer %s: ", type->name);
403 ret = type->selftest(type, tr);
404 /* the test is responsible for resetting too */
405 current_trace = saved_tracer;
406 tr->ctrl = saved_ctrl;
408 printk(KERN_CONT "FAILED!\n");
411 /* Only reset on passing, to avoid touching corrupted buffers */
412 for_each_possible_cpu(i) {
414 if (!head_page(data))
418 printk(KERN_CONT "PASSED\n");
422 type->next = trace_types;
424 len = strlen(type->name);
425 if (len > max_tracer_type_len)
426 max_tracer_type_len = len;
429 mutex_unlock(&trace_types_lock);
434 void unregister_tracer(struct tracer *type)
439 mutex_lock(&trace_types_lock);
440 for (t = &trace_types; *t; t = &(*t)->next) {
444 pr_info("Trace %s not registered\n", type->name);
449 if (strlen(type->name) != max_tracer_type_len)
452 max_tracer_type_len = 0;
453 for (t = &trace_types; *t; t = &(*t)->next) {
454 len = strlen((*t)->name);
455 if (len > max_tracer_type_len)
456 max_tracer_type_len = len;
459 mutex_unlock(&trace_types_lock);
462 notrace void tracing_reset(struct trace_array_cpu *data)
465 data->trace_head = data->trace_tail = head_page(data);
466 data->trace_head_idx = 0;
467 data->trace_tail_idx = 0;
472 function_trace_call(unsigned long ip, unsigned long parent_ip)
474 struct trace_array *tr = &global_trace;
475 struct trace_array_cpu *data;
480 if (unlikely(!tracer_enabled))
483 local_irq_save(flags);
484 cpu = raw_smp_processor_id();
485 data = tr->data[cpu];
486 disabled = atomic_inc_return(&data->disabled);
488 if (likely(disabled == 1))
489 ftrace(tr, data, ip, parent_ip, flags);
491 atomic_dec(&data->disabled);
492 local_irq_restore(flags);
495 static struct ftrace_ops trace_ops __read_mostly =
497 .func = function_trace_call,
501 notrace void tracing_start_function_trace(void)
503 register_ftrace_function(&trace_ops);
506 notrace void tracing_stop_function_trace(void)
508 unregister_ftrace_function(&trace_ops);
511 #define SAVED_CMDLINES 128
512 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
513 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
514 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
515 static int cmdline_idx;
516 static DEFINE_SPINLOCK(trace_cmdline_lock);
517 atomic_t trace_record_cmdline_disabled;
519 static void trace_init_cmdlines(void)
521 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
522 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
526 notrace void trace_stop_cmdline_recording(void);
528 static notrace void trace_save_cmdline(struct task_struct *tsk)
533 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
537 * It's not the end of the world if we don't get
538 * the lock, but we also don't want to spin
539 * nor do we want to disable interrupts,
540 * so if we miss here, then better luck next time.
542 if (!spin_trylock(&trace_cmdline_lock))
545 idx = map_pid_to_cmdline[tsk->pid];
546 if (idx >= SAVED_CMDLINES) {
547 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
549 map = map_cmdline_to_pid[idx];
550 if (map <= PID_MAX_DEFAULT)
551 map_pid_to_cmdline[map] = (unsigned)-1;
553 map_pid_to_cmdline[tsk->pid] = idx;
558 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
560 spin_unlock(&trace_cmdline_lock);
563 static notrace char *trace_find_cmdline(int pid)
565 char *cmdline = "<...>";
571 if (pid > PID_MAX_DEFAULT)
574 map = map_pid_to_cmdline[pid];
575 if (map >= SAVED_CMDLINES)
578 cmdline = saved_cmdlines[map];
584 notrace void tracing_record_cmdline(struct task_struct *tsk)
586 if (atomic_read(&trace_record_cmdline_disabled))
589 trace_save_cmdline(tsk);
592 static inline notrace struct list_head *
593 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
596 * Roundrobin - but skip the head (which is not a real page):
599 if (unlikely(next == &data->trace_pages))
601 BUG_ON(next == &data->trace_pages);
606 static inline notrace void *
607 trace_next_page(struct trace_array_cpu *data, void *addr)
609 struct list_head *next;
612 page = virt_to_page(addr);
614 next = trace_next_list(data, &page->lru);
615 page = list_entry(next, struct page, lru);
617 return page_address(page);
620 static inline notrace struct trace_entry *
621 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
623 unsigned long idx, idx_next;
624 struct trace_entry *entry;
627 idx = data->trace_head_idx;
630 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
632 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
634 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
635 data->trace_head = trace_next_page(data, data->trace_head);
639 if (data->trace_head == data->trace_tail &&
640 idx_next == data->trace_tail_idx) {
642 data->trace_tail_idx++;
643 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
645 trace_next_page(data, data->trace_tail);
646 data->trace_tail_idx = 0;
650 data->trace_head_idx = idx_next;
655 static inline notrace void
656 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
658 struct task_struct *tsk = current;
661 pc = preempt_count();
663 entry->preempt_count = pc & 0xff;
664 entry->pid = tsk->pid;
665 entry->t = ftrace_now(raw_smp_processor_id());
666 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
667 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
668 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
669 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
673 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
674 unsigned long ip, unsigned long parent_ip, unsigned long flags)
676 struct trace_entry *entry;
678 spin_lock(&data->lock);
679 entry = tracing_get_trace_entry(tr, data);
680 tracing_generic_entry_update(entry, flags);
681 entry->type = TRACE_FN;
683 entry->fn.parent_ip = parent_ip;
684 spin_unlock(&data->lock);
688 tracing_sched_switch_trace(struct trace_array *tr,
689 struct trace_array_cpu *data,
690 struct task_struct *prev, struct task_struct *next,
693 struct trace_entry *entry;
695 spin_lock(&data->lock);
696 entry = tracing_get_trace_entry(tr, data);
697 tracing_generic_entry_update(entry, flags);
698 entry->type = TRACE_CTX;
699 entry->ctx.prev_pid = prev->pid;
700 entry->ctx.prev_prio = prev->prio;
701 entry->ctx.prev_state = prev->state;
702 entry->ctx.next_pid = next->pid;
703 entry->ctx.next_prio = next->prio;
704 spin_unlock(&data->lock);
707 enum trace_file_type {
708 TRACE_FILE_LAT_FMT = 1,
711 static struct trace_entry *
712 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
713 struct trace_iterator *iter, int cpu)
716 struct trace_entry *array;
718 if (iter->next_idx[cpu] >= tr->entries ||
719 iter->next_idx[cpu] >= data->trace_idx ||
720 (data->trace_head == data->trace_tail &&
721 data->trace_head_idx == data->trace_tail_idx))
724 if (!iter->next_page[cpu]) {
725 /* Initialize the iterator for this cpu trace buffer */
726 WARN_ON(!data->trace_tail);
727 page = virt_to_page(data->trace_tail);
728 iter->next_page[cpu] = &page->lru;
729 iter->next_page_idx[cpu] = data->trace_tail_idx;
732 page = list_entry(iter->next_page[cpu], struct page, lru);
733 BUG_ON(&data->trace_pages == &page->lru);
735 array = page_address(page);
737 /* Still possible to catch up to the tail */
738 if (iter->next_idx[cpu] && array == data->trace_tail &&
739 iter->next_page_idx[cpu] == data->trace_tail_idx)
742 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
743 return &array[iter->next_page_idx[cpu]];
746 static struct notrace trace_entry *
747 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
749 struct trace_array *tr = iter->tr;
750 struct trace_entry *ent, *next = NULL;
754 for_each_possible_cpu(cpu) {
755 if (!head_page(tr->data[cpu]))
757 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
759 * Pick the entry with the smallest timestamp:
761 if (ent && (!next || ent->t < next->t)) {
773 static notrace void trace_iterator_increment(struct trace_iterator *iter)
776 iter->next_idx[iter->cpu]++;
777 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]);
788 static notrace void trace_consume(struct trace_iterator *iter)
790 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
792 data->trace_tail_idx++;
793 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
794 data->trace_tail = trace_next_page(data, data->trace_tail);
795 data->trace_tail_idx = 0;
798 /* Check if we empty it, then reset the index */
799 if (data->trace_head == data->trace_tail &&
800 data->trace_head_idx == data->trace_tail_idx)
804 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
806 struct trace_entry *next;
809 next = find_next_entry(iter, &next_cpu);
811 iter->prev_ent = iter->ent;
812 iter->prev_cpu = iter->cpu;
815 iter->cpu = next_cpu;
818 trace_iterator_increment(iter);
820 return next ? iter : NULL;
823 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
825 struct trace_iterator *iter = m->private;
826 void *last_ent = iter->ent;
832 /* can't go backwards */
837 ent = find_next_entry_inc(iter);
841 while (ent && iter->idx < i)
842 ent = find_next_entry_inc(iter);
846 if (last_ent && !ent)
847 seq_puts(m, "\n\nvim:ft=help\n");
852 static void *s_start(struct seq_file *m, loff_t *pos)
854 struct trace_iterator *iter = m->private;
859 mutex_lock(&trace_types_lock);
861 if (!current_trace || current_trace != iter->trace)
864 atomic_inc(&trace_record_cmdline_disabled);
866 /* let the tracer grab locks here if needed */
867 if (current_trace->start)
868 current_trace->start(iter);
870 if (*pos != iter->pos) {
874 iter->prev_ent = NULL;
877 for_each_possible_cpu(i) {
878 iter->next_idx[i] = 0;
879 iter->next_page[i] = NULL;
882 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
887 p = s_next(m, p, &l);
893 static void s_stop(struct seq_file *m, void *p)
895 struct trace_iterator *iter = m->private;
897 atomic_dec(&trace_record_cmdline_disabled);
899 /* let the tracer release locks here if needed */
900 if (current_trace && current_trace == iter->trace && iter->trace->stop)
901 iter->trace->stop(iter);
903 mutex_unlock(&trace_types_lock);
907 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
909 #ifdef CONFIG_KALLSYMS
910 char str[KSYM_SYMBOL_LEN];
912 kallsyms_lookup(address, NULL, NULL, NULL, str);
914 return trace_seq_printf(s, fmt, str);
920 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
921 unsigned long address)
923 #ifdef CONFIG_KALLSYMS
924 char str[KSYM_SYMBOL_LEN];
926 sprint_symbol(str, address);
927 return trace_seq_printf(s, fmt, str);
933 # define IP_FMT "%08lx"
935 # define IP_FMT "%016lx"
939 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
944 return trace_seq_printf(s, "0");
946 if (sym_flags & TRACE_ITER_SYM_OFFSET)
947 ret = seq_print_sym_offset(s, "%s", ip);
949 ret = seq_print_sym_short(s, "%s", ip);
954 if (sym_flags & TRACE_ITER_SYM_ADDR)
955 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
959 static notrace void print_lat_help_header(struct seq_file *m)
961 seq_puts(m, "# _------=> CPU# \n");
962 seq_puts(m, "# / _-----=> irqs-off \n");
963 seq_puts(m, "# | / _----=> need-resched \n");
964 seq_puts(m, "# || / _---=> hardirq/softirq \n");
965 seq_puts(m, "# ||| / _--=> preempt-depth \n");
966 seq_puts(m, "# |||| / \n");
967 seq_puts(m, "# ||||| delay \n");
968 seq_puts(m, "# cmd pid ||||| time | caller \n");
969 seq_puts(m, "# \\ / ||||| \\ | / \n");
972 static notrace void print_func_help_header(struct seq_file *m)
974 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
975 seq_puts(m, "# | | | | |\n");
980 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
982 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
983 struct trace_array *tr = iter->tr;
984 struct trace_array_cpu *data = tr->data[tr->cpu];
985 struct tracer *type = current_trace;
986 unsigned long total = 0;
987 unsigned long entries = 0;
989 const char *name = "preemption";
994 for_each_possible_cpu(cpu) {
995 if (head_page(tr->data[cpu])) {
996 total += tr->data[cpu]->trace_idx;
997 if (tr->data[cpu]->trace_idx > tr->entries)
998 entries += tr->entries;
1000 entries += tr->data[cpu]->trace_idx;
1004 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1006 seq_puts(m, "-----------------------------------"
1007 "---------------------------------\n");
1008 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1009 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1010 nsecs_to_usecs(data->saved_latency),
1014 #if defined(CONFIG_PREEMPT_NONE)
1016 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1018 #elif defined(CONFIG_PREEMPT_DESKTOP)
1023 /* These are reserved for later use */
1026 seq_printf(m, " #P:%d)\n", num_online_cpus());
1030 seq_puts(m, " -----------------\n");
1031 seq_printf(m, " | task: %.16s-%d "
1032 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1033 data->comm, data->pid, data->uid, data->nice,
1034 data->policy, data->rt_priority);
1035 seq_puts(m, " -----------------\n");
1037 if (data->critical_start) {
1038 seq_puts(m, " => started at: ");
1039 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1040 trace_print_seq(m, &iter->seq);
1041 seq_puts(m, "\n => ended at: ");
1042 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1043 trace_print_seq(m, &iter->seq);
1051 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1053 int hardirq, softirq;
1056 comm = trace_find_cmdline(entry->pid);
1058 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1059 trace_seq_printf(s, "%d", cpu);
1060 trace_seq_printf(s, "%c%c",
1061 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1062 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1064 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1065 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1066 if (hardirq && softirq)
1067 trace_seq_putc(s, 'H');
1070 trace_seq_putc(s, 'h');
1073 trace_seq_putc(s, 's');
1075 trace_seq_putc(s, '.');
1079 if (entry->preempt_count)
1080 trace_seq_printf(s, "%x", entry->preempt_count);
1082 trace_seq_puts(s, ".");
1085 unsigned long preempt_mark_thresh = 100;
1088 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1089 unsigned long rel_usecs)
1091 trace_seq_printf(s, " %4lldus", abs_usecs);
1092 if (rel_usecs > preempt_mark_thresh)
1093 trace_seq_puts(s, "!: ");
1094 else if (rel_usecs > 1)
1095 trace_seq_puts(s, "+: ");
1097 trace_seq_puts(s, " : ");
1100 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1103 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1105 struct trace_seq *s = &iter->seq;
1106 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1107 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1108 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1109 struct trace_entry *entry = iter->ent;
1110 unsigned long abs_usecs;
1111 unsigned long rel_usecs;
1117 rel_usecs = ns2usecs(next_entry->t - entry->t);
1118 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1121 comm = trace_find_cmdline(entry->pid);
1122 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1123 " %ld.%03ldms (+%ld.%03ldms): ",
1125 entry->pid, cpu, entry->flags,
1126 entry->preempt_count, trace_idx,
1129 abs_usecs % 1000, rel_usecs/1000,
1132 lat_print_generic(s, entry, cpu);
1133 lat_print_timestamp(s, abs_usecs, rel_usecs);
1135 switch (entry->type) {
1137 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1138 trace_seq_puts(s, " (");
1139 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1140 trace_seq_puts(s, ")\n");
1143 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1144 state_to_char[entry->ctx.prev_state] : 'X';
1145 comm = trace_find_cmdline(entry->ctx.next_pid);
1146 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1147 entry->ctx.prev_pid,
1148 entry->ctx.prev_prio,
1150 entry->ctx.next_pid,
1151 entry->ctx.next_prio,
1155 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1160 print_trace_fmt(struct trace_iterator *iter)
1162 struct trace_seq *s = &iter->seq;
1163 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1164 struct trace_entry *entry;
1165 unsigned long usec_rem;
1166 unsigned long long t;
1174 comm = trace_find_cmdline(iter->ent->pid);
1176 t = ns2usecs(entry->t);
1177 usec_rem = do_div(t, 1000000ULL);
1178 secs = (unsigned long)t;
1180 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1183 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1186 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1190 switch (entry->type) {
1192 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1195 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1196 entry->fn.parent_ip) {
1197 ret = trace_seq_printf(s, " <-");
1200 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1205 ret = trace_seq_printf(s, "\n");
1210 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1211 state_to_char[entry->ctx.prev_state] : 'X';
1212 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1213 entry->ctx.prev_pid,
1214 entry->ctx.prev_prio,
1216 entry->ctx.next_pid,
1217 entry->ctx.next_prio);
1225 static int trace_empty(struct trace_iterator *iter)
1227 struct trace_array_cpu *data;
1230 for_each_possible_cpu(cpu) {
1231 data = iter->tr->data[cpu];
1233 if (head_page(data) && data->trace_idx &&
1234 (data->trace_tail != data->trace_head ||
1235 data->trace_tail_idx != data->trace_head_idx))
1241 static int s_show(struct seq_file *m, void *v)
1243 struct trace_iterator *iter = v;
1245 if (iter->ent == NULL) {
1247 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1250 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1251 /* print nothing if the buffers are empty */
1252 if (trace_empty(iter))
1254 print_trace_header(m, iter);
1255 if (!(trace_flags & TRACE_ITER_VERBOSE))
1256 print_lat_help_header(m);
1258 if (!(trace_flags & TRACE_ITER_VERBOSE))
1259 print_func_help_header(m);
1262 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1263 print_lat_fmt(iter, iter->idx, iter->cpu);
1265 print_trace_fmt(iter);
1266 trace_print_seq(m, &iter->seq);
1272 static struct seq_operations tracer_seq_ops = {
1279 static struct trace_iterator notrace *
1280 __tracing_open(struct inode *inode, struct file *file, int *ret)
1282 struct trace_iterator *iter;
1284 if (tracing_disabled) {
1289 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1295 mutex_lock(&trace_types_lock);
1296 if (current_trace && current_trace->print_max)
1299 iter->tr = inode->i_private;
1300 iter->trace = current_trace;
1303 /* TODO stop tracer */
1304 *ret = seq_open(file, &tracer_seq_ops);
1306 struct seq_file *m = file->private_data;
1309 /* stop the trace while dumping */
1313 if (iter->trace && iter->trace->open)
1314 iter->trace->open(iter);
1319 mutex_unlock(&trace_types_lock);
1325 int tracing_open_generic(struct inode *inode, struct file *filp)
1327 if (tracing_disabled)
1330 filp->private_data = inode->i_private;
1334 int tracing_release(struct inode *inode, struct file *file)
1336 struct seq_file *m = (struct seq_file *)file->private_data;
1337 struct trace_iterator *iter = m->private;
1339 mutex_lock(&trace_types_lock);
1340 if (iter->trace && iter->trace->close)
1341 iter->trace->close(iter);
1343 /* reenable tracing if it was previously enabled */
1346 mutex_unlock(&trace_types_lock);
1348 seq_release(inode, file);
1353 static int tracing_open(struct inode *inode, struct file *file)
1357 __tracing_open(inode, file, &ret);
1362 static int tracing_lt_open(struct inode *inode, struct file *file)
1364 struct trace_iterator *iter;
1367 iter = __tracing_open(inode, file, &ret);
1370 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1376 static notrace void *
1377 t_next(struct seq_file *m, void *v, loff_t *pos)
1379 struct tracer *t = m->private;
1391 static void *t_start(struct seq_file *m, loff_t *pos)
1393 struct tracer *t = m->private;
1396 mutex_lock(&trace_types_lock);
1397 for (; t && l < *pos; t = t_next(m, t, &l))
1403 static void t_stop(struct seq_file *m, void *p)
1405 mutex_unlock(&trace_types_lock);
1408 static int t_show(struct seq_file *m, void *v)
1410 struct tracer *t = v;
1415 seq_printf(m, "%s", t->name);
1424 static struct seq_operations show_traces_seq_ops = {
1431 static int show_traces_open(struct inode *inode, struct file *file)
1435 if (tracing_disabled)
1438 ret = seq_open(file, &show_traces_seq_ops);
1440 struct seq_file *m = file->private_data;
1441 m->private = trace_types;
1447 static struct file_operations tracing_fops = {
1448 .open = tracing_open,
1450 .llseek = seq_lseek,
1451 .release = tracing_release,
1454 static struct file_operations tracing_lt_fops = {
1455 .open = tracing_lt_open,
1457 .llseek = seq_lseek,
1458 .release = tracing_release,
1461 static struct file_operations show_traces_fops = {
1462 .open = show_traces_open,
1464 .release = seq_release,
1468 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1469 size_t cnt, loff_t *ppos)
1476 /* calulate max size */
1477 for (i = 0; trace_options[i]; i++) {
1478 len += strlen(trace_options[i]);
1479 len += 3; /* "no" and space */
1482 /* +2 for \n and \0 */
1483 buf = kmalloc(len + 2, GFP_KERNEL);
1487 for (i = 0; trace_options[i]; i++) {
1488 if (trace_flags & (1 << i))
1489 r += sprintf(buf + r, "%s ", trace_options[i]);
1491 r += sprintf(buf + r, "no%s ", trace_options[i]);
1494 r += sprintf(buf + r, "\n");
1495 WARN_ON(r >= len + 2);
1497 r = simple_read_from_buffer(ubuf, cnt, ppos,
1506 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1507 size_t cnt, loff_t *ppos)
1517 if (copy_from_user(&buf, ubuf, cnt))
1522 if (strncmp(buf, "no", 2) == 0) {
1527 for (i = 0; trace_options[i]; i++) {
1528 int len = strlen(trace_options[i]);
1530 if (strncmp(cmp, trace_options[i], len) == 0) {
1532 trace_flags &= ~(1 << i);
1534 trace_flags |= (1 << i);
1544 static struct file_operations tracing_iter_fops = {
1545 .open = tracing_open_generic,
1546 .read = tracing_iter_ctrl_read,
1547 .write = tracing_iter_ctrl_write,
1550 static const char readme_msg[] =
1551 "tracing mini-HOWTO:\n\n"
1553 "# mount -t debugfs nodev /debug\n\n"
1554 "# cat /debug/tracing/available_tracers\n"
1555 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1556 "# cat /debug/tracing/current_tracer\n"
1558 "# echo sched_switch > /debug/tracing/current_tracer\n"
1559 "# cat /debug/tracing/current_tracer\n"
1561 "# cat /debug/tracing/iter_ctrl\n"
1562 "noprint-parent nosym-offset nosym-addr noverbose\n"
1563 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1564 "# echo 1 > /debug/tracing/tracing_enabled\n"
1565 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1566 "echo 0 > /debug/tracing/tracing_enabled\n"
1570 tracing_readme_read(struct file *filp, char __user *ubuf,
1571 size_t cnt, loff_t *ppos)
1573 return simple_read_from_buffer(ubuf, cnt, ppos,
1574 readme_msg, strlen(readme_msg));
1577 static struct file_operations tracing_readme_fops = {
1578 .open = tracing_open_generic,
1579 .read = tracing_readme_read,
1584 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1585 size_t cnt, loff_t *ppos)
1587 struct trace_array *tr = filp->private_data;
1591 r = sprintf(buf, "%ld\n", tr->ctrl);
1592 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1596 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1597 size_t cnt, loff_t *ppos)
1599 struct trace_array *tr = filp->private_data;
1606 if (copy_from_user(&buf, ubuf, cnt))
1611 val = simple_strtoul(buf, NULL, 10);
1615 mutex_lock(&trace_types_lock);
1616 if (tr->ctrl ^ val) {
1624 if (current_trace && current_trace->ctrl_update)
1625 current_trace->ctrl_update(tr);
1627 mutex_unlock(&trace_types_lock);
1635 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1636 size_t cnt, loff_t *ppos)
1638 char buf[max_tracer_type_len+2];
1641 mutex_lock(&trace_types_lock);
1643 r = sprintf(buf, "%s\n", current_trace->name);
1645 r = sprintf(buf, "\n");
1646 mutex_unlock(&trace_types_lock);
1648 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1652 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1653 size_t cnt, loff_t *ppos)
1655 struct trace_array *tr = &global_trace;
1657 char buf[max_tracer_type_len+1];
1660 if (cnt > max_tracer_type_len)
1661 cnt = max_tracer_type_len;
1663 if (copy_from_user(&buf, ubuf, cnt))
1668 /* strip ending whitespace. */
1669 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1672 mutex_lock(&trace_types_lock);
1673 for (t = trace_types; t; t = t->next) {
1674 if (strcmp(t->name, buf) == 0)
1677 if (!t || t == current_trace)
1680 if (current_trace && current_trace->reset)
1681 current_trace->reset(tr);
1688 mutex_unlock(&trace_types_lock);
1696 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1697 size_t cnt, loff_t *ppos)
1699 unsigned long *ptr = filp->private_data;
1703 r = snprintf(buf, 64, "%ld\n",
1704 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1707 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1711 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1712 size_t cnt, loff_t *ppos)
1714 long *ptr = filp->private_data;
1721 if (copy_from_user(&buf, ubuf, cnt))
1726 val = simple_strtoul(buf, NULL, 10);
1733 static atomic_t tracing_reader;
1735 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1737 struct trace_iterator *iter;
1739 if (tracing_disabled)
1742 /* We only allow for reader of the pipe */
1743 if (atomic_inc_return(&tracing_reader) != 1) {
1744 atomic_dec(&tracing_reader);
1748 /* create a buffer to store the information to pass to userspace */
1749 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1753 iter->tr = &global_trace;
1755 filp->private_data = iter;
1760 static int tracing_release_pipe(struct inode *inode, struct file *file)
1762 struct trace_iterator *iter = file->private_data;
1765 atomic_dec(&tracing_reader);
1774 tracing_read_pipe(struct file *filp, char __user *ubuf,
1775 size_t cnt, loff_t *ppos)
1777 struct trace_iterator *iter = filp->private_data;
1778 struct trace_array_cpu *data;
1779 static cpumask_t mask;
1780 struct trace_entry *entry;
1782 unsigned long flags;
1788 /* return any leftover data */
1789 if (iter->seq.len > start) {
1790 len = iter->seq.len - start;
1793 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1802 trace_seq_reset(&iter->seq);
1805 while (trace_empty(iter)) {
1807 * This is a make-shift waitqueue. The reason we don't use
1808 * an actual wait queue is because:
1809 * 1) we only ever have one waiter
1810 * 2) the tracing, traces all functions, we don't want
1811 * the overhead of calling wake_up and friends
1812 * (and tracing them too)
1813 * Anyway, this is really very primitive wakeup.
1815 set_current_state(TASK_INTERRUPTIBLE);
1816 iter->tr->waiter = current;
1818 /* sleep for one second, and try again. */
1819 schedule_timeout(HZ);
1821 iter->tr->waiter = NULL;
1823 if (signal_pending(current))
1827 * We block until we read something and tracing is disabled.
1828 * We still block if tracing is disabled, but we have never
1829 * read anything. This allows a user to cat this file, and
1830 * then enable tracing. But after we have read something,
1831 * we give an EOF when tracing is again disabled.
1833 * iter->pos will be 0 if we haven't read anything.
1835 if (!tracer_enabled && iter->pos)
1841 /* stop when tracing is finished */
1842 if (trace_empty(iter))
1845 if (cnt >= PAGE_SIZE)
1846 cnt = PAGE_SIZE - 1;
1848 memset(iter, 0, sizeof(*iter));
1849 iter->tr = &global_trace;
1853 * We need to stop all tracing on all CPUS to read the
1854 * the next buffer. This is a bit expensive, but is
1855 * not done often. We fill all what we can read,
1856 * and then release the locks again.
1860 local_irq_save(flags);
1861 for_each_possible_cpu(cpu) {
1862 data = iter->tr->data[cpu];
1864 if (!head_page(data) || !data->trace_idx)
1867 atomic_inc(&data->disabled);
1868 spin_lock(&data->lock);
1872 while ((entry = find_next_entry_inc(iter)) != NULL) {
1873 ret = print_trace_fmt(iter);
1877 trace_consume(iter);
1879 if (iter->seq.len >= cnt)
1883 for_each_cpu_mask(cpu, mask) {
1884 data = iter->tr->data[cpu];
1885 spin_unlock(&data->lock);
1886 atomic_dec(&data->disabled);
1888 local_irq_restore(flags);
1890 /* Now copy what we have to the user */
1891 read = iter->seq.len;
1895 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1897 if (read < iter->seq.len)
1900 trace_seq_reset(&iter->seq);
1908 static struct file_operations tracing_max_lat_fops = {
1909 .open = tracing_open_generic,
1910 .read = tracing_max_lat_read,
1911 .write = tracing_max_lat_write,
1914 static struct file_operations tracing_ctrl_fops = {
1915 .open = tracing_open_generic,
1916 .read = tracing_ctrl_read,
1917 .write = tracing_ctrl_write,
1920 static struct file_operations set_tracer_fops = {
1921 .open = tracing_open_generic,
1922 .read = tracing_set_trace_read,
1923 .write = tracing_set_trace_write,
1926 static struct file_operations tracing_pipe_fops = {
1927 .open = tracing_open_pipe,
1928 .read = tracing_read_pipe,
1929 .release = tracing_release_pipe,
1932 #ifdef CONFIG_DYNAMIC_FTRACE
1935 tracing_read_long(struct file *filp, char __user *ubuf,
1936 size_t cnt, loff_t *ppos)
1938 unsigned long *p = filp->private_data;
1942 r = sprintf(buf, "%ld\n", *p);
1944 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1947 static struct file_operations tracing_read_long_fops = {
1948 .open = tracing_open_generic,
1949 .read = tracing_read_long,
1953 static struct dentry *d_tracer;
1955 struct dentry *tracing_init_dentry(void)
1962 d_tracer = debugfs_create_dir("tracing", NULL);
1964 if (!d_tracer && !once) {
1966 pr_warning("Could not create debugfs directory 'tracing'\n");
1973 #ifdef CONFIG_FTRACE_SELFTEST
1974 /* Let selftest have access to static functions in this file */
1975 #include "trace_selftest.c"
1978 static __init void tracer_init_debugfs(void)
1980 struct dentry *d_tracer;
1981 struct dentry *entry;
1983 d_tracer = tracing_init_dentry();
1985 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1986 &global_trace, &tracing_ctrl_fops);
1988 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1990 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1991 NULL, &tracing_iter_fops);
1993 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1995 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1996 &global_trace, &tracing_lt_fops);
1998 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2000 entry = debugfs_create_file("trace", 0444, d_tracer,
2001 &global_trace, &tracing_fops);
2003 pr_warning("Could not create debugfs 'trace' entry\n");
2005 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2006 &global_trace, &show_traces_fops);
2008 pr_warning("Could not create debugfs 'trace' entry\n");
2010 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2011 &global_trace, &set_tracer_fops);
2013 pr_warning("Could not create debugfs 'trace' entry\n");
2015 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2016 &tracing_max_latency,
2017 &tracing_max_lat_fops);
2019 pr_warning("Could not create debugfs "
2020 "'tracing_max_latency' entry\n");
2022 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2023 &tracing_thresh, &tracing_max_lat_fops);
2025 pr_warning("Could not create debugfs "
2026 "'tracing_threash' entry\n");
2027 entry = debugfs_create_file("README", 0644, d_tracer,
2028 NULL, &tracing_readme_fops);
2030 pr_warning("Could not create debugfs 'README' entry\n");
2032 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2033 NULL, &tracing_pipe_fops);
2035 pr_warning("Could not create debugfs "
2036 "'tracing_threash' entry\n");
2038 #ifdef CONFIG_DYNAMIC_FTRACE
2039 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2040 &ftrace_update_tot_cnt,
2041 &tracing_read_long_fops);
2043 pr_warning("Could not create debugfs "
2044 "'dyn_ftrace_total_info' entry\n");
2048 /* dummy trace to disable tracing */
2049 static struct tracer no_tracer __read_mostly =
2054 static int trace_alloc_page(void)
2056 struct trace_array_cpu *data;
2057 struct page *page, *tmp;
2062 /* first allocate a page for each CPU */
2063 for_each_possible_cpu(i) {
2064 array = (void *)__get_free_page(GFP_KERNEL);
2065 if (array == NULL) {
2066 printk(KERN_ERR "tracer: failed to allocate page"
2067 "for trace buffer!\n");
2071 page = virt_to_page(array);
2072 list_add(&page->lru, &pages);
2074 /* Only allocate if we are actually using the max trace */
2075 #ifdef CONFIG_TRACER_MAX_TRACE
2076 array = (void *)__get_free_page(GFP_KERNEL);
2077 if (array == NULL) {
2078 printk(KERN_ERR "tracer: failed to allocate page"
2079 "for trace buffer!\n");
2082 page = virt_to_page(array);
2083 list_add(&page->lru, &pages);
2087 /* Now that we successfully allocate a page per CPU, add them */
2088 for_each_possible_cpu(i) {
2089 data = global_trace.data[i];
2090 spin_lock_init(&data->lock);
2091 lockdep_set_class(&data->lock, &data->lock_key);
2092 page = list_entry(pages.next, struct page, lru);
2093 list_del_init(&page->lru);
2094 list_add_tail(&page->lru, &data->trace_pages);
2097 #ifdef CONFIG_TRACER_MAX_TRACE
2098 data = max_tr.data[i];
2099 spin_lock_init(&data->lock);
2100 lockdep_set_class(&data->lock, &data->lock_key);
2101 page = list_entry(pages.next, struct page, lru);
2102 list_del_init(&page->lru);
2103 list_add_tail(&page->lru, &data->trace_pages);
2107 global_trace.entries += ENTRIES_PER_PAGE;
2112 list_for_each_entry_safe(page, tmp, &pages, lru) {
2113 list_del_init(&page->lru);
2119 __init static int tracer_alloc_buffers(void)
2121 struct trace_array_cpu *data;
2128 /* Allocate the first page for all buffers */
2129 for_each_possible_cpu(i) {
2130 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2131 max_tr.data[i] = &per_cpu(max_data, i);
2133 array = (void *)__get_free_page(GFP_KERNEL);
2134 if (array == NULL) {
2135 printk(KERN_ERR "tracer: failed to allocate page"
2136 "for trace buffer!\n");
2140 /* set the array to the list */
2141 INIT_LIST_HEAD(&data->trace_pages);
2142 page = virt_to_page(array);
2143 list_add(&page->lru, &data->trace_pages);
2144 /* use the LRU flag to differentiate the two buffers */
2147 /* Only allocate if we are actually using the max trace */
2148 #ifdef CONFIG_TRACER_MAX_TRACE
2149 array = (void *)__get_free_page(GFP_KERNEL);
2150 if (array == NULL) {
2151 printk(KERN_ERR "tracer: failed to allocate page"
2152 "for trace buffer!\n");
2156 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2157 page = virt_to_page(array);
2158 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2164 * Since we allocate by orders of pages, we may be able to
2167 global_trace.entries = ENTRIES_PER_PAGE;
2170 while (global_trace.entries < trace_nr_entries) {
2171 if (trace_alloc_page())
2175 max_tr.entries = global_trace.entries;
2177 pr_info("tracer: %d pages allocated for %ld",
2178 pages, trace_nr_entries);
2179 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2180 pr_info(" actual entries %ld\n", global_trace.entries);
2182 tracer_init_debugfs();
2184 trace_init_cmdlines();
2186 register_tracer(&no_tracer);
2187 current_trace = &no_tracer;
2189 /* All seems OK, enable tracing */
2190 tracing_disabled = 0;
2195 for (i-- ; i >= 0; i--) {
2196 struct page *page, *tmp;
2197 struct trace_array_cpu *data = global_trace.data[i];
2200 list_for_each_entry_safe(page, tmp,
2201 &data->trace_pages, lru) {
2202 list_del_init(&page->lru);
2207 #ifdef CONFIG_TRACER_MAX_TRACE
2208 data = max_tr.data[i];
2210 list_for_each_entry_safe(page, tmp,
2211 &data->trace_pages, lru) {
2212 list_del_init(&page->lru);
2220 fs_initcall(tracer_alloc_buffers);