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/gfp.h>
32 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly tracing_thresh;
35 static int tracing_disabled = 1;
38 ns2usecs(cycle_t nsec)
45 static const int time_sync_freq_max = 128;
46 static const cycle_t time_sync_thresh = 100000;
48 static DEFINE_PER_CPU(cycle_t, time_offset);
49 static DEFINE_PER_CPU(cycle_t, prev_cpu_time);
50 static DEFINE_PER_CPU(int, time_sync_count);
51 static DEFINE_PER_CPU(int, time_sync_freq);
54 * Global lock which we take every now and then to synchronize
55 * the CPUs time. This method is not warp-safe, but it's good
56 * enough to synchronize slowly diverging time sources and thus
57 * it's good enough for tracing:
59 static DEFINE_SPINLOCK(time_sync_lock);
60 static cycle_t prev_global_time;
62 static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
66 spin_lock_irqsave(&time_sync_lock, flags);
69 * Update the synchronization frequency:
71 if (per_cpu(time_sync_freq, cpu) < time_sync_freq_max)
72 per_cpu(time_sync_freq, cpu) *= 2;
73 per_cpu(time_sync_count, cpu) = per_cpu(time_sync_freq, cpu);
75 if (time < prev_global_time) {
76 per_cpu(time_offset, cpu) += prev_global_time - time;
77 time = prev_global_time;
79 prev_global_time = time;
82 spin_unlock_irqrestore(&time_sync_lock, flags);
87 notrace cycle_t ftrace_now(int cpu)
89 cycle_t prev_cpu_time, time, delta_time;
91 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
92 time = sched_clock() + per_cpu(time_offset, cpu);
93 delta_time = time-prev_cpu_time;
95 if (unlikely(delta_time > time_sync_thresh ||
96 --per_cpu(time_sync_count, cpu) <= 0))
97 time = __ftrace_now_sync(time, cpu);
102 static struct trace_array global_trace;
104 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
106 static struct trace_array max_tr;
108 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
110 static int tracer_enabled;
111 static unsigned long trace_nr_entries = 16384UL;
113 static struct tracer *trace_types __read_mostly;
114 static struct tracer *current_trace __read_mostly;
115 static int max_tracer_type_len;
117 static DEFINE_MUTEX(trace_types_lock);
119 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
121 static int __init set_nr_entries(char *str)
125 trace_nr_entries = simple_strtoul(str, &str, 0);
128 __setup("trace_entries=", set_nr_entries);
130 unsigned long nsecs_to_usecs(unsigned long nsecs)
136 __TRACE_FIRST_TYPE = 0,
145 enum trace_flag_type {
146 TRACE_FLAG_IRQS_OFF = 0x01,
147 TRACE_FLAG_NEED_RESCHED = 0x02,
148 TRACE_FLAG_HARDIRQ = 0x04,
149 TRACE_FLAG_SOFTIRQ = 0x08,
152 enum trace_iterator_flags {
153 TRACE_ITER_PRINT_PARENT = 0x01,
154 TRACE_ITER_SYM_OFFSET = 0x02,
155 TRACE_ITER_SYM_ADDR = 0x04,
156 TRACE_ITER_VERBOSE = 0x08,
157 TRACE_ITER_RAW = 0x10,
158 TRACE_ITER_BIN = 0x20,
161 #define TRACE_ITER_SYM_MASK \
162 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
164 /* These must match the bit postions above */
165 static const char *trace_options[] = {
175 static unsigned trace_flags;
177 static DEFINE_SPINLOCK(ftrace_max_lock);
180 * Copy the new maximum trace into the separate maximum-trace
181 * structure. (this way the maximum trace is permanently saved,
182 * for later retrieval via /debugfs/tracing/latency_trace)
185 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
187 struct trace_array_cpu *data = tr->data[cpu];
190 max_tr.time_start = data->preempt_timestamp;
192 data = max_tr.data[cpu];
193 data->saved_latency = tracing_max_latency;
195 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
196 data->pid = tsk->pid;
197 data->uid = tsk->uid;
198 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
199 data->policy = tsk->policy;
200 data->rt_priority = tsk->rt_priority;
202 /* record this tasks comm */
203 tracing_record_cmdline(current);
206 void check_pages(struct trace_array_cpu *data)
208 struct page *page, *tmp;
210 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
211 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
213 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
214 BUG_ON(page->lru.next->prev != &page->lru);
215 BUG_ON(page->lru.prev->next != &page->lru);
219 void *head_page(struct trace_array_cpu *data)
224 if (list_empty(&data->trace_pages))
227 page = list_entry(data->trace_pages.next, struct page, lru);
228 BUG_ON(&page->lru == &data->trace_pages);
230 return page_address(page);
234 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
236 int len = (PAGE_SIZE - 1) - s->len;
244 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
247 /* If we can't write it all, don't bother writing anything */
257 trace_seq_puts(struct trace_seq *s, const char *str)
259 int len = strlen(str);
261 if (len > ((PAGE_SIZE - 1) - s->len))
264 memcpy(s->buffer + s->len, str, len);
271 trace_seq_putc(struct trace_seq *s, unsigned char c)
273 if (s->len >= (PAGE_SIZE - 1))
276 s->buffer[s->len++] = c;
282 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
284 if (len > ((PAGE_SIZE - 1) - s->len))
287 memcpy(s->buffer + s->len, mem, len);
294 trace_seq_reset(struct trace_seq *s)
300 trace_print_seq(struct seq_file *m, struct trace_seq *s)
302 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
305 seq_puts(m, s->buffer);
311 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
313 struct list_head flip_pages;
315 INIT_LIST_HEAD(&flip_pages);
317 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
318 sizeof(struct trace_array_cpu) -
319 offsetof(struct trace_array_cpu, trace_head_idx));
323 list_splice_init(&tr1->trace_pages, &flip_pages);
324 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
325 list_splice_init(&flip_pages, &tr2->trace_pages);
326 BUG_ON(!list_empty(&flip_pages));
332 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334 struct trace_array_cpu *data;
337 WARN_ON_ONCE(!irqs_disabled());
338 spin_lock(&ftrace_max_lock);
339 /* clear out all the previous traces */
340 for_each_possible_cpu(i) {
342 flip_trace(max_tr.data[i], data);
346 __update_max_tr(tr, tsk, cpu);
347 spin_unlock(&ftrace_max_lock);
351 * update_max_tr_single - only copy one trace over, and reset the rest
353 * @tsk - task with the latency
354 * @cpu - the cpu of the buffer to copy.
357 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
359 struct trace_array_cpu *data = tr->data[cpu];
362 WARN_ON_ONCE(!irqs_disabled());
363 spin_lock(&ftrace_max_lock);
364 for_each_possible_cpu(i)
365 tracing_reset(max_tr.data[i]);
367 flip_trace(max_tr.data[cpu], data);
370 __update_max_tr(tr, tsk, cpu);
371 spin_unlock(&ftrace_max_lock);
374 int register_tracer(struct tracer *type)
381 pr_info("Tracer must have a name\n");
385 mutex_lock(&trace_types_lock);
386 for (t = trace_types; t; t = t->next) {
387 if (strcmp(type->name, t->name) == 0) {
389 pr_info("Trace %s already registered\n",
396 #ifdef CONFIG_FTRACE_STARTUP_TEST
397 if (type->selftest) {
398 struct tracer *saved_tracer = current_trace;
399 struct trace_array_cpu *data;
400 struct trace_array *tr = &global_trace;
401 int saved_ctrl = tr->ctrl;
404 * Run a selftest on this tracer.
405 * Here we reset the trace buffer, and set the current
406 * tracer to be this tracer. The tracer can then run some
407 * internal tracing to verify that everything is in order.
408 * If we fail, we do not register this tracer.
410 for_each_possible_cpu(i) {
412 if (!head_page(data))
416 current_trace = type;
418 /* the test is responsible for initializing and enabling */
419 pr_info("Testing tracer %s: ", type->name);
420 ret = type->selftest(type, tr);
421 /* the test is responsible for resetting too */
422 current_trace = saved_tracer;
423 tr->ctrl = saved_ctrl;
425 printk(KERN_CONT "FAILED!\n");
428 /* Only reset on passing, to avoid touching corrupted buffers */
429 for_each_possible_cpu(i) {
431 if (!head_page(data))
435 printk(KERN_CONT "PASSED\n");
439 type->next = trace_types;
441 len = strlen(type->name);
442 if (len > max_tracer_type_len)
443 max_tracer_type_len = len;
446 mutex_unlock(&trace_types_lock);
451 void unregister_tracer(struct tracer *type)
456 mutex_lock(&trace_types_lock);
457 for (t = &trace_types; *t; t = &(*t)->next) {
461 pr_info("Trace %s not registered\n", type->name);
466 if (strlen(type->name) != max_tracer_type_len)
469 max_tracer_type_len = 0;
470 for (t = &trace_types; *t; t = &(*t)->next) {
471 len = strlen((*t)->name);
472 if (len > max_tracer_type_len)
473 max_tracer_type_len = len;
476 mutex_unlock(&trace_types_lock);
479 notrace void tracing_reset(struct trace_array_cpu *data)
482 data->trace_head = data->trace_tail = head_page(data);
483 data->trace_head_idx = 0;
484 data->trace_tail_idx = 0;
489 function_trace_call(unsigned long ip, unsigned long parent_ip)
491 struct trace_array *tr = &global_trace;
492 struct trace_array_cpu *data;
497 if (unlikely(!tracer_enabled))
500 local_irq_save(flags);
501 cpu = raw_smp_processor_id();
502 data = tr->data[cpu];
503 disabled = atomic_inc_return(&data->disabled);
505 if (likely(disabled == 1))
506 ftrace(tr, data, ip, parent_ip, flags);
508 atomic_dec(&data->disabled);
509 local_irq_restore(flags);
512 static struct ftrace_ops trace_ops __read_mostly =
514 .func = function_trace_call,
518 notrace void tracing_start_function_trace(void)
520 register_ftrace_function(&trace_ops);
523 notrace void tracing_stop_function_trace(void)
525 unregister_ftrace_function(&trace_ops);
528 #define SAVED_CMDLINES 128
529 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
530 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
531 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
532 static int cmdline_idx;
533 static DEFINE_SPINLOCK(trace_cmdline_lock);
534 atomic_t trace_record_cmdline_disabled;
536 static void trace_init_cmdlines(void)
538 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
539 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
543 notrace void trace_stop_cmdline_recording(void);
545 static notrace void trace_save_cmdline(struct task_struct *tsk)
550 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
554 * It's not the end of the world if we don't get
555 * the lock, but we also don't want to spin
556 * nor do we want to disable interrupts,
557 * so if we miss here, then better luck next time.
559 if (!spin_trylock(&trace_cmdline_lock))
562 idx = map_pid_to_cmdline[tsk->pid];
563 if (idx >= SAVED_CMDLINES) {
564 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
566 map = map_cmdline_to_pid[idx];
567 if (map <= PID_MAX_DEFAULT)
568 map_pid_to_cmdline[map] = (unsigned)-1;
570 map_pid_to_cmdline[tsk->pid] = idx;
575 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
577 spin_unlock(&trace_cmdline_lock);
580 static notrace char *trace_find_cmdline(int pid)
582 char *cmdline = "<...>";
588 if (pid > PID_MAX_DEFAULT)
591 map = map_pid_to_cmdline[pid];
592 if (map >= SAVED_CMDLINES)
595 cmdline = saved_cmdlines[map];
601 notrace void tracing_record_cmdline(struct task_struct *tsk)
603 if (atomic_read(&trace_record_cmdline_disabled))
606 trace_save_cmdline(tsk);
609 static inline notrace struct list_head *
610 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
613 * Roundrobin - but skip the head (which is not a real page):
616 if (unlikely(next == &data->trace_pages))
618 BUG_ON(next == &data->trace_pages);
623 static inline notrace void *
624 trace_next_page(struct trace_array_cpu *data, void *addr)
626 struct list_head *next;
629 page = virt_to_page(addr);
631 next = trace_next_list(data, &page->lru);
632 page = list_entry(next, struct page, lru);
634 return page_address(page);
637 static inline notrace struct trace_entry *
638 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
640 unsigned long idx, idx_next;
641 struct trace_entry *entry;
644 idx = data->trace_head_idx;
647 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
649 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
651 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
652 data->trace_head = trace_next_page(data, data->trace_head);
656 if (data->trace_head == data->trace_tail &&
657 idx_next == data->trace_tail_idx) {
659 data->trace_tail_idx++;
660 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
662 trace_next_page(data, data->trace_tail);
663 data->trace_tail_idx = 0;
667 data->trace_head_idx = idx_next;
672 static inline notrace void
673 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
675 struct task_struct *tsk = current;
678 pc = preempt_count();
680 entry->preempt_count = pc & 0xff;
681 entry->pid = tsk->pid;
682 entry->t = ftrace_now(raw_smp_processor_id());
683 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
684 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
685 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
686 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
690 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
691 unsigned long ip, unsigned long parent_ip, unsigned long flags)
693 struct trace_entry *entry;
694 unsigned long irq_flags;
696 spin_lock_irqsave(&data->lock, irq_flags);
697 entry = tracing_get_trace_entry(tr, data);
698 tracing_generic_entry_update(entry, flags);
699 entry->type = TRACE_FN;
701 entry->fn.parent_ip = parent_ip;
702 spin_unlock_irqrestore(&data->lock, irq_flags);
706 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
707 unsigned long arg1, unsigned long arg2, unsigned long arg3)
709 struct trace_entry *entry;
710 unsigned long irq_flags;
712 spin_lock_irqsave(&data->lock, irq_flags);
713 entry = tracing_get_trace_entry(tr, data);
714 tracing_generic_entry_update(entry, 0);
715 entry->type = TRACE_SPECIAL;
716 entry->special.arg1 = arg1;
717 entry->special.arg2 = arg2;
718 entry->special.arg3 = arg3;
719 spin_unlock_irqrestore(&data->lock, irq_flags);
723 tracing_sched_switch_trace(struct trace_array *tr,
724 struct trace_array_cpu *data,
725 struct task_struct *prev, 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 spin_unlock_irqrestore(&data->lock, irq_flags);
743 enum trace_file_type {
744 TRACE_FILE_LAT_FMT = 1,
747 static struct trace_entry *
748 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
749 struct trace_iterator *iter, int cpu)
752 struct trace_entry *array;
754 if (iter->next_idx[cpu] >= tr->entries ||
755 iter->next_idx[cpu] >= data->trace_idx ||
756 (data->trace_head == data->trace_tail &&
757 data->trace_head_idx == data->trace_tail_idx))
760 if (!iter->next_page[cpu]) {
761 /* Initialize the iterator for this cpu trace buffer */
762 WARN_ON(!data->trace_tail);
763 page = virt_to_page(data->trace_tail);
764 iter->next_page[cpu] = &page->lru;
765 iter->next_page_idx[cpu] = data->trace_tail_idx;
768 page = list_entry(iter->next_page[cpu], struct page, lru);
769 BUG_ON(&data->trace_pages == &page->lru);
771 array = page_address(page);
773 /* Still possible to catch up to the tail */
774 if (iter->next_idx[cpu] && array == data->trace_tail &&
775 iter->next_page_idx[cpu] == data->trace_tail_idx)
778 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
779 return &array[iter->next_page_idx[cpu]];
782 static struct notrace trace_entry *
783 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
785 struct trace_array *tr = iter->tr;
786 struct trace_entry *ent, *next = NULL;
790 for_each_possible_cpu(cpu) {
791 if (!head_page(tr->data[cpu]))
793 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
795 * Pick the entry with the smallest timestamp:
797 if (ent && (!next || ent->t < next->t)) {
809 static notrace void trace_iterator_increment(struct trace_iterator *iter)
812 iter->next_idx[iter->cpu]++;
813 iter->next_page_idx[iter->cpu]++;
815 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
816 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
818 iter->next_page_idx[iter->cpu] = 0;
819 iter->next_page[iter->cpu] =
820 trace_next_list(data, iter->next_page[iter->cpu]);
824 static notrace void trace_consume(struct trace_iterator *iter)
826 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
828 data->trace_tail_idx++;
829 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
830 data->trace_tail = trace_next_page(data, data->trace_tail);
831 data->trace_tail_idx = 0;
834 /* Check if we empty it, then reset the index */
835 if (data->trace_head == data->trace_tail &&
836 data->trace_head_idx == data->trace_tail_idx)
840 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
842 struct trace_entry *next;
845 next = find_next_entry(iter, &next_cpu);
847 iter->prev_ent = iter->ent;
848 iter->prev_cpu = iter->cpu;
851 iter->cpu = next_cpu;
854 trace_iterator_increment(iter);
856 return next ? iter : NULL;
859 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
861 struct trace_iterator *iter = m->private;
862 void *last_ent = iter->ent;
868 /* can't go backwards */
873 ent = find_next_entry_inc(iter);
877 while (ent && iter->idx < i)
878 ent = find_next_entry_inc(iter);
882 if (last_ent && !ent)
883 seq_puts(m, "\n\nvim:ft=help\n");
888 static void *s_start(struct seq_file *m, loff_t *pos)
890 struct trace_iterator *iter = m->private;
895 mutex_lock(&trace_types_lock);
897 if (!current_trace || current_trace != iter->trace)
900 atomic_inc(&trace_record_cmdline_disabled);
902 /* let the tracer grab locks here if needed */
903 if (current_trace->start)
904 current_trace->start(iter);
906 if (*pos != iter->pos) {
910 iter->prev_ent = NULL;
913 for_each_possible_cpu(i) {
914 iter->next_idx[i] = 0;
915 iter->next_page[i] = NULL;
918 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
923 p = s_next(m, p, &l);
929 static void s_stop(struct seq_file *m, void *p)
931 struct trace_iterator *iter = m->private;
933 atomic_dec(&trace_record_cmdline_disabled);
935 /* let the tracer release locks here if needed */
936 if (current_trace && current_trace == iter->trace && iter->trace->stop)
937 iter->trace->stop(iter);
939 mutex_unlock(&trace_types_lock);
943 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
945 #ifdef CONFIG_KALLSYMS
946 char str[KSYM_SYMBOL_LEN];
948 kallsyms_lookup(address, NULL, NULL, NULL, str);
950 return trace_seq_printf(s, fmt, str);
956 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
957 unsigned long address)
959 #ifdef CONFIG_KALLSYMS
960 char str[KSYM_SYMBOL_LEN];
962 sprint_symbol(str, address);
963 return trace_seq_printf(s, fmt, str);
969 # define IP_FMT "%08lx"
971 # define IP_FMT "%016lx"
975 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
980 return trace_seq_printf(s, "0");
982 if (sym_flags & TRACE_ITER_SYM_OFFSET)
983 ret = seq_print_sym_offset(s, "%s", ip);
985 ret = seq_print_sym_short(s, "%s", ip);
990 if (sym_flags & TRACE_ITER_SYM_ADDR)
991 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
995 static notrace void print_lat_help_header(struct seq_file *m)
997 seq_puts(m, "# _------=> CPU# \n");
998 seq_puts(m, "# / _-----=> irqs-off \n");
999 seq_puts(m, "# | / _----=> need-resched \n");
1000 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1001 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1002 seq_puts(m, "# |||| / \n");
1003 seq_puts(m, "# ||||| delay \n");
1004 seq_puts(m, "# cmd pid ||||| time | caller \n");
1005 seq_puts(m, "# \\ / ||||| \\ | / \n");
1008 static notrace void print_func_help_header(struct seq_file *m)
1010 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1011 seq_puts(m, "# | | | | |\n");
1016 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1018 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1019 struct trace_array *tr = iter->tr;
1020 struct trace_array_cpu *data = tr->data[tr->cpu];
1021 struct tracer *type = current_trace;
1022 unsigned long total = 0;
1023 unsigned long entries = 0;
1025 const char *name = "preemption";
1030 for_each_possible_cpu(cpu) {
1031 if (head_page(tr->data[cpu])) {
1032 total += tr->data[cpu]->trace_idx;
1033 if (tr->data[cpu]->trace_idx > tr->entries)
1034 entries += tr->entries;
1036 entries += tr->data[cpu]->trace_idx;
1040 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1042 seq_puts(m, "-----------------------------------"
1043 "---------------------------------\n");
1044 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1045 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1046 nsecs_to_usecs(data->saved_latency),
1050 #if defined(CONFIG_PREEMPT_NONE)
1052 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1054 #elif defined(CONFIG_PREEMPT_DESKTOP)
1059 /* These are reserved for later use */
1062 seq_printf(m, " #P:%d)\n", num_online_cpus());
1066 seq_puts(m, " -----------------\n");
1067 seq_printf(m, " | task: %.16s-%d "
1068 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1069 data->comm, data->pid, data->uid, data->nice,
1070 data->policy, data->rt_priority);
1071 seq_puts(m, " -----------------\n");
1073 if (data->critical_start) {
1074 seq_puts(m, " => started at: ");
1075 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1076 trace_print_seq(m, &iter->seq);
1077 seq_puts(m, "\n => ended at: ");
1078 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1079 trace_print_seq(m, &iter->seq);
1087 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1089 int hardirq, softirq;
1092 comm = trace_find_cmdline(entry->pid);
1094 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1095 trace_seq_printf(s, "%d", cpu);
1096 trace_seq_printf(s, "%c%c",
1097 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1098 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1100 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1101 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1102 if (hardirq && softirq)
1103 trace_seq_putc(s, 'H');
1106 trace_seq_putc(s, 'h');
1109 trace_seq_putc(s, 's');
1111 trace_seq_putc(s, '.');
1115 if (entry->preempt_count)
1116 trace_seq_printf(s, "%x", entry->preempt_count);
1118 trace_seq_puts(s, ".");
1121 unsigned long preempt_mark_thresh = 100;
1124 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1125 unsigned long rel_usecs)
1127 trace_seq_printf(s, " %4lldus", abs_usecs);
1128 if (rel_usecs > preempt_mark_thresh)
1129 trace_seq_puts(s, "!: ");
1130 else if (rel_usecs > 1)
1131 trace_seq_puts(s, "+: ");
1133 trace_seq_puts(s, " : ");
1136 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1139 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1141 struct trace_seq *s = &iter->seq;
1142 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1143 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1144 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1145 struct trace_entry *entry = iter->ent;
1146 unsigned long abs_usecs;
1147 unsigned long rel_usecs;
1153 rel_usecs = ns2usecs(next_entry->t - entry->t);
1154 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1157 comm = trace_find_cmdline(entry->pid);
1158 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1159 " %ld.%03ldms (+%ld.%03ldms): ",
1161 entry->pid, cpu, entry->flags,
1162 entry->preempt_count, trace_idx,
1165 abs_usecs % 1000, rel_usecs/1000,
1168 lat_print_generic(s, entry, cpu);
1169 lat_print_timestamp(s, abs_usecs, rel_usecs);
1171 switch (entry->type) {
1173 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1174 trace_seq_puts(s, " (");
1175 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1176 trace_seq_puts(s, ")\n");
1179 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1180 state_to_char[entry->ctx.prev_state] : 'X';
1181 comm = trace_find_cmdline(entry->ctx.next_pid);
1182 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1183 entry->ctx.prev_pid,
1184 entry->ctx.prev_prio,
1186 entry->ctx.next_pid,
1187 entry->ctx.next_prio,
1191 trace_seq_printf(s, " %lx %lx %lx\n",
1192 entry->special.arg1,
1193 entry->special.arg2,
1194 entry->special.arg3);
1197 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1202 static notrace int print_trace_fmt(struct trace_iterator *iter)
1204 struct trace_seq *s = &iter->seq;
1205 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1206 struct trace_entry *entry;
1207 unsigned long usec_rem;
1208 unsigned long long t;
1216 comm = trace_find_cmdline(iter->ent->pid);
1218 t = ns2usecs(entry->t);
1219 usec_rem = do_div(t, 1000000ULL);
1220 secs = (unsigned long)t;
1222 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1225 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1228 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1232 switch (entry->type) {
1234 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1237 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1238 entry->fn.parent_ip) {
1239 ret = trace_seq_printf(s, " <-");
1242 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1247 ret = trace_seq_printf(s, "\n");
1252 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1253 state_to_char[entry->ctx.prev_state] : 'X';
1254 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1255 entry->ctx.prev_pid,
1256 entry->ctx.prev_prio,
1258 entry->ctx.next_pid,
1259 entry->ctx.next_prio);
1264 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1265 entry->special.arg1,
1266 entry->special.arg2,
1267 entry->special.arg3);
1275 static notrace int print_raw_fmt(struct trace_iterator *iter)
1277 struct trace_seq *s = &iter->seq;
1278 struct trace_entry *entry;
1284 ret = trace_seq_printf(s, "%d %d %llu ",
1285 entry->pid, iter->cpu, entry->t);
1289 switch (entry->type) {
1291 ret = trace_seq_printf(s, "%x %x\n",
1292 entry->fn.ip, entry->fn.parent_ip);
1297 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1298 state_to_char[entry->ctx.prev_state] : 'X';
1299 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1300 entry->ctx.prev_pid,
1301 entry->ctx.prev_prio,
1303 entry->ctx.next_pid,
1304 entry->ctx.next_prio);
1309 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1310 entry->special.arg1,
1311 entry->special.arg2,
1312 entry->special.arg3);
1320 #define SEQ_PUT_FIELD_RET(s, x) \
1322 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1326 static notrace int print_bin_fmt(struct trace_iterator *iter)
1328 struct trace_seq *s = &iter->seq;
1329 struct trace_entry *entry;
1333 SEQ_PUT_FIELD_RET(s, entry->pid);
1334 SEQ_PUT_FIELD_RET(s, entry->cpu);
1335 SEQ_PUT_FIELD_RET(s, entry->t);
1337 switch (entry->type) {
1339 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1340 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1343 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1344 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1345 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1346 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1347 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1350 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1351 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1352 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1358 static int trace_empty(struct trace_iterator *iter)
1360 struct trace_array_cpu *data;
1363 for_each_possible_cpu(cpu) {
1364 data = iter->tr->data[cpu];
1366 if (head_page(data) && data->trace_idx &&
1367 (data->trace_tail != data->trace_head ||
1368 data->trace_tail_idx != data->trace_head_idx))
1374 static int print_trace_line(struct trace_iterator *iter)
1376 if (trace_flags & TRACE_ITER_BIN)
1377 return print_bin_fmt(iter);
1379 if (trace_flags & TRACE_ITER_RAW)
1380 return print_raw_fmt(iter);
1382 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1383 return print_lat_fmt(iter, iter->idx, iter->cpu);
1385 return print_trace_fmt(iter);
1388 static int s_show(struct seq_file *m, void *v)
1390 struct trace_iterator *iter = v;
1392 if (iter->ent == NULL) {
1394 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1397 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1398 /* print nothing if the buffers are empty */
1399 if (trace_empty(iter))
1401 print_trace_header(m, iter);
1402 if (!(trace_flags & TRACE_ITER_VERBOSE))
1403 print_lat_help_header(m);
1405 if (!(trace_flags & TRACE_ITER_VERBOSE))
1406 print_func_help_header(m);
1409 print_trace_line(iter);
1410 trace_print_seq(m, &iter->seq);
1416 static struct seq_operations tracer_seq_ops = {
1423 static struct trace_iterator notrace *
1424 __tracing_open(struct inode *inode, struct file *file, int *ret)
1426 struct trace_iterator *iter;
1428 if (tracing_disabled) {
1433 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1439 mutex_lock(&trace_types_lock);
1440 if (current_trace && current_trace->print_max)
1443 iter->tr = inode->i_private;
1444 iter->trace = current_trace;
1447 /* TODO stop tracer */
1448 *ret = seq_open(file, &tracer_seq_ops);
1450 struct seq_file *m = file->private_data;
1453 /* stop the trace while dumping */
1457 if (iter->trace && iter->trace->open)
1458 iter->trace->open(iter);
1463 mutex_unlock(&trace_types_lock);
1469 int tracing_open_generic(struct inode *inode, struct file *filp)
1471 if (tracing_disabled)
1474 filp->private_data = inode->i_private;
1478 int tracing_release(struct inode *inode, struct file *file)
1480 struct seq_file *m = (struct seq_file *)file->private_data;
1481 struct trace_iterator *iter = m->private;
1483 mutex_lock(&trace_types_lock);
1484 if (iter->trace && iter->trace->close)
1485 iter->trace->close(iter);
1487 /* reenable tracing if it was previously enabled */
1490 mutex_unlock(&trace_types_lock);
1492 seq_release(inode, file);
1497 static int tracing_open(struct inode *inode, struct file *file)
1501 __tracing_open(inode, file, &ret);
1506 static int tracing_lt_open(struct inode *inode, struct file *file)
1508 struct trace_iterator *iter;
1511 iter = __tracing_open(inode, file, &ret);
1514 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1520 static notrace void *
1521 t_next(struct seq_file *m, void *v, loff_t *pos)
1523 struct tracer *t = m->private;
1535 static void *t_start(struct seq_file *m, loff_t *pos)
1537 struct tracer *t = m->private;
1540 mutex_lock(&trace_types_lock);
1541 for (; t && l < *pos; t = t_next(m, t, &l))
1547 static void t_stop(struct seq_file *m, void *p)
1549 mutex_unlock(&trace_types_lock);
1552 static int t_show(struct seq_file *m, void *v)
1554 struct tracer *t = v;
1559 seq_printf(m, "%s", t->name);
1568 static struct seq_operations show_traces_seq_ops = {
1575 static int show_traces_open(struct inode *inode, struct file *file)
1579 if (tracing_disabled)
1582 ret = seq_open(file, &show_traces_seq_ops);
1584 struct seq_file *m = file->private_data;
1585 m->private = trace_types;
1591 static struct file_operations tracing_fops = {
1592 .open = tracing_open,
1594 .llseek = seq_lseek,
1595 .release = tracing_release,
1598 static struct file_operations tracing_lt_fops = {
1599 .open = tracing_lt_open,
1601 .llseek = seq_lseek,
1602 .release = tracing_release,
1605 static struct file_operations show_traces_fops = {
1606 .open = show_traces_open,
1608 .release = seq_release,
1612 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1613 size_t cnt, loff_t *ppos)
1620 /* calulate max size */
1621 for (i = 0; trace_options[i]; i++) {
1622 len += strlen(trace_options[i]);
1623 len += 3; /* "no" and space */
1626 /* +2 for \n and \0 */
1627 buf = kmalloc(len + 2, GFP_KERNEL);
1631 for (i = 0; trace_options[i]; i++) {
1632 if (trace_flags & (1 << i))
1633 r += sprintf(buf + r, "%s ", trace_options[i]);
1635 r += sprintf(buf + r, "no%s ", trace_options[i]);
1638 r += sprintf(buf + r, "\n");
1639 WARN_ON(r >= len + 2);
1641 r = simple_read_from_buffer(ubuf, cnt, ppos,
1650 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1651 size_t cnt, loff_t *ppos)
1661 if (copy_from_user(&buf, ubuf, cnt))
1666 if (strncmp(buf, "no", 2) == 0) {
1671 for (i = 0; trace_options[i]; i++) {
1672 int len = strlen(trace_options[i]);
1674 if (strncmp(cmp, trace_options[i], len) == 0) {
1676 trace_flags &= ~(1 << i);
1678 trace_flags |= (1 << i);
1688 static struct file_operations tracing_iter_fops = {
1689 .open = tracing_open_generic,
1690 .read = tracing_iter_ctrl_read,
1691 .write = tracing_iter_ctrl_write,
1694 static const char readme_msg[] =
1695 "tracing mini-HOWTO:\n\n"
1697 "# mount -t debugfs nodev /debug\n\n"
1698 "# cat /debug/tracing/available_tracers\n"
1699 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1700 "# cat /debug/tracing/current_tracer\n"
1702 "# echo sched_switch > /debug/tracing/current_tracer\n"
1703 "# cat /debug/tracing/current_tracer\n"
1705 "# cat /debug/tracing/iter_ctrl\n"
1706 "noprint-parent nosym-offset nosym-addr noverbose\n"
1707 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1708 "# echo 1 > /debug/tracing/tracing_enabled\n"
1709 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1710 "echo 0 > /debug/tracing/tracing_enabled\n"
1714 tracing_readme_read(struct file *filp, char __user *ubuf,
1715 size_t cnt, loff_t *ppos)
1717 return simple_read_from_buffer(ubuf, cnt, ppos,
1718 readme_msg, strlen(readme_msg));
1721 static struct file_operations tracing_readme_fops = {
1722 .open = tracing_open_generic,
1723 .read = tracing_readme_read,
1728 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1729 size_t cnt, loff_t *ppos)
1731 struct trace_array *tr = filp->private_data;
1735 r = sprintf(buf, "%ld\n", tr->ctrl);
1736 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1740 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1741 size_t cnt, loff_t *ppos)
1743 struct trace_array *tr = filp->private_data;
1750 if (copy_from_user(&buf, ubuf, cnt))
1755 val = simple_strtoul(buf, NULL, 10);
1759 mutex_lock(&trace_types_lock);
1760 if (tr->ctrl ^ val) {
1768 if (current_trace && current_trace->ctrl_update)
1769 current_trace->ctrl_update(tr);
1771 mutex_unlock(&trace_types_lock);
1779 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1780 size_t cnt, loff_t *ppos)
1782 char buf[max_tracer_type_len+2];
1785 mutex_lock(&trace_types_lock);
1787 r = sprintf(buf, "%s\n", current_trace->name);
1789 r = sprintf(buf, "\n");
1790 mutex_unlock(&trace_types_lock);
1792 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1796 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1797 size_t cnt, loff_t *ppos)
1799 struct trace_array *tr = &global_trace;
1801 char buf[max_tracer_type_len+1];
1804 if (cnt > max_tracer_type_len)
1805 cnt = max_tracer_type_len;
1807 if (copy_from_user(&buf, ubuf, cnt))
1812 /* strip ending whitespace. */
1813 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1816 mutex_lock(&trace_types_lock);
1817 for (t = trace_types; t; t = t->next) {
1818 if (strcmp(t->name, buf) == 0)
1821 if (!t || t == current_trace)
1824 if (current_trace && current_trace->reset)
1825 current_trace->reset(tr);
1832 mutex_unlock(&trace_types_lock);
1840 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1841 size_t cnt, loff_t *ppos)
1843 unsigned long *ptr = filp->private_data;
1847 r = snprintf(buf, 64, "%ld\n",
1848 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1851 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1855 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1856 size_t cnt, loff_t *ppos)
1858 long *ptr = filp->private_data;
1865 if (copy_from_user(&buf, ubuf, cnt))
1870 val = simple_strtoul(buf, NULL, 10);
1877 static atomic_t tracing_reader;
1879 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1881 struct trace_iterator *iter;
1883 if (tracing_disabled)
1886 /* We only allow for reader of the pipe */
1887 if (atomic_inc_return(&tracing_reader) != 1) {
1888 atomic_dec(&tracing_reader);
1892 /* create a buffer to store the information to pass to userspace */
1893 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1897 iter->tr = &global_trace;
1899 filp->private_data = iter;
1904 static int tracing_release_pipe(struct inode *inode, struct file *file)
1906 struct trace_iterator *iter = file->private_data;
1909 atomic_dec(&tracing_reader);
1918 tracing_read_pipe(struct file *filp, char __user *ubuf,
1919 size_t cnt, loff_t *ppos)
1921 struct trace_iterator *iter = filp->private_data;
1922 struct trace_array_cpu *data;
1923 static cpumask_t mask;
1924 struct trace_entry *entry;
1926 unsigned long flags;
1932 /* return any leftover data */
1933 if (iter->seq.len > start) {
1934 len = iter->seq.len - start;
1937 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1946 trace_seq_reset(&iter->seq);
1949 while (trace_empty(iter)) {
1951 * This is a make-shift waitqueue. The reason we don't use
1952 * an actual wait queue is because:
1953 * 1) we only ever have one waiter
1954 * 2) the tracing, traces all functions, we don't want
1955 * the overhead of calling wake_up and friends
1956 * (and tracing them too)
1957 * Anyway, this is really very primitive wakeup.
1959 set_current_state(TASK_INTERRUPTIBLE);
1960 iter->tr->waiter = current;
1962 /* sleep for one second, and try again. */
1963 schedule_timeout(HZ);
1965 iter->tr->waiter = NULL;
1967 if (signal_pending(current))
1971 * We block until we read something and tracing is disabled.
1972 * We still block if tracing is disabled, but we have never
1973 * read anything. This allows a user to cat this file, and
1974 * then enable tracing. But after we have read something,
1975 * we give an EOF when tracing is again disabled.
1977 * iter->pos will be 0 if we haven't read anything.
1979 if (!tracer_enabled && iter->pos)
1985 /* stop when tracing is finished */
1986 if (trace_empty(iter))
1989 if (cnt >= PAGE_SIZE)
1990 cnt = PAGE_SIZE - 1;
1992 memset(iter, 0, sizeof(*iter));
1993 iter->tr = &global_trace;
1997 * We need to stop all tracing on all CPUS to read the
1998 * the next buffer. This is a bit expensive, but is
1999 * not done often. We fill all what we can read,
2000 * and then release the locks again.
2004 local_irq_save(flags);
2005 for_each_possible_cpu(cpu) {
2006 data = iter->tr->data[cpu];
2008 if (!head_page(data) || !data->trace_idx)
2011 atomic_inc(&data->disabled);
2012 spin_lock(&data->lock);
2016 while ((entry = find_next_entry_inc(iter)) != NULL) {
2017 ret = print_trace_line(iter);
2021 trace_consume(iter);
2023 if (iter->seq.len >= cnt)
2027 for_each_cpu_mask(cpu, mask) {
2028 data = iter->tr->data[cpu];
2029 spin_unlock(&data->lock);
2030 atomic_dec(&data->disabled);
2032 local_irq_restore(flags);
2034 /* Now copy what we have to the user */
2035 read = iter->seq.len;
2039 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2041 if (read < iter->seq.len)
2044 trace_seq_reset(&iter->seq);
2052 static struct file_operations tracing_max_lat_fops = {
2053 .open = tracing_open_generic,
2054 .read = tracing_max_lat_read,
2055 .write = tracing_max_lat_write,
2058 static struct file_operations tracing_ctrl_fops = {
2059 .open = tracing_open_generic,
2060 .read = tracing_ctrl_read,
2061 .write = tracing_ctrl_write,
2064 static struct file_operations set_tracer_fops = {
2065 .open = tracing_open_generic,
2066 .read = tracing_set_trace_read,
2067 .write = tracing_set_trace_write,
2070 static struct file_operations tracing_pipe_fops = {
2071 .open = tracing_open_pipe,
2072 .read = tracing_read_pipe,
2073 .release = tracing_release_pipe,
2076 #ifdef CONFIG_DYNAMIC_FTRACE
2079 tracing_read_long(struct file *filp, char __user *ubuf,
2080 size_t cnt, loff_t *ppos)
2082 unsigned long *p = filp->private_data;
2086 r = sprintf(buf, "%ld\n", *p);
2088 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2091 static struct file_operations tracing_read_long_fops = {
2092 .open = tracing_open_generic,
2093 .read = tracing_read_long,
2097 static struct dentry *d_tracer;
2099 struct dentry *tracing_init_dentry(void)
2106 d_tracer = debugfs_create_dir("tracing", NULL);
2108 if (!d_tracer && !once) {
2110 pr_warning("Could not create debugfs directory 'tracing'\n");
2117 #ifdef CONFIG_FTRACE_SELFTEST
2118 /* Let selftest have access to static functions in this file */
2119 #include "trace_selftest.c"
2122 static __init void tracer_init_debugfs(void)
2124 struct dentry *d_tracer;
2125 struct dentry *entry;
2127 d_tracer = tracing_init_dentry();
2129 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2130 &global_trace, &tracing_ctrl_fops);
2132 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2134 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2135 NULL, &tracing_iter_fops);
2137 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2139 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2140 &global_trace, &tracing_lt_fops);
2142 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2144 entry = debugfs_create_file("trace", 0444, d_tracer,
2145 &global_trace, &tracing_fops);
2147 pr_warning("Could not create debugfs 'trace' entry\n");
2149 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2150 &global_trace, &show_traces_fops);
2152 pr_warning("Could not create debugfs 'trace' entry\n");
2154 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2155 &global_trace, &set_tracer_fops);
2157 pr_warning("Could not create debugfs 'trace' entry\n");
2159 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2160 &tracing_max_latency,
2161 &tracing_max_lat_fops);
2163 pr_warning("Could not create debugfs "
2164 "'tracing_max_latency' entry\n");
2166 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2167 &tracing_thresh, &tracing_max_lat_fops);
2169 pr_warning("Could not create debugfs "
2170 "'tracing_threash' entry\n");
2171 entry = debugfs_create_file("README", 0644, d_tracer,
2172 NULL, &tracing_readme_fops);
2174 pr_warning("Could not create debugfs 'README' entry\n");
2176 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2177 NULL, &tracing_pipe_fops);
2179 pr_warning("Could not create debugfs "
2180 "'tracing_threash' entry\n");
2182 #ifdef CONFIG_DYNAMIC_FTRACE
2183 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2184 &ftrace_update_tot_cnt,
2185 &tracing_read_long_fops);
2187 pr_warning("Could not create debugfs "
2188 "'dyn_ftrace_total_info' entry\n");
2192 /* dummy trace to disable tracing */
2193 static struct tracer no_tracer __read_mostly =
2198 static int trace_alloc_page(void)
2200 struct trace_array_cpu *data;
2201 struct page *page, *tmp;
2206 /* first allocate a page for each CPU */
2207 for_each_possible_cpu(i) {
2208 array = (void *)__get_free_page(GFP_KERNEL);
2209 if (array == NULL) {
2210 printk(KERN_ERR "tracer: failed to allocate page"
2211 "for trace buffer!\n");
2215 page = virt_to_page(array);
2216 list_add(&page->lru, &pages);
2218 /* Only allocate if we are actually using the max trace */
2219 #ifdef CONFIG_TRACER_MAX_TRACE
2220 array = (void *)__get_free_page(GFP_KERNEL);
2221 if (array == NULL) {
2222 printk(KERN_ERR "tracer: failed to allocate page"
2223 "for trace buffer!\n");
2226 page = virt_to_page(array);
2227 list_add(&page->lru, &pages);
2231 /* Now that we successfully allocate a page per CPU, add them */
2232 for_each_possible_cpu(i) {
2233 data = global_trace.data[i];
2234 spin_lock_init(&data->lock);
2235 lockdep_set_class(&data->lock, &data->lock_key);
2236 page = list_entry(pages.next, struct page, lru);
2237 list_del_init(&page->lru);
2238 list_add_tail(&page->lru, &data->trace_pages);
2241 #ifdef CONFIG_TRACER_MAX_TRACE
2242 data = max_tr.data[i];
2243 spin_lock_init(&data->lock);
2244 lockdep_set_class(&data->lock, &data->lock_key);
2245 page = list_entry(pages.next, struct page, lru);
2246 list_del_init(&page->lru);
2247 list_add_tail(&page->lru, &data->trace_pages);
2251 global_trace.entries += ENTRIES_PER_PAGE;
2256 list_for_each_entry_safe(page, tmp, &pages, lru) {
2257 list_del_init(&page->lru);
2263 __init static int tracer_alloc_buffers(void)
2265 struct trace_array_cpu *data;
2272 /* Allocate the first page for all buffers */
2273 for_each_possible_cpu(i) {
2274 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2275 max_tr.data[i] = &per_cpu(max_data, i);
2277 array = (void *)__get_free_page(GFP_KERNEL);
2278 if (array == NULL) {
2279 printk(KERN_ERR "tracer: failed to allocate page"
2280 "for trace buffer!\n");
2284 /* set the array to the list */
2285 INIT_LIST_HEAD(&data->trace_pages);
2286 page = virt_to_page(array);
2287 list_add(&page->lru, &data->trace_pages);
2288 /* use the LRU flag to differentiate the two buffers */
2291 /* Only allocate if we are actually using the max trace */
2292 #ifdef CONFIG_TRACER_MAX_TRACE
2293 array = (void *)__get_free_page(GFP_KERNEL);
2294 if (array == NULL) {
2295 printk(KERN_ERR "tracer: failed to allocate page"
2296 "for trace buffer!\n");
2300 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2301 page = virt_to_page(array);
2302 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2308 * Since we allocate by orders of pages, we may be able to
2311 global_trace.entries = ENTRIES_PER_PAGE;
2314 while (global_trace.entries < trace_nr_entries) {
2315 if (trace_alloc_page())
2319 max_tr.entries = global_trace.entries;
2321 pr_info("tracer: %d pages allocated for %ld",
2322 pages, trace_nr_entries);
2323 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2324 pr_info(" actual entries %ld\n", global_trace.entries);
2326 tracer_init_debugfs();
2328 trace_init_cmdlines();
2330 register_tracer(&no_tracer);
2331 current_trace = &no_tracer;
2333 /* All seems OK, enable tracing */
2334 tracing_disabled = 0;
2339 for (i-- ; i >= 0; i--) {
2340 struct page *page, *tmp;
2341 struct trace_array_cpu *data = global_trace.data[i];
2344 list_for_each_entry_safe(page, tmp,
2345 &data->trace_pages, lru) {
2346 list_del_init(&page->lru);
2351 #ifdef CONFIG_TRACER_MAX_TRACE
2352 data = max_tr.data[i];
2354 list_for_each_entry_safe(page, tmp,
2355 &data->trace_pages, lru) {
2356 list_del_init(&page->lru);
2364 fs_initcall(tracer_alloc_buffers);