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 atomic_t tracer_counter;
46 static struct trace_array global_trace;
48 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
50 static struct trace_array max_tr;
52 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
54 static int tracer_enabled;
55 static unsigned long trace_nr_entries = 16384UL;
57 static struct tracer *trace_types __read_mostly;
58 static struct tracer *current_trace __read_mostly;
59 static int max_tracer_type_len;
61 static DEFINE_MUTEX(trace_types_lock);
63 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
65 static int __init set_nr_entries(char *str)
69 trace_nr_entries = simple_strtoul(str, &str, 0);
72 __setup("trace_entries=", set_nr_entries);
74 unsigned long nsecs_to_usecs(unsigned long nsecs)
80 __TRACE_FIRST_TYPE = 0,
88 enum trace_flag_type {
89 TRACE_FLAG_IRQS_OFF = 0x01,
90 TRACE_FLAG_NEED_RESCHED = 0x02,
91 TRACE_FLAG_HARDIRQ = 0x04,
92 TRACE_FLAG_SOFTIRQ = 0x08,
95 enum trace_iterator_flags {
96 TRACE_ITER_PRINT_PARENT = 0x01,
97 TRACE_ITER_SYM_OFFSET = 0x02,
98 TRACE_ITER_SYM_ADDR = 0x04,
99 TRACE_ITER_VERBOSE = 0x08,
102 #define TRACE_ITER_SYM_MASK \
103 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
105 /* These must match the bit postions above */
106 static const char *trace_options[] = {
114 static unsigned trace_flags;
116 static DEFINE_SPINLOCK(ftrace_max_lock);
119 * Copy the new maximum trace into the separate maximum-trace
120 * structure. (this way the maximum trace is permanently saved,
121 * for later retrieval via /debugfs/tracing/latency_trace)
124 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
126 struct trace_array_cpu *data = tr->data[cpu];
129 max_tr.time_start = data->preempt_timestamp;
131 data = max_tr.data[cpu];
132 data->saved_latency = tracing_max_latency;
134 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
135 data->pid = tsk->pid;
136 data->uid = tsk->uid;
137 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
138 data->policy = tsk->policy;
139 data->rt_priority = tsk->rt_priority;
141 /* record this tasks comm */
142 tracing_record_cmdline(current);
145 void check_pages(struct trace_array_cpu *data)
147 struct page *page, *tmp;
149 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
150 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
152 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
153 BUG_ON(page->lru.next->prev != &page->lru);
154 BUG_ON(page->lru.prev->next != &page->lru);
158 void *head_page(struct trace_array_cpu *data)
163 if (list_empty(&data->trace_pages))
166 page = list_entry(data->trace_pages.next, struct page, lru);
167 BUG_ON(&page->lru == &data->trace_pages);
169 return page_address(page);
173 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
175 struct list_head flip_pages;
177 INIT_LIST_HEAD(&flip_pages);
179 tr1->trace_current = NULL;
180 memcpy(&tr1->trace_current_idx, &tr2->trace_current_idx,
181 sizeof(struct trace_array_cpu) -
182 offsetof(struct trace_array_cpu, trace_current_idx));
186 list_splice_init(&tr1->trace_pages, &flip_pages);
187 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
188 list_splice_init(&flip_pages, &tr2->trace_pages);
189 BUG_ON(!list_empty(&flip_pages));
195 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
197 struct trace_array_cpu *data;
200 WARN_ON_ONCE(!irqs_disabled());
201 spin_lock(&ftrace_max_lock);
202 /* clear out all the previous traces */
203 for_each_possible_cpu(i) {
205 flip_trace(max_tr.data[i], data);
209 __update_max_tr(tr, tsk, cpu);
210 spin_unlock(&ftrace_max_lock);
214 * update_max_tr_single - only copy one trace over, and reset the rest
216 * @tsk - task with the latency
217 * @cpu - the cpu of the buffer to copy.
220 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
222 struct trace_array_cpu *data = tr->data[cpu];
225 WARN_ON_ONCE(!irqs_disabled());
226 spin_lock(&ftrace_max_lock);
227 for_each_possible_cpu(i)
228 tracing_reset(max_tr.data[i]);
230 flip_trace(max_tr.data[cpu], data);
234 __update_max_tr(tr, tsk, cpu);
235 spin_unlock(&ftrace_max_lock);
238 int register_tracer(struct tracer *type)
245 pr_info("Tracer must have a name\n");
249 mutex_lock(&trace_types_lock);
250 for (t = trace_types; t; t = t->next) {
251 if (strcmp(type->name, t->name) == 0) {
253 pr_info("Trace %s already registered\n",
260 #ifdef CONFIG_FTRACE_STARTUP_TEST
261 if (type->selftest) {
262 struct tracer *saved_tracer = current_trace;
263 struct trace_array_cpu *data;
264 struct trace_array *tr = &global_trace;
265 int saved_ctrl = tr->ctrl;
268 * Run a selftest on this tracer.
269 * Here we reset the trace buffer, and set the current
270 * tracer to be this tracer. The tracer can then run some
271 * internal tracing to verify that everything is in order.
272 * If we fail, we do not register this tracer.
274 for_each_possible_cpu(i) {
276 if (!head_page(data))
280 current_trace = type;
282 /* the test is responsible for initializing and enabling */
283 pr_info("Testing tracer %s: ", type->name);
284 ret = type->selftest(type, tr);
285 /* the test is responsible for resetting too */
286 current_trace = saved_tracer;
287 tr->ctrl = saved_ctrl;
289 printk(KERN_CONT "FAILED!\n");
292 /* Only reset on passing, to avoid touching corrupted buffers */
293 for_each_possible_cpu(i) {
295 if (!head_page(data))
299 printk(KERN_CONT "PASSED\n");
303 type->next = trace_types;
305 len = strlen(type->name);
306 if (len > max_tracer_type_len)
307 max_tracer_type_len = len;
310 mutex_unlock(&trace_types_lock);
315 void unregister_tracer(struct tracer *type)
320 mutex_lock(&trace_types_lock);
321 for (t = &trace_types; *t; t = &(*t)->next) {
325 pr_info("Trace %s not registered\n", type->name);
330 if (strlen(type->name) != max_tracer_type_len)
333 max_tracer_type_len = 0;
334 for (t = &trace_types; *t; t = &(*t)->next) {
335 len = strlen((*t)->name);
336 if (len > max_tracer_type_len)
337 max_tracer_type_len = len;
340 mutex_unlock(&trace_types_lock);
343 notrace void tracing_reset(struct trace_array_cpu *data)
346 data->trace_current = head_page(data);
347 data->trace_current_idx = 0;
348 data->time_offset = 0;
353 function_trace_call(unsigned long ip, unsigned long parent_ip)
355 struct trace_array *tr = &global_trace;
356 struct trace_array_cpu *data;
361 if (unlikely(!tracer_enabled))
364 local_irq_save(flags);
365 cpu = raw_smp_processor_id();
366 data = tr->data[cpu];
367 disabled = atomic_inc_return(&data->disabled);
369 if (likely(disabled == 1))
370 ftrace(tr, data, ip, parent_ip, flags);
372 atomic_dec(&data->disabled);
373 local_irq_restore(flags);
376 static struct ftrace_ops trace_ops __read_mostly =
378 .func = function_trace_call,
382 notrace void tracing_start_function_trace(void)
384 register_ftrace_function(&trace_ops);
387 notrace void tracing_stop_function_trace(void)
389 unregister_ftrace_function(&trace_ops);
392 #define SAVED_CMDLINES 128
393 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
394 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
395 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
396 static int cmdline_idx;
397 static DEFINE_SPINLOCK(trace_cmdline_lock);
398 atomic_t trace_record_cmdline_disabled;
400 static void trace_init_cmdlines(void)
402 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
403 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
407 notrace void trace_stop_cmdline_recording(void);
409 static notrace void trace_save_cmdline(struct task_struct *tsk)
414 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
418 * It's not the end of the world if we don't get
419 * the lock, but we also don't want to spin
420 * nor do we want to disable interrupts,
421 * so if we miss here, then better luck next time.
423 if (!spin_trylock(&trace_cmdline_lock))
426 idx = map_pid_to_cmdline[tsk->pid];
427 if (idx >= SAVED_CMDLINES) {
428 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
430 map = map_cmdline_to_pid[idx];
431 if (map <= PID_MAX_DEFAULT)
432 map_pid_to_cmdline[map] = (unsigned)-1;
434 map_pid_to_cmdline[tsk->pid] = idx;
439 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
441 spin_unlock(&trace_cmdline_lock);
444 static notrace char *trace_find_cmdline(int pid)
446 char *cmdline = "<...>";
452 if (pid > PID_MAX_DEFAULT)
455 map = map_pid_to_cmdline[pid];
456 if (map >= SAVED_CMDLINES)
459 cmdline = saved_cmdlines[map];
465 notrace void tracing_record_cmdline(struct task_struct *tsk)
467 if (atomic_read(&trace_record_cmdline_disabled))
470 trace_save_cmdline(tsk);
473 static inline notrace struct trace_entry *
474 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
476 unsigned long idx, idx_next;
477 struct trace_entry *entry;
478 struct list_head *next;
482 idx = data->trace_current_idx;
485 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
487 entry = data->trace_current + idx * TRACE_ENTRY_SIZE;
489 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
490 page = virt_to_page(data->trace_current);
492 * Roundrobin - but skip the head (which is not a real page):
494 next = page->lru.next;
495 if (unlikely(next == &data->trace_pages))
497 BUG_ON(next == &data->trace_pages);
499 page = list_entry(next, struct page, lru);
500 data->trace_current = page_address(page);
504 data->trace_current_idx = idx_next;
509 static inline notrace void
510 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
512 struct task_struct *tsk = current;
515 pc = preempt_count();
517 entry->idx = atomic_inc_return(&tracer_counter);
518 entry->preempt_count = pc & 0xff;
519 entry->pid = tsk->pid;
520 entry->t = now(raw_smp_processor_id());
521 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
522 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
523 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
524 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
528 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
529 unsigned long ip, unsigned long parent_ip, unsigned long flags)
531 struct trace_entry *entry;
533 entry = tracing_get_trace_entry(tr, data);
534 tracing_generic_entry_update(entry, flags);
535 entry->type = TRACE_FN;
537 entry->fn.parent_ip = parent_ip;
541 tracing_sched_switch_trace(struct trace_array *tr,
542 struct trace_array_cpu *data,
543 struct task_struct *prev, struct task_struct *next,
546 struct trace_entry *entry;
548 entry = tracing_get_trace_entry(tr, data);
549 tracing_generic_entry_update(entry, flags);
550 entry->type = TRACE_CTX;
551 entry->ctx.prev_pid = prev->pid;
552 entry->ctx.prev_prio = prev->prio;
553 entry->ctx.prev_state = prev->state;
554 entry->ctx.next_pid = next->pid;
555 entry->ctx.next_prio = next->prio;
558 enum trace_file_type {
559 TRACE_FILE_LAT_FMT = 1,
562 static struct trace_entry *
563 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
564 struct trace_iterator *iter, int cpu)
567 struct trace_entry *array;
569 if (iter->next_idx[cpu] >= tr->entries ||
570 iter->next_idx[cpu] >= data->trace_idx)
573 if (!iter->next_page[cpu]) {
575 * Initialize. If the count of elements in
576 * this buffer is greater than the max entries
577 * we had an underrun. Which means we looped around.
578 * We can simply use the current pointer as our
581 if (data->trace_idx >= tr->entries) {
582 page = virt_to_page(data->trace_current);
583 iter->next_page[cpu] = &page->lru;
584 iter->next_page_idx[cpu] = data->trace_current_idx;
586 iter->next_page[cpu] = data->trace_pages.next;
587 iter->next_page_idx[cpu] = 0;
591 page = list_entry(iter->next_page[cpu], struct page, lru);
592 BUG_ON(&data->trace_pages == &page->lru);
594 array = page_address(page);
596 return &array[iter->next_page_idx[cpu]];
599 static struct notrace trace_entry *
600 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
602 struct trace_array *tr = iter->tr;
603 struct trace_entry *ent, *next = NULL;
607 for_each_possible_cpu(cpu) {
608 if (!head_page(tr->data[cpu]))
610 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
612 (!next || (long)(next->idx - ent->idx) > 0)) {
624 static void *find_next_entry_inc(struct trace_iterator *iter)
626 struct trace_entry *next;
629 next = find_next_entry(iter, &next_cpu);
633 iter->next_idx[next_cpu]++;
634 iter->next_page_idx[next_cpu]++;
636 if (iter->next_page_idx[next_cpu] >= ENTRIES_PER_PAGE) {
637 struct trace_array_cpu *data = iter->tr->data[next_cpu];
639 iter->next_page_idx[next_cpu] = 0;
640 iter->next_page[next_cpu] =
641 iter->next_page[next_cpu]->next;
642 if (iter->next_page[next_cpu] == &data->trace_pages)
643 iter->next_page[next_cpu] =
644 data->trace_pages.next;
647 iter->prev_ent = iter->ent;
648 iter->prev_cpu = iter->cpu;
651 iter->cpu = next_cpu;
653 return next ? iter : NULL;
656 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
658 struct trace_iterator *iter = m->private;
659 void *last_ent = iter->ent;
665 /* can't go backwards */
670 ent = find_next_entry_inc(iter);
674 while (ent && iter->idx < i)
675 ent = find_next_entry_inc(iter);
679 if (last_ent && !ent)
680 seq_puts(m, "\n\nvim:ft=help\n");
685 static void *s_start(struct seq_file *m, loff_t *pos)
687 struct trace_iterator *iter = m->private;
692 mutex_lock(&trace_types_lock);
694 if (!current_trace || current_trace != iter->trace)
697 atomic_inc(&trace_record_cmdline_disabled);
699 /* let the tracer grab locks here if needed */
700 if (current_trace->start)
701 current_trace->start(iter);
703 if (*pos != iter->pos) {
707 iter->prev_ent = NULL;
710 for_each_possible_cpu(i) {
711 iter->next_idx[i] = 0;
712 iter->next_page[i] = NULL;
715 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
720 p = s_next(m, p, &l);
726 static void s_stop(struct seq_file *m, void *p)
728 struct trace_iterator *iter = m->private;
730 atomic_dec(&trace_record_cmdline_disabled);
732 /* let the tracer release locks here if needed */
733 if (current_trace && current_trace == iter->trace && iter->trace->stop)
734 iter->trace->stop(iter);
736 mutex_unlock(&trace_types_lock);
740 seq_print_sym_short(struct seq_file *m, const char *fmt, unsigned long address)
742 #ifdef CONFIG_KALLSYMS
743 char str[KSYM_SYMBOL_LEN];
745 kallsyms_lookup(address, NULL, NULL, NULL, str);
747 seq_printf(m, fmt, str);
752 seq_print_sym_offset(struct seq_file *m, const char *fmt, unsigned long address)
754 #ifdef CONFIG_KALLSYMS
755 char str[KSYM_SYMBOL_LEN];
757 sprint_symbol(str, address);
758 seq_printf(m, fmt, str);
763 # define IP_FMT "%08lx"
765 # define IP_FMT "%016lx"
769 seq_print_ip_sym(struct seq_file *m, unsigned long ip, unsigned long sym_flags)
776 if (sym_flags & TRACE_ITER_SYM_OFFSET)
777 seq_print_sym_offset(m, "%s", ip);
779 seq_print_sym_short(m, "%s", ip);
781 if (sym_flags & TRACE_ITER_SYM_ADDR)
782 seq_printf(m, " <" IP_FMT ">", ip);
785 static notrace void print_lat_help_header(struct seq_file *m)
787 seq_puts(m, "# _------=> CPU# \n");
788 seq_puts(m, "# / _-----=> irqs-off \n");
789 seq_puts(m, "# | / _----=> need-resched \n");
790 seq_puts(m, "# || / _---=> hardirq/softirq \n");
791 seq_puts(m, "# ||| / _--=> preempt-depth \n");
792 seq_puts(m, "# |||| / \n");
793 seq_puts(m, "# ||||| delay \n");
794 seq_puts(m, "# cmd pid ||||| time | caller \n");
795 seq_puts(m, "# \\ / ||||| \\ | / \n");
798 static notrace void print_func_help_header(struct seq_file *m)
800 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
801 seq_puts(m, "# | | | | |\n");
806 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
808 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
809 struct trace_array *tr = iter->tr;
810 struct trace_array_cpu *data = tr->data[tr->cpu];
811 struct tracer *type = current_trace;
812 unsigned long total = 0;
813 unsigned long entries = 0;
815 const char *name = "preemption";
820 for_each_possible_cpu(cpu) {
821 if (head_page(tr->data[cpu])) {
822 total += tr->data[cpu]->trace_idx;
823 if (tr->data[cpu]->trace_idx > tr->entries)
824 entries += tr->entries;
826 entries += tr->data[cpu]->trace_idx;
830 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
832 seq_puts(m, "-----------------------------------"
833 "---------------------------------\n");
834 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
835 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
836 nsecs_to_usecs(data->saved_latency),
840 #if defined(CONFIG_PREEMPT_NONE)
842 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
844 #elif defined(CONFIG_PREEMPT_DESKTOP)
849 /* These are reserved for later use */
852 seq_printf(m, " #P:%d)\n", num_online_cpus());
856 seq_puts(m, " -----------------\n");
857 seq_printf(m, " | task: %.16s-%d "
858 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
859 data->comm, data->pid, data->uid, data->nice,
860 data->policy, data->rt_priority);
861 seq_puts(m, " -----------------\n");
863 if (data->critical_start) {
864 seq_puts(m, " => started at: ");
865 seq_print_ip_sym(m, data->critical_start, sym_flags);
866 seq_puts(m, "\n => ended at: ");
867 seq_print_ip_sym(m, data->critical_end, sym_flags);
875 lat_print_generic(struct seq_file *m, struct trace_entry *entry, int cpu)
877 int hardirq, softirq;
880 comm = trace_find_cmdline(entry->pid);
882 seq_printf(m, "%8.8s-%-5d ", comm, entry->pid);
883 seq_printf(m, "%d", cpu);
884 seq_printf(m, "%c%c",
885 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
886 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
888 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
889 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
890 if (hardirq && softirq)
903 if (entry->preempt_count)
904 seq_printf(m, "%x", entry->preempt_count);
909 unsigned long preempt_mark_thresh = 100;
912 lat_print_timestamp(struct seq_file *m, unsigned long long abs_usecs,
913 unsigned long rel_usecs)
915 seq_printf(m, " %4lldus", abs_usecs);
916 if (rel_usecs > preempt_mark_thresh)
918 else if (rel_usecs > 1)
924 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
927 print_lat_fmt(struct seq_file *m, struct trace_iterator *iter,
928 unsigned int trace_idx, int cpu)
930 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
931 struct trace_entry *next_entry = find_next_entry(iter, NULL);
932 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
933 struct trace_entry *entry = iter->ent;
934 unsigned long abs_usecs;
935 unsigned long rel_usecs;
941 rel_usecs = ns2usecs(next_entry->t - entry->t);
942 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
945 comm = trace_find_cmdline(entry->pid);
946 seq_printf(m, "%16s %5d %d %d %08x %08x [%08lx]"
947 " %ld.%03ldms (+%ld.%03ldms): ",
949 entry->pid, cpu, entry->flags,
950 entry->preempt_count, trace_idx,
953 abs_usecs % 1000, rel_usecs/1000, rel_usecs % 1000);
955 lat_print_generic(m, entry, cpu);
956 lat_print_timestamp(m, abs_usecs, rel_usecs);
958 switch (entry->type) {
960 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
962 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
966 S = entry->ctx.prev_state < sizeof(state_to_char) ?
967 state_to_char[entry->ctx.prev_state] : 'X';
968 comm = trace_find_cmdline(entry->ctx.next_pid);
969 seq_printf(m, " %d:%d:%c --> %d:%d %s\n",
971 entry->ctx.prev_prio,
974 entry->ctx.next_prio,
978 seq_printf(m, "Unknown type %d\n", entry->type);
982 static notrace void sync_time_offset(struct trace_iterator *iter)
984 struct trace_array_cpu *prev_array, *array;
985 struct trace_entry *prev_entry, *entry;
989 prev_entry = iter->prev_ent;
993 prev_array = iter->tr->data[iter->prev_cpu];
994 array = iter->tr->data[iter->cpu];
996 prev_t = prev_entry->t + prev_array->time_offset;
997 t = entry->t + array->time_offset;
1000 * If time goes backwards we increase the offset of
1001 * the current array, to not have observable time warps.
1002 * This will quickly synchronize the time offsets of
1006 array->time_offset += prev_t - t;
1010 print_trace_fmt(struct seq_file *m, struct trace_iterator *iter)
1012 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1013 struct trace_entry *entry;
1014 unsigned long usec_rem;
1015 unsigned long long t;
1020 sync_time_offset(iter);
1023 comm = trace_find_cmdline(iter->ent->pid);
1025 t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1026 usec_rem = do_div(t, 1000000ULL);
1027 secs = (unsigned long)t;
1029 seq_printf(m, "%16s-%-5d ", comm, entry->pid);
1030 seq_printf(m, "[%02d] ", iter->cpu);
1031 seq_printf(m, "%5lu.%06lu: ", secs, usec_rem);
1033 switch (entry->type) {
1035 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
1036 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1037 entry->fn.parent_ip) {
1038 seq_printf(m, " <-");
1039 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
1041 seq_printf(m, "\n");
1044 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1045 state_to_char[entry->ctx.prev_state] : 'X';
1046 seq_printf(m, " %d:%d:%c ==> %d:%d\n",
1047 entry->ctx.prev_pid,
1048 entry->ctx.prev_prio,
1050 entry->ctx.next_pid,
1051 entry->ctx.next_prio);
1056 static int trace_empty(struct trace_iterator *iter)
1058 struct trace_array_cpu *data;
1061 for_each_possible_cpu(cpu) {
1062 data = iter->tr->data[cpu];
1064 if (head_page(data) && data->trace_idx)
1070 static int s_show(struct seq_file *m, void *v)
1072 struct trace_iterator *iter = v;
1074 if (iter->ent == NULL) {
1076 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1079 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1080 /* print nothing if the buffers are empty */
1081 if (trace_empty(iter))
1083 print_trace_header(m, iter);
1084 if (!(trace_flags & TRACE_ITER_VERBOSE))
1085 print_lat_help_header(m);
1087 if (!(trace_flags & TRACE_ITER_VERBOSE))
1088 print_func_help_header(m);
1091 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1092 print_lat_fmt(m, iter, iter->idx, iter->cpu);
1094 print_trace_fmt(m, iter);
1100 static struct seq_operations tracer_seq_ops = {
1107 static struct trace_iterator notrace *
1108 __tracing_open(struct inode *inode, struct file *file, int *ret)
1110 struct trace_iterator *iter;
1112 if (tracing_disabled) {
1117 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1123 mutex_lock(&trace_types_lock);
1124 if (current_trace && current_trace->print_max)
1127 iter->tr = inode->i_private;
1128 iter->trace = current_trace;
1131 /* TODO stop tracer */
1132 *ret = seq_open(file, &tracer_seq_ops);
1134 struct seq_file *m = file->private_data;
1137 /* stop the trace while dumping */
1141 if (iter->trace && iter->trace->open)
1142 iter->trace->open(iter);
1147 mutex_unlock(&trace_types_lock);
1153 int tracing_open_generic(struct inode *inode, struct file *filp)
1155 if (tracing_disabled)
1158 filp->private_data = inode->i_private;
1162 int tracing_release(struct inode *inode, struct file *file)
1164 struct seq_file *m = (struct seq_file *)file->private_data;
1165 struct trace_iterator *iter = m->private;
1167 mutex_lock(&trace_types_lock);
1168 if (iter->trace && iter->trace->close)
1169 iter->trace->close(iter);
1171 /* reenable tracing if it was previously enabled */
1174 mutex_unlock(&trace_types_lock);
1176 seq_release(inode, file);
1181 static int tracing_open(struct inode *inode, struct file *file)
1185 __tracing_open(inode, file, &ret);
1190 static int tracing_lt_open(struct inode *inode, struct file *file)
1192 struct trace_iterator *iter;
1195 iter = __tracing_open(inode, file, &ret);
1198 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1204 static notrace void *
1205 t_next(struct seq_file *m, void *v, loff_t *pos)
1207 struct tracer *t = m->private;
1219 static void *t_start(struct seq_file *m, loff_t *pos)
1221 struct tracer *t = m->private;
1224 mutex_lock(&trace_types_lock);
1225 for (; t && l < *pos; t = t_next(m, t, &l))
1231 static void t_stop(struct seq_file *m, void *p)
1233 mutex_unlock(&trace_types_lock);
1236 static int t_show(struct seq_file *m, void *v)
1238 struct tracer *t = v;
1243 seq_printf(m, "%s", t->name);
1252 static struct seq_operations show_traces_seq_ops = {
1259 static int show_traces_open(struct inode *inode, struct file *file)
1263 if (tracing_disabled)
1266 ret = seq_open(file, &show_traces_seq_ops);
1268 struct seq_file *m = file->private_data;
1269 m->private = trace_types;
1275 static struct file_operations tracing_fops = {
1276 .open = tracing_open,
1278 .llseek = seq_lseek,
1279 .release = tracing_release,
1282 static struct file_operations tracing_lt_fops = {
1283 .open = tracing_lt_open,
1285 .llseek = seq_lseek,
1286 .release = tracing_release,
1289 static struct file_operations show_traces_fops = {
1290 .open = show_traces_open,
1292 .release = seq_release,
1296 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1297 size_t cnt, loff_t *ppos)
1304 /* calulate max size */
1305 for (i = 0; trace_options[i]; i++) {
1306 len += strlen(trace_options[i]);
1307 len += 3; /* "no" and space */
1310 /* +2 for \n and \0 */
1311 buf = kmalloc(len + 2, GFP_KERNEL);
1315 for (i = 0; trace_options[i]; i++) {
1316 if (trace_flags & (1 << i))
1317 r += sprintf(buf + r, "%s ", trace_options[i]);
1319 r += sprintf(buf + r, "no%s ", trace_options[i]);
1322 r += sprintf(buf + r, "\n");
1323 WARN_ON(r >= len + 2);
1325 r = simple_read_from_buffer(ubuf, cnt, ppos,
1334 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1335 size_t cnt, loff_t *ppos)
1345 if (copy_from_user(&buf, ubuf, cnt))
1350 if (strncmp(buf, "no", 2) == 0) {
1355 for (i = 0; trace_options[i]; i++) {
1356 int len = strlen(trace_options[i]);
1358 if (strncmp(cmp, trace_options[i], len) == 0) {
1360 trace_flags &= ~(1 << i);
1362 trace_flags |= (1 << i);
1372 static struct file_operations tracing_iter_fops = {
1373 .open = tracing_open_generic,
1374 .read = tracing_iter_ctrl_read,
1375 .write = tracing_iter_ctrl_write,
1378 static const char readme_msg[] =
1379 "tracing mini-HOWTO:\n\n"
1381 "# mount -t debugfs nodev /debug\n\n"
1382 "# cat /debug/tracing/available_tracers\n"
1383 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1384 "# cat /debug/tracing/current_tracer\n"
1386 "# echo sched_switch > /debug/tracing/current_tracer\n"
1387 "# cat /debug/tracing/current_tracer\n"
1389 "# cat /debug/tracing/iter_ctrl\n"
1390 "noprint-parent nosym-offset nosym-addr noverbose\n"
1391 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1392 "# echo 1 > /debug/tracing/tracing_enabled\n"
1393 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1394 "echo 0 > /debug/tracing/tracing_enabled\n"
1398 tracing_readme_read(struct file *filp, char __user *ubuf,
1399 size_t cnt, loff_t *ppos)
1401 return simple_read_from_buffer(ubuf, cnt, ppos,
1402 readme_msg, strlen(readme_msg));
1405 static struct file_operations tracing_readme_fops = {
1406 .open = tracing_open_generic,
1407 .read = tracing_readme_read,
1412 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1413 size_t cnt, loff_t *ppos)
1415 struct trace_array *tr = filp->private_data;
1419 r = sprintf(buf, "%ld\n", tr->ctrl);
1420 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1424 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1425 size_t cnt, loff_t *ppos)
1427 struct trace_array *tr = filp->private_data;
1434 if (copy_from_user(&buf, ubuf, cnt))
1439 val = simple_strtoul(buf, NULL, 10);
1443 mutex_lock(&trace_types_lock);
1444 if (tr->ctrl ^ val) {
1452 if (current_trace && current_trace->ctrl_update)
1453 current_trace->ctrl_update(tr);
1455 mutex_unlock(&trace_types_lock);
1463 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1464 size_t cnt, loff_t *ppos)
1466 char buf[max_tracer_type_len+2];
1469 mutex_lock(&trace_types_lock);
1471 r = sprintf(buf, "%s\n", current_trace->name);
1473 r = sprintf(buf, "\n");
1474 mutex_unlock(&trace_types_lock);
1476 return simple_read_from_buffer(ubuf, cnt, ppos,
1481 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1482 size_t cnt, loff_t *ppos)
1484 struct trace_array *tr = &global_trace;
1486 char buf[max_tracer_type_len+1];
1489 if (cnt > max_tracer_type_len)
1490 cnt = max_tracer_type_len;
1492 if (copy_from_user(&buf, ubuf, cnt))
1497 /* strip ending whitespace. */
1498 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1501 mutex_lock(&trace_types_lock);
1502 for (t = trace_types; t; t = t->next) {
1503 if (strcmp(t->name, buf) == 0)
1506 if (!t || t == current_trace)
1509 if (current_trace && current_trace->reset)
1510 current_trace->reset(tr);
1517 mutex_unlock(&trace_types_lock);
1525 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1526 size_t cnt, loff_t *ppos)
1528 unsigned long *ptr = filp->private_data;
1532 r = snprintf(buf, 64, "%ld\n",
1533 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1536 return simple_read_from_buffer(ubuf, cnt, ppos,
1541 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1542 size_t cnt, loff_t *ppos)
1544 long *ptr = filp->private_data;
1551 if (copy_from_user(&buf, ubuf, cnt))
1556 val = simple_strtoul(buf, NULL, 10);
1563 static struct file_operations tracing_max_lat_fops = {
1564 .open = tracing_open_generic,
1565 .read = tracing_max_lat_read,
1566 .write = tracing_max_lat_write,
1569 static struct file_operations tracing_ctrl_fops = {
1570 .open = tracing_open_generic,
1571 .read = tracing_ctrl_read,
1572 .write = tracing_ctrl_write,
1575 static struct file_operations set_tracer_fops = {
1576 .open = tracing_open_generic,
1577 .read = tracing_set_trace_read,
1578 .write = tracing_set_trace_write,
1581 #ifdef CONFIG_DYNAMIC_FTRACE
1584 tracing_read_long(struct file *filp, char __user *ubuf,
1585 size_t cnt, loff_t *ppos)
1587 unsigned long *p = filp->private_data;
1591 r = sprintf(buf, "%ld\n", *p);
1592 return simple_read_from_buffer(ubuf, cnt, ppos,
1596 static struct file_operations tracing_read_long_fops = {
1597 .open = tracing_open_generic,
1598 .read = tracing_read_long,
1602 static struct dentry *d_tracer;
1604 struct dentry *tracing_init_dentry(void)
1611 d_tracer = debugfs_create_dir("tracing", NULL);
1613 if (!d_tracer && !once) {
1615 pr_warning("Could not create debugfs directory 'tracing'\n");
1622 #ifdef CONFIG_FTRACE_SELFTEST
1623 /* Let selftest have access to static functions in this file */
1624 #include "trace_selftest.c"
1627 static __init void tracer_init_debugfs(void)
1629 struct dentry *d_tracer;
1630 struct dentry *entry;
1632 d_tracer = tracing_init_dentry();
1634 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1635 &global_trace, &tracing_ctrl_fops);
1637 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1639 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1640 NULL, &tracing_iter_fops);
1642 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1644 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1645 &global_trace, &tracing_lt_fops);
1647 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1649 entry = debugfs_create_file("trace", 0444, d_tracer,
1650 &global_trace, &tracing_fops);
1652 pr_warning("Could not create debugfs 'trace' entry\n");
1654 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
1655 &global_trace, &show_traces_fops);
1657 pr_warning("Could not create debugfs 'trace' entry\n");
1659 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
1660 &global_trace, &set_tracer_fops);
1662 pr_warning("Could not create debugfs 'trace' entry\n");
1664 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
1665 &tracing_max_latency,
1666 &tracing_max_lat_fops);
1668 pr_warning("Could not create debugfs "
1669 "'tracing_max_latency' entry\n");
1671 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
1672 &tracing_thresh, &tracing_max_lat_fops);
1674 pr_warning("Could not create debugfs "
1675 "'tracing_threash' entry\n");
1676 entry = debugfs_create_file("README", 0644, d_tracer,
1677 NULL, &tracing_readme_fops);
1679 pr_warning("Could not create debugfs 'README' entry\n");
1682 #ifdef CONFIG_DYNAMIC_FTRACE
1683 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
1684 &ftrace_update_tot_cnt,
1685 &tracing_read_long_fops);
1687 pr_warning("Could not create debugfs "
1688 "'dyn_ftrace_total_info' entry\n");
1692 /* dummy trace to disable tracing */
1693 static struct tracer no_tracer __read_mostly =
1698 static int trace_alloc_page(void)
1700 struct trace_array_cpu *data;
1701 struct page *page, *tmp;
1706 /* first allocate a page for each CPU */
1707 for_each_possible_cpu(i) {
1708 array = (void *)__get_free_page(GFP_KERNEL);
1709 if (array == NULL) {
1710 printk(KERN_ERR "tracer: failed to allocate page"
1711 "for trace buffer!\n");
1715 page = virt_to_page(array);
1716 list_add(&page->lru, &pages);
1718 /* Only allocate if we are actually using the max trace */
1719 #ifdef CONFIG_TRACER_MAX_TRACE
1720 array = (void *)__get_free_page(GFP_KERNEL);
1721 if (array == NULL) {
1722 printk(KERN_ERR "tracer: failed to allocate page"
1723 "for trace buffer!\n");
1726 page = virt_to_page(array);
1727 list_add(&page->lru, &pages);
1731 /* Now that we successfully allocate a page per CPU, add them */
1732 for_each_possible_cpu(i) {
1733 data = global_trace.data[i];
1734 page = list_entry(pages.next, struct page, lru);
1735 list_del_init(&page->lru);
1736 list_add_tail(&page->lru, &data->trace_pages);
1739 #ifdef CONFIG_TRACER_MAX_TRACE
1740 data = max_tr.data[i];
1741 page = list_entry(pages.next, struct page, lru);
1742 list_del_init(&page->lru);
1743 list_add_tail(&page->lru, &data->trace_pages);
1747 global_trace.entries += ENTRIES_PER_PAGE;
1752 list_for_each_entry_safe(page, tmp, &pages, lru) {
1753 list_del_init(&page->lru);
1759 __init static int tracer_alloc_buffers(void)
1761 struct trace_array_cpu *data;
1768 /* Allocate the first page for all buffers */
1769 for_each_possible_cpu(i) {
1770 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
1771 max_tr.data[i] = &per_cpu(max_data, i);
1773 array = (void *)__get_free_page(GFP_KERNEL);
1774 if (array == NULL) {
1775 printk(KERN_ERR "tracer: failed to allocate page"
1776 "for trace buffer!\n");
1780 /* set the array to the list */
1781 INIT_LIST_HEAD(&data->trace_pages);
1782 page = virt_to_page(array);
1783 list_add(&page->lru, &data->trace_pages);
1784 /* use the LRU flag to differentiate the two buffers */
1787 /* Only allocate if we are actually using the max trace */
1788 #ifdef CONFIG_TRACER_MAX_TRACE
1789 array = (void *)__get_free_page(GFP_KERNEL);
1790 if (array == NULL) {
1791 printk(KERN_ERR "tracer: failed to allocate page"
1792 "for trace buffer!\n");
1796 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
1797 page = virt_to_page(array);
1798 list_add(&page->lru, &max_tr.data[i]->trace_pages);
1804 * Since we allocate by orders of pages, we may be able to
1807 global_trace.entries = ENTRIES_PER_PAGE;
1810 while (global_trace.entries < trace_nr_entries) {
1811 if (trace_alloc_page())
1815 max_tr.entries = global_trace.entries;
1817 pr_info("tracer: %d pages allocated for %ld",
1818 pages, trace_nr_entries);
1819 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
1820 pr_info(" actual entries %ld\n", global_trace.entries);
1822 tracer_init_debugfs();
1824 trace_init_cmdlines();
1826 register_tracer(&no_tracer);
1827 current_trace = &no_tracer;
1829 /* All seems OK, enable tracing */
1830 tracing_disabled = 0;
1835 for (i-- ; i >= 0; i--) {
1836 struct page *page, *tmp;
1837 struct trace_array_cpu *data = global_trace.data[i];
1840 list_for_each_entry_safe(page, tmp,
1841 &data->trace_pages, lru) {
1842 list_del_init(&page->lru);
1847 #ifdef CONFIG_TRACER_MAX_TRACE
1848 data = max_tr.data[i];
1850 list_for_each_entry_safe(page, tmp,
1851 &data->trace_pages, lru) {
1852 list_del_init(&page->lru);
1861 fs_initcall(tracer_alloc_buffers);