2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/gfp.h>
32 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly tracing_thresh;
35 static int tracing_disabled = 1;
38 ns2usecs(cycle_t nsec)
45 static atomic_t tracer_counter;
46 static struct trace_array global_trace;
48 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
50 static struct trace_array max_tr;
52 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
54 static int tracer_enabled;
55 static unsigned long trace_nr_entries = 16384UL;
57 static struct tracer *trace_types __read_mostly;
58 static struct tracer *current_trace __read_mostly;
59 static int max_tracer_type_len;
61 static DEFINE_MUTEX(trace_types_lock);
63 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
65 static int __init set_nr_entries(char *str)
69 trace_nr_entries = simple_strtoul(str, &str, 0);
72 __setup("trace_entries=", set_nr_entries);
74 unsigned long nsecs_to_usecs(unsigned long nsecs)
80 __TRACE_FIRST_TYPE = 0,
88 enum trace_flag_type {
89 TRACE_FLAG_IRQS_OFF = 0x01,
90 TRACE_FLAG_NEED_RESCHED = 0x02,
91 TRACE_FLAG_HARDIRQ = 0x04,
92 TRACE_FLAG_SOFTIRQ = 0x08,
95 enum trace_iterator_flags {
96 TRACE_ITER_PRINT_PARENT = 0x01,
97 TRACE_ITER_SYM_OFFSET = 0x02,
98 TRACE_ITER_SYM_ADDR = 0x04,
99 TRACE_ITER_VERBOSE = 0x08,
102 #define TRACE_ITER_SYM_MASK \
103 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
105 /* These must match the bit postions above */
106 static const char *trace_options[] = {
114 static unsigned trace_flags;
116 static DEFINE_SPINLOCK(ftrace_max_lock);
119 * Copy the new maximum trace into the separate maximum-trace
120 * structure. (this way the maximum trace is permanently saved,
121 * for later retrieval via /debugfs/tracing/latency_trace)
124 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
126 struct trace_array_cpu *data = tr->data[cpu];
129 max_tr.time_start = data->preempt_timestamp;
131 data = max_tr.data[cpu];
132 data->saved_latency = tracing_max_latency;
134 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
135 data->pid = tsk->pid;
136 data->uid = tsk->uid;
137 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
138 data->policy = tsk->policy;
139 data->rt_priority = tsk->rt_priority;
141 /* record this tasks comm */
142 tracing_record_cmdline(current);
145 void check_pages(struct trace_array_cpu *data)
147 struct page *page, *tmp;
149 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
150 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
152 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
153 BUG_ON(page->lru.next->prev != &page->lru);
154 BUG_ON(page->lru.prev->next != &page->lru);
158 void *head_page(struct trace_array_cpu *data)
163 if (list_empty(&data->trace_pages))
166 page = list_entry(data->trace_pages.next, struct page, lru);
167 BUG_ON(&page->lru == &data->trace_pages);
169 return page_address(page);
173 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
175 int len = (PAGE_SIZE - 1) - s->len;
183 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
186 /* If we can't write it all, don't bother writing anything */
196 trace_seq_puts(struct trace_seq *s, const char *str)
198 int len = strlen(str);
200 if (len > ((PAGE_SIZE - 1) - s->len))
203 memcpy(s->buffer + s->len, str, len);
210 trace_seq_putc(struct trace_seq *s, unsigned char c)
212 if (s->len >= (PAGE_SIZE - 1))
215 s->buffer[s->len++] = c;
221 trace_seq_reset(struct trace_seq *s)
227 trace_print_seq(struct seq_file *m, struct trace_seq *s)
229 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
232 seq_puts(m, s->buffer);
238 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
240 struct list_head flip_pages;
242 INIT_LIST_HEAD(&flip_pages);
244 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
245 sizeof(struct trace_array_cpu) -
246 offsetof(struct trace_array_cpu, trace_head_idx));
250 list_splice_init(&tr1->trace_pages, &flip_pages);
251 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
252 list_splice_init(&flip_pages, &tr2->trace_pages);
253 BUG_ON(!list_empty(&flip_pages));
259 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
261 struct trace_array_cpu *data;
264 WARN_ON_ONCE(!irqs_disabled());
265 spin_lock(&ftrace_max_lock);
266 /* clear out all the previous traces */
267 for_each_possible_cpu(i) {
269 flip_trace(max_tr.data[i], data);
273 __update_max_tr(tr, tsk, cpu);
274 spin_unlock(&ftrace_max_lock);
278 * update_max_tr_single - only copy one trace over, and reset the rest
280 * @tsk - task with the latency
281 * @cpu - the cpu of the buffer to copy.
284 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
286 struct trace_array_cpu *data = tr->data[cpu];
289 WARN_ON_ONCE(!irqs_disabled());
290 spin_lock(&ftrace_max_lock);
291 for_each_possible_cpu(i)
292 tracing_reset(max_tr.data[i]);
294 flip_trace(max_tr.data[cpu], data);
297 __update_max_tr(tr, tsk, cpu);
298 spin_unlock(&ftrace_max_lock);
301 int register_tracer(struct tracer *type)
308 pr_info("Tracer must have a name\n");
312 mutex_lock(&trace_types_lock);
313 for (t = trace_types; t; t = t->next) {
314 if (strcmp(type->name, t->name) == 0) {
316 pr_info("Trace %s already registered\n",
323 #ifdef CONFIG_FTRACE_STARTUP_TEST
324 if (type->selftest) {
325 struct tracer *saved_tracer = current_trace;
326 struct trace_array_cpu *data;
327 struct trace_array *tr = &global_trace;
328 int saved_ctrl = tr->ctrl;
331 * Run a selftest on this tracer.
332 * Here we reset the trace buffer, and set the current
333 * tracer to be this tracer. The tracer can then run some
334 * internal tracing to verify that everything is in order.
335 * If we fail, we do not register this tracer.
337 for_each_possible_cpu(i) {
339 if (!head_page(data))
343 current_trace = type;
345 /* the test is responsible for initializing and enabling */
346 pr_info("Testing tracer %s: ", type->name);
347 ret = type->selftest(type, tr);
348 /* the test is responsible for resetting too */
349 current_trace = saved_tracer;
350 tr->ctrl = saved_ctrl;
352 printk(KERN_CONT "FAILED!\n");
355 /* Only reset on passing, to avoid touching corrupted buffers */
356 for_each_possible_cpu(i) {
358 if (!head_page(data))
362 printk(KERN_CONT "PASSED\n");
366 type->next = trace_types;
368 len = strlen(type->name);
369 if (len > max_tracer_type_len)
370 max_tracer_type_len = len;
373 mutex_unlock(&trace_types_lock);
378 void unregister_tracer(struct tracer *type)
383 mutex_lock(&trace_types_lock);
384 for (t = &trace_types; *t; t = &(*t)->next) {
388 pr_info("Trace %s not registered\n", type->name);
393 if (strlen(type->name) != max_tracer_type_len)
396 max_tracer_type_len = 0;
397 for (t = &trace_types; *t; t = &(*t)->next) {
398 len = strlen((*t)->name);
399 if (len > max_tracer_type_len)
400 max_tracer_type_len = len;
403 mutex_unlock(&trace_types_lock);
406 notrace void tracing_reset(struct trace_array_cpu *data)
409 data->trace_head = data->trace_tail = head_page(data);
410 data->trace_head_idx = 0;
411 data->trace_tail_idx = 0;
416 function_trace_call(unsigned long ip, unsigned long parent_ip)
418 struct trace_array *tr = &global_trace;
419 struct trace_array_cpu *data;
424 if (unlikely(!tracer_enabled))
427 local_irq_save(flags);
428 cpu = raw_smp_processor_id();
429 data = tr->data[cpu];
430 disabled = atomic_inc_return(&data->disabled);
432 if (likely(disabled == 1))
433 ftrace(tr, data, ip, parent_ip, flags);
435 atomic_dec(&data->disabled);
436 local_irq_restore(flags);
439 static struct ftrace_ops trace_ops __read_mostly =
441 .func = function_trace_call,
445 notrace void tracing_start_function_trace(void)
447 register_ftrace_function(&trace_ops);
450 notrace void tracing_stop_function_trace(void)
452 unregister_ftrace_function(&trace_ops);
455 #define SAVED_CMDLINES 128
456 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
457 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
458 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
459 static int cmdline_idx;
460 static DEFINE_SPINLOCK(trace_cmdline_lock);
461 atomic_t trace_record_cmdline_disabled;
463 static void trace_init_cmdlines(void)
465 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
466 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
470 notrace void trace_stop_cmdline_recording(void);
472 static notrace void trace_save_cmdline(struct task_struct *tsk)
477 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
481 * It's not the end of the world if we don't get
482 * the lock, but we also don't want to spin
483 * nor do we want to disable interrupts,
484 * so if we miss here, then better luck next time.
486 if (!spin_trylock(&trace_cmdline_lock))
489 idx = map_pid_to_cmdline[tsk->pid];
490 if (idx >= SAVED_CMDLINES) {
491 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
493 map = map_cmdline_to_pid[idx];
494 if (map <= PID_MAX_DEFAULT)
495 map_pid_to_cmdline[map] = (unsigned)-1;
497 map_pid_to_cmdline[tsk->pid] = idx;
502 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
504 spin_unlock(&trace_cmdline_lock);
507 static notrace char *trace_find_cmdline(int pid)
509 char *cmdline = "<...>";
515 if (pid > PID_MAX_DEFAULT)
518 map = map_pid_to_cmdline[pid];
519 if (map >= SAVED_CMDLINES)
522 cmdline = saved_cmdlines[map];
528 notrace void tracing_record_cmdline(struct task_struct *tsk)
530 if (atomic_read(&trace_record_cmdline_disabled))
533 trace_save_cmdline(tsk);
536 static inline notrace struct list_head *
537 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
540 * Roundrobin - but skip the head (which is not a real page):
543 if (unlikely(next == &data->trace_pages))
545 BUG_ON(next == &data->trace_pages);
550 static inline notrace void *
551 trace_next_page(struct trace_array_cpu *data, void *addr)
553 struct list_head *next;
556 page = virt_to_page(addr);
558 next = trace_next_list(data, &page->lru);
559 page = list_entry(next, struct page, lru);
561 return page_address(page);
564 static inline notrace struct trace_entry *
565 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
567 unsigned long idx, idx_next;
568 struct trace_entry *entry;
571 idx = data->trace_head_idx;
574 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
576 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
578 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
579 data->trace_head = trace_next_page(data, data->trace_head);
583 if (data->trace_head == data->trace_tail &&
584 idx_next == data->trace_tail_idx) {
586 data->trace_tail_idx++;
587 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
589 trace_next_page(data, data->trace_tail);
590 data->trace_tail_idx = 0;
594 data->trace_head_idx = idx_next;
599 static inline notrace void
600 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
602 struct task_struct *tsk = current;
605 pc = preempt_count();
607 entry->idx = atomic_inc_return(&tracer_counter);
608 entry->preempt_count = pc & 0xff;
609 entry->pid = tsk->pid;
610 entry->t = now(raw_smp_processor_id());
611 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
612 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
613 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
614 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
618 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
619 unsigned long ip, unsigned long parent_ip, unsigned long flags)
621 struct trace_entry *entry;
623 spin_lock(&data->lock);
624 entry = tracing_get_trace_entry(tr, data);
625 tracing_generic_entry_update(entry, flags);
626 entry->type = TRACE_FN;
628 entry->fn.parent_ip = parent_ip;
629 spin_unlock(&data->lock);
633 tracing_sched_switch_trace(struct trace_array *tr,
634 struct trace_array_cpu *data,
635 struct task_struct *prev, struct task_struct *next,
638 struct trace_entry *entry;
640 spin_lock(&data->lock);
641 entry = tracing_get_trace_entry(tr, data);
642 tracing_generic_entry_update(entry, flags);
643 entry->type = TRACE_CTX;
644 entry->ctx.prev_pid = prev->pid;
645 entry->ctx.prev_prio = prev->prio;
646 entry->ctx.prev_state = prev->state;
647 entry->ctx.next_pid = next->pid;
648 entry->ctx.next_prio = next->prio;
649 spin_unlock(&data->lock);
652 enum trace_file_type {
653 TRACE_FILE_LAT_FMT = 1,
656 static struct trace_entry *
657 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
658 struct trace_iterator *iter, int cpu)
661 struct trace_entry *array;
663 if (iter->next_idx[cpu] >= tr->entries ||
664 iter->next_idx[cpu] >= data->trace_idx ||
665 (data->trace_head == data->trace_tail &&
666 data->trace_head_idx == data->trace_tail_idx))
669 if (!iter->next_page[cpu]) {
670 /* Initialize the iterator for this cpu trace buffer */
671 WARN_ON(!data->trace_tail);
672 page = virt_to_page(data->trace_tail);
673 iter->next_page[cpu] = &page->lru;
674 iter->next_page_idx[cpu] = data->trace_tail_idx;
677 page = list_entry(iter->next_page[cpu], struct page, lru);
678 BUG_ON(&data->trace_pages == &page->lru);
680 array = page_address(page);
682 /* Still possible to catch up to the tail */
683 if (iter->next_idx[cpu] && array == data->trace_tail &&
684 iter->next_page_idx[cpu] == data->trace_tail_idx)
687 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
688 return &array[iter->next_page_idx[cpu]];
691 static struct notrace trace_entry *
692 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
694 struct trace_array *tr = iter->tr;
695 struct trace_entry *ent, *next = NULL;
699 for_each_possible_cpu(cpu) {
700 if (!head_page(tr->data[cpu]))
702 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
704 (!next || (long)(next->idx - ent->idx) > 0)) {
717 trace_iterator_increment(struct trace_iterator *iter)
720 iter->next_idx[iter->cpu]++;
721 iter->next_page_idx[iter->cpu]++;
722 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
723 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
725 iter->next_page_idx[iter->cpu] = 0;
726 iter->next_page[iter->cpu] =
727 trace_next_list(data, iter->next_page[iter->cpu]);
732 trace_consume(struct trace_iterator *iter)
734 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
736 data->trace_tail_idx++;
737 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
738 data->trace_tail = trace_next_page(data, data->trace_tail);
739 data->trace_tail_idx = 0;
742 /* Check if we empty it, then reset the index */
743 if (data->trace_head == data->trace_tail &&
744 data->trace_head_idx == data->trace_tail_idx)
747 trace_iterator_increment(iter);
750 static notrace void *
751 find_next_entry_inc(struct trace_iterator *iter)
753 struct trace_entry *next;
756 next = find_next_entry(iter, &next_cpu);
758 iter->prev_ent = iter->ent;
759 iter->prev_cpu = iter->cpu;
762 iter->cpu = next_cpu;
765 trace_iterator_increment(iter);
767 return next ? iter : NULL;
770 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
772 struct trace_iterator *iter = m->private;
773 void *last_ent = iter->ent;
779 /* can't go backwards */
784 ent = find_next_entry_inc(iter);
788 while (ent && iter->idx < i)
789 ent = find_next_entry_inc(iter);
793 if (last_ent && !ent)
794 seq_puts(m, "\n\nvim:ft=help\n");
799 static void *s_start(struct seq_file *m, loff_t *pos)
801 struct trace_iterator *iter = m->private;
806 mutex_lock(&trace_types_lock);
808 if (!current_trace || current_trace != iter->trace)
811 atomic_inc(&trace_record_cmdline_disabled);
813 /* let the tracer grab locks here if needed */
814 if (current_trace->start)
815 current_trace->start(iter);
817 if (*pos != iter->pos) {
821 iter->prev_ent = NULL;
824 for_each_possible_cpu(i) {
825 iter->next_idx[i] = 0;
826 iter->next_page[i] = NULL;
829 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
834 p = s_next(m, p, &l);
840 static void s_stop(struct seq_file *m, void *p)
842 struct trace_iterator *iter = m->private;
844 atomic_dec(&trace_record_cmdline_disabled);
846 /* let the tracer release locks here if needed */
847 if (current_trace && current_trace == iter->trace && iter->trace->stop)
848 iter->trace->stop(iter);
850 mutex_unlock(&trace_types_lock);
854 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
856 #ifdef CONFIG_KALLSYMS
857 char str[KSYM_SYMBOL_LEN];
859 kallsyms_lookup(address, NULL, NULL, NULL, str);
861 return trace_seq_printf(s, fmt, str);
867 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
868 unsigned long address)
870 #ifdef CONFIG_KALLSYMS
871 char str[KSYM_SYMBOL_LEN];
873 sprint_symbol(str, address);
874 return trace_seq_printf(s, fmt, str);
880 # define IP_FMT "%08lx"
882 # define IP_FMT "%016lx"
886 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
891 return trace_seq_printf(s, "0");
893 if (sym_flags & TRACE_ITER_SYM_OFFSET)
894 ret = seq_print_sym_offset(s, "%s", ip);
896 ret = seq_print_sym_short(s, "%s", ip);
901 if (sym_flags & TRACE_ITER_SYM_ADDR)
902 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
906 static notrace void print_lat_help_header(struct seq_file *m)
908 seq_puts(m, "# _------=> CPU# \n");
909 seq_puts(m, "# / _-----=> irqs-off \n");
910 seq_puts(m, "# | / _----=> need-resched \n");
911 seq_puts(m, "# || / _---=> hardirq/softirq \n");
912 seq_puts(m, "# ||| / _--=> preempt-depth \n");
913 seq_puts(m, "# |||| / \n");
914 seq_puts(m, "# ||||| delay \n");
915 seq_puts(m, "# cmd pid ||||| time | caller \n");
916 seq_puts(m, "# \\ / ||||| \\ | / \n");
919 static notrace void print_func_help_header(struct seq_file *m)
921 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
922 seq_puts(m, "# | | | | |\n");
927 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
929 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
930 struct trace_array *tr = iter->tr;
931 struct trace_array_cpu *data = tr->data[tr->cpu];
932 struct tracer *type = current_trace;
933 unsigned long total = 0;
934 unsigned long entries = 0;
936 const char *name = "preemption";
941 for_each_possible_cpu(cpu) {
942 if (head_page(tr->data[cpu])) {
943 total += tr->data[cpu]->trace_idx;
944 if (tr->data[cpu]->trace_idx > tr->entries)
945 entries += tr->entries;
947 entries += tr->data[cpu]->trace_idx;
951 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
953 seq_puts(m, "-----------------------------------"
954 "---------------------------------\n");
955 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
956 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
957 nsecs_to_usecs(data->saved_latency),
961 #if defined(CONFIG_PREEMPT_NONE)
963 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
965 #elif defined(CONFIG_PREEMPT_DESKTOP)
970 /* These are reserved for later use */
973 seq_printf(m, " #P:%d)\n", num_online_cpus());
977 seq_puts(m, " -----------------\n");
978 seq_printf(m, " | task: %.16s-%d "
979 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
980 data->comm, data->pid, data->uid, data->nice,
981 data->policy, data->rt_priority);
982 seq_puts(m, " -----------------\n");
984 if (data->critical_start) {
985 seq_puts(m, " => started at: ");
986 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
987 trace_print_seq(m, &iter->seq);
988 seq_puts(m, "\n => ended at: ");
989 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
990 trace_print_seq(m, &iter->seq);
998 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1000 int hardirq, softirq;
1003 comm = trace_find_cmdline(entry->pid);
1005 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1006 trace_seq_printf(s, "%d", cpu);
1007 trace_seq_printf(s, "%c%c",
1008 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1009 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1011 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1012 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1013 if (hardirq && softirq)
1014 trace_seq_putc(s, 'H');
1017 trace_seq_putc(s, 'h');
1020 trace_seq_putc(s, 's');
1022 trace_seq_putc(s, '.');
1026 if (entry->preempt_count)
1027 trace_seq_printf(s, "%x", entry->preempt_count);
1029 trace_seq_puts(s, ".");
1032 unsigned long preempt_mark_thresh = 100;
1035 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1036 unsigned long rel_usecs)
1038 trace_seq_printf(s, " %4lldus", abs_usecs);
1039 if (rel_usecs > preempt_mark_thresh)
1040 trace_seq_puts(s, "!: ");
1041 else if (rel_usecs > 1)
1042 trace_seq_puts(s, "+: ");
1044 trace_seq_puts(s, " : ");
1047 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1050 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1052 struct trace_seq *s = &iter->seq;
1053 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1054 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1055 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1056 struct trace_entry *entry = iter->ent;
1057 unsigned long abs_usecs;
1058 unsigned long rel_usecs;
1064 rel_usecs = ns2usecs(next_entry->t - entry->t);
1065 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1068 comm = trace_find_cmdline(entry->pid);
1069 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1070 " %ld.%03ldms (+%ld.%03ldms): ",
1072 entry->pid, cpu, entry->flags,
1073 entry->preempt_count, trace_idx,
1076 abs_usecs % 1000, rel_usecs/1000,
1079 lat_print_generic(s, entry, cpu);
1080 lat_print_timestamp(s, abs_usecs, rel_usecs);
1082 switch (entry->type) {
1084 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1085 trace_seq_puts(s, " (");
1086 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1087 trace_seq_puts(s, ")\n");
1090 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1091 state_to_char[entry->ctx.prev_state] : 'X';
1092 comm = trace_find_cmdline(entry->ctx.next_pid);
1093 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1094 entry->ctx.prev_pid,
1095 entry->ctx.prev_prio,
1097 entry->ctx.next_pid,
1098 entry->ctx.next_prio,
1102 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1106 static notrace void sync_time_offset(struct trace_iterator *iter)
1108 struct trace_array_cpu *prev_array, *array;
1109 struct trace_entry *prev_entry, *entry;
1113 prev_entry = iter->prev_ent;
1117 prev_array = iter->tr->data[iter->prev_cpu];
1118 array = iter->tr->data[iter->cpu];
1120 prev_t = prev_entry->t + prev_array->time_offset;
1121 t = entry->t + array->time_offset;
1124 * If time goes backwards we increase the offset of
1125 * the current array, to not have observable time warps.
1126 * This will quickly synchronize the time offsets of
1130 array->time_offset += prev_t - t;
1134 print_trace_fmt(struct trace_iterator *iter)
1136 struct trace_seq *s = &iter->seq;
1137 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1138 struct trace_entry *entry;
1139 unsigned long usec_rem;
1140 unsigned long long t;
1146 sync_time_offset(iter);
1149 comm = trace_find_cmdline(iter->ent->pid);
1151 t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1152 usec_rem = do_div(t, 1000000ULL);
1153 secs = (unsigned long)t;
1155 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1158 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1161 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1165 switch (entry->type) {
1167 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1170 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1171 entry->fn.parent_ip) {
1172 ret = trace_seq_printf(s, " <-");
1175 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1180 ret = trace_seq_printf(s, "\n");
1185 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1186 state_to_char[entry->ctx.prev_state] : 'X';
1187 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1188 entry->ctx.prev_pid,
1189 entry->ctx.prev_prio,
1191 entry->ctx.next_pid,
1192 entry->ctx.next_prio);
1200 static int trace_empty(struct trace_iterator *iter)
1202 struct trace_array_cpu *data;
1205 for_each_possible_cpu(cpu) {
1206 data = iter->tr->data[cpu];
1208 if (head_page(data) && data->trace_idx &&
1209 (data->trace_tail != data->trace_head ||
1210 data->trace_tail_idx != data->trace_head_idx))
1216 static int s_show(struct seq_file *m, void *v)
1218 struct trace_iterator *iter = v;
1220 if (iter->ent == NULL) {
1222 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1225 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1226 /* print nothing if the buffers are empty */
1227 if (trace_empty(iter))
1229 print_trace_header(m, iter);
1230 if (!(trace_flags & TRACE_ITER_VERBOSE))
1231 print_lat_help_header(m);
1233 if (!(trace_flags & TRACE_ITER_VERBOSE))
1234 print_func_help_header(m);
1237 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1238 print_lat_fmt(iter, iter->idx, iter->cpu);
1240 print_trace_fmt(iter);
1241 trace_print_seq(m, &iter->seq);
1247 static struct seq_operations tracer_seq_ops = {
1254 static struct trace_iterator notrace *
1255 __tracing_open(struct inode *inode, struct file *file, int *ret)
1257 struct trace_iterator *iter;
1259 if (tracing_disabled) {
1264 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1270 mutex_lock(&trace_types_lock);
1271 if (current_trace && current_trace->print_max)
1274 iter->tr = inode->i_private;
1275 iter->trace = current_trace;
1278 /* TODO stop tracer */
1279 *ret = seq_open(file, &tracer_seq_ops);
1281 struct seq_file *m = file->private_data;
1284 /* stop the trace while dumping */
1288 if (iter->trace && iter->trace->open)
1289 iter->trace->open(iter);
1294 mutex_unlock(&trace_types_lock);
1300 int tracing_open_generic(struct inode *inode, struct file *filp)
1302 if (tracing_disabled)
1305 filp->private_data = inode->i_private;
1309 int tracing_release(struct inode *inode, struct file *file)
1311 struct seq_file *m = (struct seq_file *)file->private_data;
1312 struct trace_iterator *iter = m->private;
1314 mutex_lock(&trace_types_lock);
1315 if (iter->trace && iter->trace->close)
1316 iter->trace->close(iter);
1318 /* reenable tracing if it was previously enabled */
1321 mutex_unlock(&trace_types_lock);
1323 seq_release(inode, file);
1328 static int tracing_open(struct inode *inode, struct file *file)
1332 __tracing_open(inode, file, &ret);
1337 static int tracing_lt_open(struct inode *inode, struct file *file)
1339 struct trace_iterator *iter;
1342 iter = __tracing_open(inode, file, &ret);
1345 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1351 static notrace void *
1352 t_next(struct seq_file *m, void *v, loff_t *pos)
1354 struct tracer *t = m->private;
1366 static void *t_start(struct seq_file *m, loff_t *pos)
1368 struct tracer *t = m->private;
1371 mutex_lock(&trace_types_lock);
1372 for (; t && l < *pos; t = t_next(m, t, &l))
1378 static void t_stop(struct seq_file *m, void *p)
1380 mutex_unlock(&trace_types_lock);
1383 static int t_show(struct seq_file *m, void *v)
1385 struct tracer *t = v;
1390 seq_printf(m, "%s", t->name);
1399 static struct seq_operations show_traces_seq_ops = {
1406 static int show_traces_open(struct inode *inode, struct file *file)
1410 if (tracing_disabled)
1413 ret = seq_open(file, &show_traces_seq_ops);
1415 struct seq_file *m = file->private_data;
1416 m->private = trace_types;
1422 static struct file_operations tracing_fops = {
1423 .open = tracing_open,
1425 .llseek = seq_lseek,
1426 .release = tracing_release,
1429 static struct file_operations tracing_lt_fops = {
1430 .open = tracing_lt_open,
1432 .llseek = seq_lseek,
1433 .release = tracing_release,
1436 static struct file_operations show_traces_fops = {
1437 .open = show_traces_open,
1439 .release = seq_release,
1443 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1444 size_t cnt, loff_t *ppos)
1451 /* calulate max size */
1452 for (i = 0; trace_options[i]; i++) {
1453 len += strlen(trace_options[i]);
1454 len += 3; /* "no" and space */
1457 /* +2 for \n and \0 */
1458 buf = kmalloc(len + 2, GFP_KERNEL);
1462 for (i = 0; trace_options[i]; i++) {
1463 if (trace_flags & (1 << i))
1464 r += sprintf(buf + r, "%s ", trace_options[i]);
1466 r += sprintf(buf + r, "no%s ", trace_options[i]);
1469 r += sprintf(buf + r, "\n");
1470 WARN_ON(r >= len + 2);
1472 r = simple_read_from_buffer(ubuf, cnt, ppos,
1481 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1482 size_t cnt, loff_t *ppos)
1492 if (copy_from_user(&buf, ubuf, cnt))
1497 if (strncmp(buf, "no", 2) == 0) {
1502 for (i = 0; trace_options[i]; i++) {
1503 int len = strlen(trace_options[i]);
1505 if (strncmp(cmp, trace_options[i], len) == 0) {
1507 trace_flags &= ~(1 << i);
1509 trace_flags |= (1 << i);
1519 static struct file_operations tracing_iter_fops = {
1520 .open = tracing_open_generic,
1521 .read = tracing_iter_ctrl_read,
1522 .write = tracing_iter_ctrl_write,
1525 static const char readme_msg[] =
1526 "tracing mini-HOWTO:\n\n"
1528 "# mount -t debugfs nodev /debug\n\n"
1529 "# cat /debug/tracing/available_tracers\n"
1530 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1531 "# cat /debug/tracing/current_tracer\n"
1533 "# echo sched_switch > /debug/tracing/current_tracer\n"
1534 "# cat /debug/tracing/current_tracer\n"
1536 "# cat /debug/tracing/iter_ctrl\n"
1537 "noprint-parent nosym-offset nosym-addr noverbose\n"
1538 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1539 "# echo 1 > /debug/tracing/tracing_enabled\n"
1540 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1541 "echo 0 > /debug/tracing/tracing_enabled\n"
1545 tracing_readme_read(struct file *filp, char __user *ubuf,
1546 size_t cnt, loff_t *ppos)
1548 return simple_read_from_buffer(ubuf, cnt, ppos,
1549 readme_msg, strlen(readme_msg));
1552 static struct file_operations tracing_readme_fops = {
1553 .open = tracing_open_generic,
1554 .read = tracing_readme_read,
1559 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1560 size_t cnt, loff_t *ppos)
1562 struct trace_array *tr = filp->private_data;
1566 r = sprintf(buf, "%ld\n", tr->ctrl);
1567 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1571 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1572 size_t cnt, loff_t *ppos)
1574 struct trace_array *tr = filp->private_data;
1581 if (copy_from_user(&buf, ubuf, cnt))
1586 val = simple_strtoul(buf, NULL, 10);
1590 mutex_lock(&trace_types_lock);
1591 if (tr->ctrl ^ val) {
1599 if (current_trace && current_trace->ctrl_update)
1600 current_trace->ctrl_update(tr);
1602 mutex_unlock(&trace_types_lock);
1610 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1611 size_t cnt, loff_t *ppos)
1613 char buf[max_tracer_type_len+2];
1616 mutex_lock(&trace_types_lock);
1618 r = sprintf(buf, "%s\n", current_trace->name);
1620 r = sprintf(buf, "\n");
1621 mutex_unlock(&trace_types_lock);
1623 return simple_read_from_buffer(ubuf, cnt, ppos,
1628 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1629 size_t cnt, loff_t *ppos)
1631 struct trace_array *tr = &global_trace;
1633 char buf[max_tracer_type_len+1];
1636 if (cnt > max_tracer_type_len)
1637 cnt = max_tracer_type_len;
1639 if (copy_from_user(&buf, ubuf, cnt))
1644 /* strip ending whitespace. */
1645 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1648 mutex_lock(&trace_types_lock);
1649 for (t = trace_types; t; t = t->next) {
1650 if (strcmp(t->name, buf) == 0)
1653 if (!t || t == current_trace)
1656 if (current_trace && current_trace->reset)
1657 current_trace->reset(tr);
1664 mutex_unlock(&trace_types_lock);
1672 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1673 size_t cnt, loff_t *ppos)
1675 unsigned long *ptr = filp->private_data;
1679 r = snprintf(buf, 64, "%ld\n",
1680 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1683 return simple_read_from_buffer(ubuf, cnt, ppos,
1688 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1689 size_t cnt, loff_t *ppos)
1691 long *ptr = filp->private_data;
1698 if (copy_from_user(&buf, ubuf, cnt))
1703 val = simple_strtoul(buf, NULL, 10);
1710 static atomic_t tracing_reader;
1712 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1714 struct trace_iterator *iter;
1716 if (tracing_disabled)
1719 /* We only allow for reader of the pipe */
1720 if (atomic_inc_return(&tracing_reader) != 1) {
1721 atomic_dec(&tracing_reader);
1725 /* create a buffer to store the information to pass to userspace */
1726 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1730 iter->tr = &global_trace;
1732 filp->private_data = iter;
1737 static int tracing_release_pipe(struct inode *inode, struct file *file)
1739 struct trace_iterator *iter = file->private_data;
1742 atomic_dec(&tracing_reader);
1751 tracing_read_pipe(struct file *filp, char __user *ubuf,
1752 size_t cnt, loff_t *ppos)
1754 struct trace_iterator *iter = filp->private_data;
1755 struct trace_array_cpu *data;
1756 static cpumask_t mask;
1757 struct trace_entry *entry;
1759 unsigned long flags;
1765 /* return any leftover data */
1766 if (iter->seq.len > start) {
1767 len = iter->seq.len - start;
1770 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1779 trace_seq_reset(&iter->seq);
1782 while (trace_empty(iter)) {
1784 * This is a make-shift waitqueue. The reason we don't use
1785 * an actual wait queue is because:
1786 * 1) we only ever have one waiter
1787 * 2) the tracing, traces all functions, we don't want
1788 * the overhead of calling wake_up and friends
1789 * (and tracing them too)
1790 * Anyway, this is really very primitive wakeup.
1792 set_current_state(TASK_INTERRUPTIBLE);
1793 iter->tr->waiter = current;
1795 /* sleep for one second, and try again. */
1796 schedule_timeout(HZ);
1798 iter->tr->waiter = NULL;
1800 if (signal_pending(current))
1804 * We block until we read something and tracing is disabled.
1805 * We still block if tracing is disabled, but we have never
1806 * read anything. This allows a user to cat this file, and
1807 * then enable tracing. But after we have read something,
1808 * we give an EOF when tracing is again disabled.
1810 * iter->pos will be 0 if we haven't read anything.
1812 if (!tracer_enabled && iter->pos)
1818 /* stop when tracing is finished */
1819 if (trace_empty(iter))
1822 if (cnt >= PAGE_SIZE)
1823 cnt = PAGE_SIZE - 1;
1825 memset(iter, 0, sizeof(*iter));
1826 iter->tr = &global_trace;
1830 * We need to stop all tracing on all CPUS to read the
1831 * the next buffer. This is a bit expensive, but is
1832 * not done often. We fill all what we can read,
1833 * and then release the locks again.
1837 local_irq_save(flags);
1838 for_each_possible_cpu(cpu) {
1839 data = iter->tr->data[cpu];
1841 if (!head_page(data) || !data->trace_idx)
1844 atomic_inc(&data->disabled);
1845 spin_lock(&data->lock);
1849 while ((entry = find_next_entry(iter, &cpu))) {
1857 ret = print_trace_fmt(iter);
1861 trace_consume(iter);
1863 if (iter->seq.len >= cnt)
1868 for_each_cpu_mask(cpu, mask) {
1869 data = iter->tr->data[cpu];
1870 spin_unlock(&data->lock);
1871 atomic_dec(&data->disabled);
1873 local_irq_restore(flags);
1875 /* Now copy what we have to the user */
1876 read = iter->seq.len;
1880 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1882 if (read < iter->seq.len)
1885 trace_seq_reset(&iter->seq);
1893 static struct file_operations tracing_max_lat_fops = {
1894 .open = tracing_open_generic,
1895 .read = tracing_max_lat_read,
1896 .write = tracing_max_lat_write,
1899 static struct file_operations tracing_ctrl_fops = {
1900 .open = tracing_open_generic,
1901 .read = tracing_ctrl_read,
1902 .write = tracing_ctrl_write,
1905 static struct file_operations set_tracer_fops = {
1906 .open = tracing_open_generic,
1907 .read = tracing_set_trace_read,
1908 .write = tracing_set_trace_write,
1911 static struct file_operations tracing_pipe_fops = {
1912 .open = tracing_open_pipe,
1913 .read = tracing_read_pipe,
1914 .release = tracing_release_pipe,
1917 #ifdef CONFIG_DYNAMIC_FTRACE
1920 tracing_read_long(struct file *filp, char __user *ubuf,
1921 size_t cnt, loff_t *ppos)
1923 unsigned long *p = filp->private_data;
1927 r = sprintf(buf, "%ld\n", *p);
1928 return simple_read_from_buffer(ubuf, cnt, ppos,
1932 static struct file_operations tracing_read_long_fops = {
1933 .open = tracing_open_generic,
1934 .read = tracing_read_long,
1938 static struct dentry *d_tracer;
1940 struct dentry *tracing_init_dentry(void)
1947 d_tracer = debugfs_create_dir("tracing", NULL);
1949 if (!d_tracer && !once) {
1951 pr_warning("Could not create debugfs directory 'tracing'\n");
1958 #ifdef CONFIG_FTRACE_SELFTEST
1959 /* Let selftest have access to static functions in this file */
1960 #include "trace_selftest.c"
1963 static __init void tracer_init_debugfs(void)
1965 struct dentry *d_tracer;
1966 struct dentry *entry;
1968 d_tracer = tracing_init_dentry();
1970 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1971 &global_trace, &tracing_ctrl_fops);
1973 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1975 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1976 NULL, &tracing_iter_fops);
1978 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1980 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1981 &global_trace, &tracing_lt_fops);
1983 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1985 entry = debugfs_create_file("trace", 0444, d_tracer,
1986 &global_trace, &tracing_fops);
1988 pr_warning("Could not create debugfs 'trace' entry\n");
1990 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
1991 &global_trace, &show_traces_fops);
1993 pr_warning("Could not create debugfs 'trace' entry\n");
1995 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
1996 &global_trace, &set_tracer_fops);
1998 pr_warning("Could not create debugfs 'trace' entry\n");
2000 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2001 &tracing_max_latency,
2002 &tracing_max_lat_fops);
2004 pr_warning("Could not create debugfs "
2005 "'tracing_max_latency' entry\n");
2007 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2008 &tracing_thresh, &tracing_max_lat_fops);
2010 pr_warning("Could not create debugfs "
2011 "'tracing_threash' entry\n");
2012 entry = debugfs_create_file("README", 0644, d_tracer,
2013 NULL, &tracing_readme_fops);
2015 pr_warning("Could not create debugfs 'README' entry\n");
2017 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2018 NULL, &tracing_pipe_fops);
2020 pr_warning("Could not create debugfs "
2021 "'tracing_threash' entry\n");
2023 #ifdef CONFIG_DYNAMIC_FTRACE
2024 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2025 &ftrace_update_tot_cnt,
2026 &tracing_read_long_fops);
2028 pr_warning("Could not create debugfs "
2029 "'dyn_ftrace_total_info' entry\n");
2033 /* dummy trace to disable tracing */
2034 static struct tracer no_tracer __read_mostly =
2039 static int trace_alloc_page(void)
2041 struct trace_array_cpu *data;
2042 struct page *page, *tmp;
2047 /* first allocate a page for each CPU */
2048 for_each_possible_cpu(i) {
2049 array = (void *)__get_free_page(GFP_KERNEL);
2050 if (array == NULL) {
2051 printk(KERN_ERR "tracer: failed to allocate page"
2052 "for trace buffer!\n");
2056 page = virt_to_page(array);
2057 list_add(&page->lru, &pages);
2059 /* Only allocate if we are actually using the max trace */
2060 #ifdef CONFIG_TRACER_MAX_TRACE
2061 array = (void *)__get_free_page(GFP_KERNEL);
2062 if (array == NULL) {
2063 printk(KERN_ERR "tracer: failed to allocate page"
2064 "for trace buffer!\n");
2067 page = virt_to_page(array);
2068 list_add(&page->lru, &pages);
2072 /* Now that we successfully allocate a page per CPU, add them */
2073 for_each_possible_cpu(i) {
2074 data = global_trace.data[i];
2075 spin_lock_init(&data->lock);
2076 lockdep_set_class(&data->lock, &data->lock_key);
2077 page = list_entry(pages.next, struct page, lru);
2078 list_del_init(&page->lru);
2079 list_add_tail(&page->lru, &data->trace_pages);
2082 #ifdef CONFIG_TRACER_MAX_TRACE
2083 data = max_tr.data[i];
2084 spin_lock_init(&data->lock);
2085 lockdep_set_class(&data->lock, &data->lock_key);
2086 page = list_entry(pages.next, struct page, lru);
2087 list_del_init(&page->lru);
2088 list_add_tail(&page->lru, &data->trace_pages);
2092 global_trace.entries += ENTRIES_PER_PAGE;
2097 list_for_each_entry_safe(page, tmp, &pages, lru) {
2098 list_del_init(&page->lru);
2104 __init static int tracer_alloc_buffers(void)
2106 struct trace_array_cpu *data;
2113 /* Allocate the first page for all buffers */
2114 for_each_possible_cpu(i) {
2115 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2116 max_tr.data[i] = &per_cpu(max_data, i);
2118 array = (void *)__get_free_page(GFP_KERNEL);
2119 if (array == NULL) {
2120 printk(KERN_ERR "tracer: failed to allocate page"
2121 "for trace buffer!\n");
2125 /* set the array to the list */
2126 INIT_LIST_HEAD(&data->trace_pages);
2127 page = virt_to_page(array);
2128 list_add(&page->lru, &data->trace_pages);
2129 /* use the LRU flag to differentiate the two buffers */
2132 /* Only allocate if we are actually using the max trace */
2133 #ifdef CONFIG_TRACER_MAX_TRACE
2134 array = (void *)__get_free_page(GFP_KERNEL);
2135 if (array == NULL) {
2136 printk(KERN_ERR "tracer: failed to allocate page"
2137 "for trace buffer!\n");
2141 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2142 page = virt_to_page(array);
2143 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2149 * Since we allocate by orders of pages, we may be able to
2152 global_trace.entries = ENTRIES_PER_PAGE;
2155 while (global_trace.entries < trace_nr_entries) {
2156 if (trace_alloc_page())
2160 max_tr.entries = global_trace.entries;
2162 pr_info("tracer: %d pages allocated for %ld",
2163 pages, trace_nr_entries);
2164 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2165 pr_info(" actual entries %ld\n", global_trace.entries);
2167 tracer_init_debugfs();
2169 trace_init_cmdlines();
2171 register_tracer(&no_tracer);
2172 current_trace = &no_tracer;
2174 /* All seems OK, enable tracing */
2175 tracing_disabled = 0;
2180 for (i-- ; i >= 0; i--) {
2181 struct page *page, *tmp;
2182 struct trace_array_cpu *data = global_trace.data[i];
2185 list_for_each_entry_safe(page, tmp,
2186 &data->trace_pages, lru) {
2187 list_del_init(&page->lru);
2192 #ifdef CONFIG_TRACER_MAX_TRACE
2193 data = max_tr.data[i];
2195 list_for_each_entry_safe(page, tmp,
2196 &data->trace_pages, lru) {
2197 list_del_init(&page->lru);
2205 fs_initcall(tracer_alloc_buffers);