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/kprobes.h>
31 #include <linux/writeback.h>
33 #include <linux/stacktrace.h>
37 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
38 unsigned long __read_mostly tracing_thresh;
40 static unsigned long __read_mostly tracing_nr_buffers;
41 static cpumask_t __read_mostly tracing_buffer_mask;
43 #define for_each_tracing_cpu(cpu) \
44 for_each_cpu_mask(cpu, tracing_buffer_mask)
46 static int trace_alloc_page(void);
47 static int trace_free_page(void);
49 static int tracing_disabled = 1;
51 static unsigned long tracing_pages_allocated;
54 ns2usecs(cycle_t nsec)
61 cycle_t ftrace_now(int cpu)
63 return cpu_clock(cpu);
67 * The global_trace is the descriptor that holds the tracing
68 * buffers for the live tracing. For each CPU, it contains
69 * a link list of pages that will store trace entries. The
70 * page descriptor of the pages in the memory is used to hold
71 * the link list by linking the lru item in the page descriptor
72 * to each of the pages in the buffer per CPU.
74 * For each active CPU there is a data field that holds the
75 * pages for the buffer for that CPU. Each CPU has the same number
76 * of pages allocated for its buffer.
78 static struct trace_array global_trace;
80 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
83 * The max_tr is used to snapshot the global_trace when a maximum
84 * latency is reached. Some tracers will use this to store a maximum
85 * trace while it continues examining live traces.
87 * The buffers for the max_tr are set up the same as the global_trace.
88 * When a snapshot is taken, the link list of the max_tr is swapped
89 * with the link list of the global_trace and the buffers are reset for
90 * the global_trace so the tracing can continue.
92 static struct trace_array max_tr;
94 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
96 /* tracer_enabled is used to toggle activation of a tracer */
97 static int tracer_enabled = 1;
100 * trace_nr_entries is the number of entries that is allocated
101 * for a buffer. Note, the number of entries is always rounded
102 * to ENTRIES_PER_PAGE.
104 static unsigned long trace_nr_entries = 65536UL;
106 /* trace_types holds a link list of available tracers. */
107 static struct tracer *trace_types __read_mostly;
109 /* current_trace points to the tracer that is currently active */
110 static struct tracer *current_trace __read_mostly;
113 * max_tracer_type_len is used to simplify the allocating of
114 * buffers to read userspace tracer names. We keep track of
115 * the longest tracer name registered.
117 static int max_tracer_type_len;
120 * trace_types_lock is used to protect the trace_types list.
121 * This lock is also used to keep user access serialized.
122 * Accesses from userspace will grab this lock while userspace
123 * activities happen inside the kernel.
125 static DEFINE_MUTEX(trace_types_lock);
127 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
128 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
130 /* trace_flags holds iter_ctrl options */
131 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
133 static notrace void no_trace_init(struct trace_array *tr)
138 for_each_online_cpu(cpu)
139 tracing_reset(tr->data[cpu]);
143 /* dummy trace to disable tracing */
144 static struct tracer no_tracer __read_mostly = {
146 .init = no_trace_init
151 * trace_wake_up - wake up tasks waiting for trace input
153 * Simply wakes up any task that is blocked on the trace_wait
154 * queue. These is used with trace_poll for tasks polling the trace.
156 void trace_wake_up(void)
159 * The runqueue_is_locked() can fail, but this is the best we
162 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
163 wake_up(&trace_wait);
166 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
168 static int __init set_nr_entries(char *str)
170 unsigned long nr_entries;
175 ret = strict_strtoul(str, 0, &nr_entries);
176 /* nr_entries can not be zero */
177 if (ret < 0 || nr_entries == 0)
179 trace_nr_entries = nr_entries;
182 __setup("trace_entries=", set_nr_entries);
184 unsigned long nsecs_to_usecs(unsigned long nsecs)
190 * trace_flag_type is an enumeration that holds different
191 * states when a trace occurs. These are:
192 * IRQS_OFF - interrupts were disabled
193 * NEED_RESCED - reschedule is requested
194 * HARDIRQ - inside an interrupt handler
195 * SOFTIRQ - inside a softirq handler
197 enum trace_flag_type {
198 TRACE_FLAG_IRQS_OFF = 0x01,
199 TRACE_FLAG_NEED_RESCHED = 0x02,
200 TRACE_FLAG_HARDIRQ = 0x04,
201 TRACE_FLAG_SOFTIRQ = 0x08,
205 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
206 * control the output of kernel symbols.
208 #define TRACE_ITER_SYM_MASK \
209 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
211 /* These must match the bit postions in trace_iterator_flags */
212 static const char *trace_options[] = {
227 * ftrace_max_lock is used to protect the swapping of buffers
228 * when taking a max snapshot. The buffers themselves are
229 * protected by per_cpu spinlocks. But the action of the swap
230 * needs its own lock.
232 * This is defined as a raw_spinlock_t in order to help
233 * with performance when lockdep debugging is enabled.
235 static raw_spinlock_t ftrace_max_lock =
236 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
239 * Copy the new maximum trace into the separate maximum-trace
240 * structure. (this way the maximum trace is permanently saved,
241 * for later retrieval via /debugfs/tracing/latency_trace)
244 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
246 struct trace_array_cpu *data = tr->data[cpu];
249 max_tr.time_start = data->preempt_timestamp;
251 data = max_tr.data[cpu];
252 data->saved_latency = tracing_max_latency;
254 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
255 data->pid = tsk->pid;
256 data->uid = tsk->uid;
257 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
258 data->policy = tsk->policy;
259 data->rt_priority = tsk->rt_priority;
261 /* record this tasks comm */
262 tracing_record_cmdline(current);
265 #define CHECK_COND(cond) \
266 if (unlikely(cond)) { \
267 tracing_disabled = 1; \
273 * check_pages - integrity check of trace buffers
275 * As a safty measure we check to make sure the data pages have not
278 int check_pages(struct trace_array_cpu *data)
280 struct page *page, *tmp;
282 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
283 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
285 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
286 CHECK_COND(page->lru.next->prev != &page->lru);
287 CHECK_COND(page->lru.prev->next != &page->lru);
294 * head_page - page address of the first page in per_cpu buffer.
296 * head_page returns the page address of the first page in
297 * a per_cpu buffer. This also preforms various consistency
298 * checks to make sure the buffer has not been corrupted.
300 void *head_page(struct trace_array_cpu *data)
304 if (list_empty(&data->trace_pages))
307 page = list_entry(data->trace_pages.next, struct page, lru);
308 BUG_ON(&page->lru == &data->trace_pages);
310 return page_address(page);
314 * trace_seq_printf - sequence printing of trace information
315 * @s: trace sequence descriptor
316 * @fmt: printf format string
318 * The tracer may use either sequence operations or its own
319 * copy to user routines. To simplify formating of a trace
320 * trace_seq_printf is used to store strings into a special
321 * buffer (@s). Then the output may be either used by
322 * the sequencer or pulled into another buffer.
325 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
327 int len = (PAGE_SIZE - 1) - s->len;
335 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
338 /* If we can't write it all, don't bother writing anything */
348 * trace_seq_puts - trace sequence printing of simple string
349 * @s: trace sequence descriptor
350 * @str: simple string to record
352 * The tracer may use either the sequence operations or its own
353 * copy to user routines. This function records a simple string
354 * into a special buffer (@s) for later retrieval by a sequencer
355 * or other mechanism.
358 trace_seq_puts(struct trace_seq *s, const char *str)
360 int len = strlen(str);
362 if (len > ((PAGE_SIZE - 1) - s->len))
365 memcpy(s->buffer + s->len, str, len);
372 trace_seq_putc(struct trace_seq *s, unsigned char c)
374 if (s->len >= (PAGE_SIZE - 1))
377 s->buffer[s->len++] = c;
383 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
385 if (len > ((PAGE_SIZE - 1) - s->len))
388 memcpy(s->buffer + s->len, mem, len);
395 static const char hex2asc[] = "0123456789abcdef";
398 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
400 unsigned char hex[HEX_CHARS];
401 unsigned char *data = mem;
405 BUG_ON(len >= HEX_CHARS);
408 for (i = 0, j = 0; i < len; i++) {
410 for (i = len-1, j = 0; i >= 0; i--) {
414 hex[j++] = hex2asc[byte & 0x0f];
415 hex[j++] = hex2asc[byte >> 4];
419 return trace_seq_putmem(s, hex, j);
423 trace_seq_reset(struct trace_seq *s)
429 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
434 if (s->len <= s->readpos)
437 len = s->len - s->readpos;
440 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
449 trace_print_seq(struct seq_file *m, struct trace_seq *s)
451 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
454 seq_puts(m, s->buffer);
460 * flip the trace buffers between two trace descriptors.
461 * This usually is the buffers between the global_trace and
462 * the max_tr to record a snapshot of a current trace.
464 * The ftrace_max_lock must be held.
467 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
469 struct list_head flip_pages;
471 INIT_LIST_HEAD(&flip_pages);
473 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
474 sizeof(struct trace_array_cpu) -
475 offsetof(struct trace_array_cpu, trace_head_idx));
479 list_splice_init(&tr1->trace_pages, &flip_pages);
480 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
481 list_splice_init(&flip_pages, &tr2->trace_pages);
482 BUG_ON(!list_empty(&flip_pages));
488 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
490 * @tsk: the task with the latency
491 * @cpu: The cpu that initiated the trace.
493 * Flip the buffers between the @tr and the max_tr and record information
494 * about which task was the cause of this latency.
497 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
499 struct trace_array_cpu *data;
502 WARN_ON_ONCE(!irqs_disabled());
503 __raw_spin_lock(&ftrace_max_lock);
504 /* clear out all the previous traces */
505 for_each_tracing_cpu(i) {
507 flip_trace(max_tr.data[i], data);
511 __update_max_tr(tr, tsk, cpu);
512 __raw_spin_unlock(&ftrace_max_lock);
516 * update_max_tr_single - only copy one trace over, and reset the rest
518 * @tsk - task with the latency
519 * @cpu - the cpu of the buffer to copy.
521 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
524 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
526 struct trace_array_cpu *data = tr->data[cpu];
529 WARN_ON_ONCE(!irqs_disabled());
530 __raw_spin_lock(&ftrace_max_lock);
531 for_each_tracing_cpu(i)
532 tracing_reset(max_tr.data[i]);
534 flip_trace(max_tr.data[cpu], data);
537 __update_max_tr(tr, tsk, cpu);
538 __raw_spin_unlock(&ftrace_max_lock);
542 * register_tracer - register a tracer with the ftrace system.
543 * @type - the plugin for the tracer
545 * Register a new plugin tracer.
547 int register_tracer(struct tracer *type)
554 pr_info("Tracer must have a name\n");
558 mutex_lock(&trace_types_lock);
559 for (t = trace_types; t; t = t->next) {
560 if (strcmp(type->name, t->name) == 0) {
562 pr_info("Trace %s already registered\n",
569 #ifdef CONFIG_FTRACE_STARTUP_TEST
570 if (type->selftest) {
571 struct tracer *saved_tracer = current_trace;
572 struct trace_array_cpu *data;
573 struct trace_array *tr = &global_trace;
574 int saved_ctrl = tr->ctrl;
577 * Run a selftest on this tracer.
578 * Here we reset the trace buffer, and set the current
579 * tracer to be this tracer. The tracer can then run some
580 * internal tracing to verify that everything is in order.
581 * If we fail, we do not register this tracer.
583 for_each_tracing_cpu(i) {
585 if (!head_page(data))
589 current_trace = type;
591 /* the test is responsible for initializing and enabling */
592 pr_info("Testing tracer %s: ", type->name);
593 ret = type->selftest(type, tr);
594 /* the test is responsible for resetting too */
595 current_trace = saved_tracer;
596 tr->ctrl = saved_ctrl;
598 printk(KERN_CONT "FAILED!\n");
601 /* Only reset on passing, to avoid touching corrupted buffers */
602 for_each_tracing_cpu(i) {
604 if (!head_page(data))
608 printk(KERN_CONT "PASSED\n");
612 type->next = trace_types;
614 len = strlen(type->name);
615 if (len > max_tracer_type_len)
616 max_tracer_type_len = len;
619 mutex_unlock(&trace_types_lock);
624 void unregister_tracer(struct tracer *type)
629 mutex_lock(&trace_types_lock);
630 for (t = &trace_types; *t; t = &(*t)->next) {
634 pr_info("Trace %s not registered\n", type->name);
639 if (strlen(type->name) != max_tracer_type_len)
642 max_tracer_type_len = 0;
643 for (t = &trace_types; *t; t = &(*t)->next) {
644 len = strlen((*t)->name);
645 if (len > max_tracer_type_len)
646 max_tracer_type_len = len;
649 mutex_unlock(&trace_types_lock);
652 void tracing_reset(struct trace_array_cpu *data)
656 data->trace_head = data->trace_tail = head_page(data);
657 data->trace_head_idx = 0;
658 data->trace_tail_idx = 0;
661 #define SAVED_CMDLINES 128
662 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
663 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
664 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
665 static int cmdline_idx;
666 static DEFINE_SPINLOCK(trace_cmdline_lock);
668 /* temporary disable recording */
669 atomic_t trace_record_cmdline_disabled __read_mostly;
671 static void trace_init_cmdlines(void)
673 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
674 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
678 void trace_stop_cmdline_recording(void);
680 static void trace_save_cmdline(struct task_struct *tsk)
685 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
689 * It's not the end of the world if we don't get
690 * the lock, but we also don't want to spin
691 * nor do we want to disable interrupts,
692 * so if we miss here, then better luck next time.
694 if (!spin_trylock(&trace_cmdline_lock))
697 idx = map_pid_to_cmdline[tsk->pid];
698 if (idx >= SAVED_CMDLINES) {
699 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
701 map = map_cmdline_to_pid[idx];
702 if (map <= PID_MAX_DEFAULT)
703 map_pid_to_cmdline[map] = (unsigned)-1;
705 map_pid_to_cmdline[tsk->pid] = idx;
710 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
712 spin_unlock(&trace_cmdline_lock);
715 static char *trace_find_cmdline(int pid)
717 char *cmdline = "<...>";
723 if (pid > PID_MAX_DEFAULT)
726 map = map_pid_to_cmdline[pid];
727 if (map >= SAVED_CMDLINES)
730 cmdline = saved_cmdlines[map];
736 void tracing_record_cmdline(struct task_struct *tsk)
738 if (atomic_read(&trace_record_cmdline_disabled))
741 trace_save_cmdline(tsk);
744 static inline struct list_head *
745 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
748 * Roundrobin - but skip the head (which is not a real page):
751 if (unlikely(next == &data->trace_pages))
753 BUG_ON(next == &data->trace_pages);
759 trace_next_page(struct trace_array_cpu *data, void *addr)
761 struct list_head *next;
764 page = virt_to_page(addr);
766 next = trace_next_list(data, &page->lru);
767 page = list_entry(next, struct page, lru);
769 return page_address(page);
772 static inline struct trace_entry *
773 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
775 unsigned long idx, idx_next;
776 struct trace_entry *entry;
779 idx = data->trace_head_idx;
782 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
784 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
786 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
787 data->trace_head = trace_next_page(data, data->trace_head);
791 if (data->trace_head == data->trace_tail &&
792 idx_next == data->trace_tail_idx) {
795 data->trace_tail_idx++;
796 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
798 trace_next_page(data, data->trace_tail);
799 data->trace_tail_idx = 0;
803 data->trace_head_idx = idx_next;
809 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
811 struct task_struct *tsk = current;
814 pc = preempt_count();
816 entry->preempt_count = pc & 0xff;
817 entry->pid = (tsk) ? tsk->pid : 0;
818 entry->t = ftrace_now(raw_smp_processor_id());
819 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
820 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
821 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
822 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
826 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags)
829 struct trace_entry *entry;
830 unsigned long irq_flags;
832 raw_local_irq_save(irq_flags);
833 __raw_spin_lock(&data->lock);
834 entry = tracing_get_trace_entry(tr, data);
835 tracing_generic_entry_update(entry, flags);
836 entry->type = TRACE_FN;
838 entry->fn.parent_ip = parent_ip;
839 __raw_spin_unlock(&data->lock);
840 raw_local_irq_restore(irq_flags);
844 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
845 unsigned long ip, unsigned long parent_ip, unsigned long flags)
847 if (likely(!atomic_read(&data->disabled)))
848 trace_function(tr, data, ip, parent_ip, flags);
851 void __trace_stack(struct trace_array *tr,
852 struct trace_array_cpu *data,
856 struct trace_entry *entry;
857 struct stack_trace trace;
859 if (!(trace_flags & TRACE_ITER_STACKTRACE))
862 entry = tracing_get_trace_entry(tr, data);
863 tracing_generic_entry_update(entry, flags);
864 entry->type = TRACE_STACK;
866 memset(&entry->stack, 0, sizeof(entry->stack));
868 trace.nr_entries = 0;
869 trace.max_entries = FTRACE_STACK_ENTRIES;
871 trace.entries = entry->stack.caller;
873 save_stack_trace(&trace);
877 __trace_special(void *__tr, void *__data,
878 unsigned long arg1, unsigned long arg2, unsigned long arg3)
880 struct trace_array_cpu *data = __data;
881 struct trace_array *tr = __tr;
882 struct trace_entry *entry;
883 unsigned long irq_flags;
885 raw_local_irq_save(irq_flags);
886 __raw_spin_lock(&data->lock);
887 entry = tracing_get_trace_entry(tr, data);
888 tracing_generic_entry_update(entry, 0);
889 entry->type = TRACE_SPECIAL;
890 entry->special.arg1 = arg1;
891 entry->special.arg2 = arg2;
892 entry->special.arg3 = arg3;
893 __trace_stack(tr, data, irq_flags, 4);
894 __raw_spin_unlock(&data->lock);
895 raw_local_irq_restore(irq_flags);
901 tracing_sched_switch_trace(struct trace_array *tr,
902 struct trace_array_cpu *data,
903 struct task_struct *prev,
904 struct task_struct *next,
907 struct trace_entry *entry;
908 unsigned long irq_flags;
910 raw_local_irq_save(irq_flags);
911 __raw_spin_lock(&data->lock);
912 entry = tracing_get_trace_entry(tr, data);
913 tracing_generic_entry_update(entry, flags);
914 entry->type = TRACE_CTX;
915 entry->ctx.prev_pid = prev->pid;
916 entry->ctx.prev_prio = prev->prio;
917 entry->ctx.prev_state = prev->state;
918 entry->ctx.next_pid = next->pid;
919 entry->ctx.next_prio = next->prio;
920 entry->ctx.next_state = next->state;
921 __trace_stack(tr, data, flags, 5);
922 __raw_spin_unlock(&data->lock);
923 raw_local_irq_restore(irq_flags);
927 tracing_sched_wakeup_trace(struct trace_array *tr,
928 struct trace_array_cpu *data,
929 struct task_struct *wakee,
930 struct task_struct *curr,
933 struct trace_entry *entry;
934 unsigned long irq_flags;
936 raw_local_irq_save(irq_flags);
937 __raw_spin_lock(&data->lock);
938 entry = tracing_get_trace_entry(tr, data);
939 tracing_generic_entry_update(entry, flags);
940 entry->type = TRACE_WAKE;
941 entry->ctx.prev_pid = curr->pid;
942 entry->ctx.prev_prio = curr->prio;
943 entry->ctx.prev_state = curr->state;
944 entry->ctx.next_pid = wakee->pid;
945 entry->ctx.next_prio = wakee->prio;
946 entry->ctx.next_state = wakee->state;
947 __trace_stack(tr, data, flags, 6);
948 __raw_spin_unlock(&data->lock);
949 raw_local_irq_restore(irq_flags);
955 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
957 struct trace_array *tr = &global_trace;
958 struct trace_array_cpu *data;
963 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
966 local_irq_save(flags);
967 cpu = raw_smp_processor_id();
968 data = tr->data[cpu];
969 disabled = atomic_inc_return(&data->disabled);
971 if (likely(disabled == 1))
972 __trace_special(tr, data, arg1, arg2, arg3);
974 atomic_dec(&data->disabled);
975 local_irq_restore(flags);
980 function_trace_call(unsigned long ip, unsigned long parent_ip)
982 struct trace_array *tr = &global_trace;
983 struct trace_array_cpu *data;
988 if (unlikely(!tracer_enabled))
991 local_irq_save(flags);
992 cpu = raw_smp_processor_id();
993 data = tr->data[cpu];
994 disabled = atomic_inc_return(&data->disabled);
996 if (likely(disabled == 1))
997 trace_function(tr, data, ip, parent_ip, flags);
999 atomic_dec(&data->disabled);
1000 local_irq_restore(flags);
1003 static struct ftrace_ops trace_ops __read_mostly =
1005 .func = function_trace_call,
1008 void tracing_start_function_trace(void)
1010 register_ftrace_function(&trace_ops);
1013 void tracing_stop_function_trace(void)
1015 unregister_ftrace_function(&trace_ops);
1019 enum trace_file_type {
1020 TRACE_FILE_LAT_FMT = 1,
1023 static struct trace_entry *
1024 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1025 struct trace_iterator *iter, int cpu)
1028 struct trace_entry *array;
1030 if (iter->next_idx[cpu] >= tr->entries ||
1031 iter->next_idx[cpu] >= data->trace_idx ||
1032 (data->trace_head == data->trace_tail &&
1033 data->trace_head_idx == data->trace_tail_idx))
1036 if (!iter->next_page[cpu]) {
1037 /* Initialize the iterator for this cpu trace buffer */
1038 WARN_ON(!data->trace_tail);
1039 page = virt_to_page(data->trace_tail);
1040 iter->next_page[cpu] = &page->lru;
1041 iter->next_page_idx[cpu] = data->trace_tail_idx;
1044 page = list_entry(iter->next_page[cpu], struct page, lru);
1045 BUG_ON(&data->trace_pages == &page->lru);
1047 array = page_address(page);
1049 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1050 return &array[iter->next_page_idx[cpu]];
1053 static struct trace_entry *
1054 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1056 struct trace_array *tr = iter->tr;
1057 struct trace_entry *ent, *next = NULL;
1061 for_each_tracing_cpu(cpu) {
1062 if (!head_page(tr->data[cpu]))
1064 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1066 * Pick the entry with the smallest timestamp:
1068 if (ent && (!next || ent->t < next->t)) {
1075 *ent_cpu = next_cpu;
1080 static void trace_iterator_increment(struct trace_iterator *iter)
1083 iter->next_idx[iter->cpu]++;
1084 iter->next_page_idx[iter->cpu]++;
1086 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1087 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1089 iter->next_page_idx[iter->cpu] = 0;
1090 iter->next_page[iter->cpu] =
1091 trace_next_list(data, iter->next_page[iter->cpu]);
1095 static void trace_consume(struct trace_iterator *iter)
1097 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1099 data->trace_tail_idx++;
1100 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1101 data->trace_tail = trace_next_page(data, data->trace_tail);
1102 data->trace_tail_idx = 0;
1105 /* Check if we empty it, then reset the index */
1106 if (data->trace_head == data->trace_tail &&
1107 data->trace_head_idx == data->trace_tail_idx)
1108 data->trace_idx = 0;
1111 static void *find_next_entry_inc(struct trace_iterator *iter)
1113 struct trace_entry *next;
1116 next = find_next_entry(iter, &next_cpu);
1118 iter->prev_ent = iter->ent;
1119 iter->prev_cpu = iter->cpu;
1122 iter->cpu = next_cpu;
1125 trace_iterator_increment(iter);
1127 return next ? iter : NULL;
1130 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1132 struct trace_iterator *iter = m->private;
1133 void *last_ent = iter->ent;
1139 /* can't go backwards */
1144 ent = find_next_entry_inc(iter);
1148 while (ent && iter->idx < i)
1149 ent = find_next_entry_inc(iter);
1153 if (last_ent && !ent)
1154 seq_puts(m, "\n\nvim:ft=help\n");
1159 static void *s_start(struct seq_file *m, loff_t *pos)
1161 struct trace_iterator *iter = m->private;
1166 mutex_lock(&trace_types_lock);
1168 if (!current_trace || current_trace != iter->trace) {
1169 mutex_unlock(&trace_types_lock);
1173 atomic_inc(&trace_record_cmdline_disabled);
1175 /* let the tracer grab locks here if needed */
1176 if (current_trace->start)
1177 current_trace->start(iter);
1179 if (*pos != iter->pos) {
1183 iter->prev_ent = NULL;
1184 iter->prev_cpu = -1;
1186 for_each_tracing_cpu(i) {
1187 iter->next_idx[i] = 0;
1188 iter->next_page[i] = NULL;
1191 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1196 p = s_next(m, p, &l);
1202 static void s_stop(struct seq_file *m, void *p)
1204 struct trace_iterator *iter = m->private;
1206 atomic_dec(&trace_record_cmdline_disabled);
1208 /* let the tracer release locks here if needed */
1209 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1210 iter->trace->stop(iter);
1212 mutex_unlock(&trace_types_lock);
1215 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1217 #ifdef CONFIG_KRETPROBES
1218 static inline int kretprobed(unsigned long addr)
1220 return addr == (unsigned long)kretprobe_trampoline;
1223 static inline int kretprobed(unsigned long addr)
1227 #endif /* CONFIG_KRETPROBES */
1230 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1232 #ifdef CONFIG_KALLSYMS
1233 char str[KSYM_SYMBOL_LEN];
1235 kallsyms_lookup(address, NULL, NULL, NULL, str);
1237 return trace_seq_printf(s, fmt, str);
1243 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1244 unsigned long address)
1246 #ifdef CONFIG_KALLSYMS
1247 char str[KSYM_SYMBOL_LEN];
1249 sprint_symbol(str, address);
1250 return trace_seq_printf(s, fmt, str);
1255 #ifndef CONFIG_64BIT
1256 # define IP_FMT "%08lx"
1258 # define IP_FMT "%016lx"
1262 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1267 return trace_seq_printf(s, "0");
1269 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1270 ret = seq_print_sym_offset(s, "%s", ip);
1272 ret = seq_print_sym_short(s, "%s", ip);
1277 if (sym_flags & TRACE_ITER_SYM_ADDR)
1278 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1282 static void print_lat_help_header(struct seq_file *m)
1284 seq_puts(m, "# _------=> CPU# \n");
1285 seq_puts(m, "# / _-----=> irqs-off \n");
1286 seq_puts(m, "# | / _----=> need-resched \n");
1287 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1288 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1289 seq_puts(m, "# |||| / \n");
1290 seq_puts(m, "# ||||| delay \n");
1291 seq_puts(m, "# cmd pid ||||| time | caller \n");
1292 seq_puts(m, "# \\ / ||||| \\ | / \n");
1295 static void print_func_help_header(struct seq_file *m)
1297 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1298 seq_puts(m, "# | | | | |\n");
1303 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1305 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1306 struct trace_array *tr = iter->tr;
1307 struct trace_array_cpu *data = tr->data[tr->cpu];
1308 struct tracer *type = current_trace;
1309 unsigned long total = 0;
1310 unsigned long entries = 0;
1312 const char *name = "preemption";
1317 for_each_tracing_cpu(cpu) {
1318 if (head_page(tr->data[cpu])) {
1319 total += tr->data[cpu]->trace_idx;
1320 if (tr->data[cpu]->trace_idx > tr->entries)
1321 entries += tr->entries;
1323 entries += tr->data[cpu]->trace_idx;
1327 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1329 seq_puts(m, "-----------------------------------"
1330 "---------------------------------\n");
1331 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1332 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1333 nsecs_to_usecs(data->saved_latency),
1337 #if defined(CONFIG_PREEMPT_NONE)
1339 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1341 #elif defined(CONFIG_PREEMPT_DESKTOP)
1346 /* These are reserved for later use */
1349 seq_printf(m, " #P:%d)\n", num_online_cpus());
1353 seq_puts(m, " -----------------\n");
1354 seq_printf(m, " | task: %.16s-%d "
1355 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1356 data->comm, data->pid, data->uid, data->nice,
1357 data->policy, data->rt_priority);
1358 seq_puts(m, " -----------------\n");
1360 if (data->critical_start) {
1361 seq_puts(m, " => started at: ");
1362 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1363 trace_print_seq(m, &iter->seq);
1364 seq_puts(m, "\n => ended at: ");
1365 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1366 trace_print_seq(m, &iter->seq);
1374 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1376 int hardirq, softirq;
1379 comm = trace_find_cmdline(entry->pid);
1381 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1382 trace_seq_printf(s, "%d", cpu);
1383 trace_seq_printf(s, "%c%c",
1384 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1385 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1387 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1388 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1389 if (hardirq && softirq) {
1390 trace_seq_putc(s, 'H');
1393 trace_seq_putc(s, 'h');
1396 trace_seq_putc(s, 's');
1398 trace_seq_putc(s, '.');
1402 if (entry->preempt_count)
1403 trace_seq_printf(s, "%x", entry->preempt_count);
1405 trace_seq_puts(s, ".");
1408 unsigned long preempt_mark_thresh = 100;
1411 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1412 unsigned long rel_usecs)
1414 trace_seq_printf(s, " %4lldus", abs_usecs);
1415 if (rel_usecs > preempt_mark_thresh)
1416 trace_seq_puts(s, "!: ");
1417 else if (rel_usecs > 1)
1418 trace_seq_puts(s, "+: ");
1420 trace_seq_puts(s, " : ");
1423 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1426 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1428 struct trace_seq *s = &iter->seq;
1429 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1430 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1431 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1432 struct trace_entry *entry = iter->ent;
1433 unsigned long abs_usecs;
1434 unsigned long rel_usecs;
1442 rel_usecs = ns2usecs(next_entry->t - entry->t);
1443 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1446 comm = trace_find_cmdline(entry->pid);
1447 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1448 " %ld.%03ldms (+%ld.%03ldms): ",
1450 entry->pid, cpu, entry->flags,
1451 entry->preempt_count, trace_idx,
1454 abs_usecs % 1000, rel_usecs/1000,
1457 lat_print_generic(s, entry, cpu);
1458 lat_print_timestamp(s, abs_usecs, rel_usecs);
1460 switch (entry->type) {
1462 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1463 trace_seq_puts(s, " (");
1464 if (kretprobed(entry->fn.parent_ip))
1465 trace_seq_puts(s, KRETPROBE_MSG);
1467 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1468 trace_seq_puts(s, ")\n");
1472 T = entry->ctx.next_state < sizeof(state_to_char) ?
1473 state_to_char[entry->ctx.next_state] : 'X';
1475 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1476 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1477 comm = trace_find_cmdline(entry->ctx.next_pid);
1478 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1479 entry->ctx.prev_pid,
1480 entry->ctx.prev_prio,
1481 S, entry->type == TRACE_CTX ? "==>" : " +",
1482 entry->ctx.next_pid,
1483 entry->ctx.next_prio,
1487 trace_seq_printf(s, "# %ld %ld %ld\n",
1488 entry->special.arg1,
1489 entry->special.arg2,
1490 entry->special.arg3);
1493 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1495 trace_seq_puts(s, " <= ");
1496 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1498 trace_seq_puts(s, "\n");
1501 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1506 static int print_trace_fmt(struct trace_iterator *iter)
1508 struct trace_seq *s = &iter->seq;
1509 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1510 struct trace_entry *entry;
1511 unsigned long usec_rem;
1512 unsigned long long t;
1521 comm = trace_find_cmdline(iter->ent->pid);
1523 t = ns2usecs(entry->t);
1524 usec_rem = do_div(t, 1000000ULL);
1525 secs = (unsigned long)t;
1527 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1530 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1533 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1537 switch (entry->type) {
1539 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1542 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1543 entry->fn.parent_ip) {
1544 ret = trace_seq_printf(s, " <-");
1547 if (kretprobed(entry->fn.parent_ip))
1548 ret = trace_seq_puts(s, KRETPROBE_MSG);
1550 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1555 ret = trace_seq_printf(s, "\n");
1561 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1562 state_to_char[entry->ctx.prev_state] : 'X';
1563 T = entry->ctx.next_state < sizeof(state_to_char) ?
1564 state_to_char[entry->ctx.next_state] : 'X';
1565 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1566 entry->ctx.prev_pid,
1567 entry->ctx.prev_prio,
1569 entry->type == TRACE_CTX ? "==>" : " +",
1570 entry->ctx.next_pid,
1571 entry->ctx.next_prio,
1577 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1578 entry->special.arg1,
1579 entry->special.arg2,
1580 entry->special.arg3);
1585 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1587 ret = trace_seq_puts(s, " <= ");
1591 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1596 ret = trace_seq_puts(s, "\n");
1604 static int print_raw_fmt(struct trace_iterator *iter)
1606 struct trace_seq *s = &iter->seq;
1607 struct trace_entry *entry;
1613 ret = trace_seq_printf(s, "%d %d %llu ",
1614 entry->pid, iter->cpu, entry->t);
1618 switch (entry->type) {
1620 ret = trace_seq_printf(s, "%x %x\n",
1621 entry->fn.ip, entry->fn.parent_ip);
1627 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1628 state_to_char[entry->ctx.prev_state] : 'X';
1629 T = entry->ctx.next_state < sizeof(state_to_char) ?
1630 state_to_char[entry->ctx.next_state] : 'X';
1631 if (entry->type == TRACE_WAKE)
1633 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1634 entry->ctx.prev_pid,
1635 entry->ctx.prev_prio,
1637 entry->ctx.next_pid,
1638 entry->ctx.next_prio,
1645 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1646 entry->special.arg1,
1647 entry->special.arg2,
1648 entry->special.arg3);
1656 #define SEQ_PUT_FIELD_RET(s, x) \
1658 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1662 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1664 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1668 static int print_hex_fmt(struct trace_iterator *iter)
1670 struct trace_seq *s = &iter->seq;
1671 unsigned char newline = '\n';
1672 struct trace_entry *entry;
1677 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1678 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1679 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1681 switch (entry->type) {
1683 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1684 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1688 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1689 state_to_char[entry->ctx.prev_state] : 'X';
1690 T = entry->ctx.next_state < sizeof(state_to_char) ?
1691 state_to_char[entry->ctx.next_state] : 'X';
1692 if (entry->type == TRACE_WAKE)
1694 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1695 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1696 SEQ_PUT_HEX_FIELD_RET(s, S);
1697 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1698 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1699 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1700 SEQ_PUT_HEX_FIELD_RET(s, T);
1704 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1705 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1706 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1709 SEQ_PUT_FIELD_RET(s, newline);
1714 static int print_bin_fmt(struct trace_iterator *iter)
1716 struct trace_seq *s = &iter->seq;
1717 struct trace_entry *entry;
1721 SEQ_PUT_FIELD_RET(s, entry->pid);
1722 SEQ_PUT_FIELD_RET(s, entry->cpu);
1723 SEQ_PUT_FIELD_RET(s, entry->t);
1725 switch (entry->type) {
1727 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1728 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1731 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1732 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1733 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1734 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1735 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1736 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1740 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1741 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1742 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1748 static int trace_empty(struct trace_iterator *iter)
1750 struct trace_array_cpu *data;
1753 for_each_tracing_cpu(cpu) {
1754 data = iter->tr->data[cpu];
1756 if (head_page(data) && data->trace_idx &&
1757 (data->trace_tail != data->trace_head ||
1758 data->trace_tail_idx != data->trace_head_idx))
1764 static int print_trace_line(struct trace_iterator *iter)
1766 if (iter->trace && iter->trace->print_line)
1767 return iter->trace->print_line(iter);
1769 if (trace_flags & TRACE_ITER_BIN)
1770 return print_bin_fmt(iter);
1772 if (trace_flags & TRACE_ITER_HEX)
1773 return print_hex_fmt(iter);
1775 if (trace_flags & TRACE_ITER_RAW)
1776 return print_raw_fmt(iter);
1778 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1779 return print_lat_fmt(iter, iter->idx, iter->cpu);
1781 return print_trace_fmt(iter);
1784 static int s_show(struct seq_file *m, void *v)
1786 struct trace_iterator *iter = v;
1788 if (iter->ent == NULL) {
1790 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1793 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1794 /* print nothing if the buffers are empty */
1795 if (trace_empty(iter))
1797 print_trace_header(m, iter);
1798 if (!(trace_flags & TRACE_ITER_VERBOSE))
1799 print_lat_help_header(m);
1801 if (!(trace_flags & TRACE_ITER_VERBOSE))
1802 print_func_help_header(m);
1805 print_trace_line(iter);
1806 trace_print_seq(m, &iter->seq);
1812 static struct seq_operations tracer_seq_ops = {
1819 static struct trace_iterator *
1820 __tracing_open(struct inode *inode, struct file *file, int *ret)
1822 struct trace_iterator *iter;
1824 if (tracing_disabled) {
1829 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1835 mutex_lock(&trace_types_lock);
1836 if (current_trace && current_trace->print_max)
1839 iter->tr = inode->i_private;
1840 iter->trace = current_trace;
1843 /* TODO stop tracer */
1844 *ret = seq_open(file, &tracer_seq_ops);
1846 struct seq_file *m = file->private_data;
1849 /* stop the trace while dumping */
1853 if (iter->trace && iter->trace->open)
1854 iter->trace->open(iter);
1859 mutex_unlock(&trace_types_lock);
1865 int tracing_open_generic(struct inode *inode, struct file *filp)
1867 if (tracing_disabled)
1870 filp->private_data = inode->i_private;
1874 int tracing_release(struct inode *inode, struct file *file)
1876 struct seq_file *m = (struct seq_file *)file->private_data;
1877 struct trace_iterator *iter = m->private;
1879 mutex_lock(&trace_types_lock);
1880 if (iter->trace && iter->trace->close)
1881 iter->trace->close(iter);
1883 /* reenable tracing if it was previously enabled */
1886 mutex_unlock(&trace_types_lock);
1888 seq_release(inode, file);
1893 static int tracing_open(struct inode *inode, struct file *file)
1897 __tracing_open(inode, file, &ret);
1902 static int tracing_lt_open(struct inode *inode, struct file *file)
1904 struct trace_iterator *iter;
1907 iter = __tracing_open(inode, file, &ret);
1910 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1917 t_next(struct seq_file *m, void *v, loff_t *pos)
1919 struct tracer *t = m->private;
1931 static void *t_start(struct seq_file *m, loff_t *pos)
1933 struct tracer *t = m->private;
1936 mutex_lock(&trace_types_lock);
1937 for (; t && l < *pos; t = t_next(m, t, &l))
1943 static void t_stop(struct seq_file *m, void *p)
1945 mutex_unlock(&trace_types_lock);
1948 static int t_show(struct seq_file *m, void *v)
1950 struct tracer *t = v;
1955 seq_printf(m, "%s", t->name);
1964 static struct seq_operations show_traces_seq_ops = {
1971 static int show_traces_open(struct inode *inode, struct file *file)
1975 if (tracing_disabled)
1978 ret = seq_open(file, &show_traces_seq_ops);
1980 struct seq_file *m = file->private_data;
1981 m->private = trace_types;
1987 static struct file_operations tracing_fops = {
1988 .open = tracing_open,
1990 .llseek = seq_lseek,
1991 .release = tracing_release,
1994 static struct file_operations tracing_lt_fops = {
1995 .open = tracing_lt_open,
1997 .llseek = seq_lseek,
1998 .release = tracing_release,
2001 static struct file_operations show_traces_fops = {
2002 .open = show_traces_open,
2004 .release = seq_release,
2008 * Only trace on a CPU if the bitmask is set:
2010 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2013 * When tracing/tracing_cpu_mask is modified then this holds
2014 * the new bitmask we are about to install:
2016 static cpumask_t tracing_cpumask_new;
2019 * The tracer itself will not take this lock, but still we want
2020 * to provide a consistent cpumask to user-space:
2022 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2025 * Temporary storage for the character representation of the
2026 * CPU bitmask (and one more byte for the newline):
2028 static char mask_str[NR_CPUS + 1];
2031 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2032 size_t count, loff_t *ppos)
2036 mutex_lock(&tracing_cpumask_update_lock);
2038 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2039 if (count - len < 2) {
2043 len += sprintf(mask_str + len, "\n");
2044 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2047 mutex_unlock(&tracing_cpumask_update_lock);
2053 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2054 size_t count, loff_t *ppos)
2058 mutex_lock(&tracing_cpumask_update_lock);
2059 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2063 raw_local_irq_disable();
2064 __raw_spin_lock(&ftrace_max_lock);
2065 for_each_tracing_cpu(cpu) {
2067 * Increase/decrease the disabled counter if we are
2068 * about to flip a bit in the cpumask:
2070 if (cpu_isset(cpu, tracing_cpumask) &&
2071 !cpu_isset(cpu, tracing_cpumask_new)) {
2072 atomic_inc(&global_trace.data[cpu]->disabled);
2074 if (!cpu_isset(cpu, tracing_cpumask) &&
2075 cpu_isset(cpu, tracing_cpumask_new)) {
2076 atomic_dec(&global_trace.data[cpu]->disabled);
2079 __raw_spin_unlock(&ftrace_max_lock);
2080 raw_local_irq_enable();
2082 tracing_cpumask = tracing_cpumask_new;
2084 mutex_unlock(&tracing_cpumask_update_lock);
2089 mutex_unlock(&tracing_cpumask_update_lock);
2094 static struct file_operations tracing_cpumask_fops = {
2095 .open = tracing_open_generic,
2096 .read = tracing_cpumask_read,
2097 .write = tracing_cpumask_write,
2101 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2102 size_t cnt, loff_t *ppos)
2109 /* calulate max size */
2110 for (i = 0; trace_options[i]; i++) {
2111 len += strlen(trace_options[i]);
2112 len += 3; /* "no" and space */
2115 /* +2 for \n and \0 */
2116 buf = kmalloc(len + 2, GFP_KERNEL);
2120 for (i = 0; trace_options[i]; i++) {
2121 if (trace_flags & (1 << i))
2122 r += sprintf(buf + r, "%s ", trace_options[i]);
2124 r += sprintf(buf + r, "no%s ", trace_options[i]);
2127 r += sprintf(buf + r, "\n");
2128 WARN_ON(r >= len + 2);
2130 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2138 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2139 size_t cnt, loff_t *ppos)
2146 if (cnt >= sizeof(buf))
2149 if (copy_from_user(&buf, ubuf, cnt))
2154 if (strncmp(buf, "no", 2) == 0) {
2159 for (i = 0; trace_options[i]; i++) {
2160 int len = strlen(trace_options[i]);
2162 if (strncmp(cmp, trace_options[i], len) == 0) {
2164 trace_flags &= ~(1 << i);
2166 trace_flags |= (1 << i);
2171 * If no option could be set, return an error:
2173 if (!trace_options[i])
2181 static struct file_operations tracing_iter_fops = {
2182 .open = tracing_open_generic,
2183 .read = tracing_iter_ctrl_read,
2184 .write = tracing_iter_ctrl_write,
2187 static const char readme_msg[] =
2188 "tracing mini-HOWTO:\n\n"
2190 "# mount -t debugfs nodev /debug\n\n"
2191 "# cat /debug/tracing/available_tracers\n"
2192 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2193 "# cat /debug/tracing/current_tracer\n"
2195 "# echo sched_switch > /debug/tracing/current_tracer\n"
2196 "# cat /debug/tracing/current_tracer\n"
2198 "# cat /debug/tracing/iter_ctrl\n"
2199 "noprint-parent nosym-offset nosym-addr noverbose\n"
2200 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2201 "# echo 1 > /debug/tracing/tracing_enabled\n"
2202 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2203 "echo 0 > /debug/tracing/tracing_enabled\n"
2207 tracing_readme_read(struct file *filp, char __user *ubuf,
2208 size_t cnt, loff_t *ppos)
2210 return simple_read_from_buffer(ubuf, cnt, ppos,
2211 readme_msg, strlen(readme_msg));
2214 static struct file_operations tracing_readme_fops = {
2215 .open = tracing_open_generic,
2216 .read = tracing_readme_read,
2220 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2221 size_t cnt, loff_t *ppos)
2223 struct trace_array *tr = filp->private_data;
2227 r = sprintf(buf, "%ld\n", tr->ctrl);
2228 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2232 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2233 size_t cnt, loff_t *ppos)
2235 struct trace_array *tr = filp->private_data;
2240 if (cnt >= sizeof(buf))
2243 if (copy_from_user(&buf, ubuf, cnt))
2248 ret = strict_strtoul(buf, 10, &val);
2254 mutex_lock(&trace_types_lock);
2255 if (tr->ctrl ^ val) {
2263 if (current_trace && current_trace->ctrl_update)
2264 current_trace->ctrl_update(tr);
2266 mutex_unlock(&trace_types_lock);
2274 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2275 size_t cnt, loff_t *ppos)
2277 char buf[max_tracer_type_len+2];
2280 mutex_lock(&trace_types_lock);
2282 r = sprintf(buf, "%s\n", current_trace->name);
2284 r = sprintf(buf, "\n");
2285 mutex_unlock(&trace_types_lock);
2287 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2291 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2292 size_t cnt, loff_t *ppos)
2294 struct trace_array *tr = &global_trace;
2296 char buf[max_tracer_type_len+1];
2299 if (cnt > max_tracer_type_len)
2300 cnt = max_tracer_type_len;
2302 if (copy_from_user(&buf, ubuf, cnt))
2307 /* strip ending whitespace. */
2308 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2311 mutex_lock(&trace_types_lock);
2312 for (t = trace_types; t; t = t->next) {
2313 if (strcmp(t->name, buf) == 0)
2316 if (!t || t == current_trace)
2319 if (current_trace && current_trace->reset)
2320 current_trace->reset(tr);
2327 mutex_unlock(&trace_types_lock);
2335 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2338 unsigned long *ptr = filp->private_data;
2342 r = snprintf(buf, sizeof(buf), "%ld\n",
2343 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2344 if (r > sizeof(buf))
2346 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2350 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2351 size_t cnt, loff_t *ppos)
2353 long *ptr = filp->private_data;
2358 if (cnt >= sizeof(buf))
2361 if (copy_from_user(&buf, ubuf, cnt))
2366 ret = strict_strtoul(buf, 10, &val);
2375 static atomic_t tracing_reader;
2377 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2379 struct trace_iterator *iter;
2381 if (tracing_disabled)
2384 /* We only allow for reader of the pipe */
2385 if (atomic_inc_return(&tracing_reader) != 1) {
2386 atomic_dec(&tracing_reader);
2390 /* create a buffer to store the information to pass to userspace */
2391 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2395 mutex_lock(&trace_types_lock);
2396 iter->tr = &global_trace;
2397 iter->trace = current_trace;
2398 filp->private_data = iter;
2400 if (iter->trace->pipe_open)
2401 iter->trace->pipe_open(iter);
2402 mutex_unlock(&trace_types_lock);
2407 static int tracing_release_pipe(struct inode *inode, struct file *file)
2409 struct trace_iterator *iter = file->private_data;
2412 atomic_dec(&tracing_reader);
2418 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2420 struct trace_iterator *iter = filp->private_data;
2422 if (trace_flags & TRACE_ITER_BLOCK) {
2424 * Always select as readable when in blocking mode
2426 return POLLIN | POLLRDNORM;
2428 if (!trace_empty(iter))
2429 return POLLIN | POLLRDNORM;
2430 poll_wait(filp, &trace_wait, poll_table);
2431 if (!trace_empty(iter))
2432 return POLLIN | POLLRDNORM;
2442 tracing_read_pipe(struct file *filp, char __user *ubuf,
2443 size_t cnt, loff_t *ppos)
2445 struct trace_iterator *iter = filp->private_data;
2446 struct trace_array_cpu *data;
2447 static cpumask_t mask;
2448 unsigned long flags;
2449 #ifdef CONFIG_FTRACE
2455 /* return any leftover data */
2456 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2461 trace_seq_reset(&iter->seq);
2463 mutex_lock(&trace_types_lock);
2464 if (iter->trace->read) {
2465 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2470 while (trace_empty(iter)) {
2472 if ((filp->f_flags & O_NONBLOCK)) {
2478 * This is a make-shift waitqueue. The reason we don't use
2479 * an actual wait queue is because:
2480 * 1) we only ever have one waiter
2481 * 2) the tracing, traces all functions, we don't want
2482 * the overhead of calling wake_up and friends
2483 * (and tracing them too)
2484 * Anyway, this is really very primitive wakeup.
2486 set_current_state(TASK_INTERRUPTIBLE);
2487 iter->tr->waiter = current;
2489 mutex_unlock(&trace_types_lock);
2491 /* sleep for 100 msecs, and try again. */
2492 schedule_timeout(HZ/10);
2494 mutex_lock(&trace_types_lock);
2496 iter->tr->waiter = NULL;
2498 if (signal_pending(current)) {
2503 if (iter->trace != current_trace)
2507 * We block until we read something and tracing is disabled.
2508 * We still block if tracing is disabled, but we have never
2509 * read anything. This allows a user to cat this file, and
2510 * then enable tracing. But after we have read something,
2511 * we give an EOF when tracing is again disabled.
2513 * iter->pos will be 0 if we haven't read anything.
2515 if (!tracer_enabled && iter->pos)
2521 /* stop when tracing is finished */
2522 if (trace_empty(iter))
2525 if (cnt >= PAGE_SIZE)
2526 cnt = PAGE_SIZE - 1;
2528 /* reset all but tr, trace, and overruns */
2529 memset(&iter->seq, 0,
2530 sizeof(struct trace_iterator) -
2531 offsetof(struct trace_iterator, seq));
2535 * We need to stop all tracing on all CPUS to read the
2536 * the next buffer. This is a bit expensive, but is
2537 * not done often. We fill all what we can read,
2538 * and then release the locks again.
2542 local_irq_save(flags);
2543 #ifdef CONFIG_FTRACE
2544 ftrace_save = ftrace_enabled;
2548 for_each_tracing_cpu(cpu) {
2549 data = iter->tr->data[cpu];
2551 if (!head_page(data) || !data->trace_idx)
2554 atomic_inc(&data->disabled);
2558 for_each_cpu_mask(cpu, mask) {
2559 data = iter->tr->data[cpu];
2560 __raw_spin_lock(&data->lock);
2562 if (data->overrun > iter->last_overrun[cpu])
2563 iter->overrun[cpu] +=
2564 data->overrun - iter->last_overrun[cpu];
2565 iter->last_overrun[cpu] = data->overrun;
2568 while (find_next_entry_inc(iter) != NULL) {
2570 int len = iter->seq.len;
2572 ret = print_trace_line(iter);
2574 /* don't print partial lines */
2575 iter->seq.len = len;
2579 trace_consume(iter);
2581 if (iter->seq.len >= cnt)
2585 for_each_cpu_mask(cpu, mask) {
2586 data = iter->tr->data[cpu];
2587 __raw_spin_unlock(&data->lock);
2590 for_each_cpu_mask(cpu, mask) {
2591 data = iter->tr->data[cpu];
2592 atomic_dec(&data->disabled);
2594 #ifdef CONFIG_FTRACE
2595 ftrace_enabled = ftrace_save;
2597 local_irq_restore(flags);
2599 /* Now copy what we have to the user */
2600 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2601 if (iter->seq.readpos >= iter->seq.len)
2602 trace_seq_reset(&iter->seq);
2607 mutex_unlock(&trace_types_lock);
2613 tracing_entries_read(struct file *filp, char __user *ubuf,
2614 size_t cnt, loff_t *ppos)
2616 struct trace_array *tr = filp->private_data;
2620 r = sprintf(buf, "%lu\n", tr->entries);
2621 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2625 tracing_entries_write(struct file *filp, const char __user *ubuf,
2626 size_t cnt, loff_t *ppos)
2632 if (cnt >= sizeof(buf))
2635 if (copy_from_user(&buf, ubuf, cnt))
2640 ret = strict_strtoul(buf, 10, &val);
2644 /* must have at least 1 entry */
2648 mutex_lock(&trace_types_lock);
2650 if (current_trace != &no_tracer) {
2652 pr_info("ftrace: set current_tracer to none"
2653 " before modifying buffer size\n");
2657 if (val > global_trace.entries) {
2658 long pages_requested;
2659 unsigned long freeable_pages;
2661 /* make sure we have enough memory before mapping */
2663 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2665 /* account for each buffer (and max_tr) */
2666 pages_requested *= tracing_nr_buffers * 2;
2668 /* Check for overflow */
2669 if (pages_requested < 0) {
2674 freeable_pages = determine_dirtyable_memory();
2676 /* we only allow to request 1/4 of useable memory */
2677 if (pages_requested >
2678 ((freeable_pages + tracing_pages_allocated) / 4)) {
2683 while (global_trace.entries < val) {
2684 if (trace_alloc_page()) {
2688 /* double check that we don't go over the known pages */
2689 if (tracing_pages_allocated > pages_requested)
2694 /* include the number of entries in val (inc of page entries) */
2695 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2699 /* check integrity */
2700 for_each_tracing_cpu(i)
2701 check_pages(global_trace.data[i]);
2705 /* If check pages failed, return ENOMEM */
2706 if (tracing_disabled)
2709 max_tr.entries = global_trace.entries;
2710 mutex_unlock(&trace_types_lock);
2715 static struct file_operations tracing_max_lat_fops = {
2716 .open = tracing_open_generic,
2717 .read = tracing_max_lat_read,
2718 .write = tracing_max_lat_write,
2721 static struct file_operations tracing_ctrl_fops = {
2722 .open = tracing_open_generic,
2723 .read = tracing_ctrl_read,
2724 .write = tracing_ctrl_write,
2727 static struct file_operations set_tracer_fops = {
2728 .open = tracing_open_generic,
2729 .read = tracing_set_trace_read,
2730 .write = tracing_set_trace_write,
2733 static struct file_operations tracing_pipe_fops = {
2734 .open = tracing_open_pipe,
2735 .poll = tracing_poll_pipe,
2736 .read = tracing_read_pipe,
2737 .release = tracing_release_pipe,
2740 static struct file_operations tracing_entries_fops = {
2741 .open = tracing_open_generic,
2742 .read = tracing_entries_read,
2743 .write = tracing_entries_write,
2746 #ifdef CONFIG_DYNAMIC_FTRACE
2749 tracing_read_long(struct file *filp, char __user *ubuf,
2750 size_t cnt, loff_t *ppos)
2752 unsigned long *p = filp->private_data;
2756 r = sprintf(buf, "%ld\n", *p);
2758 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2761 static struct file_operations tracing_read_long_fops = {
2762 .open = tracing_open_generic,
2763 .read = tracing_read_long,
2767 static struct dentry *d_tracer;
2769 struct dentry *tracing_init_dentry(void)
2776 d_tracer = debugfs_create_dir("tracing", NULL);
2778 if (!d_tracer && !once) {
2780 pr_warning("Could not create debugfs directory 'tracing'\n");
2787 #ifdef CONFIG_FTRACE_SELFTEST
2788 /* Let selftest have access to static functions in this file */
2789 #include "trace_selftest.c"
2792 static __init void tracer_init_debugfs(void)
2794 struct dentry *d_tracer;
2795 struct dentry *entry;
2797 d_tracer = tracing_init_dentry();
2799 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2800 &global_trace, &tracing_ctrl_fops);
2802 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2804 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2805 NULL, &tracing_iter_fops);
2807 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2809 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2810 NULL, &tracing_cpumask_fops);
2812 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2814 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2815 &global_trace, &tracing_lt_fops);
2817 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2819 entry = debugfs_create_file("trace", 0444, d_tracer,
2820 &global_trace, &tracing_fops);
2822 pr_warning("Could not create debugfs 'trace' entry\n");
2824 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2825 &global_trace, &show_traces_fops);
2827 pr_warning("Could not create debugfs 'trace' entry\n");
2829 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2830 &global_trace, &set_tracer_fops);
2832 pr_warning("Could not create debugfs 'trace' entry\n");
2834 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2835 &tracing_max_latency,
2836 &tracing_max_lat_fops);
2838 pr_warning("Could not create debugfs "
2839 "'tracing_max_latency' entry\n");
2841 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2842 &tracing_thresh, &tracing_max_lat_fops);
2844 pr_warning("Could not create debugfs "
2845 "'tracing_threash' entry\n");
2846 entry = debugfs_create_file("README", 0644, d_tracer,
2847 NULL, &tracing_readme_fops);
2849 pr_warning("Could not create debugfs 'README' entry\n");
2851 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2852 NULL, &tracing_pipe_fops);
2854 pr_warning("Could not create debugfs "
2855 "'tracing_threash' entry\n");
2857 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2858 &global_trace, &tracing_entries_fops);
2860 pr_warning("Could not create debugfs "
2861 "'tracing_threash' entry\n");
2863 #ifdef CONFIG_DYNAMIC_FTRACE
2864 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2865 &ftrace_update_tot_cnt,
2866 &tracing_read_long_fops);
2868 pr_warning("Could not create debugfs "
2869 "'dyn_ftrace_total_info' entry\n");
2873 static int trace_alloc_page(void)
2875 struct trace_array_cpu *data;
2876 struct page *page, *tmp;
2879 unsigned pages_allocated = 0;
2882 /* first allocate a page for each CPU */
2883 for_each_tracing_cpu(i) {
2884 array = (void *)__get_free_page(GFP_KERNEL);
2885 if (array == NULL) {
2886 printk(KERN_ERR "tracer: failed to allocate page"
2887 "for trace buffer!\n");
2892 page = virt_to_page(array);
2893 list_add(&page->lru, &pages);
2895 /* Only allocate if we are actually using the max trace */
2896 #ifdef CONFIG_TRACER_MAX_TRACE
2897 array = (void *)__get_free_page(GFP_KERNEL);
2898 if (array == NULL) {
2899 printk(KERN_ERR "tracer: failed to allocate page"
2900 "for trace buffer!\n");
2904 page = virt_to_page(array);
2905 list_add(&page->lru, &pages);
2909 /* Now that we successfully allocate a page per CPU, add them */
2910 for_each_tracing_cpu(i) {
2911 data = global_trace.data[i];
2912 page = list_entry(pages.next, struct page, lru);
2913 list_del_init(&page->lru);
2914 list_add_tail(&page->lru, &data->trace_pages);
2917 #ifdef CONFIG_TRACER_MAX_TRACE
2918 data = max_tr.data[i];
2919 page = list_entry(pages.next, struct page, lru);
2920 list_del_init(&page->lru);
2921 list_add_tail(&page->lru, &data->trace_pages);
2925 tracing_pages_allocated += pages_allocated;
2926 global_trace.entries += ENTRIES_PER_PAGE;
2931 list_for_each_entry_safe(page, tmp, &pages, lru) {
2932 list_del_init(&page->lru);
2938 static int trace_free_page(void)
2940 struct trace_array_cpu *data;
2942 struct list_head *p;
2946 /* free one page from each buffer */
2947 for_each_tracing_cpu(i) {
2948 data = global_trace.data[i];
2949 p = data->trace_pages.next;
2950 if (p == &data->trace_pages) {
2951 /* should never happen */
2953 tracing_disabled = 1;
2957 page = list_entry(p, struct page, lru);
2959 list_del(&page->lru);
2960 tracing_pages_allocated--;
2961 tracing_pages_allocated--;
2964 tracing_reset(data);
2966 #ifdef CONFIG_TRACER_MAX_TRACE
2967 data = max_tr.data[i];
2968 p = data->trace_pages.next;
2969 if (p == &data->trace_pages) {
2970 /* should never happen */
2972 tracing_disabled = 1;
2976 page = list_entry(p, struct page, lru);
2978 list_del(&page->lru);
2981 tracing_reset(data);
2984 global_trace.entries -= ENTRIES_PER_PAGE;
2989 __init static int tracer_alloc_buffers(void)
2991 struct trace_array_cpu *data;
2998 /* TODO: make the number of buffers hot pluggable with CPUS */
2999 tracing_nr_buffers = num_possible_cpus();
3000 tracing_buffer_mask = cpu_possible_map;
3002 /* Allocate the first page for all buffers */
3003 for_each_tracing_cpu(i) {
3004 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3005 max_tr.data[i] = &per_cpu(max_data, i);
3007 array = (void *)__get_free_page(GFP_KERNEL);
3008 if (array == NULL) {
3009 printk(KERN_ERR "tracer: failed to allocate page"
3010 "for trace buffer!\n");
3014 /* set the array to the list */
3015 INIT_LIST_HEAD(&data->trace_pages);
3016 page = virt_to_page(array);
3017 list_add(&page->lru, &data->trace_pages);
3018 /* use the LRU flag to differentiate the two buffers */
3021 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3022 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3024 /* Only allocate if we are actually using the max trace */
3025 #ifdef CONFIG_TRACER_MAX_TRACE
3026 array = (void *)__get_free_page(GFP_KERNEL);
3027 if (array == NULL) {
3028 printk(KERN_ERR "tracer: failed to allocate page"
3029 "for trace buffer!\n");
3033 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3034 page = virt_to_page(array);
3035 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3041 * Since we allocate by orders of pages, we may be able to
3044 global_trace.entries = ENTRIES_PER_PAGE;
3047 while (global_trace.entries < trace_nr_entries) {
3048 if (trace_alloc_page())
3052 max_tr.entries = global_trace.entries;
3054 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3055 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3056 pr_info(" actual entries %ld\n", global_trace.entries);
3058 tracer_init_debugfs();
3060 trace_init_cmdlines();
3062 register_tracer(&no_tracer);
3063 current_trace = &no_tracer;
3065 /* All seems OK, enable tracing */
3066 global_trace.ctrl = tracer_enabled;
3067 tracing_disabled = 0;
3072 for (i-- ; i >= 0; i--) {
3073 struct page *page, *tmp;
3074 struct trace_array_cpu *data = global_trace.data[i];
3077 list_for_each_entry_safe(page, tmp,
3078 &data->trace_pages, lru) {
3079 list_del_init(&page->lru);
3084 #ifdef CONFIG_TRACER_MAX_TRACE
3085 data = max_tr.data[i];
3087 list_for_each_entry_safe(page, tmp,
3088 &data->trace_pages, lru) {
3089 list_del_init(&page->lru);
3097 fs_initcall(tracer_alloc_buffers);