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 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
175 int len = (PAGE_SIZE - 1) - s->len;
182 len = vsnprintf(s->buffer + s->len, len, fmt, ap);
191 trace_seq_puts(struct trace_seq *s, const char *str)
193 int len = strlen(str);
195 if (len > ((PAGE_SIZE - 1) - s->len))
196 len = (PAGE_SIZE - 1) - s->len;
198 memcpy(s->buffer + s->len, str, len);
205 trace_seq_putc(struct trace_seq *s, unsigned char c)
207 if (s->len >= (PAGE_SIZE - 1))
210 s->buffer[s->len++] = c;
216 trace_seq_reset(struct trace_seq *s)
222 trace_print_seq(struct seq_file *m, struct trace_seq *s)
224 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
227 seq_puts(m, s->buffer);
233 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
235 struct list_head flip_pages;
237 INIT_LIST_HEAD(&flip_pages);
239 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
240 sizeof(struct trace_array_cpu) -
241 offsetof(struct trace_array_cpu, trace_head_idx));
245 list_splice_init(&tr1->trace_pages, &flip_pages);
246 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
247 list_splice_init(&flip_pages, &tr2->trace_pages);
248 BUG_ON(!list_empty(&flip_pages));
254 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
256 struct trace_array_cpu *data;
259 WARN_ON_ONCE(!irqs_disabled());
260 spin_lock(&ftrace_max_lock);
261 /* clear out all the previous traces */
262 for_each_possible_cpu(i) {
264 flip_trace(max_tr.data[i], data);
268 __update_max_tr(tr, tsk, cpu);
269 spin_unlock(&ftrace_max_lock);
273 * update_max_tr_single - only copy one trace over, and reset the rest
275 * @tsk - task with the latency
276 * @cpu - the cpu of the buffer to copy.
279 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
281 struct trace_array_cpu *data = tr->data[cpu];
284 WARN_ON_ONCE(!irqs_disabled());
285 spin_lock(&ftrace_max_lock);
286 for_each_possible_cpu(i)
287 tracing_reset(max_tr.data[i]);
289 flip_trace(max_tr.data[cpu], data);
292 __update_max_tr(tr, tsk, cpu);
293 spin_unlock(&ftrace_max_lock);
296 int register_tracer(struct tracer *type)
303 pr_info("Tracer must have a name\n");
307 mutex_lock(&trace_types_lock);
308 for (t = trace_types; t; t = t->next) {
309 if (strcmp(type->name, t->name) == 0) {
311 pr_info("Trace %s already registered\n",
318 #ifdef CONFIG_FTRACE_STARTUP_TEST
319 if (type->selftest) {
320 struct tracer *saved_tracer = current_trace;
321 struct trace_array_cpu *data;
322 struct trace_array *tr = &global_trace;
323 int saved_ctrl = tr->ctrl;
326 * Run a selftest on this tracer.
327 * Here we reset the trace buffer, and set the current
328 * tracer to be this tracer. The tracer can then run some
329 * internal tracing to verify that everything is in order.
330 * If we fail, we do not register this tracer.
332 for_each_possible_cpu(i) {
334 if (!head_page(data))
338 current_trace = type;
340 /* the test is responsible for initializing and enabling */
341 pr_info("Testing tracer %s: ", type->name);
342 ret = type->selftest(type, tr);
343 /* the test is responsible for resetting too */
344 current_trace = saved_tracer;
345 tr->ctrl = saved_ctrl;
347 printk(KERN_CONT "FAILED!\n");
350 /* Only reset on passing, to avoid touching corrupted buffers */
351 for_each_possible_cpu(i) {
353 if (!head_page(data))
357 printk(KERN_CONT "PASSED\n");
361 type->next = trace_types;
363 len = strlen(type->name);
364 if (len > max_tracer_type_len)
365 max_tracer_type_len = len;
368 mutex_unlock(&trace_types_lock);
373 void unregister_tracer(struct tracer *type)
378 mutex_lock(&trace_types_lock);
379 for (t = &trace_types; *t; t = &(*t)->next) {
383 pr_info("Trace %s not registered\n", type->name);
388 if (strlen(type->name) != max_tracer_type_len)
391 max_tracer_type_len = 0;
392 for (t = &trace_types; *t; t = &(*t)->next) {
393 len = strlen((*t)->name);
394 if (len > max_tracer_type_len)
395 max_tracer_type_len = len;
398 mutex_unlock(&trace_types_lock);
401 notrace void tracing_reset(struct trace_array_cpu *data)
404 data->trace_head = data->trace_tail = head_page(data);
405 data->trace_head_idx = 0;
406 data->trace_tail_idx = 0;
411 function_trace_call(unsigned long ip, unsigned long parent_ip)
413 struct trace_array *tr = &global_trace;
414 struct trace_array_cpu *data;
419 if (unlikely(!tracer_enabled))
422 local_irq_save(flags);
423 cpu = raw_smp_processor_id();
424 data = tr->data[cpu];
425 disabled = atomic_inc_return(&data->disabled);
427 if (likely(disabled == 1))
428 ftrace(tr, data, ip, parent_ip, flags);
430 atomic_dec(&data->disabled);
431 local_irq_restore(flags);
434 static struct ftrace_ops trace_ops __read_mostly =
436 .func = function_trace_call,
440 notrace void tracing_start_function_trace(void)
442 register_ftrace_function(&trace_ops);
445 notrace void tracing_stop_function_trace(void)
447 unregister_ftrace_function(&trace_ops);
450 #define SAVED_CMDLINES 128
451 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
452 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
453 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
454 static int cmdline_idx;
455 static DEFINE_SPINLOCK(trace_cmdline_lock);
456 atomic_t trace_record_cmdline_disabled;
458 static void trace_init_cmdlines(void)
460 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
461 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
465 notrace void trace_stop_cmdline_recording(void);
467 static notrace void trace_save_cmdline(struct task_struct *tsk)
472 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
476 * It's not the end of the world if we don't get
477 * the lock, but we also don't want to spin
478 * nor do we want to disable interrupts,
479 * so if we miss here, then better luck next time.
481 if (!spin_trylock(&trace_cmdline_lock))
484 idx = map_pid_to_cmdline[tsk->pid];
485 if (idx >= SAVED_CMDLINES) {
486 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
488 map = map_cmdline_to_pid[idx];
489 if (map <= PID_MAX_DEFAULT)
490 map_pid_to_cmdline[map] = (unsigned)-1;
492 map_pid_to_cmdline[tsk->pid] = idx;
497 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
499 spin_unlock(&trace_cmdline_lock);
502 static notrace char *trace_find_cmdline(int pid)
504 char *cmdline = "<...>";
510 if (pid > PID_MAX_DEFAULT)
513 map = map_pid_to_cmdline[pid];
514 if (map >= SAVED_CMDLINES)
517 cmdline = saved_cmdlines[map];
523 notrace void tracing_record_cmdline(struct task_struct *tsk)
525 if (atomic_read(&trace_record_cmdline_disabled))
528 trace_save_cmdline(tsk);
531 static inline notrace struct list_head *
532 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
535 * Roundrobin - but skip the head (which is not a real page):
538 if (unlikely(next == &data->trace_pages))
540 BUG_ON(next == &data->trace_pages);
545 static inline notrace void *
546 trace_next_page(struct trace_array_cpu *data, void *addr)
548 struct list_head *next;
551 page = virt_to_page(addr);
553 next = trace_next_list(data, &page->lru);
554 page = list_entry(next, struct page, lru);
556 return page_address(page);
559 static inline notrace struct trace_entry *
560 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
562 unsigned long idx, idx_next;
563 struct trace_entry *entry;
566 idx = data->trace_head_idx;
569 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
571 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
573 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
574 data->trace_head = trace_next_page(data, data->trace_head);
578 if (data->trace_head == data->trace_tail &&
579 idx_next == data->trace_tail_idx) {
581 data->trace_tail_idx++;
582 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
584 trace_next_page(data, data->trace_tail);
585 data->trace_tail_idx = 0;
589 data->trace_head_idx = idx_next;
594 static inline notrace void
595 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
597 struct task_struct *tsk = current;
600 pc = preempt_count();
602 entry->idx = atomic_inc_return(&tracer_counter);
603 entry->preempt_count = pc & 0xff;
604 entry->pid = tsk->pid;
605 entry->t = now(raw_smp_processor_id());
606 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
607 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
608 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
609 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
613 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
614 unsigned long ip, unsigned long parent_ip, unsigned long flags)
616 struct trace_entry *entry;
618 entry = tracing_get_trace_entry(tr, data);
619 tracing_generic_entry_update(entry, flags);
620 entry->type = TRACE_FN;
622 entry->fn.parent_ip = parent_ip;
626 tracing_sched_switch_trace(struct trace_array *tr,
627 struct trace_array_cpu *data,
628 struct task_struct *prev, struct task_struct *next,
631 struct trace_entry *entry;
633 entry = tracing_get_trace_entry(tr, data);
634 tracing_generic_entry_update(entry, flags);
635 entry->type = TRACE_CTX;
636 entry->ctx.prev_pid = prev->pid;
637 entry->ctx.prev_prio = prev->prio;
638 entry->ctx.prev_state = prev->state;
639 entry->ctx.next_pid = next->pid;
640 entry->ctx.next_prio = next->prio;
643 enum trace_file_type {
644 TRACE_FILE_LAT_FMT = 1,
647 static struct trace_entry *
648 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
649 struct trace_iterator *iter, int cpu)
652 struct trace_entry *array;
654 if (iter->next_idx[cpu] >= tr->entries ||
655 iter->next_idx[cpu] >= data->trace_idx)
658 if (!iter->next_page[cpu]) {
659 /* Initialize the iterator for this cpu trace buffer */
660 WARN_ON(!data->trace_tail);
661 page = virt_to_page(data->trace_tail);
662 iter->next_page[cpu] = &page->lru;
663 iter->next_page_idx[cpu] = data->trace_tail_idx;
666 page = list_entry(iter->next_page[cpu], struct page, lru);
667 BUG_ON(&data->trace_pages == &page->lru);
669 array = page_address(page);
671 /* Still possible to catch up to the tail */
672 if (iter->next_idx[cpu] && array == data->trace_tail &&
673 iter->next_page_idx[cpu] == data->trace_tail_idx)
676 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
677 return &array[iter->next_page_idx[cpu]];
680 static struct notrace trace_entry *
681 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
683 struct trace_array *tr = iter->tr;
684 struct trace_entry *ent, *next = NULL;
688 for_each_possible_cpu(cpu) {
689 if (!head_page(tr->data[cpu]))
691 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
693 (!next || (long)(next->idx - ent->idx) > 0)) {
705 static void *find_next_entry_inc(struct trace_iterator *iter)
707 struct trace_entry *next;
710 next = find_next_entry(iter, &next_cpu);
714 iter->next_idx[next_cpu]++;
715 iter->next_page_idx[next_cpu]++;
717 if (iter->next_page_idx[next_cpu] >= ENTRIES_PER_PAGE) {
718 struct trace_array_cpu *data = iter->tr->data[next_cpu];
720 iter->next_page_idx[next_cpu] = 0;
721 iter->next_page[next_cpu] =
722 trace_next_list(data, iter->next_page[next_cpu]);
726 iter->prev_ent = iter->ent;
727 iter->prev_cpu = iter->cpu;
730 iter->cpu = next_cpu;
732 return next ? iter : NULL;
735 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
737 struct trace_iterator *iter = m->private;
738 void *last_ent = iter->ent;
744 /* can't go backwards */
749 ent = find_next_entry_inc(iter);
753 while (ent && iter->idx < i)
754 ent = find_next_entry_inc(iter);
758 if (last_ent && !ent)
759 seq_puts(m, "\n\nvim:ft=help\n");
764 static void *s_start(struct seq_file *m, loff_t *pos)
766 struct trace_iterator *iter = m->private;
771 mutex_lock(&trace_types_lock);
773 if (!current_trace || current_trace != iter->trace)
776 atomic_inc(&trace_record_cmdline_disabled);
778 /* let the tracer grab locks here if needed */
779 if (current_trace->start)
780 current_trace->start(iter);
782 if (*pos != iter->pos) {
786 iter->prev_ent = NULL;
789 for_each_possible_cpu(i) {
790 iter->next_idx[i] = 0;
791 iter->next_page[i] = NULL;
794 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
799 p = s_next(m, p, &l);
805 static void s_stop(struct seq_file *m, void *p)
807 struct trace_iterator *iter = m->private;
809 atomic_dec(&trace_record_cmdline_disabled);
811 /* let the tracer release locks here if needed */
812 if (current_trace && current_trace == iter->trace && iter->trace->stop)
813 iter->trace->stop(iter);
815 mutex_unlock(&trace_types_lock);
819 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
821 #ifdef CONFIG_KALLSYMS
822 char str[KSYM_SYMBOL_LEN];
824 kallsyms_lookup(address, NULL, NULL, NULL, str);
826 trace_seq_printf(s, fmt, str);
831 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
832 unsigned long address)
834 #ifdef CONFIG_KALLSYMS
835 char str[KSYM_SYMBOL_LEN];
837 sprint_symbol(str, address);
838 trace_seq_printf(s, fmt, str);
843 # define IP_FMT "%08lx"
845 # define IP_FMT "%016lx"
849 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
852 trace_seq_printf(s, "0");
856 if (sym_flags & TRACE_ITER_SYM_OFFSET)
857 seq_print_sym_offset(s, "%s", ip);
859 seq_print_sym_short(s, "%s", ip);
861 if (sym_flags & TRACE_ITER_SYM_ADDR)
862 trace_seq_printf(s, " <" IP_FMT ">", ip);
865 static notrace void print_lat_help_header(struct seq_file *m)
867 seq_puts(m, "# _------=> CPU# \n");
868 seq_puts(m, "# / _-----=> irqs-off \n");
869 seq_puts(m, "# | / _----=> need-resched \n");
870 seq_puts(m, "# || / _---=> hardirq/softirq \n");
871 seq_puts(m, "# ||| / _--=> preempt-depth \n");
872 seq_puts(m, "# |||| / \n");
873 seq_puts(m, "# ||||| delay \n");
874 seq_puts(m, "# cmd pid ||||| time | caller \n");
875 seq_puts(m, "# \\ / ||||| \\ | / \n");
878 static notrace void print_func_help_header(struct seq_file *m)
880 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
881 seq_puts(m, "# | | | | |\n");
886 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
888 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
889 struct trace_array *tr = iter->tr;
890 struct trace_array_cpu *data = tr->data[tr->cpu];
891 struct tracer *type = current_trace;
892 unsigned long total = 0;
893 unsigned long entries = 0;
895 const char *name = "preemption";
900 for_each_possible_cpu(cpu) {
901 if (head_page(tr->data[cpu])) {
902 total += tr->data[cpu]->trace_idx;
903 if (tr->data[cpu]->trace_idx > tr->entries)
904 entries += tr->entries;
906 entries += tr->data[cpu]->trace_idx;
910 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
912 seq_puts(m, "-----------------------------------"
913 "---------------------------------\n");
914 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
915 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
916 nsecs_to_usecs(data->saved_latency),
920 #if defined(CONFIG_PREEMPT_NONE)
922 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
924 #elif defined(CONFIG_PREEMPT_DESKTOP)
929 /* These are reserved for later use */
932 seq_printf(m, " #P:%d)\n", num_online_cpus());
936 seq_puts(m, " -----------------\n");
937 seq_printf(m, " | task: %.16s-%d "
938 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
939 data->comm, data->pid, data->uid, data->nice,
940 data->policy, data->rt_priority);
941 seq_puts(m, " -----------------\n");
943 if (data->critical_start) {
944 seq_puts(m, " => started at: ");
945 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
946 trace_print_seq(m, &iter->seq);
947 seq_puts(m, "\n => ended at: ");
948 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
949 trace_print_seq(m, &iter->seq);
957 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
959 int hardirq, softirq;
962 comm = trace_find_cmdline(entry->pid);
964 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
965 trace_seq_printf(s, "%d", cpu);
966 trace_seq_printf(s, "%c%c",
967 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
968 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
970 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
971 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
972 if (hardirq && softirq)
973 trace_seq_putc(s, 'H');
976 trace_seq_putc(s, 'h');
979 trace_seq_putc(s, 's');
981 trace_seq_putc(s, '.');
985 if (entry->preempt_count)
986 trace_seq_printf(s, "%x", entry->preempt_count);
988 trace_seq_puts(s, ".");
991 unsigned long preempt_mark_thresh = 100;
994 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
995 unsigned long rel_usecs)
997 trace_seq_printf(s, " %4lldus", abs_usecs);
998 if (rel_usecs > preempt_mark_thresh)
999 trace_seq_puts(s, "!: ");
1000 else if (rel_usecs > 1)
1001 trace_seq_puts(s, "+: ");
1003 trace_seq_puts(s, " : ");
1006 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1009 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1011 struct trace_seq *s = &iter->seq;
1012 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1013 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1014 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1015 struct trace_entry *entry = iter->ent;
1016 unsigned long abs_usecs;
1017 unsigned long rel_usecs;
1023 rel_usecs = ns2usecs(next_entry->t - entry->t);
1024 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1027 comm = trace_find_cmdline(entry->pid);
1028 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1029 " %ld.%03ldms (+%ld.%03ldms): ",
1031 entry->pid, cpu, entry->flags,
1032 entry->preempt_count, trace_idx,
1035 abs_usecs % 1000, rel_usecs/1000,
1038 lat_print_generic(s, entry, cpu);
1039 lat_print_timestamp(s, abs_usecs, rel_usecs);
1041 switch (entry->type) {
1043 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1044 trace_seq_puts(s, " (");
1045 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1046 trace_seq_puts(s, ")\n");
1049 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1050 state_to_char[entry->ctx.prev_state] : 'X';
1051 comm = trace_find_cmdline(entry->ctx.next_pid);
1052 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1053 entry->ctx.prev_pid,
1054 entry->ctx.prev_prio,
1056 entry->ctx.next_pid,
1057 entry->ctx.next_prio,
1061 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1065 static notrace void sync_time_offset(struct trace_iterator *iter)
1067 struct trace_array_cpu *prev_array, *array;
1068 struct trace_entry *prev_entry, *entry;
1072 prev_entry = iter->prev_ent;
1076 prev_array = iter->tr->data[iter->prev_cpu];
1077 array = iter->tr->data[iter->cpu];
1079 prev_t = prev_entry->t + prev_array->time_offset;
1080 t = entry->t + array->time_offset;
1083 * If time goes backwards we increase the offset of
1084 * the current array, to not have observable time warps.
1085 * This will quickly synchronize the time offsets of
1089 array->time_offset += prev_t - t;
1093 print_trace_fmt(struct trace_iterator *iter)
1095 struct trace_seq *s = &iter->seq;
1096 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1097 struct trace_entry *entry;
1098 unsigned long usec_rem;
1099 unsigned long long t;
1104 sync_time_offset(iter);
1107 comm = trace_find_cmdline(iter->ent->pid);
1109 t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1110 usec_rem = do_div(t, 1000000ULL);
1111 secs = (unsigned long)t;
1113 trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1114 trace_seq_printf(s, "[%02d] ", iter->cpu);
1115 trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1117 switch (entry->type) {
1119 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1120 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1121 entry->fn.parent_ip) {
1122 trace_seq_printf(s, " <-");
1123 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1125 trace_seq_printf(s, "\n");
1128 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1129 state_to_char[entry->ctx.prev_state] : 'X';
1130 trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1131 entry->ctx.prev_pid,
1132 entry->ctx.prev_prio,
1134 entry->ctx.next_pid,
1135 entry->ctx.next_prio);
1140 static int trace_empty(struct trace_iterator *iter)
1142 struct trace_array_cpu *data;
1145 for_each_possible_cpu(cpu) {
1146 data = iter->tr->data[cpu];
1148 if (head_page(data) && data->trace_idx)
1154 static int s_show(struct seq_file *m, void *v)
1156 struct trace_iterator *iter = v;
1158 if (iter->ent == NULL) {
1160 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1163 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1164 /* print nothing if the buffers are empty */
1165 if (trace_empty(iter))
1167 print_trace_header(m, iter);
1168 if (!(trace_flags & TRACE_ITER_VERBOSE))
1169 print_lat_help_header(m);
1171 if (!(trace_flags & TRACE_ITER_VERBOSE))
1172 print_func_help_header(m);
1175 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1176 print_lat_fmt(iter, iter->idx, iter->cpu);
1178 print_trace_fmt(iter);
1179 trace_print_seq(m, &iter->seq);
1185 static struct seq_operations tracer_seq_ops = {
1192 static struct trace_iterator notrace *
1193 __tracing_open(struct inode *inode, struct file *file, int *ret)
1195 struct trace_iterator *iter;
1197 if (tracing_disabled) {
1202 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1208 mutex_lock(&trace_types_lock);
1209 if (current_trace && current_trace->print_max)
1212 iter->tr = inode->i_private;
1213 iter->trace = current_trace;
1216 /* TODO stop tracer */
1217 *ret = seq_open(file, &tracer_seq_ops);
1219 struct seq_file *m = file->private_data;
1222 /* stop the trace while dumping */
1226 if (iter->trace && iter->trace->open)
1227 iter->trace->open(iter);
1232 mutex_unlock(&trace_types_lock);
1238 int tracing_open_generic(struct inode *inode, struct file *filp)
1240 if (tracing_disabled)
1243 filp->private_data = inode->i_private;
1247 int tracing_release(struct inode *inode, struct file *file)
1249 struct seq_file *m = (struct seq_file *)file->private_data;
1250 struct trace_iterator *iter = m->private;
1252 mutex_lock(&trace_types_lock);
1253 if (iter->trace && iter->trace->close)
1254 iter->trace->close(iter);
1256 /* reenable tracing if it was previously enabled */
1259 mutex_unlock(&trace_types_lock);
1261 seq_release(inode, file);
1266 static int tracing_open(struct inode *inode, struct file *file)
1270 __tracing_open(inode, file, &ret);
1275 static int tracing_lt_open(struct inode *inode, struct file *file)
1277 struct trace_iterator *iter;
1280 iter = __tracing_open(inode, file, &ret);
1283 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1289 static notrace void *
1290 t_next(struct seq_file *m, void *v, loff_t *pos)
1292 struct tracer *t = m->private;
1304 static void *t_start(struct seq_file *m, loff_t *pos)
1306 struct tracer *t = m->private;
1309 mutex_lock(&trace_types_lock);
1310 for (; t && l < *pos; t = t_next(m, t, &l))
1316 static void t_stop(struct seq_file *m, void *p)
1318 mutex_unlock(&trace_types_lock);
1321 static int t_show(struct seq_file *m, void *v)
1323 struct tracer *t = v;
1328 seq_printf(m, "%s", t->name);
1337 static struct seq_operations show_traces_seq_ops = {
1344 static int show_traces_open(struct inode *inode, struct file *file)
1348 if (tracing_disabled)
1351 ret = seq_open(file, &show_traces_seq_ops);
1353 struct seq_file *m = file->private_data;
1354 m->private = trace_types;
1360 static struct file_operations tracing_fops = {
1361 .open = tracing_open,
1363 .llseek = seq_lseek,
1364 .release = tracing_release,
1367 static struct file_operations tracing_lt_fops = {
1368 .open = tracing_lt_open,
1370 .llseek = seq_lseek,
1371 .release = tracing_release,
1374 static struct file_operations show_traces_fops = {
1375 .open = show_traces_open,
1377 .release = seq_release,
1381 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1382 size_t cnt, loff_t *ppos)
1389 /* calulate max size */
1390 for (i = 0; trace_options[i]; i++) {
1391 len += strlen(trace_options[i]);
1392 len += 3; /* "no" and space */
1395 /* +2 for \n and \0 */
1396 buf = kmalloc(len + 2, GFP_KERNEL);
1400 for (i = 0; trace_options[i]; i++) {
1401 if (trace_flags & (1 << i))
1402 r += sprintf(buf + r, "%s ", trace_options[i]);
1404 r += sprintf(buf + r, "no%s ", trace_options[i]);
1407 r += sprintf(buf + r, "\n");
1408 WARN_ON(r >= len + 2);
1410 r = simple_read_from_buffer(ubuf, cnt, ppos,
1419 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1420 size_t cnt, loff_t *ppos)
1430 if (copy_from_user(&buf, ubuf, cnt))
1435 if (strncmp(buf, "no", 2) == 0) {
1440 for (i = 0; trace_options[i]; i++) {
1441 int len = strlen(trace_options[i]);
1443 if (strncmp(cmp, trace_options[i], len) == 0) {
1445 trace_flags &= ~(1 << i);
1447 trace_flags |= (1 << i);
1457 static struct file_operations tracing_iter_fops = {
1458 .open = tracing_open_generic,
1459 .read = tracing_iter_ctrl_read,
1460 .write = tracing_iter_ctrl_write,
1463 static const char readme_msg[] =
1464 "tracing mini-HOWTO:\n\n"
1466 "# mount -t debugfs nodev /debug\n\n"
1467 "# cat /debug/tracing/available_tracers\n"
1468 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1469 "# cat /debug/tracing/current_tracer\n"
1471 "# echo sched_switch > /debug/tracing/current_tracer\n"
1472 "# cat /debug/tracing/current_tracer\n"
1474 "# cat /debug/tracing/iter_ctrl\n"
1475 "noprint-parent nosym-offset nosym-addr noverbose\n"
1476 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1477 "# echo 1 > /debug/tracing/tracing_enabled\n"
1478 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1479 "echo 0 > /debug/tracing/tracing_enabled\n"
1483 tracing_readme_read(struct file *filp, char __user *ubuf,
1484 size_t cnt, loff_t *ppos)
1486 return simple_read_from_buffer(ubuf, cnt, ppos,
1487 readme_msg, strlen(readme_msg));
1490 static struct file_operations tracing_readme_fops = {
1491 .open = tracing_open_generic,
1492 .read = tracing_readme_read,
1497 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1498 size_t cnt, loff_t *ppos)
1500 struct trace_array *tr = filp->private_data;
1504 r = sprintf(buf, "%ld\n", tr->ctrl);
1505 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1509 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1510 size_t cnt, loff_t *ppos)
1512 struct trace_array *tr = filp->private_data;
1519 if (copy_from_user(&buf, ubuf, cnt))
1524 val = simple_strtoul(buf, NULL, 10);
1528 mutex_lock(&trace_types_lock);
1529 if (tr->ctrl ^ val) {
1537 if (current_trace && current_trace->ctrl_update)
1538 current_trace->ctrl_update(tr);
1540 mutex_unlock(&trace_types_lock);
1548 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1549 size_t cnt, loff_t *ppos)
1551 char buf[max_tracer_type_len+2];
1554 mutex_lock(&trace_types_lock);
1556 r = sprintf(buf, "%s\n", current_trace->name);
1558 r = sprintf(buf, "\n");
1559 mutex_unlock(&trace_types_lock);
1561 return simple_read_from_buffer(ubuf, cnt, ppos,
1566 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1567 size_t cnt, loff_t *ppos)
1569 struct trace_array *tr = &global_trace;
1571 char buf[max_tracer_type_len+1];
1574 if (cnt > max_tracer_type_len)
1575 cnt = max_tracer_type_len;
1577 if (copy_from_user(&buf, ubuf, cnt))
1582 /* strip ending whitespace. */
1583 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1586 mutex_lock(&trace_types_lock);
1587 for (t = trace_types; t; t = t->next) {
1588 if (strcmp(t->name, buf) == 0)
1591 if (!t || t == current_trace)
1594 if (current_trace && current_trace->reset)
1595 current_trace->reset(tr);
1602 mutex_unlock(&trace_types_lock);
1610 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1611 size_t cnt, loff_t *ppos)
1613 unsigned long *ptr = filp->private_data;
1617 r = snprintf(buf, 64, "%ld\n",
1618 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1621 return simple_read_from_buffer(ubuf, cnt, ppos,
1626 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1627 size_t cnt, loff_t *ppos)
1629 long *ptr = filp->private_data;
1636 if (copy_from_user(&buf, ubuf, cnt))
1641 val = simple_strtoul(buf, NULL, 10);
1648 static struct file_operations tracing_max_lat_fops = {
1649 .open = tracing_open_generic,
1650 .read = tracing_max_lat_read,
1651 .write = tracing_max_lat_write,
1654 static struct file_operations tracing_ctrl_fops = {
1655 .open = tracing_open_generic,
1656 .read = tracing_ctrl_read,
1657 .write = tracing_ctrl_write,
1660 static struct file_operations set_tracer_fops = {
1661 .open = tracing_open_generic,
1662 .read = tracing_set_trace_read,
1663 .write = tracing_set_trace_write,
1666 #ifdef CONFIG_DYNAMIC_FTRACE
1669 tracing_read_long(struct file *filp, char __user *ubuf,
1670 size_t cnt, loff_t *ppos)
1672 unsigned long *p = filp->private_data;
1676 r = sprintf(buf, "%ld\n", *p);
1677 return simple_read_from_buffer(ubuf, cnt, ppos,
1681 static struct file_operations tracing_read_long_fops = {
1682 .open = tracing_open_generic,
1683 .read = tracing_read_long,
1687 static struct dentry *d_tracer;
1689 struct dentry *tracing_init_dentry(void)
1696 d_tracer = debugfs_create_dir("tracing", NULL);
1698 if (!d_tracer && !once) {
1700 pr_warning("Could not create debugfs directory 'tracing'\n");
1707 #ifdef CONFIG_FTRACE_SELFTEST
1708 /* Let selftest have access to static functions in this file */
1709 #include "trace_selftest.c"
1712 static __init void tracer_init_debugfs(void)
1714 struct dentry *d_tracer;
1715 struct dentry *entry;
1717 d_tracer = tracing_init_dentry();
1719 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1720 &global_trace, &tracing_ctrl_fops);
1722 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1724 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1725 NULL, &tracing_iter_fops);
1727 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1729 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1730 &global_trace, &tracing_lt_fops);
1732 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1734 entry = debugfs_create_file("trace", 0444, d_tracer,
1735 &global_trace, &tracing_fops);
1737 pr_warning("Could not create debugfs 'trace' entry\n");
1739 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
1740 &global_trace, &show_traces_fops);
1742 pr_warning("Could not create debugfs 'trace' entry\n");
1744 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
1745 &global_trace, &set_tracer_fops);
1747 pr_warning("Could not create debugfs 'trace' entry\n");
1749 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
1750 &tracing_max_latency,
1751 &tracing_max_lat_fops);
1753 pr_warning("Could not create debugfs "
1754 "'tracing_max_latency' entry\n");
1756 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
1757 &tracing_thresh, &tracing_max_lat_fops);
1759 pr_warning("Could not create debugfs "
1760 "'tracing_threash' entry\n");
1761 entry = debugfs_create_file("README", 0644, d_tracer,
1762 NULL, &tracing_readme_fops);
1764 pr_warning("Could not create debugfs 'README' entry\n");
1767 #ifdef CONFIG_DYNAMIC_FTRACE
1768 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
1769 &ftrace_update_tot_cnt,
1770 &tracing_read_long_fops);
1772 pr_warning("Could not create debugfs "
1773 "'dyn_ftrace_total_info' entry\n");
1777 /* dummy trace to disable tracing */
1778 static struct tracer no_tracer __read_mostly =
1783 static int trace_alloc_page(void)
1785 struct trace_array_cpu *data;
1786 struct page *page, *tmp;
1791 /* first allocate a page for each CPU */
1792 for_each_possible_cpu(i) {
1793 array = (void *)__get_free_page(GFP_KERNEL);
1794 if (array == NULL) {
1795 printk(KERN_ERR "tracer: failed to allocate page"
1796 "for trace buffer!\n");
1800 page = virt_to_page(array);
1801 list_add(&page->lru, &pages);
1803 /* Only allocate if we are actually using the max trace */
1804 #ifdef CONFIG_TRACER_MAX_TRACE
1805 array = (void *)__get_free_page(GFP_KERNEL);
1806 if (array == NULL) {
1807 printk(KERN_ERR "tracer: failed to allocate page"
1808 "for trace buffer!\n");
1811 page = virt_to_page(array);
1812 list_add(&page->lru, &pages);
1816 /* Now that we successfully allocate a page per CPU, add them */
1817 for_each_possible_cpu(i) {
1818 data = global_trace.data[i];
1819 page = list_entry(pages.next, struct page, lru);
1820 list_del_init(&page->lru);
1821 list_add_tail(&page->lru, &data->trace_pages);
1824 #ifdef CONFIG_TRACER_MAX_TRACE
1825 data = max_tr.data[i];
1826 page = list_entry(pages.next, struct page, lru);
1827 list_del_init(&page->lru);
1828 list_add_tail(&page->lru, &data->trace_pages);
1832 global_trace.entries += ENTRIES_PER_PAGE;
1837 list_for_each_entry_safe(page, tmp, &pages, lru) {
1838 list_del_init(&page->lru);
1844 __init static int tracer_alloc_buffers(void)
1846 struct trace_array_cpu *data;
1853 /* Allocate the first page for all buffers */
1854 for_each_possible_cpu(i) {
1855 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
1856 max_tr.data[i] = &per_cpu(max_data, i);
1858 array = (void *)__get_free_page(GFP_KERNEL);
1859 if (array == NULL) {
1860 printk(KERN_ERR "tracer: failed to allocate page"
1861 "for trace buffer!\n");
1865 /* set the array to the list */
1866 INIT_LIST_HEAD(&data->trace_pages);
1867 page = virt_to_page(array);
1868 list_add(&page->lru, &data->trace_pages);
1869 /* use the LRU flag to differentiate the two buffers */
1872 /* Only allocate if we are actually using the max trace */
1873 #ifdef CONFIG_TRACER_MAX_TRACE
1874 array = (void *)__get_free_page(GFP_KERNEL);
1875 if (array == NULL) {
1876 printk(KERN_ERR "tracer: failed to allocate page"
1877 "for trace buffer!\n");
1881 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
1882 page = virt_to_page(array);
1883 list_add(&page->lru, &max_tr.data[i]->trace_pages);
1889 * Since we allocate by orders of pages, we may be able to
1892 global_trace.entries = ENTRIES_PER_PAGE;
1895 while (global_trace.entries < trace_nr_entries) {
1896 if (trace_alloc_page())
1900 max_tr.entries = global_trace.entries;
1902 pr_info("tracer: %d pages allocated for %ld",
1903 pages, trace_nr_entries);
1904 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
1905 pr_info(" actual entries %ld\n", global_trace.entries);
1907 tracer_init_debugfs();
1909 trace_init_cmdlines();
1911 register_tracer(&no_tracer);
1912 current_trace = &no_tracer;
1914 /* All seems OK, enable tracing */
1915 tracing_disabled = 0;
1920 for (i-- ; i >= 0; i--) {
1921 struct page *page, *tmp;
1922 struct trace_array_cpu *data = global_trace.data[i];
1925 list_for_each_entry_safe(page, tmp,
1926 &data->trace_pages, lru) {
1927 list_del_init(&page->lru);
1932 #ifdef CONFIG_TRACER_MAX_TRACE
1933 data = max_tr.data[i];
1935 list_for_each_entry_safe(page, tmp,
1936 &data->trace_pages, lru) {
1937 list_del_init(&page->lru);
1946 fs_initcall(tracer_alloc_buffers);