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 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
73 static int __init set_nr_entries(char *str)
77 trace_nr_entries = simple_strtoul(str, &str, 0);
80 __setup("trace_entries=", set_nr_entries);
82 unsigned long nsecs_to_usecs(unsigned long nsecs)
88 __TRACE_FIRST_TYPE = 0,
99 enum trace_flag_type {
100 TRACE_FLAG_IRQS_OFF = 0x01,
101 TRACE_FLAG_NEED_RESCHED = 0x02,
102 TRACE_FLAG_HARDIRQ = 0x04,
103 TRACE_FLAG_SOFTIRQ = 0x08,
106 enum trace_iterator_flags {
107 TRACE_ITER_PRINT_PARENT = 0x01,
108 TRACE_ITER_SYM_OFFSET = 0x02,
109 TRACE_ITER_SYM_ADDR = 0x04,
110 TRACE_ITER_VERBOSE = 0x08,
111 TRACE_ITER_RAW = 0x10,
112 TRACE_ITER_HEX = 0x20,
113 TRACE_ITER_BIN = 0x40,
114 TRACE_ITER_BLOCK = 0x80,
115 TRACE_ITER_STACKTRACE = 0x100,
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 unsigned trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_STACKTRACE;
137 static DEFINE_SPINLOCK(ftrace_max_lock);
140 * Copy the new maximum trace into the separate maximum-trace
141 * structure. (this way the maximum trace is permanently saved,
142 * for later retrieval via /debugfs/tracing/latency_trace)
145 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
147 struct trace_array_cpu *data = tr->data[cpu];
150 max_tr.time_start = data->preempt_timestamp;
152 data = max_tr.data[cpu];
153 data->saved_latency = tracing_max_latency;
155 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
156 data->pid = tsk->pid;
157 data->uid = tsk->uid;
158 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
159 data->policy = tsk->policy;
160 data->rt_priority = tsk->rt_priority;
162 /* record this tasks comm */
163 tracing_record_cmdline(current);
166 void check_pages(struct trace_array_cpu *data)
168 struct page *page, *tmp;
170 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
171 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
173 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
174 BUG_ON(page->lru.next->prev != &page->lru);
175 BUG_ON(page->lru.prev->next != &page->lru);
179 void *head_page(struct trace_array_cpu *data)
184 if (list_empty(&data->trace_pages))
187 page = list_entry(data->trace_pages.next, struct page, lru);
188 BUG_ON(&page->lru == &data->trace_pages);
190 return page_address(page);
194 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
196 int len = (PAGE_SIZE - 1) - s->len;
204 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
207 /* If we can't write it all, don't bother writing anything */
217 trace_seq_puts(struct trace_seq *s, const char *str)
219 int len = strlen(str);
221 if (len > ((PAGE_SIZE - 1) - s->len))
224 memcpy(s->buffer + s->len, str, len);
231 trace_seq_putc(struct trace_seq *s, unsigned char c)
233 if (s->len >= (PAGE_SIZE - 1))
236 s->buffer[s->len++] = c;
242 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
244 if (len > ((PAGE_SIZE - 1) - s->len))
247 memcpy(s->buffer + s->len, mem, len);
256 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
258 unsigned char hex[HEX_CHARS];
263 BUG_ON(len >= HEX_CHARS);
268 for (i = 0, j = 0; i < len; i++) {
270 for (i = len-1, j = 0; i >= 0; i--) {
274 hex[j] = byte & 0x0f;
291 return trace_seq_putmem(s, hex, j);
295 trace_seq_reset(struct trace_seq *s)
301 trace_print_seq(struct seq_file *m, struct trace_seq *s)
303 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
306 seq_puts(m, s->buffer);
312 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
314 struct list_head flip_pages;
316 INIT_LIST_HEAD(&flip_pages);
318 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
319 sizeof(struct trace_array_cpu) -
320 offsetof(struct trace_array_cpu, trace_head_idx));
324 list_splice_init(&tr1->trace_pages, &flip_pages);
325 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
326 list_splice_init(&flip_pages, &tr2->trace_pages);
327 BUG_ON(!list_empty(&flip_pages));
333 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
335 struct trace_array_cpu *data;
338 WARN_ON_ONCE(!irqs_disabled());
339 spin_lock(&ftrace_max_lock);
340 /* clear out all the previous traces */
341 for_each_possible_cpu(i) {
343 flip_trace(max_tr.data[i], data);
347 __update_max_tr(tr, tsk, cpu);
348 spin_unlock(&ftrace_max_lock);
352 * update_max_tr_single - only copy one trace over, and reset the rest
354 * @tsk - task with the latency
355 * @cpu - the cpu of the buffer to copy.
358 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
360 struct trace_array_cpu *data = tr->data[cpu];
363 WARN_ON_ONCE(!irqs_disabled());
364 spin_lock(&ftrace_max_lock);
365 for_each_possible_cpu(i)
366 tracing_reset(max_tr.data[i]);
368 flip_trace(max_tr.data[cpu], data);
371 __update_max_tr(tr, tsk, cpu);
372 spin_unlock(&ftrace_max_lock);
375 int register_tracer(struct tracer *type)
382 pr_info("Tracer must have a name\n");
386 mutex_lock(&trace_types_lock);
387 for (t = trace_types; t; t = t->next) {
388 if (strcmp(type->name, t->name) == 0) {
390 pr_info("Trace %s already registered\n",
397 #ifdef CONFIG_FTRACE_STARTUP_TEST
398 if (type->selftest) {
399 struct tracer *saved_tracer = current_trace;
400 struct trace_array_cpu *data;
401 struct trace_array *tr = &global_trace;
402 int saved_ctrl = tr->ctrl;
405 * Run a selftest on this tracer.
406 * Here we reset the trace buffer, and set the current
407 * tracer to be this tracer. The tracer can then run some
408 * internal tracing to verify that everything is in order.
409 * If we fail, we do not register this tracer.
411 for_each_possible_cpu(i) {
413 if (!head_page(data))
417 current_trace = type;
419 /* the test is responsible for initializing and enabling */
420 pr_info("Testing tracer %s: ", type->name);
421 ret = type->selftest(type, tr);
422 /* the test is responsible for resetting too */
423 current_trace = saved_tracer;
424 tr->ctrl = saved_ctrl;
426 printk(KERN_CONT "FAILED!\n");
429 /* Only reset on passing, to avoid touching corrupted buffers */
430 for_each_possible_cpu(i) {
432 if (!head_page(data))
436 printk(KERN_CONT "PASSED\n");
440 type->next = trace_types;
442 len = strlen(type->name);
443 if (len > max_tracer_type_len)
444 max_tracer_type_len = len;
447 mutex_unlock(&trace_types_lock);
452 void unregister_tracer(struct tracer *type)
457 mutex_lock(&trace_types_lock);
458 for (t = &trace_types; *t; t = &(*t)->next) {
462 pr_info("Trace %s not registered\n", type->name);
467 if (strlen(type->name) != max_tracer_type_len)
470 max_tracer_type_len = 0;
471 for (t = &trace_types; *t; t = &(*t)->next) {
472 len = strlen((*t)->name);
473 if (len > max_tracer_type_len)
474 max_tracer_type_len = len;
477 mutex_unlock(&trace_types_lock);
480 void tracing_reset(struct trace_array_cpu *data)
483 data->trace_head = data->trace_tail = head_page(data);
484 data->trace_head_idx = 0;
485 data->trace_tail_idx = 0;
488 #define SAVED_CMDLINES 128
489 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
490 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
491 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
492 static int cmdline_idx;
493 static DEFINE_SPINLOCK(trace_cmdline_lock);
494 atomic_t trace_record_cmdline_disabled;
496 static void trace_init_cmdlines(void)
498 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
499 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
503 void trace_stop_cmdline_recording(void);
505 static void trace_save_cmdline(struct task_struct *tsk)
510 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
514 * It's not the end of the world if we don't get
515 * the lock, but we also don't want to spin
516 * nor do we want to disable interrupts,
517 * so if we miss here, then better luck next time.
519 if (!spin_trylock(&trace_cmdline_lock))
522 idx = map_pid_to_cmdline[tsk->pid];
523 if (idx >= SAVED_CMDLINES) {
524 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
526 map = map_cmdline_to_pid[idx];
527 if (map <= PID_MAX_DEFAULT)
528 map_pid_to_cmdline[map] = (unsigned)-1;
530 map_pid_to_cmdline[tsk->pid] = idx;
535 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
537 spin_unlock(&trace_cmdline_lock);
540 static char *trace_find_cmdline(int pid)
542 char *cmdline = "<...>";
548 if (pid > PID_MAX_DEFAULT)
551 map = map_pid_to_cmdline[pid];
552 if (map >= SAVED_CMDLINES)
555 cmdline = saved_cmdlines[map];
561 void tracing_record_cmdline(struct task_struct *tsk)
563 if (atomic_read(&trace_record_cmdline_disabled))
566 trace_save_cmdline(tsk);
569 static inline struct list_head *
570 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
573 * Roundrobin - but skip the head (which is not a real page):
576 if (unlikely(next == &data->trace_pages))
578 BUG_ON(next == &data->trace_pages);
584 trace_next_page(struct trace_array_cpu *data, void *addr)
586 struct list_head *next;
589 page = virt_to_page(addr);
591 next = trace_next_list(data, &page->lru);
592 page = list_entry(next, struct page, lru);
594 return page_address(page);
597 static inline struct trace_entry *
598 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
600 unsigned long idx, idx_next;
601 struct trace_entry *entry;
604 idx = data->trace_head_idx;
607 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
609 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
611 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
612 data->trace_head = trace_next_page(data, data->trace_head);
616 if (data->trace_head == data->trace_tail &&
617 idx_next == data->trace_tail_idx) {
619 data->trace_tail_idx++;
620 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
622 trace_next_page(data, data->trace_tail);
623 data->trace_tail_idx = 0;
627 data->trace_head_idx = idx_next;
633 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
635 struct task_struct *tsk = current;
638 pc = preempt_count();
640 entry->preempt_count = pc & 0xff;
641 entry->pid = tsk->pid;
642 entry->t = ftrace_now(raw_smp_processor_id());
643 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
644 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
645 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
646 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
650 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
651 unsigned long ip, unsigned long parent_ip, unsigned long flags)
653 struct trace_entry *entry;
654 unsigned long irq_flags;
656 spin_lock_irqsave(&data->lock, irq_flags);
657 entry = tracing_get_trace_entry(tr, data);
658 tracing_generic_entry_update(entry, flags);
659 entry->type = TRACE_FN;
661 entry->fn.parent_ip = parent_ip;
662 spin_unlock_irqrestore(&data->lock, irq_flags);
664 if (!(trace_flags & TRACE_ITER_BLOCK))
665 wake_up(&trace_wait);
669 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
670 unsigned long ip, unsigned long parent_ip, unsigned long flags)
672 if (likely(!atomic_read(&data->disabled)))
673 trace_function(tr, data, ip, parent_ip, flags);
677 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
678 unsigned long arg1, unsigned long arg2, unsigned long arg3)
680 struct trace_entry *entry;
681 unsigned long irq_flags;
683 spin_lock_irqsave(&data->lock, irq_flags);
684 entry = tracing_get_trace_entry(tr, data);
685 tracing_generic_entry_update(entry, 0);
686 entry->type = TRACE_SPECIAL;
687 entry->special.arg1 = arg1;
688 entry->special.arg2 = arg2;
689 entry->special.arg3 = arg3;
690 spin_unlock_irqrestore(&data->lock, irq_flags);
692 if (!(trace_flags & TRACE_ITER_BLOCK))
693 wake_up(&trace_wait);
696 void __trace_stack(struct trace_array *tr,
697 struct trace_array_cpu *data,
701 struct trace_entry *entry;
702 struct stack_trace trace;
704 if (!(trace_flags & TRACE_ITER_STACKTRACE))
707 entry = tracing_get_trace_entry(tr, data);
708 tracing_generic_entry_update(entry, flags);
709 entry->type = TRACE_STACK;
711 memset(&entry->stack, 0, sizeof(entry->stack));
713 trace.nr_entries = 0;
714 trace.max_entries = FTRACE_STACK_ENTRIES;
716 trace.entries = entry->stack.caller;
718 save_stack_trace(&trace);
722 tracing_sched_switch_trace(struct trace_array *tr,
723 struct trace_array_cpu *data,
724 struct task_struct *prev,
725 struct task_struct *next,
728 struct trace_entry *entry;
729 unsigned long irq_flags;
731 spin_lock_irqsave(&data->lock, irq_flags);
732 entry = tracing_get_trace_entry(tr, data);
733 tracing_generic_entry_update(entry, flags);
734 entry->type = TRACE_CTX;
735 entry->ctx.prev_pid = prev->pid;
736 entry->ctx.prev_prio = prev->prio;
737 entry->ctx.prev_state = prev->state;
738 entry->ctx.next_pid = next->pid;
739 entry->ctx.next_prio = next->prio;
740 __trace_stack(tr, data, flags, 4);
741 spin_unlock_irqrestore(&data->lock, irq_flags);
743 if (!(trace_flags & TRACE_ITER_BLOCK))
744 wake_up(&trace_wait);
748 tracing_sched_wakeup_trace(struct trace_array *tr,
749 struct trace_array_cpu *data,
750 struct task_struct *wakee,
751 struct task_struct *curr,
754 struct trace_entry *entry;
755 unsigned long irq_flags;
757 spin_lock_irqsave(&data->lock, irq_flags);
758 entry = tracing_get_trace_entry(tr, data);
759 tracing_generic_entry_update(entry, flags);
760 entry->type = TRACE_WAKE;
761 entry->ctx.prev_pid = curr->pid;
762 entry->ctx.prev_prio = curr->prio;
763 entry->ctx.prev_state = curr->state;
764 entry->ctx.next_pid = wakee->pid;
765 entry->ctx.next_prio = wakee->prio;
766 __trace_stack(tr, data, flags, 5);
767 spin_unlock_irqrestore(&data->lock, irq_flags);
769 if (!(trace_flags & TRACE_ITER_BLOCK))
770 wake_up(&trace_wait);
775 function_trace_call(unsigned long ip, unsigned long parent_ip)
777 struct trace_array *tr = &global_trace;
778 struct trace_array_cpu *data;
783 if (unlikely(!tracer_enabled))
786 local_irq_save(flags);
787 cpu = raw_smp_processor_id();
788 data = tr->data[cpu];
789 disabled = atomic_inc_return(&data->disabled);
791 if (likely(disabled == 1))
792 trace_function(tr, data, ip, parent_ip, flags);
794 atomic_dec(&data->disabled);
795 local_irq_restore(flags);
798 static struct ftrace_ops trace_ops __read_mostly =
800 .func = function_trace_call,
803 void tracing_start_function_trace(void)
805 register_ftrace_function(&trace_ops);
808 void tracing_stop_function_trace(void)
810 unregister_ftrace_function(&trace_ops);
814 enum trace_file_type {
815 TRACE_FILE_LAT_FMT = 1,
818 static struct trace_entry *
819 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
820 struct trace_iterator *iter, int cpu)
823 struct trace_entry *array;
825 if (iter->next_idx[cpu] >= tr->entries ||
826 iter->next_idx[cpu] >= data->trace_idx ||
827 (data->trace_head == data->trace_tail &&
828 data->trace_head_idx == data->trace_tail_idx))
831 if (!iter->next_page[cpu]) {
832 /* Initialize the iterator for this cpu trace buffer */
833 WARN_ON(!data->trace_tail);
834 page = virt_to_page(data->trace_tail);
835 iter->next_page[cpu] = &page->lru;
836 iter->next_page_idx[cpu] = data->trace_tail_idx;
839 page = list_entry(iter->next_page[cpu], struct page, lru);
840 BUG_ON(&data->trace_pages == &page->lru);
842 array = page_address(page);
844 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
845 return &array[iter->next_page_idx[cpu]];
848 static struct trace_entry *
849 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
851 struct trace_array *tr = iter->tr;
852 struct trace_entry *ent, *next = NULL;
856 for_each_possible_cpu(cpu) {
857 if (!head_page(tr->data[cpu]))
859 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
861 * Pick the entry with the smallest timestamp:
863 if (ent && (!next || ent->t < next->t)) {
875 static void trace_iterator_increment(struct trace_iterator *iter)
878 iter->next_idx[iter->cpu]++;
879 iter->next_page_idx[iter->cpu]++;
881 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
882 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
884 iter->next_page_idx[iter->cpu] = 0;
885 iter->next_page[iter->cpu] =
886 trace_next_list(data, iter->next_page[iter->cpu]);
890 static void trace_consume(struct trace_iterator *iter)
892 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
894 data->trace_tail_idx++;
895 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
896 data->trace_tail = trace_next_page(data, data->trace_tail);
897 data->trace_tail_idx = 0;
900 /* Check if we empty it, then reset the index */
901 if (data->trace_head == data->trace_tail &&
902 data->trace_head_idx == data->trace_tail_idx)
906 static void *find_next_entry_inc(struct trace_iterator *iter)
908 struct trace_entry *next;
911 next = find_next_entry(iter, &next_cpu);
913 iter->prev_ent = iter->ent;
914 iter->prev_cpu = iter->cpu;
917 iter->cpu = next_cpu;
920 trace_iterator_increment(iter);
922 return next ? iter : NULL;
925 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
927 struct trace_iterator *iter = m->private;
928 void *last_ent = iter->ent;
934 /* can't go backwards */
939 ent = find_next_entry_inc(iter);
943 while (ent && iter->idx < i)
944 ent = find_next_entry_inc(iter);
948 if (last_ent && !ent)
949 seq_puts(m, "\n\nvim:ft=help\n");
954 static void *s_start(struct seq_file *m, loff_t *pos)
956 struct trace_iterator *iter = m->private;
961 mutex_lock(&trace_types_lock);
963 if (!current_trace || current_trace != iter->trace)
966 atomic_inc(&trace_record_cmdline_disabled);
968 /* let the tracer grab locks here if needed */
969 if (current_trace->start)
970 current_trace->start(iter);
972 if (*pos != iter->pos) {
976 iter->prev_ent = NULL;
979 for_each_possible_cpu(i) {
980 iter->next_idx[i] = 0;
981 iter->next_page[i] = NULL;
984 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
989 p = s_next(m, p, &l);
995 static void s_stop(struct seq_file *m, void *p)
997 struct trace_iterator *iter = m->private;
999 atomic_dec(&trace_record_cmdline_disabled);
1001 /* let the tracer release locks here if needed */
1002 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1003 iter->trace->stop(iter);
1005 mutex_unlock(&trace_types_lock);
1009 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1011 #ifdef CONFIG_KALLSYMS
1012 char str[KSYM_SYMBOL_LEN];
1014 kallsyms_lookup(address, NULL, NULL, NULL, str);
1016 return trace_seq_printf(s, fmt, str);
1022 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1023 unsigned long address)
1025 #ifdef CONFIG_KALLSYMS
1026 char str[KSYM_SYMBOL_LEN];
1028 sprint_symbol(str, address);
1029 return trace_seq_printf(s, fmt, str);
1034 #ifndef CONFIG_64BIT
1035 # define IP_FMT "%08lx"
1037 # define IP_FMT "%016lx"
1041 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1046 return trace_seq_printf(s, "0");
1048 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1049 ret = seq_print_sym_offset(s, "%s", ip);
1051 ret = seq_print_sym_short(s, "%s", ip);
1056 if (sym_flags & TRACE_ITER_SYM_ADDR)
1057 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1061 static void print_lat_help_header(struct seq_file *m)
1063 seq_puts(m, "# _------=> CPU# \n");
1064 seq_puts(m, "# / _-----=> irqs-off \n");
1065 seq_puts(m, "# | / _----=> need-resched \n");
1066 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1067 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1068 seq_puts(m, "# |||| / \n");
1069 seq_puts(m, "# ||||| delay \n");
1070 seq_puts(m, "# cmd pid ||||| time | caller \n");
1071 seq_puts(m, "# \\ / ||||| \\ | / \n");
1074 static void print_func_help_header(struct seq_file *m)
1076 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1077 seq_puts(m, "# | | | | |\n");
1082 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1084 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1085 struct trace_array *tr = iter->tr;
1086 struct trace_array_cpu *data = tr->data[tr->cpu];
1087 struct tracer *type = current_trace;
1088 unsigned long total = 0;
1089 unsigned long entries = 0;
1091 const char *name = "preemption";
1096 for_each_possible_cpu(cpu) {
1097 if (head_page(tr->data[cpu])) {
1098 total += tr->data[cpu]->trace_idx;
1099 if (tr->data[cpu]->trace_idx > tr->entries)
1100 entries += tr->entries;
1102 entries += tr->data[cpu]->trace_idx;
1106 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1108 seq_puts(m, "-----------------------------------"
1109 "---------------------------------\n");
1110 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1111 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1112 nsecs_to_usecs(data->saved_latency),
1116 #if defined(CONFIG_PREEMPT_NONE)
1118 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1120 #elif defined(CONFIG_PREEMPT_DESKTOP)
1125 /* These are reserved for later use */
1128 seq_printf(m, " #P:%d)\n", num_online_cpus());
1132 seq_puts(m, " -----------------\n");
1133 seq_printf(m, " | task: %.16s-%d "
1134 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1135 data->comm, data->pid, data->uid, data->nice,
1136 data->policy, data->rt_priority);
1137 seq_puts(m, " -----------------\n");
1139 if (data->critical_start) {
1140 seq_puts(m, " => started at: ");
1141 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1142 trace_print_seq(m, &iter->seq);
1143 seq_puts(m, "\n => ended at: ");
1144 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1145 trace_print_seq(m, &iter->seq);
1153 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1155 int hardirq, softirq;
1158 comm = trace_find_cmdline(entry->pid);
1160 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1161 trace_seq_printf(s, "%d", cpu);
1162 trace_seq_printf(s, "%c%c",
1163 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1164 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1166 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1167 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1168 if (hardirq && softirq)
1169 trace_seq_putc(s, 'H');
1172 trace_seq_putc(s, 'h');
1175 trace_seq_putc(s, 's');
1177 trace_seq_putc(s, '.');
1181 if (entry->preempt_count)
1182 trace_seq_printf(s, "%x", entry->preempt_count);
1184 trace_seq_puts(s, ".");
1187 unsigned long preempt_mark_thresh = 100;
1190 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1191 unsigned long rel_usecs)
1193 trace_seq_printf(s, " %4lldus", abs_usecs);
1194 if (rel_usecs > preempt_mark_thresh)
1195 trace_seq_puts(s, "!: ");
1196 else if (rel_usecs > 1)
1197 trace_seq_puts(s, "+: ");
1199 trace_seq_puts(s, " : ");
1202 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1205 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1207 struct trace_seq *s = &iter->seq;
1208 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1209 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1210 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1211 struct trace_entry *entry = iter->ent;
1212 unsigned long abs_usecs;
1213 unsigned long rel_usecs;
1220 rel_usecs = ns2usecs(next_entry->t - entry->t);
1221 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1224 comm = trace_find_cmdline(entry->pid);
1225 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1226 " %ld.%03ldms (+%ld.%03ldms): ",
1228 entry->pid, cpu, entry->flags,
1229 entry->preempt_count, trace_idx,
1232 abs_usecs % 1000, rel_usecs/1000,
1235 if (entry->type != TRACE_STACK) {
1236 lat_print_generic(s, entry, cpu);
1237 lat_print_timestamp(s, abs_usecs, rel_usecs);
1240 switch (entry->type) {
1242 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1243 trace_seq_puts(s, " (");
1244 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1245 trace_seq_puts(s, ")\n");
1249 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1250 state_to_char[entry->ctx.prev_state] : 'X';
1251 comm = trace_find_cmdline(entry->ctx.next_pid);
1252 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
1253 entry->ctx.prev_pid,
1254 entry->ctx.prev_prio,
1255 S, entry->type == TRACE_CTX ? "==>" : " +",
1256 entry->ctx.next_pid,
1257 entry->ctx.next_prio,
1261 trace_seq_printf(s, " %lx %lx %lx\n",
1262 entry->special.arg1,
1263 entry->special.arg2,
1264 entry->special.arg3);
1267 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1269 trace_seq_puts(s, " <= ");
1270 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1272 trace_seq_puts(s, "\n");
1275 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1280 static int print_trace_fmt(struct trace_iterator *iter)
1282 struct trace_seq *s = &iter->seq;
1283 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1284 struct trace_entry *entry;
1285 unsigned long usec_rem;
1286 unsigned long long t;
1295 comm = trace_find_cmdline(iter->ent->pid);
1297 t = ns2usecs(entry->t);
1298 usec_rem = do_div(t, 1000000ULL);
1299 secs = (unsigned long)t;
1301 if (entry->type != TRACE_STACK) {
1302 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1305 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1308 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1313 switch (entry->type) {
1315 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1318 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1319 entry->fn.parent_ip) {
1320 ret = trace_seq_printf(s, " <-");
1323 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1328 ret = trace_seq_printf(s, "\n");
1334 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1335 state_to_char[entry->ctx.prev_state] : 'X';
1336 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
1337 entry->ctx.prev_pid,
1338 entry->ctx.prev_prio,
1340 entry->type == TRACE_CTX ? "==>" : " +",
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);
1355 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1357 ret = trace_seq_puts(s, " <= ");
1361 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1366 ret = trace_seq_puts(s, "\n");
1374 static int print_raw_fmt(struct trace_iterator *iter)
1376 struct trace_seq *s = &iter->seq;
1377 struct trace_entry *entry;
1383 ret = trace_seq_printf(s, "%d %d %llu ",
1384 entry->pid, iter->cpu, entry->t);
1388 switch (entry->type) {
1390 ret = trace_seq_printf(s, "%x %x\n",
1391 entry->fn.ip, entry->fn.parent_ip);
1397 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1398 state_to_char[entry->ctx.prev_state] : 'X';
1399 if (entry->type == TRACE_WAKE)
1401 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1402 entry->ctx.prev_pid,
1403 entry->ctx.prev_prio,
1405 entry->ctx.next_pid,
1406 entry->ctx.next_prio);
1412 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1413 entry->special.arg1,
1414 entry->special.arg2,
1415 entry->special.arg3);
1423 #define SEQ_PUT_FIELD_RET(s, x) \
1425 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1429 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1431 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1435 static int print_hex_fmt(struct trace_iterator *iter)
1437 struct trace_seq *s = &iter->seq;
1438 unsigned char newline = '\n';
1439 struct trace_entry *entry;
1444 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1445 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1446 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1448 switch (entry->type) {
1450 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1451 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1455 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1456 state_to_char[entry->ctx.prev_state] : 'X';
1457 if (entry->type == TRACE_WAKE)
1459 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1461 SEQ_PUT_HEX_FIELD_RET(s, S);
1462 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1463 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1464 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1468 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1469 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1470 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1473 SEQ_PUT_FIELD_RET(s, newline);
1478 static int print_bin_fmt(struct trace_iterator *iter)
1480 struct trace_seq *s = &iter->seq;
1481 struct trace_entry *entry;
1485 SEQ_PUT_FIELD_RET(s, entry->pid);
1486 SEQ_PUT_FIELD_RET(s, entry->cpu);
1487 SEQ_PUT_FIELD_RET(s, entry->t);
1489 switch (entry->type) {
1491 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1492 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1495 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1496 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1497 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1498 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1499 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1503 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1504 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1505 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1511 static int trace_empty(struct trace_iterator *iter)
1513 struct trace_array_cpu *data;
1516 for_each_possible_cpu(cpu) {
1517 data = iter->tr->data[cpu];
1519 if (head_page(data) && data->trace_idx &&
1520 (data->trace_tail != data->trace_head ||
1521 data->trace_tail_idx != data->trace_head_idx))
1527 static int print_trace_line(struct trace_iterator *iter)
1529 if (trace_flags & TRACE_ITER_BIN)
1530 return print_bin_fmt(iter);
1532 if (trace_flags & TRACE_ITER_HEX)
1533 return print_hex_fmt(iter);
1535 if (trace_flags & TRACE_ITER_RAW)
1536 return print_raw_fmt(iter);
1538 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1539 return print_lat_fmt(iter, iter->idx, iter->cpu);
1541 return print_trace_fmt(iter);
1544 static int s_show(struct seq_file *m, void *v)
1546 struct trace_iterator *iter = v;
1548 if (iter->ent == NULL) {
1550 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1553 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1554 /* print nothing if the buffers are empty */
1555 if (trace_empty(iter))
1557 print_trace_header(m, iter);
1558 if (!(trace_flags & TRACE_ITER_VERBOSE))
1559 print_lat_help_header(m);
1561 if (!(trace_flags & TRACE_ITER_VERBOSE))
1562 print_func_help_header(m);
1565 print_trace_line(iter);
1566 trace_print_seq(m, &iter->seq);
1572 static struct seq_operations tracer_seq_ops = {
1579 static struct trace_iterator *
1580 __tracing_open(struct inode *inode, struct file *file, int *ret)
1582 struct trace_iterator *iter;
1584 if (tracing_disabled) {
1589 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1595 mutex_lock(&trace_types_lock);
1596 if (current_trace && current_trace->print_max)
1599 iter->tr = inode->i_private;
1600 iter->trace = current_trace;
1603 /* TODO stop tracer */
1604 *ret = seq_open(file, &tracer_seq_ops);
1606 struct seq_file *m = file->private_data;
1609 /* stop the trace while dumping */
1613 if (iter->trace && iter->trace->open)
1614 iter->trace->open(iter);
1619 mutex_unlock(&trace_types_lock);
1625 int tracing_open_generic(struct inode *inode, struct file *filp)
1627 if (tracing_disabled)
1630 filp->private_data = inode->i_private;
1634 int tracing_release(struct inode *inode, struct file *file)
1636 struct seq_file *m = (struct seq_file *)file->private_data;
1637 struct trace_iterator *iter = m->private;
1639 mutex_lock(&trace_types_lock);
1640 if (iter->trace && iter->trace->close)
1641 iter->trace->close(iter);
1643 /* reenable tracing if it was previously enabled */
1646 mutex_unlock(&trace_types_lock);
1648 seq_release(inode, file);
1653 static int tracing_open(struct inode *inode, struct file *file)
1657 __tracing_open(inode, file, &ret);
1662 static int tracing_lt_open(struct inode *inode, struct file *file)
1664 struct trace_iterator *iter;
1667 iter = __tracing_open(inode, file, &ret);
1670 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1677 t_next(struct seq_file *m, void *v, loff_t *pos)
1679 struct tracer *t = m->private;
1691 static void *t_start(struct seq_file *m, loff_t *pos)
1693 struct tracer *t = m->private;
1696 mutex_lock(&trace_types_lock);
1697 for (; t && l < *pos; t = t_next(m, t, &l))
1703 static void t_stop(struct seq_file *m, void *p)
1705 mutex_unlock(&trace_types_lock);
1708 static int t_show(struct seq_file *m, void *v)
1710 struct tracer *t = v;
1715 seq_printf(m, "%s", t->name);
1724 static struct seq_operations show_traces_seq_ops = {
1731 static int show_traces_open(struct inode *inode, struct file *file)
1735 if (tracing_disabled)
1738 ret = seq_open(file, &show_traces_seq_ops);
1740 struct seq_file *m = file->private_data;
1741 m->private = trace_types;
1747 static struct file_operations tracing_fops = {
1748 .open = tracing_open,
1750 .llseek = seq_lseek,
1751 .release = tracing_release,
1754 static struct file_operations tracing_lt_fops = {
1755 .open = tracing_lt_open,
1757 .llseek = seq_lseek,
1758 .release = tracing_release,
1761 static struct file_operations show_traces_fops = {
1762 .open = show_traces_open,
1764 .release = seq_release,
1768 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1769 size_t cnt, loff_t *ppos)
1776 /* calulate max size */
1777 for (i = 0; trace_options[i]; i++) {
1778 len += strlen(trace_options[i]);
1779 len += 3; /* "no" and space */
1782 /* +2 for \n and \0 */
1783 buf = kmalloc(len + 2, GFP_KERNEL);
1787 for (i = 0; trace_options[i]; i++) {
1788 if (trace_flags & (1 << i))
1789 r += sprintf(buf + r, "%s ", trace_options[i]);
1791 r += sprintf(buf + r, "no%s ", trace_options[i]);
1794 r += sprintf(buf + r, "\n");
1795 WARN_ON(r >= len + 2);
1797 r = simple_read_from_buffer(ubuf, cnt, ppos,
1806 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1807 size_t cnt, loff_t *ppos)
1817 if (copy_from_user(&buf, ubuf, cnt))
1822 if (strncmp(buf, "no", 2) == 0) {
1827 for (i = 0; trace_options[i]; i++) {
1828 int len = strlen(trace_options[i]);
1830 if (strncmp(cmp, trace_options[i], len) == 0) {
1832 trace_flags &= ~(1 << i);
1834 trace_flags |= (1 << i);
1844 static struct file_operations tracing_iter_fops = {
1845 .open = tracing_open_generic,
1846 .read = tracing_iter_ctrl_read,
1847 .write = tracing_iter_ctrl_write,
1850 static const char readme_msg[] =
1851 "tracing mini-HOWTO:\n\n"
1853 "# mount -t debugfs nodev /debug\n\n"
1854 "# cat /debug/tracing/available_tracers\n"
1855 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1856 "# cat /debug/tracing/current_tracer\n"
1858 "# echo sched_switch > /debug/tracing/current_tracer\n"
1859 "# cat /debug/tracing/current_tracer\n"
1861 "# cat /debug/tracing/iter_ctrl\n"
1862 "noprint-parent nosym-offset nosym-addr noverbose\n"
1863 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1864 "# echo 1 > /debug/tracing/tracing_enabled\n"
1865 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1866 "echo 0 > /debug/tracing/tracing_enabled\n"
1870 tracing_readme_read(struct file *filp, char __user *ubuf,
1871 size_t cnt, loff_t *ppos)
1873 return simple_read_from_buffer(ubuf, cnt, ppos,
1874 readme_msg, strlen(readme_msg));
1877 static struct file_operations tracing_readme_fops = {
1878 .open = tracing_open_generic,
1879 .read = tracing_readme_read,
1883 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1884 size_t cnt, loff_t *ppos)
1886 struct trace_array *tr = filp->private_data;
1890 r = sprintf(buf, "%ld\n", tr->ctrl);
1891 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1895 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1896 size_t cnt, loff_t *ppos)
1898 struct trace_array *tr = filp->private_data;
1905 if (copy_from_user(&buf, ubuf, cnt))
1910 val = simple_strtoul(buf, NULL, 10);
1914 mutex_lock(&trace_types_lock);
1915 if (tr->ctrl ^ val) {
1923 if (current_trace && current_trace->ctrl_update)
1924 current_trace->ctrl_update(tr);
1926 mutex_unlock(&trace_types_lock);
1934 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1935 size_t cnt, loff_t *ppos)
1937 char buf[max_tracer_type_len+2];
1940 mutex_lock(&trace_types_lock);
1942 r = sprintf(buf, "%s\n", current_trace->name);
1944 r = sprintf(buf, "\n");
1945 mutex_unlock(&trace_types_lock);
1947 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1951 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1952 size_t cnt, loff_t *ppos)
1954 struct trace_array *tr = &global_trace;
1956 char buf[max_tracer_type_len+1];
1959 if (cnt > max_tracer_type_len)
1960 cnt = max_tracer_type_len;
1962 if (copy_from_user(&buf, ubuf, cnt))
1967 /* strip ending whitespace. */
1968 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1971 mutex_lock(&trace_types_lock);
1972 for (t = trace_types; t; t = t->next) {
1973 if (strcmp(t->name, buf) == 0)
1976 if (!t || t == current_trace)
1979 if (current_trace && current_trace->reset)
1980 current_trace->reset(tr);
1987 mutex_unlock(&trace_types_lock);
1995 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1996 size_t cnt, loff_t *ppos)
1998 unsigned long *ptr = filp->private_data;
2002 r = snprintf(buf, 64, "%ld\n",
2003 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2006 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2010 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2011 size_t cnt, loff_t *ppos)
2013 long *ptr = filp->private_data;
2020 if (copy_from_user(&buf, ubuf, cnt))
2025 val = simple_strtoul(buf, NULL, 10);
2032 static atomic_t tracing_reader;
2034 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2036 struct trace_iterator *iter;
2038 if (tracing_disabled)
2041 /* We only allow for reader of the pipe */
2042 if (atomic_inc_return(&tracing_reader) != 1) {
2043 atomic_dec(&tracing_reader);
2047 /* create a buffer to store the information to pass to userspace */
2048 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2052 iter->tr = &global_trace;
2054 filp->private_data = iter;
2059 static int tracing_release_pipe(struct inode *inode, struct file *file)
2061 struct trace_iterator *iter = file->private_data;
2064 atomic_dec(&tracing_reader);
2070 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2072 struct trace_iterator *iter = filp->private_data;
2074 if (trace_flags & TRACE_ITER_BLOCK) {
2076 * Always select as readable when in blocking mode
2078 return POLLIN | POLLRDNORM;
2081 if (!trace_empty(iter))
2082 return POLLIN | POLLRDNORM;
2083 poll_wait(filp, &trace_wait, poll_table);
2084 if (!trace_empty(iter))
2085 return POLLIN | POLLRDNORM;
2095 tracing_read_pipe(struct file *filp, char __user *ubuf,
2096 size_t cnt, loff_t *ppos)
2098 struct trace_iterator *iter = filp->private_data;
2099 struct trace_array_cpu *data;
2100 static cpumask_t mask;
2102 unsigned long flags;
2103 #ifdef CONFIG_FTRACE
2111 /* return any leftover data */
2112 if (iter->seq.len > start) {
2113 len = iter->seq.len - start;
2116 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2125 trace_seq_reset(&iter->seq);
2128 while (trace_empty(iter)) {
2129 if (!(trace_flags & TRACE_ITER_BLOCK))
2130 return -EWOULDBLOCK;
2132 * This is a make-shift waitqueue. The reason we don't use
2133 * an actual wait queue is because:
2134 * 1) we only ever have one waiter
2135 * 2) the tracing, traces all functions, we don't want
2136 * the overhead of calling wake_up and friends
2137 * (and tracing them too)
2138 * Anyway, this is really very primitive wakeup.
2140 set_current_state(TASK_INTERRUPTIBLE);
2141 iter->tr->waiter = current;
2143 /* sleep for one second, and try again. */
2144 schedule_timeout(HZ);
2146 iter->tr->waiter = NULL;
2148 if (signal_pending(current))
2152 * We block until we read something and tracing is disabled.
2153 * We still block if tracing is disabled, but we have never
2154 * read anything. This allows a user to cat this file, and
2155 * then enable tracing. But after we have read something,
2156 * we give an EOF when tracing is again disabled.
2158 * iter->pos will be 0 if we haven't read anything.
2160 if (!tracer_enabled && iter->pos)
2166 /* stop when tracing is finished */
2167 if (trace_empty(iter))
2170 if (cnt >= PAGE_SIZE)
2171 cnt = PAGE_SIZE - 1;
2173 memset(iter, 0, sizeof(*iter));
2174 iter->tr = &global_trace;
2178 * We need to stop all tracing on all CPUS to read the
2179 * the next buffer. This is a bit expensive, but is
2180 * not done often. We fill all what we can read,
2181 * and then release the locks again.
2185 local_irq_save(flags);
2186 #ifdef CONFIG_FTRACE
2187 ftrace_save = ftrace_enabled;
2191 for_each_possible_cpu(cpu) {
2192 data = iter->tr->data[cpu];
2194 if (!head_page(data) || !data->trace_idx)
2197 atomic_inc(&data->disabled);
2201 for_each_cpu_mask(cpu, mask) {
2202 data = iter->tr->data[cpu];
2203 spin_lock(&data->lock);
2206 while (find_next_entry_inc(iter) != NULL) {
2207 int len = iter->seq.len;
2209 ret = print_trace_line(iter);
2211 /* don't print partial lines */
2212 iter->seq.len = len;
2216 trace_consume(iter);
2218 if (iter->seq.len >= cnt)
2222 for_each_cpu_mask(cpu, mask) {
2223 data = iter->tr->data[cpu];
2224 spin_unlock(&data->lock);
2227 for_each_cpu_mask(cpu, mask) {
2228 data = iter->tr->data[cpu];
2229 atomic_dec(&data->disabled);
2231 #ifdef CONFIG_FTRACE
2232 ftrace_enabled = ftrace_save;
2234 local_irq_restore(flags);
2236 /* Now copy what we have to the user */
2237 read = iter->seq.len;
2241 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2243 if (read < iter->seq.len)
2246 trace_seq_reset(&iter->seq);
2254 static struct file_operations tracing_max_lat_fops = {
2255 .open = tracing_open_generic,
2256 .read = tracing_max_lat_read,
2257 .write = tracing_max_lat_write,
2260 static struct file_operations tracing_ctrl_fops = {
2261 .open = tracing_open_generic,
2262 .read = tracing_ctrl_read,
2263 .write = tracing_ctrl_write,
2266 static struct file_operations set_tracer_fops = {
2267 .open = tracing_open_generic,
2268 .read = tracing_set_trace_read,
2269 .write = tracing_set_trace_write,
2272 static struct file_operations tracing_pipe_fops = {
2273 .open = tracing_open_pipe,
2274 .poll = tracing_poll_pipe,
2275 .read = tracing_read_pipe,
2276 .release = tracing_release_pipe,
2279 #ifdef CONFIG_DYNAMIC_FTRACE
2282 tracing_read_long(struct file *filp, char __user *ubuf,
2283 size_t cnt, loff_t *ppos)
2285 unsigned long *p = filp->private_data;
2289 r = sprintf(buf, "%ld\n", *p);
2291 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2294 static struct file_operations tracing_read_long_fops = {
2295 .open = tracing_open_generic,
2296 .read = tracing_read_long,
2300 static struct dentry *d_tracer;
2302 struct dentry *tracing_init_dentry(void)
2309 d_tracer = debugfs_create_dir("tracing", NULL);
2311 if (!d_tracer && !once) {
2313 pr_warning("Could not create debugfs directory 'tracing'\n");
2320 #ifdef CONFIG_FTRACE_SELFTEST
2321 /* Let selftest have access to static functions in this file */
2322 #include "trace_selftest.c"
2325 static __init void tracer_init_debugfs(void)
2327 struct dentry *d_tracer;
2328 struct dentry *entry;
2330 d_tracer = tracing_init_dentry();
2332 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2333 &global_trace, &tracing_ctrl_fops);
2335 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2337 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2338 NULL, &tracing_iter_fops);
2340 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2342 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2343 &global_trace, &tracing_lt_fops);
2345 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2347 entry = debugfs_create_file("trace", 0444, d_tracer,
2348 &global_trace, &tracing_fops);
2350 pr_warning("Could not create debugfs 'trace' entry\n");
2352 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2353 &global_trace, &show_traces_fops);
2355 pr_warning("Could not create debugfs 'trace' entry\n");
2357 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2358 &global_trace, &set_tracer_fops);
2360 pr_warning("Could not create debugfs 'trace' entry\n");
2362 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2363 &tracing_max_latency,
2364 &tracing_max_lat_fops);
2366 pr_warning("Could not create debugfs "
2367 "'tracing_max_latency' entry\n");
2369 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2370 &tracing_thresh, &tracing_max_lat_fops);
2372 pr_warning("Could not create debugfs "
2373 "'tracing_threash' entry\n");
2374 entry = debugfs_create_file("README", 0644, d_tracer,
2375 NULL, &tracing_readme_fops);
2377 pr_warning("Could not create debugfs 'README' entry\n");
2379 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2380 NULL, &tracing_pipe_fops);
2382 pr_warning("Could not create debugfs "
2383 "'tracing_threash' entry\n");
2385 #ifdef CONFIG_DYNAMIC_FTRACE
2386 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2387 &ftrace_update_tot_cnt,
2388 &tracing_read_long_fops);
2390 pr_warning("Could not create debugfs "
2391 "'dyn_ftrace_total_info' entry\n");
2395 /* dummy trace to disable tracing */
2396 static struct tracer no_tracer __read_mostly =
2401 static int trace_alloc_page(void)
2403 struct trace_array_cpu *data;
2404 struct page *page, *tmp;
2409 /* first allocate a page for each CPU */
2410 for_each_possible_cpu(i) {
2411 array = (void *)__get_free_page(GFP_KERNEL);
2412 if (array == NULL) {
2413 printk(KERN_ERR "tracer: failed to allocate page"
2414 "for trace buffer!\n");
2418 page = virt_to_page(array);
2419 list_add(&page->lru, &pages);
2421 /* Only allocate if we are actually using the max trace */
2422 #ifdef CONFIG_TRACER_MAX_TRACE
2423 array = (void *)__get_free_page(GFP_KERNEL);
2424 if (array == NULL) {
2425 printk(KERN_ERR "tracer: failed to allocate page"
2426 "for trace buffer!\n");
2429 page = virt_to_page(array);
2430 list_add(&page->lru, &pages);
2434 /* Now that we successfully allocate a page per CPU, add them */
2435 for_each_possible_cpu(i) {
2436 data = global_trace.data[i];
2437 spin_lock_init(&data->lock);
2438 lockdep_set_class(&data->lock, &data->lock_key);
2439 page = list_entry(pages.next, struct page, lru);
2440 list_del_init(&page->lru);
2441 list_add_tail(&page->lru, &data->trace_pages);
2444 #ifdef CONFIG_TRACER_MAX_TRACE
2445 data = max_tr.data[i];
2446 spin_lock_init(&data->lock);
2447 lockdep_set_class(&data->lock, &data->lock_key);
2448 page = list_entry(pages.next, struct page, lru);
2449 list_del_init(&page->lru);
2450 list_add_tail(&page->lru, &data->trace_pages);
2454 global_trace.entries += ENTRIES_PER_PAGE;
2459 list_for_each_entry_safe(page, tmp, &pages, lru) {
2460 list_del_init(&page->lru);
2466 __init static int tracer_alloc_buffers(void)
2468 struct trace_array_cpu *data;
2475 global_trace.ctrl = tracer_enabled;
2477 /* Allocate the first page for all buffers */
2478 for_each_possible_cpu(i) {
2479 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2480 max_tr.data[i] = &per_cpu(max_data, i);
2482 array = (void *)__get_free_page(GFP_KERNEL);
2483 if (array == NULL) {
2484 printk(KERN_ERR "tracer: failed to allocate page"
2485 "for trace buffer!\n");
2489 /* set the array to the list */
2490 INIT_LIST_HEAD(&data->trace_pages);
2491 page = virt_to_page(array);
2492 list_add(&page->lru, &data->trace_pages);
2493 /* use the LRU flag to differentiate the two buffers */
2496 /* Only allocate if we are actually using the max trace */
2497 #ifdef CONFIG_TRACER_MAX_TRACE
2498 array = (void *)__get_free_page(GFP_KERNEL);
2499 if (array == NULL) {
2500 printk(KERN_ERR "tracer: failed to allocate page"
2501 "for trace buffer!\n");
2505 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2506 page = virt_to_page(array);
2507 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2513 * Since we allocate by orders of pages, we may be able to
2516 global_trace.entries = ENTRIES_PER_PAGE;
2519 while (global_trace.entries < trace_nr_entries) {
2520 if (trace_alloc_page())
2524 max_tr.entries = global_trace.entries;
2526 pr_info("tracer: %d pages allocated for %ld",
2527 pages, trace_nr_entries);
2528 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2529 pr_info(" actual entries %ld\n", global_trace.entries);
2531 tracer_init_debugfs();
2533 trace_init_cmdlines();
2535 register_tracer(&no_tracer);
2536 current_trace = &no_tracer;
2538 /* All seems OK, enable tracing */
2539 tracing_disabled = 0;
2544 for (i-- ; i >= 0; i--) {
2545 struct page *page, *tmp;
2546 struct trace_array_cpu *data = global_trace.data[i];
2549 list_for_each_entry_safe(page, tmp,
2550 &data->trace_pages, lru) {
2551 list_del_init(&page->lru);
2556 #ifdef CONFIG_TRACER_MAX_TRACE
2557 data = max_tr.data[i];
2559 list_for_each_entry_safe(page, tmp,
2560 &data->trace_pages, lru) {
2561 list_del_init(&page->lru);
2569 fs_initcall(tracer_alloc_buffers);