2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/poll.h>
28 #include <linux/gfp.h>
30 #include <linux/writeback.h>
32 #include <linux/stacktrace.h>
36 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
37 unsigned long __read_mostly tracing_thresh;
39 static unsigned long __read_mostly tracing_nr_buffers;
40 static cpumask_t __read_mostly tracing_buffer_mask;
42 #define for_each_tracing_cpu(cpu) \
43 for_each_cpu_mask(cpu, tracing_buffer_mask)
45 /* dummy trace to disable tracing */
46 static struct tracer no_tracer __read_mostly = {
50 static int trace_alloc_page(void);
51 static int trace_free_page(void);
53 static int tracing_disabled = 1;
55 static unsigned long tracing_pages_allocated;
58 ns2usecs(cycle_t nsec)
65 cycle_t ftrace_now(int cpu)
67 return cpu_clock(cpu);
71 * The global_trace is the descriptor that holds the tracing
72 * buffers for the live tracing. For each CPU, it contains
73 * a link list of pages that will store trace entries. The
74 * page descriptor of the pages in the memory is used to hold
75 * the link list by linking the lru item in the page descriptor
76 * to each of the pages in the buffer per CPU.
78 * For each active CPU there is a data field that holds the
79 * pages for the buffer for that CPU. Each CPU has the same number
80 * of pages allocated for its buffer.
82 static struct trace_array global_trace;
84 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
87 * The max_tr is used to snapshot the global_trace when a maximum
88 * latency is reached. Some tracers will use this to store a maximum
89 * trace while it continues examining live traces.
91 * The buffers for the max_tr are set up the same as the global_trace.
92 * When a snapshot is taken, the link list of the max_tr is swapped
93 * with the link list of the global_trace and the buffers are reset for
94 * the global_trace so the tracing can continue.
96 static struct trace_array max_tr;
98 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
100 /* tracer_enabled is used to toggle activation of a tracer */
101 static int tracer_enabled = 1;
104 * trace_nr_entries is the number of entries that is allocated
105 * for a buffer. Note, the number of entries is always rounded
106 * to ENTRIES_PER_PAGE.
108 static unsigned long trace_nr_entries = 65536UL;
110 /* trace_types holds a link list of available tracers. */
111 static struct tracer *trace_types __read_mostly;
113 /* current_trace points to the tracer that is currently active */
114 static struct tracer *current_trace __read_mostly;
117 * max_tracer_type_len is used to simplify the allocating of
118 * buffers to read userspace tracer names. We keep track of
119 * the longest tracer name registered.
121 static int max_tracer_type_len;
124 * trace_types_lock is used to protect the trace_types list.
125 * This lock is also used to keep user access serialized.
126 * Accesses from userspace will grab this lock while userspace
127 * activities happen inside the kernel.
129 static DEFINE_MUTEX(trace_types_lock);
131 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
132 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
134 /* trace_flags holds iter_ctrl options */
135 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
138 * trace_wake_up - wake up tasks waiting for trace input
140 * Simply wakes up any task that is blocked on the trace_wait
141 * queue. These is used with trace_poll for tasks polling the trace.
143 void trace_wake_up(void)
146 * The runqueue_is_locked() can fail, but this is the best we
149 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
150 wake_up(&trace_wait);
153 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
155 static int __init set_nr_entries(char *str)
157 unsigned long nr_entries;
162 ret = strict_strtoul(str, 0, &nr_entries);
163 /* nr_entries can not be zero */
164 if (ret < 0 || nr_entries == 0)
166 trace_nr_entries = nr_entries;
169 __setup("trace_entries=", set_nr_entries);
171 unsigned long nsecs_to_usecs(unsigned long nsecs)
177 * trace_flag_type is an enumeration that holds different
178 * states when a trace occurs. These are:
179 * IRQS_OFF - interrupts were disabled
180 * NEED_RESCED - reschedule is requested
181 * HARDIRQ - inside an interrupt handler
182 * SOFTIRQ - inside a softirq handler
184 enum trace_flag_type {
185 TRACE_FLAG_IRQS_OFF = 0x01,
186 TRACE_FLAG_NEED_RESCHED = 0x02,
187 TRACE_FLAG_HARDIRQ = 0x04,
188 TRACE_FLAG_SOFTIRQ = 0x08,
192 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
193 * control the output of kernel symbols.
195 #define TRACE_ITER_SYM_MASK \
196 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
198 /* These must match the bit postions in trace_iterator_flags */
199 static const char *trace_options[] = {
214 * ftrace_max_lock is used to protect the swapping of buffers
215 * when taking a max snapshot. The buffers themselves are
216 * protected by per_cpu spinlocks. But the action of the swap
217 * needs its own lock.
219 * This is defined as a raw_spinlock_t in order to help
220 * with performance when lockdep debugging is enabled.
222 static raw_spinlock_t ftrace_max_lock =
223 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
226 * Copy the new maximum trace into the separate maximum-trace
227 * structure. (this way the maximum trace is permanently saved,
228 * for later retrieval via /debugfs/tracing/latency_trace)
231 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
233 struct trace_array_cpu *data = tr->data[cpu];
236 max_tr.time_start = data->preempt_timestamp;
238 data = max_tr.data[cpu];
239 data->saved_latency = tracing_max_latency;
241 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
242 data->pid = tsk->pid;
243 data->uid = tsk->uid;
244 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
245 data->policy = tsk->policy;
246 data->rt_priority = tsk->rt_priority;
248 /* record this tasks comm */
249 tracing_record_cmdline(current);
252 #define CHECK_COND(cond) \
253 if (unlikely(cond)) { \
254 tracing_disabled = 1; \
260 * check_pages - integrity check of trace buffers
262 * As a safty measure we check to make sure the data pages have not
265 int check_pages(struct trace_array_cpu *data)
267 struct page *page, *tmp;
269 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
270 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
272 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
273 CHECK_COND(page->lru.next->prev != &page->lru);
274 CHECK_COND(page->lru.prev->next != &page->lru);
281 * head_page - page address of the first page in per_cpu buffer.
283 * head_page returns the page address of the first page in
284 * a per_cpu buffer. This also preforms various consistency
285 * checks to make sure the buffer has not been corrupted.
287 void *head_page(struct trace_array_cpu *data)
291 if (list_empty(&data->trace_pages))
294 page = list_entry(data->trace_pages.next, struct page, lru);
295 BUG_ON(&page->lru == &data->trace_pages);
297 return page_address(page);
301 * trace_seq_printf - sequence printing of trace information
302 * @s: trace sequence descriptor
303 * @fmt: printf format string
305 * The tracer may use either sequence operations or its own
306 * copy to user routines. To simplify formating of a trace
307 * trace_seq_printf is used to store strings into a special
308 * buffer (@s). Then the output may be either used by
309 * the sequencer or pulled into another buffer.
312 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
314 int len = (PAGE_SIZE - 1) - s->len;
322 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
325 /* If we can't write it all, don't bother writing anything */
335 * trace_seq_puts - trace sequence printing of simple string
336 * @s: trace sequence descriptor
337 * @str: simple string to record
339 * The tracer may use either the sequence operations or its own
340 * copy to user routines. This function records a simple string
341 * into a special buffer (@s) for later retrieval by a sequencer
342 * or other mechanism.
345 trace_seq_puts(struct trace_seq *s, const char *str)
347 int len = strlen(str);
349 if (len > ((PAGE_SIZE - 1) - s->len))
352 memcpy(s->buffer + s->len, str, len);
359 trace_seq_putc(struct trace_seq *s, unsigned char c)
361 if (s->len >= (PAGE_SIZE - 1))
364 s->buffer[s->len++] = c;
370 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
372 if (len > ((PAGE_SIZE - 1) - s->len))
375 memcpy(s->buffer + s->len, mem, len);
382 static const char hex2asc[] = "0123456789abcdef";
385 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
387 unsigned char hex[HEX_CHARS];
388 unsigned char *data = mem;
392 BUG_ON(len >= HEX_CHARS);
395 for (i = 0, j = 0; i < len; i++) {
397 for (i = len-1, j = 0; i >= 0; i--) {
401 hex[j++] = hex2asc[byte & 0x0f];
402 hex[j++] = hex2asc[byte >> 4];
406 return trace_seq_putmem(s, hex, j);
410 trace_seq_reset(struct trace_seq *s)
416 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
421 if (s->len <= s->readpos)
424 len = s->len - s->readpos;
427 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
436 trace_print_seq(struct seq_file *m, struct trace_seq *s)
438 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
441 seq_puts(m, s->buffer);
447 * flip the trace buffers between two trace descriptors.
448 * This usually is the buffers between the global_trace and
449 * the max_tr to record a snapshot of a current trace.
451 * The ftrace_max_lock must be held.
454 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
456 struct list_head flip_pages;
458 INIT_LIST_HEAD(&flip_pages);
460 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
461 sizeof(struct trace_array_cpu) -
462 offsetof(struct trace_array_cpu, trace_head_idx));
466 list_splice_init(&tr1->trace_pages, &flip_pages);
467 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
468 list_splice_init(&flip_pages, &tr2->trace_pages);
469 BUG_ON(!list_empty(&flip_pages));
475 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
477 * @tsk: the task with the latency
478 * @cpu: The cpu that initiated the trace.
480 * Flip the buffers between the @tr and the max_tr and record information
481 * about which task was the cause of this latency.
484 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
486 struct trace_array_cpu *data;
489 WARN_ON_ONCE(!irqs_disabled());
490 __raw_spin_lock(&ftrace_max_lock);
491 /* clear out all the previous traces */
492 for_each_tracing_cpu(i) {
494 flip_trace(max_tr.data[i], data);
498 __update_max_tr(tr, tsk, cpu);
499 __raw_spin_unlock(&ftrace_max_lock);
503 * update_max_tr_single - only copy one trace over, and reset the rest
505 * @tsk - task with the latency
506 * @cpu - the cpu of the buffer to copy.
508 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
511 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
513 struct trace_array_cpu *data = tr->data[cpu];
516 WARN_ON_ONCE(!irqs_disabled());
517 __raw_spin_lock(&ftrace_max_lock);
518 for_each_tracing_cpu(i)
519 tracing_reset(max_tr.data[i]);
521 flip_trace(max_tr.data[cpu], data);
524 __update_max_tr(tr, tsk, cpu);
525 __raw_spin_unlock(&ftrace_max_lock);
529 * register_tracer - register a tracer with the ftrace system.
530 * @type - the plugin for the tracer
532 * Register a new plugin tracer.
534 int register_tracer(struct tracer *type)
541 pr_info("Tracer must have a name\n");
545 mutex_lock(&trace_types_lock);
546 for (t = trace_types; t; t = t->next) {
547 if (strcmp(type->name, t->name) == 0) {
549 pr_info("Trace %s already registered\n",
556 #ifdef CONFIG_FTRACE_STARTUP_TEST
557 if (type->selftest) {
558 struct tracer *saved_tracer = current_trace;
559 struct trace_array_cpu *data;
560 struct trace_array *tr = &global_trace;
561 int saved_ctrl = tr->ctrl;
564 * Run a selftest on this tracer.
565 * Here we reset the trace buffer, and set the current
566 * tracer to be this tracer. The tracer can then run some
567 * internal tracing to verify that everything is in order.
568 * If we fail, we do not register this tracer.
570 for_each_tracing_cpu(i) {
572 if (!head_page(data))
576 current_trace = type;
578 /* the test is responsible for initializing and enabling */
579 pr_info("Testing tracer %s: ", type->name);
580 ret = type->selftest(type, tr);
581 /* the test is responsible for resetting too */
582 current_trace = saved_tracer;
583 tr->ctrl = saved_ctrl;
585 printk(KERN_CONT "FAILED!\n");
588 /* Only reset on passing, to avoid touching corrupted buffers */
589 for_each_tracing_cpu(i) {
591 if (!head_page(data))
595 printk(KERN_CONT "PASSED\n");
599 type->next = trace_types;
601 len = strlen(type->name);
602 if (len > max_tracer_type_len)
603 max_tracer_type_len = len;
606 mutex_unlock(&trace_types_lock);
611 void unregister_tracer(struct tracer *type)
616 mutex_lock(&trace_types_lock);
617 for (t = &trace_types; *t; t = &(*t)->next) {
621 pr_info("Trace %s not registered\n", type->name);
626 if (strlen(type->name) != max_tracer_type_len)
629 max_tracer_type_len = 0;
630 for (t = &trace_types; *t; t = &(*t)->next) {
631 len = strlen((*t)->name);
632 if (len > max_tracer_type_len)
633 max_tracer_type_len = len;
636 mutex_unlock(&trace_types_lock);
639 void tracing_reset(struct trace_array_cpu *data)
643 data->trace_head = data->trace_tail = head_page(data);
644 data->trace_head_idx = 0;
645 data->trace_tail_idx = 0;
648 #define SAVED_CMDLINES 128
649 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
650 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
651 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
652 static int cmdline_idx;
653 static DEFINE_SPINLOCK(trace_cmdline_lock);
655 /* trace in all context switches */
656 atomic_t trace_record_cmdline_enabled __read_mostly;
658 /* temporary disable recording */
659 atomic_t trace_record_cmdline_disabled __read_mostly;
661 static void trace_init_cmdlines(void)
663 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
664 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
668 void trace_stop_cmdline_recording(void);
670 static void trace_save_cmdline(struct task_struct *tsk)
675 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
679 * It's not the end of the world if we don't get
680 * the lock, but we also don't want to spin
681 * nor do we want to disable interrupts,
682 * so if we miss here, then better luck next time.
684 if (!spin_trylock(&trace_cmdline_lock))
687 idx = map_pid_to_cmdline[tsk->pid];
688 if (idx >= SAVED_CMDLINES) {
689 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
691 map = map_cmdline_to_pid[idx];
692 if (map <= PID_MAX_DEFAULT)
693 map_pid_to_cmdline[map] = (unsigned)-1;
695 map_pid_to_cmdline[tsk->pid] = idx;
700 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
702 spin_unlock(&trace_cmdline_lock);
705 static char *trace_find_cmdline(int pid)
707 char *cmdline = "<...>";
713 if (pid > PID_MAX_DEFAULT)
716 map = map_pid_to_cmdline[pid];
717 if (map >= SAVED_CMDLINES)
720 cmdline = saved_cmdlines[map];
726 void tracing_record_cmdline(struct task_struct *tsk)
728 if (atomic_read(&trace_record_cmdline_disabled))
731 trace_save_cmdline(tsk);
734 static inline struct list_head *
735 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
738 * Roundrobin - but skip the head (which is not a real page):
741 if (unlikely(next == &data->trace_pages))
743 BUG_ON(next == &data->trace_pages);
749 trace_next_page(struct trace_array_cpu *data, void *addr)
751 struct list_head *next;
754 page = virt_to_page(addr);
756 next = trace_next_list(data, &page->lru);
757 page = list_entry(next, struct page, lru);
759 return page_address(page);
762 static inline struct trace_entry *
763 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
765 unsigned long idx, idx_next;
766 struct trace_entry *entry;
769 idx = data->trace_head_idx;
772 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
774 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
776 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
777 data->trace_head = trace_next_page(data, data->trace_head);
781 if (data->trace_head == data->trace_tail &&
782 idx_next == data->trace_tail_idx) {
785 data->trace_tail_idx++;
786 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
788 trace_next_page(data, data->trace_tail);
789 data->trace_tail_idx = 0;
793 data->trace_head_idx = idx_next;
799 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
801 struct task_struct *tsk = current;
804 pc = preempt_count();
806 entry->preempt_count = pc & 0xff;
807 entry->pid = (tsk) ? tsk->pid : 0;
808 entry->t = ftrace_now(raw_smp_processor_id());
809 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
810 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
811 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
812 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
816 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
817 unsigned long ip, unsigned long parent_ip, unsigned long flags)
819 struct trace_entry *entry;
820 unsigned long irq_flags;
822 raw_local_irq_save(irq_flags);
823 __raw_spin_lock(&data->lock);
824 entry = tracing_get_trace_entry(tr, data);
825 tracing_generic_entry_update(entry, flags);
826 entry->type = TRACE_FN;
828 entry->fn.parent_ip = parent_ip;
829 __raw_spin_unlock(&data->lock);
830 raw_local_irq_restore(irq_flags);
834 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
835 unsigned long ip, unsigned long parent_ip, unsigned long flags)
837 if (likely(!atomic_read(&data->disabled)))
838 trace_function(tr, data, ip, parent_ip, flags);
841 void __trace_stack(struct trace_array *tr,
842 struct trace_array_cpu *data,
846 struct trace_entry *entry;
847 struct stack_trace trace;
849 if (!(trace_flags & TRACE_ITER_STACKTRACE))
852 entry = tracing_get_trace_entry(tr, data);
853 tracing_generic_entry_update(entry, flags);
854 entry->type = TRACE_STACK;
856 memset(&entry->stack, 0, sizeof(entry->stack));
858 trace.nr_entries = 0;
859 trace.max_entries = FTRACE_STACK_ENTRIES;
861 trace.entries = entry->stack.caller;
863 save_stack_trace(&trace);
867 __trace_special(void *__tr, void *__data,
868 unsigned long arg1, unsigned long arg2, unsigned long arg3)
870 struct trace_array_cpu *data = __data;
871 struct trace_array *tr = __tr;
872 struct trace_entry *entry;
873 unsigned long irq_flags;
875 raw_local_irq_save(irq_flags);
876 __raw_spin_lock(&data->lock);
877 entry = tracing_get_trace_entry(tr, data);
878 tracing_generic_entry_update(entry, 0);
879 entry->type = TRACE_SPECIAL;
880 entry->special.arg1 = arg1;
881 entry->special.arg2 = arg2;
882 entry->special.arg3 = arg3;
883 __trace_stack(tr, data, irq_flags, 4);
884 __raw_spin_unlock(&data->lock);
885 raw_local_irq_restore(irq_flags);
891 tracing_sched_switch_trace(struct trace_array *tr,
892 struct trace_array_cpu *data,
893 struct task_struct *prev,
894 struct task_struct *next,
897 struct trace_entry *entry;
898 unsigned long irq_flags;
900 raw_local_irq_save(irq_flags);
901 __raw_spin_lock(&data->lock);
902 entry = tracing_get_trace_entry(tr, data);
903 tracing_generic_entry_update(entry, flags);
904 entry->type = TRACE_CTX;
905 entry->ctx.prev_pid = prev->pid;
906 entry->ctx.prev_prio = prev->prio;
907 entry->ctx.prev_state = prev->state;
908 entry->ctx.next_pid = next->pid;
909 entry->ctx.next_prio = next->prio;
910 entry->ctx.next_state = next->state;
911 __trace_stack(tr, data, flags, 5);
912 __raw_spin_unlock(&data->lock);
913 raw_local_irq_restore(irq_flags);
917 tracing_sched_wakeup_trace(struct trace_array *tr,
918 struct trace_array_cpu *data,
919 struct task_struct *wakee,
920 struct task_struct *curr,
923 struct trace_entry *entry;
924 unsigned long irq_flags;
926 raw_local_irq_save(irq_flags);
927 __raw_spin_lock(&data->lock);
928 entry = tracing_get_trace_entry(tr, data);
929 tracing_generic_entry_update(entry, flags);
930 entry->type = TRACE_WAKE;
931 entry->ctx.prev_pid = curr->pid;
932 entry->ctx.prev_prio = curr->prio;
933 entry->ctx.prev_state = curr->state;
934 entry->ctx.next_pid = wakee->pid;
935 entry->ctx.next_prio = wakee->prio;
936 entry->ctx.next_state = wakee->state;
937 __trace_stack(tr, data, flags, 6);
938 __raw_spin_unlock(&data->lock);
939 raw_local_irq_restore(irq_flags);
946 function_trace_call(unsigned long ip, unsigned long parent_ip)
948 struct trace_array *tr = &global_trace;
949 struct trace_array_cpu *data;
954 if (unlikely(!tracer_enabled))
957 local_irq_save(flags);
958 cpu = raw_smp_processor_id();
959 data = tr->data[cpu];
960 disabled = atomic_inc_return(&data->disabled);
962 if (likely(disabled == 1))
963 trace_function(tr, data, ip, parent_ip, flags);
965 atomic_dec(&data->disabled);
966 local_irq_restore(flags);
969 static struct ftrace_ops trace_ops __read_mostly =
971 .func = function_trace_call,
974 void tracing_start_function_trace(void)
976 register_ftrace_function(&trace_ops);
979 void tracing_stop_function_trace(void)
981 unregister_ftrace_function(&trace_ops);
985 enum trace_file_type {
986 TRACE_FILE_LAT_FMT = 1,
989 static struct trace_entry *
990 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
991 struct trace_iterator *iter, int cpu)
994 struct trace_entry *array;
996 if (iter->next_idx[cpu] >= tr->entries ||
997 iter->next_idx[cpu] >= data->trace_idx ||
998 (data->trace_head == data->trace_tail &&
999 data->trace_head_idx == data->trace_tail_idx))
1002 if (!iter->next_page[cpu]) {
1003 /* Initialize the iterator for this cpu trace buffer */
1004 WARN_ON(!data->trace_tail);
1005 page = virt_to_page(data->trace_tail);
1006 iter->next_page[cpu] = &page->lru;
1007 iter->next_page_idx[cpu] = data->trace_tail_idx;
1010 page = list_entry(iter->next_page[cpu], struct page, lru);
1011 BUG_ON(&data->trace_pages == &page->lru);
1013 array = page_address(page);
1015 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1016 return &array[iter->next_page_idx[cpu]];
1019 static struct trace_entry *
1020 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1022 struct trace_array *tr = iter->tr;
1023 struct trace_entry *ent, *next = NULL;
1027 for_each_tracing_cpu(cpu) {
1028 if (!head_page(tr->data[cpu]))
1030 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1032 * Pick the entry with the smallest timestamp:
1034 if (ent && (!next || ent->t < next->t)) {
1041 *ent_cpu = next_cpu;
1046 static void trace_iterator_increment(struct trace_iterator *iter)
1049 iter->next_idx[iter->cpu]++;
1050 iter->next_page_idx[iter->cpu]++;
1052 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1053 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1055 iter->next_page_idx[iter->cpu] = 0;
1056 iter->next_page[iter->cpu] =
1057 trace_next_list(data, iter->next_page[iter->cpu]);
1061 static void trace_consume(struct trace_iterator *iter)
1063 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1065 data->trace_tail_idx++;
1066 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1067 data->trace_tail = trace_next_page(data, data->trace_tail);
1068 data->trace_tail_idx = 0;
1071 /* Check if we empty it, then reset the index */
1072 if (data->trace_head == data->trace_tail &&
1073 data->trace_head_idx == data->trace_tail_idx)
1074 data->trace_idx = 0;
1077 static void *find_next_entry_inc(struct trace_iterator *iter)
1079 struct trace_entry *next;
1082 next = find_next_entry(iter, &next_cpu);
1084 iter->prev_ent = iter->ent;
1085 iter->prev_cpu = iter->cpu;
1088 iter->cpu = next_cpu;
1091 trace_iterator_increment(iter);
1093 return next ? iter : NULL;
1096 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1098 struct trace_iterator *iter = m->private;
1099 void *last_ent = iter->ent;
1105 /* can't go backwards */
1110 ent = find_next_entry_inc(iter);
1114 while (ent && iter->idx < i)
1115 ent = find_next_entry_inc(iter);
1119 if (last_ent && !ent)
1120 seq_puts(m, "\n\nvim:ft=help\n");
1125 static void *s_start(struct seq_file *m, loff_t *pos)
1127 struct trace_iterator *iter = m->private;
1132 mutex_lock(&trace_types_lock);
1134 if (!current_trace || current_trace != iter->trace) {
1135 mutex_unlock(&trace_types_lock);
1139 atomic_inc(&trace_record_cmdline_disabled);
1141 /* let the tracer grab locks here if needed */
1142 if (current_trace->start)
1143 current_trace->start(iter);
1145 if (*pos != iter->pos) {
1149 iter->prev_ent = NULL;
1150 iter->prev_cpu = -1;
1152 for_each_tracing_cpu(i) {
1153 iter->next_idx[i] = 0;
1154 iter->next_page[i] = NULL;
1157 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1162 p = s_next(m, p, &l);
1168 static void s_stop(struct seq_file *m, void *p)
1170 struct trace_iterator *iter = m->private;
1172 atomic_dec(&trace_record_cmdline_disabled);
1174 /* let the tracer release locks here if needed */
1175 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1176 iter->trace->stop(iter);
1178 mutex_unlock(&trace_types_lock);
1182 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1184 #ifdef CONFIG_KALLSYMS
1185 char str[KSYM_SYMBOL_LEN];
1187 kallsyms_lookup(address, NULL, NULL, NULL, str);
1189 return trace_seq_printf(s, fmt, str);
1195 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1196 unsigned long address)
1198 #ifdef CONFIG_KALLSYMS
1199 char str[KSYM_SYMBOL_LEN];
1201 sprint_symbol(str, address);
1202 return trace_seq_printf(s, fmt, str);
1207 #ifndef CONFIG_64BIT
1208 # define IP_FMT "%08lx"
1210 # define IP_FMT "%016lx"
1214 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1219 return trace_seq_printf(s, "0");
1221 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1222 ret = seq_print_sym_offset(s, "%s", ip);
1224 ret = seq_print_sym_short(s, "%s", ip);
1229 if (sym_flags & TRACE_ITER_SYM_ADDR)
1230 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1234 static void print_lat_help_header(struct seq_file *m)
1236 seq_puts(m, "# _------=> CPU# \n");
1237 seq_puts(m, "# / _-----=> irqs-off \n");
1238 seq_puts(m, "# | / _----=> need-resched \n");
1239 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1240 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1241 seq_puts(m, "# |||| / \n");
1242 seq_puts(m, "# ||||| delay \n");
1243 seq_puts(m, "# cmd pid ||||| time | caller \n");
1244 seq_puts(m, "# \\ / ||||| \\ | / \n");
1247 static void print_func_help_header(struct seq_file *m)
1249 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1250 seq_puts(m, "# | | | | |\n");
1255 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1257 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1258 struct trace_array *tr = iter->tr;
1259 struct trace_array_cpu *data = tr->data[tr->cpu];
1260 struct tracer *type = current_trace;
1261 unsigned long total = 0;
1262 unsigned long entries = 0;
1264 const char *name = "preemption";
1269 for_each_tracing_cpu(cpu) {
1270 if (head_page(tr->data[cpu])) {
1271 total += tr->data[cpu]->trace_idx;
1272 if (tr->data[cpu]->trace_idx > tr->entries)
1273 entries += tr->entries;
1275 entries += tr->data[cpu]->trace_idx;
1279 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1281 seq_puts(m, "-----------------------------------"
1282 "---------------------------------\n");
1283 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1284 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1285 nsecs_to_usecs(data->saved_latency),
1289 #if defined(CONFIG_PREEMPT_NONE)
1291 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1293 #elif defined(CONFIG_PREEMPT_DESKTOP)
1298 /* These are reserved for later use */
1301 seq_printf(m, " #P:%d)\n", num_online_cpus());
1305 seq_puts(m, " -----------------\n");
1306 seq_printf(m, " | task: %.16s-%d "
1307 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1308 data->comm, data->pid, data->uid, data->nice,
1309 data->policy, data->rt_priority);
1310 seq_puts(m, " -----------------\n");
1312 if (data->critical_start) {
1313 seq_puts(m, " => started at: ");
1314 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1315 trace_print_seq(m, &iter->seq);
1316 seq_puts(m, "\n => ended at: ");
1317 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1318 trace_print_seq(m, &iter->seq);
1326 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1328 int hardirq, softirq;
1331 comm = trace_find_cmdline(entry->pid);
1333 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1334 trace_seq_printf(s, "%d", cpu);
1335 trace_seq_printf(s, "%c%c",
1336 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1337 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1339 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1340 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1341 if (hardirq && softirq) {
1342 trace_seq_putc(s, 'H');
1345 trace_seq_putc(s, 'h');
1348 trace_seq_putc(s, 's');
1350 trace_seq_putc(s, '.');
1354 if (entry->preempt_count)
1355 trace_seq_printf(s, "%x", entry->preempt_count);
1357 trace_seq_puts(s, ".");
1360 unsigned long preempt_mark_thresh = 100;
1363 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1364 unsigned long rel_usecs)
1366 trace_seq_printf(s, " %4lldus", abs_usecs);
1367 if (rel_usecs > preempt_mark_thresh)
1368 trace_seq_puts(s, "!: ");
1369 else if (rel_usecs > 1)
1370 trace_seq_puts(s, "+: ");
1372 trace_seq_puts(s, " : ");
1375 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1378 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1380 struct trace_seq *s = &iter->seq;
1381 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1382 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1383 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1384 struct trace_entry *entry = iter->ent;
1385 unsigned long abs_usecs;
1386 unsigned long rel_usecs;
1394 rel_usecs = ns2usecs(next_entry->t - entry->t);
1395 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1398 comm = trace_find_cmdline(entry->pid);
1399 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1400 " %ld.%03ldms (+%ld.%03ldms): ",
1402 entry->pid, cpu, entry->flags,
1403 entry->preempt_count, trace_idx,
1406 abs_usecs % 1000, rel_usecs/1000,
1409 lat_print_generic(s, entry, cpu);
1410 lat_print_timestamp(s, abs_usecs, rel_usecs);
1412 switch (entry->type) {
1414 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1415 trace_seq_puts(s, " (");
1416 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1417 trace_seq_puts(s, ")\n");
1421 T = entry->ctx.next_state < sizeof(state_to_char) ?
1422 state_to_char[entry->ctx.next_state] : 'X';
1424 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1425 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1426 comm = trace_find_cmdline(entry->ctx.next_pid);
1427 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1428 entry->ctx.prev_pid,
1429 entry->ctx.prev_prio,
1430 S, entry->type == TRACE_CTX ? "==>" : " +",
1431 entry->ctx.next_pid,
1432 entry->ctx.next_prio,
1436 trace_seq_printf(s, "# %ld %ld %ld\n",
1437 entry->special.arg1,
1438 entry->special.arg2,
1439 entry->special.arg3);
1442 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1444 trace_seq_puts(s, " <= ");
1445 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1447 trace_seq_puts(s, "\n");
1450 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1455 static int print_trace_fmt(struct trace_iterator *iter)
1457 struct trace_seq *s = &iter->seq;
1458 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1459 struct trace_entry *entry;
1460 unsigned long usec_rem;
1461 unsigned long long t;
1470 comm = trace_find_cmdline(iter->ent->pid);
1472 t = ns2usecs(entry->t);
1473 usec_rem = do_div(t, 1000000ULL);
1474 secs = (unsigned long)t;
1476 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1479 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1482 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1486 switch (entry->type) {
1488 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1491 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1492 entry->fn.parent_ip) {
1493 ret = trace_seq_printf(s, " <-");
1496 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1501 ret = trace_seq_printf(s, "\n");
1507 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1508 state_to_char[entry->ctx.prev_state] : 'X';
1509 T = entry->ctx.next_state < sizeof(state_to_char) ?
1510 state_to_char[entry->ctx.next_state] : 'X';
1511 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1512 entry->ctx.prev_pid,
1513 entry->ctx.prev_prio,
1515 entry->type == TRACE_CTX ? "==>" : " +",
1516 entry->ctx.next_pid,
1517 entry->ctx.next_prio,
1523 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1524 entry->special.arg1,
1525 entry->special.arg2,
1526 entry->special.arg3);
1531 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1533 ret = trace_seq_puts(s, " <= ");
1537 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1542 ret = trace_seq_puts(s, "\n");
1550 static int print_raw_fmt(struct trace_iterator *iter)
1552 struct trace_seq *s = &iter->seq;
1553 struct trace_entry *entry;
1559 ret = trace_seq_printf(s, "%d %d %llu ",
1560 entry->pid, iter->cpu, entry->t);
1564 switch (entry->type) {
1566 ret = trace_seq_printf(s, "%x %x\n",
1567 entry->fn.ip, entry->fn.parent_ip);
1573 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1574 state_to_char[entry->ctx.prev_state] : 'X';
1575 T = entry->ctx.next_state < sizeof(state_to_char) ?
1576 state_to_char[entry->ctx.next_state] : 'X';
1577 if (entry->type == TRACE_WAKE)
1579 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1580 entry->ctx.prev_pid,
1581 entry->ctx.prev_prio,
1583 entry->ctx.next_pid,
1584 entry->ctx.next_prio,
1591 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1592 entry->special.arg1,
1593 entry->special.arg2,
1594 entry->special.arg3);
1602 #define SEQ_PUT_FIELD_RET(s, x) \
1604 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1608 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1610 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1614 static int print_hex_fmt(struct trace_iterator *iter)
1616 struct trace_seq *s = &iter->seq;
1617 unsigned char newline = '\n';
1618 struct trace_entry *entry;
1623 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1624 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1625 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1627 switch (entry->type) {
1629 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1630 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1634 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1635 state_to_char[entry->ctx.prev_state] : 'X';
1636 T = entry->ctx.next_state < sizeof(state_to_char) ?
1637 state_to_char[entry->ctx.next_state] : 'X';
1638 if (entry->type == TRACE_WAKE)
1640 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1641 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1642 SEQ_PUT_HEX_FIELD_RET(s, S);
1643 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1644 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1645 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1646 SEQ_PUT_HEX_FIELD_RET(s, T);
1650 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1651 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1652 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1655 SEQ_PUT_FIELD_RET(s, newline);
1660 static int print_bin_fmt(struct trace_iterator *iter)
1662 struct trace_seq *s = &iter->seq;
1663 struct trace_entry *entry;
1667 SEQ_PUT_FIELD_RET(s, entry->pid);
1668 SEQ_PUT_FIELD_RET(s, entry->cpu);
1669 SEQ_PUT_FIELD_RET(s, entry->t);
1671 switch (entry->type) {
1673 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1674 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1677 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1678 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1679 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1680 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1681 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1682 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1686 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1687 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1688 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1694 static int trace_empty(struct trace_iterator *iter)
1696 struct trace_array_cpu *data;
1699 for_each_tracing_cpu(cpu) {
1700 data = iter->tr->data[cpu];
1702 if (head_page(data) && data->trace_idx &&
1703 (data->trace_tail != data->trace_head ||
1704 data->trace_tail_idx != data->trace_head_idx))
1710 static int print_trace_line(struct trace_iterator *iter)
1712 if (iter->trace && iter->trace->print_line)
1713 return iter->trace->print_line(iter);
1715 if (trace_flags & TRACE_ITER_BIN)
1716 return print_bin_fmt(iter);
1718 if (trace_flags & TRACE_ITER_HEX)
1719 return print_hex_fmt(iter);
1721 if (trace_flags & TRACE_ITER_RAW)
1722 return print_raw_fmt(iter);
1724 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1725 return print_lat_fmt(iter, iter->idx, iter->cpu);
1727 return print_trace_fmt(iter);
1730 static int s_show(struct seq_file *m, void *v)
1732 struct trace_iterator *iter = v;
1734 if (iter->ent == NULL) {
1736 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1739 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1740 /* print nothing if the buffers are empty */
1741 if (trace_empty(iter))
1743 print_trace_header(m, iter);
1744 if (!(trace_flags & TRACE_ITER_VERBOSE))
1745 print_lat_help_header(m);
1747 if (!(trace_flags & TRACE_ITER_VERBOSE))
1748 print_func_help_header(m);
1751 print_trace_line(iter);
1752 trace_print_seq(m, &iter->seq);
1758 static struct seq_operations tracer_seq_ops = {
1765 static struct trace_iterator *
1766 __tracing_open(struct inode *inode, struct file *file, int *ret)
1768 struct trace_iterator *iter;
1770 if (tracing_disabled) {
1775 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1781 mutex_lock(&trace_types_lock);
1782 if (current_trace && current_trace->print_max)
1785 iter->tr = inode->i_private;
1786 iter->trace = current_trace;
1789 /* TODO stop tracer */
1790 *ret = seq_open(file, &tracer_seq_ops);
1792 struct seq_file *m = file->private_data;
1795 /* stop the trace while dumping */
1799 if (iter->trace && iter->trace->open)
1800 iter->trace->open(iter);
1805 mutex_unlock(&trace_types_lock);
1811 int tracing_open_generic(struct inode *inode, struct file *filp)
1813 if (tracing_disabled)
1816 filp->private_data = inode->i_private;
1820 int tracing_release(struct inode *inode, struct file *file)
1822 struct seq_file *m = (struct seq_file *)file->private_data;
1823 struct trace_iterator *iter = m->private;
1825 mutex_lock(&trace_types_lock);
1826 if (iter->trace && iter->trace->close)
1827 iter->trace->close(iter);
1829 /* reenable tracing if it was previously enabled */
1832 mutex_unlock(&trace_types_lock);
1834 seq_release(inode, file);
1839 static int tracing_open(struct inode *inode, struct file *file)
1843 __tracing_open(inode, file, &ret);
1848 static int tracing_lt_open(struct inode *inode, struct file *file)
1850 struct trace_iterator *iter;
1853 iter = __tracing_open(inode, file, &ret);
1856 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1863 t_next(struct seq_file *m, void *v, loff_t *pos)
1865 struct tracer *t = m->private;
1877 static void *t_start(struct seq_file *m, loff_t *pos)
1879 struct tracer *t = m->private;
1882 mutex_lock(&trace_types_lock);
1883 for (; t && l < *pos; t = t_next(m, t, &l))
1889 static void t_stop(struct seq_file *m, void *p)
1891 mutex_unlock(&trace_types_lock);
1894 static int t_show(struct seq_file *m, void *v)
1896 struct tracer *t = v;
1901 seq_printf(m, "%s", t->name);
1910 static struct seq_operations show_traces_seq_ops = {
1917 static int show_traces_open(struct inode *inode, struct file *file)
1921 if (tracing_disabled)
1924 ret = seq_open(file, &show_traces_seq_ops);
1926 struct seq_file *m = file->private_data;
1927 m->private = trace_types;
1933 static struct file_operations tracing_fops = {
1934 .open = tracing_open,
1936 .llseek = seq_lseek,
1937 .release = tracing_release,
1940 static struct file_operations tracing_lt_fops = {
1941 .open = tracing_lt_open,
1943 .llseek = seq_lseek,
1944 .release = tracing_release,
1947 static struct file_operations show_traces_fops = {
1948 .open = show_traces_open,
1950 .release = seq_release,
1954 * Only trace on a CPU if the bitmask is set:
1956 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1959 * When tracing/tracing_cpu_mask is modified then this holds
1960 * the new bitmask we are about to install:
1962 static cpumask_t tracing_cpumask_new;
1965 * The tracer itself will not take this lock, but still we want
1966 * to provide a consistent cpumask to user-space:
1968 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1971 * Temporary storage for the character representation of the
1972 * CPU bitmask (and one more byte for the newline):
1974 static char mask_str[NR_CPUS + 1];
1977 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1978 size_t count, loff_t *ppos)
1982 mutex_lock(&tracing_cpumask_update_lock);
1984 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1985 if (count - len < 2) {
1989 len += sprintf(mask_str + len, "\n");
1990 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1993 mutex_unlock(&tracing_cpumask_update_lock);
1999 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2000 size_t count, loff_t *ppos)
2004 mutex_lock(&tracing_cpumask_update_lock);
2005 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2009 raw_local_irq_disable();
2010 __raw_spin_lock(&ftrace_max_lock);
2011 for_each_tracing_cpu(cpu) {
2013 * Increase/decrease the disabled counter if we are
2014 * about to flip a bit in the cpumask:
2016 if (cpu_isset(cpu, tracing_cpumask) &&
2017 !cpu_isset(cpu, tracing_cpumask_new)) {
2018 atomic_inc(&global_trace.data[cpu]->disabled);
2020 if (!cpu_isset(cpu, tracing_cpumask) &&
2021 cpu_isset(cpu, tracing_cpumask_new)) {
2022 atomic_dec(&global_trace.data[cpu]->disabled);
2025 __raw_spin_unlock(&ftrace_max_lock);
2026 raw_local_irq_enable();
2028 tracing_cpumask = tracing_cpumask_new;
2030 mutex_unlock(&tracing_cpumask_update_lock);
2035 mutex_unlock(&tracing_cpumask_update_lock);
2040 static struct file_operations tracing_cpumask_fops = {
2041 .open = tracing_open_generic,
2042 .read = tracing_cpumask_read,
2043 .write = tracing_cpumask_write,
2047 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2048 size_t cnt, loff_t *ppos)
2055 /* calulate max size */
2056 for (i = 0; trace_options[i]; i++) {
2057 len += strlen(trace_options[i]);
2058 len += 3; /* "no" and space */
2061 /* +2 for \n and \0 */
2062 buf = kmalloc(len + 2, GFP_KERNEL);
2066 for (i = 0; trace_options[i]; i++) {
2067 if (trace_flags & (1 << i))
2068 r += sprintf(buf + r, "%s ", trace_options[i]);
2070 r += sprintf(buf + r, "no%s ", trace_options[i]);
2073 r += sprintf(buf + r, "\n");
2074 WARN_ON(r >= len + 2);
2076 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2084 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2085 size_t cnt, loff_t *ppos)
2092 if (cnt >= sizeof(buf))
2095 if (copy_from_user(&buf, ubuf, cnt))
2100 if (strncmp(buf, "no", 2) == 0) {
2105 for (i = 0; trace_options[i]; i++) {
2106 int len = strlen(trace_options[i]);
2108 if (strncmp(cmp, trace_options[i], len) == 0) {
2110 trace_flags &= ~(1 << i);
2112 trace_flags |= (1 << i);
2117 * If no option could be set, return an error:
2119 if (!trace_options[i])
2127 static struct file_operations tracing_iter_fops = {
2128 .open = tracing_open_generic,
2129 .read = tracing_iter_ctrl_read,
2130 .write = tracing_iter_ctrl_write,
2133 static const char readme_msg[] =
2134 "tracing mini-HOWTO:\n\n"
2136 "# mount -t debugfs nodev /debug\n\n"
2137 "# cat /debug/tracing/available_tracers\n"
2138 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2139 "# cat /debug/tracing/current_tracer\n"
2141 "# echo sched_switch > /debug/tracing/current_tracer\n"
2142 "# cat /debug/tracing/current_tracer\n"
2144 "# cat /debug/tracing/iter_ctrl\n"
2145 "noprint-parent nosym-offset nosym-addr noverbose\n"
2146 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2147 "# echo 1 > /debug/tracing/tracing_enabled\n"
2148 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2149 "echo 0 > /debug/tracing/tracing_enabled\n"
2153 tracing_readme_read(struct file *filp, char __user *ubuf,
2154 size_t cnt, loff_t *ppos)
2156 return simple_read_from_buffer(ubuf, cnt, ppos,
2157 readme_msg, strlen(readme_msg));
2160 static struct file_operations tracing_readme_fops = {
2161 .open = tracing_open_generic,
2162 .read = tracing_readme_read,
2166 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2167 size_t cnt, loff_t *ppos)
2169 struct trace_array *tr = filp->private_data;
2173 r = sprintf(buf, "%ld\n", tr->ctrl);
2174 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2178 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2179 size_t cnt, loff_t *ppos)
2181 struct trace_array *tr = filp->private_data;
2186 if (cnt >= sizeof(buf))
2189 if (copy_from_user(&buf, ubuf, cnt))
2194 ret = strict_strtoul(buf, 10, &val);
2200 mutex_lock(&trace_types_lock);
2201 if (tr->ctrl ^ val) {
2209 if (current_trace && current_trace->ctrl_update)
2210 current_trace->ctrl_update(tr);
2212 mutex_unlock(&trace_types_lock);
2220 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2221 size_t cnt, loff_t *ppos)
2223 char buf[max_tracer_type_len+2];
2226 mutex_lock(&trace_types_lock);
2228 r = sprintf(buf, "%s\n", current_trace->name);
2230 r = sprintf(buf, "\n");
2231 mutex_unlock(&trace_types_lock);
2233 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2237 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2238 size_t cnt, loff_t *ppos)
2240 struct trace_array *tr = &global_trace;
2242 char buf[max_tracer_type_len+1];
2245 if (cnt > max_tracer_type_len)
2246 cnt = max_tracer_type_len;
2248 if (copy_from_user(&buf, ubuf, cnt))
2253 /* strip ending whitespace. */
2254 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2257 mutex_lock(&trace_types_lock);
2258 for (t = trace_types; t; t = t->next) {
2259 if (strcmp(t->name, buf) == 0)
2262 if (!t || t == current_trace)
2265 if (current_trace && current_trace->reset)
2266 current_trace->reset(tr);
2273 mutex_unlock(&trace_types_lock);
2281 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2282 size_t cnt, loff_t *ppos)
2284 unsigned long *ptr = filp->private_data;
2288 r = snprintf(buf, sizeof(buf), "%ld\n",
2289 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2290 if (r > sizeof(buf))
2292 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2296 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2297 size_t cnt, loff_t *ppos)
2299 long *ptr = filp->private_data;
2304 if (cnt >= sizeof(buf))
2307 if (copy_from_user(&buf, ubuf, cnt))
2312 ret = strict_strtoul(buf, 10, &val);
2321 static atomic_t tracing_reader;
2323 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2325 struct trace_iterator *iter;
2327 if (tracing_disabled)
2330 /* We only allow for reader of the pipe */
2331 if (atomic_inc_return(&tracing_reader) != 1) {
2332 atomic_dec(&tracing_reader);
2336 /* create a buffer to store the information to pass to userspace */
2337 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2341 mutex_lock(&trace_types_lock);
2342 iter->tr = &global_trace;
2343 iter->trace = current_trace;
2344 filp->private_data = iter;
2346 if (iter->trace->pipe_open)
2347 iter->trace->pipe_open(iter);
2348 mutex_unlock(&trace_types_lock);
2353 static int tracing_release_pipe(struct inode *inode, struct file *file)
2355 struct trace_iterator *iter = file->private_data;
2358 atomic_dec(&tracing_reader);
2364 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2366 struct trace_iterator *iter = filp->private_data;
2368 if (trace_flags & TRACE_ITER_BLOCK) {
2370 * Always select as readable when in blocking mode
2372 return POLLIN | POLLRDNORM;
2374 if (!trace_empty(iter))
2375 return POLLIN | POLLRDNORM;
2376 poll_wait(filp, &trace_wait, poll_table);
2377 if (!trace_empty(iter))
2378 return POLLIN | POLLRDNORM;
2388 tracing_read_pipe(struct file *filp, char __user *ubuf,
2389 size_t cnt, loff_t *ppos)
2391 struct trace_iterator *iter = filp->private_data;
2392 struct trace_array_cpu *data;
2393 static cpumask_t mask;
2394 unsigned long flags;
2395 #ifdef CONFIG_FTRACE
2401 /* return any leftover data */
2402 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2407 trace_seq_reset(&iter->seq);
2409 mutex_lock(&trace_types_lock);
2410 if (iter->trace->read) {
2411 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2416 while (trace_empty(iter)) {
2418 if ((filp->f_flags & O_NONBLOCK)) {
2424 * This is a make-shift waitqueue. The reason we don't use
2425 * an actual wait queue is because:
2426 * 1) we only ever have one waiter
2427 * 2) the tracing, traces all functions, we don't want
2428 * the overhead of calling wake_up and friends
2429 * (and tracing them too)
2430 * Anyway, this is really very primitive wakeup.
2432 set_current_state(TASK_INTERRUPTIBLE);
2433 iter->tr->waiter = current;
2435 mutex_unlock(&trace_types_lock);
2437 /* sleep for 100 msecs, and try again. */
2438 schedule_timeout(HZ/10);
2440 mutex_lock(&trace_types_lock);
2442 iter->tr->waiter = NULL;
2444 if (signal_pending(current)) {
2449 if (iter->trace != current_trace)
2453 * We block until we read something and tracing is disabled.
2454 * We still block if tracing is disabled, but we have never
2455 * read anything. This allows a user to cat this file, and
2456 * then enable tracing. But after we have read something,
2457 * we give an EOF when tracing is again disabled.
2459 * iter->pos will be 0 if we haven't read anything.
2461 if (!tracer_enabled && iter->pos)
2467 /* stop when tracing is finished */
2468 if (trace_empty(iter))
2471 if (cnt >= PAGE_SIZE)
2472 cnt = PAGE_SIZE - 1;
2474 /* reset all but tr, trace, and overruns */
2475 memset(&iter->seq, 0,
2476 sizeof(struct trace_iterator) -
2477 offsetof(struct trace_iterator, seq));
2481 * We need to stop all tracing on all CPUS to read the
2482 * the next buffer. This is a bit expensive, but is
2483 * not done often. We fill all what we can read,
2484 * and then release the locks again.
2488 local_irq_save(flags);
2489 #ifdef CONFIG_FTRACE
2490 ftrace_save = ftrace_enabled;
2494 for_each_tracing_cpu(cpu) {
2495 data = iter->tr->data[cpu];
2497 if (!head_page(data) || !data->trace_idx)
2500 atomic_inc(&data->disabled);
2504 for_each_cpu_mask(cpu, mask) {
2505 data = iter->tr->data[cpu];
2506 __raw_spin_lock(&data->lock);
2508 if (data->overrun > iter->last_overrun[cpu])
2509 iter->overrun[cpu] +=
2510 data->overrun - iter->last_overrun[cpu];
2511 iter->last_overrun[cpu] = data->overrun;
2514 while (find_next_entry_inc(iter) != NULL) {
2516 int len = iter->seq.len;
2518 ret = print_trace_line(iter);
2520 /* don't print partial lines */
2521 iter->seq.len = len;
2525 trace_consume(iter);
2527 if (iter->seq.len >= cnt)
2531 for_each_cpu_mask(cpu, mask) {
2532 data = iter->tr->data[cpu];
2533 __raw_spin_unlock(&data->lock);
2536 for_each_cpu_mask(cpu, mask) {
2537 data = iter->tr->data[cpu];
2538 atomic_dec(&data->disabled);
2540 #ifdef CONFIG_FTRACE
2541 ftrace_enabled = ftrace_save;
2543 local_irq_restore(flags);
2545 /* Now copy what we have to the user */
2546 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2547 if (iter->seq.readpos >= iter->seq.len)
2548 trace_seq_reset(&iter->seq);
2553 mutex_unlock(&trace_types_lock);
2559 tracing_entries_read(struct file *filp, char __user *ubuf,
2560 size_t cnt, loff_t *ppos)
2562 struct trace_array *tr = filp->private_data;
2566 r = sprintf(buf, "%lu\n", tr->entries);
2567 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2571 tracing_entries_write(struct file *filp, const char __user *ubuf,
2572 size_t cnt, loff_t *ppos)
2578 if (cnt >= sizeof(buf))
2581 if (copy_from_user(&buf, ubuf, cnt))
2586 ret = strict_strtoul(buf, 10, &val);
2590 /* must have at least 1 entry */
2594 mutex_lock(&trace_types_lock);
2596 if (current_trace != &no_tracer) {
2598 pr_info("ftrace: set current_tracer to none"
2599 " before modifying buffer size\n");
2603 if (val > global_trace.entries) {
2604 long pages_requested;
2605 unsigned long freeable_pages;
2607 /* make sure we have enough memory before mapping */
2609 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2611 /* account for each buffer (and max_tr) */
2612 pages_requested *= tracing_nr_buffers * 2;
2614 /* Check for overflow */
2615 if (pages_requested < 0) {
2620 freeable_pages = determine_dirtyable_memory();
2622 /* we only allow to request 1/4 of useable memory */
2623 if (pages_requested >
2624 ((freeable_pages + tracing_pages_allocated) / 4)) {
2629 while (global_trace.entries < val) {
2630 if (trace_alloc_page()) {
2634 /* double check that we don't go over the known pages */
2635 if (tracing_pages_allocated > pages_requested)
2640 /* include the number of entries in val (inc of page entries) */
2641 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2645 /* check integrity */
2646 for_each_tracing_cpu(i)
2647 check_pages(global_trace.data[i]);
2651 /* If check pages failed, return ENOMEM */
2652 if (tracing_disabled)
2655 max_tr.entries = global_trace.entries;
2656 mutex_unlock(&trace_types_lock);
2661 static struct file_operations tracing_max_lat_fops = {
2662 .open = tracing_open_generic,
2663 .read = tracing_max_lat_read,
2664 .write = tracing_max_lat_write,
2667 static struct file_operations tracing_ctrl_fops = {
2668 .open = tracing_open_generic,
2669 .read = tracing_ctrl_read,
2670 .write = tracing_ctrl_write,
2673 static struct file_operations set_tracer_fops = {
2674 .open = tracing_open_generic,
2675 .read = tracing_set_trace_read,
2676 .write = tracing_set_trace_write,
2679 static struct file_operations tracing_pipe_fops = {
2680 .open = tracing_open_pipe,
2681 .poll = tracing_poll_pipe,
2682 .read = tracing_read_pipe,
2683 .release = tracing_release_pipe,
2686 static struct file_operations tracing_entries_fops = {
2687 .open = tracing_open_generic,
2688 .read = tracing_entries_read,
2689 .write = tracing_entries_write,
2692 #ifdef CONFIG_DYNAMIC_FTRACE
2695 tracing_read_long(struct file *filp, char __user *ubuf,
2696 size_t cnt, loff_t *ppos)
2698 unsigned long *p = filp->private_data;
2702 r = sprintf(buf, "%ld\n", *p);
2704 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2707 static struct file_operations tracing_read_long_fops = {
2708 .open = tracing_open_generic,
2709 .read = tracing_read_long,
2713 static struct dentry *d_tracer;
2715 struct dentry *tracing_init_dentry(void)
2722 d_tracer = debugfs_create_dir("tracing", NULL);
2724 if (!d_tracer && !once) {
2726 pr_warning("Could not create debugfs directory 'tracing'\n");
2733 #ifdef CONFIG_FTRACE_SELFTEST
2734 /* Let selftest have access to static functions in this file */
2735 #include "trace_selftest.c"
2738 static __init void tracer_init_debugfs(void)
2740 struct dentry *d_tracer;
2741 struct dentry *entry;
2743 d_tracer = tracing_init_dentry();
2745 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2746 &global_trace, &tracing_ctrl_fops);
2748 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2750 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2751 NULL, &tracing_iter_fops);
2753 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2755 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2756 NULL, &tracing_cpumask_fops);
2758 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2760 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2761 &global_trace, &tracing_lt_fops);
2763 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2765 entry = debugfs_create_file("trace", 0444, d_tracer,
2766 &global_trace, &tracing_fops);
2768 pr_warning("Could not create debugfs 'trace' entry\n");
2770 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2771 &global_trace, &show_traces_fops);
2773 pr_warning("Could not create debugfs 'trace' entry\n");
2775 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2776 &global_trace, &set_tracer_fops);
2778 pr_warning("Could not create debugfs 'trace' entry\n");
2780 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2781 &tracing_max_latency,
2782 &tracing_max_lat_fops);
2784 pr_warning("Could not create debugfs "
2785 "'tracing_max_latency' entry\n");
2787 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2788 &tracing_thresh, &tracing_max_lat_fops);
2790 pr_warning("Could not create debugfs "
2791 "'tracing_threash' entry\n");
2792 entry = debugfs_create_file("README", 0644, d_tracer,
2793 NULL, &tracing_readme_fops);
2795 pr_warning("Could not create debugfs 'README' entry\n");
2797 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2798 NULL, &tracing_pipe_fops);
2800 pr_warning("Could not create debugfs "
2801 "'tracing_threash' entry\n");
2803 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2804 &global_trace, &tracing_entries_fops);
2806 pr_warning("Could not create debugfs "
2807 "'tracing_threash' entry\n");
2809 #ifdef CONFIG_DYNAMIC_FTRACE
2810 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2811 &ftrace_update_tot_cnt,
2812 &tracing_read_long_fops);
2814 pr_warning("Could not create debugfs "
2815 "'dyn_ftrace_total_info' entry\n");
2819 static int trace_alloc_page(void)
2821 struct trace_array_cpu *data;
2822 struct page *page, *tmp;
2825 unsigned pages_allocated = 0;
2828 /* first allocate a page for each CPU */
2829 for_each_tracing_cpu(i) {
2830 array = (void *)__get_free_page(GFP_KERNEL);
2831 if (array == NULL) {
2832 printk(KERN_ERR "tracer: failed to allocate page"
2833 "for trace buffer!\n");
2838 page = virt_to_page(array);
2839 list_add(&page->lru, &pages);
2841 /* Only allocate if we are actually using the max trace */
2842 #ifdef CONFIG_TRACER_MAX_TRACE
2843 array = (void *)__get_free_page(GFP_KERNEL);
2844 if (array == NULL) {
2845 printk(KERN_ERR "tracer: failed to allocate page"
2846 "for trace buffer!\n");
2850 page = virt_to_page(array);
2851 list_add(&page->lru, &pages);
2855 /* Now that we successfully allocate a page per CPU, add them */
2856 for_each_tracing_cpu(i) {
2857 data = global_trace.data[i];
2858 page = list_entry(pages.next, struct page, lru);
2859 list_del_init(&page->lru);
2860 list_add_tail(&page->lru, &data->trace_pages);
2863 #ifdef CONFIG_TRACER_MAX_TRACE
2864 data = max_tr.data[i];
2865 page = list_entry(pages.next, struct page, lru);
2866 list_del_init(&page->lru);
2867 list_add_tail(&page->lru, &data->trace_pages);
2871 tracing_pages_allocated += pages_allocated;
2872 global_trace.entries += ENTRIES_PER_PAGE;
2877 list_for_each_entry_safe(page, tmp, &pages, lru) {
2878 list_del_init(&page->lru);
2884 static int trace_free_page(void)
2886 struct trace_array_cpu *data;
2888 struct list_head *p;
2892 /* free one page from each buffer */
2893 for_each_tracing_cpu(i) {
2894 data = global_trace.data[i];
2895 p = data->trace_pages.next;
2896 if (p == &data->trace_pages) {
2897 /* should never happen */
2899 tracing_disabled = 1;
2903 page = list_entry(p, struct page, lru);
2905 list_del(&page->lru);
2906 tracing_pages_allocated--;
2907 tracing_pages_allocated--;
2910 tracing_reset(data);
2912 #ifdef CONFIG_TRACER_MAX_TRACE
2913 data = max_tr.data[i];
2914 p = data->trace_pages.next;
2915 if (p == &data->trace_pages) {
2916 /* should never happen */
2918 tracing_disabled = 1;
2922 page = list_entry(p, struct page, lru);
2924 list_del(&page->lru);
2927 tracing_reset(data);
2930 global_trace.entries -= ENTRIES_PER_PAGE;
2935 __init static int tracer_alloc_buffers(void)
2937 struct trace_array_cpu *data;
2944 global_trace.ctrl = tracer_enabled;
2946 /* TODO: make the number of buffers hot pluggable with CPUS */
2947 tracing_nr_buffers = num_possible_cpus();
2948 tracing_buffer_mask = cpu_possible_map;
2950 /* Allocate the first page for all buffers */
2951 for_each_tracing_cpu(i) {
2952 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2953 max_tr.data[i] = &per_cpu(max_data, i);
2955 array = (void *)__get_free_page(GFP_KERNEL);
2956 if (array == NULL) {
2957 printk(KERN_ERR "tracer: failed to allocate page"
2958 "for trace buffer!\n");
2962 /* set the array to the list */
2963 INIT_LIST_HEAD(&data->trace_pages);
2964 page = virt_to_page(array);
2965 list_add(&page->lru, &data->trace_pages);
2966 /* use the LRU flag to differentiate the two buffers */
2969 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2970 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2972 /* Only allocate if we are actually using the max trace */
2973 #ifdef CONFIG_TRACER_MAX_TRACE
2974 array = (void *)__get_free_page(GFP_KERNEL);
2975 if (array == NULL) {
2976 printk(KERN_ERR "tracer: failed to allocate page"
2977 "for trace buffer!\n");
2981 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2982 page = virt_to_page(array);
2983 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2989 * Since we allocate by orders of pages, we may be able to
2992 global_trace.entries = ENTRIES_PER_PAGE;
2995 while (global_trace.entries < trace_nr_entries) {
2996 if (trace_alloc_page())
3000 max_tr.entries = global_trace.entries;
3002 pr_info("tracer: %d pages allocated for %ld",
3003 pages, trace_nr_entries);
3004 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
3005 pr_info(" actual entries %ld\n", global_trace.entries);
3007 tracer_init_debugfs();
3009 trace_init_cmdlines();
3011 register_tracer(&no_tracer);
3012 current_trace = &no_tracer;
3014 /* All seems OK, enable tracing */
3015 tracing_disabled = 0;
3020 for (i-- ; i >= 0; i--) {
3021 struct page *page, *tmp;
3022 struct trace_array_cpu *data = global_trace.data[i];
3025 list_for_each_entry_safe(page, tmp,
3026 &data->trace_pages, lru) {
3027 list_del_init(&page->lru);
3032 #ifdef CONFIG_TRACER_MAX_TRACE
3033 data = max_tr.data[i];
3035 list_for_each_entry_safe(page, tmp,
3036 &data->trace_pages, lru) {
3037 list_del_init(&page->lru);
3045 fs_initcall(tracer_alloc_buffers);