2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/gfp.h>
32 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly tracing_thresh;
35 static int tracing_disabled = 1;
38 ns2usecs(cycle_t nsec)
45 notrace cycle_t ftrace_now(int cpu)
47 return cpu_clock(cpu);
50 static struct trace_array global_trace;
52 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
54 static struct trace_array max_tr;
56 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
58 static int tracer_enabled = 1;
59 static unsigned long trace_nr_entries = 16384UL;
61 static struct tracer *trace_types __read_mostly;
62 static struct tracer *current_trace __read_mostly;
63 static int max_tracer_type_len;
65 static DEFINE_MUTEX(trace_types_lock);
67 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
69 static int __init set_nr_entries(char *str)
73 trace_nr_entries = simple_strtoul(str, &str, 0);
76 __setup("trace_entries=", set_nr_entries);
78 unsigned long nsecs_to_usecs(unsigned long nsecs)
84 __TRACE_FIRST_TYPE = 0,
93 enum trace_flag_type {
94 TRACE_FLAG_IRQS_OFF = 0x01,
95 TRACE_FLAG_NEED_RESCHED = 0x02,
96 TRACE_FLAG_HARDIRQ = 0x04,
97 TRACE_FLAG_SOFTIRQ = 0x08,
100 enum trace_iterator_flags {
101 TRACE_ITER_PRINT_PARENT = 0x01,
102 TRACE_ITER_SYM_OFFSET = 0x02,
103 TRACE_ITER_SYM_ADDR = 0x04,
104 TRACE_ITER_VERBOSE = 0x08,
105 TRACE_ITER_RAW = 0x10,
106 TRACE_ITER_BIN = 0x20,
109 #define TRACE_ITER_SYM_MASK \
110 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
112 /* These must match the bit postions above */
113 static const char *trace_options[] = {
123 static unsigned trace_flags;
125 static DEFINE_SPINLOCK(ftrace_max_lock);
128 * Copy the new maximum trace into the separate maximum-trace
129 * structure. (this way the maximum trace is permanently saved,
130 * for later retrieval via /debugfs/tracing/latency_trace)
133 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
135 struct trace_array_cpu *data = tr->data[cpu];
138 max_tr.time_start = data->preempt_timestamp;
140 data = max_tr.data[cpu];
141 data->saved_latency = tracing_max_latency;
143 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
144 data->pid = tsk->pid;
145 data->uid = tsk->uid;
146 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
147 data->policy = tsk->policy;
148 data->rt_priority = tsk->rt_priority;
150 /* record this tasks comm */
151 tracing_record_cmdline(current);
154 void check_pages(struct trace_array_cpu *data)
156 struct page *page, *tmp;
158 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
159 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
161 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
162 BUG_ON(page->lru.next->prev != &page->lru);
163 BUG_ON(page->lru.prev->next != &page->lru);
167 void *head_page(struct trace_array_cpu *data)
172 if (list_empty(&data->trace_pages))
175 page = list_entry(data->trace_pages.next, struct page, lru);
176 BUG_ON(&page->lru == &data->trace_pages);
178 return page_address(page);
182 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
184 int len = (PAGE_SIZE - 1) - s->len;
192 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
195 /* If we can't write it all, don't bother writing anything */
205 trace_seq_puts(struct trace_seq *s, const char *str)
207 int len = strlen(str);
209 if (len > ((PAGE_SIZE - 1) - s->len))
212 memcpy(s->buffer + s->len, str, len);
219 trace_seq_putc(struct trace_seq *s, unsigned char c)
221 if (s->len >= (PAGE_SIZE - 1))
224 s->buffer[s->len++] = c;
230 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
232 if (len > ((PAGE_SIZE - 1) - s->len))
235 memcpy(s->buffer + s->len, mem, len);
242 trace_seq_reset(struct trace_seq *s)
248 trace_print_seq(struct seq_file *m, struct trace_seq *s)
250 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
253 seq_puts(m, s->buffer);
259 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
261 struct list_head flip_pages;
263 INIT_LIST_HEAD(&flip_pages);
265 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
266 sizeof(struct trace_array_cpu) -
267 offsetof(struct trace_array_cpu, trace_head_idx));
271 list_splice_init(&tr1->trace_pages, &flip_pages);
272 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
273 list_splice_init(&flip_pages, &tr2->trace_pages);
274 BUG_ON(!list_empty(&flip_pages));
280 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
282 struct trace_array_cpu *data;
285 WARN_ON_ONCE(!irqs_disabled());
286 spin_lock(&ftrace_max_lock);
287 /* clear out all the previous traces */
288 for_each_possible_cpu(i) {
290 flip_trace(max_tr.data[i], data);
294 __update_max_tr(tr, tsk, cpu);
295 spin_unlock(&ftrace_max_lock);
299 * update_max_tr_single - only copy one trace over, and reset the rest
301 * @tsk - task with the latency
302 * @cpu - the cpu of the buffer to copy.
305 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
307 struct trace_array_cpu *data = tr->data[cpu];
310 WARN_ON_ONCE(!irqs_disabled());
311 spin_lock(&ftrace_max_lock);
312 for_each_possible_cpu(i)
313 tracing_reset(max_tr.data[i]);
315 flip_trace(max_tr.data[cpu], data);
318 __update_max_tr(tr, tsk, cpu);
319 spin_unlock(&ftrace_max_lock);
322 int register_tracer(struct tracer *type)
329 pr_info("Tracer must have a name\n");
333 mutex_lock(&trace_types_lock);
334 for (t = trace_types; t; t = t->next) {
335 if (strcmp(type->name, t->name) == 0) {
337 pr_info("Trace %s already registered\n",
344 #ifdef CONFIG_FTRACE_STARTUP_TEST
345 if (type->selftest) {
346 struct tracer *saved_tracer = current_trace;
347 struct trace_array_cpu *data;
348 struct trace_array *tr = &global_trace;
349 int saved_ctrl = tr->ctrl;
352 * Run a selftest on this tracer.
353 * Here we reset the trace buffer, and set the current
354 * tracer to be this tracer. The tracer can then run some
355 * internal tracing to verify that everything is in order.
356 * If we fail, we do not register this tracer.
358 for_each_possible_cpu(i) {
360 if (!head_page(data))
364 current_trace = type;
366 /* the test is responsible for initializing and enabling */
367 pr_info("Testing tracer %s: ", type->name);
368 ret = type->selftest(type, tr);
369 /* the test is responsible for resetting too */
370 current_trace = saved_tracer;
371 tr->ctrl = saved_ctrl;
373 printk(KERN_CONT "FAILED!\n");
376 /* Only reset on passing, to avoid touching corrupted buffers */
377 for_each_possible_cpu(i) {
379 if (!head_page(data))
383 printk(KERN_CONT "PASSED\n");
387 type->next = trace_types;
389 len = strlen(type->name);
390 if (len > max_tracer_type_len)
391 max_tracer_type_len = len;
394 mutex_unlock(&trace_types_lock);
399 void unregister_tracer(struct tracer *type)
404 mutex_lock(&trace_types_lock);
405 for (t = &trace_types; *t; t = &(*t)->next) {
409 pr_info("Trace %s not registered\n", type->name);
414 if (strlen(type->name) != max_tracer_type_len)
417 max_tracer_type_len = 0;
418 for (t = &trace_types; *t; t = &(*t)->next) {
419 len = strlen((*t)->name);
420 if (len > max_tracer_type_len)
421 max_tracer_type_len = len;
424 mutex_unlock(&trace_types_lock);
427 notrace void tracing_reset(struct trace_array_cpu *data)
430 data->trace_head = data->trace_tail = head_page(data);
431 data->trace_head_idx = 0;
432 data->trace_tail_idx = 0;
435 #define SAVED_CMDLINES 128
436 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
437 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
438 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
439 static int cmdline_idx;
440 static DEFINE_SPINLOCK(trace_cmdline_lock);
441 atomic_t trace_record_cmdline_disabled;
443 static void trace_init_cmdlines(void)
445 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
446 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
450 notrace void trace_stop_cmdline_recording(void);
452 static notrace void trace_save_cmdline(struct task_struct *tsk)
457 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
461 * It's not the end of the world if we don't get
462 * the lock, but we also don't want to spin
463 * nor do we want to disable interrupts,
464 * so if we miss here, then better luck next time.
466 if (!spin_trylock(&trace_cmdline_lock))
469 idx = map_pid_to_cmdline[tsk->pid];
470 if (idx >= SAVED_CMDLINES) {
471 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
473 map = map_cmdline_to_pid[idx];
474 if (map <= PID_MAX_DEFAULT)
475 map_pid_to_cmdline[map] = (unsigned)-1;
477 map_pid_to_cmdline[tsk->pid] = idx;
482 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
484 spin_unlock(&trace_cmdline_lock);
487 static notrace char *trace_find_cmdline(int pid)
489 char *cmdline = "<...>";
495 if (pid > PID_MAX_DEFAULT)
498 map = map_pid_to_cmdline[pid];
499 if (map >= SAVED_CMDLINES)
502 cmdline = saved_cmdlines[map];
508 notrace void tracing_record_cmdline(struct task_struct *tsk)
510 if (atomic_read(&trace_record_cmdline_disabled))
513 trace_save_cmdline(tsk);
516 static inline notrace struct list_head *
517 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
520 * Roundrobin - but skip the head (which is not a real page):
523 if (unlikely(next == &data->trace_pages))
525 BUG_ON(next == &data->trace_pages);
530 static inline notrace void *
531 trace_next_page(struct trace_array_cpu *data, void *addr)
533 struct list_head *next;
536 page = virt_to_page(addr);
538 next = trace_next_list(data, &page->lru);
539 page = list_entry(next, struct page, lru);
541 return page_address(page);
544 static inline notrace struct trace_entry *
545 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
547 unsigned long idx, idx_next;
548 struct trace_entry *entry;
551 idx = data->trace_head_idx;
554 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
556 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
558 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
559 data->trace_head = trace_next_page(data, data->trace_head);
563 if (data->trace_head == data->trace_tail &&
564 idx_next == data->trace_tail_idx) {
566 data->trace_tail_idx++;
567 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
569 trace_next_page(data, data->trace_tail);
570 data->trace_tail_idx = 0;
574 data->trace_head_idx = idx_next;
579 static inline notrace void
580 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
582 struct task_struct *tsk = current;
585 pc = preempt_count();
587 entry->preempt_count = pc & 0xff;
588 entry->pid = tsk->pid;
589 entry->t = ftrace_now(raw_smp_processor_id());
590 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
591 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
592 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
593 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
597 __ftrace(struct trace_array *tr, struct trace_array_cpu *data,
598 unsigned long ip, unsigned long parent_ip, unsigned long flags)
600 struct trace_entry *entry;
601 unsigned long irq_flags;
603 spin_lock_irqsave(&data->lock, irq_flags);
604 entry = tracing_get_trace_entry(tr, data);
605 tracing_generic_entry_update(entry, flags);
606 entry->type = TRACE_FN;
608 entry->fn.parent_ip = parent_ip;
609 spin_unlock_irqrestore(&data->lock, irq_flags);
613 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
614 unsigned long ip, unsigned long parent_ip, unsigned long flags)
616 if (likely(!atomic_read(&data->disabled)))
617 __ftrace(tr, data, ip, parent_ip, flags);
621 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
622 unsigned long arg1, unsigned long arg2, unsigned long arg3)
624 struct trace_entry *entry;
625 unsigned long irq_flags;
627 spin_lock_irqsave(&data->lock, irq_flags);
628 entry = tracing_get_trace_entry(tr, data);
629 tracing_generic_entry_update(entry, 0);
630 entry->type = TRACE_SPECIAL;
631 entry->special.arg1 = arg1;
632 entry->special.arg2 = arg2;
633 entry->special.arg3 = arg3;
634 spin_unlock_irqrestore(&data->lock, irq_flags);
638 tracing_sched_switch_trace(struct trace_array *tr,
639 struct trace_array_cpu *data,
640 struct task_struct *prev, struct task_struct *next,
643 struct trace_entry *entry;
644 unsigned long irq_flags;
646 spin_lock_irqsave(&data->lock, irq_flags);
647 entry = tracing_get_trace_entry(tr, data);
648 tracing_generic_entry_update(entry, flags);
649 entry->type = TRACE_CTX;
650 entry->ctx.prev_pid = prev->pid;
651 entry->ctx.prev_prio = prev->prio;
652 entry->ctx.prev_state = prev->state;
653 entry->ctx.next_pid = next->pid;
654 entry->ctx.next_prio = next->prio;
655 spin_unlock_irqrestore(&data->lock, irq_flags);
660 function_trace_call(unsigned long ip, unsigned long parent_ip)
662 struct trace_array *tr = &global_trace;
663 struct trace_array_cpu *data;
668 if (unlikely(!tracer_enabled))
671 local_irq_save(flags);
672 cpu = raw_smp_processor_id();
673 data = tr->data[cpu];
674 disabled = atomic_inc_return(&data->disabled);
676 if (likely(disabled == 1))
677 __ftrace(tr, data, ip, parent_ip, flags);
679 atomic_dec(&data->disabled);
680 local_irq_restore(flags);
683 static struct ftrace_ops trace_ops __read_mostly =
685 .func = function_trace_call,
688 notrace void tracing_start_function_trace(void)
690 register_ftrace_function(&trace_ops);
693 notrace void tracing_stop_function_trace(void)
695 unregister_ftrace_function(&trace_ops);
699 enum trace_file_type {
700 TRACE_FILE_LAT_FMT = 1,
703 static struct trace_entry *
704 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
705 struct trace_iterator *iter, int cpu)
708 struct trace_entry *array;
710 if (iter->next_idx[cpu] >= tr->entries ||
711 iter->next_idx[cpu] >= data->trace_idx ||
712 (data->trace_head == data->trace_tail &&
713 data->trace_head_idx == data->trace_tail_idx))
716 if (!iter->next_page[cpu]) {
717 /* Initialize the iterator for this cpu trace buffer */
718 WARN_ON(!data->trace_tail);
719 page = virt_to_page(data->trace_tail);
720 iter->next_page[cpu] = &page->lru;
721 iter->next_page_idx[cpu] = data->trace_tail_idx;
724 page = list_entry(iter->next_page[cpu], struct page, lru);
725 BUG_ON(&data->trace_pages == &page->lru);
727 array = page_address(page);
729 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
730 return &array[iter->next_page_idx[cpu]];
733 static struct trace_entry * notrace
734 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
736 struct trace_array *tr = iter->tr;
737 struct trace_entry *ent, *next = NULL;
741 for_each_possible_cpu(cpu) {
742 if (!head_page(tr->data[cpu]))
744 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
746 * Pick the entry with the smallest timestamp:
748 if (ent && (!next || ent->t < next->t)) {
760 static notrace void trace_iterator_increment(struct trace_iterator *iter)
763 iter->next_idx[iter->cpu]++;
764 iter->next_page_idx[iter->cpu]++;
766 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
767 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
769 iter->next_page_idx[iter->cpu] = 0;
770 iter->next_page[iter->cpu] =
771 trace_next_list(data, iter->next_page[iter->cpu]);
775 static notrace void trace_consume(struct trace_iterator *iter)
777 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
779 data->trace_tail_idx++;
780 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
781 data->trace_tail = trace_next_page(data, data->trace_tail);
782 data->trace_tail_idx = 0;
785 /* Check if we empty it, then reset the index */
786 if (data->trace_head == data->trace_tail &&
787 data->trace_head_idx == data->trace_tail_idx)
791 static notrace void *find_next_entry_inc(struct trace_iterator *iter)
793 struct trace_entry *next;
796 next = find_next_entry(iter, &next_cpu);
798 iter->prev_ent = iter->ent;
799 iter->prev_cpu = iter->cpu;
802 iter->cpu = next_cpu;
805 trace_iterator_increment(iter);
807 return next ? iter : NULL;
810 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
812 struct trace_iterator *iter = m->private;
813 void *last_ent = iter->ent;
819 /* can't go backwards */
824 ent = find_next_entry_inc(iter);
828 while (ent && iter->idx < i)
829 ent = find_next_entry_inc(iter);
833 if (last_ent && !ent)
834 seq_puts(m, "\n\nvim:ft=help\n");
839 static void *s_start(struct seq_file *m, loff_t *pos)
841 struct trace_iterator *iter = m->private;
846 mutex_lock(&trace_types_lock);
848 if (!current_trace || current_trace != iter->trace)
851 atomic_inc(&trace_record_cmdline_disabled);
853 /* let the tracer grab locks here if needed */
854 if (current_trace->start)
855 current_trace->start(iter);
857 if (*pos != iter->pos) {
861 iter->prev_ent = NULL;
864 for_each_possible_cpu(i) {
865 iter->next_idx[i] = 0;
866 iter->next_page[i] = NULL;
869 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
874 p = s_next(m, p, &l);
880 static void s_stop(struct seq_file *m, void *p)
882 struct trace_iterator *iter = m->private;
884 atomic_dec(&trace_record_cmdline_disabled);
886 /* let the tracer release locks here if needed */
887 if (current_trace && current_trace == iter->trace && iter->trace->stop)
888 iter->trace->stop(iter);
890 mutex_unlock(&trace_types_lock);
894 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
896 #ifdef CONFIG_KALLSYMS
897 char str[KSYM_SYMBOL_LEN];
899 kallsyms_lookup(address, NULL, NULL, NULL, str);
901 return trace_seq_printf(s, fmt, str);
907 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
908 unsigned long address)
910 #ifdef CONFIG_KALLSYMS
911 char str[KSYM_SYMBOL_LEN];
913 sprint_symbol(str, address);
914 return trace_seq_printf(s, fmt, str);
920 # define IP_FMT "%08lx"
922 # define IP_FMT "%016lx"
926 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
931 return trace_seq_printf(s, "0");
933 if (sym_flags & TRACE_ITER_SYM_OFFSET)
934 ret = seq_print_sym_offset(s, "%s", ip);
936 ret = seq_print_sym_short(s, "%s", ip);
941 if (sym_flags & TRACE_ITER_SYM_ADDR)
942 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
946 static notrace void print_lat_help_header(struct seq_file *m)
948 seq_puts(m, "# _------=> CPU# \n");
949 seq_puts(m, "# / _-----=> irqs-off \n");
950 seq_puts(m, "# | / _----=> need-resched \n");
951 seq_puts(m, "# || / _---=> hardirq/softirq \n");
952 seq_puts(m, "# ||| / _--=> preempt-depth \n");
953 seq_puts(m, "# |||| / \n");
954 seq_puts(m, "# ||||| delay \n");
955 seq_puts(m, "# cmd pid ||||| time | caller \n");
956 seq_puts(m, "# \\ / ||||| \\ | / \n");
959 static notrace void print_func_help_header(struct seq_file *m)
961 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
962 seq_puts(m, "# | | | | |\n");
967 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
969 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
970 struct trace_array *tr = iter->tr;
971 struct trace_array_cpu *data = tr->data[tr->cpu];
972 struct tracer *type = current_trace;
973 unsigned long total = 0;
974 unsigned long entries = 0;
976 const char *name = "preemption";
981 for_each_possible_cpu(cpu) {
982 if (head_page(tr->data[cpu])) {
983 total += tr->data[cpu]->trace_idx;
984 if (tr->data[cpu]->trace_idx > tr->entries)
985 entries += tr->entries;
987 entries += tr->data[cpu]->trace_idx;
991 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
993 seq_puts(m, "-----------------------------------"
994 "---------------------------------\n");
995 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
996 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
997 nsecs_to_usecs(data->saved_latency),
1001 #if defined(CONFIG_PREEMPT_NONE)
1003 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1005 #elif defined(CONFIG_PREEMPT_DESKTOP)
1010 /* These are reserved for later use */
1013 seq_printf(m, " #P:%d)\n", num_online_cpus());
1017 seq_puts(m, " -----------------\n");
1018 seq_printf(m, " | task: %.16s-%d "
1019 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1020 data->comm, data->pid, data->uid, data->nice,
1021 data->policy, data->rt_priority);
1022 seq_puts(m, " -----------------\n");
1024 if (data->critical_start) {
1025 seq_puts(m, " => started at: ");
1026 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1027 trace_print_seq(m, &iter->seq);
1028 seq_puts(m, "\n => ended at: ");
1029 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1030 trace_print_seq(m, &iter->seq);
1038 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1040 int hardirq, softirq;
1043 comm = trace_find_cmdline(entry->pid);
1045 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1046 trace_seq_printf(s, "%d", cpu);
1047 trace_seq_printf(s, "%c%c",
1048 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1049 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1051 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1052 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1053 if (hardirq && softirq)
1054 trace_seq_putc(s, 'H');
1057 trace_seq_putc(s, 'h');
1060 trace_seq_putc(s, 's');
1062 trace_seq_putc(s, '.');
1066 if (entry->preempt_count)
1067 trace_seq_printf(s, "%x", entry->preempt_count);
1069 trace_seq_puts(s, ".");
1072 unsigned long preempt_mark_thresh = 100;
1075 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1076 unsigned long rel_usecs)
1078 trace_seq_printf(s, " %4lldus", abs_usecs);
1079 if (rel_usecs > preempt_mark_thresh)
1080 trace_seq_puts(s, "!: ");
1081 else if (rel_usecs > 1)
1082 trace_seq_puts(s, "+: ");
1084 trace_seq_puts(s, " : ");
1087 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1090 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1092 struct trace_seq *s = &iter->seq;
1093 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1094 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1095 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1096 struct trace_entry *entry = iter->ent;
1097 unsigned long abs_usecs;
1098 unsigned long rel_usecs;
1104 rel_usecs = ns2usecs(next_entry->t - entry->t);
1105 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1108 comm = trace_find_cmdline(entry->pid);
1109 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1110 " %ld.%03ldms (+%ld.%03ldms): ",
1112 entry->pid, cpu, entry->flags,
1113 entry->preempt_count, trace_idx,
1116 abs_usecs % 1000, rel_usecs/1000,
1119 lat_print_generic(s, entry, cpu);
1120 lat_print_timestamp(s, abs_usecs, rel_usecs);
1122 switch (entry->type) {
1124 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1125 trace_seq_puts(s, " (");
1126 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1127 trace_seq_puts(s, ")\n");
1130 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1131 state_to_char[entry->ctx.prev_state] : 'X';
1132 comm = trace_find_cmdline(entry->ctx.next_pid);
1133 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1134 entry->ctx.prev_pid,
1135 entry->ctx.prev_prio,
1137 entry->ctx.next_pid,
1138 entry->ctx.next_prio,
1142 trace_seq_printf(s, " %lx %lx %lx\n",
1143 entry->special.arg1,
1144 entry->special.arg2,
1145 entry->special.arg3);
1148 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1153 static notrace int print_trace_fmt(struct trace_iterator *iter)
1155 struct trace_seq *s = &iter->seq;
1156 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1157 struct trace_entry *entry;
1158 unsigned long usec_rem;
1159 unsigned long long t;
1167 comm = trace_find_cmdline(iter->ent->pid);
1169 t = ns2usecs(entry->t);
1170 usec_rem = do_div(t, 1000000ULL);
1171 secs = (unsigned long)t;
1173 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1176 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1179 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1183 switch (entry->type) {
1185 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1188 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1189 entry->fn.parent_ip) {
1190 ret = trace_seq_printf(s, " <-");
1193 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1198 ret = trace_seq_printf(s, "\n");
1203 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1204 state_to_char[entry->ctx.prev_state] : 'X';
1205 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1206 entry->ctx.prev_pid,
1207 entry->ctx.prev_prio,
1209 entry->ctx.next_pid,
1210 entry->ctx.next_prio);
1215 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1216 entry->special.arg1,
1217 entry->special.arg2,
1218 entry->special.arg3);
1226 static notrace int print_raw_fmt(struct trace_iterator *iter)
1228 struct trace_seq *s = &iter->seq;
1229 struct trace_entry *entry;
1235 ret = trace_seq_printf(s, "%d %d %llu ",
1236 entry->pid, iter->cpu, entry->t);
1240 switch (entry->type) {
1242 ret = trace_seq_printf(s, "%x %x\n",
1243 entry->fn.ip, entry->fn.parent_ip);
1248 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1249 state_to_char[entry->ctx.prev_state] : 'X';
1250 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1251 entry->ctx.prev_pid,
1252 entry->ctx.prev_prio,
1254 entry->ctx.next_pid,
1255 entry->ctx.next_prio);
1260 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1261 entry->special.arg1,
1262 entry->special.arg2,
1263 entry->special.arg3);
1271 #define SEQ_PUT_FIELD_RET(s, x) \
1273 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1277 static notrace int print_bin_fmt(struct trace_iterator *iter)
1279 struct trace_seq *s = &iter->seq;
1280 struct trace_entry *entry;
1284 SEQ_PUT_FIELD_RET(s, entry->pid);
1285 SEQ_PUT_FIELD_RET(s, entry->cpu);
1286 SEQ_PUT_FIELD_RET(s, entry->t);
1288 switch (entry->type) {
1290 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1291 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1294 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1295 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1296 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1297 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1298 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1301 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1302 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1303 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1309 static int trace_empty(struct trace_iterator *iter)
1311 struct trace_array_cpu *data;
1314 for_each_possible_cpu(cpu) {
1315 data = iter->tr->data[cpu];
1317 if (head_page(data) && data->trace_idx &&
1318 (data->trace_tail != data->trace_head ||
1319 data->trace_tail_idx != data->trace_head_idx))
1325 static int print_trace_line(struct trace_iterator *iter)
1327 if (trace_flags & TRACE_ITER_BIN)
1328 return print_bin_fmt(iter);
1330 if (trace_flags & TRACE_ITER_RAW)
1331 return print_raw_fmt(iter);
1333 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1334 return print_lat_fmt(iter, iter->idx, iter->cpu);
1336 return print_trace_fmt(iter);
1339 static int s_show(struct seq_file *m, void *v)
1341 struct trace_iterator *iter = v;
1343 if (iter->ent == NULL) {
1345 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1348 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1349 /* print nothing if the buffers are empty */
1350 if (trace_empty(iter))
1352 print_trace_header(m, iter);
1353 if (!(trace_flags & TRACE_ITER_VERBOSE))
1354 print_lat_help_header(m);
1356 if (!(trace_flags & TRACE_ITER_VERBOSE))
1357 print_func_help_header(m);
1360 print_trace_line(iter);
1361 trace_print_seq(m, &iter->seq);
1367 static struct seq_operations tracer_seq_ops = {
1374 static struct trace_iterator notrace *
1375 __tracing_open(struct inode *inode, struct file *file, int *ret)
1377 struct trace_iterator *iter;
1379 if (tracing_disabled) {
1384 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1390 mutex_lock(&trace_types_lock);
1391 if (current_trace && current_trace->print_max)
1394 iter->tr = inode->i_private;
1395 iter->trace = current_trace;
1398 /* TODO stop tracer */
1399 *ret = seq_open(file, &tracer_seq_ops);
1401 struct seq_file *m = file->private_data;
1404 /* stop the trace while dumping */
1408 if (iter->trace && iter->trace->open)
1409 iter->trace->open(iter);
1414 mutex_unlock(&trace_types_lock);
1420 int tracing_open_generic(struct inode *inode, struct file *filp)
1422 if (tracing_disabled)
1425 filp->private_data = inode->i_private;
1429 int tracing_release(struct inode *inode, struct file *file)
1431 struct seq_file *m = (struct seq_file *)file->private_data;
1432 struct trace_iterator *iter = m->private;
1434 mutex_lock(&trace_types_lock);
1435 if (iter->trace && iter->trace->close)
1436 iter->trace->close(iter);
1438 /* reenable tracing if it was previously enabled */
1441 mutex_unlock(&trace_types_lock);
1443 seq_release(inode, file);
1448 static int tracing_open(struct inode *inode, struct file *file)
1452 __tracing_open(inode, file, &ret);
1457 static int tracing_lt_open(struct inode *inode, struct file *file)
1459 struct trace_iterator *iter;
1462 iter = __tracing_open(inode, file, &ret);
1465 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1471 static notrace void *
1472 t_next(struct seq_file *m, void *v, loff_t *pos)
1474 struct tracer *t = m->private;
1486 static void *t_start(struct seq_file *m, loff_t *pos)
1488 struct tracer *t = m->private;
1491 mutex_lock(&trace_types_lock);
1492 for (; t && l < *pos; t = t_next(m, t, &l))
1498 static void t_stop(struct seq_file *m, void *p)
1500 mutex_unlock(&trace_types_lock);
1503 static int t_show(struct seq_file *m, void *v)
1505 struct tracer *t = v;
1510 seq_printf(m, "%s", t->name);
1519 static struct seq_operations show_traces_seq_ops = {
1526 static int show_traces_open(struct inode *inode, struct file *file)
1530 if (tracing_disabled)
1533 ret = seq_open(file, &show_traces_seq_ops);
1535 struct seq_file *m = file->private_data;
1536 m->private = trace_types;
1542 static struct file_operations tracing_fops = {
1543 .open = tracing_open,
1545 .llseek = seq_lseek,
1546 .release = tracing_release,
1549 static struct file_operations tracing_lt_fops = {
1550 .open = tracing_lt_open,
1552 .llseek = seq_lseek,
1553 .release = tracing_release,
1556 static struct file_operations show_traces_fops = {
1557 .open = show_traces_open,
1559 .release = seq_release,
1563 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1564 size_t cnt, loff_t *ppos)
1571 /* calulate max size */
1572 for (i = 0; trace_options[i]; i++) {
1573 len += strlen(trace_options[i]);
1574 len += 3; /* "no" and space */
1577 /* +2 for \n and \0 */
1578 buf = kmalloc(len + 2, GFP_KERNEL);
1582 for (i = 0; trace_options[i]; i++) {
1583 if (trace_flags & (1 << i))
1584 r += sprintf(buf + r, "%s ", trace_options[i]);
1586 r += sprintf(buf + r, "no%s ", trace_options[i]);
1589 r += sprintf(buf + r, "\n");
1590 WARN_ON(r >= len + 2);
1592 r = simple_read_from_buffer(ubuf, cnt, ppos,
1601 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1602 size_t cnt, loff_t *ppos)
1612 if (copy_from_user(&buf, ubuf, cnt))
1617 if (strncmp(buf, "no", 2) == 0) {
1622 for (i = 0; trace_options[i]; i++) {
1623 int len = strlen(trace_options[i]);
1625 if (strncmp(cmp, trace_options[i], len) == 0) {
1627 trace_flags &= ~(1 << i);
1629 trace_flags |= (1 << i);
1639 static struct file_operations tracing_iter_fops = {
1640 .open = tracing_open_generic,
1641 .read = tracing_iter_ctrl_read,
1642 .write = tracing_iter_ctrl_write,
1645 static const char readme_msg[] =
1646 "tracing mini-HOWTO:\n\n"
1648 "# mount -t debugfs nodev /debug\n\n"
1649 "# cat /debug/tracing/available_tracers\n"
1650 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1651 "# cat /debug/tracing/current_tracer\n"
1653 "# echo sched_switch > /debug/tracing/current_tracer\n"
1654 "# cat /debug/tracing/current_tracer\n"
1656 "# cat /debug/tracing/iter_ctrl\n"
1657 "noprint-parent nosym-offset nosym-addr noverbose\n"
1658 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1659 "# echo 1 > /debug/tracing/tracing_enabled\n"
1660 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1661 "echo 0 > /debug/tracing/tracing_enabled\n"
1665 tracing_readme_read(struct file *filp, char __user *ubuf,
1666 size_t cnt, loff_t *ppos)
1668 return simple_read_from_buffer(ubuf, cnt, ppos,
1669 readme_msg, strlen(readme_msg));
1672 static struct file_operations tracing_readme_fops = {
1673 .open = tracing_open_generic,
1674 .read = tracing_readme_read,
1679 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1680 size_t cnt, loff_t *ppos)
1682 struct trace_array *tr = filp->private_data;
1686 r = sprintf(buf, "%ld\n", tr->ctrl);
1687 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1691 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1692 size_t cnt, loff_t *ppos)
1694 struct trace_array *tr = filp->private_data;
1701 if (copy_from_user(&buf, ubuf, cnt))
1706 val = simple_strtoul(buf, NULL, 10);
1710 mutex_lock(&trace_types_lock);
1711 if (tr->ctrl ^ val) {
1719 if (current_trace && current_trace->ctrl_update)
1720 current_trace->ctrl_update(tr);
1722 mutex_unlock(&trace_types_lock);
1730 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1731 size_t cnt, loff_t *ppos)
1733 char buf[max_tracer_type_len+2];
1736 mutex_lock(&trace_types_lock);
1738 r = sprintf(buf, "%s\n", current_trace->name);
1740 r = sprintf(buf, "\n");
1741 mutex_unlock(&trace_types_lock);
1743 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1747 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1748 size_t cnt, loff_t *ppos)
1750 struct trace_array *tr = &global_trace;
1752 char buf[max_tracer_type_len+1];
1755 if (cnt > max_tracer_type_len)
1756 cnt = max_tracer_type_len;
1758 if (copy_from_user(&buf, ubuf, cnt))
1763 /* strip ending whitespace. */
1764 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1767 mutex_lock(&trace_types_lock);
1768 for (t = trace_types; t; t = t->next) {
1769 if (strcmp(t->name, buf) == 0)
1772 if (!t || t == current_trace)
1775 if (current_trace && current_trace->reset)
1776 current_trace->reset(tr);
1783 mutex_unlock(&trace_types_lock);
1791 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1792 size_t cnt, loff_t *ppos)
1794 unsigned long *ptr = filp->private_data;
1798 r = snprintf(buf, 64, "%ld\n",
1799 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1802 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1806 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1807 size_t cnt, loff_t *ppos)
1809 long *ptr = filp->private_data;
1816 if (copy_from_user(&buf, ubuf, cnt))
1821 val = simple_strtoul(buf, NULL, 10);
1828 static atomic_t tracing_reader;
1830 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1832 struct trace_iterator *iter;
1834 if (tracing_disabled)
1837 /* We only allow for reader of the pipe */
1838 if (atomic_inc_return(&tracing_reader) != 1) {
1839 atomic_dec(&tracing_reader);
1843 /* create a buffer to store the information to pass to userspace */
1844 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1848 iter->tr = &global_trace;
1850 filp->private_data = iter;
1855 static int tracing_release_pipe(struct inode *inode, struct file *file)
1857 struct trace_iterator *iter = file->private_data;
1860 atomic_dec(&tracing_reader);
1869 tracing_read_pipe(struct file *filp, char __user *ubuf,
1870 size_t cnt, loff_t *ppos)
1872 struct trace_iterator *iter = filp->private_data;
1873 struct trace_array_cpu *data;
1874 static cpumask_t mask;
1876 unsigned long flags;
1883 /* return any leftover data */
1884 if (iter->seq.len > start) {
1885 len = iter->seq.len - start;
1888 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1897 trace_seq_reset(&iter->seq);
1900 while (trace_empty(iter)) {
1902 * This is a make-shift waitqueue. The reason we don't use
1903 * an actual wait queue is because:
1904 * 1) we only ever have one waiter
1905 * 2) the tracing, traces all functions, we don't want
1906 * the overhead of calling wake_up and friends
1907 * (and tracing them too)
1908 * Anyway, this is really very primitive wakeup.
1910 set_current_state(TASK_INTERRUPTIBLE);
1911 iter->tr->waiter = current;
1913 /* sleep for one second, and try again. */
1914 schedule_timeout(HZ);
1916 iter->tr->waiter = NULL;
1918 if (signal_pending(current))
1922 * We block until we read something and tracing is disabled.
1923 * We still block if tracing is disabled, but we have never
1924 * read anything. This allows a user to cat this file, and
1925 * then enable tracing. But after we have read something,
1926 * we give an EOF when tracing is again disabled.
1928 * iter->pos will be 0 if we haven't read anything.
1930 if (!tracer_enabled && iter->pos)
1936 /* stop when tracing is finished */
1937 if (trace_empty(iter))
1940 if (cnt >= PAGE_SIZE)
1941 cnt = PAGE_SIZE - 1;
1943 memset(iter, 0, sizeof(*iter));
1944 iter->tr = &global_trace;
1948 * We need to stop all tracing on all CPUS to read the
1949 * the next buffer. This is a bit expensive, but is
1950 * not done often. We fill all what we can read,
1951 * and then release the locks again.
1955 local_irq_save(flags);
1956 ftrace_save = ftrace_enabled;
1959 for_each_possible_cpu(cpu) {
1960 data = iter->tr->data[cpu];
1962 if (!head_page(data) || !data->trace_idx)
1965 atomic_inc(&data->disabled);
1969 for_each_cpu_mask(cpu, mask) {
1970 data = iter->tr->data[cpu];
1971 spin_lock(&data->lock);
1974 while (find_next_entry_inc(iter) != NULL) {
1975 int len = iter->seq.len;
1977 ret = print_trace_line(iter);
1979 /* don't print partial lines */
1980 iter->seq.len = len;
1984 trace_consume(iter);
1986 if (iter->seq.len >= cnt)
1990 for_each_cpu_mask(cpu, mask) {
1991 data = iter->tr->data[cpu];
1992 spin_unlock(&data->lock);
1995 for_each_cpu_mask(cpu, mask) {
1996 data = iter->tr->data[cpu];
1997 atomic_dec(&data->disabled);
1999 ftrace_enabled = ftrace_save;
2000 local_irq_restore(flags);
2002 /* Now copy what we have to the user */
2003 read = iter->seq.len;
2007 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2009 if (read < iter->seq.len)
2012 trace_seq_reset(&iter->seq);
2020 static struct file_operations tracing_max_lat_fops = {
2021 .open = tracing_open_generic,
2022 .read = tracing_max_lat_read,
2023 .write = tracing_max_lat_write,
2026 static struct file_operations tracing_ctrl_fops = {
2027 .open = tracing_open_generic,
2028 .read = tracing_ctrl_read,
2029 .write = tracing_ctrl_write,
2032 static struct file_operations set_tracer_fops = {
2033 .open = tracing_open_generic,
2034 .read = tracing_set_trace_read,
2035 .write = tracing_set_trace_write,
2038 static struct file_operations tracing_pipe_fops = {
2039 .open = tracing_open_pipe,
2040 .read = tracing_read_pipe,
2041 .release = tracing_release_pipe,
2044 #ifdef CONFIG_DYNAMIC_FTRACE
2047 tracing_read_long(struct file *filp, char __user *ubuf,
2048 size_t cnt, loff_t *ppos)
2050 unsigned long *p = filp->private_data;
2054 r = sprintf(buf, "%ld\n", *p);
2056 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2059 static struct file_operations tracing_read_long_fops = {
2060 .open = tracing_open_generic,
2061 .read = tracing_read_long,
2065 static struct dentry *d_tracer;
2067 struct dentry *tracing_init_dentry(void)
2074 d_tracer = debugfs_create_dir("tracing", NULL);
2076 if (!d_tracer && !once) {
2078 pr_warning("Could not create debugfs directory 'tracing'\n");
2085 #ifdef CONFIG_FTRACE_SELFTEST
2086 /* Let selftest have access to static functions in this file */
2087 #include "trace_selftest.c"
2090 static __init void tracer_init_debugfs(void)
2092 struct dentry *d_tracer;
2093 struct dentry *entry;
2095 d_tracer = tracing_init_dentry();
2097 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2098 &global_trace, &tracing_ctrl_fops);
2100 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2102 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2103 NULL, &tracing_iter_fops);
2105 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2107 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2108 &global_trace, &tracing_lt_fops);
2110 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2112 entry = debugfs_create_file("trace", 0444, d_tracer,
2113 &global_trace, &tracing_fops);
2115 pr_warning("Could not create debugfs 'trace' entry\n");
2117 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2118 &global_trace, &show_traces_fops);
2120 pr_warning("Could not create debugfs 'trace' entry\n");
2122 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2123 &global_trace, &set_tracer_fops);
2125 pr_warning("Could not create debugfs 'trace' entry\n");
2127 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2128 &tracing_max_latency,
2129 &tracing_max_lat_fops);
2131 pr_warning("Could not create debugfs "
2132 "'tracing_max_latency' entry\n");
2134 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2135 &tracing_thresh, &tracing_max_lat_fops);
2137 pr_warning("Could not create debugfs "
2138 "'tracing_threash' entry\n");
2139 entry = debugfs_create_file("README", 0644, d_tracer,
2140 NULL, &tracing_readme_fops);
2142 pr_warning("Could not create debugfs 'README' entry\n");
2144 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2145 NULL, &tracing_pipe_fops);
2147 pr_warning("Could not create debugfs "
2148 "'tracing_threash' entry\n");
2150 #ifdef CONFIG_DYNAMIC_FTRACE
2151 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2152 &ftrace_update_tot_cnt,
2153 &tracing_read_long_fops);
2155 pr_warning("Could not create debugfs "
2156 "'dyn_ftrace_total_info' entry\n");
2160 /* dummy trace to disable tracing */
2161 static struct tracer no_tracer __read_mostly =
2166 static int trace_alloc_page(void)
2168 struct trace_array_cpu *data;
2169 struct page *page, *tmp;
2174 /* first allocate a page for each CPU */
2175 for_each_possible_cpu(i) {
2176 array = (void *)__get_free_page(GFP_KERNEL);
2177 if (array == NULL) {
2178 printk(KERN_ERR "tracer: failed to allocate page"
2179 "for trace buffer!\n");
2183 page = virt_to_page(array);
2184 list_add(&page->lru, &pages);
2186 /* Only allocate if we are actually using the max trace */
2187 #ifdef CONFIG_TRACER_MAX_TRACE
2188 array = (void *)__get_free_page(GFP_KERNEL);
2189 if (array == NULL) {
2190 printk(KERN_ERR "tracer: failed to allocate page"
2191 "for trace buffer!\n");
2194 page = virt_to_page(array);
2195 list_add(&page->lru, &pages);
2199 /* Now that we successfully allocate a page per CPU, add them */
2200 for_each_possible_cpu(i) {
2201 data = global_trace.data[i];
2202 spin_lock_init(&data->lock);
2203 lockdep_set_class(&data->lock, &data->lock_key);
2204 page = list_entry(pages.next, struct page, lru);
2205 list_del_init(&page->lru);
2206 list_add_tail(&page->lru, &data->trace_pages);
2209 #ifdef CONFIG_TRACER_MAX_TRACE
2210 data = max_tr.data[i];
2211 spin_lock_init(&data->lock);
2212 lockdep_set_class(&data->lock, &data->lock_key);
2213 page = list_entry(pages.next, struct page, lru);
2214 list_del_init(&page->lru);
2215 list_add_tail(&page->lru, &data->trace_pages);
2219 global_trace.entries += ENTRIES_PER_PAGE;
2224 list_for_each_entry_safe(page, tmp, &pages, lru) {
2225 list_del_init(&page->lru);
2231 __init static int tracer_alloc_buffers(void)
2233 struct trace_array_cpu *data;
2240 global_trace.ctrl = tracer_enabled;
2242 /* Allocate the first page for all buffers */
2243 for_each_possible_cpu(i) {
2244 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2245 max_tr.data[i] = &per_cpu(max_data, i);
2247 array = (void *)__get_free_page(GFP_KERNEL);
2248 if (array == NULL) {
2249 printk(KERN_ERR "tracer: failed to allocate page"
2250 "for trace buffer!\n");
2254 /* set the array to the list */
2255 INIT_LIST_HEAD(&data->trace_pages);
2256 page = virt_to_page(array);
2257 list_add(&page->lru, &data->trace_pages);
2258 /* use the LRU flag to differentiate the two buffers */
2261 /* Only allocate if we are actually using the max trace */
2262 #ifdef CONFIG_TRACER_MAX_TRACE
2263 array = (void *)__get_free_page(GFP_KERNEL);
2264 if (array == NULL) {
2265 printk(KERN_ERR "tracer: failed to allocate page"
2266 "for trace buffer!\n");
2270 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2271 page = virt_to_page(array);
2272 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2278 * Since we allocate by orders of pages, we may be able to
2281 global_trace.entries = ENTRIES_PER_PAGE;
2284 while (global_trace.entries < trace_nr_entries) {
2285 if (trace_alloc_page())
2289 max_tr.entries = global_trace.entries;
2291 pr_info("tracer: %d pages allocated for %ld",
2292 pages, trace_nr_entries);
2293 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2294 pr_info(" actual entries %ld\n", global_trace.entries);
2296 tracer_init_debugfs();
2298 trace_init_cmdlines();
2300 register_tracer(&no_tracer);
2301 current_trace = &no_tracer;
2303 /* All seems OK, enable tracing */
2304 tracing_disabled = 0;
2309 for (i-- ; i >= 0; i--) {
2310 struct page *page, *tmp;
2311 struct trace_array_cpu *data = global_trace.data[i];
2314 list_for_each_entry_safe(page, tmp,
2315 &data->trace_pages, lru) {
2316 list_del_init(&page->lru);
2321 #ifdef CONFIG_TRACER_MAX_TRACE
2322 data = max_tr.data[i];
2324 list_for_each_entry_safe(page, tmp,
2325 &data->trace_pages, lru) {
2326 list_del_init(&page->lru);
2334 fs_initcall(tracer_alloc_buffers);