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 notrace cycle_t ftrace_now(int cpu)
47 return cpu_clock(cpu);
50 static struct trace_array global_trace;
52 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
54 static struct trace_array max_tr;
56 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
58 static int tracer_enabled = 1;
59 static unsigned long trace_nr_entries = 16384UL;
61 static struct tracer *trace_types __read_mostly;
62 static struct tracer *current_trace __read_mostly;
63 static int max_tracer_type_len;
65 static DEFINE_MUTEX(trace_types_lock);
67 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
69 static int __init set_nr_entries(char *str)
73 trace_nr_entries = simple_strtoul(str, &str, 0);
76 __setup("trace_entries=", set_nr_entries);
78 unsigned long nsecs_to_usecs(unsigned long nsecs)
84 __TRACE_FIRST_TYPE = 0,
93 enum trace_flag_type {
94 TRACE_FLAG_IRQS_OFF = 0x01,
95 TRACE_FLAG_NEED_RESCHED = 0x02,
96 TRACE_FLAG_HARDIRQ = 0x04,
97 TRACE_FLAG_SOFTIRQ = 0x08,
100 enum trace_iterator_flags {
101 TRACE_ITER_PRINT_PARENT = 0x01,
102 TRACE_ITER_SYM_OFFSET = 0x02,
103 TRACE_ITER_SYM_ADDR = 0x04,
104 TRACE_ITER_VERBOSE = 0x08,
105 TRACE_ITER_RAW = 0x10,
106 TRACE_ITER_HEX = 0x20,
107 TRACE_ITER_BIN = 0x40,
110 #define TRACE_ITER_SYM_MASK \
111 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
113 /* These must match the bit postions above */
114 static const char *trace_options[] = {
125 static unsigned trace_flags;
127 static DEFINE_SPINLOCK(ftrace_max_lock);
130 * Copy the new maximum trace into the separate maximum-trace
131 * structure. (this way the maximum trace is permanently saved,
132 * for later retrieval via /debugfs/tracing/latency_trace)
135 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
137 struct trace_array_cpu *data = tr->data[cpu];
140 max_tr.time_start = data->preempt_timestamp;
142 data = max_tr.data[cpu];
143 data->saved_latency = tracing_max_latency;
145 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
146 data->pid = tsk->pid;
147 data->uid = tsk->uid;
148 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
149 data->policy = tsk->policy;
150 data->rt_priority = tsk->rt_priority;
152 /* record this tasks comm */
153 tracing_record_cmdline(current);
156 void check_pages(struct trace_array_cpu *data)
158 struct page *page, *tmp;
160 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
161 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
163 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
164 BUG_ON(page->lru.next->prev != &page->lru);
165 BUG_ON(page->lru.prev->next != &page->lru);
169 void *head_page(struct trace_array_cpu *data)
174 if (list_empty(&data->trace_pages))
177 page = list_entry(data->trace_pages.next, struct page, lru);
178 BUG_ON(&page->lru == &data->trace_pages);
180 return page_address(page);
184 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
186 int len = (PAGE_SIZE - 1) - s->len;
194 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
197 /* If we can't write it all, don't bother writing anything */
207 trace_seq_puts(struct trace_seq *s, const char *str)
209 int len = strlen(str);
211 if (len > ((PAGE_SIZE - 1) - s->len))
214 memcpy(s->buffer + s->len, str, len);
221 trace_seq_putc(struct trace_seq *s, unsigned char c)
223 if (s->len >= (PAGE_SIZE - 1))
226 s->buffer[s->len++] = c;
232 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
234 if (len > ((PAGE_SIZE - 1) - s->len))
237 memcpy(s->buffer + s->len, mem, len);
246 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
248 unsigned char hex[HEX_CHARS];
253 BUG_ON(len >= HEX_CHARS);
258 for (i = 0, j = 0; i < len; i++) {
260 for (i = len-1, j = 0; i >= 0; i--) {
264 hex[j] = byte & 0x0f;
281 return trace_seq_putmem(s, hex, j);
285 trace_seq_reset(struct trace_seq *s)
291 trace_print_seq(struct seq_file *m, struct trace_seq *s)
293 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
296 seq_puts(m, s->buffer);
302 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
304 struct list_head flip_pages;
306 INIT_LIST_HEAD(&flip_pages);
308 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
309 sizeof(struct trace_array_cpu) -
310 offsetof(struct trace_array_cpu, trace_head_idx));
314 list_splice_init(&tr1->trace_pages, &flip_pages);
315 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
316 list_splice_init(&flip_pages, &tr2->trace_pages);
317 BUG_ON(!list_empty(&flip_pages));
323 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
325 struct trace_array_cpu *data;
328 WARN_ON_ONCE(!irqs_disabled());
329 spin_lock(&ftrace_max_lock);
330 /* clear out all the previous traces */
331 for_each_possible_cpu(i) {
333 flip_trace(max_tr.data[i], data);
337 __update_max_tr(tr, tsk, cpu);
338 spin_unlock(&ftrace_max_lock);
342 * update_max_tr_single - only copy one trace over, and reset the rest
344 * @tsk - task with the latency
345 * @cpu - the cpu of the buffer to copy.
348 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
350 struct trace_array_cpu *data = tr->data[cpu];
353 WARN_ON_ONCE(!irqs_disabled());
354 spin_lock(&ftrace_max_lock);
355 for_each_possible_cpu(i)
356 tracing_reset(max_tr.data[i]);
358 flip_trace(max_tr.data[cpu], data);
361 __update_max_tr(tr, tsk, cpu);
362 spin_unlock(&ftrace_max_lock);
365 int register_tracer(struct tracer *type)
372 pr_info("Tracer must have a name\n");
376 mutex_lock(&trace_types_lock);
377 for (t = trace_types; t; t = t->next) {
378 if (strcmp(type->name, t->name) == 0) {
380 pr_info("Trace %s already registered\n",
387 #ifdef CONFIG_FTRACE_STARTUP_TEST
388 if (type->selftest) {
389 struct tracer *saved_tracer = current_trace;
390 struct trace_array_cpu *data;
391 struct trace_array *tr = &global_trace;
392 int saved_ctrl = tr->ctrl;
395 * Run a selftest on this tracer.
396 * Here we reset the trace buffer, and set the current
397 * tracer to be this tracer. The tracer can then run some
398 * internal tracing to verify that everything is in order.
399 * If we fail, we do not register this tracer.
401 for_each_possible_cpu(i) {
403 if (!head_page(data))
407 current_trace = type;
409 /* the test is responsible for initializing and enabling */
410 pr_info("Testing tracer %s: ", type->name);
411 ret = type->selftest(type, tr);
412 /* the test is responsible for resetting too */
413 current_trace = saved_tracer;
414 tr->ctrl = saved_ctrl;
416 printk(KERN_CONT "FAILED!\n");
419 /* Only reset on passing, to avoid touching corrupted buffers */
420 for_each_possible_cpu(i) {
422 if (!head_page(data))
426 printk(KERN_CONT "PASSED\n");
430 type->next = trace_types;
432 len = strlen(type->name);
433 if (len > max_tracer_type_len)
434 max_tracer_type_len = len;
437 mutex_unlock(&trace_types_lock);
442 void unregister_tracer(struct tracer *type)
447 mutex_lock(&trace_types_lock);
448 for (t = &trace_types; *t; t = &(*t)->next) {
452 pr_info("Trace %s not registered\n", type->name);
457 if (strlen(type->name) != max_tracer_type_len)
460 max_tracer_type_len = 0;
461 for (t = &trace_types; *t; t = &(*t)->next) {
462 len = strlen((*t)->name);
463 if (len > max_tracer_type_len)
464 max_tracer_type_len = len;
467 mutex_unlock(&trace_types_lock);
470 notrace void tracing_reset(struct trace_array_cpu *data)
473 data->trace_head = data->trace_tail = head_page(data);
474 data->trace_head_idx = 0;
475 data->trace_tail_idx = 0;
478 #define SAVED_CMDLINES 128
479 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
480 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
481 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
482 static int cmdline_idx;
483 static DEFINE_SPINLOCK(trace_cmdline_lock);
484 atomic_t trace_record_cmdline_disabled;
486 static void trace_init_cmdlines(void)
488 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
489 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
493 notrace void trace_stop_cmdline_recording(void);
495 static notrace void trace_save_cmdline(struct task_struct *tsk)
500 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
504 * It's not the end of the world if we don't get
505 * the lock, but we also don't want to spin
506 * nor do we want to disable interrupts,
507 * so if we miss here, then better luck next time.
509 if (!spin_trylock(&trace_cmdline_lock))
512 idx = map_pid_to_cmdline[tsk->pid];
513 if (idx >= SAVED_CMDLINES) {
514 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
516 map = map_cmdline_to_pid[idx];
517 if (map <= PID_MAX_DEFAULT)
518 map_pid_to_cmdline[map] = (unsigned)-1;
520 map_pid_to_cmdline[tsk->pid] = idx;
525 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
527 spin_unlock(&trace_cmdline_lock);
530 static notrace char *trace_find_cmdline(int pid)
532 char *cmdline = "<...>";
538 if (pid > PID_MAX_DEFAULT)
541 map = map_pid_to_cmdline[pid];
542 if (map >= SAVED_CMDLINES)
545 cmdline = saved_cmdlines[map];
551 notrace void tracing_record_cmdline(struct task_struct *tsk)
553 if (atomic_read(&trace_record_cmdline_disabled))
556 trace_save_cmdline(tsk);
559 static inline notrace struct list_head *
560 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
563 * Roundrobin - but skip the head (which is not a real page):
566 if (unlikely(next == &data->trace_pages))
568 BUG_ON(next == &data->trace_pages);
573 static inline notrace void *
574 trace_next_page(struct trace_array_cpu *data, void *addr)
576 struct list_head *next;
579 page = virt_to_page(addr);
581 next = trace_next_list(data, &page->lru);
582 page = list_entry(next, struct page, lru);
584 return page_address(page);
587 static inline notrace struct trace_entry *
588 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
590 unsigned long idx, idx_next;
591 struct trace_entry *entry;
594 idx = data->trace_head_idx;
597 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
599 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
601 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
602 data->trace_head = trace_next_page(data, data->trace_head);
606 if (data->trace_head == data->trace_tail &&
607 idx_next == data->trace_tail_idx) {
609 data->trace_tail_idx++;
610 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
612 trace_next_page(data, data->trace_tail);
613 data->trace_tail_idx = 0;
617 data->trace_head_idx = idx_next;
622 static inline notrace void
623 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
625 struct task_struct *tsk = current;
628 pc = preempt_count();
630 entry->preempt_count = pc & 0xff;
631 entry->pid = tsk->pid;
632 entry->t = ftrace_now(raw_smp_processor_id());
633 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
634 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
635 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
636 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
640 __ftrace(struct trace_array *tr, struct trace_array_cpu *data,
641 unsigned long ip, unsigned long parent_ip, unsigned long flags)
643 struct trace_entry *entry;
644 unsigned long irq_flags;
646 spin_lock_irqsave(&data->lock, irq_flags);
647 entry = tracing_get_trace_entry(tr, data);
648 tracing_generic_entry_update(entry, flags);
649 entry->type = TRACE_FN;
651 entry->fn.parent_ip = parent_ip;
652 spin_unlock_irqrestore(&data->lock, irq_flags);
656 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
657 unsigned long ip, unsigned long parent_ip, unsigned long flags)
659 if (likely(!atomic_read(&data->disabled)))
660 __ftrace(tr, data, ip, parent_ip, flags);
664 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
665 unsigned long arg1, unsigned long arg2, unsigned long arg3)
667 struct trace_entry *entry;
668 unsigned long irq_flags;
670 spin_lock_irqsave(&data->lock, irq_flags);
671 entry = tracing_get_trace_entry(tr, data);
672 tracing_generic_entry_update(entry, 0);
673 entry->type = TRACE_SPECIAL;
674 entry->special.arg1 = arg1;
675 entry->special.arg2 = arg2;
676 entry->special.arg3 = arg3;
677 spin_unlock_irqrestore(&data->lock, irq_flags);
681 tracing_sched_switch_trace(struct trace_array *tr,
682 struct trace_array_cpu *data,
683 struct task_struct *prev, struct task_struct *next,
686 struct trace_entry *entry;
687 unsigned long irq_flags;
689 spin_lock_irqsave(&data->lock, irq_flags);
690 entry = tracing_get_trace_entry(tr, data);
691 tracing_generic_entry_update(entry, flags);
692 entry->type = TRACE_CTX;
693 entry->ctx.prev_pid = prev->pid;
694 entry->ctx.prev_prio = prev->prio;
695 entry->ctx.prev_state = prev->state;
696 entry->ctx.next_pid = next->pid;
697 entry->ctx.next_prio = next->prio;
698 spin_unlock_irqrestore(&data->lock, irq_flags);
703 function_trace_call(unsigned long ip, unsigned long parent_ip)
705 struct trace_array *tr = &global_trace;
706 struct trace_array_cpu *data;
711 if (unlikely(!tracer_enabled))
714 local_irq_save(flags);
715 cpu = raw_smp_processor_id();
716 data = tr->data[cpu];
717 disabled = atomic_inc_return(&data->disabled);
719 if (likely(disabled == 1))
720 __ftrace(tr, data, ip, parent_ip, flags);
722 atomic_dec(&data->disabled);
723 local_irq_restore(flags);
726 static struct ftrace_ops trace_ops __read_mostly =
728 .func = function_trace_call,
731 notrace void tracing_start_function_trace(void)
733 register_ftrace_function(&trace_ops);
736 notrace void tracing_stop_function_trace(void)
738 unregister_ftrace_function(&trace_ops);
742 enum trace_file_type {
743 TRACE_FILE_LAT_FMT = 1,
746 static struct trace_entry *
747 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
748 struct trace_iterator *iter, int cpu)
751 struct trace_entry *array;
753 if (iter->next_idx[cpu] >= tr->entries ||
754 iter->next_idx[cpu] >= data->trace_idx ||
755 (data->trace_head == data->trace_tail &&
756 data->trace_head_idx == data->trace_tail_idx))
759 if (!iter->next_page[cpu]) {
760 /* Initialize the iterator for this cpu trace buffer */
761 WARN_ON(!data->trace_tail);
762 page = virt_to_page(data->trace_tail);
763 iter->next_page[cpu] = &page->lru;
764 iter->next_page_idx[cpu] = data->trace_tail_idx;
767 page = list_entry(iter->next_page[cpu], struct page, lru);
768 BUG_ON(&data->trace_pages == &page->lru);
770 array = page_address(page);
772 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
773 return &array[iter->next_page_idx[cpu]];
776 static struct trace_entry * notrace
777 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
779 struct trace_array *tr = iter->tr;
780 struct trace_entry *ent, *next = NULL;
784 for_each_possible_cpu(cpu) {
785 if (!head_page(tr->data[cpu]))
787 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
789 * Pick the entry with the smallest timestamp:
791 if (ent && (!next || ent->t < next->t)) {
803 static notrace void trace_iterator_increment(struct trace_iterator *iter)
806 iter->next_idx[iter->cpu]++;
807 iter->next_page_idx[iter->cpu]++;
809 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
810 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
812 iter->next_page_idx[iter->cpu] = 0;
813 iter->next_page[iter->cpu] =
814 trace_next_list(data, iter->next_page[iter->cpu]);
818 static notrace void trace_consume(struct trace_iterator *iter)
820 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
822 data->trace_tail_idx++;
823 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
824 data->trace_tail = trace_next_page(data, data->trace_tail);
825 data->trace_tail_idx = 0;
828 /* Check if we empty it, then reset the index */
829 if (data->trace_head == data->trace_tail &&
830 data->trace_head_idx == data->trace_tail_idx)
834 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
836 struct trace_entry *next;
839 next = find_next_entry(iter, &next_cpu);
841 iter->prev_ent = iter->ent;
842 iter->prev_cpu = iter->cpu;
845 iter->cpu = next_cpu;
848 trace_iterator_increment(iter);
850 return next ? iter : NULL;
853 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
855 struct trace_iterator *iter = m->private;
856 void *last_ent = iter->ent;
862 /* can't go backwards */
867 ent = find_next_entry_inc(iter);
871 while (ent && iter->idx < i)
872 ent = find_next_entry_inc(iter);
876 if (last_ent && !ent)
877 seq_puts(m, "\n\nvim:ft=help\n");
882 static void *s_start(struct seq_file *m, loff_t *pos)
884 struct trace_iterator *iter = m->private;
889 mutex_lock(&trace_types_lock);
891 if (!current_trace || current_trace != iter->trace)
894 atomic_inc(&trace_record_cmdline_disabled);
896 /* let the tracer grab locks here if needed */
897 if (current_trace->start)
898 current_trace->start(iter);
900 if (*pos != iter->pos) {
904 iter->prev_ent = NULL;
907 for_each_possible_cpu(i) {
908 iter->next_idx[i] = 0;
909 iter->next_page[i] = NULL;
912 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
917 p = s_next(m, p, &l);
923 static void s_stop(struct seq_file *m, void *p)
925 struct trace_iterator *iter = m->private;
927 atomic_dec(&trace_record_cmdline_disabled);
929 /* let the tracer release locks here if needed */
930 if (current_trace && current_trace == iter->trace && iter->trace->stop)
931 iter->trace->stop(iter);
933 mutex_unlock(&trace_types_lock);
937 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
939 #ifdef CONFIG_KALLSYMS
940 char str[KSYM_SYMBOL_LEN];
942 kallsyms_lookup(address, NULL, NULL, NULL, str);
944 return trace_seq_printf(s, fmt, str);
950 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
951 unsigned long address)
953 #ifdef CONFIG_KALLSYMS
954 char str[KSYM_SYMBOL_LEN];
956 sprint_symbol(str, address);
957 return trace_seq_printf(s, fmt, str);
963 # define IP_FMT "%08lx"
965 # define IP_FMT "%016lx"
969 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
974 return trace_seq_printf(s, "0");
976 if (sym_flags & TRACE_ITER_SYM_OFFSET)
977 ret = seq_print_sym_offset(s, "%s", ip);
979 ret = seq_print_sym_short(s, "%s", ip);
984 if (sym_flags & TRACE_ITER_SYM_ADDR)
985 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
989 static notrace void print_lat_help_header(struct seq_file *m)
991 seq_puts(m, "# _------=> CPU# \n");
992 seq_puts(m, "# / _-----=> irqs-off \n");
993 seq_puts(m, "# | / _----=> need-resched \n");
994 seq_puts(m, "# || / _---=> hardirq/softirq \n");
995 seq_puts(m, "# ||| / _--=> preempt-depth \n");
996 seq_puts(m, "# |||| / \n");
997 seq_puts(m, "# ||||| delay \n");
998 seq_puts(m, "# cmd pid ||||| time | caller \n");
999 seq_puts(m, "# \\ / ||||| \\ | / \n");
1002 static notrace void print_func_help_header(struct seq_file *m)
1004 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1005 seq_puts(m, "# | | | | |\n");
1010 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1012 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1013 struct trace_array *tr = iter->tr;
1014 struct trace_array_cpu *data = tr->data[tr->cpu];
1015 struct tracer *type = current_trace;
1016 unsigned long total = 0;
1017 unsigned long entries = 0;
1019 const char *name = "preemption";
1024 for_each_possible_cpu(cpu) {
1025 if (head_page(tr->data[cpu])) {
1026 total += tr->data[cpu]->trace_idx;
1027 if (tr->data[cpu]->trace_idx > tr->entries)
1028 entries += tr->entries;
1030 entries += tr->data[cpu]->trace_idx;
1034 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1036 seq_puts(m, "-----------------------------------"
1037 "---------------------------------\n");
1038 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1039 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1040 nsecs_to_usecs(data->saved_latency),
1044 #if defined(CONFIG_PREEMPT_NONE)
1046 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1048 #elif defined(CONFIG_PREEMPT_DESKTOP)
1053 /* These are reserved for later use */
1056 seq_printf(m, " #P:%d)\n", num_online_cpus());
1060 seq_puts(m, " -----------------\n");
1061 seq_printf(m, " | task: %.16s-%d "
1062 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1063 data->comm, data->pid, data->uid, data->nice,
1064 data->policy, data->rt_priority);
1065 seq_puts(m, " -----------------\n");
1067 if (data->critical_start) {
1068 seq_puts(m, " => started at: ");
1069 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1070 trace_print_seq(m, &iter->seq);
1071 seq_puts(m, "\n => ended at: ");
1072 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1073 trace_print_seq(m, &iter->seq);
1081 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1083 int hardirq, softirq;
1086 comm = trace_find_cmdline(entry->pid);
1088 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1089 trace_seq_printf(s, "%d", cpu);
1090 trace_seq_printf(s, "%c%c",
1091 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1092 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1094 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1095 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1096 if (hardirq && softirq)
1097 trace_seq_putc(s, 'H');
1100 trace_seq_putc(s, 'h');
1103 trace_seq_putc(s, 's');
1105 trace_seq_putc(s, '.');
1109 if (entry->preempt_count)
1110 trace_seq_printf(s, "%x", entry->preempt_count);
1112 trace_seq_puts(s, ".");
1115 unsigned long preempt_mark_thresh = 100;
1118 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1119 unsigned long rel_usecs)
1121 trace_seq_printf(s, " %4lldus", abs_usecs);
1122 if (rel_usecs > preempt_mark_thresh)
1123 trace_seq_puts(s, "!: ");
1124 else if (rel_usecs > 1)
1125 trace_seq_puts(s, "+: ");
1127 trace_seq_puts(s, " : ");
1130 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1133 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1135 struct trace_seq *s = &iter->seq;
1136 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1137 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1138 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1139 struct trace_entry *entry = iter->ent;
1140 unsigned long abs_usecs;
1141 unsigned long rel_usecs;
1147 rel_usecs = ns2usecs(next_entry->t - entry->t);
1148 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1151 comm = trace_find_cmdline(entry->pid);
1152 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1153 " %ld.%03ldms (+%ld.%03ldms): ",
1155 entry->pid, cpu, entry->flags,
1156 entry->preempt_count, trace_idx,
1159 abs_usecs % 1000, rel_usecs/1000,
1162 lat_print_generic(s, entry, cpu);
1163 lat_print_timestamp(s, abs_usecs, rel_usecs);
1165 switch (entry->type) {
1167 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1168 trace_seq_puts(s, " (");
1169 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1170 trace_seq_puts(s, ")\n");
1173 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1174 state_to_char[entry->ctx.prev_state] : 'X';
1175 comm = trace_find_cmdline(entry->ctx.next_pid);
1176 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1177 entry->ctx.prev_pid,
1178 entry->ctx.prev_prio,
1180 entry->ctx.next_pid,
1181 entry->ctx.next_prio,
1185 trace_seq_printf(s, " %lx %lx %lx\n",
1186 entry->special.arg1,
1187 entry->special.arg2,
1188 entry->special.arg3);
1191 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1196 static notrace int print_trace_fmt(struct trace_iterator *iter)
1198 struct trace_seq *s = &iter->seq;
1199 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1200 struct trace_entry *entry;
1201 unsigned long usec_rem;
1202 unsigned long long t;
1210 comm = trace_find_cmdline(iter->ent->pid);
1212 t = ns2usecs(entry->t);
1213 usec_rem = do_div(t, 1000000ULL);
1214 secs = (unsigned long)t;
1216 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1219 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1222 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1226 switch (entry->type) {
1228 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1231 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1232 entry->fn.parent_ip) {
1233 ret = trace_seq_printf(s, " <-");
1236 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1241 ret = trace_seq_printf(s, "\n");
1246 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1247 state_to_char[entry->ctx.prev_state] : 'X';
1248 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1249 entry->ctx.prev_pid,
1250 entry->ctx.prev_prio,
1252 entry->ctx.next_pid,
1253 entry->ctx.next_prio);
1258 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1259 entry->special.arg1,
1260 entry->special.arg2,
1261 entry->special.arg3);
1269 static notrace int print_raw_fmt(struct trace_iterator *iter)
1271 struct trace_seq *s = &iter->seq;
1272 struct trace_entry *entry;
1278 ret = trace_seq_printf(s, "%d %d %llu ",
1279 entry->pid, iter->cpu, entry->t);
1283 switch (entry->type) {
1285 ret = trace_seq_printf(s, "%x %x\n",
1286 entry->fn.ip, entry->fn.parent_ip);
1291 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1292 state_to_char[entry->ctx.prev_state] : 'X';
1293 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1294 entry->ctx.prev_pid,
1295 entry->ctx.prev_prio,
1297 entry->ctx.next_pid,
1298 entry->ctx.next_prio);
1303 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1304 entry->special.arg1,
1305 entry->special.arg2,
1306 entry->special.arg3);
1314 #define SEQ_PUT_FIELD_RET(s, x) \
1316 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1320 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1322 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1326 static notrace int print_hex_fmt(struct trace_iterator *iter)
1328 struct trace_seq *s = &iter->seq;
1329 unsigned char newline = '\n';
1330 struct trace_entry *entry;
1335 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1336 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1337 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1339 switch (entry->type) {
1341 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1342 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1345 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1346 state_to_char[entry->ctx.prev_state] : 'X';
1347 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1348 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1349 SEQ_PUT_HEX_FIELD_RET(s, S);
1350 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1351 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1352 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1355 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1356 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1357 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1360 SEQ_PUT_FIELD_RET(s, newline);
1365 static notrace int print_bin_fmt(struct trace_iterator *iter)
1367 struct trace_seq *s = &iter->seq;
1368 struct trace_entry *entry;
1372 SEQ_PUT_FIELD_RET(s, entry->pid);
1373 SEQ_PUT_FIELD_RET(s, entry->cpu);
1374 SEQ_PUT_FIELD_RET(s, entry->t);
1376 switch (entry->type) {
1378 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1379 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1382 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1383 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1384 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1385 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1386 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1389 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1390 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1391 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1397 static int trace_empty(struct trace_iterator *iter)
1399 struct trace_array_cpu *data;
1402 for_each_possible_cpu(cpu) {
1403 data = iter->tr->data[cpu];
1405 if (head_page(data) && data->trace_idx &&
1406 (data->trace_tail != data->trace_head ||
1407 data->trace_tail_idx != data->trace_head_idx))
1413 static int print_trace_line(struct trace_iterator *iter)
1415 if (trace_flags & TRACE_ITER_BIN)
1416 return print_bin_fmt(iter);
1418 if (trace_flags & TRACE_ITER_HEX)
1419 return print_hex_fmt(iter);
1421 if (trace_flags & TRACE_ITER_RAW)
1422 return print_raw_fmt(iter);
1424 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1425 return print_lat_fmt(iter, iter->idx, iter->cpu);
1427 return print_trace_fmt(iter);
1430 static int s_show(struct seq_file *m, void *v)
1432 struct trace_iterator *iter = v;
1434 if (iter->ent == NULL) {
1436 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1439 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1440 /* print nothing if the buffers are empty */
1441 if (trace_empty(iter))
1443 print_trace_header(m, iter);
1444 if (!(trace_flags & TRACE_ITER_VERBOSE))
1445 print_lat_help_header(m);
1447 if (!(trace_flags & TRACE_ITER_VERBOSE))
1448 print_func_help_header(m);
1451 print_trace_line(iter);
1452 trace_print_seq(m, &iter->seq);
1458 static struct seq_operations tracer_seq_ops = {
1465 static struct trace_iterator notrace *
1466 __tracing_open(struct inode *inode, struct file *file, int *ret)
1468 struct trace_iterator *iter;
1470 if (tracing_disabled) {
1475 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1481 mutex_lock(&trace_types_lock);
1482 if (current_trace && current_trace->print_max)
1485 iter->tr = inode->i_private;
1486 iter->trace = current_trace;
1489 /* TODO stop tracer */
1490 *ret = seq_open(file, &tracer_seq_ops);
1492 struct seq_file *m = file->private_data;
1495 /* stop the trace while dumping */
1499 if (iter->trace && iter->trace->open)
1500 iter->trace->open(iter);
1505 mutex_unlock(&trace_types_lock);
1511 int tracing_open_generic(struct inode *inode, struct file *filp)
1513 if (tracing_disabled)
1516 filp->private_data = inode->i_private;
1520 int tracing_release(struct inode *inode, struct file *file)
1522 struct seq_file *m = (struct seq_file *)file->private_data;
1523 struct trace_iterator *iter = m->private;
1525 mutex_lock(&trace_types_lock);
1526 if (iter->trace && iter->trace->close)
1527 iter->trace->close(iter);
1529 /* reenable tracing if it was previously enabled */
1532 mutex_unlock(&trace_types_lock);
1534 seq_release(inode, file);
1539 static int tracing_open(struct inode *inode, struct file *file)
1543 __tracing_open(inode, file, &ret);
1548 static int tracing_lt_open(struct inode *inode, struct file *file)
1550 struct trace_iterator *iter;
1553 iter = __tracing_open(inode, file, &ret);
1556 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1562 static notrace void *
1563 t_next(struct seq_file *m, void *v, loff_t *pos)
1565 struct tracer *t = m->private;
1577 static void *t_start(struct seq_file *m, loff_t *pos)
1579 struct tracer *t = m->private;
1582 mutex_lock(&trace_types_lock);
1583 for (; t && l < *pos; t = t_next(m, t, &l))
1589 static void t_stop(struct seq_file *m, void *p)
1591 mutex_unlock(&trace_types_lock);
1594 static int t_show(struct seq_file *m, void *v)
1596 struct tracer *t = v;
1601 seq_printf(m, "%s", t->name);
1610 static struct seq_operations show_traces_seq_ops = {
1617 static int show_traces_open(struct inode *inode, struct file *file)
1621 if (tracing_disabled)
1624 ret = seq_open(file, &show_traces_seq_ops);
1626 struct seq_file *m = file->private_data;
1627 m->private = trace_types;
1633 static struct file_operations tracing_fops = {
1634 .open = tracing_open,
1636 .llseek = seq_lseek,
1637 .release = tracing_release,
1640 static struct file_operations tracing_lt_fops = {
1641 .open = tracing_lt_open,
1643 .llseek = seq_lseek,
1644 .release = tracing_release,
1647 static struct file_operations show_traces_fops = {
1648 .open = show_traces_open,
1650 .release = seq_release,
1654 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1655 size_t cnt, loff_t *ppos)
1662 /* calulate max size */
1663 for (i = 0; trace_options[i]; i++) {
1664 len += strlen(trace_options[i]);
1665 len += 3; /* "no" and space */
1668 /* +2 for \n and \0 */
1669 buf = kmalloc(len + 2, GFP_KERNEL);
1673 for (i = 0; trace_options[i]; i++) {
1674 if (trace_flags & (1 << i))
1675 r += sprintf(buf + r, "%s ", trace_options[i]);
1677 r += sprintf(buf + r, "no%s ", trace_options[i]);
1680 r += sprintf(buf + r, "\n");
1681 WARN_ON(r >= len + 2);
1683 r = simple_read_from_buffer(ubuf, cnt, ppos,
1692 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1693 size_t cnt, loff_t *ppos)
1703 if (copy_from_user(&buf, ubuf, cnt))
1708 if (strncmp(buf, "no", 2) == 0) {
1713 for (i = 0; trace_options[i]; i++) {
1714 int len = strlen(trace_options[i]);
1716 if (strncmp(cmp, trace_options[i], len) == 0) {
1718 trace_flags &= ~(1 << i);
1720 trace_flags |= (1 << i);
1730 static struct file_operations tracing_iter_fops = {
1731 .open = tracing_open_generic,
1732 .read = tracing_iter_ctrl_read,
1733 .write = tracing_iter_ctrl_write,
1736 static const char readme_msg[] =
1737 "tracing mini-HOWTO:\n\n"
1739 "# mount -t debugfs nodev /debug\n\n"
1740 "# cat /debug/tracing/available_tracers\n"
1741 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1742 "# cat /debug/tracing/current_tracer\n"
1744 "# echo sched_switch > /debug/tracing/current_tracer\n"
1745 "# cat /debug/tracing/current_tracer\n"
1747 "# cat /debug/tracing/iter_ctrl\n"
1748 "noprint-parent nosym-offset nosym-addr noverbose\n"
1749 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1750 "# echo 1 > /debug/tracing/tracing_enabled\n"
1751 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1752 "echo 0 > /debug/tracing/tracing_enabled\n"
1756 tracing_readme_read(struct file *filp, char __user *ubuf,
1757 size_t cnt, loff_t *ppos)
1759 return simple_read_from_buffer(ubuf, cnt, ppos,
1760 readme_msg, strlen(readme_msg));
1763 static struct file_operations tracing_readme_fops = {
1764 .open = tracing_open_generic,
1765 .read = tracing_readme_read,
1770 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1771 size_t cnt, loff_t *ppos)
1773 struct trace_array *tr = filp->private_data;
1777 r = sprintf(buf, "%ld\n", tr->ctrl);
1778 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1782 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1783 size_t cnt, loff_t *ppos)
1785 struct trace_array *tr = filp->private_data;
1792 if (copy_from_user(&buf, ubuf, cnt))
1797 val = simple_strtoul(buf, NULL, 10);
1801 mutex_lock(&trace_types_lock);
1802 if (tr->ctrl ^ val) {
1810 if (current_trace && current_trace->ctrl_update)
1811 current_trace->ctrl_update(tr);
1813 mutex_unlock(&trace_types_lock);
1821 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1822 size_t cnt, loff_t *ppos)
1824 char buf[max_tracer_type_len+2];
1827 mutex_lock(&trace_types_lock);
1829 r = sprintf(buf, "%s\n", current_trace->name);
1831 r = sprintf(buf, "\n");
1832 mutex_unlock(&trace_types_lock);
1834 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1838 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1839 size_t cnt, loff_t *ppos)
1841 struct trace_array *tr = &global_trace;
1843 char buf[max_tracer_type_len+1];
1846 if (cnt > max_tracer_type_len)
1847 cnt = max_tracer_type_len;
1849 if (copy_from_user(&buf, ubuf, cnt))
1854 /* strip ending whitespace. */
1855 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1858 mutex_lock(&trace_types_lock);
1859 for (t = trace_types; t; t = t->next) {
1860 if (strcmp(t->name, buf) == 0)
1863 if (!t || t == current_trace)
1866 if (current_trace && current_trace->reset)
1867 current_trace->reset(tr);
1874 mutex_unlock(&trace_types_lock);
1882 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1883 size_t cnt, loff_t *ppos)
1885 unsigned long *ptr = filp->private_data;
1889 r = snprintf(buf, 64, "%ld\n",
1890 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1893 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1897 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1898 size_t cnt, loff_t *ppos)
1900 long *ptr = filp->private_data;
1907 if (copy_from_user(&buf, ubuf, cnt))
1912 val = simple_strtoul(buf, NULL, 10);
1919 static atomic_t tracing_reader;
1921 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1923 struct trace_iterator *iter;
1925 if (tracing_disabled)
1928 /* We only allow for reader of the pipe */
1929 if (atomic_inc_return(&tracing_reader) != 1) {
1930 atomic_dec(&tracing_reader);
1934 /* create a buffer to store the information to pass to userspace */
1935 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1939 iter->tr = &global_trace;
1941 filp->private_data = iter;
1946 static int tracing_release_pipe(struct inode *inode, struct file *file)
1948 struct trace_iterator *iter = file->private_data;
1951 atomic_dec(&tracing_reader);
1960 tracing_read_pipe(struct file *filp, char __user *ubuf,
1961 size_t cnt, loff_t *ppos)
1963 struct trace_iterator *iter = filp->private_data;
1964 struct trace_array_cpu *data;
1965 static cpumask_t mask;
1967 unsigned long flags;
1974 /* return any leftover data */
1975 if (iter->seq.len > start) {
1976 len = iter->seq.len - start;
1979 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1988 trace_seq_reset(&iter->seq);
1991 while (trace_empty(iter)) {
1993 * This is a make-shift waitqueue. The reason we don't use
1994 * an actual wait queue is because:
1995 * 1) we only ever have one waiter
1996 * 2) the tracing, traces all functions, we don't want
1997 * the overhead of calling wake_up and friends
1998 * (and tracing them too)
1999 * Anyway, this is really very primitive wakeup.
2001 set_current_state(TASK_INTERRUPTIBLE);
2002 iter->tr->waiter = current;
2004 /* sleep for one second, and try again. */
2005 schedule_timeout(HZ);
2007 iter->tr->waiter = NULL;
2009 if (signal_pending(current))
2013 * We block until we read something and tracing is disabled.
2014 * We still block if tracing is disabled, but we have never
2015 * read anything. This allows a user to cat this file, and
2016 * then enable tracing. But after we have read something,
2017 * we give an EOF when tracing is again disabled.
2019 * iter->pos will be 0 if we haven't read anything.
2021 if (!tracer_enabled && iter->pos)
2027 /* stop when tracing is finished */
2028 if (trace_empty(iter))
2031 if (cnt >= PAGE_SIZE)
2032 cnt = PAGE_SIZE - 1;
2034 memset(iter, 0, sizeof(*iter));
2035 iter->tr = &global_trace;
2039 * We need to stop all tracing on all CPUS to read the
2040 * the next buffer. This is a bit expensive, but is
2041 * not done often. We fill all what we can read,
2042 * and then release the locks again.
2046 local_irq_save(flags);
2047 ftrace_save = ftrace_enabled;
2050 for_each_possible_cpu(cpu) {
2051 data = iter->tr->data[cpu];
2053 if (!head_page(data) || !data->trace_idx)
2056 atomic_inc(&data->disabled);
2060 for_each_cpu_mask(cpu, mask) {
2061 data = iter->tr->data[cpu];
2062 spin_lock(&data->lock);
2065 while (find_next_entry_inc(iter) != NULL) {
2066 int len = iter->seq.len;
2068 ret = print_trace_line(iter);
2070 /* don't print partial lines */
2071 iter->seq.len = len;
2075 trace_consume(iter);
2077 if (iter->seq.len >= cnt)
2081 for_each_cpu_mask(cpu, mask) {
2082 data = iter->tr->data[cpu];
2083 spin_unlock(&data->lock);
2086 for_each_cpu_mask(cpu, mask) {
2087 data = iter->tr->data[cpu];
2088 atomic_dec(&data->disabled);
2090 ftrace_enabled = ftrace_save;
2091 local_irq_restore(flags);
2093 /* Now copy what we have to the user */
2094 read = iter->seq.len;
2098 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2100 if (read < iter->seq.len)
2103 trace_seq_reset(&iter->seq);
2111 static struct file_operations tracing_max_lat_fops = {
2112 .open = tracing_open_generic,
2113 .read = tracing_max_lat_read,
2114 .write = tracing_max_lat_write,
2117 static struct file_operations tracing_ctrl_fops = {
2118 .open = tracing_open_generic,
2119 .read = tracing_ctrl_read,
2120 .write = tracing_ctrl_write,
2123 static struct file_operations set_tracer_fops = {
2124 .open = tracing_open_generic,
2125 .read = tracing_set_trace_read,
2126 .write = tracing_set_trace_write,
2129 static struct file_operations tracing_pipe_fops = {
2130 .open = tracing_open_pipe,
2131 .read = tracing_read_pipe,
2132 .release = tracing_release_pipe,
2135 #ifdef CONFIG_DYNAMIC_FTRACE
2138 tracing_read_long(struct file *filp, char __user *ubuf,
2139 size_t cnt, loff_t *ppos)
2141 unsigned long *p = filp->private_data;
2145 r = sprintf(buf, "%ld\n", *p);
2147 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2150 static struct file_operations tracing_read_long_fops = {
2151 .open = tracing_open_generic,
2152 .read = tracing_read_long,
2156 static struct dentry *d_tracer;
2158 struct dentry *tracing_init_dentry(void)
2165 d_tracer = debugfs_create_dir("tracing", NULL);
2167 if (!d_tracer && !once) {
2169 pr_warning("Could not create debugfs directory 'tracing'\n");
2176 #ifdef CONFIG_FTRACE_SELFTEST
2177 /* Let selftest have access to static functions in this file */
2178 #include "trace_selftest.c"
2181 static __init void tracer_init_debugfs(void)
2183 struct dentry *d_tracer;
2184 struct dentry *entry;
2186 d_tracer = tracing_init_dentry();
2188 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2189 &global_trace, &tracing_ctrl_fops);
2191 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2193 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2194 NULL, &tracing_iter_fops);
2196 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2198 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2199 &global_trace, &tracing_lt_fops);
2201 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2203 entry = debugfs_create_file("trace", 0444, d_tracer,
2204 &global_trace, &tracing_fops);
2206 pr_warning("Could not create debugfs 'trace' entry\n");
2208 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2209 &global_trace, &show_traces_fops);
2211 pr_warning("Could not create debugfs 'trace' entry\n");
2213 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2214 &global_trace, &set_tracer_fops);
2216 pr_warning("Could not create debugfs 'trace' entry\n");
2218 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2219 &tracing_max_latency,
2220 &tracing_max_lat_fops);
2222 pr_warning("Could not create debugfs "
2223 "'tracing_max_latency' entry\n");
2225 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2226 &tracing_thresh, &tracing_max_lat_fops);
2228 pr_warning("Could not create debugfs "
2229 "'tracing_threash' entry\n");
2230 entry = debugfs_create_file("README", 0644, d_tracer,
2231 NULL, &tracing_readme_fops);
2233 pr_warning("Could not create debugfs 'README' entry\n");
2235 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2236 NULL, &tracing_pipe_fops);
2238 pr_warning("Could not create debugfs "
2239 "'tracing_threash' entry\n");
2241 #ifdef CONFIG_DYNAMIC_FTRACE
2242 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2243 &ftrace_update_tot_cnt,
2244 &tracing_read_long_fops);
2246 pr_warning("Could not create debugfs "
2247 "'dyn_ftrace_total_info' entry\n");
2251 /* dummy trace to disable tracing */
2252 static struct tracer no_tracer __read_mostly =
2257 static int trace_alloc_page(void)
2259 struct trace_array_cpu *data;
2260 struct page *page, *tmp;
2265 /* first allocate a page for each CPU */
2266 for_each_possible_cpu(i) {
2267 array = (void *)__get_free_page(GFP_KERNEL);
2268 if (array == NULL) {
2269 printk(KERN_ERR "tracer: failed to allocate page"
2270 "for trace buffer!\n");
2274 page = virt_to_page(array);
2275 list_add(&page->lru, &pages);
2277 /* Only allocate if we are actually using the max trace */
2278 #ifdef CONFIG_TRACER_MAX_TRACE
2279 array = (void *)__get_free_page(GFP_KERNEL);
2280 if (array == NULL) {
2281 printk(KERN_ERR "tracer: failed to allocate page"
2282 "for trace buffer!\n");
2285 page = virt_to_page(array);
2286 list_add(&page->lru, &pages);
2290 /* Now that we successfully allocate a page per CPU, add them */
2291 for_each_possible_cpu(i) {
2292 data = global_trace.data[i];
2293 spin_lock_init(&data->lock);
2294 lockdep_set_class(&data->lock, &data->lock_key);
2295 page = list_entry(pages.next, struct page, lru);
2296 list_del_init(&page->lru);
2297 list_add_tail(&page->lru, &data->trace_pages);
2300 #ifdef CONFIG_TRACER_MAX_TRACE
2301 data = max_tr.data[i];
2302 spin_lock_init(&data->lock);
2303 lockdep_set_class(&data->lock, &data->lock_key);
2304 page = list_entry(pages.next, struct page, lru);
2305 list_del_init(&page->lru);
2306 list_add_tail(&page->lru, &data->trace_pages);
2310 global_trace.entries += ENTRIES_PER_PAGE;
2315 list_for_each_entry_safe(page, tmp, &pages, lru) {
2316 list_del_init(&page->lru);
2322 __init static int tracer_alloc_buffers(void)
2324 struct trace_array_cpu *data;
2331 global_trace.ctrl = tracer_enabled;
2333 /* Allocate the first page for all buffers */
2334 for_each_possible_cpu(i) {
2335 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2336 max_tr.data[i] = &per_cpu(max_data, i);
2338 array = (void *)__get_free_page(GFP_KERNEL);
2339 if (array == NULL) {
2340 printk(KERN_ERR "tracer: failed to allocate page"
2341 "for trace buffer!\n");
2345 /* set the array to the list */
2346 INIT_LIST_HEAD(&data->trace_pages);
2347 page = virt_to_page(array);
2348 list_add(&page->lru, &data->trace_pages);
2349 /* use the LRU flag to differentiate the two buffers */
2352 /* Only allocate if we are actually using the max trace */
2353 #ifdef CONFIG_TRACER_MAX_TRACE
2354 array = (void *)__get_free_page(GFP_KERNEL);
2355 if (array == NULL) {
2356 printk(KERN_ERR "tracer: failed to allocate page"
2357 "for trace buffer!\n");
2361 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2362 page = virt_to_page(array);
2363 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2369 * Since we allocate by orders of pages, we may be able to
2372 global_trace.entries = ENTRIES_PER_PAGE;
2375 while (global_trace.entries < trace_nr_entries) {
2376 if (trace_alloc_page())
2380 max_tr.entries = global_trace.entries;
2382 pr_info("tracer: %d pages allocated for %ld",
2383 pages, trace_nr_entries);
2384 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2385 pr_info(" actual entries %ld\n", global_trace.entries);
2387 tracer_init_debugfs();
2389 trace_init_cmdlines();
2391 register_tracer(&no_tracer);
2392 current_trace = &no_tracer;
2394 /* All seems OK, enable tracing */
2395 tracing_disabled = 0;
2400 for (i-- ; i >= 0; i--) {
2401 struct page *page, *tmp;
2402 struct trace_array_cpu *data = global_trace.data[i];
2405 list_for_each_entry_safe(page, tmp,
2406 &data->trace_pages, lru) {
2407 list_del_init(&page->lru);
2412 #ifdef CONFIG_TRACER_MAX_TRACE
2413 data = max_tr.data[i];
2415 list_for_each_entry_safe(page, tmp,
2416 &data->trace_pages, lru) {
2417 list_del_init(&page->lru);
2425 fs_initcall(tracer_alloc_buffers);