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/poll.h>
28 #include <linux/gfp.h>
31 #include <linux/stacktrace.h>
35 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36 unsigned long __read_mostly tracing_thresh;
38 static int tracing_disabled = 1;
41 ns2usecs(cycle_t nsec)
48 cycle_t ftrace_now(int cpu)
50 return cpu_clock(cpu);
53 static struct trace_array global_trace;
55 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
57 static struct trace_array max_tr;
59 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
61 static int tracer_enabled = 1;
62 static unsigned long trace_nr_entries = 65536UL;
64 static struct tracer *trace_types __read_mostly;
65 static struct tracer *current_trace __read_mostly;
66 static int max_tracer_type_len;
68 static DEFINE_MUTEX(trace_types_lock);
69 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
71 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
73 void trace_wake_up(void)
76 * The runqueue_is_locked() can fail, but this is the best we
79 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
83 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
85 static int __init set_nr_entries(char *str)
89 trace_nr_entries = simple_strtoul(str, &str, 0);
92 __setup("trace_entries=", set_nr_entries);
94 unsigned long nsecs_to_usecs(unsigned long nsecs)
100 __TRACE_FIRST_TYPE = 0,
111 enum trace_flag_type {
112 TRACE_FLAG_IRQS_OFF = 0x01,
113 TRACE_FLAG_NEED_RESCHED = 0x02,
114 TRACE_FLAG_HARDIRQ = 0x04,
115 TRACE_FLAG_SOFTIRQ = 0x08,
118 #define TRACE_ITER_SYM_MASK \
119 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
121 /* These must match the bit postions above */
122 static const char *trace_options[] = {
135 static DEFINE_SPINLOCK(ftrace_max_lock);
138 * Copy the new maximum trace into the separate maximum-trace
139 * structure. (this way the maximum trace is permanently saved,
140 * for later retrieval via /debugfs/tracing/latency_trace)
143 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
145 struct trace_array_cpu *data = tr->data[cpu];
148 max_tr.time_start = data->preempt_timestamp;
150 data = max_tr.data[cpu];
151 data->saved_latency = tracing_max_latency;
153 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
154 data->pid = tsk->pid;
155 data->uid = tsk->uid;
156 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
157 data->policy = tsk->policy;
158 data->rt_priority = tsk->rt_priority;
160 /* record this tasks comm */
161 tracing_record_cmdline(current);
164 void check_pages(struct trace_array_cpu *data)
166 struct page *page, *tmp;
168 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
169 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
171 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
172 BUG_ON(page->lru.next->prev != &page->lru);
173 BUG_ON(page->lru.prev->next != &page->lru);
177 void *head_page(struct trace_array_cpu *data)
182 if (list_empty(&data->trace_pages))
185 page = list_entry(data->trace_pages.next, struct page, lru);
186 BUG_ON(&page->lru == &data->trace_pages);
188 return page_address(page);
192 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
194 int len = (PAGE_SIZE - 1) - s->len;
202 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
205 /* If we can't write it all, don't bother writing anything */
215 trace_seq_puts(struct trace_seq *s, const char *str)
217 int len = strlen(str);
219 if (len > ((PAGE_SIZE - 1) - s->len))
222 memcpy(s->buffer + s->len, str, len);
229 trace_seq_putc(struct trace_seq *s, unsigned char c)
231 if (s->len >= (PAGE_SIZE - 1))
234 s->buffer[s->len++] = c;
240 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
242 if (len > ((PAGE_SIZE - 1) - s->len))
245 memcpy(s->buffer + s->len, mem, len);
254 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
256 unsigned char hex[HEX_CHARS];
261 BUG_ON(len >= HEX_CHARS);
266 for (i = 0, j = 0; i < len; i++) {
268 for (i = len-1, j = 0; i >= 0; i--) {
272 hex[j] = byte & 0x0f;
289 return trace_seq_putmem(s, hex, j);
293 trace_seq_reset(struct trace_seq *s)
299 trace_print_seq(struct seq_file *m, struct trace_seq *s)
301 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
304 seq_puts(m, s->buffer);
310 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
312 struct list_head flip_pages;
314 INIT_LIST_HEAD(&flip_pages);
316 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
317 sizeof(struct trace_array_cpu) -
318 offsetof(struct trace_array_cpu, trace_head_idx));
322 list_splice_init(&tr1->trace_pages, &flip_pages);
323 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
324 list_splice_init(&flip_pages, &tr2->trace_pages);
325 BUG_ON(!list_empty(&flip_pages));
331 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
333 struct trace_array_cpu *data;
336 WARN_ON_ONCE(!irqs_disabled());
337 spin_lock(&ftrace_max_lock);
338 /* clear out all the previous traces */
339 for_each_possible_cpu(i) {
341 flip_trace(max_tr.data[i], data);
345 __update_max_tr(tr, tsk, cpu);
346 spin_unlock(&ftrace_max_lock);
350 * update_max_tr_single - only copy one trace over, and reset the rest
352 * @tsk - task with the latency
353 * @cpu - the cpu of the buffer to copy.
356 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
358 struct trace_array_cpu *data = tr->data[cpu];
361 WARN_ON_ONCE(!irqs_disabled());
362 spin_lock(&ftrace_max_lock);
363 for_each_possible_cpu(i)
364 tracing_reset(max_tr.data[i]);
366 flip_trace(max_tr.data[cpu], data);
369 __update_max_tr(tr, tsk, cpu);
370 spin_unlock(&ftrace_max_lock);
373 int register_tracer(struct tracer *type)
380 pr_info("Tracer must have a name\n");
384 mutex_lock(&trace_types_lock);
385 for (t = trace_types; t; t = t->next) {
386 if (strcmp(type->name, t->name) == 0) {
388 pr_info("Trace %s already registered\n",
395 #ifdef CONFIG_FTRACE_STARTUP_TEST
396 if (type->selftest) {
397 struct tracer *saved_tracer = current_trace;
398 struct trace_array_cpu *data;
399 struct trace_array *tr = &global_trace;
400 int saved_ctrl = tr->ctrl;
403 * Run a selftest on this tracer.
404 * Here we reset the trace buffer, and set the current
405 * tracer to be this tracer. The tracer can then run some
406 * internal tracing to verify that everything is in order.
407 * If we fail, we do not register this tracer.
409 for_each_possible_cpu(i) {
411 if (!head_page(data))
415 current_trace = type;
417 /* the test is responsible for initializing and enabling */
418 pr_info("Testing tracer %s: ", type->name);
419 ret = type->selftest(type, tr);
420 /* the test is responsible for resetting too */
421 current_trace = saved_tracer;
422 tr->ctrl = saved_ctrl;
424 printk(KERN_CONT "FAILED!\n");
427 /* Only reset on passing, to avoid touching corrupted buffers */
428 for_each_possible_cpu(i) {
430 if (!head_page(data))
434 printk(KERN_CONT "PASSED\n");
438 type->next = trace_types;
440 len = strlen(type->name);
441 if (len > max_tracer_type_len)
442 max_tracer_type_len = len;
445 mutex_unlock(&trace_types_lock);
450 void unregister_tracer(struct tracer *type)
455 mutex_lock(&trace_types_lock);
456 for (t = &trace_types; *t; t = &(*t)->next) {
460 pr_info("Trace %s not registered\n", type->name);
465 if (strlen(type->name) != max_tracer_type_len)
468 max_tracer_type_len = 0;
469 for (t = &trace_types; *t; t = &(*t)->next) {
470 len = strlen((*t)->name);
471 if (len > max_tracer_type_len)
472 max_tracer_type_len = len;
475 mutex_unlock(&trace_types_lock);
478 void tracing_reset(struct trace_array_cpu *data)
481 data->trace_head = data->trace_tail = head_page(data);
482 data->trace_head_idx = 0;
483 data->trace_tail_idx = 0;
486 #define SAVED_CMDLINES 128
487 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
488 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
489 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
490 static int cmdline_idx;
491 static DEFINE_SPINLOCK(trace_cmdline_lock);
492 atomic_t trace_record_cmdline_disabled;
494 static void trace_init_cmdlines(void)
496 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
497 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
501 void trace_stop_cmdline_recording(void);
503 static void trace_save_cmdline(struct task_struct *tsk)
508 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
512 * It's not the end of the world if we don't get
513 * the lock, but we also don't want to spin
514 * nor do we want to disable interrupts,
515 * so if we miss here, then better luck next time.
517 if (!spin_trylock(&trace_cmdline_lock))
520 idx = map_pid_to_cmdline[tsk->pid];
521 if (idx >= SAVED_CMDLINES) {
522 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
524 map = map_cmdline_to_pid[idx];
525 if (map <= PID_MAX_DEFAULT)
526 map_pid_to_cmdline[map] = (unsigned)-1;
528 map_pid_to_cmdline[tsk->pid] = idx;
533 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
535 spin_unlock(&trace_cmdline_lock);
538 static char *trace_find_cmdline(int pid)
540 char *cmdline = "<...>";
546 if (pid > PID_MAX_DEFAULT)
549 map = map_pid_to_cmdline[pid];
550 if (map >= SAVED_CMDLINES)
553 cmdline = saved_cmdlines[map];
559 void tracing_record_cmdline(struct task_struct *tsk)
561 if (atomic_read(&trace_record_cmdline_disabled))
564 trace_save_cmdline(tsk);
567 static inline struct list_head *
568 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
571 * Roundrobin - but skip the head (which is not a real page):
574 if (unlikely(next == &data->trace_pages))
576 BUG_ON(next == &data->trace_pages);
582 trace_next_page(struct trace_array_cpu *data, void *addr)
584 struct list_head *next;
587 page = virt_to_page(addr);
589 next = trace_next_list(data, &page->lru);
590 page = list_entry(next, struct page, lru);
592 return page_address(page);
595 static inline struct trace_entry *
596 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
598 unsigned long idx, idx_next;
599 struct trace_entry *entry;
602 idx = data->trace_head_idx;
605 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
607 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
609 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
610 data->trace_head = trace_next_page(data, data->trace_head);
614 if (data->trace_head == data->trace_tail &&
615 idx_next == data->trace_tail_idx) {
617 data->trace_tail_idx++;
618 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
620 trace_next_page(data, data->trace_tail);
621 data->trace_tail_idx = 0;
625 data->trace_head_idx = idx_next;
631 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
633 struct task_struct *tsk = current;
636 pc = preempt_count();
638 entry->preempt_count = pc & 0xff;
639 entry->pid = tsk->pid;
640 entry->t = ftrace_now(raw_smp_processor_id());
641 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
642 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
643 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
644 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
648 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
649 unsigned long ip, unsigned long parent_ip, unsigned long flags)
651 struct trace_entry *entry;
652 unsigned long irq_flags;
654 spin_lock_irqsave(&data->lock, irq_flags);
655 entry = tracing_get_trace_entry(tr, data);
656 tracing_generic_entry_update(entry, flags);
657 entry->type = TRACE_FN;
659 entry->fn.parent_ip = parent_ip;
660 spin_unlock_irqrestore(&data->lock, irq_flags);
666 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
667 unsigned long ip, unsigned long parent_ip, unsigned long flags)
669 if (likely(!atomic_read(&data->disabled)))
670 trace_function(tr, data, ip, parent_ip, flags);
673 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
676 __trace_special(void *__tr, void *__data,
677 unsigned long arg1, unsigned long arg2, unsigned long arg3)
679 struct trace_array_cpu *data = __data;
680 struct trace_array *tr = __tr;
681 struct trace_entry *entry;
682 unsigned long irq_flags;
684 spin_lock_irqsave(&data->lock, irq_flags);
685 entry = tracing_get_trace_entry(tr, data);
686 tracing_generic_entry_update(entry, 0);
687 entry->type = TRACE_SPECIAL;
688 entry->special.arg1 = arg1;
689 entry->special.arg2 = arg2;
690 entry->special.arg3 = arg3;
691 spin_unlock_irqrestore(&data->lock, irq_flags);
698 void __trace_stack(struct trace_array *tr,
699 struct trace_array_cpu *data,
703 struct trace_entry *entry;
704 struct stack_trace trace;
706 if (!(trace_flags & TRACE_ITER_STACKTRACE))
709 entry = tracing_get_trace_entry(tr, data);
710 tracing_generic_entry_update(entry, flags);
711 entry->type = TRACE_STACK;
713 memset(&entry->stack, 0, sizeof(entry->stack));
715 trace.nr_entries = 0;
716 trace.max_entries = FTRACE_STACK_ENTRIES;
718 trace.entries = entry->stack.caller;
720 save_stack_trace(&trace);
724 tracing_sched_switch_trace(struct trace_array *tr,
725 struct trace_array_cpu *data,
726 struct task_struct *prev,
727 struct task_struct *next,
730 struct trace_entry *entry;
731 unsigned long irq_flags;
733 spin_lock_irqsave(&data->lock, irq_flags);
734 entry = tracing_get_trace_entry(tr, data);
735 tracing_generic_entry_update(entry, flags);
736 entry->type = TRACE_CTX;
737 entry->ctx.prev_pid = prev->pid;
738 entry->ctx.prev_prio = prev->prio;
739 entry->ctx.prev_state = prev->state;
740 entry->ctx.next_pid = next->pid;
741 entry->ctx.next_prio = next->prio;
742 __trace_stack(tr, data, flags, 4);
743 spin_unlock_irqrestore(&data->lock, irq_flags);
747 tracing_sched_wakeup_trace(struct trace_array *tr,
748 struct trace_array_cpu *data,
749 struct task_struct *wakee,
750 struct task_struct *curr,
753 struct trace_entry *entry;
754 unsigned long irq_flags;
756 spin_lock_irqsave(&data->lock, irq_flags);
757 entry = tracing_get_trace_entry(tr, data);
758 tracing_generic_entry_update(entry, flags);
759 entry->type = TRACE_WAKE;
760 entry->ctx.prev_pid = curr->pid;
761 entry->ctx.prev_prio = curr->prio;
762 entry->ctx.prev_state = curr->state;
763 entry->ctx.next_pid = wakee->pid;
764 entry->ctx.next_prio = wakee->prio;
765 __trace_stack(tr, data, flags, 5);
766 spin_unlock_irqrestore(&data->lock, irq_flags);
773 function_trace_call(unsigned long ip, unsigned long parent_ip)
775 struct trace_array *tr = &global_trace;
776 struct trace_array_cpu *data;
781 if (unlikely(!tracer_enabled))
784 local_irq_save(flags);
785 cpu = raw_smp_processor_id();
786 data = tr->data[cpu];
787 disabled = atomic_inc_return(&data->disabled);
789 if (likely(disabled == 1))
790 trace_function(tr, data, ip, parent_ip, flags);
792 atomic_dec(&data->disabled);
793 local_irq_restore(flags);
796 static struct ftrace_ops trace_ops __read_mostly =
798 .func = function_trace_call,
801 void tracing_start_function_trace(void)
803 register_ftrace_function(&trace_ops);
806 void tracing_stop_function_trace(void)
808 unregister_ftrace_function(&trace_ops);
812 enum trace_file_type {
813 TRACE_FILE_LAT_FMT = 1,
816 static struct trace_entry *
817 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
818 struct trace_iterator *iter, int cpu)
821 struct trace_entry *array;
823 if (iter->next_idx[cpu] >= tr->entries ||
824 iter->next_idx[cpu] >= data->trace_idx ||
825 (data->trace_head == data->trace_tail &&
826 data->trace_head_idx == data->trace_tail_idx))
829 if (!iter->next_page[cpu]) {
830 /* Initialize the iterator for this cpu trace buffer */
831 WARN_ON(!data->trace_tail);
832 page = virt_to_page(data->trace_tail);
833 iter->next_page[cpu] = &page->lru;
834 iter->next_page_idx[cpu] = data->trace_tail_idx;
837 page = list_entry(iter->next_page[cpu], struct page, lru);
838 BUG_ON(&data->trace_pages == &page->lru);
840 array = page_address(page);
842 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
843 return &array[iter->next_page_idx[cpu]];
846 static struct trace_entry *
847 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
849 struct trace_array *tr = iter->tr;
850 struct trace_entry *ent, *next = NULL;
854 for_each_possible_cpu(cpu) {
855 if (!head_page(tr->data[cpu]))
857 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
859 * Pick the entry with the smallest timestamp:
861 if (ent && (!next || ent->t < next->t)) {
873 static void trace_iterator_increment(struct trace_iterator *iter)
876 iter->next_idx[iter->cpu]++;
877 iter->next_page_idx[iter->cpu]++;
879 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
880 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
882 iter->next_page_idx[iter->cpu] = 0;
883 iter->next_page[iter->cpu] =
884 trace_next_list(data, iter->next_page[iter->cpu]);
888 static void trace_consume(struct trace_iterator *iter)
890 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
892 data->trace_tail_idx++;
893 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
894 data->trace_tail = trace_next_page(data, data->trace_tail);
895 data->trace_tail_idx = 0;
898 /* Check if we empty it, then reset the index */
899 if (data->trace_head == data->trace_tail &&
900 data->trace_head_idx == data->trace_tail_idx)
904 static void *find_next_entry_inc(struct trace_iterator *iter)
906 struct trace_entry *next;
909 next = find_next_entry(iter, &next_cpu);
911 iter->prev_ent = iter->ent;
912 iter->prev_cpu = iter->cpu;
915 iter->cpu = next_cpu;
918 trace_iterator_increment(iter);
920 return next ? iter : NULL;
923 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
925 struct trace_iterator *iter = m->private;
926 void *last_ent = iter->ent;
932 /* can't go backwards */
937 ent = find_next_entry_inc(iter);
941 while (ent && iter->idx < i)
942 ent = find_next_entry_inc(iter);
946 if (last_ent && !ent)
947 seq_puts(m, "\n\nvim:ft=help\n");
952 static void *s_start(struct seq_file *m, loff_t *pos)
954 struct trace_iterator *iter = m->private;
959 mutex_lock(&trace_types_lock);
961 if (!current_trace || current_trace != iter->trace)
964 atomic_inc(&trace_record_cmdline_disabled);
966 /* let the tracer grab locks here if needed */
967 if (current_trace->start)
968 current_trace->start(iter);
970 if (*pos != iter->pos) {
974 iter->prev_ent = NULL;
977 for_each_possible_cpu(i) {
978 iter->next_idx[i] = 0;
979 iter->next_page[i] = NULL;
982 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
987 p = s_next(m, p, &l);
993 static void s_stop(struct seq_file *m, void *p)
995 struct trace_iterator *iter = m->private;
997 atomic_dec(&trace_record_cmdline_disabled);
999 /* let the tracer release locks here if needed */
1000 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1001 iter->trace->stop(iter);
1003 mutex_unlock(&trace_types_lock);
1007 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1009 #ifdef CONFIG_KALLSYMS
1010 char str[KSYM_SYMBOL_LEN];
1012 kallsyms_lookup(address, NULL, NULL, NULL, str);
1014 return trace_seq_printf(s, fmt, str);
1020 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1021 unsigned long address)
1023 #ifdef CONFIG_KALLSYMS
1024 char str[KSYM_SYMBOL_LEN];
1026 sprint_symbol(str, address);
1027 return trace_seq_printf(s, fmt, str);
1032 #ifndef CONFIG_64BIT
1033 # define IP_FMT "%08lx"
1035 # define IP_FMT "%016lx"
1039 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1044 return trace_seq_printf(s, "0");
1046 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1047 ret = seq_print_sym_offset(s, "%s", ip);
1049 ret = seq_print_sym_short(s, "%s", ip);
1054 if (sym_flags & TRACE_ITER_SYM_ADDR)
1055 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1059 static void print_lat_help_header(struct seq_file *m)
1061 seq_puts(m, "# _------=> CPU# \n");
1062 seq_puts(m, "# / _-----=> irqs-off \n");
1063 seq_puts(m, "# | / _----=> need-resched \n");
1064 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1065 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1066 seq_puts(m, "# |||| / \n");
1067 seq_puts(m, "# ||||| delay \n");
1068 seq_puts(m, "# cmd pid ||||| time | caller \n");
1069 seq_puts(m, "# \\ / ||||| \\ | / \n");
1072 static void print_func_help_header(struct seq_file *m)
1074 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1075 seq_puts(m, "# | | | | |\n");
1080 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1082 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1083 struct trace_array *tr = iter->tr;
1084 struct trace_array_cpu *data = tr->data[tr->cpu];
1085 struct tracer *type = current_trace;
1086 unsigned long total = 0;
1087 unsigned long entries = 0;
1089 const char *name = "preemption";
1094 for_each_possible_cpu(cpu) {
1095 if (head_page(tr->data[cpu])) {
1096 total += tr->data[cpu]->trace_idx;
1097 if (tr->data[cpu]->trace_idx > tr->entries)
1098 entries += tr->entries;
1100 entries += tr->data[cpu]->trace_idx;
1104 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1106 seq_puts(m, "-----------------------------------"
1107 "---------------------------------\n");
1108 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1109 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1110 nsecs_to_usecs(data->saved_latency),
1114 #if defined(CONFIG_PREEMPT_NONE)
1116 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1118 #elif defined(CONFIG_PREEMPT_DESKTOP)
1123 /* These are reserved for later use */
1126 seq_printf(m, " #P:%d)\n", num_online_cpus());
1130 seq_puts(m, " -----------------\n");
1131 seq_printf(m, " | task: %.16s-%d "
1132 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1133 data->comm, data->pid, data->uid, data->nice,
1134 data->policy, data->rt_priority);
1135 seq_puts(m, " -----------------\n");
1137 if (data->critical_start) {
1138 seq_puts(m, " => started at: ");
1139 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1140 trace_print_seq(m, &iter->seq);
1141 seq_puts(m, "\n => ended at: ");
1142 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1143 trace_print_seq(m, &iter->seq);
1151 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1153 int hardirq, softirq;
1156 comm = trace_find_cmdline(entry->pid);
1158 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1159 trace_seq_printf(s, "%d", cpu);
1160 trace_seq_printf(s, "%c%c",
1161 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1162 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1164 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1165 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1166 if (hardirq && softirq)
1167 trace_seq_putc(s, 'H');
1170 trace_seq_putc(s, 'h');
1173 trace_seq_putc(s, 's');
1175 trace_seq_putc(s, '.');
1179 if (entry->preempt_count)
1180 trace_seq_printf(s, "%x", entry->preempt_count);
1182 trace_seq_puts(s, ".");
1185 unsigned long preempt_mark_thresh = 100;
1188 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1189 unsigned long rel_usecs)
1191 trace_seq_printf(s, " %4lldus", abs_usecs);
1192 if (rel_usecs > preempt_mark_thresh)
1193 trace_seq_puts(s, "!: ");
1194 else if (rel_usecs > 1)
1195 trace_seq_puts(s, "+: ");
1197 trace_seq_puts(s, " : ");
1200 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1203 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1205 struct trace_seq *s = &iter->seq;
1206 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1207 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1208 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1209 struct trace_entry *entry = iter->ent;
1210 unsigned long abs_usecs;
1211 unsigned long rel_usecs;
1218 rel_usecs = ns2usecs(next_entry->t - entry->t);
1219 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1222 comm = trace_find_cmdline(entry->pid);
1223 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1224 " %ld.%03ldms (+%ld.%03ldms): ",
1226 entry->pid, cpu, entry->flags,
1227 entry->preempt_count, trace_idx,
1230 abs_usecs % 1000, rel_usecs/1000,
1233 if (entry->type != TRACE_STACK) {
1234 lat_print_generic(s, entry, cpu);
1235 lat_print_timestamp(s, abs_usecs, rel_usecs);
1238 switch (entry->type) {
1240 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1241 trace_seq_puts(s, " (");
1242 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1243 trace_seq_puts(s, ")\n");
1247 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1248 state_to_char[entry->ctx.prev_state] : 'X';
1249 comm = trace_find_cmdline(entry->ctx.next_pid);
1250 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
1251 entry->ctx.prev_pid,
1252 entry->ctx.prev_prio,
1253 S, entry->type == TRACE_CTX ? "==>" : " +",
1254 entry->ctx.next_pid,
1255 entry->ctx.next_prio,
1259 trace_seq_printf(s, " %ld %ld %ld\n",
1260 entry->special.arg1,
1261 entry->special.arg2,
1262 entry->special.arg3);
1265 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1267 trace_seq_puts(s, " <= ");
1268 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1270 trace_seq_puts(s, "\n");
1273 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1278 static int print_trace_fmt(struct trace_iterator *iter)
1280 struct trace_seq *s = &iter->seq;
1281 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1282 struct trace_entry *entry;
1283 unsigned long usec_rem;
1284 unsigned long long t;
1293 comm = trace_find_cmdline(iter->ent->pid);
1295 t = ns2usecs(entry->t);
1296 usec_rem = do_div(t, 1000000ULL);
1297 secs = (unsigned long)t;
1299 if (entry->type != TRACE_STACK) {
1300 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1303 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1306 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1311 switch (entry->type) {
1313 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1316 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1317 entry->fn.parent_ip) {
1318 ret = trace_seq_printf(s, " <-");
1321 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1326 ret = trace_seq_printf(s, "\n");
1332 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1333 state_to_char[entry->ctx.prev_state] : 'X';
1334 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
1335 entry->ctx.prev_pid,
1336 entry->ctx.prev_prio,
1338 entry->type == TRACE_CTX ? "==>" : " +",
1339 entry->ctx.next_pid,
1340 entry->ctx.next_prio);
1345 ret = trace_seq_printf(s, " %ld %ld %ld\n",
1346 entry->special.arg1,
1347 entry->special.arg2,
1348 entry->special.arg3);
1353 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1355 ret = trace_seq_puts(s, " <= ");
1359 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1364 ret = trace_seq_puts(s, "\n");
1372 static int print_raw_fmt(struct trace_iterator *iter)
1374 struct trace_seq *s = &iter->seq;
1375 struct trace_entry *entry;
1381 ret = trace_seq_printf(s, "%d %d %llu ",
1382 entry->pid, iter->cpu, entry->t);
1386 switch (entry->type) {
1388 ret = trace_seq_printf(s, "%x %x\n",
1389 entry->fn.ip, entry->fn.parent_ip);
1395 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1396 state_to_char[entry->ctx.prev_state] : 'X';
1397 if (entry->type == TRACE_WAKE)
1399 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1400 entry->ctx.prev_pid,
1401 entry->ctx.prev_prio,
1403 entry->ctx.next_pid,
1404 entry->ctx.next_prio);
1410 ret = trace_seq_printf(s, " %ld %ld %ld\n",
1411 entry->special.arg1,
1412 entry->special.arg2,
1413 entry->special.arg3);
1421 #define SEQ_PUT_FIELD_RET(s, x) \
1423 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1427 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1429 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1433 static int print_hex_fmt(struct trace_iterator *iter)
1435 struct trace_seq *s = &iter->seq;
1436 unsigned char newline = '\n';
1437 struct trace_entry *entry;
1442 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1443 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1444 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1446 switch (entry->type) {
1448 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1449 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1453 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1454 state_to_char[entry->ctx.prev_state] : 'X';
1455 if (entry->type == TRACE_WAKE)
1457 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1458 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1459 SEQ_PUT_HEX_FIELD_RET(s, S);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1462 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1466 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1467 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1468 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1471 SEQ_PUT_FIELD_RET(s, newline);
1476 static int print_bin_fmt(struct trace_iterator *iter)
1478 struct trace_seq *s = &iter->seq;
1479 struct trace_entry *entry;
1483 SEQ_PUT_FIELD_RET(s, entry->pid);
1484 SEQ_PUT_FIELD_RET(s, entry->cpu);
1485 SEQ_PUT_FIELD_RET(s, entry->t);
1487 switch (entry->type) {
1489 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1490 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1493 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1494 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1495 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1496 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1497 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1501 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1502 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1503 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1509 static int trace_empty(struct trace_iterator *iter)
1511 struct trace_array_cpu *data;
1514 for_each_possible_cpu(cpu) {
1515 data = iter->tr->data[cpu];
1517 if (head_page(data) && data->trace_idx &&
1518 (data->trace_tail != data->trace_head ||
1519 data->trace_tail_idx != data->trace_head_idx))
1525 static int print_trace_line(struct trace_iterator *iter)
1527 if (trace_flags & TRACE_ITER_BIN)
1528 return print_bin_fmt(iter);
1530 if (trace_flags & TRACE_ITER_HEX)
1531 return print_hex_fmt(iter);
1533 if (trace_flags & TRACE_ITER_RAW)
1534 return print_raw_fmt(iter);
1536 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1537 return print_lat_fmt(iter, iter->idx, iter->cpu);
1539 return print_trace_fmt(iter);
1542 static int s_show(struct seq_file *m, void *v)
1544 struct trace_iterator *iter = v;
1546 if (iter->ent == NULL) {
1548 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1551 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1552 /* print nothing if the buffers are empty */
1553 if (trace_empty(iter))
1555 print_trace_header(m, iter);
1556 if (!(trace_flags & TRACE_ITER_VERBOSE))
1557 print_lat_help_header(m);
1559 if (!(trace_flags & TRACE_ITER_VERBOSE))
1560 print_func_help_header(m);
1563 print_trace_line(iter);
1564 trace_print_seq(m, &iter->seq);
1570 static struct seq_operations tracer_seq_ops = {
1577 static struct trace_iterator *
1578 __tracing_open(struct inode *inode, struct file *file, int *ret)
1580 struct trace_iterator *iter;
1582 if (tracing_disabled) {
1587 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1593 mutex_lock(&trace_types_lock);
1594 if (current_trace && current_trace->print_max)
1597 iter->tr = inode->i_private;
1598 iter->trace = current_trace;
1601 /* TODO stop tracer */
1602 *ret = seq_open(file, &tracer_seq_ops);
1604 struct seq_file *m = file->private_data;
1607 /* stop the trace while dumping */
1611 if (iter->trace && iter->trace->open)
1612 iter->trace->open(iter);
1617 mutex_unlock(&trace_types_lock);
1623 int tracing_open_generic(struct inode *inode, struct file *filp)
1625 if (tracing_disabled)
1628 filp->private_data = inode->i_private;
1632 int tracing_release(struct inode *inode, struct file *file)
1634 struct seq_file *m = (struct seq_file *)file->private_data;
1635 struct trace_iterator *iter = m->private;
1637 mutex_lock(&trace_types_lock);
1638 if (iter->trace && iter->trace->close)
1639 iter->trace->close(iter);
1641 /* reenable tracing if it was previously enabled */
1644 mutex_unlock(&trace_types_lock);
1646 seq_release(inode, file);
1651 static int tracing_open(struct inode *inode, struct file *file)
1655 __tracing_open(inode, file, &ret);
1660 static int tracing_lt_open(struct inode *inode, struct file *file)
1662 struct trace_iterator *iter;
1665 iter = __tracing_open(inode, file, &ret);
1668 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1675 t_next(struct seq_file *m, void *v, loff_t *pos)
1677 struct tracer *t = m->private;
1689 static void *t_start(struct seq_file *m, loff_t *pos)
1691 struct tracer *t = m->private;
1694 mutex_lock(&trace_types_lock);
1695 for (; t && l < *pos; t = t_next(m, t, &l))
1701 static void t_stop(struct seq_file *m, void *p)
1703 mutex_unlock(&trace_types_lock);
1706 static int t_show(struct seq_file *m, void *v)
1708 struct tracer *t = v;
1713 seq_printf(m, "%s", t->name);
1722 static struct seq_operations show_traces_seq_ops = {
1729 static int show_traces_open(struct inode *inode, struct file *file)
1733 if (tracing_disabled)
1736 ret = seq_open(file, &show_traces_seq_ops);
1738 struct seq_file *m = file->private_data;
1739 m->private = trace_types;
1745 static struct file_operations tracing_fops = {
1746 .open = tracing_open,
1748 .llseek = seq_lseek,
1749 .release = tracing_release,
1752 static struct file_operations tracing_lt_fops = {
1753 .open = tracing_lt_open,
1755 .llseek = seq_lseek,
1756 .release = tracing_release,
1759 static struct file_operations show_traces_fops = {
1760 .open = show_traces_open,
1762 .release = seq_release,
1766 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1767 size_t cnt, loff_t *ppos)
1774 /* calulate max size */
1775 for (i = 0; trace_options[i]; i++) {
1776 len += strlen(trace_options[i]);
1777 len += 3; /* "no" and space */
1780 /* +2 for \n and \0 */
1781 buf = kmalloc(len + 2, GFP_KERNEL);
1785 for (i = 0; trace_options[i]; i++) {
1786 if (trace_flags & (1 << i))
1787 r += sprintf(buf + r, "%s ", trace_options[i]);
1789 r += sprintf(buf + r, "no%s ", trace_options[i]);
1792 r += sprintf(buf + r, "\n");
1793 WARN_ON(r >= len + 2);
1795 r = simple_read_from_buffer(ubuf, cnt, ppos,
1804 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1805 size_t cnt, loff_t *ppos)
1815 if (copy_from_user(&buf, ubuf, cnt))
1820 if (strncmp(buf, "no", 2) == 0) {
1825 for (i = 0; trace_options[i]; i++) {
1826 int len = strlen(trace_options[i]);
1828 if (strncmp(cmp, trace_options[i], len) == 0) {
1830 trace_flags &= ~(1 << i);
1832 trace_flags |= (1 << i);
1842 static struct file_operations tracing_iter_fops = {
1843 .open = tracing_open_generic,
1844 .read = tracing_iter_ctrl_read,
1845 .write = tracing_iter_ctrl_write,
1848 static const char readme_msg[] =
1849 "tracing mini-HOWTO:\n\n"
1851 "# mount -t debugfs nodev /debug\n\n"
1852 "# cat /debug/tracing/available_tracers\n"
1853 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1854 "# cat /debug/tracing/current_tracer\n"
1856 "# echo sched_switch > /debug/tracing/current_tracer\n"
1857 "# cat /debug/tracing/current_tracer\n"
1859 "# cat /debug/tracing/iter_ctrl\n"
1860 "noprint-parent nosym-offset nosym-addr noverbose\n"
1861 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1862 "# echo 1 > /debug/tracing/tracing_enabled\n"
1863 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1864 "echo 0 > /debug/tracing/tracing_enabled\n"
1868 tracing_readme_read(struct file *filp, char __user *ubuf,
1869 size_t cnt, loff_t *ppos)
1871 return simple_read_from_buffer(ubuf, cnt, ppos,
1872 readme_msg, strlen(readme_msg));
1875 static struct file_operations tracing_readme_fops = {
1876 .open = tracing_open_generic,
1877 .read = tracing_readme_read,
1881 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1882 size_t cnt, loff_t *ppos)
1884 struct trace_array *tr = filp->private_data;
1888 r = sprintf(buf, "%ld\n", tr->ctrl);
1889 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1893 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1894 size_t cnt, loff_t *ppos)
1896 struct trace_array *tr = filp->private_data;
1903 if (copy_from_user(&buf, ubuf, cnt))
1908 val = simple_strtoul(buf, NULL, 10);
1912 mutex_lock(&trace_types_lock);
1913 if (tr->ctrl ^ val) {
1921 if (current_trace && current_trace->ctrl_update)
1922 current_trace->ctrl_update(tr);
1924 mutex_unlock(&trace_types_lock);
1932 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1933 size_t cnt, loff_t *ppos)
1935 char buf[max_tracer_type_len+2];
1938 mutex_lock(&trace_types_lock);
1940 r = sprintf(buf, "%s\n", current_trace->name);
1942 r = sprintf(buf, "\n");
1943 mutex_unlock(&trace_types_lock);
1945 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1949 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1950 size_t cnt, loff_t *ppos)
1952 struct trace_array *tr = &global_trace;
1954 char buf[max_tracer_type_len+1];
1957 if (cnt > max_tracer_type_len)
1958 cnt = max_tracer_type_len;
1960 if (copy_from_user(&buf, ubuf, cnt))
1965 /* strip ending whitespace. */
1966 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1969 mutex_lock(&trace_types_lock);
1970 for (t = trace_types; t; t = t->next) {
1971 if (strcmp(t->name, buf) == 0)
1974 if (!t || t == current_trace)
1977 if (current_trace && current_trace->reset)
1978 current_trace->reset(tr);
1985 mutex_unlock(&trace_types_lock);
1993 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1994 size_t cnt, loff_t *ppos)
1996 unsigned long *ptr = filp->private_data;
2000 r = snprintf(buf, 64, "%ld\n",
2001 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2004 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2008 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2009 size_t cnt, loff_t *ppos)
2011 long *ptr = filp->private_data;
2018 if (copy_from_user(&buf, ubuf, cnt))
2023 val = simple_strtoul(buf, NULL, 10);
2030 static atomic_t tracing_reader;
2032 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2034 struct trace_iterator *iter;
2036 if (tracing_disabled)
2039 /* We only allow for reader of the pipe */
2040 if (atomic_inc_return(&tracing_reader) != 1) {
2041 atomic_dec(&tracing_reader);
2045 /* create a buffer to store the information to pass to userspace */
2046 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2050 iter->tr = &global_trace;
2052 filp->private_data = iter;
2057 static int tracing_release_pipe(struct inode *inode, struct file *file)
2059 struct trace_iterator *iter = file->private_data;
2062 atomic_dec(&tracing_reader);
2068 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2070 struct trace_iterator *iter = filp->private_data;
2072 if (trace_flags & TRACE_ITER_BLOCK) {
2074 * Always select as readable when in blocking mode
2076 return POLLIN | POLLRDNORM;
2079 if (!trace_empty(iter))
2080 return POLLIN | POLLRDNORM;
2081 poll_wait(filp, &trace_wait, poll_table);
2082 if (!trace_empty(iter))
2083 return POLLIN | POLLRDNORM;
2093 tracing_read_pipe(struct file *filp, char __user *ubuf,
2094 size_t cnt, loff_t *ppos)
2096 struct trace_iterator *iter = filp->private_data;
2097 struct trace_array_cpu *data;
2098 static cpumask_t mask;
2100 unsigned long flags;
2101 #ifdef CONFIG_FTRACE
2109 /* return any leftover data */
2110 if (iter->seq.len > start) {
2111 len = iter->seq.len - start;
2114 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2123 trace_seq_reset(&iter->seq);
2126 while (trace_empty(iter)) {
2127 if (!(trace_flags & TRACE_ITER_BLOCK))
2128 return -EWOULDBLOCK;
2130 * This is a make-shift waitqueue. The reason we don't use
2131 * an actual wait queue is because:
2132 * 1) we only ever have one waiter
2133 * 2) the tracing, traces all functions, we don't want
2134 * the overhead of calling wake_up and friends
2135 * (and tracing them too)
2136 * Anyway, this is really very primitive wakeup.
2138 set_current_state(TASK_INTERRUPTIBLE);
2139 iter->tr->waiter = current;
2141 /* sleep for one second, and try again. */
2142 schedule_timeout(HZ);
2144 iter->tr->waiter = NULL;
2146 if (signal_pending(current))
2150 * We block until we read something and tracing is disabled.
2151 * We still block if tracing is disabled, but we have never
2152 * read anything. This allows a user to cat this file, and
2153 * then enable tracing. But after we have read something,
2154 * we give an EOF when tracing is again disabled.
2156 * iter->pos will be 0 if we haven't read anything.
2158 if (!tracer_enabled && iter->pos)
2164 /* stop when tracing is finished */
2165 if (trace_empty(iter))
2168 if (cnt >= PAGE_SIZE)
2169 cnt = PAGE_SIZE - 1;
2171 memset(iter, 0, sizeof(*iter));
2172 iter->tr = &global_trace;
2176 * We need to stop all tracing on all CPUS to read the
2177 * the next buffer. This is a bit expensive, but is
2178 * not done often. We fill all what we can read,
2179 * and then release the locks again.
2183 local_irq_save(flags);
2184 #ifdef CONFIG_FTRACE
2185 ftrace_save = ftrace_enabled;
2189 for_each_possible_cpu(cpu) {
2190 data = iter->tr->data[cpu];
2192 if (!head_page(data) || !data->trace_idx)
2195 atomic_inc(&data->disabled);
2199 for_each_cpu_mask(cpu, mask) {
2200 data = iter->tr->data[cpu];
2201 spin_lock(&data->lock);
2204 while (find_next_entry_inc(iter) != NULL) {
2205 int len = iter->seq.len;
2207 ret = print_trace_line(iter);
2209 /* don't print partial lines */
2210 iter->seq.len = len;
2214 trace_consume(iter);
2216 if (iter->seq.len >= cnt)
2220 for_each_cpu_mask(cpu, mask) {
2221 data = iter->tr->data[cpu];
2222 spin_unlock(&data->lock);
2225 for_each_cpu_mask(cpu, mask) {
2226 data = iter->tr->data[cpu];
2227 atomic_dec(&data->disabled);
2229 #ifdef CONFIG_FTRACE
2230 ftrace_enabled = ftrace_save;
2232 local_irq_restore(flags);
2234 /* Now copy what we have to the user */
2235 read = iter->seq.len;
2239 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2241 if (read < iter->seq.len)
2244 trace_seq_reset(&iter->seq);
2252 static struct file_operations tracing_max_lat_fops = {
2253 .open = tracing_open_generic,
2254 .read = tracing_max_lat_read,
2255 .write = tracing_max_lat_write,
2258 static struct file_operations tracing_ctrl_fops = {
2259 .open = tracing_open_generic,
2260 .read = tracing_ctrl_read,
2261 .write = tracing_ctrl_write,
2264 static struct file_operations set_tracer_fops = {
2265 .open = tracing_open_generic,
2266 .read = tracing_set_trace_read,
2267 .write = tracing_set_trace_write,
2270 static struct file_operations tracing_pipe_fops = {
2271 .open = tracing_open_pipe,
2272 .poll = tracing_poll_pipe,
2273 .read = tracing_read_pipe,
2274 .release = tracing_release_pipe,
2277 #ifdef CONFIG_DYNAMIC_FTRACE
2280 tracing_read_long(struct file *filp, char __user *ubuf,
2281 size_t cnt, loff_t *ppos)
2283 unsigned long *p = filp->private_data;
2287 r = sprintf(buf, "%ld\n", *p);
2289 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2292 static struct file_operations tracing_read_long_fops = {
2293 .open = tracing_open_generic,
2294 .read = tracing_read_long,
2298 static struct dentry *d_tracer;
2300 struct dentry *tracing_init_dentry(void)
2307 d_tracer = debugfs_create_dir("tracing", NULL);
2309 if (!d_tracer && !once) {
2311 pr_warning("Could not create debugfs directory 'tracing'\n");
2318 #ifdef CONFIG_FTRACE_SELFTEST
2319 /* Let selftest have access to static functions in this file */
2320 #include "trace_selftest.c"
2323 static __init void tracer_init_debugfs(void)
2325 struct dentry *d_tracer;
2326 struct dentry *entry;
2328 d_tracer = tracing_init_dentry();
2330 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2331 &global_trace, &tracing_ctrl_fops);
2333 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2335 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2336 NULL, &tracing_iter_fops);
2338 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2340 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2341 &global_trace, &tracing_lt_fops);
2343 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2345 entry = debugfs_create_file("trace", 0444, d_tracer,
2346 &global_trace, &tracing_fops);
2348 pr_warning("Could not create debugfs 'trace' entry\n");
2350 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2351 &global_trace, &show_traces_fops);
2353 pr_warning("Could not create debugfs 'trace' entry\n");
2355 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2356 &global_trace, &set_tracer_fops);
2358 pr_warning("Could not create debugfs 'trace' entry\n");
2360 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2361 &tracing_max_latency,
2362 &tracing_max_lat_fops);
2364 pr_warning("Could not create debugfs "
2365 "'tracing_max_latency' entry\n");
2367 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2368 &tracing_thresh, &tracing_max_lat_fops);
2370 pr_warning("Could not create debugfs "
2371 "'tracing_threash' entry\n");
2372 entry = debugfs_create_file("README", 0644, d_tracer,
2373 NULL, &tracing_readme_fops);
2375 pr_warning("Could not create debugfs 'README' entry\n");
2377 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2378 NULL, &tracing_pipe_fops);
2380 pr_warning("Could not create debugfs "
2381 "'tracing_threash' entry\n");
2383 #ifdef CONFIG_DYNAMIC_FTRACE
2384 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2385 &ftrace_update_tot_cnt,
2386 &tracing_read_long_fops);
2388 pr_warning("Could not create debugfs "
2389 "'dyn_ftrace_total_info' entry\n");
2393 /* dummy trace to disable tracing */
2394 static struct tracer no_tracer __read_mostly =
2399 static int trace_alloc_page(void)
2401 struct trace_array_cpu *data;
2402 struct page *page, *tmp;
2407 /* first allocate a page for each CPU */
2408 for_each_possible_cpu(i) {
2409 array = (void *)__get_free_page(GFP_KERNEL);
2410 if (array == NULL) {
2411 printk(KERN_ERR "tracer: failed to allocate page"
2412 "for trace buffer!\n");
2416 page = virt_to_page(array);
2417 list_add(&page->lru, &pages);
2419 /* Only allocate if we are actually using the max trace */
2420 #ifdef CONFIG_TRACER_MAX_TRACE
2421 array = (void *)__get_free_page(GFP_KERNEL);
2422 if (array == NULL) {
2423 printk(KERN_ERR "tracer: failed to allocate page"
2424 "for trace buffer!\n");
2427 page = virt_to_page(array);
2428 list_add(&page->lru, &pages);
2432 /* Now that we successfully allocate a page per CPU, add them */
2433 for_each_possible_cpu(i) {
2434 data = global_trace.data[i];
2435 spin_lock_init(&data->lock);
2436 lockdep_set_class(&data->lock, &data->lock_key);
2437 page = list_entry(pages.next, struct page, lru);
2438 list_del_init(&page->lru);
2439 list_add_tail(&page->lru, &data->trace_pages);
2442 #ifdef CONFIG_TRACER_MAX_TRACE
2443 data = max_tr.data[i];
2444 spin_lock_init(&data->lock);
2445 lockdep_set_class(&data->lock, &data->lock_key);
2446 page = list_entry(pages.next, struct page, lru);
2447 list_del_init(&page->lru);
2448 list_add_tail(&page->lru, &data->trace_pages);
2452 global_trace.entries += ENTRIES_PER_PAGE;
2457 list_for_each_entry_safe(page, tmp, &pages, lru) {
2458 list_del_init(&page->lru);
2464 __init static int tracer_alloc_buffers(void)
2466 struct trace_array_cpu *data;
2473 global_trace.ctrl = tracer_enabled;
2475 /* Allocate the first page for all buffers */
2476 for_each_possible_cpu(i) {
2477 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2478 max_tr.data[i] = &per_cpu(max_data, i);
2480 array = (void *)__get_free_page(GFP_KERNEL);
2481 if (array == NULL) {
2482 printk(KERN_ERR "tracer: failed to allocate page"
2483 "for trace buffer!\n");
2487 /* set the array to the list */
2488 INIT_LIST_HEAD(&data->trace_pages);
2489 page = virt_to_page(array);
2490 list_add(&page->lru, &data->trace_pages);
2491 /* use the LRU flag to differentiate the two buffers */
2494 /* Only allocate if we are actually using the max trace */
2495 #ifdef CONFIG_TRACER_MAX_TRACE
2496 array = (void *)__get_free_page(GFP_KERNEL);
2497 if (array == NULL) {
2498 printk(KERN_ERR "tracer: failed to allocate page"
2499 "for trace buffer!\n");
2503 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2504 page = virt_to_page(array);
2505 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2511 * Since we allocate by orders of pages, we may be able to
2514 global_trace.entries = ENTRIES_PER_PAGE;
2517 while (global_trace.entries < trace_nr_entries) {
2518 if (trace_alloc_page())
2522 max_tr.entries = global_trace.entries;
2524 pr_info("tracer: %d pages allocated for %ld",
2525 pages, trace_nr_entries);
2526 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2527 pr_info(" actual entries %ld\n", global_trace.entries);
2529 tracer_init_debugfs();
2531 trace_init_cmdlines();
2533 register_tracer(&no_tracer);
2534 current_trace = &no_tracer;
2536 /* All seems OK, enable tracing */
2537 tracing_disabled = 0;
2542 for (i-- ; i >= 0; i--) {
2543 struct page *page, *tmp;
2544 struct trace_array_cpu *data = global_trace.data[i];
2547 list_for_each_entry_safe(page, tmp,
2548 &data->trace_pages, lru) {
2549 list_del_init(&page->lru);
2554 #ifdef CONFIG_TRACER_MAX_TRACE
2555 data = max_tr.data[i];
2557 list_for_each_entry_safe(page, tmp,
2558 &data->trace_pages, lru) {
2559 list_del_init(&page->lru);
2567 fs_initcall(tracer_alloc_buffers);