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 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
180 sizeof(struct trace_array_cpu) -
181 offsetof(struct trace_array_cpu, trace_head_idx));
185 list_splice_init(&tr1->trace_pages, &flip_pages);
186 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
187 list_splice_init(&flip_pages, &tr2->trace_pages);
188 BUG_ON(!list_empty(&flip_pages));
194 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
196 struct trace_array_cpu *data;
199 WARN_ON_ONCE(!irqs_disabled());
200 spin_lock(&ftrace_max_lock);
201 /* clear out all the previous traces */
202 for_each_possible_cpu(i) {
204 flip_trace(max_tr.data[i], data);
208 __update_max_tr(tr, tsk, cpu);
209 spin_unlock(&ftrace_max_lock);
213 * update_max_tr_single - only copy one trace over, and reset the rest
215 * @tsk - task with the latency
216 * @cpu - the cpu of the buffer to copy.
219 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
221 struct trace_array_cpu *data = tr->data[cpu];
224 WARN_ON_ONCE(!irqs_disabled());
225 spin_lock(&ftrace_max_lock);
226 for_each_possible_cpu(i)
227 tracing_reset(max_tr.data[i]);
229 flip_trace(max_tr.data[cpu], data);
232 __update_max_tr(tr, tsk, cpu);
233 spin_unlock(&ftrace_max_lock);
236 int register_tracer(struct tracer *type)
243 pr_info("Tracer must have a name\n");
247 mutex_lock(&trace_types_lock);
248 for (t = trace_types; t; t = t->next) {
249 if (strcmp(type->name, t->name) == 0) {
251 pr_info("Trace %s already registered\n",
258 #ifdef CONFIG_FTRACE_STARTUP_TEST
259 if (type->selftest) {
260 struct tracer *saved_tracer = current_trace;
261 struct trace_array_cpu *data;
262 struct trace_array *tr = &global_trace;
263 int saved_ctrl = tr->ctrl;
266 * Run a selftest on this tracer.
267 * Here we reset the trace buffer, and set the current
268 * tracer to be this tracer. The tracer can then run some
269 * internal tracing to verify that everything is in order.
270 * If we fail, we do not register this tracer.
272 for_each_possible_cpu(i) {
274 if (!head_page(data))
278 current_trace = type;
280 /* the test is responsible for initializing and enabling */
281 pr_info("Testing tracer %s: ", type->name);
282 ret = type->selftest(type, tr);
283 /* the test is responsible for resetting too */
284 current_trace = saved_tracer;
285 tr->ctrl = saved_ctrl;
287 printk(KERN_CONT "FAILED!\n");
290 /* Only reset on passing, to avoid touching corrupted buffers */
291 for_each_possible_cpu(i) {
293 if (!head_page(data))
297 printk(KERN_CONT "PASSED\n");
301 type->next = trace_types;
303 len = strlen(type->name);
304 if (len > max_tracer_type_len)
305 max_tracer_type_len = len;
308 mutex_unlock(&trace_types_lock);
313 void unregister_tracer(struct tracer *type)
318 mutex_lock(&trace_types_lock);
319 for (t = &trace_types; *t; t = &(*t)->next) {
323 pr_info("Trace %s not registered\n", type->name);
328 if (strlen(type->name) != max_tracer_type_len)
331 max_tracer_type_len = 0;
332 for (t = &trace_types; *t; t = &(*t)->next) {
333 len = strlen((*t)->name);
334 if (len > max_tracer_type_len)
335 max_tracer_type_len = len;
338 mutex_unlock(&trace_types_lock);
341 notrace void tracing_reset(struct trace_array_cpu *data)
344 data->trace_head = data->trace_tail = head_page(data);
345 data->trace_head_idx = 0;
346 data->trace_tail_idx = 0;
351 function_trace_call(unsigned long ip, unsigned long parent_ip)
353 struct trace_array *tr = &global_trace;
354 struct trace_array_cpu *data;
359 if (unlikely(!tracer_enabled))
362 local_irq_save(flags);
363 cpu = raw_smp_processor_id();
364 data = tr->data[cpu];
365 disabled = atomic_inc_return(&data->disabled);
367 if (likely(disabled == 1))
368 ftrace(tr, data, ip, parent_ip, flags);
370 atomic_dec(&data->disabled);
371 local_irq_restore(flags);
374 static struct ftrace_ops trace_ops __read_mostly =
376 .func = function_trace_call,
380 notrace void tracing_start_function_trace(void)
382 register_ftrace_function(&trace_ops);
385 notrace void tracing_stop_function_trace(void)
387 unregister_ftrace_function(&trace_ops);
390 #define SAVED_CMDLINES 128
391 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
392 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
393 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
394 static int cmdline_idx;
395 static DEFINE_SPINLOCK(trace_cmdline_lock);
396 atomic_t trace_record_cmdline_disabled;
398 static void trace_init_cmdlines(void)
400 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
401 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
405 notrace void trace_stop_cmdline_recording(void);
407 static notrace void trace_save_cmdline(struct task_struct *tsk)
412 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
416 * It's not the end of the world if we don't get
417 * the lock, but we also don't want to spin
418 * nor do we want to disable interrupts,
419 * so if we miss here, then better luck next time.
421 if (!spin_trylock(&trace_cmdline_lock))
424 idx = map_pid_to_cmdline[tsk->pid];
425 if (idx >= SAVED_CMDLINES) {
426 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
428 map = map_cmdline_to_pid[idx];
429 if (map <= PID_MAX_DEFAULT)
430 map_pid_to_cmdline[map] = (unsigned)-1;
432 map_pid_to_cmdline[tsk->pid] = idx;
437 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
439 spin_unlock(&trace_cmdline_lock);
442 static notrace char *trace_find_cmdline(int pid)
444 char *cmdline = "<...>";
450 if (pid > PID_MAX_DEFAULT)
453 map = map_pid_to_cmdline[pid];
454 if (map >= SAVED_CMDLINES)
457 cmdline = saved_cmdlines[map];
463 notrace void tracing_record_cmdline(struct task_struct *tsk)
465 if (atomic_read(&trace_record_cmdline_disabled))
468 trace_save_cmdline(tsk);
471 static inline notrace struct list_head *
472 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
475 * Roundrobin - but skip the head (which is not a real page):
478 if (unlikely(next == &data->trace_pages))
480 BUG_ON(next == &data->trace_pages);
485 static inline notrace void *
486 trace_next_page(struct trace_array_cpu *data, void *addr)
488 struct list_head *next;
491 page = virt_to_page(addr);
493 next = trace_next_list(data, &page->lru);
494 page = list_entry(next, struct page, lru);
496 return page_address(page);
499 static inline notrace struct trace_entry *
500 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
502 unsigned long idx, idx_next;
503 struct trace_entry *entry;
506 idx = data->trace_head_idx;
509 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
511 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
513 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
514 data->trace_head = trace_next_page(data, data->trace_head);
518 if (data->trace_head == data->trace_tail &&
519 idx_next == data->trace_tail_idx) {
521 data->trace_tail_idx++;
522 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
524 trace_next_page(data, data->trace_tail);
525 data->trace_tail_idx = 0;
529 data->trace_head_idx = idx_next;
534 static inline notrace void
535 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
537 struct task_struct *tsk = current;
540 pc = preempt_count();
542 entry->idx = atomic_inc_return(&tracer_counter);
543 entry->preempt_count = pc & 0xff;
544 entry->pid = tsk->pid;
545 entry->t = now(raw_smp_processor_id());
546 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
547 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
548 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
549 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
553 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
554 unsigned long ip, unsigned long parent_ip, unsigned long flags)
556 struct trace_entry *entry;
558 entry = tracing_get_trace_entry(tr, data);
559 tracing_generic_entry_update(entry, flags);
560 entry->type = TRACE_FN;
562 entry->fn.parent_ip = parent_ip;
566 tracing_sched_switch_trace(struct trace_array *tr,
567 struct trace_array_cpu *data,
568 struct task_struct *prev, struct task_struct *next,
571 struct trace_entry *entry;
573 entry = tracing_get_trace_entry(tr, data);
574 tracing_generic_entry_update(entry, flags);
575 entry->type = TRACE_CTX;
576 entry->ctx.prev_pid = prev->pid;
577 entry->ctx.prev_prio = prev->prio;
578 entry->ctx.prev_state = prev->state;
579 entry->ctx.next_pid = next->pid;
580 entry->ctx.next_prio = next->prio;
583 enum trace_file_type {
584 TRACE_FILE_LAT_FMT = 1,
587 static struct trace_entry *
588 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
589 struct trace_iterator *iter, int cpu)
592 struct trace_entry *array;
594 if (iter->next_idx[cpu] >= tr->entries ||
595 iter->next_idx[cpu] >= data->trace_idx)
598 if (!iter->next_page[cpu]) {
599 /* Initialize the iterator for this cpu trace buffer */
600 WARN_ON(!data->trace_tail);
601 page = virt_to_page(data->trace_tail);
602 iter->next_page[cpu] = &page->lru;
603 iter->next_page_idx[cpu] = data->trace_tail_idx;
606 page = list_entry(iter->next_page[cpu], struct page, lru);
607 BUG_ON(&data->trace_pages == &page->lru);
609 array = page_address(page);
611 /* Still possible to catch up to the tail */
612 if (iter->next_idx[cpu] && array == data->trace_tail &&
613 iter->next_page_idx[cpu] == data->trace_tail_idx)
616 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
617 return &array[iter->next_page_idx[cpu]];
620 static struct notrace trace_entry *
621 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
623 struct trace_array *tr = iter->tr;
624 struct trace_entry *ent, *next = NULL;
628 for_each_possible_cpu(cpu) {
629 if (!head_page(tr->data[cpu]))
631 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
633 (!next || (long)(next->idx - ent->idx) > 0)) {
645 static void *find_next_entry_inc(struct trace_iterator *iter)
647 struct trace_entry *next;
650 next = find_next_entry(iter, &next_cpu);
654 iter->next_idx[next_cpu]++;
655 iter->next_page_idx[next_cpu]++;
657 if (iter->next_page_idx[next_cpu] >= ENTRIES_PER_PAGE) {
658 struct trace_array_cpu *data = iter->tr->data[next_cpu];
660 iter->next_page_idx[next_cpu] = 0;
661 iter->next_page[next_cpu] =
662 trace_next_list(data, iter->next_page[next_cpu]);
666 iter->prev_ent = iter->ent;
667 iter->prev_cpu = iter->cpu;
670 iter->cpu = next_cpu;
672 return next ? iter : NULL;
675 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
677 struct trace_iterator *iter = m->private;
678 void *last_ent = iter->ent;
684 /* can't go backwards */
689 ent = find_next_entry_inc(iter);
693 while (ent && iter->idx < i)
694 ent = find_next_entry_inc(iter);
698 if (last_ent && !ent)
699 seq_puts(m, "\n\nvim:ft=help\n");
704 static void *s_start(struct seq_file *m, loff_t *pos)
706 struct trace_iterator *iter = m->private;
711 mutex_lock(&trace_types_lock);
713 if (!current_trace || current_trace != iter->trace)
716 atomic_inc(&trace_record_cmdline_disabled);
718 /* let the tracer grab locks here if needed */
719 if (current_trace->start)
720 current_trace->start(iter);
722 if (*pos != iter->pos) {
726 iter->prev_ent = NULL;
729 for_each_possible_cpu(i) {
730 iter->next_idx[i] = 0;
731 iter->next_page[i] = NULL;
734 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
739 p = s_next(m, p, &l);
745 static void s_stop(struct seq_file *m, void *p)
747 struct trace_iterator *iter = m->private;
749 atomic_dec(&trace_record_cmdline_disabled);
751 /* let the tracer release locks here if needed */
752 if (current_trace && current_trace == iter->trace && iter->trace->stop)
753 iter->trace->stop(iter);
755 mutex_unlock(&trace_types_lock);
759 seq_print_sym_short(struct seq_file *m, const char *fmt, unsigned long address)
761 #ifdef CONFIG_KALLSYMS
762 char str[KSYM_SYMBOL_LEN];
764 kallsyms_lookup(address, NULL, NULL, NULL, str);
766 seq_printf(m, fmt, str);
771 seq_print_sym_offset(struct seq_file *m, const char *fmt, unsigned long address)
773 #ifdef CONFIG_KALLSYMS
774 char str[KSYM_SYMBOL_LEN];
776 sprint_symbol(str, address);
777 seq_printf(m, fmt, str);
782 # define IP_FMT "%08lx"
784 # define IP_FMT "%016lx"
788 seq_print_ip_sym(struct seq_file *m, unsigned long ip, unsigned long sym_flags)
795 if (sym_flags & TRACE_ITER_SYM_OFFSET)
796 seq_print_sym_offset(m, "%s", ip);
798 seq_print_sym_short(m, "%s", ip);
800 if (sym_flags & TRACE_ITER_SYM_ADDR)
801 seq_printf(m, " <" IP_FMT ">", ip);
804 static notrace void print_lat_help_header(struct seq_file *m)
806 seq_puts(m, "# _------=> CPU# \n");
807 seq_puts(m, "# / _-----=> irqs-off \n");
808 seq_puts(m, "# | / _----=> need-resched \n");
809 seq_puts(m, "# || / _---=> hardirq/softirq \n");
810 seq_puts(m, "# ||| / _--=> preempt-depth \n");
811 seq_puts(m, "# |||| / \n");
812 seq_puts(m, "# ||||| delay \n");
813 seq_puts(m, "# cmd pid ||||| time | caller \n");
814 seq_puts(m, "# \\ / ||||| \\ | / \n");
817 static notrace void print_func_help_header(struct seq_file *m)
819 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
820 seq_puts(m, "# | | | | |\n");
825 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
827 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
828 struct trace_array *tr = iter->tr;
829 struct trace_array_cpu *data = tr->data[tr->cpu];
830 struct tracer *type = current_trace;
831 unsigned long total = 0;
832 unsigned long entries = 0;
834 const char *name = "preemption";
839 for_each_possible_cpu(cpu) {
840 if (head_page(tr->data[cpu])) {
841 total += tr->data[cpu]->trace_idx;
842 if (tr->data[cpu]->trace_idx > tr->entries)
843 entries += tr->entries;
845 entries += tr->data[cpu]->trace_idx;
849 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
851 seq_puts(m, "-----------------------------------"
852 "---------------------------------\n");
853 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
854 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
855 nsecs_to_usecs(data->saved_latency),
859 #if defined(CONFIG_PREEMPT_NONE)
861 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
863 #elif defined(CONFIG_PREEMPT_DESKTOP)
868 /* These are reserved for later use */
871 seq_printf(m, " #P:%d)\n", num_online_cpus());
875 seq_puts(m, " -----------------\n");
876 seq_printf(m, " | task: %.16s-%d "
877 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
878 data->comm, data->pid, data->uid, data->nice,
879 data->policy, data->rt_priority);
880 seq_puts(m, " -----------------\n");
882 if (data->critical_start) {
883 seq_puts(m, " => started at: ");
884 seq_print_ip_sym(m, data->critical_start, sym_flags);
885 seq_puts(m, "\n => ended at: ");
886 seq_print_ip_sym(m, data->critical_end, sym_flags);
894 lat_print_generic(struct seq_file *m, struct trace_entry *entry, int cpu)
896 int hardirq, softirq;
899 comm = trace_find_cmdline(entry->pid);
901 seq_printf(m, "%8.8s-%-5d ", comm, entry->pid);
902 seq_printf(m, "%d", cpu);
903 seq_printf(m, "%c%c",
904 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
905 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
907 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
908 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
909 if (hardirq && softirq)
922 if (entry->preempt_count)
923 seq_printf(m, "%x", entry->preempt_count);
928 unsigned long preempt_mark_thresh = 100;
931 lat_print_timestamp(struct seq_file *m, unsigned long long abs_usecs,
932 unsigned long rel_usecs)
934 seq_printf(m, " %4lldus", abs_usecs);
935 if (rel_usecs > preempt_mark_thresh)
937 else if (rel_usecs > 1)
943 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
946 print_lat_fmt(struct seq_file *m, struct trace_iterator *iter,
947 unsigned int trace_idx, int cpu)
949 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
950 struct trace_entry *next_entry = find_next_entry(iter, NULL);
951 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
952 struct trace_entry *entry = iter->ent;
953 unsigned long abs_usecs;
954 unsigned long rel_usecs;
960 rel_usecs = ns2usecs(next_entry->t - entry->t);
961 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
964 comm = trace_find_cmdline(entry->pid);
965 seq_printf(m, "%16s %5d %d %d %08x %08x [%08lx]"
966 " %ld.%03ldms (+%ld.%03ldms): ",
968 entry->pid, cpu, entry->flags,
969 entry->preempt_count, trace_idx,
972 abs_usecs % 1000, rel_usecs/1000, rel_usecs % 1000);
974 lat_print_generic(m, entry, cpu);
975 lat_print_timestamp(m, abs_usecs, rel_usecs);
977 switch (entry->type) {
979 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
981 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
985 S = entry->ctx.prev_state < sizeof(state_to_char) ?
986 state_to_char[entry->ctx.prev_state] : 'X';
987 comm = trace_find_cmdline(entry->ctx.next_pid);
988 seq_printf(m, " %d:%d:%c --> %d:%d %s\n",
990 entry->ctx.prev_prio,
993 entry->ctx.next_prio,
997 seq_printf(m, "Unknown type %d\n", entry->type);
1001 static notrace void sync_time_offset(struct trace_iterator *iter)
1003 struct trace_array_cpu *prev_array, *array;
1004 struct trace_entry *prev_entry, *entry;
1008 prev_entry = iter->prev_ent;
1012 prev_array = iter->tr->data[iter->prev_cpu];
1013 array = iter->tr->data[iter->cpu];
1015 prev_t = prev_entry->t + prev_array->time_offset;
1016 t = entry->t + array->time_offset;
1019 * If time goes backwards we increase the offset of
1020 * the current array, to not have observable time warps.
1021 * This will quickly synchronize the time offsets of
1025 array->time_offset += prev_t - t;
1029 print_trace_fmt(struct seq_file *m, struct trace_iterator *iter)
1031 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1032 struct trace_entry *entry;
1033 unsigned long usec_rem;
1034 unsigned long long t;
1039 sync_time_offset(iter);
1042 comm = trace_find_cmdline(iter->ent->pid);
1044 t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1045 usec_rem = do_div(t, 1000000ULL);
1046 secs = (unsigned long)t;
1048 seq_printf(m, "%16s-%-5d ", comm, entry->pid);
1049 seq_printf(m, "[%02d] ", iter->cpu);
1050 seq_printf(m, "%5lu.%06lu: ", secs, usec_rem);
1052 switch (entry->type) {
1054 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
1055 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1056 entry->fn.parent_ip) {
1057 seq_printf(m, " <-");
1058 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
1060 seq_printf(m, "\n");
1063 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1064 state_to_char[entry->ctx.prev_state] : 'X';
1065 seq_printf(m, " %d:%d:%c ==> %d:%d\n",
1066 entry->ctx.prev_pid,
1067 entry->ctx.prev_prio,
1069 entry->ctx.next_pid,
1070 entry->ctx.next_prio);
1075 static int trace_empty(struct trace_iterator *iter)
1077 struct trace_array_cpu *data;
1080 for_each_possible_cpu(cpu) {
1081 data = iter->tr->data[cpu];
1083 if (head_page(data) && data->trace_idx)
1089 static int s_show(struct seq_file *m, void *v)
1091 struct trace_iterator *iter = v;
1093 if (iter->ent == NULL) {
1095 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1098 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1099 /* print nothing if the buffers are empty */
1100 if (trace_empty(iter))
1102 print_trace_header(m, iter);
1103 if (!(trace_flags & TRACE_ITER_VERBOSE))
1104 print_lat_help_header(m);
1106 if (!(trace_flags & TRACE_ITER_VERBOSE))
1107 print_func_help_header(m);
1110 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1111 print_lat_fmt(m, iter, iter->idx, iter->cpu);
1113 print_trace_fmt(m, iter);
1119 static struct seq_operations tracer_seq_ops = {
1126 static struct trace_iterator notrace *
1127 __tracing_open(struct inode *inode, struct file *file, int *ret)
1129 struct trace_iterator *iter;
1131 if (tracing_disabled) {
1136 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1142 mutex_lock(&trace_types_lock);
1143 if (current_trace && current_trace->print_max)
1146 iter->tr = inode->i_private;
1147 iter->trace = current_trace;
1150 /* TODO stop tracer */
1151 *ret = seq_open(file, &tracer_seq_ops);
1153 struct seq_file *m = file->private_data;
1156 /* stop the trace while dumping */
1160 if (iter->trace && iter->trace->open)
1161 iter->trace->open(iter);
1166 mutex_unlock(&trace_types_lock);
1172 int tracing_open_generic(struct inode *inode, struct file *filp)
1174 if (tracing_disabled)
1177 filp->private_data = inode->i_private;
1181 int tracing_release(struct inode *inode, struct file *file)
1183 struct seq_file *m = (struct seq_file *)file->private_data;
1184 struct trace_iterator *iter = m->private;
1186 mutex_lock(&trace_types_lock);
1187 if (iter->trace && iter->trace->close)
1188 iter->trace->close(iter);
1190 /* reenable tracing if it was previously enabled */
1193 mutex_unlock(&trace_types_lock);
1195 seq_release(inode, file);
1200 static int tracing_open(struct inode *inode, struct file *file)
1204 __tracing_open(inode, file, &ret);
1209 static int tracing_lt_open(struct inode *inode, struct file *file)
1211 struct trace_iterator *iter;
1214 iter = __tracing_open(inode, file, &ret);
1217 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1223 static notrace void *
1224 t_next(struct seq_file *m, void *v, loff_t *pos)
1226 struct tracer *t = m->private;
1238 static void *t_start(struct seq_file *m, loff_t *pos)
1240 struct tracer *t = m->private;
1243 mutex_lock(&trace_types_lock);
1244 for (; t && l < *pos; t = t_next(m, t, &l))
1250 static void t_stop(struct seq_file *m, void *p)
1252 mutex_unlock(&trace_types_lock);
1255 static int t_show(struct seq_file *m, void *v)
1257 struct tracer *t = v;
1262 seq_printf(m, "%s", t->name);
1271 static struct seq_operations show_traces_seq_ops = {
1278 static int show_traces_open(struct inode *inode, struct file *file)
1282 if (tracing_disabled)
1285 ret = seq_open(file, &show_traces_seq_ops);
1287 struct seq_file *m = file->private_data;
1288 m->private = trace_types;
1294 static struct file_operations tracing_fops = {
1295 .open = tracing_open,
1297 .llseek = seq_lseek,
1298 .release = tracing_release,
1301 static struct file_operations tracing_lt_fops = {
1302 .open = tracing_lt_open,
1304 .llseek = seq_lseek,
1305 .release = tracing_release,
1308 static struct file_operations show_traces_fops = {
1309 .open = show_traces_open,
1311 .release = seq_release,
1315 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1316 size_t cnt, loff_t *ppos)
1323 /* calulate max size */
1324 for (i = 0; trace_options[i]; i++) {
1325 len += strlen(trace_options[i]);
1326 len += 3; /* "no" and space */
1329 /* +2 for \n and \0 */
1330 buf = kmalloc(len + 2, GFP_KERNEL);
1334 for (i = 0; trace_options[i]; i++) {
1335 if (trace_flags & (1 << i))
1336 r += sprintf(buf + r, "%s ", trace_options[i]);
1338 r += sprintf(buf + r, "no%s ", trace_options[i]);
1341 r += sprintf(buf + r, "\n");
1342 WARN_ON(r >= len + 2);
1344 r = simple_read_from_buffer(ubuf, cnt, ppos,
1353 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1354 size_t cnt, loff_t *ppos)
1364 if (copy_from_user(&buf, ubuf, cnt))
1369 if (strncmp(buf, "no", 2) == 0) {
1374 for (i = 0; trace_options[i]; i++) {
1375 int len = strlen(trace_options[i]);
1377 if (strncmp(cmp, trace_options[i], len) == 0) {
1379 trace_flags &= ~(1 << i);
1381 trace_flags |= (1 << i);
1391 static struct file_operations tracing_iter_fops = {
1392 .open = tracing_open_generic,
1393 .read = tracing_iter_ctrl_read,
1394 .write = tracing_iter_ctrl_write,
1397 static const char readme_msg[] =
1398 "tracing mini-HOWTO:\n\n"
1400 "# mount -t debugfs nodev /debug\n\n"
1401 "# cat /debug/tracing/available_tracers\n"
1402 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1403 "# cat /debug/tracing/current_tracer\n"
1405 "# echo sched_switch > /debug/tracing/current_tracer\n"
1406 "# cat /debug/tracing/current_tracer\n"
1408 "# cat /debug/tracing/iter_ctrl\n"
1409 "noprint-parent nosym-offset nosym-addr noverbose\n"
1410 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1411 "# echo 1 > /debug/tracing/tracing_enabled\n"
1412 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1413 "echo 0 > /debug/tracing/tracing_enabled\n"
1417 tracing_readme_read(struct file *filp, char __user *ubuf,
1418 size_t cnt, loff_t *ppos)
1420 return simple_read_from_buffer(ubuf, cnt, ppos,
1421 readme_msg, strlen(readme_msg));
1424 static struct file_operations tracing_readme_fops = {
1425 .open = tracing_open_generic,
1426 .read = tracing_readme_read,
1431 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1432 size_t cnt, loff_t *ppos)
1434 struct trace_array *tr = filp->private_data;
1438 r = sprintf(buf, "%ld\n", tr->ctrl);
1439 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1443 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1444 size_t cnt, loff_t *ppos)
1446 struct trace_array *tr = filp->private_data;
1453 if (copy_from_user(&buf, ubuf, cnt))
1458 val = simple_strtoul(buf, NULL, 10);
1462 mutex_lock(&trace_types_lock);
1463 if (tr->ctrl ^ val) {
1471 if (current_trace && current_trace->ctrl_update)
1472 current_trace->ctrl_update(tr);
1474 mutex_unlock(&trace_types_lock);
1482 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1483 size_t cnt, loff_t *ppos)
1485 char buf[max_tracer_type_len+2];
1488 mutex_lock(&trace_types_lock);
1490 r = sprintf(buf, "%s\n", current_trace->name);
1492 r = sprintf(buf, "\n");
1493 mutex_unlock(&trace_types_lock);
1495 return simple_read_from_buffer(ubuf, cnt, ppos,
1500 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1501 size_t cnt, loff_t *ppos)
1503 struct trace_array *tr = &global_trace;
1505 char buf[max_tracer_type_len+1];
1508 if (cnt > max_tracer_type_len)
1509 cnt = max_tracer_type_len;
1511 if (copy_from_user(&buf, ubuf, cnt))
1516 /* strip ending whitespace. */
1517 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1520 mutex_lock(&trace_types_lock);
1521 for (t = trace_types; t; t = t->next) {
1522 if (strcmp(t->name, buf) == 0)
1525 if (!t || t == current_trace)
1528 if (current_trace && current_trace->reset)
1529 current_trace->reset(tr);
1536 mutex_unlock(&trace_types_lock);
1544 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1545 size_t cnt, loff_t *ppos)
1547 unsigned long *ptr = filp->private_data;
1551 r = snprintf(buf, 64, "%ld\n",
1552 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1555 return simple_read_from_buffer(ubuf, cnt, ppos,
1560 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1561 size_t cnt, loff_t *ppos)
1563 long *ptr = filp->private_data;
1570 if (copy_from_user(&buf, ubuf, cnt))
1575 val = simple_strtoul(buf, NULL, 10);
1582 static struct file_operations tracing_max_lat_fops = {
1583 .open = tracing_open_generic,
1584 .read = tracing_max_lat_read,
1585 .write = tracing_max_lat_write,
1588 static struct file_operations tracing_ctrl_fops = {
1589 .open = tracing_open_generic,
1590 .read = tracing_ctrl_read,
1591 .write = tracing_ctrl_write,
1594 static struct file_operations set_tracer_fops = {
1595 .open = tracing_open_generic,
1596 .read = tracing_set_trace_read,
1597 .write = tracing_set_trace_write,
1600 #ifdef CONFIG_DYNAMIC_FTRACE
1603 tracing_read_long(struct file *filp, char __user *ubuf,
1604 size_t cnt, loff_t *ppos)
1606 unsigned long *p = filp->private_data;
1610 r = sprintf(buf, "%ld\n", *p);
1611 return simple_read_from_buffer(ubuf, cnt, ppos,
1615 static struct file_operations tracing_read_long_fops = {
1616 .open = tracing_open_generic,
1617 .read = tracing_read_long,
1621 static struct dentry *d_tracer;
1623 struct dentry *tracing_init_dentry(void)
1630 d_tracer = debugfs_create_dir("tracing", NULL);
1632 if (!d_tracer && !once) {
1634 pr_warning("Could not create debugfs directory 'tracing'\n");
1641 #ifdef CONFIG_FTRACE_SELFTEST
1642 /* Let selftest have access to static functions in this file */
1643 #include "trace_selftest.c"
1646 static __init void tracer_init_debugfs(void)
1648 struct dentry *d_tracer;
1649 struct dentry *entry;
1651 d_tracer = tracing_init_dentry();
1653 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1654 &global_trace, &tracing_ctrl_fops);
1656 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1658 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1659 NULL, &tracing_iter_fops);
1661 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1663 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1664 &global_trace, &tracing_lt_fops);
1666 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1668 entry = debugfs_create_file("trace", 0444, d_tracer,
1669 &global_trace, &tracing_fops);
1671 pr_warning("Could not create debugfs 'trace' entry\n");
1673 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
1674 &global_trace, &show_traces_fops);
1676 pr_warning("Could not create debugfs 'trace' entry\n");
1678 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
1679 &global_trace, &set_tracer_fops);
1681 pr_warning("Could not create debugfs 'trace' entry\n");
1683 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
1684 &tracing_max_latency,
1685 &tracing_max_lat_fops);
1687 pr_warning("Could not create debugfs "
1688 "'tracing_max_latency' entry\n");
1690 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
1691 &tracing_thresh, &tracing_max_lat_fops);
1693 pr_warning("Could not create debugfs "
1694 "'tracing_threash' entry\n");
1695 entry = debugfs_create_file("README", 0644, d_tracer,
1696 NULL, &tracing_readme_fops);
1698 pr_warning("Could not create debugfs 'README' entry\n");
1701 #ifdef CONFIG_DYNAMIC_FTRACE
1702 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
1703 &ftrace_update_tot_cnt,
1704 &tracing_read_long_fops);
1706 pr_warning("Could not create debugfs "
1707 "'dyn_ftrace_total_info' entry\n");
1711 /* dummy trace to disable tracing */
1712 static struct tracer no_tracer __read_mostly =
1717 static int trace_alloc_page(void)
1719 struct trace_array_cpu *data;
1720 struct page *page, *tmp;
1725 /* first allocate a page for each CPU */
1726 for_each_possible_cpu(i) {
1727 array = (void *)__get_free_page(GFP_KERNEL);
1728 if (array == NULL) {
1729 printk(KERN_ERR "tracer: failed to allocate page"
1730 "for trace buffer!\n");
1734 page = virt_to_page(array);
1735 list_add(&page->lru, &pages);
1737 /* Only allocate if we are actually using the max trace */
1738 #ifdef CONFIG_TRACER_MAX_TRACE
1739 array = (void *)__get_free_page(GFP_KERNEL);
1740 if (array == NULL) {
1741 printk(KERN_ERR "tracer: failed to allocate page"
1742 "for trace buffer!\n");
1745 page = virt_to_page(array);
1746 list_add(&page->lru, &pages);
1750 /* Now that we successfully allocate a page per CPU, add them */
1751 for_each_possible_cpu(i) {
1752 data = global_trace.data[i];
1753 page = list_entry(pages.next, struct page, lru);
1754 list_del_init(&page->lru);
1755 list_add_tail(&page->lru, &data->trace_pages);
1758 #ifdef CONFIG_TRACER_MAX_TRACE
1759 data = max_tr.data[i];
1760 page = list_entry(pages.next, struct page, lru);
1761 list_del_init(&page->lru);
1762 list_add_tail(&page->lru, &data->trace_pages);
1766 global_trace.entries += ENTRIES_PER_PAGE;
1771 list_for_each_entry_safe(page, tmp, &pages, lru) {
1772 list_del_init(&page->lru);
1778 __init static int tracer_alloc_buffers(void)
1780 struct trace_array_cpu *data;
1787 /* Allocate the first page for all buffers */
1788 for_each_possible_cpu(i) {
1789 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
1790 max_tr.data[i] = &per_cpu(max_data, i);
1792 array = (void *)__get_free_page(GFP_KERNEL);
1793 if (array == NULL) {
1794 printk(KERN_ERR "tracer: failed to allocate page"
1795 "for trace buffer!\n");
1799 /* set the array to the list */
1800 INIT_LIST_HEAD(&data->trace_pages);
1801 page = virt_to_page(array);
1802 list_add(&page->lru, &data->trace_pages);
1803 /* use the LRU flag to differentiate the two buffers */
1806 /* Only allocate if we are actually using the max trace */
1807 #ifdef CONFIG_TRACER_MAX_TRACE
1808 array = (void *)__get_free_page(GFP_KERNEL);
1809 if (array == NULL) {
1810 printk(KERN_ERR "tracer: failed to allocate page"
1811 "for trace buffer!\n");
1815 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
1816 page = virt_to_page(array);
1817 list_add(&page->lru, &max_tr.data[i]->trace_pages);
1823 * Since we allocate by orders of pages, we may be able to
1826 global_trace.entries = ENTRIES_PER_PAGE;
1829 while (global_trace.entries < trace_nr_entries) {
1830 if (trace_alloc_page())
1834 max_tr.entries = global_trace.entries;
1836 pr_info("tracer: %d pages allocated for %ld",
1837 pages, trace_nr_entries);
1838 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
1839 pr_info(" actual entries %ld\n", global_trace.entries);
1841 tracer_init_debugfs();
1843 trace_init_cmdlines();
1845 register_tracer(&no_tracer);
1846 current_trace = &no_tracer;
1848 /* All seems OK, enable tracing */
1849 tracing_disabled = 0;
1854 for (i-- ; i >= 0; i--) {
1855 struct page *page, *tmp;
1856 struct trace_array_cpu *data = global_trace.data[i];
1859 list_for_each_entry_safe(page, tmp,
1860 &data->trace_pages, lru) {
1861 list_del_init(&page->lru);
1866 #ifdef CONFIG_TRACER_MAX_TRACE
1867 data = max_tr.data[i];
1869 list_for_each_entry_safe(page, tmp,
1870 &data->trace_pages, lru) {
1871 list_del_init(&page->lru);
1880 fs_initcall(tracer_alloc_buffers);