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 printk(KERN_CONT "PASSED\n");
296 type->next = trace_types;
298 len = strlen(type->name);
299 if (len > max_tracer_type_len)
300 max_tracer_type_len = len;
303 mutex_unlock(&trace_types_lock);
308 void unregister_tracer(struct tracer *type)
313 mutex_lock(&trace_types_lock);
314 for (t = &trace_types; *t; t = &(*t)->next) {
318 pr_info("Trace %s not registered\n", type->name);
323 if (strlen(type->name) != max_tracer_type_len)
326 max_tracer_type_len = 0;
327 for (t = &trace_types; *t; t = &(*t)->next) {
328 len = strlen((*t)->name);
329 if (len > max_tracer_type_len)
330 max_tracer_type_len = len;
333 mutex_unlock(&trace_types_lock);
336 notrace void tracing_reset(struct trace_array_cpu *data)
339 data->trace_current = head_page(data);
340 data->trace_current_idx = 0;
341 data->time_offset = 0;
346 function_trace_call(unsigned long ip, unsigned long parent_ip)
348 struct trace_array *tr = &global_trace;
349 struct trace_array_cpu *data;
354 if (unlikely(!tracer_enabled))
357 local_irq_save(flags);
358 cpu = raw_smp_processor_id();
359 data = tr->data[cpu];
360 disabled = atomic_inc_return(&data->disabled);
362 if (likely(disabled == 1))
363 ftrace(tr, data, ip, parent_ip, flags);
365 atomic_dec(&data->disabled);
366 local_irq_restore(flags);
369 static struct ftrace_ops trace_ops __read_mostly =
371 .func = function_trace_call,
375 notrace void tracing_start_function_trace(void)
377 register_ftrace_function(&trace_ops);
380 notrace void tracing_stop_function_trace(void)
382 unregister_ftrace_function(&trace_ops);
385 #define SAVED_CMDLINES 128
386 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
387 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
388 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
389 static int cmdline_idx;
390 static DEFINE_SPINLOCK(trace_cmdline_lock);
391 atomic_t trace_record_cmdline_disabled;
393 static void trace_init_cmdlines(void)
395 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
396 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
400 notrace void trace_stop_cmdline_recording(void);
402 static notrace void trace_save_cmdline(struct task_struct *tsk)
407 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
411 * It's not the end of the world if we don't get
412 * the lock, but we also don't want to spin
413 * nor do we want to disable interrupts,
414 * so if we miss here, then better luck next time.
416 if (!spin_trylock(&trace_cmdline_lock))
419 idx = map_pid_to_cmdline[tsk->pid];
420 if (idx >= SAVED_CMDLINES) {
421 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
423 map = map_cmdline_to_pid[idx];
424 if (map <= PID_MAX_DEFAULT)
425 map_pid_to_cmdline[map] = (unsigned)-1;
427 map_pid_to_cmdline[tsk->pid] = idx;
432 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
434 spin_unlock(&trace_cmdline_lock);
437 static notrace char *trace_find_cmdline(int pid)
439 char *cmdline = "<...>";
445 if (pid > PID_MAX_DEFAULT)
448 map = map_pid_to_cmdline[pid];
449 if (map >= SAVED_CMDLINES)
452 cmdline = saved_cmdlines[map];
458 notrace void tracing_record_cmdline(struct task_struct *tsk)
460 if (atomic_read(&trace_record_cmdline_disabled))
463 trace_save_cmdline(tsk);
466 static inline notrace struct trace_entry *
467 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
469 unsigned long idx, idx_next;
470 struct trace_entry *entry;
471 struct list_head *next;
475 idx = data->trace_current_idx;
478 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
480 entry = data->trace_current + idx * TRACE_ENTRY_SIZE;
482 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
483 page = virt_to_page(data->trace_current);
485 * Roundrobin - but skip the head (which is not a real page):
487 next = page->lru.next;
488 if (unlikely(next == &data->trace_pages))
490 BUG_ON(next == &data->trace_pages);
492 page = list_entry(next, struct page, lru);
493 data->trace_current = page_address(page);
497 data->trace_current_idx = idx_next;
502 static inline notrace void
503 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
505 struct task_struct *tsk = current;
508 pc = preempt_count();
510 entry->idx = atomic_inc_return(&tracer_counter);
511 entry->preempt_count = pc & 0xff;
512 entry->pid = tsk->pid;
513 entry->t = now(raw_smp_processor_id());
514 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
515 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
516 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
517 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
521 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
522 unsigned long ip, unsigned long parent_ip, unsigned long flags)
524 struct trace_entry *entry;
526 entry = tracing_get_trace_entry(tr, data);
527 tracing_generic_entry_update(entry, flags);
528 entry->type = TRACE_FN;
530 entry->fn.parent_ip = parent_ip;
534 tracing_sched_switch_trace(struct trace_array *tr,
535 struct trace_array_cpu *data,
536 struct task_struct *prev, struct task_struct *next,
539 struct trace_entry *entry;
541 entry = tracing_get_trace_entry(tr, data);
542 tracing_generic_entry_update(entry, flags);
543 entry->type = TRACE_CTX;
544 entry->ctx.prev_pid = prev->pid;
545 entry->ctx.prev_prio = prev->prio;
546 entry->ctx.prev_state = prev->state;
547 entry->ctx.next_pid = next->pid;
548 entry->ctx.next_prio = next->prio;
551 enum trace_file_type {
552 TRACE_FILE_LAT_FMT = 1,
555 static struct trace_entry *
556 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
557 struct trace_iterator *iter, int cpu)
560 struct trace_entry *array;
562 if (iter->next_idx[cpu] >= tr->entries ||
563 iter->next_idx[cpu] >= data->trace_idx)
566 if (!iter->next_page[cpu]) {
568 * Initialize. If the count of elements in
569 * this buffer is greater than the max entries
570 * we had an underrun. Which means we looped around.
571 * We can simply use the current pointer as our
574 if (data->trace_idx >= tr->entries) {
575 page = virt_to_page(data->trace_current);
576 iter->next_page[cpu] = &page->lru;
577 iter->next_page_idx[cpu] = data->trace_current_idx;
579 iter->next_page[cpu] = data->trace_pages.next;
580 iter->next_page_idx[cpu] = 0;
584 page = list_entry(iter->next_page[cpu], struct page, lru);
585 BUG_ON(&data->trace_pages == &page->lru);
587 array = page_address(page);
589 return &array[iter->next_page_idx[cpu]];
592 static struct notrace trace_entry *
593 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
595 struct trace_array *tr = iter->tr;
596 struct trace_entry *ent, *next = NULL;
600 for_each_possible_cpu(cpu) {
601 if (!head_page(tr->data[cpu]))
603 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
605 (!next || (long)(next->idx - ent->idx) > 0)) {
617 static void *find_next_entry_inc(struct trace_iterator *iter)
619 struct trace_entry *next;
622 next = find_next_entry(iter, &next_cpu);
626 iter->next_idx[next_cpu]++;
627 iter->next_page_idx[next_cpu]++;
629 if (iter->next_page_idx[next_cpu] >= ENTRIES_PER_PAGE) {
630 struct trace_array_cpu *data = iter->tr->data[next_cpu];
632 iter->next_page_idx[next_cpu] = 0;
633 iter->next_page[next_cpu] =
634 iter->next_page[next_cpu]->next;
635 if (iter->next_page[next_cpu] == &data->trace_pages)
636 iter->next_page[next_cpu] =
637 data->trace_pages.next;
640 iter->prev_ent = iter->ent;
641 iter->prev_cpu = iter->cpu;
644 iter->cpu = next_cpu;
646 return next ? iter : NULL;
649 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
651 struct trace_iterator *iter = m->private;
652 void *last_ent = iter->ent;
658 /* can't go backwards */
663 ent = find_next_entry_inc(iter);
667 while (ent && iter->idx < i)
668 ent = find_next_entry_inc(iter);
672 if (last_ent && !ent)
673 seq_puts(m, "\n\nvim:ft=help\n");
678 static void *s_start(struct seq_file *m, loff_t *pos)
680 struct trace_iterator *iter = m->private;
685 mutex_lock(&trace_types_lock);
687 if (!current_trace || current_trace != iter->trace)
690 atomic_inc(&trace_record_cmdline_disabled);
692 /* let the tracer grab locks here if needed */
693 if (current_trace->start)
694 current_trace->start(iter);
696 if (*pos != iter->pos) {
700 iter->prev_ent = NULL;
703 for_each_possible_cpu(i) {
704 iter->next_idx[i] = 0;
705 iter->next_page[i] = NULL;
708 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
713 p = s_next(m, p, &l);
719 static void s_stop(struct seq_file *m, void *p)
721 struct trace_iterator *iter = m->private;
723 atomic_dec(&trace_record_cmdline_disabled);
725 /* let the tracer release locks here if needed */
726 if (current_trace && current_trace == iter->trace && iter->trace->stop)
727 iter->trace->stop(iter);
729 mutex_unlock(&trace_types_lock);
733 seq_print_sym_short(struct seq_file *m, const char *fmt, unsigned long address)
735 #ifdef CONFIG_KALLSYMS
736 char str[KSYM_SYMBOL_LEN];
738 kallsyms_lookup(address, NULL, NULL, NULL, str);
740 seq_printf(m, fmt, str);
745 seq_print_sym_offset(struct seq_file *m, const char *fmt, unsigned long address)
747 #ifdef CONFIG_KALLSYMS
748 char str[KSYM_SYMBOL_LEN];
750 sprint_symbol(str, address);
751 seq_printf(m, fmt, str);
756 # define IP_FMT "%08lx"
758 # define IP_FMT "%016lx"
762 seq_print_ip_sym(struct seq_file *m, unsigned long ip, unsigned long sym_flags)
769 if (sym_flags & TRACE_ITER_SYM_OFFSET)
770 seq_print_sym_offset(m, "%s", ip);
772 seq_print_sym_short(m, "%s", ip);
774 if (sym_flags & TRACE_ITER_SYM_ADDR)
775 seq_printf(m, " <" IP_FMT ">", ip);
778 static notrace void print_lat_help_header(struct seq_file *m)
780 seq_puts(m, "# _------=> CPU# \n");
781 seq_puts(m, "# / _-----=> irqs-off \n");
782 seq_puts(m, "# | / _----=> need-resched \n");
783 seq_puts(m, "# || / _---=> hardirq/softirq \n");
784 seq_puts(m, "# ||| / _--=> preempt-depth \n");
785 seq_puts(m, "# |||| / \n");
786 seq_puts(m, "# ||||| delay \n");
787 seq_puts(m, "# cmd pid ||||| time | caller \n");
788 seq_puts(m, "# \\ / ||||| \\ | / \n");
791 static notrace void print_func_help_header(struct seq_file *m)
793 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
794 seq_puts(m, "# | | | | |\n");
799 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
801 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
802 struct trace_array *tr = iter->tr;
803 struct trace_array_cpu *data = tr->data[tr->cpu];
804 struct tracer *type = current_trace;
805 unsigned long total = 0;
806 unsigned long entries = 0;
808 const char *name = "preemption";
813 for_each_possible_cpu(cpu) {
814 if (head_page(tr->data[cpu])) {
815 total += tr->data[cpu]->trace_idx;
816 if (tr->data[cpu]->trace_idx > tr->entries)
817 entries += tr->entries;
819 entries += tr->data[cpu]->trace_idx;
823 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
825 seq_puts(m, "-----------------------------------"
826 "---------------------------------\n");
827 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
828 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
829 nsecs_to_usecs(data->saved_latency),
833 #if defined(CONFIG_PREEMPT_NONE)
835 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
837 #elif defined(CONFIG_PREEMPT_DESKTOP)
842 /* These are reserved for later use */
845 seq_printf(m, " #P:%d)\n", num_online_cpus());
849 seq_puts(m, " -----------------\n");
850 seq_printf(m, " | task: %.16s-%d "
851 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
852 data->comm, data->pid, data->uid, data->nice,
853 data->policy, data->rt_priority);
854 seq_puts(m, " -----------------\n");
856 if (data->critical_start) {
857 seq_puts(m, " => started at: ");
858 seq_print_ip_sym(m, data->critical_start, sym_flags);
859 seq_puts(m, "\n => ended at: ");
860 seq_print_ip_sym(m, data->critical_end, sym_flags);
868 lat_print_generic(struct seq_file *m, struct trace_entry *entry, int cpu)
870 int hardirq, softirq;
873 comm = trace_find_cmdline(entry->pid);
875 seq_printf(m, "%8.8s-%-5d ", comm, entry->pid);
876 seq_printf(m, "%d", cpu);
877 seq_printf(m, "%c%c",
878 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
879 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
881 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
882 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
883 if (hardirq && softirq)
896 if (entry->preempt_count)
897 seq_printf(m, "%x", entry->preempt_count);
902 unsigned long preempt_mark_thresh = 100;
905 lat_print_timestamp(struct seq_file *m, unsigned long long abs_usecs,
906 unsigned long rel_usecs)
908 seq_printf(m, " %4lldus", abs_usecs);
909 if (rel_usecs > preempt_mark_thresh)
911 else if (rel_usecs > 1)
917 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
920 print_lat_fmt(struct seq_file *m, struct trace_iterator *iter,
921 unsigned int trace_idx, int cpu)
923 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
924 struct trace_entry *next_entry = find_next_entry(iter, NULL);
925 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
926 struct trace_entry *entry = iter->ent;
927 unsigned long abs_usecs;
928 unsigned long rel_usecs;
934 rel_usecs = ns2usecs(next_entry->t - entry->t);
935 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
938 comm = trace_find_cmdline(entry->pid);
939 seq_printf(m, "%16s %5d %d %d %08x %08x [%08lx]"
940 " %ld.%03ldms (+%ld.%03ldms): ",
942 entry->pid, cpu, entry->flags,
943 entry->preempt_count, trace_idx,
946 abs_usecs % 1000, rel_usecs/1000, rel_usecs % 1000);
948 lat_print_generic(m, entry, cpu);
949 lat_print_timestamp(m, abs_usecs, rel_usecs);
951 switch (entry->type) {
953 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
955 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
959 S = entry->ctx.prev_state < sizeof(state_to_char) ?
960 state_to_char[entry->ctx.prev_state] : 'X';
961 comm = trace_find_cmdline(entry->ctx.next_pid);
962 seq_printf(m, " %d:%d:%c --> %d:%d %s\n",
964 entry->ctx.prev_prio,
967 entry->ctx.next_prio,
971 seq_printf(m, "Unknown type %d\n", entry->type);
975 static notrace void sync_time_offset(struct trace_iterator *iter)
977 struct trace_array_cpu *prev_array, *array;
978 struct trace_entry *prev_entry, *entry;
982 prev_entry = iter->prev_ent;
986 prev_array = iter->tr->data[iter->prev_cpu];
987 array = iter->tr->data[iter->cpu];
989 prev_t = prev_entry->t + prev_array->time_offset;
990 t = entry->t + array->time_offset;
993 * If time goes backwards we increase the offset of
994 * the current array, to not have observable time warps.
995 * This will quickly synchronize the time offsets of
999 array->time_offset += prev_t - t;
1003 print_trace_fmt(struct seq_file *m, struct trace_iterator *iter)
1005 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1006 struct trace_entry *entry;
1007 unsigned long usec_rem;
1008 unsigned long long t;
1013 sync_time_offset(iter);
1016 comm = trace_find_cmdline(iter->ent->pid);
1018 t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1019 usec_rem = do_div(t, 1000000ULL);
1020 secs = (unsigned long)t;
1022 seq_printf(m, "%16s-%-5d ", comm, entry->pid);
1023 seq_printf(m, "[%02d] ", iter->cpu);
1024 seq_printf(m, "%5lu.%06lu: ", secs, usec_rem);
1026 switch (entry->type) {
1028 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
1029 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1030 entry->fn.parent_ip) {
1031 seq_printf(m, " <-");
1032 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
1034 seq_printf(m, "\n");
1037 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1038 state_to_char[entry->ctx.prev_state] : 'X';
1039 seq_printf(m, " %d:%d:%c ==> %d:%d\n",
1040 entry->ctx.prev_pid,
1041 entry->ctx.prev_prio,
1043 entry->ctx.next_pid,
1044 entry->ctx.next_prio);
1049 static int trace_empty(struct trace_iterator *iter)
1051 struct trace_array_cpu *data;
1054 for_each_possible_cpu(cpu) {
1055 data = iter->tr->data[cpu];
1057 if (head_page(data) && data->trace_idx)
1063 static int s_show(struct seq_file *m, void *v)
1065 struct trace_iterator *iter = v;
1067 if (iter->ent == NULL) {
1069 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1072 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1073 /* print nothing if the buffers are empty */
1074 if (trace_empty(iter))
1076 print_trace_header(m, iter);
1077 if (!(trace_flags & TRACE_ITER_VERBOSE))
1078 print_lat_help_header(m);
1080 if (!(trace_flags & TRACE_ITER_VERBOSE))
1081 print_func_help_header(m);
1084 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1085 print_lat_fmt(m, iter, iter->idx, iter->cpu);
1087 print_trace_fmt(m, iter);
1093 static struct seq_operations tracer_seq_ops = {
1100 static struct trace_iterator notrace *
1101 __tracing_open(struct inode *inode, struct file *file, int *ret)
1103 struct trace_iterator *iter;
1105 if (tracing_disabled) {
1110 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1116 mutex_lock(&trace_types_lock);
1117 if (current_trace && current_trace->print_max)
1120 iter->tr = inode->i_private;
1121 iter->trace = current_trace;
1124 /* TODO stop tracer */
1125 *ret = seq_open(file, &tracer_seq_ops);
1127 struct seq_file *m = file->private_data;
1130 /* stop the trace while dumping */
1134 if (iter->trace && iter->trace->open)
1135 iter->trace->open(iter);
1140 mutex_unlock(&trace_types_lock);
1146 int tracing_open_generic(struct inode *inode, struct file *filp)
1148 if (tracing_disabled)
1151 filp->private_data = inode->i_private;
1155 int tracing_release(struct inode *inode, struct file *file)
1157 struct seq_file *m = (struct seq_file *)file->private_data;
1158 struct trace_iterator *iter = m->private;
1160 mutex_lock(&trace_types_lock);
1161 if (iter->trace && iter->trace->close)
1162 iter->trace->close(iter);
1164 /* reenable tracing if it was previously enabled */
1167 mutex_unlock(&trace_types_lock);
1169 seq_release(inode, file);
1174 static int tracing_open(struct inode *inode, struct file *file)
1178 __tracing_open(inode, file, &ret);
1183 static int tracing_lt_open(struct inode *inode, struct file *file)
1185 struct trace_iterator *iter;
1188 iter = __tracing_open(inode, file, &ret);
1191 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1197 static notrace void *
1198 t_next(struct seq_file *m, void *v, loff_t *pos)
1200 struct tracer *t = m->private;
1212 static void *t_start(struct seq_file *m, loff_t *pos)
1214 struct tracer *t = m->private;
1217 mutex_lock(&trace_types_lock);
1218 for (; t && l < *pos; t = t_next(m, t, &l))
1224 static void t_stop(struct seq_file *m, void *p)
1226 mutex_unlock(&trace_types_lock);
1229 static int t_show(struct seq_file *m, void *v)
1231 struct tracer *t = v;
1236 seq_printf(m, "%s", t->name);
1245 static struct seq_operations show_traces_seq_ops = {
1252 static int show_traces_open(struct inode *inode, struct file *file)
1256 if (tracing_disabled)
1259 ret = seq_open(file, &show_traces_seq_ops);
1261 struct seq_file *m = file->private_data;
1262 m->private = trace_types;
1268 static struct file_operations tracing_fops = {
1269 .open = tracing_open,
1271 .llseek = seq_lseek,
1272 .release = tracing_release,
1275 static struct file_operations tracing_lt_fops = {
1276 .open = tracing_lt_open,
1278 .llseek = seq_lseek,
1279 .release = tracing_release,
1282 static struct file_operations show_traces_fops = {
1283 .open = show_traces_open,
1285 .release = seq_release,
1289 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1290 size_t cnt, loff_t *ppos)
1297 /* calulate max size */
1298 for (i = 0; trace_options[i]; i++) {
1299 len += strlen(trace_options[i]);
1300 len += 3; /* "no" and space */
1303 /* +2 for \n and \0 */
1304 buf = kmalloc(len + 2, GFP_KERNEL);
1308 for (i = 0; trace_options[i]; i++) {
1309 if (trace_flags & (1 << i))
1310 r += sprintf(buf + r, "%s ", trace_options[i]);
1312 r += sprintf(buf + r, "no%s ", trace_options[i]);
1315 r += sprintf(buf + r, "\n");
1316 WARN_ON(r >= len + 2);
1318 r = simple_read_from_buffer(ubuf, cnt, ppos,
1327 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1328 size_t cnt, loff_t *ppos)
1338 if (copy_from_user(&buf, ubuf, cnt))
1343 if (strncmp(buf, "no", 2) == 0) {
1348 for (i = 0; trace_options[i]; i++) {
1349 int len = strlen(trace_options[i]);
1351 if (strncmp(cmp, trace_options[i], len) == 0) {
1353 trace_flags &= ~(1 << i);
1355 trace_flags |= (1 << i);
1365 static struct file_operations tracing_iter_fops = {
1366 .open = tracing_open_generic,
1367 .read = tracing_iter_ctrl_read,
1368 .write = tracing_iter_ctrl_write,
1371 static const char readme_msg[] =
1372 "tracing mini-HOWTO:\n\n"
1374 "# mount -t debugfs nodev /debug\n\n"
1375 "# cat /debug/tracing/available_tracers\n"
1376 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1377 "# cat /debug/tracing/current_tracer\n"
1379 "# echo sched_switch > /debug/tracing/current_tracer\n"
1380 "# cat /debug/tracing/current_tracer\n"
1382 "# cat /debug/tracing/iter_ctrl\n"
1383 "noprint-parent nosym-offset nosym-addr noverbose\n"
1384 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1385 "# echo 1 > /debug/tracing/tracing_enabled\n"
1386 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1387 "echo 0 > /debug/tracing/tracing_enabled\n"
1391 tracing_readme_read(struct file *filp, char __user *ubuf,
1392 size_t cnt, loff_t *ppos)
1394 return simple_read_from_buffer(ubuf, cnt, ppos,
1395 readme_msg, strlen(readme_msg));
1398 static struct file_operations tracing_readme_fops = {
1399 .open = tracing_open_generic,
1400 .read = tracing_readme_read,
1405 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1406 size_t cnt, loff_t *ppos)
1408 struct trace_array *tr = filp->private_data;
1412 r = sprintf(buf, "%ld\n", tr->ctrl);
1413 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1417 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1418 size_t cnt, loff_t *ppos)
1420 struct trace_array *tr = filp->private_data;
1427 if (copy_from_user(&buf, ubuf, cnt))
1432 val = simple_strtoul(buf, NULL, 10);
1436 mutex_lock(&trace_types_lock);
1437 if (tr->ctrl ^ val) {
1445 if (current_trace && current_trace->ctrl_update)
1446 current_trace->ctrl_update(tr);
1448 mutex_unlock(&trace_types_lock);
1456 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1457 size_t cnt, loff_t *ppos)
1459 char buf[max_tracer_type_len+2];
1462 mutex_lock(&trace_types_lock);
1464 r = sprintf(buf, "%s\n", current_trace->name);
1466 r = sprintf(buf, "\n");
1467 mutex_unlock(&trace_types_lock);
1469 return simple_read_from_buffer(ubuf, cnt, ppos,
1474 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1475 size_t cnt, loff_t *ppos)
1477 struct trace_array *tr = &global_trace;
1479 char buf[max_tracer_type_len+1];
1482 if (cnt > max_tracer_type_len)
1483 cnt = max_tracer_type_len;
1485 if (copy_from_user(&buf, ubuf, cnt))
1490 /* strip ending whitespace. */
1491 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1494 mutex_lock(&trace_types_lock);
1495 for (t = trace_types; t; t = t->next) {
1496 if (strcmp(t->name, buf) == 0)
1499 if (!t || t == current_trace)
1502 if (current_trace && current_trace->reset)
1503 current_trace->reset(tr);
1510 mutex_unlock(&trace_types_lock);
1518 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1519 size_t cnt, loff_t *ppos)
1521 unsigned long *ptr = filp->private_data;
1525 r = snprintf(buf, 64, "%ld\n",
1526 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1529 return simple_read_from_buffer(ubuf, cnt, ppos,
1534 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1535 size_t cnt, loff_t *ppos)
1537 long *ptr = filp->private_data;
1544 if (copy_from_user(&buf, ubuf, cnt))
1549 val = simple_strtoul(buf, NULL, 10);
1556 static struct file_operations tracing_max_lat_fops = {
1557 .open = tracing_open_generic,
1558 .read = tracing_max_lat_read,
1559 .write = tracing_max_lat_write,
1562 static struct file_operations tracing_ctrl_fops = {
1563 .open = tracing_open_generic,
1564 .read = tracing_ctrl_read,
1565 .write = tracing_ctrl_write,
1568 static struct file_operations set_tracer_fops = {
1569 .open = tracing_open_generic,
1570 .read = tracing_set_trace_read,
1571 .write = tracing_set_trace_write,
1574 #ifdef CONFIG_DYNAMIC_FTRACE
1577 tracing_read_long(struct file *filp, char __user *ubuf,
1578 size_t cnt, loff_t *ppos)
1580 unsigned long *p = filp->private_data;
1584 r = sprintf(buf, "%ld\n", *p);
1585 return simple_read_from_buffer(ubuf, cnt, ppos,
1589 static struct file_operations tracing_read_long_fops = {
1590 .open = tracing_open_generic,
1591 .read = tracing_read_long,
1595 static struct dentry *d_tracer;
1597 struct dentry *tracing_init_dentry(void)
1604 d_tracer = debugfs_create_dir("tracing", NULL);
1606 if (!d_tracer && !once) {
1608 pr_warning("Could not create debugfs directory 'tracing'\n");
1615 #ifdef CONFIG_FTRACE_SELFTEST
1616 /* Let selftest have access to static functions in this file */
1617 #include "trace_selftest.c"
1620 static __init void tracer_init_debugfs(void)
1622 struct dentry *d_tracer;
1623 struct dentry *entry;
1625 d_tracer = tracing_init_dentry();
1627 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1628 &global_trace, &tracing_ctrl_fops);
1630 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1632 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1633 NULL, &tracing_iter_fops);
1635 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1637 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1638 &global_trace, &tracing_lt_fops);
1640 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1642 entry = debugfs_create_file("trace", 0444, d_tracer,
1643 &global_trace, &tracing_fops);
1645 pr_warning("Could not create debugfs 'trace' entry\n");
1647 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
1648 &global_trace, &show_traces_fops);
1650 pr_warning("Could not create debugfs 'trace' entry\n");
1652 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
1653 &global_trace, &set_tracer_fops);
1655 pr_warning("Could not create debugfs 'trace' entry\n");
1657 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
1658 &tracing_max_latency,
1659 &tracing_max_lat_fops);
1661 pr_warning("Could not create debugfs "
1662 "'tracing_max_latency' entry\n");
1664 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
1665 &tracing_thresh, &tracing_max_lat_fops);
1667 pr_warning("Could not create debugfs "
1668 "'tracing_threash' entry\n");
1669 entry = debugfs_create_file("README", 0644, d_tracer,
1670 NULL, &tracing_readme_fops);
1672 pr_warning("Could not create debugfs 'README' entry\n");
1675 #ifdef CONFIG_DYNAMIC_FTRACE
1676 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
1677 &ftrace_update_tot_cnt,
1678 &tracing_read_long_fops);
1680 pr_warning("Could not create debugfs "
1681 "'dyn_ftrace_total_info' entry\n");
1685 /* dummy trace to disable tracing */
1686 static struct tracer no_tracer __read_mostly =
1691 static int trace_alloc_page(void)
1693 struct trace_array_cpu *data;
1694 struct page *page, *tmp;
1699 /* first allocate a page for each CPU */
1700 for_each_possible_cpu(i) {
1701 array = (void *)__get_free_page(GFP_KERNEL);
1702 if (array == NULL) {
1703 printk(KERN_ERR "tracer: failed to allocate page"
1704 "for trace buffer!\n");
1708 page = virt_to_page(array);
1709 list_add(&page->lru, &pages);
1711 /* Only allocate if we are actually using the max trace */
1712 #ifdef CONFIG_TRACER_MAX_TRACE
1713 array = (void *)__get_free_page(GFP_KERNEL);
1714 if (array == NULL) {
1715 printk(KERN_ERR "tracer: failed to allocate page"
1716 "for trace buffer!\n");
1719 page = virt_to_page(array);
1720 list_add(&page->lru, &pages);
1724 /* Now that we successfully allocate a page per CPU, add them */
1725 for_each_possible_cpu(i) {
1726 data = global_trace.data[i];
1727 page = list_entry(pages.next, struct page, lru);
1728 list_del_init(&page->lru);
1729 list_add_tail(&page->lru, &data->trace_pages);
1732 #ifdef CONFIG_TRACER_MAX_TRACE
1733 data = max_tr.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);
1740 global_trace.entries += ENTRIES_PER_PAGE;
1745 list_for_each_entry_safe(page, tmp, &pages, lru) {
1746 list_del_init(&page->lru);
1752 __init static int tracer_alloc_buffers(void)
1754 struct trace_array_cpu *data;
1761 /* Allocate the first page for all buffers */
1762 for_each_possible_cpu(i) {
1763 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
1764 max_tr.data[i] = &per_cpu(max_data, i);
1766 array = (void *)__get_free_page(GFP_KERNEL);
1767 if (array == NULL) {
1768 printk(KERN_ERR "tracer: failed to allocate page"
1769 "for trace buffer!\n");
1773 /* set the array to the list */
1774 INIT_LIST_HEAD(&data->trace_pages);
1775 page = virt_to_page(array);
1776 list_add(&page->lru, &data->trace_pages);
1777 /* use the LRU flag to differentiate the two buffers */
1780 /* Only allocate if we are actually using the max trace */
1781 #ifdef CONFIG_TRACER_MAX_TRACE
1782 array = (void *)__get_free_page(GFP_KERNEL);
1783 if (array == NULL) {
1784 printk(KERN_ERR "tracer: failed to allocate page"
1785 "for trace buffer!\n");
1789 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
1790 page = virt_to_page(array);
1791 list_add(&page->lru, &max_tr.data[i]->trace_pages);
1797 * Since we allocate by orders of pages, we may be able to
1800 global_trace.entries = ENTRIES_PER_PAGE;
1803 while (global_trace.entries < trace_nr_entries) {
1804 if (trace_alloc_page())
1808 max_tr.entries = global_trace.entries;
1810 pr_info("tracer: %d pages allocated for %ld",
1811 pages, trace_nr_entries);
1812 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
1813 pr_info(" actual entries %ld\n", global_trace.entries);
1815 tracer_init_debugfs();
1817 trace_init_cmdlines();
1819 register_tracer(&no_tracer);
1820 current_trace = &no_tracer;
1822 /* All seems OK, enable tracing */
1823 tracing_disabled = 0;
1828 for (i-- ; i >= 0; i--) {
1829 struct page *page, *tmp;
1830 struct trace_array_cpu *data = global_trace.data[i];
1833 list_for_each_entry_safe(page, tmp,
1834 &data->trace_pages, lru) {
1835 list_del_init(&page->lru);
1840 #ifdef CONFIG_TRACER_MAX_TRACE
1841 data = max_tr.data[i];
1843 list_for_each_entry_safe(page, tmp,
1844 &data->trace_pages, lru) {
1845 list_del_init(&page->lru);
1854 fs_initcall(tracer_alloc_buffers);