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>
33 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
34 unsigned long __read_mostly tracing_thresh;
36 static int tracing_disabled = 1;
39 ns2usecs(cycle_t nsec)
46 cycle_t ftrace_now(int cpu)
48 return cpu_clock(cpu);
51 static struct trace_array global_trace;
53 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
55 static struct trace_array max_tr;
57 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
59 static int tracer_enabled = 1;
60 static unsigned long trace_nr_entries = 65536UL;
62 static struct tracer *trace_types __read_mostly;
63 static struct tracer *current_trace __read_mostly;
64 static int max_tracer_type_len;
66 static DEFINE_MUTEX(trace_types_lock);
67 static DECLARE_WAIT_QUEUE_HEAD (trace_wait);
69 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
71 static int __init set_nr_entries(char *str)
75 trace_nr_entries = simple_strtoul(str, &str, 0);
78 __setup("trace_entries=", set_nr_entries);
80 unsigned long nsecs_to_usecs(unsigned long nsecs)
86 __TRACE_FIRST_TYPE = 0,
96 enum trace_flag_type {
97 TRACE_FLAG_IRQS_OFF = 0x01,
98 TRACE_FLAG_NEED_RESCHED = 0x02,
99 TRACE_FLAG_HARDIRQ = 0x04,
100 TRACE_FLAG_SOFTIRQ = 0x08,
103 enum trace_iterator_flags {
104 TRACE_ITER_PRINT_PARENT = 0x01,
105 TRACE_ITER_SYM_OFFSET = 0x02,
106 TRACE_ITER_SYM_ADDR = 0x04,
107 TRACE_ITER_VERBOSE = 0x08,
108 TRACE_ITER_RAW = 0x10,
109 TRACE_ITER_HEX = 0x20,
110 TRACE_ITER_BIN = 0x40,
111 TRACE_ITER_BLOCK = 0x80,
114 #define TRACE_ITER_SYM_MASK \
115 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
117 /* These must match the bit postions above */
118 static const char *trace_options[] = {
130 static unsigned trace_flags;
132 static DEFINE_SPINLOCK(ftrace_max_lock);
135 * Copy the new maximum trace into the separate maximum-trace
136 * structure. (this way the maximum trace is permanently saved,
137 * for later retrieval via /debugfs/tracing/latency_trace)
140 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
142 struct trace_array_cpu *data = tr->data[cpu];
145 max_tr.time_start = data->preempt_timestamp;
147 data = max_tr.data[cpu];
148 data->saved_latency = tracing_max_latency;
150 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
151 data->pid = tsk->pid;
152 data->uid = tsk->uid;
153 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
154 data->policy = tsk->policy;
155 data->rt_priority = tsk->rt_priority;
157 /* record this tasks comm */
158 tracing_record_cmdline(current);
161 void check_pages(struct trace_array_cpu *data)
163 struct page *page, *tmp;
165 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
166 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
168 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
169 BUG_ON(page->lru.next->prev != &page->lru);
170 BUG_ON(page->lru.prev->next != &page->lru);
174 void *head_page(struct trace_array_cpu *data)
179 if (list_empty(&data->trace_pages))
182 page = list_entry(data->trace_pages.next, struct page, lru);
183 BUG_ON(&page->lru == &data->trace_pages);
185 return page_address(page);
189 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
191 int len = (PAGE_SIZE - 1) - s->len;
199 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
202 /* If we can't write it all, don't bother writing anything */
212 trace_seq_puts(struct trace_seq *s, const char *str)
214 int len = strlen(str);
216 if (len > ((PAGE_SIZE - 1) - s->len))
219 memcpy(s->buffer + s->len, str, len);
226 trace_seq_putc(struct trace_seq *s, unsigned char c)
228 if (s->len >= (PAGE_SIZE - 1))
231 s->buffer[s->len++] = c;
237 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
239 if (len > ((PAGE_SIZE - 1) - s->len))
242 memcpy(s->buffer + s->len, mem, len);
251 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
253 unsigned char hex[HEX_CHARS];
258 BUG_ON(len >= HEX_CHARS);
263 for (i = 0, j = 0; i < len; i++) {
265 for (i = len-1, j = 0; i >= 0; i--) {
269 hex[j] = byte & 0x0f;
286 return trace_seq_putmem(s, hex, j);
290 trace_seq_reset(struct trace_seq *s)
296 trace_print_seq(struct seq_file *m, struct trace_seq *s)
298 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
301 seq_puts(m, s->buffer);
307 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
309 struct list_head flip_pages;
311 INIT_LIST_HEAD(&flip_pages);
313 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
314 sizeof(struct trace_array_cpu) -
315 offsetof(struct trace_array_cpu, trace_head_idx));
319 list_splice_init(&tr1->trace_pages, &flip_pages);
320 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
321 list_splice_init(&flip_pages, &tr2->trace_pages);
322 BUG_ON(!list_empty(&flip_pages));
328 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
330 struct trace_array_cpu *data;
333 WARN_ON_ONCE(!irqs_disabled());
334 spin_lock(&ftrace_max_lock);
335 /* clear out all the previous traces */
336 for_each_possible_cpu(i) {
338 flip_trace(max_tr.data[i], data);
342 __update_max_tr(tr, tsk, cpu);
343 spin_unlock(&ftrace_max_lock);
347 * update_max_tr_single - only copy one trace over, and reset the rest
349 * @tsk - task with the latency
350 * @cpu - the cpu of the buffer to copy.
353 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
355 struct trace_array_cpu *data = tr->data[cpu];
358 WARN_ON_ONCE(!irqs_disabled());
359 spin_lock(&ftrace_max_lock);
360 for_each_possible_cpu(i)
361 tracing_reset(max_tr.data[i]);
363 flip_trace(max_tr.data[cpu], data);
366 __update_max_tr(tr, tsk, cpu);
367 spin_unlock(&ftrace_max_lock);
370 int register_tracer(struct tracer *type)
377 pr_info("Tracer must have a name\n");
381 mutex_lock(&trace_types_lock);
382 for (t = trace_types; t; t = t->next) {
383 if (strcmp(type->name, t->name) == 0) {
385 pr_info("Trace %s already registered\n",
392 #ifdef CONFIG_FTRACE_STARTUP_TEST
393 if (type->selftest) {
394 struct tracer *saved_tracer = current_trace;
395 struct trace_array_cpu *data;
396 struct trace_array *tr = &global_trace;
397 int saved_ctrl = tr->ctrl;
400 * Run a selftest on this tracer.
401 * Here we reset the trace buffer, and set the current
402 * tracer to be this tracer. The tracer can then run some
403 * internal tracing to verify that everything is in order.
404 * If we fail, we do not register this tracer.
406 for_each_possible_cpu(i) {
408 if (!head_page(data))
412 current_trace = type;
414 /* the test is responsible for initializing and enabling */
415 pr_info("Testing tracer %s: ", type->name);
416 ret = type->selftest(type, tr);
417 /* the test is responsible for resetting too */
418 current_trace = saved_tracer;
419 tr->ctrl = saved_ctrl;
421 printk(KERN_CONT "FAILED!\n");
424 /* Only reset on passing, to avoid touching corrupted buffers */
425 for_each_possible_cpu(i) {
427 if (!head_page(data))
431 printk(KERN_CONT "PASSED\n");
435 type->next = trace_types;
437 len = strlen(type->name);
438 if (len > max_tracer_type_len)
439 max_tracer_type_len = len;
442 mutex_unlock(&trace_types_lock);
447 void unregister_tracer(struct tracer *type)
452 mutex_lock(&trace_types_lock);
453 for (t = &trace_types; *t; t = &(*t)->next) {
457 pr_info("Trace %s not registered\n", type->name);
462 if (strlen(type->name) != max_tracer_type_len)
465 max_tracer_type_len = 0;
466 for (t = &trace_types; *t; t = &(*t)->next) {
467 len = strlen((*t)->name);
468 if (len > max_tracer_type_len)
469 max_tracer_type_len = len;
472 mutex_unlock(&trace_types_lock);
475 void tracing_reset(struct trace_array_cpu *data)
478 data->trace_head = data->trace_tail = head_page(data);
479 data->trace_head_idx = 0;
480 data->trace_tail_idx = 0;
483 #define SAVED_CMDLINES 128
484 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
485 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
486 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
487 static int cmdline_idx;
488 static DEFINE_SPINLOCK(trace_cmdline_lock);
489 atomic_t trace_record_cmdline_disabled;
491 static void trace_init_cmdlines(void)
493 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
494 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
498 void trace_stop_cmdline_recording(void);
500 static void trace_save_cmdline(struct task_struct *tsk)
505 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
509 * It's not the end of the world if we don't get
510 * the lock, but we also don't want to spin
511 * nor do we want to disable interrupts,
512 * so if we miss here, then better luck next time.
514 if (!spin_trylock(&trace_cmdline_lock))
517 idx = map_pid_to_cmdline[tsk->pid];
518 if (idx >= SAVED_CMDLINES) {
519 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
521 map = map_cmdline_to_pid[idx];
522 if (map <= PID_MAX_DEFAULT)
523 map_pid_to_cmdline[map] = (unsigned)-1;
525 map_pid_to_cmdline[tsk->pid] = idx;
530 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
532 spin_unlock(&trace_cmdline_lock);
535 static char *trace_find_cmdline(int pid)
537 char *cmdline = "<...>";
543 if (pid > PID_MAX_DEFAULT)
546 map = map_pid_to_cmdline[pid];
547 if (map >= SAVED_CMDLINES)
550 cmdline = saved_cmdlines[map];
556 void tracing_record_cmdline(struct task_struct *tsk)
558 if (atomic_read(&trace_record_cmdline_disabled))
561 trace_save_cmdline(tsk);
564 static inline struct list_head *
565 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
568 * Roundrobin - but skip the head (which is not a real page):
571 if (unlikely(next == &data->trace_pages))
573 BUG_ON(next == &data->trace_pages);
579 trace_next_page(struct trace_array_cpu *data, void *addr)
581 struct list_head *next;
584 page = virt_to_page(addr);
586 next = trace_next_list(data, &page->lru);
587 page = list_entry(next, struct page, lru);
589 return page_address(page);
592 static inline struct trace_entry *
593 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
595 unsigned long idx, idx_next;
596 struct trace_entry *entry;
599 idx = data->trace_head_idx;
602 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
604 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
606 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
607 data->trace_head = trace_next_page(data, data->trace_head);
611 if (data->trace_head == data->trace_tail &&
612 idx_next == data->trace_tail_idx) {
614 data->trace_tail_idx++;
615 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
617 trace_next_page(data, data->trace_tail);
618 data->trace_tail_idx = 0;
622 data->trace_head_idx = idx_next;
628 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
630 struct task_struct *tsk = current;
633 pc = preempt_count();
635 entry->preempt_count = pc & 0xff;
636 entry->pid = tsk->pid;
637 entry->t = ftrace_now(raw_smp_processor_id());
638 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
639 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
640 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
641 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
645 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
646 unsigned long ip, unsigned long parent_ip, unsigned long flags)
648 struct trace_entry *entry;
649 unsigned long irq_flags;
651 spin_lock_irqsave(&data->lock, irq_flags);
652 entry = tracing_get_trace_entry(tr, data);
653 tracing_generic_entry_update(entry, flags);
654 entry->type = TRACE_FN;
656 entry->fn.parent_ip = parent_ip;
657 spin_unlock_irqrestore(&data->lock, irq_flags);
659 if (!(trace_flags & TRACE_ITER_BLOCK))
660 wake_up (&trace_wait);
664 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
665 unsigned long ip, unsigned long parent_ip, unsigned long flags)
667 if (likely(!atomic_read(&data->disabled)))
668 trace_function(tr, data, ip, parent_ip, flags);
672 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
673 unsigned long arg1, unsigned long arg2, unsigned long arg3)
675 struct trace_entry *entry;
676 unsigned long irq_flags;
678 spin_lock_irqsave(&data->lock, irq_flags);
679 entry = tracing_get_trace_entry(tr, data);
680 tracing_generic_entry_update(entry, 0);
681 entry->type = TRACE_SPECIAL;
682 entry->special.arg1 = arg1;
683 entry->special.arg2 = arg2;
684 entry->special.arg3 = arg3;
685 spin_unlock_irqrestore(&data->lock, irq_flags);
687 if (!(trace_flags & TRACE_ITER_BLOCK))
688 wake_up (&trace_wait);
692 tracing_sched_switch_trace(struct trace_array *tr,
693 struct trace_array_cpu *data,
694 struct task_struct *prev, struct task_struct *next,
697 struct trace_entry *entry;
698 unsigned long irq_flags;
700 spin_lock_irqsave(&data->lock, irq_flags);
701 entry = tracing_get_trace_entry(tr, data);
702 tracing_generic_entry_update(entry, flags);
703 entry->type = TRACE_CTX;
704 entry->ctx.prev_pid = prev->pid;
705 entry->ctx.prev_prio = prev->prio;
706 entry->ctx.prev_state = prev->state;
707 entry->ctx.next_pid = next->pid;
708 entry->ctx.next_prio = next->prio;
709 spin_unlock_irqrestore(&data->lock, irq_flags);
711 if (!(trace_flags & TRACE_ITER_BLOCK))
712 wake_up (&trace_wait);
716 tracing_sched_wakeup_trace(struct trace_array *tr,
717 struct trace_array_cpu *data,
718 struct task_struct *wakee, struct task_struct *curr,
721 struct trace_entry *entry;
722 unsigned long irq_flags;
724 spin_lock_irqsave(&data->lock, irq_flags);
725 entry = tracing_get_trace_entry(tr, data);
726 tracing_generic_entry_update(entry, flags);
727 entry->type = TRACE_WAKE;
728 entry->ctx.prev_pid = curr->pid;
729 entry->ctx.prev_prio = curr->prio;
730 entry->ctx.prev_state = curr->state;
731 entry->ctx.next_pid = wakee->pid;
732 entry->ctx.next_prio = wakee->prio;
733 spin_unlock_irqrestore(&data->lock, irq_flags);
735 if (!(trace_flags & TRACE_ITER_BLOCK))
736 wake_up(&trace_wait);
741 function_trace_call(unsigned long ip, unsigned long parent_ip)
743 struct trace_array *tr = &global_trace;
744 struct trace_array_cpu *data;
749 if (unlikely(!tracer_enabled))
752 local_irq_save(flags);
753 cpu = raw_smp_processor_id();
754 data = tr->data[cpu];
755 disabled = atomic_inc_return(&data->disabled);
757 if (likely(disabled == 1))
758 trace_function(tr, data, ip, parent_ip, flags);
760 atomic_dec(&data->disabled);
761 local_irq_restore(flags);
764 static struct ftrace_ops trace_ops __read_mostly =
766 .func = function_trace_call,
769 void tracing_start_function_trace(void)
771 register_ftrace_function(&trace_ops);
774 void tracing_stop_function_trace(void)
776 unregister_ftrace_function(&trace_ops);
780 enum trace_file_type {
781 TRACE_FILE_LAT_FMT = 1,
784 static struct trace_entry *
785 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
786 struct trace_iterator *iter, int cpu)
789 struct trace_entry *array;
791 if (iter->next_idx[cpu] >= tr->entries ||
792 iter->next_idx[cpu] >= data->trace_idx ||
793 (data->trace_head == data->trace_tail &&
794 data->trace_head_idx == data->trace_tail_idx))
797 if (!iter->next_page[cpu]) {
798 /* Initialize the iterator for this cpu trace buffer */
799 WARN_ON(!data->trace_tail);
800 page = virt_to_page(data->trace_tail);
801 iter->next_page[cpu] = &page->lru;
802 iter->next_page_idx[cpu] = data->trace_tail_idx;
805 page = list_entry(iter->next_page[cpu], struct page, lru);
806 BUG_ON(&data->trace_pages == &page->lru);
808 array = page_address(page);
810 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
811 return &array[iter->next_page_idx[cpu]];
814 static struct trace_entry *
815 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
817 struct trace_array *tr = iter->tr;
818 struct trace_entry *ent, *next = NULL;
822 for_each_possible_cpu(cpu) {
823 if (!head_page(tr->data[cpu]))
825 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
827 * Pick the entry with the smallest timestamp:
829 if (ent && (!next || ent->t < next->t)) {
841 static void trace_iterator_increment(struct trace_iterator *iter)
844 iter->next_idx[iter->cpu]++;
845 iter->next_page_idx[iter->cpu]++;
847 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
848 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
850 iter->next_page_idx[iter->cpu] = 0;
851 iter->next_page[iter->cpu] =
852 trace_next_list(data, iter->next_page[iter->cpu]);
856 static void trace_consume(struct trace_iterator *iter)
858 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
860 data->trace_tail_idx++;
861 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
862 data->trace_tail = trace_next_page(data, data->trace_tail);
863 data->trace_tail_idx = 0;
866 /* Check if we empty it, then reset the index */
867 if (data->trace_head == data->trace_tail &&
868 data->trace_head_idx == data->trace_tail_idx)
872 static void *find_next_entry_inc(struct trace_iterator *iter)
874 struct trace_entry *next;
877 next = find_next_entry(iter, &next_cpu);
879 iter->prev_ent = iter->ent;
880 iter->prev_cpu = iter->cpu;
883 iter->cpu = next_cpu;
886 trace_iterator_increment(iter);
888 return next ? iter : NULL;
891 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
893 struct trace_iterator *iter = m->private;
894 void *last_ent = iter->ent;
900 /* can't go backwards */
905 ent = find_next_entry_inc(iter);
909 while (ent && iter->idx < i)
910 ent = find_next_entry_inc(iter);
914 if (last_ent && !ent)
915 seq_puts(m, "\n\nvim:ft=help\n");
920 static void *s_start(struct seq_file *m, loff_t *pos)
922 struct trace_iterator *iter = m->private;
927 mutex_lock(&trace_types_lock);
929 if (!current_trace || current_trace != iter->trace)
932 atomic_inc(&trace_record_cmdline_disabled);
934 /* let the tracer grab locks here if needed */
935 if (current_trace->start)
936 current_trace->start(iter);
938 if (*pos != iter->pos) {
942 iter->prev_ent = NULL;
945 for_each_possible_cpu(i) {
946 iter->next_idx[i] = 0;
947 iter->next_page[i] = NULL;
950 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
955 p = s_next(m, p, &l);
961 static void s_stop(struct seq_file *m, void *p)
963 struct trace_iterator *iter = m->private;
965 atomic_dec(&trace_record_cmdline_disabled);
967 /* let the tracer release locks here if needed */
968 if (current_trace && current_trace == iter->trace && iter->trace->stop)
969 iter->trace->stop(iter);
971 mutex_unlock(&trace_types_lock);
975 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
977 #ifdef CONFIG_KALLSYMS
978 char str[KSYM_SYMBOL_LEN];
980 kallsyms_lookup(address, NULL, NULL, NULL, str);
982 return trace_seq_printf(s, fmt, str);
988 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
989 unsigned long address)
991 #ifdef CONFIG_KALLSYMS
992 char str[KSYM_SYMBOL_LEN];
994 sprint_symbol(str, address);
995 return trace_seq_printf(s, fmt, str);
1000 #ifndef CONFIG_64BIT
1001 # define IP_FMT "%08lx"
1003 # define IP_FMT "%016lx"
1007 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1012 return trace_seq_printf(s, "0");
1014 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1015 ret = seq_print_sym_offset(s, "%s", ip);
1017 ret = seq_print_sym_short(s, "%s", ip);
1022 if (sym_flags & TRACE_ITER_SYM_ADDR)
1023 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1027 static void print_lat_help_header(struct seq_file *m)
1029 seq_puts(m, "# _------=> CPU# \n");
1030 seq_puts(m, "# / _-----=> irqs-off \n");
1031 seq_puts(m, "# | / _----=> need-resched \n");
1032 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1033 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1034 seq_puts(m, "# |||| / \n");
1035 seq_puts(m, "# ||||| delay \n");
1036 seq_puts(m, "# cmd pid ||||| time | caller \n");
1037 seq_puts(m, "# \\ / ||||| \\ | / \n");
1040 static void print_func_help_header(struct seq_file *m)
1042 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1043 seq_puts(m, "# | | | | |\n");
1048 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1050 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1051 struct trace_array *tr = iter->tr;
1052 struct trace_array_cpu *data = tr->data[tr->cpu];
1053 struct tracer *type = current_trace;
1054 unsigned long total = 0;
1055 unsigned long entries = 0;
1057 const char *name = "preemption";
1062 for_each_possible_cpu(cpu) {
1063 if (head_page(tr->data[cpu])) {
1064 total += tr->data[cpu]->trace_idx;
1065 if (tr->data[cpu]->trace_idx > tr->entries)
1066 entries += tr->entries;
1068 entries += tr->data[cpu]->trace_idx;
1072 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1074 seq_puts(m, "-----------------------------------"
1075 "---------------------------------\n");
1076 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1077 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1078 nsecs_to_usecs(data->saved_latency),
1082 #if defined(CONFIG_PREEMPT_NONE)
1084 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1086 #elif defined(CONFIG_PREEMPT_DESKTOP)
1091 /* These are reserved for later use */
1094 seq_printf(m, " #P:%d)\n", num_online_cpus());
1098 seq_puts(m, " -----------------\n");
1099 seq_printf(m, " | task: %.16s-%d "
1100 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1101 data->comm, data->pid, data->uid, data->nice,
1102 data->policy, data->rt_priority);
1103 seq_puts(m, " -----------------\n");
1105 if (data->critical_start) {
1106 seq_puts(m, " => started at: ");
1107 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1108 trace_print_seq(m, &iter->seq);
1109 seq_puts(m, "\n => ended at: ");
1110 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1111 trace_print_seq(m, &iter->seq);
1119 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1121 int hardirq, softirq;
1124 comm = trace_find_cmdline(entry->pid);
1126 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1127 trace_seq_printf(s, "%d", cpu);
1128 trace_seq_printf(s, "%c%c",
1129 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1130 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1132 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1133 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1134 if (hardirq && softirq)
1135 trace_seq_putc(s, 'H');
1138 trace_seq_putc(s, 'h');
1141 trace_seq_putc(s, 's');
1143 trace_seq_putc(s, '.');
1147 if (entry->preempt_count)
1148 trace_seq_printf(s, "%x", entry->preempt_count);
1150 trace_seq_puts(s, ".");
1153 unsigned long preempt_mark_thresh = 100;
1156 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1157 unsigned long rel_usecs)
1159 trace_seq_printf(s, " %4lldus", abs_usecs);
1160 if (rel_usecs > preempt_mark_thresh)
1161 trace_seq_puts(s, "!: ");
1162 else if (rel_usecs > 1)
1163 trace_seq_puts(s, "+: ");
1165 trace_seq_puts(s, " : ");
1168 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1171 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1173 struct trace_seq *s = &iter->seq;
1174 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1175 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1176 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1177 struct trace_entry *entry = iter->ent;
1178 unsigned long abs_usecs;
1179 unsigned long rel_usecs;
1185 rel_usecs = ns2usecs(next_entry->t - entry->t);
1186 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1189 comm = trace_find_cmdline(entry->pid);
1190 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1191 " %ld.%03ldms (+%ld.%03ldms): ",
1193 entry->pid, cpu, entry->flags,
1194 entry->preempt_count, trace_idx,
1197 abs_usecs % 1000, rel_usecs/1000,
1200 lat_print_generic(s, entry, cpu);
1201 lat_print_timestamp(s, abs_usecs, rel_usecs);
1203 switch (entry->type) {
1205 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1206 trace_seq_puts(s, " (");
1207 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1208 trace_seq_puts(s, ")\n");
1212 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1213 state_to_char[entry->ctx.prev_state] : 'X';
1214 comm = trace_find_cmdline(entry->ctx.next_pid);
1215 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
1216 entry->ctx.prev_pid,
1217 entry->ctx.prev_prio,
1218 S, entry->type == TRACE_CTX ? "==>" : " +",
1219 entry->ctx.next_pid,
1220 entry->ctx.next_prio,
1224 trace_seq_printf(s, " %lx %lx %lx\n",
1225 entry->special.arg1,
1226 entry->special.arg2,
1227 entry->special.arg3);
1230 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1235 static int print_trace_fmt(struct trace_iterator *iter)
1237 struct trace_seq *s = &iter->seq;
1238 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1239 struct trace_entry *entry;
1240 unsigned long usec_rem;
1241 unsigned long long t;
1249 comm = trace_find_cmdline(iter->ent->pid);
1251 t = ns2usecs(entry->t);
1252 usec_rem = do_div(t, 1000000ULL);
1253 secs = (unsigned long)t;
1255 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1258 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1261 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1265 switch (entry->type) {
1267 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1270 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1271 entry->fn.parent_ip) {
1272 ret = trace_seq_printf(s, " <-");
1275 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1280 ret = trace_seq_printf(s, "\n");
1286 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1287 state_to_char[entry->ctx.prev_state] : 'X';
1288 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
1289 entry->ctx.prev_pid,
1290 entry->ctx.prev_prio,
1292 entry->type == TRACE_CTX ? "==>" : " +",
1293 entry->ctx.next_pid,
1294 entry->ctx.next_prio);
1299 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1300 entry->special.arg1,
1301 entry->special.arg2,
1302 entry->special.arg3);
1310 static int print_raw_fmt(struct trace_iterator *iter)
1312 struct trace_seq *s = &iter->seq;
1313 struct trace_entry *entry;
1319 ret = trace_seq_printf(s, "%d %d %llu ",
1320 entry->pid, iter->cpu, entry->t);
1324 switch (entry->type) {
1326 ret = trace_seq_printf(s, "%x %x\n",
1327 entry->fn.ip, entry->fn.parent_ip);
1333 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1334 state_to_char[entry->ctx.prev_state] : 'X';
1335 if (entry->type == TRACE_WAKE)
1337 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1338 entry->ctx.prev_pid,
1339 entry->ctx.prev_prio,
1341 entry->ctx.next_pid,
1342 entry->ctx.next_prio);
1347 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1348 entry->special.arg1,
1349 entry->special.arg2,
1350 entry->special.arg3);
1358 #define SEQ_PUT_FIELD_RET(s, x) \
1360 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1364 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1366 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1370 static int print_hex_fmt(struct trace_iterator *iter)
1372 struct trace_seq *s = &iter->seq;
1373 unsigned char newline = '\n';
1374 struct trace_entry *entry;
1379 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1380 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1381 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1383 switch (entry->type) {
1385 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1386 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1390 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1391 state_to_char[entry->ctx.prev_state] : 'X';
1392 if (entry->type == TRACE_WAKE)
1394 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1395 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1396 SEQ_PUT_HEX_FIELD_RET(s, S);
1397 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1398 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1399 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1402 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1403 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1404 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1407 SEQ_PUT_FIELD_RET(s, newline);
1412 static int print_bin_fmt(struct trace_iterator *iter)
1414 struct trace_seq *s = &iter->seq;
1415 struct trace_entry *entry;
1419 SEQ_PUT_FIELD_RET(s, entry->pid);
1420 SEQ_PUT_FIELD_RET(s, entry->cpu);
1421 SEQ_PUT_FIELD_RET(s, entry->t);
1423 switch (entry->type) {
1425 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1426 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1429 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1430 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1431 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1432 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1433 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1436 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1437 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1438 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1444 static int trace_empty(struct trace_iterator *iter)
1446 struct trace_array_cpu *data;
1449 for_each_possible_cpu(cpu) {
1450 data = iter->tr->data[cpu];
1452 if (head_page(data) && data->trace_idx &&
1453 (data->trace_tail != data->trace_head ||
1454 data->trace_tail_idx != data->trace_head_idx))
1460 static int print_trace_line(struct trace_iterator *iter)
1462 if (trace_flags & TRACE_ITER_BIN)
1463 return print_bin_fmt(iter);
1465 if (trace_flags & TRACE_ITER_HEX)
1466 return print_hex_fmt(iter);
1468 if (trace_flags & TRACE_ITER_RAW)
1469 return print_raw_fmt(iter);
1471 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1472 return print_lat_fmt(iter, iter->idx, iter->cpu);
1474 return print_trace_fmt(iter);
1477 static int s_show(struct seq_file *m, void *v)
1479 struct trace_iterator *iter = v;
1481 if (iter->ent == NULL) {
1483 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1486 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1487 /* print nothing if the buffers are empty */
1488 if (trace_empty(iter))
1490 print_trace_header(m, iter);
1491 if (!(trace_flags & TRACE_ITER_VERBOSE))
1492 print_lat_help_header(m);
1494 if (!(trace_flags & TRACE_ITER_VERBOSE))
1495 print_func_help_header(m);
1498 print_trace_line(iter);
1499 trace_print_seq(m, &iter->seq);
1505 static struct seq_operations tracer_seq_ops = {
1512 static struct trace_iterator *
1513 __tracing_open(struct inode *inode, struct file *file, int *ret)
1515 struct trace_iterator *iter;
1517 if (tracing_disabled) {
1522 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1528 mutex_lock(&trace_types_lock);
1529 if (current_trace && current_trace->print_max)
1532 iter->tr = inode->i_private;
1533 iter->trace = current_trace;
1536 /* TODO stop tracer */
1537 *ret = seq_open(file, &tracer_seq_ops);
1539 struct seq_file *m = file->private_data;
1542 /* stop the trace while dumping */
1546 if (iter->trace && iter->trace->open)
1547 iter->trace->open(iter);
1552 mutex_unlock(&trace_types_lock);
1558 int tracing_open_generic(struct inode *inode, struct file *filp)
1560 if (tracing_disabled)
1563 filp->private_data = inode->i_private;
1567 int tracing_release(struct inode *inode, struct file *file)
1569 struct seq_file *m = (struct seq_file *)file->private_data;
1570 struct trace_iterator *iter = m->private;
1572 mutex_lock(&trace_types_lock);
1573 if (iter->trace && iter->trace->close)
1574 iter->trace->close(iter);
1576 /* reenable tracing if it was previously enabled */
1579 mutex_unlock(&trace_types_lock);
1581 seq_release(inode, file);
1586 static int tracing_open(struct inode *inode, struct file *file)
1590 __tracing_open(inode, file, &ret);
1595 static int tracing_lt_open(struct inode *inode, struct file *file)
1597 struct trace_iterator *iter;
1600 iter = __tracing_open(inode, file, &ret);
1603 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1610 t_next(struct seq_file *m, void *v, loff_t *pos)
1612 struct tracer *t = m->private;
1624 static void *t_start(struct seq_file *m, loff_t *pos)
1626 struct tracer *t = m->private;
1629 mutex_lock(&trace_types_lock);
1630 for (; t && l < *pos; t = t_next(m, t, &l))
1636 static void t_stop(struct seq_file *m, void *p)
1638 mutex_unlock(&trace_types_lock);
1641 static int t_show(struct seq_file *m, void *v)
1643 struct tracer *t = v;
1648 seq_printf(m, "%s", t->name);
1657 static struct seq_operations show_traces_seq_ops = {
1664 static int show_traces_open(struct inode *inode, struct file *file)
1668 if (tracing_disabled)
1671 ret = seq_open(file, &show_traces_seq_ops);
1673 struct seq_file *m = file->private_data;
1674 m->private = trace_types;
1680 static struct file_operations tracing_fops = {
1681 .open = tracing_open,
1683 .llseek = seq_lseek,
1684 .release = tracing_release,
1687 static struct file_operations tracing_lt_fops = {
1688 .open = tracing_lt_open,
1690 .llseek = seq_lseek,
1691 .release = tracing_release,
1694 static struct file_operations show_traces_fops = {
1695 .open = show_traces_open,
1697 .release = seq_release,
1701 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1702 size_t cnt, loff_t *ppos)
1709 /* calulate max size */
1710 for (i = 0; trace_options[i]; i++) {
1711 len += strlen(trace_options[i]);
1712 len += 3; /* "no" and space */
1715 /* +2 for \n and \0 */
1716 buf = kmalloc(len + 2, GFP_KERNEL);
1720 for (i = 0; trace_options[i]; i++) {
1721 if (trace_flags & (1 << i))
1722 r += sprintf(buf + r, "%s ", trace_options[i]);
1724 r += sprintf(buf + r, "no%s ", trace_options[i]);
1727 r += sprintf(buf + r, "\n");
1728 WARN_ON(r >= len + 2);
1730 r = simple_read_from_buffer(ubuf, cnt, ppos,
1739 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1740 size_t cnt, loff_t *ppos)
1750 if (copy_from_user(&buf, ubuf, cnt))
1755 if (strncmp(buf, "no", 2) == 0) {
1760 for (i = 0; trace_options[i]; i++) {
1761 int len = strlen(trace_options[i]);
1763 if (strncmp(cmp, trace_options[i], len) == 0) {
1765 trace_flags &= ~(1 << i);
1767 trace_flags |= (1 << i);
1777 static struct file_operations tracing_iter_fops = {
1778 .open = tracing_open_generic,
1779 .read = tracing_iter_ctrl_read,
1780 .write = tracing_iter_ctrl_write,
1783 static const char readme_msg[] =
1784 "tracing mini-HOWTO:\n\n"
1786 "# mount -t debugfs nodev /debug\n\n"
1787 "# cat /debug/tracing/available_tracers\n"
1788 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1789 "# cat /debug/tracing/current_tracer\n"
1791 "# echo sched_switch > /debug/tracing/current_tracer\n"
1792 "# cat /debug/tracing/current_tracer\n"
1794 "# cat /debug/tracing/iter_ctrl\n"
1795 "noprint-parent nosym-offset nosym-addr noverbose\n"
1796 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1797 "# echo 1 > /debug/tracing/tracing_enabled\n"
1798 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1799 "echo 0 > /debug/tracing/tracing_enabled\n"
1803 tracing_readme_read(struct file *filp, char __user *ubuf,
1804 size_t cnt, loff_t *ppos)
1806 return simple_read_from_buffer(ubuf, cnt, ppos,
1807 readme_msg, strlen(readme_msg));
1810 static struct file_operations tracing_readme_fops = {
1811 .open = tracing_open_generic,
1812 .read = tracing_readme_read,
1816 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1817 size_t cnt, loff_t *ppos)
1819 struct trace_array *tr = filp->private_data;
1823 r = sprintf(buf, "%ld\n", tr->ctrl);
1824 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1828 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1829 size_t cnt, loff_t *ppos)
1831 struct trace_array *tr = filp->private_data;
1838 if (copy_from_user(&buf, ubuf, cnt))
1843 val = simple_strtoul(buf, NULL, 10);
1847 mutex_lock(&trace_types_lock);
1848 if (tr->ctrl ^ val) {
1856 if (current_trace && current_trace->ctrl_update)
1857 current_trace->ctrl_update(tr);
1859 mutex_unlock(&trace_types_lock);
1867 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1868 size_t cnt, loff_t *ppos)
1870 char buf[max_tracer_type_len+2];
1873 mutex_lock(&trace_types_lock);
1875 r = sprintf(buf, "%s\n", current_trace->name);
1877 r = sprintf(buf, "\n");
1878 mutex_unlock(&trace_types_lock);
1880 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1884 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1885 size_t cnt, loff_t *ppos)
1887 struct trace_array *tr = &global_trace;
1889 char buf[max_tracer_type_len+1];
1892 if (cnt > max_tracer_type_len)
1893 cnt = max_tracer_type_len;
1895 if (copy_from_user(&buf, ubuf, cnt))
1900 /* strip ending whitespace. */
1901 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1904 mutex_lock(&trace_types_lock);
1905 for (t = trace_types; t; t = t->next) {
1906 if (strcmp(t->name, buf) == 0)
1909 if (!t || t == current_trace)
1912 if (current_trace && current_trace->reset)
1913 current_trace->reset(tr);
1920 mutex_unlock(&trace_types_lock);
1928 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1929 size_t cnt, loff_t *ppos)
1931 unsigned long *ptr = filp->private_data;
1935 r = snprintf(buf, 64, "%ld\n",
1936 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1939 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1943 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1944 size_t cnt, loff_t *ppos)
1946 long *ptr = filp->private_data;
1953 if (copy_from_user(&buf, ubuf, cnt))
1958 val = simple_strtoul(buf, NULL, 10);
1965 static atomic_t tracing_reader;
1967 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1969 struct trace_iterator *iter;
1971 if (tracing_disabled)
1974 /* We only allow for reader of the pipe */
1975 if (atomic_inc_return(&tracing_reader) != 1) {
1976 atomic_dec(&tracing_reader);
1980 /* create a buffer to store the information to pass to userspace */
1981 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1985 iter->tr = &global_trace;
1987 filp->private_data = iter;
1992 static int tracing_release_pipe(struct inode *inode, struct file *file)
1994 struct trace_iterator *iter = file->private_data;
1997 atomic_dec(&tracing_reader);
2003 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2005 struct trace_iterator *iter = filp->private_data;
2007 if (trace_flags & TRACE_ITER_BLOCK) {
2009 * Always select as readable when in blocking mode
2011 return POLLIN | POLLRDNORM;
2014 if (!trace_empty(iter))
2015 return POLLIN | POLLRDNORM;
2016 poll_wait(filp, &trace_wait, poll_table);
2017 if (!trace_empty(iter))
2018 return POLLIN | POLLRDNORM;
2028 tracing_read_pipe(struct file *filp, char __user *ubuf,
2029 size_t cnt, loff_t *ppos)
2031 struct trace_iterator *iter = filp->private_data;
2032 struct trace_array_cpu *data;
2033 static cpumask_t mask;
2035 unsigned long flags;
2036 #ifdef CONFIG_FTRACE
2044 /* return any leftover data */
2045 if (iter->seq.len > start) {
2046 len = iter->seq.len - start;
2049 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2058 trace_seq_reset(&iter->seq);
2061 while (trace_empty(iter)) {
2062 if (!(trace_flags & TRACE_ITER_BLOCK))
2063 return -EWOULDBLOCK;
2065 * This is a make-shift waitqueue. The reason we don't use
2066 * an actual wait queue is because:
2067 * 1) we only ever have one waiter
2068 * 2) the tracing, traces all functions, we don't want
2069 * the overhead of calling wake_up and friends
2070 * (and tracing them too)
2071 * Anyway, this is really very primitive wakeup.
2073 set_current_state(TASK_INTERRUPTIBLE);
2074 iter->tr->waiter = current;
2076 /* sleep for one second, and try again. */
2077 schedule_timeout(HZ);
2079 iter->tr->waiter = NULL;
2081 if (signal_pending(current))
2085 * We block until we read something and tracing is disabled.
2086 * We still block if tracing is disabled, but we have never
2087 * read anything. This allows a user to cat this file, and
2088 * then enable tracing. But after we have read something,
2089 * we give an EOF when tracing is again disabled.
2091 * iter->pos will be 0 if we haven't read anything.
2093 if (!tracer_enabled && iter->pos)
2099 /* stop when tracing is finished */
2100 if (trace_empty(iter))
2103 if (cnt >= PAGE_SIZE)
2104 cnt = PAGE_SIZE - 1;
2106 memset(iter, 0, sizeof(*iter));
2107 iter->tr = &global_trace;
2111 * We need to stop all tracing on all CPUS to read the
2112 * the next buffer. This is a bit expensive, but is
2113 * not done often. We fill all what we can read,
2114 * and then release the locks again.
2118 local_irq_save(flags);
2119 #ifdef CONFIG_FTRACE
2120 ftrace_save = ftrace_enabled;
2124 for_each_possible_cpu(cpu) {
2125 data = iter->tr->data[cpu];
2127 if (!head_page(data) || !data->trace_idx)
2130 atomic_inc(&data->disabled);
2134 for_each_cpu_mask(cpu, mask) {
2135 data = iter->tr->data[cpu];
2136 spin_lock(&data->lock);
2139 while (find_next_entry_inc(iter) != NULL) {
2140 int len = iter->seq.len;
2142 ret = print_trace_line(iter);
2144 /* don't print partial lines */
2145 iter->seq.len = len;
2149 trace_consume(iter);
2151 if (iter->seq.len >= cnt)
2155 for_each_cpu_mask(cpu, mask) {
2156 data = iter->tr->data[cpu];
2157 spin_unlock(&data->lock);
2160 for_each_cpu_mask(cpu, mask) {
2161 data = iter->tr->data[cpu];
2162 atomic_dec(&data->disabled);
2164 #ifdef CONFIG_FTRACE
2165 ftrace_enabled = ftrace_save;
2167 local_irq_restore(flags);
2169 /* Now copy what we have to the user */
2170 read = iter->seq.len;
2174 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2176 if (read < iter->seq.len)
2179 trace_seq_reset(&iter->seq);
2187 static struct file_operations tracing_max_lat_fops = {
2188 .open = tracing_open_generic,
2189 .read = tracing_max_lat_read,
2190 .write = tracing_max_lat_write,
2193 static struct file_operations tracing_ctrl_fops = {
2194 .open = tracing_open_generic,
2195 .read = tracing_ctrl_read,
2196 .write = tracing_ctrl_write,
2199 static struct file_operations set_tracer_fops = {
2200 .open = tracing_open_generic,
2201 .read = tracing_set_trace_read,
2202 .write = tracing_set_trace_write,
2205 static struct file_operations tracing_pipe_fops = {
2206 .open = tracing_open_pipe,
2207 .poll = tracing_poll_pipe,
2208 .read = tracing_read_pipe,
2209 .release = tracing_release_pipe,
2212 #ifdef CONFIG_DYNAMIC_FTRACE
2215 tracing_read_long(struct file *filp, char __user *ubuf,
2216 size_t cnt, loff_t *ppos)
2218 unsigned long *p = filp->private_data;
2222 r = sprintf(buf, "%ld\n", *p);
2224 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2227 static struct file_operations tracing_read_long_fops = {
2228 .open = tracing_open_generic,
2229 .read = tracing_read_long,
2233 static struct dentry *d_tracer;
2235 struct dentry *tracing_init_dentry(void)
2242 d_tracer = debugfs_create_dir("tracing", NULL);
2244 if (!d_tracer && !once) {
2246 pr_warning("Could not create debugfs directory 'tracing'\n");
2253 #ifdef CONFIG_FTRACE_SELFTEST
2254 /* Let selftest have access to static functions in this file */
2255 #include "trace_selftest.c"
2258 static __init void tracer_init_debugfs(void)
2260 struct dentry *d_tracer;
2261 struct dentry *entry;
2263 d_tracer = tracing_init_dentry();
2265 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2266 &global_trace, &tracing_ctrl_fops);
2268 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2270 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2271 NULL, &tracing_iter_fops);
2273 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2275 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2276 &global_trace, &tracing_lt_fops);
2278 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2280 entry = debugfs_create_file("trace", 0444, d_tracer,
2281 &global_trace, &tracing_fops);
2283 pr_warning("Could not create debugfs 'trace' entry\n");
2285 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2286 &global_trace, &show_traces_fops);
2288 pr_warning("Could not create debugfs 'trace' entry\n");
2290 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2291 &global_trace, &set_tracer_fops);
2293 pr_warning("Could not create debugfs 'trace' entry\n");
2295 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2296 &tracing_max_latency,
2297 &tracing_max_lat_fops);
2299 pr_warning("Could not create debugfs "
2300 "'tracing_max_latency' entry\n");
2302 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2303 &tracing_thresh, &tracing_max_lat_fops);
2305 pr_warning("Could not create debugfs "
2306 "'tracing_threash' entry\n");
2307 entry = debugfs_create_file("README", 0644, d_tracer,
2308 NULL, &tracing_readme_fops);
2310 pr_warning("Could not create debugfs 'README' entry\n");
2312 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2313 NULL, &tracing_pipe_fops);
2315 pr_warning("Could not create debugfs "
2316 "'tracing_threash' entry\n");
2318 #ifdef CONFIG_DYNAMIC_FTRACE
2319 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2320 &ftrace_update_tot_cnt,
2321 &tracing_read_long_fops);
2323 pr_warning("Could not create debugfs "
2324 "'dyn_ftrace_total_info' entry\n");
2328 /* dummy trace to disable tracing */
2329 static struct tracer no_tracer __read_mostly =
2334 static int trace_alloc_page(void)
2336 struct trace_array_cpu *data;
2337 struct page *page, *tmp;
2342 /* first allocate a page for each CPU */
2343 for_each_possible_cpu(i) {
2344 array = (void *)__get_free_page(GFP_KERNEL);
2345 if (array == NULL) {
2346 printk(KERN_ERR "tracer: failed to allocate page"
2347 "for trace buffer!\n");
2351 page = virt_to_page(array);
2352 list_add(&page->lru, &pages);
2354 /* Only allocate if we are actually using the max trace */
2355 #ifdef CONFIG_TRACER_MAX_TRACE
2356 array = (void *)__get_free_page(GFP_KERNEL);
2357 if (array == NULL) {
2358 printk(KERN_ERR "tracer: failed to allocate page"
2359 "for trace buffer!\n");
2362 page = virt_to_page(array);
2363 list_add(&page->lru, &pages);
2367 /* Now that we successfully allocate a page per CPU, add them */
2368 for_each_possible_cpu(i) {
2369 data = global_trace.data[i];
2370 spin_lock_init(&data->lock);
2371 lockdep_set_class(&data->lock, &data->lock_key);
2372 page = list_entry(pages.next, struct page, lru);
2373 list_del_init(&page->lru);
2374 list_add_tail(&page->lru, &data->trace_pages);
2377 #ifdef CONFIG_TRACER_MAX_TRACE
2378 data = max_tr.data[i];
2379 spin_lock_init(&data->lock);
2380 lockdep_set_class(&data->lock, &data->lock_key);
2381 page = list_entry(pages.next, struct page, lru);
2382 list_del_init(&page->lru);
2383 list_add_tail(&page->lru, &data->trace_pages);
2387 global_trace.entries += ENTRIES_PER_PAGE;
2392 list_for_each_entry_safe(page, tmp, &pages, lru) {
2393 list_del_init(&page->lru);
2399 __init static int tracer_alloc_buffers(void)
2401 struct trace_array_cpu *data;
2408 global_trace.ctrl = tracer_enabled;
2410 /* Allocate the first page for all buffers */
2411 for_each_possible_cpu(i) {
2412 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2413 max_tr.data[i] = &per_cpu(max_data, i);
2415 array = (void *)__get_free_page(GFP_KERNEL);
2416 if (array == NULL) {
2417 printk(KERN_ERR "tracer: failed to allocate page"
2418 "for trace buffer!\n");
2422 /* set the array to the list */
2423 INIT_LIST_HEAD(&data->trace_pages);
2424 page = virt_to_page(array);
2425 list_add(&page->lru, &data->trace_pages);
2426 /* use the LRU flag to differentiate the two buffers */
2429 /* Only allocate if we are actually using the max trace */
2430 #ifdef CONFIG_TRACER_MAX_TRACE
2431 array = (void *)__get_free_page(GFP_KERNEL);
2432 if (array == NULL) {
2433 printk(KERN_ERR "tracer: failed to allocate page"
2434 "for trace buffer!\n");
2438 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2439 page = virt_to_page(array);
2440 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2446 * Since we allocate by orders of pages, we may be able to
2449 global_trace.entries = ENTRIES_PER_PAGE;
2452 while (global_trace.entries < trace_nr_entries) {
2453 if (trace_alloc_page())
2457 max_tr.entries = global_trace.entries;
2459 pr_info("tracer: %d pages allocated for %ld",
2460 pages, trace_nr_entries);
2461 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2462 pr_info(" actual entries %ld\n", global_trace.entries);
2464 tracer_init_debugfs();
2466 trace_init_cmdlines();
2468 register_tracer(&no_tracer);
2469 current_trace = &no_tracer;
2471 /* All seems OK, enable tracing */
2472 tracing_disabled = 0;
2477 for (i-- ; i >= 0; i--) {
2478 struct page *page, *tmp;
2479 struct trace_array_cpu *data = global_trace.data[i];
2482 list_for_each_entry_safe(page, tmp,
2483 &data->trace_pages, lru) {
2484 list_del_init(&page->lru);
2489 #ifdef CONFIG_TRACER_MAX_TRACE
2490 data = max_tr.data[i];
2492 list_for_each_entry_safe(page, tmp,
2493 &data->trace_pages, lru) {
2494 list_del_init(&page->lru);
2502 fs_initcall(tracer_alloc_buffers);