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;
99 /* function tracing enabled */
100 int ftrace_function_enabled;
103 * trace_nr_entries is the number of entries that is allocated
104 * for a buffer. Note, the number of entries is always rounded
105 * to ENTRIES_PER_PAGE.
107 static unsigned long trace_nr_entries = 65536UL;
109 /* trace_types holds a link list of available tracers. */
110 static struct tracer *trace_types __read_mostly;
112 /* current_trace points to the tracer that is currently active */
113 static struct tracer *current_trace __read_mostly;
116 * max_tracer_type_len is used to simplify the allocating of
117 * buffers to read userspace tracer names. We keep track of
118 * the longest tracer name registered.
120 static int max_tracer_type_len;
123 * trace_types_lock is used to protect the trace_types list.
124 * This lock is also used to keep user access serialized.
125 * Accesses from userspace will grab this lock while userspace
126 * activities happen inside the kernel.
128 static DEFINE_MUTEX(trace_types_lock);
130 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
131 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
133 /* trace_flags holds iter_ctrl options */
134 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
136 static notrace void no_trace_init(struct trace_array *tr)
140 ftrace_function_enabled = 0;
142 for_each_online_cpu(cpu)
143 tracing_reset(tr->data[cpu]);
147 /* dummy trace to disable tracing */
148 static struct tracer no_tracer __read_mostly = {
150 .init = no_trace_init
155 * trace_wake_up - wake up tasks waiting for trace input
157 * Simply wakes up any task that is blocked on the trace_wait
158 * queue. These is used with trace_poll for tasks polling the trace.
160 void trace_wake_up(void)
163 * The runqueue_is_locked() can fail, but this is the best we
166 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
167 wake_up(&trace_wait);
170 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
172 static int __init set_nr_entries(char *str)
174 unsigned long nr_entries;
179 ret = strict_strtoul(str, 0, &nr_entries);
180 /* nr_entries can not be zero */
181 if (ret < 0 || nr_entries == 0)
183 trace_nr_entries = nr_entries;
186 __setup("trace_entries=", set_nr_entries);
188 unsigned long nsecs_to_usecs(unsigned long nsecs)
194 * trace_flag_type is an enumeration that holds different
195 * states when a trace occurs. These are:
196 * IRQS_OFF - interrupts were disabled
197 * NEED_RESCED - reschedule is requested
198 * HARDIRQ - inside an interrupt handler
199 * SOFTIRQ - inside a softirq handler
201 enum trace_flag_type {
202 TRACE_FLAG_IRQS_OFF = 0x01,
203 TRACE_FLAG_NEED_RESCHED = 0x02,
204 TRACE_FLAG_HARDIRQ = 0x04,
205 TRACE_FLAG_SOFTIRQ = 0x08,
209 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
210 * control the output of kernel symbols.
212 #define TRACE_ITER_SYM_MASK \
213 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
215 /* These must match the bit postions in trace_iterator_flags */
216 static const char *trace_options[] = {
231 * ftrace_max_lock is used to protect the swapping of buffers
232 * when taking a max snapshot. The buffers themselves are
233 * protected by per_cpu spinlocks. But the action of the swap
234 * needs its own lock.
236 * This is defined as a raw_spinlock_t in order to help
237 * with performance when lockdep debugging is enabled.
239 static raw_spinlock_t ftrace_max_lock =
240 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
243 * Copy the new maximum trace into the separate maximum-trace
244 * structure. (this way the maximum trace is permanently saved,
245 * for later retrieval via /debugfs/tracing/latency_trace)
248 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
250 struct trace_array_cpu *data = tr->data[cpu];
253 max_tr.time_start = data->preempt_timestamp;
255 data = max_tr.data[cpu];
256 data->saved_latency = tracing_max_latency;
258 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
259 data->pid = tsk->pid;
260 data->uid = tsk->uid;
261 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
262 data->policy = tsk->policy;
263 data->rt_priority = tsk->rt_priority;
265 /* record this tasks comm */
266 tracing_record_cmdline(current);
269 #define CHECK_COND(cond) \
270 if (unlikely(cond)) { \
271 tracing_disabled = 1; \
277 * check_pages - integrity check of trace buffers
279 * As a safty measure we check to make sure the data pages have not
282 int check_pages(struct trace_array_cpu *data)
284 struct page *page, *tmp;
286 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
287 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
289 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
290 CHECK_COND(page->lru.next->prev != &page->lru);
291 CHECK_COND(page->lru.prev->next != &page->lru);
298 * head_page - page address of the first page in per_cpu buffer.
300 * head_page returns the page address of the first page in
301 * a per_cpu buffer. This also preforms various consistency
302 * checks to make sure the buffer has not been corrupted.
304 void *head_page(struct trace_array_cpu *data)
308 if (list_empty(&data->trace_pages))
311 page = list_entry(data->trace_pages.next, struct page, lru);
312 BUG_ON(&page->lru == &data->trace_pages);
314 return page_address(page);
318 * trace_seq_printf - sequence printing of trace information
319 * @s: trace sequence descriptor
320 * @fmt: printf format string
322 * The tracer may use either sequence operations or its own
323 * copy to user routines. To simplify formating of a trace
324 * trace_seq_printf is used to store strings into a special
325 * buffer (@s). Then the output may be either used by
326 * the sequencer or pulled into another buffer.
329 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
331 int len = (PAGE_SIZE - 1) - s->len;
339 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
342 /* If we can't write it all, don't bother writing anything */
352 * trace_seq_puts - trace sequence printing of simple string
353 * @s: trace sequence descriptor
354 * @str: simple string to record
356 * The tracer may use either the sequence operations or its own
357 * copy to user routines. This function records a simple string
358 * into a special buffer (@s) for later retrieval by a sequencer
359 * or other mechanism.
362 trace_seq_puts(struct trace_seq *s, const char *str)
364 int len = strlen(str);
366 if (len > ((PAGE_SIZE - 1) - s->len))
369 memcpy(s->buffer + s->len, str, len);
376 trace_seq_putc(struct trace_seq *s, unsigned char c)
378 if (s->len >= (PAGE_SIZE - 1))
381 s->buffer[s->len++] = c;
387 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
389 if (len > ((PAGE_SIZE - 1) - s->len))
392 memcpy(s->buffer + s->len, mem, len);
399 static const char hex2asc[] = "0123456789abcdef";
402 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
404 unsigned char hex[HEX_CHARS];
405 unsigned char *data = mem;
409 BUG_ON(len >= HEX_CHARS);
412 for (i = 0, j = 0; i < len; i++) {
414 for (i = len-1, j = 0; i >= 0; i--) {
418 hex[j++] = hex2asc[byte & 0x0f];
419 hex[j++] = hex2asc[byte >> 4];
423 return trace_seq_putmem(s, hex, j);
427 trace_seq_reset(struct trace_seq *s)
433 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
438 if (s->len <= s->readpos)
441 len = s->len - s->readpos;
444 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
453 trace_print_seq(struct seq_file *m, struct trace_seq *s)
455 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
458 seq_puts(m, s->buffer);
464 * flip the trace buffers between two trace descriptors.
465 * This usually is the buffers between the global_trace and
466 * the max_tr to record a snapshot of a current trace.
468 * The ftrace_max_lock must be held.
471 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
473 struct list_head flip_pages;
475 INIT_LIST_HEAD(&flip_pages);
477 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
478 sizeof(struct trace_array_cpu) -
479 offsetof(struct trace_array_cpu, trace_head_idx));
483 list_splice_init(&tr1->trace_pages, &flip_pages);
484 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
485 list_splice_init(&flip_pages, &tr2->trace_pages);
486 BUG_ON(!list_empty(&flip_pages));
492 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
494 * @tsk: the task with the latency
495 * @cpu: The cpu that initiated the trace.
497 * Flip the buffers between the @tr and the max_tr and record information
498 * about which task was the cause of this latency.
501 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
503 struct trace_array_cpu *data;
506 WARN_ON_ONCE(!irqs_disabled());
507 __raw_spin_lock(&ftrace_max_lock);
508 /* clear out all the previous traces */
509 for_each_tracing_cpu(i) {
511 flip_trace(max_tr.data[i], data);
515 __update_max_tr(tr, tsk, cpu);
516 __raw_spin_unlock(&ftrace_max_lock);
520 * update_max_tr_single - only copy one trace over, and reset the rest
522 * @tsk - task with the latency
523 * @cpu - the cpu of the buffer to copy.
525 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
528 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
530 struct trace_array_cpu *data = tr->data[cpu];
533 WARN_ON_ONCE(!irqs_disabled());
534 __raw_spin_lock(&ftrace_max_lock);
535 for_each_tracing_cpu(i)
536 tracing_reset(max_tr.data[i]);
538 flip_trace(max_tr.data[cpu], data);
541 __update_max_tr(tr, tsk, cpu);
542 __raw_spin_unlock(&ftrace_max_lock);
546 * register_tracer - register a tracer with the ftrace system.
547 * @type - the plugin for the tracer
549 * Register a new plugin tracer.
551 int register_tracer(struct tracer *type)
558 pr_info("Tracer must have a name\n");
562 mutex_lock(&trace_types_lock);
563 for (t = trace_types; t; t = t->next) {
564 if (strcmp(type->name, t->name) == 0) {
566 pr_info("Trace %s already registered\n",
573 #ifdef CONFIG_FTRACE_STARTUP_TEST
574 if (type->selftest) {
575 struct tracer *saved_tracer = current_trace;
576 struct trace_array_cpu *data;
577 struct trace_array *tr = &global_trace;
578 int saved_ctrl = tr->ctrl;
581 * Run a selftest on this tracer.
582 * Here we reset the trace buffer, and set the current
583 * tracer to be this tracer. The tracer can then run some
584 * internal tracing to verify that everything is in order.
585 * If we fail, we do not register this tracer.
587 for_each_tracing_cpu(i) {
589 if (!head_page(data))
593 current_trace = type;
595 /* the test is responsible for initializing and enabling */
596 pr_info("Testing tracer %s: ", type->name);
597 ret = type->selftest(type, tr);
598 /* the test is responsible for resetting too */
599 current_trace = saved_tracer;
600 tr->ctrl = saved_ctrl;
602 printk(KERN_CONT "FAILED!\n");
605 /* Only reset on passing, to avoid touching corrupted buffers */
606 for_each_tracing_cpu(i) {
608 if (!head_page(data))
612 printk(KERN_CONT "PASSED\n");
616 type->next = trace_types;
618 len = strlen(type->name);
619 if (len > max_tracer_type_len)
620 max_tracer_type_len = len;
623 mutex_unlock(&trace_types_lock);
628 void unregister_tracer(struct tracer *type)
633 mutex_lock(&trace_types_lock);
634 for (t = &trace_types; *t; t = &(*t)->next) {
638 pr_info("Trace %s not registered\n", type->name);
643 if (strlen(type->name) != max_tracer_type_len)
646 max_tracer_type_len = 0;
647 for (t = &trace_types; *t; t = &(*t)->next) {
648 len = strlen((*t)->name);
649 if (len > max_tracer_type_len)
650 max_tracer_type_len = len;
653 mutex_unlock(&trace_types_lock);
656 void tracing_reset(struct trace_array_cpu *data)
660 data->trace_head = data->trace_tail = head_page(data);
661 data->trace_head_idx = 0;
662 data->trace_tail_idx = 0;
665 #define SAVED_CMDLINES 128
666 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
667 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
668 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
669 static int cmdline_idx;
670 static DEFINE_SPINLOCK(trace_cmdline_lock);
672 /* temporary disable recording */
673 atomic_t trace_record_cmdline_disabled __read_mostly;
675 static void trace_init_cmdlines(void)
677 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
678 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
682 void trace_stop_cmdline_recording(void);
684 static void trace_save_cmdline(struct task_struct *tsk)
689 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
693 * It's not the end of the world if we don't get
694 * the lock, but we also don't want to spin
695 * nor do we want to disable interrupts,
696 * so if we miss here, then better luck next time.
698 if (!spin_trylock(&trace_cmdline_lock))
701 idx = map_pid_to_cmdline[tsk->pid];
702 if (idx >= SAVED_CMDLINES) {
703 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
705 map = map_cmdline_to_pid[idx];
706 if (map <= PID_MAX_DEFAULT)
707 map_pid_to_cmdline[map] = (unsigned)-1;
709 map_pid_to_cmdline[tsk->pid] = idx;
714 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
716 spin_unlock(&trace_cmdline_lock);
719 static char *trace_find_cmdline(int pid)
721 char *cmdline = "<...>";
727 if (pid > PID_MAX_DEFAULT)
730 map = map_pid_to_cmdline[pid];
731 if (map >= SAVED_CMDLINES)
734 cmdline = saved_cmdlines[map];
740 void tracing_record_cmdline(struct task_struct *tsk)
742 if (atomic_read(&trace_record_cmdline_disabled))
745 trace_save_cmdline(tsk);
748 static inline struct list_head *
749 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
752 * Roundrobin - but skip the head (which is not a real page):
755 if (unlikely(next == &data->trace_pages))
757 BUG_ON(next == &data->trace_pages);
763 trace_next_page(struct trace_array_cpu *data, void *addr)
765 struct list_head *next;
768 page = virt_to_page(addr);
770 next = trace_next_list(data, &page->lru);
771 page = list_entry(next, struct page, lru);
773 return page_address(page);
776 static inline struct trace_entry *
777 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
779 unsigned long idx, idx_next;
780 struct trace_entry *entry;
783 idx = data->trace_head_idx;
786 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
788 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
790 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
791 data->trace_head = trace_next_page(data, data->trace_head);
795 if (data->trace_head == data->trace_tail &&
796 idx_next == data->trace_tail_idx) {
799 data->trace_tail_idx++;
800 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
802 trace_next_page(data, data->trace_tail);
803 data->trace_tail_idx = 0;
807 data->trace_head_idx = idx_next;
813 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
815 struct task_struct *tsk = current;
818 pc = preempt_count();
820 entry->preempt_count = pc & 0xff;
821 entry->pid = (tsk) ? tsk->pid : 0;
822 entry->t = ftrace_now(raw_smp_processor_id());
823 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
824 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
825 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
826 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
830 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
831 unsigned long ip, unsigned long parent_ip, unsigned long flags)
833 struct trace_entry *entry;
834 unsigned long irq_flags;
836 raw_local_irq_save(irq_flags);
837 __raw_spin_lock(&data->lock);
838 entry = tracing_get_trace_entry(tr, data);
839 tracing_generic_entry_update(entry, flags);
840 entry->type = TRACE_FN;
842 entry->fn.parent_ip = parent_ip;
843 __raw_spin_unlock(&data->lock);
844 raw_local_irq_restore(irq_flags);
848 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
849 unsigned long ip, unsigned long parent_ip, unsigned long flags)
851 if (likely(!atomic_read(&data->disabled)))
852 trace_function(tr, data, ip, parent_ip, flags);
855 #ifdef CONFIG_MMIOTRACE
856 void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
857 struct mmiotrace_rw *rw)
859 struct trace_entry *entry;
860 unsigned long irq_flags;
862 raw_local_irq_save(irq_flags);
863 __raw_spin_lock(&data->lock);
865 entry = tracing_get_trace_entry(tr, data);
866 tracing_generic_entry_update(entry, 0);
867 entry->type = TRACE_MMIO_RW;
870 __raw_spin_unlock(&data->lock);
871 raw_local_irq_restore(irq_flags);
876 void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
877 struct mmiotrace_map *map)
879 struct trace_entry *entry;
880 unsigned long irq_flags;
882 raw_local_irq_save(irq_flags);
883 __raw_spin_lock(&data->lock);
885 entry = tracing_get_trace_entry(tr, data);
886 tracing_generic_entry_update(entry, 0);
887 entry->type = TRACE_MMIO_MAP;
888 entry->mmiomap = *map;
890 __raw_spin_unlock(&data->lock);
891 raw_local_irq_restore(irq_flags);
897 void __trace_stack(struct trace_array *tr,
898 struct trace_array_cpu *data,
902 struct trace_entry *entry;
903 struct stack_trace trace;
905 if (!(trace_flags & TRACE_ITER_STACKTRACE))
908 entry = tracing_get_trace_entry(tr, data);
909 tracing_generic_entry_update(entry, flags);
910 entry->type = TRACE_STACK;
912 memset(&entry->stack, 0, sizeof(entry->stack));
914 trace.nr_entries = 0;
915 trace.max_entries = FTRACE_STACK_ENTRIES;
917 trace.entries = entry->stack.caller;
919 save_stack_trace(&trace);
923 __trace_special(void *__tr, void *__data,
924 unsigned long arg1, unsigned long arg2, unsigned long arg3)
926 struct trace_array_cpu *data = __data;
927 struct trace_array *tr = __tr;
928 struct trace_entry *entry;
929 unsigned long irq_flags;
931 raw_local_irq_save(irq_flags);
932 __raw_spin_lock(&data->lock);
933 entry = tracing_get_trace_entry(tr, data);
934 tracing_generic_entry_update(entry, 0);
935 entry->type = TRACE_SPECIAL;
936 entry->special.arg1 = arg1;
937 entry->special.arg2 = arg2;
938 entry->special.arg3 = arg3;
939 __trace_stack(tr, data, irq_flags, 4);
940 __raw_spin_unlock(&data->lock);
941 raw_local_irq_restore(irq_flags);
947 tracing_sched_switch_trace(struct trace_array *tr,
948 struct trace_array_cpu *data,
949 struct task_struct *prev,
950 struct task_struct *next,
953 struct trace_entry *entry;
954 unsigned long irq_flags;
956 raw_local_irq_save(irq_flags);
957 __raw_spin_lock(&data->lock);
958 entry = tracing_get_trace_entry(tr, data);
959 tracing_generic_entry_update(entry, flags);
960 entry->type = TRACE_CTX;
961 entry->ctx.prev_pid = prev->pid;
962 entry->ctx.prev_prio = prev->prio;
963 entry->ctx.prev_state = prev->state;
964 entry->ctx.next_pid = next->pid;
965 entry->ctx.next_prio = next->prio;
966 entry->ctx.next_state = next->state;
967 __trace_stack(tr, data, flags, 5);
968 __raw_spin_unlock(&data->lock);
969 raw_local_irq_restore(irq_flags);
973 tracing_sched_wakeup_trace(struct trace_array *tr,
974 struct trace_array_cpu *data,
975 struct task_struct *wakee,
976 struct task_struct *curr,
979 struct trace_entry *entry;
980 unsigned long irq_flags;
982 raw_local_irq_save(irq_flags);
983 __raw_spin_lock(&data->lock);
984 entry = tracing_get_trace_entry(tr, data);
985 tracing_generic_entry_update(entry, flags);
986 entry->type = TRACE_WAKE;
987 entry->ctx.prev_pid = curr->pid;
988 entry->ctx.prev_prio = curr->prio;
989 entry->ctx.prev_state = curr->state;
990 entry->ctx.next_pid = wakee->pid;
991 entry->ctx.next_prio = wakee->prio;
992 entry->ctx.next_state = wakee->state;
993 __trace_stack(tr, data, flags, 6);
994 __raw_spin_unlock(&data->lock);
995 raw_local_irq_restore(irq_flags);
1001 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1003 struct trace_array *tr = &global_trace;
1004 struct trace_array_cpu *data;
1005 unsigned long flags;
1009 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1012 local_irq_save(flags);
1013 cpu = raw_smp_processor_id();
1014 data = tr->data[cpu];
1015 disabled = atomic_inc_return(&data->disabled);
1017 if (likely(disabled == 1))
1018 __trace_special(tr, data, arg1, arg2, arg3);
1020 atomic_dec(&data->disabled);
1021 local_irq_restore(flags);
1024 #ifdef CONFIG_FTRACE
1026 function_trace_call(unsigned long ip, unsigned long parent_ip)
1028 struct trace_array *tr = &global_trace;
1029 struct trace_array_cpu *data;
1030 unsigned long flags;
1034 if (unlikely(!ftrace_function_enabled))
1040 local_irq_save(flags);
1041 cpu = raw_smp_processor_id();
1042 data = tr->data[cpu];
1043 disabled = atomic_inc_return(&data->disabled);
1045 if (likely(disabled == 1))
1046 trace_function(tr, data, ip, parent_ip, flags);
1048 atomic_dec(&data->disabled);
1049 local_irq_restore(flags);
1052 static struct ftrace_ops trace_ops __read_mostly =
1054 .func = function_trace_call,
1057 void tracing_start_function_trace(void)
1059 ftrace_function_enabled = 0;
1060 register_ftrace_function(&trace_ops);
1062 ftrace_function_enabled = 1;
1065 void tracing_stop_function_trace(void)
1067 ftrace_function_enabled = 0;
1068 unregister_ftrace_function(&trace_ops);
1072 enum trace_file_type {
1073 TRACE_FILE_LAT_FMT = 1,
1076 static struct trace_entry *
1077 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1078 struct trace_iterator *iter, int cpu)
1081 struct trace_entry *array;
1083 if (iter->next_idx[cpu] >= tr->entries ||
1084 iter->next_idx[cpu] >= data->trace_idx ||
1085 (data->trace_head == data->trace_tail &&
1086 data->trace_head_idx == data->trace_tail_idx))
1089 if (!iter->next_page[cpu]) {
1090 /* Initialize the iterator for this cpu trace buffer */
1091 WARN_ON(!data->trace_tail);
1092 page = virt_to_page(data->trace_tail);
1093 iter->next_page[cpu] = &page->lru;
1094 iter->next_page_idx[cpu] = data->trace_tail_idx;
1097 page = list_entry(iter->next_page[cpu], struct page, lru);
1098 BUG_ON(&data->trace_pages == &page->lru);
1100 array = page_address(page);
1102 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1103 return &array[iter->next_page_idx[cpu]];
1106 static struct trace_entry *
1107 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1109 struct trace_array *tr = iter->tr;
1110 struct trace_entry *ent, *next = NULL;
1114 for_each_tracing_cpu(cpu) {
1115 if (!head_page(tr->data[cpu]))
1117 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1119 * Pick the entry with the smallest timestamp:
1121 if (ent && (!next || ent->t < next->t)) {
1128 *ent_cpu = next_cpu;
1133 static void trace_iterator_increment(struct trace_iterator *iter)
1136 iter->next_idx[iter->cpu]++;
1137 iter->next_page_idx[iter->cpu]++;
1139 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1140 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1142 iter->next_page_idx[iter->cpu] = 0;
1143 iter->next_page[iter->cpu] =
1144 trace_next_list(data, iter->next_page[iter->cpu]);
1148 static void trace_consume(struct trace_iterator *iter)
1150 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1152 data->trace_tail_idx++;
1153 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1154 data->trace_tail = trace_next_page(data, data->trace_tail);
1155 data->trace_tail_idx = 0;
1158 /* Check if we empty it, then reset the index */
1159 if (data->trace_head == data->trace_tail &&
1160 data->trace_head_idx == data->trace_tail_idx)
1161 data->trace_idx = 0;
1164 static void *find_next_entry_inc(struct trace_iterator *iter)
1166 struct trace_entry *next;
1169 next = find_next_entry(iter, &next_cpu);
1171 iter->prev_ent = iter->ent;
1172 iter->prev_cpu = iter->cpu;
1175 iter->cpu = next_cpu;
1178 trace_iterator_increment(iter);
1180 return next ? iter : NULL;
1183 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1185 struct trace_iterator *iter = m->private;
1186 void *last_ent = iter->ent;
1192 /* can't go backwards */
1197 ent = find_next_entry_inc(iter);
1201 while (ent && iter->idx < i)
1202 ent = find_next_entry_inc(iter);
1206 if (last_ent && !ent)
1207 seq_puts(m, "\n\nvim:ft=help\n");
1212 static void *s_start(struct seq_file *m, loff_t *pos)
1214 struct trace_iterator *iter = m->private;
1219 mutex_lock(&trace_types_lock);
1221 if (!current_trace || current_trace != iter->trace) {
1222 mutex_unlock(&trace_types_lock);
1226 atomic_inc(&trace_record_cmdline_disabled);
1228 /* let the tracer grab locks here if needed */
1229 if (current_trace->start)
1230 current_trace->start(iter);
1232 if (*pos != iter->pos) {
1236 iter->prev_ent = NULL;
1237 iter->prev_cpu = -1;
1239 for_each_tracing_cpu(i) {
1240 iter->next_idx[i] = 0;
1241 iter->next_page[i] = NULL;
1244 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1249 p = s_next(m, p, &l);
1255 static void s_stop(struct seq_file *m, void *p)
1257 struct trace_iterator *iter = m->private;
1259 atomic_dec(&trace_record_cmdline_disabled);
1261 /* let the tracer release locks here if needed */
1262 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1263 iter->trace->stop(iter);
1265 mutex_unlock(&trace_types_lock);
1268 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1270 #ifdef CONFIG_KRETPROBES
1271 static inline int kretprobed(unsigned long addr)
1273 return addr == (unsigned long)kretprobe_trampoline;
1276 static inline int kretprobed(unsigned long addr)
1280 #endif /* CONFIG_KRETPROBES */
1283 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1285 #ifdef CONFIG_KALLSYMS
1286 char str[KSYM_SYMBOL_LEN];
1288 kallsyms_lookup(address, NULL, NULL, NULL, str);
1290 return trace_seq_printf(s, fmt, str);
1296 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1297 unsigned long address)
1299 #ifdef CONFIG_KALLSYMS
1300 char str[KSYM_SYMBOL_LEN];
1302 sprint_symbol(str, address);
1303 return trace_seq_printf(s, fmt, str);
1308 #ifndef CONFIG_64BIT
1309 # define IP_FMT "%08lx"
1311 # define IP_FMT "%016lx"
1315 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1320 return trace_seq_printf(s, "0");
1322 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1323 ret = seq_print_sym_offset(s, "%s", ip);
1325 ret = seq_print_sym_short(s, "%s", ip);
1330 if (sym_flags & TRACE_ITER_SYM_ADDR)
1331 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1335 static void print_lat_help_header(struct seq_file *m)
1337 seq_puts(m, "# _------=> CPU# \n");
1338 seq_puts(m, "# / _-----=> irqs-off \n");
1339 seq_puts(m, "# | / _----=> need-resched \n");
1340 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1341 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1342 seq_puts(m, "# |||| / \n");
1343 seq_puts(m, "# ||||| delay \n");
1344 seq_puts(m, "# cmd pid ||||| time | caller \n");
1345 seq_puts(m, "# \\ / ||||| \\ | / \n");
1348 static void print_func_help_header(struct seq_file *m)
1350 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1351 seq_puts(m, "# | | | | |\n");
1356 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1358 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1359 struct trace_array *tr = iter->tr;
1360 struct trace_array_cpu *data = tr->data[tr->cpu];
1361 struct tracer *type = current_trace;
1362 unsigned long total = 0;
1363 unsigned long entries = 0;
1365 const char *name = "preemption";
1370 for_each_tracing_cpu(cpu) {
1371 if (head_page(tr->data[cpu])) {
1372 total += tr->data[cpu]->trace_idx;
1373 if (tr->data[cpu]->trace_idx > tr->entries)
1374 entries += tr->entries;
1376 entries += tr->data[cpu]->trace_idx;
1380 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1382 seq_puts(m, "-----------------------------------"
1383 "---------------------------------\n");
1384 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1385 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1386 nsecs_to_usecs(data->saved_latency),
1390 #if defined(CONFIG_PREEMPT_NONE)
1392 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1394 #elif defined(CONFIG_PREEMPT)
1399 /* These are reserved for later use */
1402 seq_printf(m, " #P:%d)\n", num_online_cpus());
1406 seq_puts(m, " -----------------\n");
1407 seq_printf(m, " | task: %.16s-%d "
1408 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1409 data->comm, data->pid, data->uid, data->nice,
1410 data->policy, data->rt_priority);
1411 seq_puts(m, " -----------------\n");
1413 if (data->critical_start) {
1414 seq_puts(m, " => started at: ");
1415 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1416 trace_print_seq(m, &iter->seq);
1417 seq_puts(m, "\n => ended at: ");
1418 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1419 trace_print_seq(m, &iter->seq);
1427 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1429 int hardirq, softirq;
1432 comm = trace_find_cmdline(entry->pid);
1434 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1435 trace_seq_printf(s, "%d", cpu);
1436 trace_seq_printf(s, "%c%c",
1437 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1438 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1440 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1441 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1442 if (hardirq && softirq) {
1443 trace_seq_putc(s, 'H');
1446 trace_seq_putc(s, 'h');
1449 trace_seq_putc(s, 's');
1451 trace_seq_putc(s, '.');
1455 if (entry->preempt_count)
1456 trace_seq_printf(s, "%x", entry->preempt_count);
1458 trace_seq_puts(s, ".");
1461 unsigned long preempt_mark_thresh = 100;
1464 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1465 unsigned long rel_usecs)
1467 trace_seq_printf(s, " %4lldus", abs_usecs);
1468 if (rel_usecs > preempt_mark_thresh)
1469 trace_seq_puts(s, "!: ");
1470 else if (rel_usecs > 1)
1471 trace_seq_puts(s, "+: ");
1473 trace_seq_puts(s, " : ");
1476 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1479 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1481 struct trace_seq *s = &iter->seq;
1482 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1483 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1484 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1485 struct trace_entry *entry = iter->ent;
1486 unsigned long abs_usecs;
1487 unsigned long rel_usecs;
1495 rel_usecs = ns2usecs(next_entry->t - entry->t);
1496 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1499 comm = trace_find_cmdline(entry->pid);
1500 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1501 " %ld.%03ldms (+%ld.%03ldms): ",
1503 entry->pid, cpu, entry->flags,
1504 entry->preempt_count, trace_idx,
1507 abs_usecs % 1000, rel_usecs/1000,
1510 lat_print_generic(s, entry, cpu);
1511 lat_print_timestamp(s, abs_usecs, rel_usecs);
1513 switch (entry->type) {
1515 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1516 trace_seq_puts(s, " (");
1517 if (kretprobed(entry->fn.parent_ip))
1518 trace_seq_puts(s, KRETPROBE_MSG);
1520 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1521 trace_seq_puts(s, ")\n");
1525 T = entry->ctx.next_state < sizeof(state_to_char) ?
1526 state_to_char[entry->ctx.next_state] : 'X';
1528 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1529 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1530 comm = trace_find_cmdline(entry->ctx.next_pid);
1531 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1532 entry->ctx.prev_pid,
1533 entry->ctx.prev_prio,
1534 S, entry->type == TRACE_CTX ? "==>" : " +",
1535 entry->ctx.next_pid,
1536 entry->ctx.next_prio,
1540 trace_seq_printf(s, "# %ld %ld %ld\n",
1541 entry->special.arg1,
1542 entry->special.arg2,
1543 entry->special.arg3);
1546 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1548 trace_seq_puts(s, " <= ");
1549 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1551 trace_seq_puts(s, "\n");
1554 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1559 static int print_trace_fmt(struct trace_iterator *iter)
1561 struct trace_seq *s = &iter->seq;
1562 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1563 struct trace_entry *entry;
1564 unsigned long usec_rem;
1565 unsigned long long t;
1574 comm = trace_find_cmdline(iter->ent->pid);
1576 t = ns2usecs(entry->t);
1577 usec_rem = do_div(t, 1000000ULL);
1578 secs = (unsigned long)t;
1580 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1583 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1586 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1590 switch (entry->type) {
1592 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1595 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1596 entry->fn.parent_ip) {
1597 ret = trace_seq_printf(s, " <-");
1600 if (kretprobed(entry->fn.parent_ip))
1601 ret = trace_seq_puts(s, KRETPROBE_MSG);
1603 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1608 ret = trace_seq_printf(s, "\n");
1614 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1615 state_to_char[entry->ctx.prev_state] : 'X';
1616 T = entry->ctx.next_state < sizeof(state_to_char) ?
1617 state_to_char[entry->ctx.next_state] : 'X';
1618 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1619 entry->ctx.prev_pid,
1620 entry->ctx.prev_prio,
1622 entry->type == TRACE_CTX ? "==>" : " +",
1623 entry->ctx.next_pid,
1624 entry->ctx.next_prio,
1630 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1631 entry->special.arg1,
1632 entry->special.arg2,
1633 entry->special.arg3);
1638 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1640 ret = trace_seq_puts(s, " <= ");
1644 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1649 ret = trace_seq_puts(s, "\n");
1657 static int print_raw_fmt(struct trace_iterator *iter)
1659 struct trace_seq *s = &iter->seq;
1660 struct trace_entry *entry;
1666 ret = trace_seq_printf(s, "%d %d %llu ",
1667 entry->pid, iter->cpu, entry->t);
1671 switch (entry->type) {
1673 ret = trace_seq_printf(s, "%x %x\n",
1674 entry->fn.ip, entry->fn.parent_ip);
1680 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1681 state_to_char[entry->ctx.prev_state] : 'X';
1682 T = entry->ctx.next_state < sizeof(state_to_char) ?
1683 state_to_char[entry->ctx.next_state] : 'X';
1684 if (entry->type == TRACE_WAKE)
1686 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1687 entry->ctx.prev_pid,
1688 entry->ctx.prev_prio,
1690 entry->ctx.next_pid,
1691 entry->ctx.next_prio,
1698 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1699 entry->special.arg1,
1700 entry->special.arg2,
1701 entry->special.arg3);
1709 #define SEQ_PUT_FIELD_RET(s, x) \
1711 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1715 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1717 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1721 static int print_hex_fmt(struct trace_iterator *iter)
1723 struct trace_seq *s = &iter->seq;
1724 unsigned char newline = '\n';
1725 struct trace_entry *entry;
1730 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1731 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1732 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1734 switch (entry->type) {
1736 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1737 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1741 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1742 state_to_char[entry->ctx.prev_state] : 'X';
1743 T = entry->ctx.next_state < sizeof(state_to_char) ?
1744 state_to_char[entry->ctx.next_state] : 'X';
1745 if (entry->type == TRACE_WAKE)
1747 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1748 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1749 SEQ_PUT_HEX_FIELD_RET(s, S);
1750 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1751 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1752 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1753 SEQ_PUT_HEX_FIELD_RET(s, T);
1757 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1758 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1759 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1762 SEQ_PUT_FIELD_RET(s, newline);
1767 static int print_bin_fmt(struct trace_iterator *iter)
1769 struct trace_seq *s = &iter->seq;
1770 struct trace_entry *entry;
1774 SEQ_PUT_FIELD_RET(s, entry->pid);
1775 SEQ_PUT_FIELD_RET(s, entry->cpu);
1776 SEQ_PUT_FIELD_RET(s, entry->t);
1778 switch (entry->type) {
1780 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1781 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1784 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1785 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1786 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1787 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1788 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1789 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1793 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1794 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1795 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1801 static int trace_empty(struct trace_iterator *iter)
1803 struct trace_array_cpu *data;
1806 for_each_tracing_cpu(cpu) {
1807 data = iter->tr->data[cpu];
1809 if (head_page(data) && data->trace_idx &&
1810 (data->trace_tail != data->trace_head ||
1811 data->trace_tail_idx != data->trace_head_idx))
1817 static int print_trace_line(struct trace_iterator *iter)
1819 if (iter->trace && iter->trace->print_line)
1820 return iter->trace->print_line(iter);
1822 if (trace_flags & TRACE_ITER_BIN)
1823 return print_bin_fmt(iter);
1825 if (trace_flags & TRACE_ITER_HEX)
1826 return print_hex_fmt(iter);
1828 if (trace_flags & TRACE_ITER_RAW)
1829 return print_raw_fmt(iter);
1831 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1832 return print_lat_fmt(iter, iter->idx, iter->cpu);
1834 return print_trace_fmt(iter);
1837 static int s_show(struct seq_file *m, void *v)
1839 struct trace_iterator *iter = v;
1841 if (iter->ent == NULL) {
1843 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1846 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1847 /* print nothing if the buffers are empty */
1848 if (trace_empty(iter))
1850 print_trace_header(m, iter);
1851 if (!(trace_flags & TRACE_ITER_VERBOSE))
1852 print_lat_help_header(m);
1854 if (!(trace_flags & TRACE_ITER_VERBOSE))
1855 print_func_help_header(m);
1858 print_trace_line(iter);
1859 trace_print_seq(m, &iter->seq);
1865 static struct seq_operations tracer_seq_ops = {
1872 static struct trace_iterator *
1873 __tracing_open(struct inode *inode, struct file *file, int *ret)
1875 struct trace_iterator *iter;
1877 if (tracing_disabled) {
1882 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1888 mutex_lock(&trace_types_lock);
1889 if (current_trace && current_trace->print_max)
1892 iter->tr = inode->i_private;
1893 iter->trace = current_trace;
1896 /* TODO stop tracer */
1897 *ret = seq_open(file, &tracer_seq_ops);
1899 struct seq_file *m = file->private_data;
1902 /* stop the trace while dumping */
1903 if (iter->tr->ctrl) {
1905 ftrace_function_enabled = 0;
1908 if (iter->trace && iter->trace->open)
1909 iter->trace->open(iter);
1914 mutex_unlock(&trace_types_lock);
1920 int tracing_open_generic(struct inode *inode, struct file *filp)
1922 if (tracing_disabled)
1925 filp->private_data = inode->i_private;
1929 int tracing_release(struct inode *inode, struct file *file)
1931 struct seq_file *m = (struct seq_file *)file->private_data;
1932 struct trace_iterator *iter = m->private;
1934 mutex_lock(&trace_types_lock);
1935 if (iter->trace && iter->trace->close)
1936 iter->trace->close(iter);
1938 /* reenable tracing if it was previously enabled */
1939 if (iter->tr->ctrl) {
1942 * It is safe to enable function tracing even if it
1945 ftrace_function_enabled = 1;
1947 mutex_unlock(&trace_types_lock);
1949 seq_release(inode, file);
1954 static int tracing_open(struct inode *inode, struct file *file)
1958 __tracing_open(inode, file, &ret);
1963 static int tracing_lt_open(struct inode *inode, struct file *file)
1965 struct trace_iterator *iter;
1968 iter = __tracing_open(inode, file, &ret);
1971 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1978 t_next(struct seq_file *m, void *v, loff_t *pos)
1980 struct tracer *t = m->private;
1992 static void *t_start(struct seq_file *m, loff_t *pos)
1994 struct tracer *t = m->private;
1997 mutex_lock(&trace_types_lock);
1998 for (; t && l < *pos; t = t_next(m, t, &l))
2004 static void t_stop(struct seq_file *m, void *p)
2006 mutex_unlock(&trace_types_lock);
2009 static int t_show(struct seq_file *m, void *v)
2011 struct tracer *t = v;
2016 seq_printf(m, "%s", t->name);
2025 static struct seq_operations show_traces_seq_ops = {
2032 static int show_traces_open(struct inode *inode, struct file *file)
2036 if (tracing_disabled)
2039 ret = seq_open(file, &show_traces_seq_ops);
2041 struct seq_file *m = file->private_data;
2042 m->private = trace_types;
2048 static struct file_operations tracing_fops = {
2049 .open = tracing_open,
2051 .llseek = seq_lseek,
2052 .release = tracing_release,
2055 static struct file_operations tracing_lt_fops = {
2056 .open = tracing_lt_open,
2058 .llseek = seq_lseek,
2059 .release = tracing_release,
2062 static struct file_operations show_traces_fops = {
2063 .open = show_traces_open,
2065 .release = seq_release,
2069 * Only trace on a CPU if the bitmask is set:
2071 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2074 * When tracing/tracing_cpu_mask is modified then this holds
2075 * the new bitmask we are about to install:
2077 static cpumask_t tracing_cpumask_new;
2080 * The tracer itself will not take this lock, but still we want
2081 * to provide a consistent cpumask to user-space:
2083 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2086 * Temporary storage for the character representation of the
2087 * CPU bitmask (and one more byte for the newline):
2089 static char mask_str[NR_CPUS + 1];
2092 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2093 size_t count, loff_t *ppos)
2097 mutex_lock(&tracing_cpumask_update_lock);
2099 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2100 if (count - len < 2) {
2104 len += sprintf(mask_str + len, "\n");
2105 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2108 mutex_unlock(&tracing_cpumask_update_lock);
2114 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2115 size_t count, loff_t *ppos)
2119 mutex_lock(&tracing_cpumask_update_lock);
2120 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2124 raw_local_irq_disable();
2125 __raw_spin_lock(&ftrace_max_lock);
2126 for_each_tracing_cpu(cpu) {
2128 * Increase/decrease the disabled counter if we are
2129 * about to flip a bit in the cpumask:
2131 if (cpu_isset(cpu, tracing_cpumask) &&
2132 !cpu_isset(cpu, tracing_cpumask_new)) {
2133 atomic_inc(&global_trace.data[cpu]->disabled);
2135 if (!cpu_isset(cpu, tracing_cpumask) &&
2136 cpu_isset(cpu, tracing_cpumask_new)) {
2137 atomic_dec(&global_trace.data[cpu]->disabled);
2140 __raw_spin_unlock(&ftrace_max_lock);
2141 raw_local_irq_enable();
2143 tracing_cpumask = tracing_cpumask_new;
2145 mutex_unlock(&tracing_cpumask_update_lock);
2150 mutex_unlock(&tracing_cpumask_update_lock);
2155 static struct file_operations tracing_cpumask_fops = {
2156 .open = tracing_open_generic,
2157 .read = tracing_cpumask_read,
2158 .write = tracing_cpumask_write,
2162 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2163 size_t cnt, loff_t *ppos)
2170 /* calulate max size */
2171 for (i = 0; trace_options[i]; i++) {
2172 len += strlen(trace_options[i]);
2173 len += 3; /* "no" and space */
2176 /* +2 for \n and \0 */
2177 buf = kmalloc(len + 2, GFP_KERNEL);
2181 for (i = 0; trace_options[i]; i++) {
2182 if (trace_flags & (1 << i))
2183 r += sprintf(buf + r, "%s ", trace_options[i]);
2185 r += sprintf(buf + r, "no%s ", trace_options[i]);
2188 r += sprintf(buf + r, "\n");
2189 WARN_ON(r >= len + 2);
2191 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2199 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2200 size_t cnt, loff_t *ppos)
2207 if (cnt >= sizeof(buf))
2210 if (copy_from_user(&buf, ubuf, cnt))
2215 if (strncmp(buf, "no", 2) == 0) {
2220 for (i = 0; trace_options[i]; i++) {
2221 int len = strlen(trace_options[i]);
2223 if (strncmp(cmp, trace_options[i], len) == 0) {
2225 trace_flags &= ~(1 << i);
2227 trace_flags |= (1 << i);
2232 * If no option could be set, return an error:
2234 if (!trace_options[i])
2242 static struct file_operations tracing_iter_fops = {
2243 .open = tracing_open_generic,
2244 .read = tracing_iter_ctrl_read,
2245 .write = tracing_iter_ctrl_write,
2248 static const char readme_msg[] =
2249 "tracing mini-HOWTO:\n\n"
2251 "# mount -t debugfs nodev /debug\n\n"
2252 "# cat /debug/tracing/available_tracers\n"
2253 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2254 "# cat /debug/tracing/current_tracer\n"
2256 "# echo sched_switch > /debug/tracing/current_tracer\n"
2257 "# cat /debug/tracing/current_tracer\n"
2259 "# cat /debug/tracing/iter_ctrl\n"
2260 "noprint-parent nosym-offset nosym-addr noverbose\n"
2261 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2262 "# echo 1 > /debug/tracing/tracing_enabled\n"
2263 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2264 "echo 0 > /debug/tracing/tracing_enabled\n"
2268 tracing_readme_read(struct file *filp, char __user *ubuf,
2269 size_t cnt, loff_t *ppos)
2271 return simple_read_from_buffer(ubuf, cnt, ppos,
2272 readme_msg, strlen(readme_msg));
2275 static struct file_operations tracing_readme_fops = {
2276 .open = tracing_open_generic,
2277 .read = tracing_readme_read,
2281 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2282 size_t cnt, loff_t *ppos)
2284 struct trace_array *tr = filp->private_data;
2288 r = sprintf(buf, "%ld\n", tr->ctrl);
2289 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2293 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2294 size_t cnt, loff_t *ppos)
2296 struct trace_array *tr = filp->private_data;
2301 if (cnt >= sizeof(buf))
2304 if (copy_from_user(&buf, ubuf, cnt))
2309 ret = strict_strtoul(buf, 10, &val);
2315 mutex_lock(&trace_types_lock);
2316 if (tr->ctrl ^ val) {
2324 if (current_trace && current_trace->ctrl_update)
2325 current_trace->ctrl_update(tr);
2327 mutex_unlock(&trace_types_lock);
2335 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2338 char buf[max_tracer_type_len+2];
2341 mutex_lock(&trace_types_lock);
2343 r = sprintf(buf, "%s\n", current_trace->name);
2345 r = sprintf(buf, "\n");
2346 mutex_unlock(&trace_types_lock);
2348 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2352 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2353 size_t cnt, loff_t *ppos)
2355 struct trace_array *tr = &global_trace;
2357 char buf[max_tracer_type_len+1];
2360 if (cnt > max_tracer_type_len)
2361 cnt = max_tracer_type_len;
2363 if (copy_from_user(&buf, ubuf, cnt))
2368 /* strip ending whitespace. */
2369 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2372 mutex_lock(&trace_types_lock);
2373 for (t = trace_types; t; t = t->next) {
2374 if (strcmp(t->name, buf) == 0)
2377 if (!t || t == current_trace)
2380 if (current_trace && current_trace->reset)
2381 current_trace->reset(tr);
2388 mutex_unlock(&trace_types_lock);
2396 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2397 size_t cnt, loff_t *ppos)
2399 unsigned long *ptr = filp->private_data;
2403 r = snprintf(buf, sizeof(buf), "%ld\n",
2404 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2405 if (r > sizeof(buf))
2407 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2411 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2412 size_t cnt, loff_t *ppos)
2414 long *ptr = filp->private_data;
2419 if (cnt >= sizeof(buf))
2422 if (copy_from_user(&buf, ubuf, cnt))
2427 ret = strict_strtoul(buf, 10, &val);
2436 static atomic_t tracing_reader;
2438 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2440 struct trace_iterator *iter;
2442 if (tracing_disabled)
2445 /* We only allow for reader of the pipe */
2446 if (atomic_inc_return(&tracing_reader) != 1) {
2447 atomic_dec(&tracing_reader);
2451 /* create a buffer to store the information to pass to userspace */
2452 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2456 mutex_lock(&trace_types_lock);
2457 iter->tr = &global_trace;
2458 iter->trace = current_trace;
2459 filp->private_data = iter;
2461 if (iter->trace->pipe_open)
2462 iter->trace->pipe_open(iter);
2463 mutex_unlock(&trace_types_lock);
2468 static int tracing_release_pipe(struct inode *inode, struct file *file)
2470 struct trace_iterator *iter = file->private_data;
2473 atomic_dec(&tracing_reader);
2479 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2481 struct trace_iterator *iter = filp->private_data;
2483 if (trace_flags & TRACE_ITER_BLOCK) {
2485 * Always select as readable when in blocking mode
2487 return POLLIN | POLLRDNORM;
2489 if (!trace_empty(iter))
2490 return POLLIN | POLLRDNORM;
2491 poll_wait(filp, &trace_wait, poll_table);
2492 if (!trace_empty(iter))
2493 return POLLIN | POLLRDNORM;
2503 tracing_read_pipe(struct file *filp, char __user *ubuf,
2504 size_t cnt, loff_t *ppos)
2506 struct trace_iterator *iter = filp->private_data;
2507 struct trace_array_cpu *data;
2508 static cpumask_t mask;
2509 unsigned long flags;
2510 #ifdef CONFIG_FTRACE
2516 /* return any leftover data */
2517 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2522 trace_seq_reset(&iter->seq);
2524 mutex_lock(&trace_types_lock);
2525 if (iter->trace->read) {
2526 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2531 while (trace_empty(iter)) {
2533 if ((filp->f_flags & O_NONBLOCK)) {
2539 * This is a make-shift waitqueue. The reason we don't use
2540 * an actual wait queue is because:
2541 * 1) we only ever have one waiter
2542 * 2) the tracing, traces all functions, we don't want
2543 * the overhead of calling wake_up and friends
2544 * (and tracing them too)
2545 * Anyway, this is really very primitive wakeup.
2547 set_current_state(TASK_INTERRUPTIBLE);
2548 iter->tr->waiter = current;
2550 mutex_unlock(&trace_types_lock);
2552 /* sleep for 100 msecs, and try again. */
2553 schedule_timeout(HZ/10);
2555 mutex_lock(&trace_types_lock);
2557 iter->tr->waiter = NULL;
2559 if (signal_pending(current)) {
2564 if (iter->trace != current_trace)
2568 * We block until we read something and tracing is disabled.
2569 * We still block if tracing is disabled, but we have never
2570 * read anything. This allows a user to cat this file, and
2571 * then enable tracing. But after we have read something,
2572 * we give an EOF when tracing is again disabled.
2574 * iter->pos will be 0 if we haven't read anything.
2576 if (!tracer_enabled && iter->pos)
2582 /* stop when tracing is finished */
2583 if (trace_empty(iter))
2586 if (cnt >= PAGE_SIZE)
2587 cnt = PAGE_SIZE - 1;
2589 /* reset all but tr, trace, and overruns */
2590 memset(&iter->seq, 0,
2591 sizeof(struct trace_iterator) -
2592 offsetof(struct trace_iterator, seq));
2596 * We need to stop all tracing on all CPUS to read the
2597 * the next buffer. This is a bit expensive, but is
2598 * not done often. We fill all what we can read,
2599 * and then release the locks again.
2603 local_irq_save(flags);
2604 #ifdef CONFIG_FTRACE
2605 ftrace_save = ftrace_enabled;
2609 for_each_tracing_cpu(cpu) {
2610 data = iter->tr->data[cpu];
2612 if (!head_page(data) || !data->trace_idx)
2615 atomic_inc(&data->disabled);
2619 for_each_cpu_mask(cpu, mask) {
2620 data = iter->tr->data[cpu];
2621 __raw_spin_lock(&data->lock);
2623 if (data->overrun > iter->last_overrun[cpu])
2624 iter->overrun[cpu] +=
2625 data->overrun - iter->last_overrun[cpu];
2626 iter->last_overrun[cpu] = data->overrun;
2629 while (find_next_entry_inc(iter) != NULL) {
2631 int len = iter->seq.len;
2633 ret = print_trace_line(iter);
2635 /* don't print partial lines */
2636 iter->seq.len = len;
2640 trace_consume(iter);
2642 if (iter->seq.len >= cnt)
2646 for_each_cpu_mask(cpu, mask) {
2647 data = iter->tr->data[cpu];
2648 __raw_spin_unlock(&data->lock);
2651 for_each_cpu_mask(cpu, mask) {
2652 data = iter->tr->data[cpu];
2653 atomic_dec(&data->disabled);
2655 #ifdef CONFIG_FTRACE
2656 ftrace_enabled = ftrace_save;
2658 local_irq_restore(flags);
2660 /* Now copy what we have to the user */
2661 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2662 if (iter->seq.readpos >= iter->seq.len)
2663 trace_seq_reset(&iter->seq);
2668 mutex_unlock(&trace_types_lock);
2674 tracing_entries_read(struct file *filp, char __user *ubuf,
2675 size_t cnt, loff_t *ppos)
2677 struct trace_array *tr = filp->private_data;
2681 r = sprintf(buf, "%lu\n", tr->entries);
2682 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2686 tracing_entries_write(struct file *filp, const char __user *ubuf,
2687 size_t cnt, loff_t *ppos)
2693 if (cnt >= sizeof(buf))
2696 if (copy_from_user(&buf, ubuf, cnt))
2701 ret = strict_strtoul(buf, 10, &val);
2705 /* must have at least 1 entry */
2709 mutex_lock(&trace_types_lock);
2711 if (current_trace != &no_tracer) {
2713 pr_info("ftrace: set current_tracer to none"
2714 " before modifying buffer size\n");
2718 if (val > global_trace.entries) {
2719 long pages_requested;
2720 unsigned long freeable_pages;
2722 /* make sure we have enough memory before mapping */
2724 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2726 /* account for each buffer (and max_tr) */
2727 pages_requested *= tracing_nr_buffers * 2;
2729 /* Check for overflow */
2730 if (pages_requested < 0) {
2735 freeable_pages = determine_dirtyable_memory();
2737 /* we only allow to request 1/4 of useable memory */
2738 if (pages_requested >
2739 ((freeable_pages + tracing_pages_allocated) / 4)) {
2744 while (global_trace.entries < val) {
2745 if (trace_alloc_page()) {
2749 /* double check that we don't go over the known pages */
2750 if (tracing_pages_allocated > pages_requested)
2755 /* include the number of entries in val (inc of page entries) */
2756 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2760 /* check integrity */
2761 for_each_tracing_cpu(i)
2762 check_pages(global_trace.data[i]);
2766 /* If check pages failed, return ENOMEM */
2767 if (tracing_disabled)
2770 max_tr.entries = global_trace.entries;
2771 mutex_unlock(&trace_types_lock);
2776 static struct file_operations tracing_max_lat_fops = {
2777 .open = tracing_open_generic,
2778 .read = tracing_max_lat_read,
2779 .write = tracing_max_lat_write,
2782 static struct file_operations tracing_ctrl_fops = {
2783 .open = tracing_open_generic,
2784 .read = tracing_ctrl_read,
2785 .write = tracing_ctrl_write,
2788 static struct file_operations set_tracer_fops = {
2789 .open = tracing_open_generic,
2790 .read = tracing_set_trace_read,
2791 .write = tracing_set_trace_write,
2794 static struct file_operations tracing_pipe_fops = {
2795 .open = tracing_open_pipe,
2796 .poll = tracing_poll_pipe,
2797 .read = tracing_read_pipe,
2798 .release = tracing_release_pipe,
2801 static struct file_operations tracing_entries_fops = {
2802 .open = tracing_open_generic,
2803 .read = tracing_entries_read,
2804 .write = tracing_entries_write,
2807 #ifdef CONFIG_DYNAMIC_FTRACE
2810 tracing_read_long(struct file *filp, char __user *ubuf,
2811 size_t cnt, loff_t *ppos)
2813 unsigned long *p = filp->private_data;
2817 r = sprintf(buf, "%ld\n", *p);
2819 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2822 static struct file_operations tracing_read_long_fops = {
2823 .open = tracing_open_generic,
2824 .read = tracing_read_long,
2828 static struct dentry *d_tracer;
2830 struct dentry *tracing_init_dentry(void)
2837 d_tracer = debugfs_create_dir("tracing", NULL);
2839 if (!d_tracer && !once) {
2841 pr_warning("Could not create debugfs directory 'tracing'\n");
2848 #ifdef CONFIG_FTRACE_SELFTEST
2849 /* Let selftest have access to static functions in this file */
2850 #include "trace_selftest.c"
2853 static __init void tracer_init_debugfs(void)
2855 struct dentry *d_tracer;
2856 struct dentry *entry;
2858 d_tracer = tracing_init_dentry();
2860 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2861 &global_trace, &tracing_ctrl_fops);
2863 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2865 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2866 NULL, &tracing_iter_fops);
2868 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2870 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2871 NULL, &tracing_cpumask_fops);
2873 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2875 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2876 &global_trace, &tracing_lt_fops);
2878 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2880 entry = debugfs_create_file("trace", 0444, d_tracer,
2881 &global_trace, &tracing_fops);
2883 pr_warning("Could not create debugfs 'trace' entry\n");
2885 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2886 &global_trace, &show_traces_fops);
2888 pr_warning("Could not create debugfs 'trace' entry\n");
2890 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2891 &global_trace, &set_tracer_fops);
2893 pr_warning("Could not create debugfs 'trace' entry\n");
2895 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2896 &tracing_max_latency,
2897 &tracing_max_lat_fops);
2899 pr_warning("Could not create debugfs "
2900 "'tracing_max_latency' entry\n");
2902 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2903 &tracing_thresh, &tracing_max_lat_fops);
2905 pr_warning("Could not create debugfs "
2906 "'tracing_threash' entry\n");
2907 entry = debugfs_create_file("README", 0644, d_tracer,
2908 NULL, &tracing_readme_fops);
2910 pr_warning("Could not create debugfs 'README' entry\n");
2912 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2913 NULL, &tracing_pipe_fops);
2915 pr_warning("Could not create debugfs "
2916 "'tracing_threash' entry\n");
2918 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2919 &global_trace, &tracing_entries_fops);
2921 pr_warning("Could not create debugfs "
2922 "'tracing_threash' entry\n");
2924 #ifdef CONFIG_DYNAMIC_FTRACE
2925 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2926 &ftrace_update_tot_cnt,
2927 &tracing_read_long_fops);
2929 pr_warning("Could not create debugfs "
2930 "'dyn_ftrace_total_info' entry\n");
2932 #ifdef CONFIG_SYSPROF_TRACER
2933 init_tracer_sysprof_debugfs(d_tracer);
2937 static int trace_alloc_page(void)
2939 struct trace_array_cpu *data;
2940 struct page *page, *tmp;
2943 unsigned pages_allocated = 0;
2946 /* first allocate a page for each CPU */
2947 for_each_tracing_cpu(i) {
2948 array = (void *)__get_free_page(GFP_KERNEL);
2949 if (array == NULL) {
2950 printk(KERN_ERR "tracer: failed to allocate page"
2951 "for trace buffer!\n");
2956 page = virt_to_page(array);
2957 list_add(&page->lru, &pages);
2959 /* Only allocate if we are actually using the max trace */
2960 #ifdef CONFIG_TRACER_MAX_TRACE
2961 array = (void *)__get_free_page(GFP_KERNEL);
2962 if (array == NULL) {
2963 printk(KERN_ERR "tracer: failed to allocate page"
2964 "for trace buffer!\n");
2968 page = virt_to_page(array);
2969 list_add(&page->lru, &pages);
2973 /* Now that we successfully allocate a page per CPU, add them */
2974 for_each_tracing_cpu(i) {
2975 data = global_trace.data[i];
2976 page = list_entry(pages.next, struct page, lru);
2977 list_del_init(&page->lru);
2978 list_add_tail(&page->lru, &data->trace_pages);
2981 #ifdef CONFIG_TRACER_MAX_TRACE
2982 data = max_tr.data[i];
2983 page = list_entry(pages.next, struct page, lru);
2984 list_del_init(&page->lru);
2985 list_add_tail(&page->lru, &data->trace_pages);
2989 tracing_pages_allocated += pages_allocated;
2990 global_trace.entries += ENTRIES_PER_PAGE;
2995 list_for_each_entry_safe(page, tmp, &pages, lru) {
2996 list_del_init(&page->lru);
3002 static int trace_free_page(void)
3004 struct trace_array_cpu *data;
3006 struct list_head *p;
3010 /* free one page from each buffer */
3011 for_each_tracing_cpu(i) {
3012 data = global_trace.data[i];
3013 p = data->trace_pages.next;
3014 if (p == &data->trace_pages) {
3015 /* should never happen */
3017 tracing_disabled = 1;
3021 page = list_entry(p, struct page, lru);
3023 list_del(&page->lru);
3024 tracing_pages_allocated--;
3025 tracing_pages_allocated--;
3028 tracing_reset(data);
3030 #ifdef CONFIG_TRACER_MAX_TRACE
3031 data = max_tr.data[i];
3032 p = data->trace_pages.next;
3033 if (p == &data->trace_pages) {
3034 /* should never happen */
3036 tracing_disabled = 1;
3040 page = list_entry(p, struct page, lru);
3042 list_del(&page->lru);
3045 tracing_reset(data);
3048 global_trace.entries -= ENTRIES_PER_PAGE;
3053 __init static int tracer_alloc_buffers(void)
3055 struct trace_array_cpu *data;
3062 /* TODO: make the number of buffers hot pluggable with CPUS */
3063 tracing_nr_buffers = num_possible_cpus();
3064 tracing_buffer_mask = cpu_possible_map;
3066 /* Allocate the first page for all buffers */
3067 for_each_tracing_cpu(i) {
3068 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3069 max_tr.data[i] = &per_cpu(max_data, i);
3071 array = (void *)__get_free_page(GFP_KERNEL);
3072 if (array == NULL) {
3073 printk(KERN_ERR "tracer: failed to allocate page"
3074 "for trace buffer!\n");
3078 /* set the array to the list */
3079 INIT_LIST_HEAD(&data->trace_pages);
3080 page = virt_to_page(array);
3081 list_add(&page->lru, &data->trace_pages);
3082 /* use the LRU flag to differentiate the two buffers */
3085 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3086 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3088 /* Only allocate if we are actually using the max trace */
3089 #ifdef CONFIG_TRACER_MAX_TRACE
3090 array = (void *)__get_free_page(GFP_KERNEL);
3091 if (array == NULL) {
3092 printk(KERN_ERR "tracer: failed to allocate page"
3093 "for trace buffer!\n");
3097 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3098 page = virt_to_page(array);
3099 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3105 * Since we allocate by orders of pages, we may be able to
3108 global_trace.entries = ENTRIES_PER_PAGE;
3111 while (global_trace.entries < trace_nr_entries) {
3112 if (trace_alloc_page())
3116 max_tr.entries = global_trace.entries;
3118 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3119 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3120 pr_info(" actual entries %ld\n", global_trace.entries);
3122 tracer_init_debugfs();
3124 trace_init_cmdlines();
3126 register_tracer(&no_tracer);
3127 current_trace = &no_tracer;
3129 /* All seems OK, enable tracing */
3130 global_trace.ctrl = tracer_enabled;
3131 tracing_disabled = 0;
3136 for (i-- ; i >= 0; i--) {
3137 struct page *page, *tmp;
3138 struct trace_array_cpu *data = global_trace.data[i];
3141 list_for_each_entry_safe(page, tmp,
3142 &data->trace_pages, lru) {
3143 list_del_init(&page->lru);
3148 #ifdef CONFIG_TRACER_MAX_TRACE
3149 data = max_tr.data[i];
3151 list_for_each_entry_safe(page, tmp,
3152 &data->trace_pages, lru) {
3153 list_del_init(&page->lru);
3161 fs_initcall(tracer_alloc_buffers);