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);
674 __trace_special(void *__tr, void *__data,
675 unsigned long arg1, unsigned long arg2, unsigned long arg3)
677 struct trace_array_cpu *data = __data;
678 struct trace_array *tr = __tr;
679 struct trace_entry *entry;
680 unsigned long irq_flags;
682 spin_lock_irqsave(&data->lock, irq_flags);
683 entry = tracing_get_trace_entry(tr, data);
684 tracing_generic_entry_update(entry, 0);
685 entry->type = TRACE_SPECIAL;
686 entry->special.arg1 = arg1;
687 entry->special.arg2 = arg2;
688 entry->special.arg3 = arg3;
689 spin_unlock_irqrestore(&data->lock, irq_flags);
694 void __trace_stack(struct trace_array *tr,
695 struct trace_array_cpu *data,
699 struct trace_entry *entry;
700 struct stack_trace trace;
702 if (!(trace_flags & TRACE_ITER_STACKTRACE))
705 entry = tracing_get_trace_entry(tr, data);
706 tracing_generic_entry_update(entry, flags);
707 entry->type = TRACE_STACK;
709 memset(&entry->stack, 0, sizeof(entry->stack));
711 trace.nr_entries = 0;
712 trace.max_entries = FTRACE_STACK_ENTRIES;
714 trace.entries = entry->stack.caller;
716 save_stack_trace(&trace);
720 tracing_sched_switch_trace(struct trace_array *tr,
721 struct trace_array_cpu *data,
722 struct task_struct *prev,
723 struct task_struct *next,
726 struct trace_entry *entry;
727 unsigned long irq_flags;
729 spin_lock_irqsave(&data->lock, irq_flags);
730 entry = tracing_get_trace_entry(tr, data);
731 tracing_generic_entry_update(entry, flags);
732 entry->type = TRACE_CTX;
733 entry->ctx.prev_pid = prev->pid;
734 entry->ctx.prev_prio = prev->prio;
735 entry->ctx.prev_state = prev->state;
736 entry->ctx.next_pid = next->pid;
737 entry->ctx.next_prio = next->prio;
738 __trace_stack(tr, data, flags, 4);
739 spin_unlock_irqrestore(&data->lock, irq_flags);
743 tracing_sched_wakeup_trace(struct trace_array *tr,
744 struct trace_array_cpu *data,
745 struct task_struct *wakee,
746 struct task_struct *curr,
749 struct trace_entry *entry;
750 unsigned long irq_flags;
752 spin_lock_irqsave(&data->lock, irq_flags);
753 entry = tracing_get_trace_entry(tr, data);
754 tracing_generic_entry_update(entry, flags);
755 entry->type = TRACE_WAKE;
756 entry->ctx.prev_pid = curr->pid;
757 entry->ctx.prev_prio = curr->prio;
758 entry->ctx.prev_state = curr->state;
759 entry->ctx.next_pid = wakee->pid;
760 entry->ctx.next_prio = wakee->prio;
761 __trace_stack(tr, data, flags, 5);
762 spin_unlock_irqrestore(&data->lock, irq_flags);
769 function_trace_call(unsigned long ip, unsigned long parent_ip)
771 struct trace_array *tr = &global_trace;
772 struct trace_array_cpu *data;
777 if (unlikely(!tracer_enabled))
780 local_irq_save(flags);
781 cpu = raw_smp_processor_id();
782 data = tr->data[cpu];
783 disabled = atomic_inc_return(&data->disabled);
785 if (likely(disabled == 1))
786 trace_function(tr, data, ip, parent_ip, flags);
788 atomic_dec(&data->disabled);
789 local_irq_restore(flags);
792 static struct ftrace_ops trace_ops __read_mostly =
794 .func = function_trace_call,
797 void tracing_start_function_trace(void)
799 register_ftrace_function(&trace_ops);
802 void tracing_stop_function_trace(void)
804 unregister_ftrace_function(&trace_ops);
808 enum trace_file_type {
809 TRACE_FILE_LAT_FMT = 1,
812 static struct trace_entry *
813 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
814 struct trace_iterator *iter, int cpu)
817 struct trace_entry *array;
819 if (iter->next_idx[cpu] >= tr->entries ||
820 iter->next_idx[cpu] >= data->trace_idx ||
821 (data->trace_head == data->trace_tail &&
822 data->trace_head_idx == data->trace_tail_idx))
825 if (!iter->next_page[cpu]) {
826 /* Initialize the iterator for this cpu trace buffer */
827 WARN_ON(!data->trace_tail);
828 page = virt_to_page(data->trace_tail);
829 iter->next_page[cpu] = &page->lru;
830 iter->next_page_idx[cpu] = data->trace_tail_idx;
833 page = list_entry(iter->next_page[cpu], struct page, lru);
834 BUG_ON(&data->trace_pages == &page->lru);
836 array = page_address(page);
838 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
839 return &array[iter->next_page_idx[cpu]];
842 static struct trace_entry *
843 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
845 struct trace_array *tr = iter->tr;
846 struct trace_entry *ent, *next = NULL;
850 for_each_possible_cpu(cpu) {
851 if (!head_page(tr->data[cpu]))
853 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
855 * Pick the entry with the smallest timestamp:
857 if (ent && (!next || ent->t < next->t)) {
869 static void trace_iterator_increment(struct trace_iterator *iter)
872 iter->next_idx[iter->cpu]++;
873 iter->next_page_idx[iter->cpu]++;
875 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
876 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
878 iter->next_page_idx[iter->cpu] = 0;
879 iter->next_page[iter->cpu] =
880 trace_next_list(data, iter->next_page[iter->cpu]);
884 static void trace_consume(struct trace_iterator *iter)
886 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
888 data->trace_tail_idx++;
889 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
890 data->trace_tail = trace_next_page(data, data->trace_tail);
891 data->trace_tail_idx = 0;
894 /* Check if we empty it, then reset the index */
895 if (data->trace_head == data->trace_tail &&
896 data->trace_head_idx == data->trace_tail_idx)
900 static void *find_next_entry_inc(struct trace_iterator *iter)
902 struct trace_entry *next;
905 next = find_next_entry(iter, &next_cpu);
907 iter->prev_ent = iter->ent;
908 iter->prev_cpu = iter->cpu;
911 iter->cpu = next_cpu;
914 trace_iterator_increment(iter);
916 return next ? iter : NULL;
919 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
921 struct trace_iterator *iter = m->private;
922 void *last_ent = iter->ent;
928 /* can't go backwards */
933 ent = find_next_entry_inc(iter);
937 while (ent && iter->idx < i)
938 ent = find_next_entry_inc(iter);
942 if (last_ent && !ent)
943 seq_puts(m, "\n\nvim:ft=help\n");
948 static void *s_start(struct seq_file *m, loff_t *pos)
950 struct trace_iterator *iter = m->private;
955 mutex_lock(&trace_types_lock);
957 if (!current_trace || current_trace != iter->trace)
960 atomic_inc(&trace_record_cmdline_disabled);
962 /* let the tracer grab locks here if needed */
963 if (current_trace->start)
964 current_trace->start(iter);
966 if (*pos != iter->pos) {
970 iter->prev_ent = NULL;
973 for_each_possible_cpu(i) {
974 iter->next_idx[i] = 0;
975 iter->next_page[i] = NULL;
978 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
983 p = s_next(m, p, &l);
989 static void s_stop(struct seq_file *m, void *p)
991 struct trace_iterator *iter = m->private;
993 atomic_dec(&trace_record_cmdline_disabled);
995 /* let the tracer release locks here if needed */
996 if (current_trace && current_trace == iter->trace && iter->trace->stop)
997 iter->trace->stop(iter);
999 mutex_unlock(&trace_types_lock);
1003 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1005 #ifdef CONFIG_KALLSYMS
1006 char str[KSYM_SYMBOL_LEN];
1008 kallsyms_lookup(address, NULL, NULL, NULL, str);
1010 return trace_seq_printf(s, fmt, str);
1016 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1017 unsigned long address)
1019 #ifdef CONFIG_KALLSYMS
1020 char str[KSYM_SYMBOL_LEN];
1022 sprint_symbol(str, address);
1023 return trace_seq_printf(s, fmt, str);
1028 #ifndef CONFIG_64BIT
1029 # define IP_FMT "%08lx"
1031 # define IP_FMT "%016lx"
1035 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1040 return trace_seq_printf(s, "0");
1042 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1043 ret = seq_print_sym_offset(s, "%s", ip);
1045 ret = seq_print_sym_short(s, "%s", ip);
1050 if (sym_flags & TRACE_ITER_SYM_ADDR)
1051 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1055 static void print_lat_help_header(struct seq_file *m)
1057 seq_puts(m, "# _------=> CPU# \n");
1058 seq_puts(m, "# / _-----=> irqs-off \n");
1059 seq_puts(m, "# | / _----=> need-resched \n");
1060 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1061 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1062 seq_puts(m, "# |||| / \n");
1063 seq_puts(m, "# ||||| delay \n");
1064 seq_puts(m, "# cmd pid ||||| time | caller \n");
1065 seq_puts(m, "# \\ / ||||| \\ | / \n");
1068 static void print_func_help_header(struct seq_file *m)
1070 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1071 seq_puts(m, "# | | | | |\n");
1076 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1078 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1079 struct trace_array *tr = iter->tr;
1080 struct trace_array_cpu *data = tr->data[tr->cpu];
1081 struct tracer *type = current_trace;
1082 unsigned long total = 0;
1083 unsigned long entries = 0;
1085 const char *name = "preemption";
1090 for_each_possible_cpu(cpu) {
1091 if (head_page(tr->data[cpu])) {
1092 total += tr->data[cpu]->trace_idx;
1093 if (tr->data[cpu]->trace_idx > tr->entries)
1094 entries += tr->entries;
1096 entries += tr->data[cpu]->trace_idx;
1100 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1102 seq_puts(m, "-----------------------------------"
1103 "---------------------------------\n");
1104 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1105 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1106 nsecs_to_usecs(data->saved_latency),
1110 #if defined(CONFIG_PREEMPT_NONE)
1112 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1114 #elif defined(CONFIG_PREEMPT_DESKTOP)
1119 /* These are reserved for later use */
1122 seq_printf(m, " #P:%d)\n", num_online_cpus());
1126 seq_puts(m, " -----------------\n");
1127 seq_printf(m, " | task: %.16s-%d "
1128 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1129 data->comm, data->pid, data->uid, data->nice,
1130 data->policy, data->rt_priority);
1131 seq_puts(m, " -----------------\n");
1133 if (data->critical_start) {
1134 seq_puts(m, " => started at: ");
1135 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1136 trace_print_seq(m, &iter->seq);
1137 seq_puts(m, "\n => ended at: ");
1138 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1139 trace_print_seq(m, &iter->seq);
1147 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1149 int hardirq, softirq;
1152 comm = trace_find_cmdline(entry->pid);
1154 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1155 trace_seq_printf(s, "%d", cpu);
1156 trace_seq_printf(s, "%c%c",
1157 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1158 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1160 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1161 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1162 if (hardirq && softirq)
1163 trace_seq_putc(s, 'H');
1166 trace_seq_putc(s, 'h');
1169 trace_seq_putc(s, 's');
1171 trace_seq_putc(s, '.');
1175 if (entry->preempt_count)
1176 trace_seq_printf(s, "%x", entry->preempt_count);
1178 trace_seq_puts(s, ".");
1181 unsigned long preempt_mark_thresh = 100;
1184 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1185 unsigned long rel_usecs)
1187 trace_seq_printf(s, " %4lldus", abs_usecs);
1188 if (rel_usecs > preempt_mark_thresh)
1189 trace_seq_puts(s, "!: ");
1190 else if (rel_usecs > 1)
1191 trace_seq_puts(s, "+: ");
1193 trace_seq_puts(s, " : ");
1196 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1199 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1201 struct trace_seq *s = &iter->seq;
1202 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1203 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1204 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1205 struct trace_entry *entry = iter->ent;
1206 unsigned long abs_usecs;
1207 unsigned long rel_usecs;
1214 rel_usecs = ns2usecs(next_entry->t - entry->t);
1215 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1218 comm = trace_find_cmdline(entry->pid);
1219 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1220 " %ld.%03ldms (+%ld.%03ldms): ",
1222 entry->pid, cpu, entry->flags,
1223 entry->preempt_count, trace_idx,
1226 abs_usecs % 1000, rel_usecs/1000,
1229 if (entry->type != TRACE_STACK) {
1230 lat_print_generic(s, entry, cpu);
1231 lat_print_timestamp(s, abs_usecs, rel_usecs);
1234 switch (entry->type) {
1236 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1237 trace_seq_puts(s, " (");
1238 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1239 trace_seq_puts(s, ")\n");
1243 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1244 state_to_char[entry->ctx.prev_state] : 'X';
1245 comm = trace_find_cmdline(entry->ctx.next_pid);
1246 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
1247 entry->ctx.prev_pid,
1248 entry->ctx.prev_prio,
1249 S, entry->type == TRACE_CTX ? "==>" : " +",
1250 entry->ctx.next_pid,
1251 entry->ctx.next_prio,
1255 trace_seq_printf(s, " %ld %ld %ld\n",
1256 entry->special.arg1,
1257 entry->special.arg2,
1258 entry->special.arg3);
1261 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1263 trace_seq_puts(s, " <= ");
1264 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1266 trace_seq_puts(s, "\n");
1269 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1274 static int print_trace_fmt(struct trace_iterator *iter)
1276 struct trace_seq *s = &iter->seq;
1277 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1278 struct trace_entry *entry;
1279 unsigned long usec_rem;
1280 unsigned long long t;
1289 comm = trace_find_cmdline(iter->ent->pid);
1291 t = ns2usecs(entry->t);
1292 usec_rem = do_div(t, 1000000ULL);
1293 secs = (unsigned long)t;
1295 if (entry->type != TRACE_STACK) {
1296 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1299 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1302 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1307 switch (entry->type) {
1309 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1312 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1313 entry->fn.parent_ip) {
1314 ret = trace_seq_printf(s, " <-");
1317 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1322 ret = trace_seq_printf(s, "\n");
1328 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1329 state_to_char[entry->ctx.prev_state] : 'X';
1330 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
1331 entry->ctx.prev_pid,
1332 entry->ctx.prev_prio,
1334 entry->type == TRACE_CTX ? "==>" : " +",
1335 entry->ctx.next_pid,
1336 entry->ctx.next_prio);
1341 ret = trace_seq_printf(s, " %ld %ld %ld\n",
1342 entry->special.arg1,
1343 entry->special.arg2,
1344 entry->special.arg3);
1349 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1351 ret = trace_seq_puts(s, " <= ");
1355 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1360 ret = trace_seq_puts(s, "\n");
1368 static int print_raw_fmt(struct trace_iterator *iter)
1370 struct trace_seq *s = &iter->seq;
1371 struct trace_entry *entry;
1377 ret = trace_seq_printf(s, "%d %d %llu ",
1378 entry->pid, iter->cpu, entry->t);
1382 switch (entry->type) {
1384 ret = trace_seq_printf(s, "%x %x\n",
1385 entry->fn.ip, entry->fn.parent_ip);
1391 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1392 state_to_char[entry->ctx.prev_state] : 'X';
1393 if (entry->type == TRACE_WAKE)
1395 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1396 entry->ctx.prev_pid,
1397 entry->ctx.prev_prio,
1399 entry->ctx.next_pid,
1400 entry->ctx.next_prio);
1406 ret = trace_seq_printf(s, " %ld %ld %ld\n",
1407 entry->special.arg1,
1408 entry->special.arg2,
1409 entry->special.arg3);
1417 #define SEQ_PUT_FIELD_RET(s, x) \
1419 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1423 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1425 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1429 static int print_hex_fmt(struct trace_iterator *iter)
1431 struct trace_seq *s = &iter->seq;
1432 unsigned char newline = '\n';
1433 struct trace_entry *entry;
1438 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1439 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1440 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1442 switch (entry->type) {
1444 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1445 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1449 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1450 state_to_char[entry->ctx.prev_state] : 'X';
1451 if (entry->type == TRACE_WAKE)
1453 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1454 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1455 SEQ_PUT_HEX_FIELD_RET(s, S);
1456 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1457 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1458 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1462 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1463 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1464 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1467 SEQ_PUT_FIELD_RET(s, newline);
1472 static int print_bin_fmt(struct trace_iterator *iter)
1474 struct trace_seq *s = &iter->seq;
1475 struct trace_entry *entry;
1479 SEQ_PUT_FIELD_RET(s, entry->pid);
1480 SEQ_PUT_FIELD_RET(s, entry->cpu);
1481 SEQ_PUT_FIELD_RET(s, entry->t);
1483 switch (entry->type) {
1485 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1486 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1489 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1490 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1491 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1492 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1493 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1497 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1498 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1499 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1505 static int trace_empty(struct trace_iterator *iter)
1507 struct trace_array_cpu *data;
1510 for_each_possible_cpu(cpu) {
1511 data = iter->tr->data[cpu];
1513 if (head_page(data) && data->trace_idx &&
1514 (data->trace_tail != data->trace_head ||
1515 data->trace_tail_idx != data->trace_head_idx))
1521 static int print_trace_line(struct trace_iterator *iter)
1523 if (trace_flags & TRACE_ITER_BIN)
1524 return print_bin_fmt(iter);
1526 if (trace_flags & TRACE_ITER_HEX)
1527 return print_hex_fmt(iter);
1529 if (trace_flags & TRACE_ITER_RAW)
1530 return print_raw_fmt(iter);
1532 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1533 return print_lat_fmt(iter, iter->idx, iter->cpu);
1535 return print_trace_fmt(iter);
1538 static int s_show(struct seq_file *m, void *v)
1540 struct trace_iterator *iter = v;
1542 if (iter->ent == NULL) {
1544 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1547 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1548 /* print nothing if the buffers are empty */
1549 if (trace_empty(iter))
1551 print_trace_header(m, iter);
1552 if (!(trace_flags & TRACE_ITER_VERBOSE))
1553 print_lat_help_header(m);
1555 if (!(trace_flags & TRACE_ITER_VERBOSE))
1556 print_func_help_header(m);
1559 print_trace_line(iter);
1560 trace_print_seq(m, &iter->seq);
1566 static struct seq_operations tracer_seq_ops = {
1573 static struct trace_iterator *
1574 __tracing_open(struct inode *inode, struct file *file, int *ret)
1576 struct trace_iterator *iter;
1578 if (tracing_disabled) {
1583 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1589 mutex_lock(&trace_types_lock);
1590 if (current_trace && current_trace->print_max)
1593 iter->tr = inode->i_private;
1594 iter->trace = current_trace;
1597 /* TODO stop tracer */
1598 *ret = seq_open(file, &tracer_seq_ops);
1600 struct seq_file *m = file->private_data;
1603 /* stop the trace while dumping */
1607 if (iter->trace && iter->trace->open)
1608 iter->trace->open(iter);
1613 mutex_unlock(&trace_types_lock);
1619 int tracing_open_generic(struct inode *inode, struct file *filp)
1621 if (tracing_disabled)
1624 filp->private_data = inode->i_private;
1628 int tracing_release(struct inode *inode, struct file *file)
1630 struct seq_file *m = (struct seq_file *)file->private_data;
1631 struct trace_iterator *iter = m->private;
1633 mutex_lock(&trace_types_lock);
1634 if (iter->trace && iter->trace->close)
1635 iter->trace->close(iter);
1637 /* reenable tracing if it was previously enabled */
1640 mutex_unlock(&trace_types_lock);
1642 seq_release(inode, file);
1647 static int tracing_open(struct inode *inode, struct file *file)
1651 __tracing_open(inode, file, &ret);
1656 static int tracing_lt_open(struct inode *inode, struct file *file)
1658 struct trace_iterator *iter;
1661 iter = __tracing_open(inode, file, &ret);
1664 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1671 t_next(struct seq_file *m, void *v, loff_t *pos)
1673 struct tracer *t = m->private;
1685 static void *t_start(struct seq_file *m, loff_t *pos)
1687 struct tracer *t = m->private;
1690 mutex_lock(&trace_types_lock);
1691 for (; t && l < *pos; t = t_next(m, t, &l))
1697 static void t_stop(struct seq_file *m, void *p)
1699 mutex_unlock(&trace_types_lock);
1702 static int t_show(struct seq_file *m, void *v)
1704 struct tracer *t = v;
1709 seq_printf(m, "%s", t->name);
1718 static struct seq_operations show_traces_seq_ops = {
1725 static int show_traces_open(struct inode *inode, struct file *file)
1729 if (tracing_disabled)
1732 ret = seq_open(file, &show_traces_seq_ops);
1734 struct seq_file *m = file->private_data;
1735 m->private = trace_types;
1741 static struct file_operations tracing_fops = {
1742 .open = tracing_open,
1744 .llseek = seq_lseek,
1745 .release = tracing_release,
1748 static struct file_operations tracing_lt_fops = {
1749 .open = tracing_lt_open,
1751 .llseek = seq_lseek,
1752 .release = tracing_release,
1755 static struct file_operations show_traces_fops = {
1756 .open = show_traces_open,
1758 .release = seq_release,
1762 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1763 size_t cnt, loff_t *ppos)
1770 /* calulate max size */
1771 for (i = 0; trace_options[i]; i++) {
1772 len += strlen(trace_options[i]);
1773 len += 3; /* "no" and space */
1776 /* +2 for \n and \0 */
1777 buf = kmalloc(len + 2, GFP_KERNEL);
1781 for (i = 0; trace_options[i]; i++) {
1782 if (trace_flags & (1 << i))
1783 r += sprintf(buf + r, "%s ", trace_options[i]);
1785 r += sprintf(buf + r, "no%s ", trace_options[i]);
1788 r += sprintf(buf + r, "\n");
1789 WARN_ON(r >= len + 2);
1791 r = simple_read_from_buffer(ubuf, cnt, ppos,
1800 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1801 size_t cnt, loff_t *ppos)
1811 if (copy_from_user(&buf, ubuf, cnt))
1816 if (strncmp(buf, "no", 2) == 0) {
1821 for (i = 0; trace_options[i]; i++) {
1822 int len = strlen(trace_options[i]);
1824 if (strncmp(cmp, trace_options[i], len) == 0) {
1826 trace_flags &= ~(1 << i);
1828 trace_flags |= (1 << i);
1838 static struct file_operations tracing_iter_fops = {
1839 .open = tracing_open_generic,
1840 .read = tracing_iter_ctrl_read,
1841 .write = tracing_iter_ctrl_write,
1844 static const char readme_msg[] =
1845 "tracing mini-HOWTO:\n\n"
1847 "# mount -t debugfs nodev /debug\n\n"
1848 "# cat /debug/tracing/available_tracers\n"
1849 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1850 "# cat /debug/tracing/current_tracer\n"
1852 "# echo sched_switch > /debug/tracing/current_tracer\n"
1853 "# cat /debug/tracing/current_tracer\n"
1855 "# cat /debug/tracing/iter_ctrl\n"
1856 "noprint-parent nosym-offset nosym-addr noverbose\n"
1857 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1858 "# echo 1 > /debug/tracing/tracing_enabled\n"
1859 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1860 "echo 0 > /debug/tracing/tracing_enabled\n"
1864 tracing_readme_read(struct file *filp, char __user *ubuf,
1865 size_t cnt, loff_t *ppos)
1867 return simple_read_from_buffer(ubuf, cnt, ppos,
1868 readme_msg, strlen(readme_msg));
1871 static struct file_operations tracing_readme_fops = {
1872 .open = tracing_open_generic,
1873 .read = tracing_readme_read,
1877 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1878 size_t cnt, loff_t *ppos)
1880 struct trace_array *tr = filp->private_data;
1884 r = sprintf(buf, "%ld\n", tr->ctrl);
1885 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1889 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1890 size_t cnt, loff_t *ppos)
1892 struct trace_array *tr = filp->private_data;
1899 if (copy_from_user(&buf, ubuf, cnt))
1904 val = simple_strtoul(buf, NULL, 10);
1908 mutex_lock(&trace_types_lock);
1909 if (tr->ctrl ^ val) {
1917 if (current_trace && current_trace->ctrl_update)
1918 current_trace->ctrl_update(tr);
1920 mutex_unlock(&trace_types_lock);
1928 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1929 size_t cnt, loff_t *ppos)
1931 char buf[max_tracer_type_len+2];
1934 mutex_lock(&trace_types_lock);
1936 r = sprintf(buf, "%s\n", current_trace->name);
1938 r = sprintf(buf, "\n");
1939 mutex_unlock(&trace_types_lock);
1941 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1945 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1946 size_t cnt, loff_t *ppos)
1948 struct trace_array *tr = &global_trace;
1950 char buf[max_tracer_type_len+1];
1953 if (cnt > max_tracer_type_len)
1954 cnt = max_tracer_type_len;
1956 if (copy_from_user(&buf, ubuf, cnt))
1961 /* strip ending whitespace. */
1962 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1965 mutex_lock(&trace_types_lock);
1966 for (t = trace_types; t; t = t->next) {
1967 if (strcmp(t->name, buf) == 0)
1970 if (!t || t == current_trace)
1973 if (current_trace && current_trace->reset)
1974 current_trace->reset(tr);
1981 mutex_unlock(&trace_types_lock);
1989 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1990 size_t cnt, loff_t *ppos)
1992 unsigned long *ptr = filp->private_data;
1996 r = snprintf(buf, 64, "%ld\n",
1997 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2000 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2004 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2005 size_t cnt, loff_t *ppos)
2007 long *ptr = filp->private_data;
2014 if (copy_from_user(&buf, ubuf, cnt))
2019 val = simple_strtoul(buf, NULL, 10);
2026 static atomic_t tracing_reader;
2028 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2030 struct trace_iterator *iter;
2032 if (tracing_disabled)
2035 /* We only allow for reader of the pipe */
2036 if (atomic_inc_return(&tracing_reader) != 1) {
2037 atomic_dec(&tracing_reader);
2041 /* create a buffer to store the information to pass to userspace */
2042 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2046 iter->tr = &global_trace;
2048 filp->private_data = iter;
2053 static int tracing_release_pipe(struct inode *inode, struct file *file)
2055 struct trace_iterator *iter = file->private_data;
2058 atomic_dec(&tracing_reader);
2064 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2066 struct trace_iterator *iter = filp->private_data;
2068 if (trace_flags & TRACE_ITER_BLOCK) {
2070 * Always select as readable when in blocking mode
2072 return POLLIN | POLLRDNORM;
2075 if (!trace_empty(iter))
2076 return POLLIN | POLLRDNORM;
2077 poll_wait(filp, &trace_wait, poll_table);
2078 if (!trace_empty(iter))
2079 return POLLIN | POLLRDNORM;
2089 tracing_read_pipe(struct file *filp, char __user *ubuf,
2090 size_t cnt, loff_t *ppos)
2092 struct trace_iterator *iter = filp->private_data;
2093 struct trace_array_cpu *data;
2094 static cpumask_t mask;
2096 unsigned long flags;
2097 #ifdef CONFIG_FTRACE
2105 /* return any leftover data */
2106 if (iter->seq.len > start) {
2107 len = iter->seq.len - start;
2110 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2119 trace_seq_reset(&iter->seq);
2122 while (trace_empty(iter)) {
2123 if (!(trace_flags & TRACE_ITER_BLOCK))
2124 return -EWOULDBLOCK;
2126 * This is a make-shift waitqueue. The reason we don't use
2127 * an actual wait queue is because:
2128 * 1) we only ever have one waiter
2129 * 2) the tracing, traces all functions, we don't want
2130 * the overhead of calling wake_up and friends
2131 * (and tracing them too)
2132 * Anyway, this is really very primitive wakeup.
2134 set_current_state(TASK_INTERRUPTIBLE);
2135 iter->tr->waiter = current;
2137 /* sleep for one second, and try again. */
2138 schedule_timeout(HZ);
2140 iter->tr->waiter = NULL;
2142 if (signal_pending(current))
2146 * We block until we read something and tracing is disabled.
2147 * We still block if tracing is disabled, but we have never
2148 * read anything. This allows a user to cat this file, and
2149 * then enable tracing. But after we have read something,
2150 * we give an EOF when tracing is again disabled.
2152 * iter->pos will be 0 if we haven't read anything.
2154 if (!tracer_enabled && iter->pos)
2160 /* stop when tracing is finished */
2161 if (trace_empty(iter))
2164 if (cnt >= PAGE_SIZE)
2165 cnt = PAGE_SIZE - 1;
2167 memset(iter, 0, sizeof(*iter));
2168 iter->tr = &global_trace;
2172 * We need to stop all tracing on all CPUS to read the
2173 * the next buffer. This is a bit expensive, but is
2174 * not done often. We fill all what we can read,
2175 * and then release the locks again.
2179 local_irq_save(flags);
2180 #ifdef CONFIG_FTRACE
2181 ftrace_save = ftrace_enabled;
2185 for_each_possible_cpu(cpu) {
2186 data = iter->tr->data[cpu];
2188 if (!head_page(data) || !data->trace_idx)
2191 atomic_inc(&data->disabled);
2195 for_each_cpu_mask(cpu, mask) {
2196 data = iter->tr->data[cpu];
2197 spin_lock(&data->lock);
2200 while (find_next_entry_inc(iter) != NULL) {
2201 int len = iter->seq.len;
2203 ret = print_trace_line(iter);
2205 /* don't print partial lines */
2206 iter->seq.len = len;
2210 trace_consume(iter);
2212 if (iter->seq.len >= cnt)
2216 for_each_cpu_mask(cpu, mask) {
2217 data = iter->tr->data[cpu];
2218 spin_unlock(&data->lock);
2221 for_each_cpu_mask(cpu, mask) {
2222 data = iter->tr->data[cpu];
2223 atomic_dec(&data->disabled);
2225 #ifdef CONFIG_FTRACE
2226 ftrace_enabled = ftrace_save;
2228 local_irq_restore(flags);
2230 /* Now copy what we have to the user */
2231 read = iter->seq.len;
2235 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2237 if (read < iter->seq.len)
2240 trace_seq_reset(&iter->seq);
2248 static struct file_operations tracing_max_lat_fops = {
2249 .open = tracing_open_generic,
2250 .read = tracing_max_lat_read,
2251 .write = tracing_max_lat_write,
2254 static struct file_operations tracing_ctrl_fops = {
2255 .open = tracing_open_generic,
2256 .read = tracing_ctrl_read,
2257 .write = tracing_ctrl_write,
2260 static struct file_operations set_tracer_fops = {
2261 .open = tracing_open_generic,
2262 .read = tracing_set_trace_read,
2263 .write = tracing_set_trace_write,
2266 static struct file_operations tracing_pipe_fops = {
2267 .open = tracing_open_pipe,
2268 .poll = tracing_poll_pipe,
2269 .read = tracing_read_pipe,
2270 .release = tracing_release_pipe,
2273 #ifdef CONFIG_DYNAMIC_FTRACE
2276 tracing_read_long(struct file *filp, char __user *ubuf,
2277 size_t cnt, loff_t *ppos)
2279 unsigned long *p = filp->private_data;
2283 r = sprintf(buf, "%ld\n", *p);
2285 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2288 static struct file_operations tracing_read_long_fops = {
2289 .open = tracing_open_generic,
2290 .read = tracing_read_long,
2294 static struct dentry *d_tracer;
2296 struct dentry *tracing_init_dentry(void)
2303 d_tracer = debugfs_create_dir("tracing", NULL);
2305 if (!d_tracer && !once) {
2307 pr_warning("Could not create debugfs directory 'tracing'\n");
2314 #ifdef CONFIG_FTRACE_SELFTEST
2315 /* Let selftest have access to static functions in this file */
2316 #include "trace_selftest.c"
2319 static __init void tracer_init_debugfs(void)
2321 struct dentry *d_tracer;
2322 struct dentry *entry;
2324 d_tracer = tracing_init_dentry();
2326 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2327 &global_trace, &tracing_ctrl_fops);
2329 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2331 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2332 NULL, &tracing_iter_fops);
2334 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2336 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2337 &global_trace, &tracing_lt_fops);
2339 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2341 entry = debugfs_create_file("trace", 0444, d_tracer,
2342 &global_trace, &tracing_fops);
2344 pr_warning("Could not create debugfs 'trace' entry\n");
2346 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2347 &global_trace, &show_traces_fops);
2349 pr_warning("Could not create debugfs 'trace' entry\n");
2351 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2352 &global_trace, &set_tracer_fops);
2354 pr_warning("Could not create debugfs 'trace' entry\n");
2356 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2357 &tracing_max_latency,
2358 &tracing_max_lat_fops);
2360 pr_warning("Could not create debugfs "
2361 "'tracing_max_latency' entry\n");
2363 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2364 &tracing_thresh, &tracing_max_lat_fops);
2366 pr_warning("Could not create debugfs "
2367 "'tracing_threash' entry\n");
2368 entry = debugfs_create_file("README", 0644, d_tracer,
2369 NULL, &tracing_readme_fops);
2371 pr_warning("Could not create debugfs 'README' entry\n");
2373 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2374 NULL, &tracing_pipe_fops);
2376 pr_warning("Could not create debugfs "
2377 "'tracing_threash' entry\n");
2379 #ifdef CONFIG_DYNAMIC_FTRACE
2380 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2381 &ftrace_update_tot_cnt,
2382 &tracing_read_long_fops);
2384 pr_warning("Could not create debugfs "
2385 "'dyn_ftrace_total_info' entry\n");
2389 /* dummy trace to disable tracing */
2390 static struct tracer no_tracer __read_mostly =
2395 static int trace_alloc_page(void)
2397 struct trace_array_cpu *data;
2398 struct page *page, *tmp;
2403 /* first allocate a page for each CPU */
2404 for_each_possible_cpu(i) {
2405 array = (void *)__get_free_page(GFP_KERNEL);
2406 if (array == NULL) {
2407 printk(KERN_ERR "tracer: failed to allocate page"
2408 "for trace buffer!\n");
2412 page = virt_to_page(array);
2413 list_add(&page->lru, &pages);
2415 /* Only allocate if we are actually using the max trace */
2416 #ifdef CONFIG_TRACER_MAX_TRACE
2417 array = (void *)__get_free_page(GFP_KERNEL);
2418 if (array == NULL) {
2419 printk(KERN_ERR "tracer: failed to allocate page"
2420 "for trace buffer!\n");
2423 page = virt_to_page(array);
2424 list_add(&page->lru, &pages);
2428 /* Now that we successfully allocate a page per CPU, add them */
2429 for_each_possible_cpu(i) {
2430 data = global_trace.data[i];
2431 spin_lock_init(&data->lock);
2432 lockdep_set_class(&data->lock, &data->lock_key);
2433 page = list_entry(pages.next, struct page, lru);
2434 list_del_init(&page->lru);
2435 list_add_tail(&page->lru, &data->trace_pages);
2438 #ifdef CONFIG_TRACER_MAX_TRACE
2439 data = max_tr.data[i];
2440 spin_lock_init(&data->lock);
2441 lockdep_set_class(&data->lock, &data->lock_key);
2442 page = list_entry(pages.next, struct page, lru);
2443 list_del_init(&page->lru);
2444 list_add_tail(&page->lru, &data->trace_pages);
2448 global_trace.entries += ENTRIES_PER_PAGE;
2453 list_for_each_entry_safe(page, tmp, &pages, lru) {
2454 list_del_init(&page->lru);
2460 __init static int tracer_alloc_buffers(void)
2462 struct trace_array_cpu *data;
2469 global_trace.ctrl = tracer_enabled;
2471 /* Allocate the first page for all buffers */
2472 for_each_possible_cpu(i) {
2473 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2474 max_tr.data[i] = &per_cpu(max_data, i);
2476 array = (void *)__get_free_page(GFP_KERNEL);
2477 if (array == NULL) {
2478 printk(KERN_ERR "tracer: failed to allocate page"
2479 "for trace buffer!\n");
2483 /* set the array to the list */
2484 INIT_LIST_HEAD(&data->trace_pages);
2485 page = virt_to_page(array);
2486 list_add(&page->lru, &data->trace_pages);
2487 /* use the LRU flag to differentiate the two buffers */
2490 /* Only allocate if we are actually using the max trace */
2491 #ifdef CONFIG_TRACER_MAX_TRACE
2492 array = (void *)__get_free_page(GFP_KERNEL);
2493 if (array == NULL) {
2494 printk(KERN_ERR "tracer: failed to allocate page"
2495 "for trace buffer!\n");
2499 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2500 page = virt_to_page(array);
2501 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2507 * Since we allocate by orders of pages, we may be able to
2510 global_trace.entries = ENTRIES_PER_PAGE;
2513 while (global_trace.entries < trace_nr_entries) {
2514 if (trace_alloc_page())
2518 max_tr.entries = global_trace.entries;
2520 pr_info("tracer: %d pages allocated for %ld",
2521 pages, trace_nr_entries);
2522 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2523 pr_info(" actual entries %ld\n", global_trace.entries);
2525 tracer_init_debugfs();
2527 trace_init_cmdlines();
2529 register_tracer(&no_tracer);
2530 current_trace = &no_tracer;
2532 /* All seems OK, enable tracing */
2533 tracing_disabled = 0;
2538 for (i-- ; i >= 0; i--) {
2539 struct page *page, *tmp;
2540 struct trace_array_cpu *data = global_trace.data[i];
2543 list_for_each_entry_safe(page, tmp,
2544 &data->trace_pages, lru) {
2545 list_del_init(&page->lru);
2550 #ifdef CONFIG_TRACER_MAX_TRACE
2551 data = max_tr.data[i];
2553 list_for_each_entry_safe(page, tmp,
2554 &data->trace_pages, lru) {
2555 list_del_init(&page->lru);
2563 fs_initcall(tracer_alloc_buffers);