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 notrace 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 = 16384UL;
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,
95 enum trace_flag_type {
96 TRACE_FLAG_IRQS_OFF = 0x01,
97 TRACE_FLAG_NEED_RESCHED = 0x02,
98 TRACE_FLAG_HARDIRQ = 0x04,
99 TRACE_FLAG_SOFTIRQ = 0x08,
102 enum trace_iterator_flags {
103 TRACE_ITER_PRINT_PARENT = 0x01,
104 TRACE_ITER_SYM_OFFSET = 0x02,
105 TRACE_ITER_SYM_ADDR = 0x04,
106 TRACE_ITER_VERBOSE = 0x08,
107 TRACE_ITER_RAW = 0x10,
108 TRACE_ITER_HEX = 0x20,
109 TRACE_ITER_BIN = 0x40,
110 TRACE_ITER_BLOCK = 0x80,
113 #define TRACE_ITER_SYM_MASK \
114 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
116 /* These must match the bit postions above */
117 static const char *trace_options[] = {
129 static unsigned trace_flags;
131 static DEFINE_SPINLOCK(ftrace_max_lock);
134 * Copy the new maximum trace into the separate maximum-trace
135 * structure. (this way the maximum trace is permanently saved,
136 * for later retrieval via /debugfs/tracing/latency_trace)
139 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
141 struct trace_array_cpu *data = tr->data[cpu];
144 max_tr.time_start = data->preempt_timestamp;
146 data = max_tr.data[cpu];
147 data->saved_latency = tracing_max_latency;
149 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
150 data->pid = tsk->pid;
151 data->uid = tsk->uid;
152 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
153 data->policy = tsk->policy;
154 data->rt_priority = tsk->rt_priority;
156 /* record this tasks comm */
157 tracing_record_cmdline(current);
160 void check_pages(struct trace_array_cpu *data)
162 struct page *page, *tmp;
164 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
165 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
167 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
168 BUG_ON(page->lru.next->prev != &page->lru);
169 BUG_ON(page->lru.prev->next != &page->lru);
173 void *head_page(struct trace_array_cpu *data)
178 if (list_empty(&data->trace_pages))
181 page = list_entry(data->trace_pages.next, struct page, lru);
182 BUG_ON(&page->lru == &data->trace_pages);
184 return page_address(page);
188 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
190 int len = (PAGE_SIZE - 1) - s->len;
198 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
201 /* If we can't write it all, don't bother writing anything */
211 trace_seq_puts(struct trace_seq *s, const char *str)
213 int len = strlen(str);
215 if (len > ((PAGE_SIZE - 1) - s->len))
218 memcpy(s->buffer + s->len, str, len);
225 trace_seq_putc(struct trace_seq *s, unsigned char c)
227 if (s->len >= (PAGE_SIZE - 1))
230 s->buffer[s->len++] = c;
236 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
238 if (len > ((PAGE_SIZE - 1) - s->len))
241 memcpy(s->buffer + s->len, mem, len);
250 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
252 unsigned char hex[HEX_CHARS];
257 BUG_ON(len >= HEX_CHARS);
262 for (i = 0, j = 0; i < len; i++) {
264 for (i = len-1, j = 0; i >= 0; i--) {
268 hex[j] = byte & 0x0f;
285 return trace_seq_putmem(s, hex, j);
289 trace_seq_reset(struct trace_seq *s)
295 trace_print_seq(struct seq_file *m, struct trace_seq *s)
297 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
300 seq_puts(m, s->buffer);
306 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
308 struct list_head flip_pages;
310 INIT_LIST_HEAD(&flip_pages);
312 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
313 sizeof(struct trace_array_cpu) -
314 offsetof(struct trace_array_cpu, trace_head_idx));
318 list_splice_init(&tr1->trace_pages, &flip_pages);
319 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
320 list_splice_init(&flip_pages, &tr2->trace_pages);
321 BUG_ON(!list_empty(&flip_pages));
327 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
329 struct trace_array_cpu *data;
332 WARN_ON_ONCE(!irqs_disabled());
333 spin_lock(&ftrace_max_lock);
334 /* clear out all the previous traces */
335 for_each_possible_cpu(i) {
337 flip_trace(max_tr.data[i], data);
341 __update_max_tr(tr, tsk, cpu);
342 spin_unlock(&ftrace_max_lock);
346 * update_max_tr_single - only copy one trace over, and reset the rest
348 * @tsk - task with the latency
349 * @cpu - the cpu of the buffer to copy.
352 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
354 struct trace_array_cpu *data = tr->data[cpu];
357 WARN_ON_ONCE(!irqs_disabled());
358 spin_lock(&ftrace_max_lock);
359 for_each_possible_cpu(i)
360 tracing_reset(max_tr.data[i]);
362 flip_trace(max_tr.data[cpu], data);
365 __update_max_tr(tr, tsk, cpu);
366 spin_unlock(&ftrace_max_lock);
369 int register_tracer(struct tracer *type)
376 pr_info("Tracer must have a name\n");
380 mutex_lock(&trace_types_lock);
381 for (t = trace_types; t; t = t->next) {
382 if (strcmp(type->name, t->name) == 0) {
384 pr_info("Trace %s already registered\n",
391 #ifdef CONFIG_FTRACE_STARTUP_TEST
392 if (type->selftest) {
393 struct tracer *saved_tracer = current_trace;
394 struct trace_array_cpu *data;
395 struct trace_array *tr = &global_trace;
396 int saved_ctrl = tr->ctrl;
399 * Run a selftest on this tracer.
400 * Here we reset the trace buffer, and set the current
401 * tracer to be this tracer. The tracer can then run some
402 * internal tracing to verify that everything is in order.
403 * If we fail, we do not register this tracer.
405 for_each_possible_cpu(i) {
407 if (!head_page(data))
411 current_trace = type;
413 /* the test is responsible for initializing and enabling */
414 pr_info("Testing tracer %s: ", type->name);
415 ret = type->selftest(type, tr);
416 /* the test is responsible for resetting too */
417 current_trace = saved_tracer;
418 tr->ctrl = saved_ctrl;
420 printk(KERN_CONT "FAILED!\n");
423 /* Only reset on passing, to avoid touching corrupted buffers */
424 for_each_possible_cpu(i) {
426 if (!head_page(data))
430 printk(KERN_CONT "PASSED\n");
434 type->next = trace_types;
436 len = strlen(type->name);
437 if (len > max_tracer_type_len)
438 max_tracer_type_len = len;
441 mutex_unlock(&trace_types_lock);
446 void unregister_tracer(struct tracer *type)
451 mutex_lock(&trace_types_lock);
452 for (t = &trace_types; *t; t = &(*t)->next) {
456 pr_info("Trace %s not registered\n", type->name);
461 if (strlen(type->name) != max_tracer_type_len)
464 max_tracer_type_len = 0;
465 for (t = &trace_types; *t; t = &(*t)->next) {
466 len = strlen((*t)->name);
467 if (len > max_tracer_type_len)
468 max_tracer_type_len = len;
471 mutex_unlock(&trace_types_lock);
474 notrace void tracing_reset(struct trace_array_cpu *data)
477 data->trace_head = data->trace_tail = head_page(data);
478 data->trace_head_idx = 0;
479 data->trace_tail_idx = 0;
482 #define SAVED_CMDLINES 128
483 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
484 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
485 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
486 static int cmdline_idx;
487 static DEFINE_SPINLOCK(trace_cmdline_lock);
488 atomic_t trace_record_cmdline_disabled;
490 static void trace_init_cmdlines(void)
492 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
493 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
497 notrace void trace_stop_cmdline_recording(void);
499 static notrace void trace_save_cmdline(struct task_struct *tsk)
504 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
508 * It's not the end of the world if we don't get
509 * the lock, but we also don't want to spin
510 * nor do we want to disable interrupts,
511 * so if we miss here, then better luck next time.
513 if (!spin_trylock(&trace_cmdline_lock))
516 idx = map_pid_to_cmdline[tsk->pid];
517 if (idx >= SAVED_CMDLINES) {
518 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
520 map = map_cmdline_to_pid[idx];
521 if (map <= PID_MAX_DEFAULT)
522 map_pid_to_cmdline[map] = (unsigned)-1;
524 map_pid_to_cmdline[tsk->pid] = idx;
529 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
531 spin_unlock(&trace_cmdline_lock);
534 static notrace char *trace_find_cmdline(int pid)
536 char *cmdline = "<...>";
542 if (pid > PID_MAX_DEFAULT)
545 map = map_pid_to_cmdline[pid];
546 if (map >= SAVED_CMDLINES)
549 cmdline = saved_cmdlines[map];
555 notrace void tracing_record_cmdline(struct task_struct *tsk)
557 if (atomic_read(&trace_record_cmdline_disabled))
560 trace_save_cmdline(tsk);
563 static inline notrace struct list_head *
564 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
567 * Roundrobin - but skip the head (which is not a real page):
570 if (unlikely(next == &data->trace_pages))
572 BUG_ON(next == &data->trace_pages);
577 static inline notrace void *
578 trace_next_page(struct trace_array_cpu *data, void *addr)
580 struct list_head *next;
583 page = virt_to_page(addr);
585 next = trace_next_list(data, &page->lru);
586 page = list_entry(next, struct page, lru);
588 return page_address(page);
591 static inline notrace struct trace_entry *
592 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
594 unsigned long idx, idx_next;
595 struct trace_entry *entry;
598 idx = data->trace_head_idx;
601 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
603 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
605 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
606 data->trace_head = trace_next_page(data, data->trace_head);
610 if (data->trace_head == data->trace_tail &&
611 idx_next == data->trace_tail_idx) {
613 data->trace_tail_idx++;
614 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
616 trace_next_page(data, data->trace_tail);
617 data->trace_tail_idx = 0;
621 data->trace_head_idx = idx_next;
626 static inline notrace void
627 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
629 struct task_struct *tsk = current;
632 pc = preempt_count();
634 entry->preempt_count = pc & 0xff;
635 entry->pid = tsk->pid;
636 entry->t = ftrace_now(raw_smp_processor_id());
637 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
638 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
639 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
640 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
644 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
645 unsigned long ip, unsigned long parent_ip, unsigned long flags)
647 struct trace_entry *entry;
648 unsigned long irq_flags;
650 spin_lock_irqsave(&data->lock, irq_flags);
651 entry = tracing_get_trace_entry(tr, data);
652 tracing_generic_entry_update(entry, flags);
653 entry->type = TRACE_FN;
655 entry->fn.parent_ip = parent_ip;
656 spin_unlock_irqrestore(&data->lock, irq_flags);
658 if (!(trace_flags & TRACE_ITER_BLOCK))
659 wake_up (&trace_wait);
663 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
664 unsigned long ip, unsigned long parent_ip, unsigned long flags)
666 if (likely(!atomic_read(&data->disabled)))
667 trace_function(tr, data, ip, parent_ip, flags);
671 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
672 unsigned long arg1, unsigned long arg2, unsigned long arg3)
674 struct trace_entry *entry;
675 unsigned long irq_flags;
677 spin_lock_irqsave(&data->lock, irq_flags);
678 entry = tracing_get_trace_entry(tr, data);
679 tracing_generic_entry_update(entry, 0);
680 entry->type = TRACE_SPECIAL;
681 entry->special.arg1 = arg1;
682 entry->special.arg2 = arg2;
683 entry->special.arg3 = arg3;
684 spin_unlock_irqrestore(&data->lock, irq_flags);
686 if (!(trace_flags & TRACE_ITER_BLOCK))
687 wake_up (&trace_wait);
691 tracing_sched_switch_trace(struct trace_array *tr,
692 struct trace_array_cpu *data,
693 struct task_struct *prev, struct task_struct *next,
696 struct trace_entry *entry;
697 unsigned long irq_flags;
699 spin_lock_irqsave(&data->lock, irq_flags);
700 entry = tracing_get_trace_entry(tr, data);
701 tracing_generic_entry_update(entry, flags);
702 entry->type = TRACE_CTX;
703 entry->ctx.prev_pid = prev->pid;
704 entry->ctx.prev_prio = prev->prio;
705 entry->ctx.prev_state = prev->state;
706 entry->ctx.next_pid = next->pid;
707 entry->ctx.next_prio = next->prio;
708 spin_unlock_irqrestore(&data->lock, irq_flags);
710 if (!(trace_flags & TRACE_ITER_BLOCK))
711 wake_up (&trace_wait);
716 function_trace_call(unsigned long ip, unsigned long parent_ip)
718 struct trace_array *tr = &global_trace;
719 struct trace_array_cpu *data;
724 if (unlikely(!tracer_enabled))
727 local_irq_save(flags);
728 cpu = raw_smp_processor_id();
729 data = tr->data[cpu];
730 disabled = atomic_inc_return(&data->disabled);
732 if (likely(disabled == 1))
733 trace_function(tr, data, ip, parent_ip, flags);
735 atomic_dec(&data->disabled);
736 local_irq_restore(flags);
739 static struct ftrace_ops trace_ops __read_mostly =
741 .func = function_trace_call,
744 notrace void tracing_start_function_trace(void)
746 register_ftrace_function(&trace_ops);
749 notrace void tracing_stop_function_trace(void)
751 unregister_ftrace_function(&trace_ops);
755 enum trace_file_type {
756 TRACE_FILE_LAT_FMT = 1,
759 static struct trace_entry *
760 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
761 struct trace_iterator *iter, int cpu)
764 struct trace_entry *array;
766 if (iter->next_idx[cpu] >= tr->entries ||
767 iter->next_idx[cpu] >= data->trace_idx ||
768 (data->trace_head == data->trace_tail &&
769 data->trace_head_idx == data->trace_tail_idx))
772 if (!iter->next_page[cpu]) {
773 /* Initialize the iterator for this cpu trace buffer */
774 WARN_ON(!data->trace_tail);
775 page = virt_to_page(data->trace_tail);
776 iter->next_page[cpu] = &page->lru;
777 iter->next_page_idx[cpu] = data->trace_tail_idx;
780 page = list_entry(iter->next_page[cpu], struct page, lru);
781 BUG_ON(&data->trace_pages == &page->lru);
783 array = page_address(page);
785 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
786 return &array[iter->next_page_idx[cpu]];
789 static struct trace_entry * notrace
790 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
792 struct trace_array *tr = iter->tr;
793 struct trace_entry *ent, *next = NULL;
797 for_each_possible_cpu(cpu) {
798 if (!head_page(tr->data[cpu]))
800 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
802 * Pick the entry with the smallest timestamp:
804 if (ent && (!next || ent->t < next->t)) {
816 static notrace void trace_iterator_increment(struct trace_iterator *iter)
819 iter->next_idx[iter->cpu]++;
820 iter->next_page_idx[iter->cpu]++;
822 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
823 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
825 iter->next_page_idx[iter->cpu] = 0;
826 iter->next_page[iter->cpu] =
827 trace_next_list(data, iter->next_page[iter->cpu]);
831 static notrace void trace_consume(struct trace_iterator *iter)
833 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
835 data->trace_tail_idx++;
836 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
837 data->trace_tail = trace_next_page(data, data->trace_tail);
838 data->trace_tail_idx = 0;
841 /* Check if we empty it, then reset the index */
842 if (data->trace_head == data->trace_tail &&
843 data->trace_head_idx == data->trace_tail_idx)
847 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
849 struct trace_entry *next;
852 next = find_next_entry(iter, &next_cpu);
854 iter->prev_ent = iter->ent;
855 iter->prev_cpu = iter->cpu;
858 iter->cpu = next_cpu;
861 trace_iterator_increment(iter);
863 return next ? iter : NULL;
866 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
868 struct trace_iterator *iter = m->private;
869 void *last_ent = iter->ent;
875 /* can't go backwards */
880 ent = find_next_entry_inc(iter);
884 while (ent && iter->idx < i)
885 ent = find_next_entry_inc(iter);
889 if (last_ent && !ent)
890 seq_puts(m, "\n\nvim:ft=help\n");
895 static void *s_start(struct seq_file *m, loff_t *pos)
897 struct trace_iterator *iter = m->private;
902 mutex_lock(&trace_types_lock);
904 if (!current_trace || current_trace != iter->trace)
907 atomic_inc(&trace_record_cmdline_disabled);
909 /* let the tracer grab locks here if needed */
910 if (current_trace->start)
911 current_trace->start(iter);
913 if (*pos != iter->pos) {
917 iter->prev_ent = NULL;
920 for_each_possible_cpu(i) {
921 iter->next_idx[i] = 0;
922 iter->next_page[i] = NULL;
925 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
930 p = s_next(m, p, &l);
936 static void s_stop(struct seq_file *m, void *p)
938 struct trace_iterator *iter = m->private;
940 atomic_dec(&trace_record_cmdline_disabled);
942 /* let the tracer release locks here if needed */
943 if (current_trace && current_trace == iter->trace && iter->trace->stop)
944 iter->trace->stop(iter);
946 mutex_unlock(&trace_types_lock);
950 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
952 #ifdef CONFIG_KALLSYMS
953 char str[KSYM_SYMBOL_LEN];
955 kallsyms_lookup(address, NULL, NULL, NULL, str);
957 return trace_seq_printf(s, fmt, str);
963 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
964 unsigned long address)
966 #ifdef CONFIG_KALLSYMS
967 char str[KSYM_SYMBOL_LEN];
969 sprint_symbol(str, address);
970 return trace_seq_printf(s, fmt, str);
976 # define IP_FMT "%08lx"
978 # define IP_FMT "%016lx"
982 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
987 return trace_seq_printf(s, "0");
989 if (sym_flags & TRACE_ITER_SYM_OFFSET)
990 ret = seq_print_sym_offset(s, "%s", ip);
992 ret = seq_print_sym_short(s, "%s", ip);
997 if (sym_flags & TRACE_ITER_SYM_ADDR)
998 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1002 static notrace void print_lat_help_header(struct seq_file *m)
1004 seq_puts(m, "# _------=> CPU# \n");
1005 seq_puts(m, "# / _-----=> irqs-off \n");
1006 seq_puts(m, "# | / _----=> need-resched \n");
1007 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1008 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1009 seq_puts(m, "# |||| / \n");
1010 seq_puts(m, "# ||||| delay \n");
1011 seq_puts(m, "# cmd pid ||||| time | caller \n");
1012 seq_puts(m, "# \\ / ||||| \\ | / \n");
1015 static notrace void print_func_help_header(struct seq_file *m)
1017 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1018 seq_puts(m, "# | | | | |\n");
1023 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1025 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1026 struct trace_array *tr = iter->tr;
1027 struct trace_array_cpu *data = tr->data[tr->cpu];
1028 struct tracer *type = current_trace;
1029 unsigned long total = 0;
1030 unsigned long entries = 0;
1032 const char *name = "preemption";
1037 for_each_possible_cpu(cpu) {
1038 if (head_page(tr->data[cpu])) {
1039 total += tr->data[cpu]->trace_idx;
1040 if (tr->data[cpu]->trace_idx > tr->entries)
1041 entries += tr->entries;
1043 entries += tr->data[cpu]->trace_idx;
1047 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1049 seq_puts(m, "-----------------------------------"
1050 "---------------------------------\n");
1051 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1052 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1053 nsecs_to_usecs(data->saved_latency),
1057 #if defined(CONFIG_PREEMPT_NONE)
1059 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1061 #elif defined(CONFIG_PREEMPT_DESKTOP)
1066 /* These are reserved for later use */
1069 seq_printf(m, " #P:%d)\n", num_online_cpus());
1073 seq_puts(m, " -----------------\n");
1074 seq_printf(m, " | task: %.16s-%d "
1075 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1076 data->comm, data->pid, data->uid, data->nice,
1077 data->policy, data->rt_priority);
1078 seq_puts(m, " -----------------\n");
1080 if (data->critical_start) {
1081 seq_puts(m, " => started at: ");
1082 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1083 trace_print_seq(m, &iter->seq);
1084 seq_puts(m, "\n => ended at: ");
1085 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1086 trace_print_seq(m, &iter->seq);
1094 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1096 int hardirq, softirq;
1099 comm = trace_find_cmdline(entry->pid);
1101 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1102 trace_seq_printf(s, "%d", cpu);
1103 trace_seq_printf(s, "%c%c",
1104 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1105 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1107 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1108 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1109 if (hardirq && softirq)
1110 trace_seq_putc(s, 'H');
1113 trace_seq_putc(s, 'h');
1116 trace_seq_putc(s, 's');
1118 trace_seq_putc(s, '.');
1122 if (entry->preempt_count)
1123 trace_seq_printf(s, "%x", entry->preempt_count);
1125 trace_seq_puts(s, ".");
1128 unsigned long preempt_mark_thresh = 100;
1131 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1132 unsigned long rel_usecs)
1134 trace_seq_printf(s, " %4lldus", abs_usecs);
1135 if (rel_usecs > preempt_mark_thresh)
1136 trace_seq_puts(s, "!: ");
1137 else if (rel_usecs > 1)
1138 trace_seq_puts(s, "+: ");
1140 trace_seq_puts(s, " : ");
1143 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1146 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1148 struct trace_seq *s = &iter->seq;
1149 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1150 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1151 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1152 struct trace_entry *entry = iter->ent;
1153 unsigned long abs_usecs;
1154 unsigned long rel_usecs;
1160 rel_usecs = ns2usecs(next_entry->t - entry->t);
1161 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1164 comm = trace_find_cmdline(entry->pid);
1165 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1166 " %ld.%03ldms (+%ld.%03ldms): ",
1168 entry->pid, cpu, entry->flags,
1169 entry->preempt_count, trace_idx,
1172 abs_usecs % 1000, rel_usecs/1000,
1175 lat_print_generic(s, entry, cpu);
1176 lat_print_timestamp(s, abs_usecs, rel_usecs);
1178 switch (entry->type) {
1180 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1181 trace_seq_puts(s, " (");
1182 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1183 trace_seq_puts(s, ")\n");
1186 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1187 state_to_char[entry->ctx.prev_state] : 'X';
1188 comm = trace_find_cmdline(entry->ctx.next_pid);
1189 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1190 entry->ctx.prev_pid,
1191 entry->ctx.prev_prio,
1193 entry->ctx.next_pid,
1194 entry->ctx.next_prio,
1198 trace_seq_printf(s, " %lx %lx %lx\n",
1199 entry->special.arg1,
1200 entry->special.arg2,
1201 entry->special.arg3);
1204 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1209 static notrace int print_trace_fmt(struct trace_iterator *iter)
1211 struct trace_seq *s = &iter->seq;
1212 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1213 struct trace_entry *entry;
1214 unsigned long usec_rem;
1215 unsigned long long t;
1223 comm = trace_find_cmdline(iter->ent->pid);
1225 t = ns2usecs(entry->t);
1226 usec_rem = do_div(t, 1000000ULL);
1227 secs = (unsigned long)t;
1229 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1232 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1235 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1239 switch (entry->type) {
1241 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1244 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1245 entry->fn.parent_ip) {
1246 ret = trace_seq_printf(s, " <-");
1249 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1254 ret = trace_seq_printf(s, "\n");
1259 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1260 state_to_char[entry->ctx.prev_state] : 'X';
1261 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1262 entry->ctx.prev_pid,
1263 entry->ctx.prev_prio,
1265 entry->ctx.next_pid,
1266 entry->ctx.next_prio);
1271 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1272 entry->special.arg1,
1273 entry->special.arg2,
1274 entry->special.arg3);
1282 static notrace int print_raw_fmt(struct trace_iterator *iter)
1284 struct trace_seq *s = &iter->seq;
1285 struct trace_entry *entry;
1291 ret = trace_seq_printf(s, "%d %d %llu ",
1292 entry->pid, iter->cpu, entry->t);
1296 switch (entry->type) {
1298 ret = trace_seq_printf(s, "%x %x\n",
1299 entry->fn.ip, entry->fn.parent_ip);
1304 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1305 state_to_char[entry->ctx.prev_state] : 'X';
1306 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1307 entry->ctx.prev_pid,
1308 entry->ctx.prev_prio,
1310 entry->ctx.next_pid,
1311 entry->ctx.next_prio);
1316 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1317 entry->special.arg1,
1318 entry->special.arg2,
1319 entry->special.arg3);
1327 #define SEQ_PUT_FIELD_RET(s, x) \
1329 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1333 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1335 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1339 static notrace int print_hex_fmt(struct trace_iterator *iter)
1341 struct trace_seq *s = &iter->seq;
1342 unsigned char newline = '\n';
1343 struct trace_entry *entry;
1348 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1349 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1350 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1352 switch (entry->type) {
1354 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1355 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1358 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1359 state_to_char[entry->ctx.prev_state] : 'X';
1360 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1361 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1362 SEQ_PUT_HEX_FIELD_RET(s, S);
1363 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1364 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1365 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1368 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1369 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1370 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1373 SEQ_PUT_FIELD_RET(s, newline);
1378 static notrace int print_bin_fmt(struct trace_iterator *iter)
1380 struct trace_seq *s = &iter->seq;
1381 struct trace_entry *entry;
1385 SEQ_PUT_FIELD_RET(s, entry->pid);
1386 SEQ_PUT_FIELD_RET(s, entry->cpu);
1387 SEQ_PUT_FIELD_RET(s, entry->t);
1389 switch (entry->type) {
1391 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1392 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1395 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1396 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1397 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1398 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1399 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1402 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1403 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1404 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1410 static int trace_empty(struct trace_iterator *iter)
1412 struct trace_array_cpu *data;
1415 for_each_possible_cpu(cpu) {
1416 data = iter->tr->data[cpu];
1418 if (head_page(data) && data->trace_idx &&
1419 (data->trace_tail != data->trace_head ||
1420 data->trace_tail_idx != data->trace_head_idx))
1426 static int print_trace_line(struct trace_iterator *iter)
1428 if (trace_flags & TRACE_ITER_BIN)
1429 return print_bin_fmt(iter);
1431 if (trace_flags & TRACE_ITER_HEX)
1432 return print_hex_fmt(iter);
1434 if (trace_flags & TRACE_ITER_RAW)
1435 return print_raw_fmt(iter);
1437 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1438 return print_lat_fmt(iter, iter->idx, iter->cpu);
1440 return print_trace_fmt(iter);
1443 static int s_show(struct seq_file *m, void *v)
1445 struct trace_iterator *iter = v;
1447 if (iter->ent == NULL) {
1449 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1452 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1453 /* print nothing if the buffers are empty */
1454 if (trace_empty(iter))
1456 print_trace_header(m, iter);
1457 if (!(trace_flags & TRACE_ITER_VERBOSE))
1458 print_lat_help_header(m);
1460 if (!(trace_flags & TRACE_ITER_VERBOSE))
1461 print_func_help_header(m);
1464 print_trace_line(iter);
1465 trace_print_seq(m, &iter->seq);
1471 static struct seq_operations tracer_seq_ops = {
1478 static struct trace_iterator notrace *
1479 __tracing_open(struct inode *inode, struct file *file, int *ret)
1481 struct trace_iterator *iter;
1483 if (tracing_disabled) {
1488 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1494 mutex_lock(&trace_types_lock);
1495 if (current_trace && current_trace->print_max)
1498 iter->tr = inode->i_private;
1499 iter->trace = current_trace;
1502 /* TODO stop tracer */
1503 *ret = seq_open(file, &tracer_seq_ops);
1505 struct seq_file *m = file->private_data;
1508 /* stop the trace while dumping */
1512 if (iter->trace && iter->trace->open)
1513 iter->trace->open(iter);
1518 mutex_unlock(&trace_types_lock);
1524 int tracing_open_generic(struct inode *inode, struct file *filp)
1526 if (tracing_disabled)
1529 filp->private_data = inode->i_private;
1533 int tracing_release(struct inode *inode, struct file *file)
1535 struct seq_file *m = (struct seq_file *)file->private_data;
1536 struct trace_iterator *iter = m->private;
1538 mutex_lock(&trace_types_lock);
1539 if (iter->trace && iter->trace->close)
1540 iter->trace->close(iter);
1542 /* reenable tracing if it was previously enabled */
1545 mutex_unlock(&trace_types_lock);
1547 seq_release(inode, file);
1552 static int tracing_open(struct inode *inode, struct file *file)
1556 __tracing_open(inode, file, &ret);
1561 static int tracing_lt_open(struct inode *inode, struct file *file)
1563 struct trace_iterator *iter;
1566 iter = __tracing_open(inode, file, &ret);
1569 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1575 static notrace void *
1576 t_next(struct seq_file *m, void *v, loff_t *pos)
1578 struct tracer *t = m->private;
1590 static void *t_start(struct seq_file *m, loff_t *pos)
1592 struct tracer *t = m->private;
1595 mutex_lock(&trace_types_lock);
1596 for (; t && l < *pos; t = t_next(m, t, &l))
1602 static void t_stop(struct seq_file *m, void *p)
1604 mutex_unlock(&trace_types_lock);
1607 static int t_show(struct seq_file *m, void *v)
1609 struct tracer *t = v;
1614 seq_printf(m, "%s", t->name);
1623 static struct seq_operations show_traces_seq_ops = {
1630 static int show_traces_open(struct inode *inode, struct file *file)
1634 if (tracing_disabled)
1637 ret = seq_open(file, &show_traces_seq_ops);
1639 struct seq_file *m = file->private_data;
1640 m->private = trace_types;
1646 static struct file_operations tracing_fops = {
1647 .open = tracing_open,
1649 .llseek = seq_lseek,
1650 .release = tracing_release,
1653 static struct file_operations tracing_lt_fops = {
1654 .open = tracing_lt_open,
1656 .llseek = seq_lseek,
1657 .release = tracing_release,
1660 static struct file_operations show_traces_fops = {
1661 .open = show_traces_open,
1663 .release = seq_release,
1667 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1668 size_t cnt, loff_t *ppos)
1675 /* calulate max size */
1676 for (i = 0; trace_options[i]; i++) {
1677 len += strlen(trace_options[i]);
1678 len += 3; /* "no" and space */
1681 /* +2 for \n and \0 */
1682 buf = kmalloc(len + 2, GFP_KERNEL);
1686 for (i = 0; trace_options[i]; i++) {
1687 if (trace_flags & (1 << i))
1688 r += sprintf(buf + r, "%s ", trace_options[i]);
1690 r += sprintf(buf + r, "no%s ", trace_options[i]);
1693 r += sprintf(buf + r, "\n");
1694 WARN_ON(r >= len + 2);
1696 r = simple_read_from_buffer(ubuf, cnt, ppos,
1705 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1706 size_t cnt, loff_t *ppos)
1716 if (copy_from_user(&buf, ubuf, cnt))
1721 if (strncmp(buf, "no", 2) == 0) {
1726 for (i = 0; trace_options[i]; i++) {
1727 int len = strlen(trace_options[i]);
1729 if (strncmp(cmp, trace_options[i], len) == 0) {
1731 trace_flags &= ~(1 << i);
1733 trace_flags |= (1 << i);
1743 static struct file_operations tracing_iter_fops = {
1744 .open = tracing_open_generic,
1745 .read = tracing_iter_ctrl_read,
1746 .write = tracing_iter_ctrl_write,
1749 static const char readme_msg[] =
1750 "tracing mini-HOWTO:\n\n"
1752 "# mount -t debugfs nodev /debug\n\n"
1753 "# cat /debug/tracing/available_tracers\n"
1754 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1755 "# cat /debug/tracing/current_tracer\n"
1757 "# echo sched_switch > /debug/tracing/current_tracer\n"
1758 "# cat /debug/tracing/current_tracer\n"
1760 "# cat /debug/tracing/iter_ctrl\n"
1761 "noprint-parent nosym-offset nosym-addr noverbose\n"
1762 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1763 "# echo 1 > /debug/tracing/tracing_enabled\n"
1764 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1765 "echo 0 > /debug/tracing/tracing_enabled\n"
1769 tracing_readme_read(struct file *filp, char __user *ubuf,
1770 size_t cnt, loff_t *ppos)
1772 return simple_read_from_buffer(ubuf, cnt, ppos,
1773 readme_msg, strlen(readme_msg));
1776 static struct file_operations tracing_readme_fops = {
1777 .open = tracing_open_generic,
1778 .read = tracing_readme_read,
1782 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1783 size_t cnt, loff_t *ppos)
1785 struct trace_array *tr = filp->private_data;
1789 r = sprintf(buf, "%ld\n", tr->ctrl);
1790 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1794 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1795 size_t cnt, loff_t *ppos)
1797 struct trace_array *tr = filp->private_data;
1804 if (copy_from_user(&buf, ubuf, cnt))
1809 val = simple_strtoul(buf, NULL, 10);
1813 mutex_lock(&trace_types_lock);
1814 if (tr->ctrl ^ val) {
1822 if (current_trace && current_trace->ctrl_update)
1823 current_trace->ctrl_update(tr);
1825 mutex_unlock(&trace_types_lock);
1833 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1834 size_t cnt, loff_t *ppos)
1836 char buf[max_tracer_type_len+2];
1839 mutex_lock(&trace_types_lock);
1841 r = sprintf(buf, "%s\n", current_trace->name);
1843 r = sprintf(buf, "\n");
1844 mutex_unlock(&trace_types_lock);
1846 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1850 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1851 size_t cnt, loff_t *ppos)
1853 struct trace_array *tr = &global_trace;
1855 char buf[max_tracer_type_len+1];
1858 if (cnt > max_tracer_type_len)
1859 cnt = max_tracer_type_len;
1861 if (copy_from_user(&buf, ubuf, cnt))
1866 /* strip ending whitespace. */
1867 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1870 mutex_lock(&trace_types_lock);
1871 for (t = trace_types; t; t = t->next) {
1872 if (strcmp(t->name, buf) == 0)
1875 if (!t || t == current_trace)
1878 if (current_trace && current_trace->reset)
1879 current_trace->reset(tr);
1886 mutex_unlock(&trace_types_lock);
1894 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1895 size_t cnt, loff_t *ppos)
1897 unsigned long *ptr = filp->private_data;
1901 r = snprintf(buf, 64, "%ld\n",
1902 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1905 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1909 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1910 size_t cnt, loff_t *ppos)
1912 long *ptr = filp->private_data;
1919 if (copy_from_user(&buf, ubuf, cnt))
1924 val = simple_strtoul(buf, NULL, 10);
1931 static atomic_t tracing_reader;
1933 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1935 struct trace_iterator *iter;
1937 if (tracing_disabled)
1940 /* We only allow for reader of the pipe */
1941 if (atomic_inc_return(&tracing_reader) != 1) {
1942 atomic_dec(&tracing_reader);
1946 /* create a buffer to store the information to pass to userspace */
1947 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1951 iter->tr = &global_trace;
1953 filp->private_data = iter;
1958 static int tracing_release_pipe(struct inode *inode, struct file *file)
1960 struct trace_iterator *iter = file->private_data;
1963 atomic_dec(&tracing_reader);
1969 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
1971 struct trace_iterator *iter = filp->private_data;
1973 if (trace_flags & TRACE_ITER_BLOCK) {
1975 * Always select as readable when in blocking mode
1977 return POLLIN | POLLRDNORM;
1980 if (!trace_empty(iter))
1981 return POLLIN | POLLRDNORM;
1982 poll_wait(filp, &trace_wait, poll_table);
1983 if (!trace_empty(iter))
1984 return POLLIN | POLLRDNORM;
1994 tracing_read_pipe(struct file *filp, char __user *ubuf,
1995 size_t cnt, loff_t *ppos)
1997 struct trace_iterator *iter = filp->private_data;
1998 struct trace_array_cpu *data;
1999 static cpumask_t mask;
2001 unsigned long flags;
2002 #ifdef CONFIG_FTRACE
2010 /* return any leftover data */
2011 if (iter->seq.len > start) {
2012 len = iter->seq.len - start;
2015 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2024 trace_seq_reset(&iter->seq);
2027 while (trace_empty(iter)) {
2028 if (!(trace_flags & TRACE_ITER_BLOCK))
2029 return -EWOULDBLOCK;
2031 * This is a make-shift waitqueue. The reason we don't use
2032 * an actual wait queue is because:
2033 * 1) we only ever have one waiter
2034 * 2) the tracing, traces all functions, we don't want
2035 * the overhead of calling wake_up and friends
2036 * (and tracing them too)
2037 * Anyway, this is really very primitive wakeup.
2039 set_current_state(TASK_INTERRUPTIBLE);
2040 iter->tr->waiter = current;
2042 /* sleep for one second, and try again. */
2043 schedule_timeout(HZ);
2045 iter->tr->waiter = NULL;
2047 if (signal_pending(current))
2051 * We block until we read something and tracing is disabled.
2052 * We still block if tracing is disabled, but we have never
2053 * read anything. This allows a user to cat this file, and
2054 * then enable tracing. But after we have read something,
2055 * we give an EOF when tracing is again disabled.
2057 * iter->pos will be 0 if we haven't read anything.
2059 if (!tracer_enabled && iter->pos)
2065 /* stop when tracing is finished */
2066 if (trace_empty(iter))
2069 if (cnt >= PAGE_SIZE)
2070 cnt = PAGE_SIZE - 1;
2072 memset(iter, 0, sizeof(*iter));
2073 iter->tr = &global_trace;
2077 * We need to stop all tracing on all CPUS to read the
2078 * the next buffer. This is a bit expensive, but is
2079 * not done often. We fill all what we can read,
2080 * and then release the locks again.
2084 local_irq_save(flags);
2085 #ifdef CONFIG_FTRACE
2086 ftrace_save = ftrace_enabled;
2090 for_each_possible_cpu(cpu) {
2091 data = iter->tr->data[cpu];
2093 if (!head_page(data) || !data->trace_idx)
2096 atomic_inc(&data->disabled);
2100 for_each_cpu_mask(cpu, mask) {
2101 data = iter->tr->data[cpu];
2102 spin_lock(&data->lock);
2105 while (find_next_entry_inc(iter) != NULL) {
2106 int len = iter->seq.len;
2108 ret = print_trace_line(iter);
2110 /* don't print partial lines */
2111 iter->seq.len = len;
2115 trace_consume(iter);
2117 if (iter->seq.len >= cnt)
2121 for_each_cpu_mask(cpu, mask) {
2122 data = iter->tr->data[cpu];
2123 spin_unlock(&data->lock);
2126 for_each_cpu_mask(cpu, mask) {
2127 data = iter->tr->data[cpu];
2128 atomic_dec(&data->disabled);
2130 #ifdef CONFIG_FTRACE
2131 ftrace_enabled = ftrace_save;
2133 local_irq_restore(flags);
2135 /* Now copy what we have to the user */
2136 read = iter->seq.len;
2140 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2142 if (read < iter->seq.len)
2145 trace_seq_reset(&iter->seq);
2153 static struct file_operations tracing_max_lat_fops = {
2154 .open = tracing_open_generic,
2155 .read = tracing_max_lat_read,
2156 .write = tracing_max_lat_write,
2159 static struct file_operations tracing_ctrl_fops = {
2160 .open = tracing_open_generic,
2161 .read = tracing_ctrl_read,
2162 .write = tracing_ctrl_write,
2165 static struct file_operations set_tracer_fops = {
2166 .open = tracing_open_generic,
2167 .read = tracing_set_trace_read,
2168 .write = tracing_set_trace_write,
2171 static struct file_operations tracing_pipe_fops = {
2172 .open = tracing_open_pipe,
2173 .poll = tracing_poll_pipe,
2174 .read = tracing_read_pipe,
2175 .release = tracing_release_pipe,
2178 #ifdef CONFIG_DYNAMIC_FTRACE
2181 tracing_read_long(struct file *filp, char __user *ubuf,
2182 size_t cnt, loff_t *ppos)
2184 unsigned long *p = filp->private_data;
2188 r = sprintf(buf, "%ld\n", *p);
2190 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2193 static struct file_operations tracing_read_long_fops = {
2194 .open = tracing_open_generic,
2195 .read = tracing_read_long,
2199 static struct dentry *d_tracer;
2201 struct dentry *tracing_init_dentry(void)
2208 d_tracer = debugfs_create_dir("tracing", NULL);
2210 if (!d_tracer && !once) {
2212 pr_warning("Could not create debugfs directory 'tracing'\n");
2219 #ifdef CONFIG_FTRACE_SELFTEST
2220 /* Let selftest have access to static functions in this file */
2221 #include "trace_selftest.c"
2224 static __init void tracer_init_debugfs(void)
2226 struct dentry *d_tracer;
2227 struct dentry *entry;
2229 d_tracer = tracing_init_dentry();
2231 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2232 &global_trace, &tracing_ctrl_fops);
2234 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2236 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2237 NULL, &tracing_iter_fops);
2239 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2241 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2242 &global_trace, &tracing_lt_fops);
2244 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2246 entry = debugfs_create_file("trace", 0444, d_tracer,
2247 &global_trace, &tracing_fops);
2249 pr_warning("Could not create debugfs 'trace' entry\n");
2251 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2252 &global_trace, &show_traces_fops);
2254 pr_warning("Could not create debugfs 'trace' entry\n");
2256 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2257 &global_trace, &set_tracer_fops);
2259 pr_warning("Could not create debugfs 'trace' entry\n");
2261 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2262 &tracing_max_latency,
2263 &tracing_max_lat_fops);
2265 pr_warning("Could not create debugfs "
2266 "'tracing_max_latency' entry\n");
2268 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2269 &tracing_thresh, &tracing_max_lat_fops);
2271 pr_warning("Could not create debugfs "
2272 "'tracing_threash' entry\n");
2273 entry = debugfs_create_file("README", 0644, d_tracer,
2274 NULL, &tracing_readme_fops);
2276 pr_warning("Could not create debugfs 'README' entry\n");
2278 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2279 NULL, &tracing_pipe_fops);
2281 pr_warning("Could not create debugfs "
2282 "'tracing_threash' entry\n");
2284 #ifdef CONFIG_DYNAMIC_FTRACE
2285 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2286 &ftrace_update_tot_cnt,
2287 &tracing_read_long_fops);
2289 pr_warning("Could not create debugfs "
2290 "'dyn_ftrace_total_info' entry\n");
2294 /* dummy trace to disable tracing */
2295 static struct tracer no_tracer __read_mostly =
2300 static int trace_alloc_page(void)
2302 struct trace_array_cpu *data;
2303 struct page *page, *tmp;
2308 /* first allocate a page for each CPU */
2309 for_each_possible_cpu(i) {
2310 array = (void *)__get_free_page(GFP_KERNEL);
2311 if (array == NULL) {
2312 printk(KERN_ERR "tracer: failed to allocate page"
2313 "for trace buffer!\n");
2317 page = virt_to_page(array);
2318 list_add(&page->lru, &pages);
2320 /* Only allocate if we are actually using the max trace */
2321 #ifdef CONFIG_TRACER_MAX_TRACE
2322 array = (void *)__get_free_page(GFP_KERNEL);
2323 if (array == NULL) {
2324 printk(KERN_ERR "tracer: failed to allocate page"
2325 "for trace buffer!\n");
2328 page = virt_to_page(array);
2329 list_add(&page->lru, &pages);
2333 /* Now that we successfully allocate a page per CPU, add them */
2334 for_each_possible_cpu(i) {
2335 data = global_trace.data[i];
2336 spin_lock_init(&data->lock);
2337 lockdep_set_class(&data->lock, &data->lock_key);
2338 page = list_entry(pages.next, struct page, lru);
2339 list_del_init(&page->lru);
2340 list_add_tail(&page->lru, &data->trace_pages);
2343 #ifdef CONFIG_TRACER_MAX_TRACE
2344 data = max_tr.data[i];
2345 spin_lock_init(&data->lock);
2346 lockdep_set_class(&data->lock, &data->lock_key);
2347 page = list_entry(pages.next, struct page, lru);
2348 list_del_init(&page->lru);
2349 list_add_tail(&page->lru, &data->trace_pages);
2353 global_trace.entries += ENTRIES_PER_PAGE;
2358 list_for_each_entry_safe(page, tmp, &pages, lru) {
2359 list_del_init(&page->lru);
2365 __init static int tracer_alloc_buffers(void)
2367 struct trace_array_cpu *data;
2374 global_trace.ctrl = tracer_enabled;
2376 /* Allocate the first page for all buffers */
2377 for_each_possible_cpu(i) {
2378 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2379 max_tr.data[i] = &per_cpu(max_data, i);
2381 array = (void *)__get_free_page(GFP_KERNEL);
2382 if (array == NULL) {
2383 printk(KERN_ERR "tracer: failed to allocate page"
2384 "for trace buffer!\n");
2388 /* set the array to the list */
2389 INIT_LIST_HEAD(&data->trace_pages);
2390 page = virt_to_page(array);
2391 list_add(&page->lru, &data->trace_pages);
2392 /* use the LRU flag to differentiate the two buffers */
2395 /* Only allocate if we are actually using the max trace */
2396 #ifdef CONFIG_TRACER_MAX_TRACE
2397 array = (void *)__get_free_page(GFP_KERNEL);
2398 if (array == NULL) {
2399 printk(KERN_ERR "tracer: failed to allocate page"
2400 "for trace buffer!\n");
2404 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2405 page = virt_to_page(array);
2406 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2412 * Since we allocate by orders of pages, we may be able to
2415 global_trace.entries = ENTRIES_PER_PAGE;
2418 while (global_trace.entries < trace_nr_entries) {
2419 if (trace_alloc_page())
2423 max_tr.entries = global_trace.entries;
2425 pr_info("tracer: %d pages allocated for %ld",
2426 pages, trace_nr_entries);
2427 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2428 pr_info(" actual entries %ld\n", global_trace.entries);
2430 tracer_init_debugfs();
2432 trace_init_cmdlines();
2434 register_tracer(&no_tracer);
2435 current_trace = &no_tracer;
2437 /* All seems OK, enable tracing */
2438 tracing_disabled = 0;
2443 for (i-- ; i >= 0; i--) {
2444 struct page *page, *tmp;
2445 struct trace_array_cpu *data = global_trace.data[i];
2448 list_for_each_entry_safe(page, tmp,
2449 &data->trace_pages, lru) {
2450 list_del_init(&page->lru);
2455 #ifdef CONFIG_TRACER_MAX_TRACE
2456 data = max_tr.data[i];
2458 list_for_each_entry_safe(page, tmp,
2459 &data->trace_pages, lru) {
2460 list_del_init(&page->lru);
2468 fs_initcall(tracer_alloc_buffers);