ftrace: trace_entries to dynamically change trace buffer size
[linux-2.6.git] / kernel / trace / trace.c
blob4723e012151d99e558c416c3656351f7c8ef9a61
1 /*
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>
29 #include <linux/fs.h>
31 #include <linux/stacktrace.h>
33 #include "trace.h"
35 unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36 unsigned long __read_mostly tracing_thresh;
38 /* dummy trace to disable tracing */
39 static struct tracer no_tracer __read_mostly =
41 .name = "none",
44 static int trace_alloc_page(void);
45 static int trace_free_page(void);
47 static int tracing_disabled = 1;
49 long
50 ns2usecs(cycle_t nsec)
52 nsec += 500;
53 do_div(nsec, 1000);
54 return nsec;
57 cycle_t ftrace_now(int cpu)
59 return cpu_clock(cpu);
62 static struct trace_array global_trace;
64 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
66 static struct trace_array max_tr;
68 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
70 static int tracer_enabled = 1;
71 static unsigned long trace_nr_entries = 65536UL;
73 static struct tracer *trace_types __read_mostly;
74 static struct tracer *current_trace __read_mostly;
75 static int max_tracer_type_len;
77 static DEFINE_MUTEX(trace_types_lock);
78 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
80 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
82 void trace_wake_up(void)
85 * The runqueue_is_locked() can fail, but this is the best we
86 * have for now:
88 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
89 wake_up(&trace_wait);
92 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
94 static int __init set_nr_entries(char *str)
96 if (!str)
97 return 0;
98 trace_nr_entries = simple_strtoul(str, &str, 0);
99 return 1;
101 __setup("trace_entries=", set_nr_entries);
103 unsigned long nsecs_to_usecs(unsigned long nsecs)
105 return nsecs / 1000;
108 enum trace_flag_type {
109 TRACE_FLAG_IRQS_OFF = 0x01,
110 TRACE_FLAG_NEED_RESCHED = 0x02,
111 TRACE_FLAG_HARDIRQ = 0x04,
112 TRACE_FLAG_SOFTIRQ = 0x08,
115 #define TRACE_ITER_SYM_MASK \
116 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
118 /* These must match the bit postions above */
119 static const char *trace_options[] = {
120 "print-parent",
121 "sym-offset",
122 "sym-addr",
123 "verbose",
124 "raw",
125 "hex",
126 "bin",
127 "block",
128 "stacktrace",
129 "sched-tree",
130 NULL
133 static raw_spinlock_t ftrace_max_lock =
134 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
137 * Copy the new maximum trace into the separate maximum-trace
138 * structure. (this way the maximum trace is permanently saved,
139 * for later retrieval via /debugfs/tracing/latency_trace)
141 static void
142 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
144 struct trace_array_cpu *data = tr->data[cpu];
146 max_tr.cpu = cpu;
147 max_tr.time_start = data->preempt_timestamp;
149 data = max_tr.data[cpu];
150 data->saved_latency = tracing_max_latency;
152 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
153 data->pid = tsk->pid;
154 data->uid = tsk->uid;
155 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
156 data->policy = tsk->policy;
157 data->rt_priority = tsk->rt_priority;
159 /* record this tasks comm */
160 tracing_record_cmdline(current);
163 void check_pages(struct trace_array_cpu *data)
165 struct page *page, *tmp;
167 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
168 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
170 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
171 BUG_ON(page->lru.next->prev != &page->lru);
172 BUG_ON(page->lru.prev->next != &page->lru);
176 void *head_page(struct trace_array_cpu *data)
178 struct page *page;
180 check_pages(data);
181 if (list_empty(&data->trace_pages))
182 return NULL;
184 page = list_entry(data->trace_pages.next, struct page, lru);
185 BUG_ON(&page->lru == &data->trace_pages);
187 return page_address(page);
191 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
193 int len = (PAGE_SIZE - 1) - s->len;
194 va_list ap;
195 int ret;
197 if (!len)
198 return 0;
200 va_start(ap, fmt);
201 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
202 va_end(ap);
204 /* If we can't write it all, don't bother writing anything */
205 if (ret >= len)
206 return 0;
208 s->len += ret;
210 return len;
213 static int
214 trace_seq_puts(struct trace_seq *s, const char *str)
216 int len = strlen(str);
218 if (len > ((PAGE_SIZE - 1) - s->len))
219 return 0;
221 memcpy(s->buffer + s->len, str, len);
222 s->len += len;
224 return len;
227 static int
228 trace_seq_putc(struct trace_seq *s, unsigned char c)
230 if (s->len >= (PAGE_SIZE - 1))
231 return 0;
233 s->buffer[s->len++] = c;
235 return 1;
238 static int
239 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
241 if (len > ((PAGE_SIZE - 1) - s->len))
242 return 0;
244 memcpy(s->buffer + s->len, mem, len);
245 s->len += len;
247 return len;
250 #define HEX_CHARS 17
252 static int
253 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
255 unsigned char hex[HEX_CHARS];
256 unsigned char *data;
257 unsigned char byte;
258 int i, j;
260 BUG_ON(len >= HEX_CHARS);
262 data = mem;
264 #ifdef __BIG_ENDIAN
265 for (i = 0, j = 0; i < len; i++) {
266 #else
267 for (i = len-1, j = 0; i >= 0; i--) {
268 #endif
269 byte = data[i];
271 hex[j] = byte & 0x0f;
272 if (hex[j] >= 10)
273 hex[j] += 'a' - 10;
274 else
275 hex[j] += '0';
276 j++;
278 hex[j] = byte >> 4;
279 if (hex[j] >= 10)
280 hex[j] += 'a' - 10;
281 else
282 hex[j] += '0';
283 j++;
285 hex[j] = ' ';
286 j++;
288 return trace_seq_putmem(s, hex, j);
291 static void
292 trace_seq_reset(struct trace_seq *s)
294 s->len = 0;
297 static void
298 trace_print_seq(struct seq_file *m, struct trace_seq *s)
300 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
302 s->buffer[len] = 0;
303 seq_puts(m, s->buffer);
305 trace_seq_reset(s);
308 static void
309 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
311 struct list_head flip_pages;
313 INIT_LIST_HEAD(&flip_pages);
315 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
316 sizeof(struct trace_array_cpu) -
317 offsetof(struct trace_array_cpu, trace_head_idx));
319 check_pages(tr1);
320 check_pages(tr2);
321 list_splice_init(&tr1->trace_pages, &flip_pages);
322 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
323 list_splice_init(&flip_pages, &tr2->trace_pages);
324 BUG_ON(!list_empty(&flip_pages));
325 check_pages(tr1);
326 check_pages(tr2);
329 void
330 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
332 struct trace_array_cpu *data;
333 int i;
335 WARN_ON_ONCE(!irqs_disabled());
336 __raw_spin_lock(&ftrace_max_lock);
337 /* clear out all the previous traces */
338 for_each_possible_cpu(i) {
339 data = tr->data[i];
340 flip_trace(max_tr.data[i], data);
341 tracing_reset(data);
344 __update_max_tr(tr, tsk, cpu);
345 __raw_spin_unlock(&ftrace_max_lock);
349 * update_max_tr_single - only copy one trace over, and reset the rest
350 * @tr - tracer
351 * @tsk - task with the latency
352 * @cpu - the cpu of the buffer to copy.
354 void
355 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
357 struct trace_array_cpu *data = tr->data[cpu];
358 int i;
360 WARN_ON_ONCE(!irqs_disabled());
361 __raw_spin_lock(&ftrace_max_lock);
362 for_each_possible_cpu(i)
363 tracing_reset(max_tr.data[i]);
365 flip_trace(max_tr.data[cpu], data);
366 tracing_reset(data);
368 __update_max_tr(tr, tsk, cpu);
369 __raw_spin_unlock(&ftrace_max_lock);
372 int register_tracer(struct tracer *type)
374 struct tracer *t;
375 int len;
376 int ret = 0;
378 if (!type->name) {
379 pr_info("Tracer must have a name\n");
380 return -1;
383 mutex_lock(&trace_types_lock);
384 for (t = trace_types; t; t = t->next) {
385 if (strcmp(type->name, t->name) == 0) {
386 /* already found */
387 pr_info("Trace %s already registered\n",
388 type->name);
389 ret = -1;
390 goto out;
394 #ifdef CONFIG_FTRACE_STARTUP_TEST
395 if (type->selftest) {
396 struct tracer *saved_tracer = current_trace;
397 struct trace_array_cpu *data;
398 struct trace_array *tr = &global_trace;
399 int saved_ctrl = tr->ctrl;
400 int i;
402 * Run a selftest on this tracer.
403 * Here we reset the trace buffer, and set the current
404 * tracer to be this tracer. The tracer can then run some
405 * internal tracing to verify that everything is in order.
406 * If we fail, we do not register this tracer.
408 for_each_possible_cpu(i) {
409 data = tr->data[i];
410 if (!head_page(data))
411 continue;
412 tracing_reset(data);
414 current_trace = type;
415 tr->ctrl = 0;
416 /* the test is responsible for initializing and enabling */
417 pr_info("Testing tracer %s: ", type->name);
418 ret = type->selftest(type, tr);
419 /* the test is responsible for resetting too */
420 current_trace = saved_tracer;
421 tr->ctrl = saved_ctrl;
422 if (ret) {
423 printk(KERN_CONT "FAILED!\n");
424 goto out;
426 /* Only reset on passing, to avoid touching corrupted buffers */
427 for_each_possible_cpu(i) {
428 data = tr->data[i];
429 if (!head_page(data))
430 continue;
431 tracing_reset(data);
433 printk(KERN_CONT "PASSED\n");
435 #endif
437 type->next = trace_types;
438 trace_types = type;
439 len = strlen(type->name);
440 if (len > max_tracer_type_len)
441 max_tracer_type_len = len;
443 out:
444 mutex_unlock(&trace_types_lock);
446 return ret;
449 void unregister_tracer(struct tracer *type)
451 struct tracer **t;
452 int len;
454 mutex_lock(&trace_types_lock);
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 if (*t == type)
457 goto found;
459 pr_info("Trace %s not registered\n", type->name);
460 goto out;
462 found:
463 *t = (*t)->next;
464 if (strlen(type->name) != max_tracer_type_len)
465 goto out;
467 max_tracer_type_len = 0;
468 for (t = &trace_types; *t; t = &(*t)->next) {
469 len = strlen((*t)->name);
470 if (len > max_tracer_type_len)
471 max_tracer_type_len = len;
473 out:
474 mutex_unlock(&trace_types_lock);
477 void tracing_reset(struct trace_array_cpu *data)
479 data->trace_idx = 0;
480 data->trace_head = data->trace_tail = head_page(data);
481 data->trace_head_idx = 0;
482 data->trace_tail_idx = 0;
485 #define SAVED_CMDLINES 128
486 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
487 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
488 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
489 static int cmdline_idx;
490 static DEFINE_SPINLOCK(trace_cmdline_lock);
491 atomic_t trace_record_cmdline_disabled;
493 static void trace_init_cmdlines(void)
495 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
496 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
497 cmdline_idx = 0;
500 void trace_stop_cmdline_recording(void);
502 static void trace_save_cmdline(struct task_struct *tsk)
504 unsigned map;
505 unsigned idx;
507 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
508 return;
511 * It's not the end of the world if we don't get
512 * the lock, but we also don't want to spin
513 * nor do we want to disable interrupts,
514 * so if we miss here, then better luck next time.
516 if (!spin_trylock(&trace_cmdline_lock))
517 return;
519 idx = map_pid_to_cmdline[tsk->pid];
520 if (idx >= SAVED_CMDLINES) {
521 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
523 map = map_cmdline_to_pid[idx];
524 if (map <= PID_MAX_DEFAULT)
525 map_pid_to_cmdline[map] = (unsigned)-1;
527 map_pid_to_cmdline[tsk->pid] = idx;
529 cmdline_idx = idx;
532 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
534 spin_unlock(&trace_cmdline_lock);
537 static char *trace_find_cmdline(int pid)
539 char *cmdline = "<...>";
540 unsigned map;
542 if (!pid)
543 return "<idle>";
545 if (pid > PID_MAX_DEFAULT)
546 goto out;
548 map = map_pid_to_cmdline[pid];
549 if (map >= SAVED_CMDLINES)
550 goto out;
552 cmdline = saved_cmdlines[map];
554 out:
555 return cmdline;
558 void tracing_record_cmdline(struct task_struct *tsk)
560 if (atomic_read(&trace_record_cmdline_disabled))
561 return;
563 trace_save_cmdline(tsk);
566 static inline struct list_head *
567 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
570 * Roundrobin - but skip the head (which is not a real page):
572 next = next->next;
573 if (unlikely(next == &data->trace_pages))
574 next = next->next;
575 BUG_ON(next == &data->trace_pages);
577 return next;
580 static inline void *
581 trace_next_page(struct trace_array_cpu *data, void *addr)
583 struct list_head *next;
584 struct page *page;
586 page = virt_to_page(addr);
588 next = trace_next_list(data, &page->lru);
589 page = list_entry(next, struct page, lru);
591 return page_address(page);
594 static inline struct trace_entry *
595 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
597 unsigned long idx, idx_next;
598 struct trace_entry *entry;
600 data->trace_idx++;
601 idx = data->trace_head_idx;
602 idx_next = idx + 1;
604 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
606 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
608 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
609 data->trace_head = trace_next_page(data, data->trace_head);
610 idx_next = 0;
613 if (data->trace_head == data->trace_tail &&
614 idx_next == data->trace_tail_idx) {
615 /* overrun */
616 data->trace_tail_idx++;
617 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
618 data->trace_tail =
619 trace_next_page(data, data->trace_tail);
620 data->trace_tail_idx = 0;
624 data->trace_head_idx = idx_next;
626 return entry;
629 static inline void
630 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
632 struct task_struct *tsk = current;
633 unsigned long pc;
635 pc = preempt_count();
637 entry->preempt_count = pc & 0xff;
638 entry->pid = (tsk) ? tsk->pid : 0;
639 entry->t = ftrace_now(raw_smp_processor_id());
640 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
641 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
642 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
643 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
646 void
647 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
648 unsigned long ip, unsigned long parent_ip, unsigned long flags)
650 struct trace_entry *entry;
651 unsigned long irq_flags;
653 raw_local_irq_save(irq_flags);
654 __raw_spin_lock(&data->lock);
655 entry = tracing_get_trace_entry(tr, data);
656 tracing_generic_entry_update(entry, flags);
657 entry->type = TRACE_FN;
658 entry->fn.ip = ip;
659 entry->fn.parent_ip = parent_ip;
660 __raw_spin_unlock(&data->lock);
661 raw_local_irq_restore(irq_flags);
664 void
665 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
666 unsigned long ip, unsigned long parent_ip, unsigned long flags)
668 if (likely(!atomic_read(&data->disabled)))
669 trace_function(tr, data, ip, parent_ip, flags);
672 void
673 __trace_special(void *__tr, void *__data,
674 unsigned long arg1, unsigned long arg2, unsigned long arg3)
676 struct trace_array_cpu *data = __data;
677 struct trace_array *tr = __tr;
678 struct trace_entry *entry;
679 unsigned long irq_flags;
681 raw_local_irq_save(irq_flags);
682 __raw_spin_lock(&data->lock);
683 entry = tracing_get_trace_entry(tr, data);
684 tracing_generic_entry_update(entry, 0);
685 entry->type = TRACE_SPECIAL;
686 entry->special.arg1 = arg1;
687 entry->special.arg2 = arg2;
688 entry->special.arg3 = arg3;
689 __raw_spin_unlock(&data->lock);
690 raw_local_irq_restore(irq_flags);
692 trace_wake_up();
695 void __trace_stack(struct trace_array *tr,
696 struct trace_array_cpu *data,
697 unsigned long flags,
698 int skip)
700 struct trace_entry *entry;
701 struct stack_trace trace;
703 if (!(trace_flags & TRACE_ITER_STACKTRACE))
704 return;
706 entry = tracing_get_trace_entry(tr, data);
707 tracing_generic_entry_update(entry, flags);
708 entry->type = TRACE_STACK;
710 memset(&entry->stack, 0, sizeof(entry->stack));
712 trace.nr_entries = 0;
713 trace.max_entries = FTRACE_STACK_ENTRIES;
714 trace.skip = skip;
715 trace.entries = entry->stack.caller;
717 save_stack_trace(&trace);
720 void
721 tracing_sched_switch_trace(struct trace_array *tr,
722 struct trace_array_cpu *data,
723 struct task_struct *prev,
724 struct task_struct *next,
725 unsigned long flags)
727 struct trace_entry *entry;
728 unsigned long irq_flags;
730 raw_local_irq_save(irq_flags);
731 __raw_spin_lock(&data->lock);
732 entry = tracing_get_trace_entry(tr, data);
733 tracing_generic_entry_update(entry, flags);
734 entry->type = TRACE_CTX;
735 entry->ctx.prev_pid = prev->pid;
736 entry->ctx.prev_prio = prev->prio;
737 entry->ctx.prev_state = prev->state;
738 entry->ctx.next_pid = next->pid;
739 entry->ctx.next_prio = next->prio;
740 entry->ctx.next_state = next->state;
741 __trace_stack(tr, data, flags, 4);
742 __raw_spin_unlock(&data->lock);
743 raw_local_irq_restore(irq_flags);
746 void
747 tracing_sched_wakeup_trace(struct trace_array *tr,
748 struct trace_array_cpu *data,
749 struct task_struct *wakee,
750 struct task_struct *curr,
751 unsigned long flags)
753 struct trace_entry *entry;
754 unsigned long irq_flags;
756 raw_local_irq_save(irq_flags);
757 __raw_spin_lock(&data->lock);
758 entry = tracing_get_trace_entry(tr, data);
759 tracing_generic_entry_update(entry, flags);
760 entry->type = TRACE_WAKE;
761 entry->ctx.prev_pid = curr->pid;
762 entry->ctx.prev_prio = curr->prio;
763 entry->ctx.prev_state = curr->state;
764 entry->ctx.next_pid = wakee->pid;
765 entry->ctx.next_prio = wakee->prio;
766 entry->ctx.next_state = wakee->state;
767 __trace_stack(tr, data, flags, 5);
768 __raw_spin_unlock(&data->lock);
769 raw_local_irq_restore(irq_flags);
771 trace_wake_up();
774 #ifdef CONFIG_FTRACE
775 static void
776 function_trace_call(unsigned long ip, unsigned long parent_ip)
778 struct trace_array *tr = &global_trace;
779 struct trace_array_cpu *data;
780 unsigned long flags;
781 long disabled;
782 int cpu;
784 if (unlikely(!tracer_enabled))
785 return;
787 local_irq_save(flags);
788 cpu = raw_smp_processor_id();
789 data = tr->data[cpu];
790 disabled = atomic_inc_return(&data->disabled);
792 if (likely(disabled == 1))
793 trace_function(tr, data, ip, parent_ip, flags);
795 atomic_dec(&data->disabled);
796 local_irq_restore(flags);
799 static struct ftrace_ops trace_ops __read_mostly =
801 .func = function_trace_call,
804 void tracing_start_function_trace(void)
806 register_ftrace_function(&trace_ops);
809 void tracing_stop_function_trace(void)
811 unregister_ftrace_function(&trace_ops);
813 #endif
815 enum trace_file_type {
816 TRACE_FILE_LAT_FMT = 1,
819 static struct trace_entry *
820 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
821 struct trace_iterator *iter, int cpu)
823 struct page *page;
824 struct trace_entry *array;
826 if (iter->next_idx[cpu] >= tr->entries ||
827 iter->next_idx[cpu] >= data->trace_idx ||
828 (data->trace_head == data->trace_tail &&
829 data->trace_head_idx == data->trace_tail_idx))
830 return NULL;
832 if (!iter->next_page[cpu]) {
833 /* Initialize the iterator for this cpu trace buffer */
834 WARN_ON(!data->trace_tail);
835 page = virt_to_page(data->trace_tail);
836 iter->next_page[cpu] = &page->lru;
837 iter->next_page_idx[cpu] = data->trace_tail_idx;
840 page = list_entry(iter->next_page[cpu], struct page, lru);
841 BUG_ON(&data->trace_pages == &page->lru);
843 array = page_address(page);
845 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
846 return &array[iter->next_page_idx[cpu]];
849 static struct trace_entry *
850 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
852 struct trace_array *tr = iter->tr;
853 struct trace_entry *ent, *next = NULL;
854 int next_cpu = -1;
855 int cpu;
857 for_each_possible_cpu(cpu) {
858 if (!head_page(tr->data[cpu]))
859 continue;
860 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
862 * Pick the entry with the smallest timestamp:
864 if (ent && (!next || ent->t < next->t)) {
865 next = ent;
866 next_cpu = cpu;
870 if (ent_cpu)
871 *ent_cpu = next_cpu;
873 return next;
876 static void trace_iterator_increment(struct trace_iterator *iter)
878 iter->idx++;
879 iter->next_idx[iter->cpu]++;
880 iter->next_page_idx[iter->cpu]++;
882 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
883 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
885 iter->next_page_idx[iter->cpu] = 0;
886 iter->next_page[iter->cpu] =
887 trace_next_list(data, iter->next_page[iter->cpu]);
891 static void trace_consume(struct trace_iterator *iter)
893 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
895 data->trace_tail_idx++;
896 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
897 data->trace_tail = trace_next_page(data, data->trace_tail);
898 data->trace_tail_idx = 0;
901 /* Check if we empty it, then reset the index */
902 if (data->trace_head == data->trace_tail &&
903 data->trace_head_idx == data->trace_tail_idx)
904 data->trace_idx = 0;
907 static void *find_next_entry_inc(struct trace_iterator *iter)
909 struct trace_entry *next;
910 int next_cpu = -1;
912 next = find_next_entry(iter, &next_cpu);
914 iter->prev_ent = iter->ent;
915 iter->prev_cpu = iter->cpu;
917 iter->ent = next;
918 iter->cpu = next_cpu;
920 if (next)
921 trace_iterator_increment(iter);
923 return next ? iter : NULL;
926 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
928 struct trace_iterator *iter = m->private;
929 void *last_ent = iter->ent;
930 int i = (int)*pos;
931 void *ent;
933 (*pos)++;
935 /* can't go backwards */
936 if (iter->idx > i)
937 return NULL;
939 if (iter->idx < 0)
940 ent = find_next_entry_inc(iter);
941 else
942 ent = iter;
944 while (ent && iter->idx < i)
945 ent = find_next_entry_inc(iter);
947 iter->pos = *pos;
949 if (last_ent && !ent)
950 seq_puts(m, "\n\nvim:ft=help\n");
952 return ent;
955 static void *s_start(struct seq_file *m, loff_t *pos)
957 struct trace_iterator *iter = m->private;
958 void *p = NULL;
959 loff_t l = 0;
960 int i;
962 mutex_lock(&trace_types_lock);
964 if (!current_trace || current_trace != iter->trace) {
965 mutex_unlock(&trace_types_lock);
966 return NULL;
969 atomic_inc(&trace_record_cmdline_disabled);
971 /* let the tracer grab locks here if needed */
972 if (current_trace->start)
973 current_trace->start(iter);
975 if (*pos != iter->pos) {
976 iter->ent = NULL;
977 iter->cpu = 0;
978 iter->idx = -1;
979 iter->prev_ent = NULL;
980 iter->prev_cpu = -1;
982 for_each_possible_cpu(i) {
983 iter->next_idx[i] = 0;
984 iter->next_page[i] = NULL;
987 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
990 } else {
991 l = *pos - 1;
992 p = s_next(m, p, &l);
995 return p;
998 static void s_stop(struct seq_file *m, void *p)
1000 struct trace_iterator *iter = m->private;
1002 atomic_dec(&trace_record_cmdline_disabled);
1004 /* let the tracer release locks here if needed */
1005 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1006 iter->trace->stop(iter);
1008 mutex_unlock(&trace_types_lock);
1011 static int
1012 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1014 #ifdef CONFIG_KALLSYMS
1015 char str[KSYM_SYMBOL_LEN];
1017 kallsyms_lookup(address, NULL, NULL, NULL, str);
1019 return trace_seq_printf(s, fmt, str);
1020 #endif
1021 return 1;
1024 static int
1025 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1026 unsigned long address)
1028 #ifdef CONFIG_KALLSYMS
1029 char str[KSYM_SYMBOL_LEN];
1031 sprint_symbol(str, address);
1032 return trace_seq_printf(s, fmt, str);
1033 #endif
1034 return 1;
1037 #ifndef CONFIG_64BIT
1038 # define IP_FMT "%08lx"
1039 #else
1040 # define IP_FMT "%016lx"
1041 #endif
1043 static int
1044 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1046 int ret;
1048 if (!ip)
1049 return trace_seq_printf(s, "0");
1051 if (sym_flags & TRACE_ITER_SYM_OFFSET)
1052 ret = seq_print_sym_offset(s, "%s", ip);
1053 else
1054 ret = seq_print_sym_short(s, "%s", ip);
1056 if (!ret)
1057 return 0;
1059 if (sym_flags & TRACE_ITER_SYM_ADDR)
1060 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1061 return ret;
1064 static void print_lat_help_header(struct seq_file *m)
1066 seq_puts(m, "# _------=> CPU# \n");
1067 seq_puts(m, "# / _-----=> irqs-off \n");
1068 seq_puts(m, "# | / _----=> need-resched \n");
1069 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1070 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1071 seq_puts(m, "# |||| / \n");
1072 seq_puts(m, "# ||||| delay \n");
1073 seq_puts(m, "# cmd pid ||||| time | caller \n");
1074 seq_puts(m, "# \\ / ||||| \\ | / \n");
1077 static void print_func_help_header(struct seq_file *m)
1079 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1080 seq_puts(m, "# | | | | |\n");
1084 static void
1085 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1087 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1088 struct trace_array *tr = iter->tr;
1089 struct trace_array_cpu *data = tr->data[tr->cpu];
1090 struct tracer *type = current_trace;
1091 unsigned long total = 0;
1092 unsigned long entries = 0;
1093 int cpu;
1094 const char *name = "preemption";
1096 if (type)
1097 name = type->name;
1099 for_each_possible_cpu(cpu) {
1100 if (head_page(tr->data[cpu])) {
1101 total += tr->data[cpu]->trace_idx;
1102 if (tr->data[cpu]->trace_idx > tr->entries)
1103 entries += tr->entries;
1104 else
1105 entries += tr->data[cpu]->trace_idx;
1109 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1110 name, UTS_RELEASE);
1111 seq_puts(m, "-----------------------------------"
1112 "---------------------------------\n");
1113 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1114 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1115 nsecs_to_usecs(data->saved_latency),
1116 entries,
1117 total,
1118 tr->cpu,
1119 #if defined(CONFIG_PREEMPT_NONE)
1120 "server",
1121 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1122 "desktop",
1123 #elif defined(CONFIG_PREEMPT_DESKTOP)
1124 "preempt",
1125 #else
1126 "unknown",
1127 #endif
1128 /* These are reserved for later use */
1129 0, 0, 0, 0);
1130 #ifdef CONFIG_SMP
1131 seq_printf(m, " #P:%d)\n", num_online_cpus());
1132 #else
1133 seq_puts(m, ")\n");
1134 #endif
1135 seq_puts(m, " -----------------\n");
1136 seq_printf(m, " | task: %.16s-%d "
1137 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1138 data->comm, data->pid, data->uid, data->nice,
1139 data->policy, data->rt_priority);
1140 seq_puts(m, " -----------------\n");
1142 if (data->critical_start) {
1143 seq_puts(m, " => started at: ");
1144 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1145 trace_print_seq(m, &iter->seq);
1146 seq_puts(m, "\n => ended at: ");
1147 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1148 trace_print_seq(m, &iter->seq);
1149 seq_puts(m, "\n");
1152 seq_puts(m, "\n");
1155 static void
1156 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1158 int hardirq, softirq;
1159 char *comm;
1161 comm = trace_find_cmdline(entry->pid);
1163 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1164 trace_seq_printf(s, "%d", cpu);
1165 trace_seq_printf(s, "%c%c",
1166 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1167 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1169 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1170 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1171 if (hardirq && softirq)
1172 trace_seq_putc(s, 'H');
1173 else {
1174 if (hardirq)
1175 trace_seq_putc(s, 'h');
1176 else {
1177 if (softirq)
1178 trace_seq_putc(s, 's');
1179 else
1180 trace_seq_putc(s, '.');
1184 if (entry->preempt_count)
1185 trace_seq_printf(s, "%x", entry->preempt_count);
1186 else
1187 trace_seq_puts(s, ".");
1190 unsigned long preempt_mark_thresh = 100;
1192 static void
1193 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1194 unsigned long rel_usecs)
1196 trace_seq_printf(s, " %4lldus", abs_usecs);
1197 if (rel_usecs > preempt_mark_thresh)
1198 trace_seq_puts(s, "!: ");
1199 else if (rel_usecs > 1)
1200 trace_seq_puts(s, "+: ");
1201 else
1202 trace_seq_puts(s, " : ");
1205 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1207 static int
1208 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1210 struct trace_seq *s = &iter->seq;
1211 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1212 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1213 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1214 struct trace_entry *entry = iter->ent;
1215 unsigned long abs_usecs;
1216 unsigned long rel_usecs;
1217 char *comm;
1218 int S, T;
1219 int i;
1220 unsigned state;
1222 if (!next_entry)
1223 next_entry = entry;
1224 rel_usecs = ns2usecs(next_entry->t - entry->t);
1225 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1227 if (verbose) {
1228 comm = trace_find_cmdline(entry->pid);
1229 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1230 " %ld.%03ldms (+%ld.%03ldms): ",
1231 comm,
1232 entry->pid, cpu, entry->flags,
1233 entry->preempt_count, trace_idx,
1234 ns2usecs(entry->t),
1235 abs_usecs/1000,
1236 abs_usecs % 1000, rel_usecs/1000,
1237 rel_usecs % 1000);
1238 } else {
1239 lat_print_generic(s, entry, cpu);
1240 lat_print_timestamp(s, abs_usecs, rel_usecs);
1242 switch (entry->type) {
1243 case TRACE_FN:
1244 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1245 trace_seq_puts(s, " (");
1246 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1247 trace_seq_puts(s, ")\n");
1248 break;
1249 case TRACE_CTX:
1250 case TRACE_WAKE:
1251 T = entry->ctx.next_state < sizeof(state_to_char) ?
1252 state_to_char[entry->ctx.next_state] : 'X';
1254 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1255 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1256 comm = trace_find_cmdline(entry->ctx.next_pid);
1257 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1258 entry->ctx.prev_pid,
1259 entry->ctx.prev_prio,
1260 S, entry->type == TRACE_CTX ? "==>" : " +",
1261 entry->ctx.next_pid,
1262 entry->ctx.next_prio,
1263 T, comm);
1264 break;
1265 case TRACE_SPECIAL:
1266 trace_seq_printf(s, "# %ld %ld %ld\n",
1267 entry->special.arg1,
1268 entry->special.arg2,
1269 entry->special.arg3);
1270 break;
1271 case TRACE_STACK:
1272 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1273 if (i)
1274 trace_seq_puts(s, " <= ");
1275 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1277 trace_seq_puts(s, "\n");
1278 break;
1279 default:
1280 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1282 return 1;
1285 static int print_trace_fmt(struct trace_iterator *iter)
1287 struct trace_seq *s = &iter->seq;
1288 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1289 struct trace_entry *entry;
1290 unsigned long usec_rem;
1291 unsigned long long t;
1292 unsigned long secs;
1293 char *comm;
1294 int ret;
1295 int S, T;
1296 int i;
1298 entry = iter->ent;
1300 comm = trace_find_cmdline(iter->ent->pid);
1302 t = ns2usecs(entry->t);
1303 usec_rem = do_div(t, 1000000ULL);
1304 secs = (unsigned long)t;
1306 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1307 if (!ret)
1308 return 0;
1309 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1310 if (!ret)
1311 return 0;
1312 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1313 if (!ret)
1314 return 0;
1316 switch (entry->type) {
1317 case TRACE_FN:
1318 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1319 if (!ret)
1320 return 0;
1321 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1322 entry->fn.parent_ip) {
1323 ret = trace_seq_printf(s, " <-");
1324 if (!ret)
1325 return 0;
1326 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1327 sym_flags);
1328 if (!ret)
1329 return 0;
1331 ret = trace_seq_printf(s, "\n");
1332 if (!ret)
1333 return 0;
1334 break;
1335 case TRACE_CTX:
1336 case TRACE_WAKE:
1337 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1338 state_to_char[entry->ctx.prev_state] : 'X';
1339 T = entry->ctx.next_state < sizeof(state_to_char) ?
1340 state_to_char[entry->ctx.next_state] : 'X';
1341 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1342 entry->ctx.prev_pid,
1343 entry->ctx.prev_prio,
1345 entry->type == TRACE_CTX ? "==>" : " +",
1346 entry->ctx.next_pid,
1347 entry->ctx.next_prio,
1349 if (!ret)
1350 return 0;
1351 break;
1352 case TRACE_SPECIAL:
1353 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1354 entry->special.arg1,
1355 entry->special.arg2,
1356 entry->special.arg3);
1357 if (!ret)
1358 return 0;
1359 break;
1360 case TRACE_STACK:
1361 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1362 if (i) {
1363 ret = trace_seq_puts(s, " <= ");
1364 if (!ret)
1365 return 0;
1367 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1368 sym_flags);
1369 if (!ret)
1370 return 0;
1372 ret = trace_seq_puts(s, "\n");
1373 if (!ret)
1374 return 0;
1375 break;
1377 return 1;
1380 static int print_raw_fmt(struct trace_iterator *iter)
1382 struct trace_seq *s = &iter->seq;
1383 struct trace_entry *entry;
1384 int ret;
1385 int S, T;
1387 entry = iter->ent;
1389 ret = trace_seq_printf(s, "%d %d %llu ",
1390 entry->pid, iter->cpu, entry->t);
1391 if (!ret)
1392 return 0;
1394 switch (entry->type) {
1395 case TRACE_FN:
1396 ret = trace_seq_printf(s, "%x %x\n",
1397 entry->fn.ip, entry->fn.parent_ip);
1398 if (!ret)
1399 return 0;
1400 break;
1401 case TRACE_CTX:
1402 case TRACE_WAKE:
1403 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1404 state_to_char[entry->ctx.prev_state] : 'X';
1405 T = entry->ctx.next_state < sizeof(state_to_char) ?
1406 state_to_char[entry->ctx.next_state] : 'X';
1407 if (entry->type == TRACE_WAKE)
1408 S = '+';
1409 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1410 entry->ctx.prev_pid,
1411 entry->ctx.prev_prio,
1413 entry->ctx.next_pid,
1414 entry->ctx.next_prio,
1416 if (!ret)
1417 return 0;
1418 break;
1419 case TRACE_SPECIAL:
1420 case TRACE_STACK:
1421 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1422 entry->special.arg1,
1423 entry->special.arg2,
1424 entry->special.arg3);
1425 if (!ret)
1426 return 0;
1427 break;
1429 return 1;
1432 #define SEQ_PUT_FIELD_RET(s, x) \
1433 do { \
1434 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1435 return 0; \
1436 } while (0)
1438 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1439 do { \
1440 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1441 return 0; \
1442 } while (0)
1444 static int print_hex_fmt(struct trace_iterator *iter)
1446 struct trace_seq *s = &iter->seq;
1447 unsigned char newline = '\n';
1448 struct trace_entry *entry;
1449 int S, T;
1451 entry = iter->ent;
1453 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1454 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1455 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1457 switch (entry->type) {
1458 case TRACE_FN:
1459 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1461 break;
1462 case TRACE_CTX:
1463 case TRACE_WAKE:
1464 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1465 state_to_char[entry->ctx.prev_state] : 'X';
1466 T = entry->ctx.next_state < sizeof(state_to_char) ?
1467 state_to_char[entry->ctx.next_state] : 'X';
1468 if (entry->type == TRACE_WAKE)
1469 S = '+';
1470 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1471 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1472 SEQ_PUT_HEX_FIELD_RET(s, S);
1473 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1474 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1475 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1476 SEQ_PUT_HEX_FIELD_RET(s, T);
1477 break;
1478 case TRACE_SPECIAL:
1479 case TRACE_STACK:
1480 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1481 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1482 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1483 break;
1485 SEQ_PUT_FIELD_RET(s, newline);
1487 return 1;
1490 static int print_bin_fmt(struct trace_iterator *iter)
1492 struct trace_seq *s = &iter->seq;
1493 struct trace_entry *entry;
1495 entry = iter->ent;
1497 SEQ_PUT_FIELD_RET(s, entry->pid);
1498 SEQ_PUT_FIELD_RET(s, entry->cpu);
1499 SEQ_PUT_FIELD_RET(s, entry->t);
1501 switch (entry->type) {
1502 case TRACE_FN:
1503 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1504 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1505 break;
1506 case TRACE_CTX:
1507 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1508 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1509 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1510 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1511 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1512 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1513 break;
1514 case TRACE_SPECIAL:
1515 case TRACE_STACK:
1516 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1517 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1518 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1519 break;
1521 return 1;
1524 static int trace_empty(struct trace_iterator *iter)
1526 struct trace_array_cpu *data;
1527 int cpu;
1529 for_each_possible_cpu(cpu) {
1530 data = iter->tr->data[cpu];
1532 if (head_page(data) && data->trace_idx &&
1533 (data->trace_tail != data->trace_head ||
1534 data->trace_tail_idx != data->trace_head_idx))
1535 return 0;
1537 return 1;
1540 static int print_trace_line(struct trace_iterator *iter)
1542 if (iter->trace && iter->trace->print_line)
1543 return iter->trace->print_line(iter);
1545 if (trace_flags & TRACE_ITER_BIN)
1546 return print_bin_fmt(iter);
1548 if (trace_flags & TRACE_ITER_HEX)
1549 return print_hex_fmt(iter);
1551 if (trace_flags & TRACE_ITER_RAW)
1552 return print_raw_fmt(iter);
1554 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1555 return print_lat_fmt(iter, iter->idx, iter->cpu);
1557 return print_trace_fmt(iter);
1560 static int s_show(struct seq_file *m, void *v)
1562 struct trace_iterator *iter = v;
1564 if (iter->ent == NULL) {
1565 if (iter->tr) {
1566 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1567 seq_puts(m, "#\n");
1569 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1570 /* print nothing if the buffers are empty */
1571 if (trace_empty(iter))
1572 return 0;
1573 print_trace_header(m, iter);
1574 if (!(trace_flags & TRACE_ITER_VERBOSE))
1575 print_lat_help_header(m);
1576 } else {
1577 if (!(trace_flags & TRACE_ITER_VERBOSE))
1578 print_func_help_header(m);
1580 } else {
1581 print_trace_line(iter);
1582 trace_print_seq(m, &iter->seq);
1585 return 0;
1588 static struct seq_operations tracer_seq_ops = {
1589 .start = s_start,
1590 .next = s_next,
1591 .stop = s_stop,
1592 .show = s_show,
1595 static struct trace_iterator *
1596 __tracing_open(struct inode *inode, struct file *file, int *ret)
1598 struct trace_iterator *iter;
1600 if (tracing_disabled) {
1601 *ret = -ENODEV;
1602 return NULL;
1605 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1606 if (!iter) {
1607 *ret = -ENOMEM;
1608 goto out;
1611 mutex_lock(&trace_types_lock);
1612 if (current_trace && current_trace->print_max)
1613 iter->tr = &max_tr;
1614 else
1615 iter->tr = inode->i_private;
1616 iter->trace = current_trace;
1617 iter->pos = -1;
1619 /* TODO stop tracer */
1620 *ret = seq_open(file, &tracer_seq_ops);
1621 if (!*ret) {
1622 struct seq_file *m = file->private_data;
1623 m->private = iter;
1625 /* stop the trace while dumping */
1626 if (iter->tr->ctrl)
1627 tracer_enabled = 0;
1629 if (iter->trace && iter->trace->open)
1630 iter->trace->open(iter);
1631 } else {
1632 kfree(iter);
1633 iter = NULL;
1635 mutex_unlock(&trace_types_lock);
1637 out:
1638 return iter;
1641 int tracing_open_generic(struct inode *inode, struct file *filp)
1643 if (tracing_disabled)
1644 return -ENODEV;
1646 filp->private_data = inode->i_private;
1647 return 0;
1650 int tracing_release(struct inode *inode, struct file *file)
1652 struct seq_file *m = (struct seq_file *)file->private_data;
1653 struct trace_iterator *iter = m->private;
1655 mutex_lock(&trace_types_lock);
1656 if (iter->trace && iter->trace->close)
1657 iter->trace->close(iter);
1659 /* reenable tracing if it was previously enabled */
1660 if (iter->tr->ctrl)
1661 tracer_enabled = 1;
1662 mutex_unlock(&trace_types_lock);
1664 seq_release(inode, file);
1665 kfree(iter);
1666 return 0;
1669 static int tracing_open(struct inode *inode, struct file *file)
1671 int ret;
1673 __tracing_open(inode, file, &ret);
1675 return ret;
1678 static int tracing_lt_open(struct inode *inode, struct file *file)
1680 struct trace_iterator *iter;
1681 int ret;
1683 iter = __tracing_open(inode, file, &ret);
1685 if (!ret)
1686 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1688 return ret;
1692 static void *
1693 t_next(struct seq_file *m, void *v, loff_t *pos)
1695 struct tracer *t = m->private;
1697 (*pos)++;
1699 if (t)
1700 t = t->next;
1702 m->private = t;
1704 return t;
1707 static void *t_start(struct seq_file *m, loff_t *pos)
1709 struct tracer *t = m->private;
1710 loff_t l = 0;
1712 mutex_lock(&trace_types_lock);
1713 for (; t && l < *pos; t = t_next(m, t, &l))
1716 return t;
1719 static void t_stop(struct seq_file *m, void *p)
1721 mutex_unlock(&trace_types_lock);
1724 static int t_show(struct seq_file *m, void *v)
1726 struct tracer *t = v;
1728 if (!t)
1729 return 0;
1731 seq_printf(m, "%s", t->name);
1732 if (t->next)
1733 seq_putc(m, ' ');
1734 else
1735 seq_putc(m, '\n');
1737 return 0;
1740 static struct seq_operations show_traces_seq_ops = {
1741 .start = t_start,
1742 .next = t_next,
1743 .stop = t_stop,
1744 .show = t_show,
1747 static int show_traces_open(struct inode *inode, struct file *file)
1749 int ret;
1751 if (tracing_disabled)
1752 return -ENODEV;
1754 ret = seq_open(file, &show_traces_seq_ops);
1755 if (!ret) {
1756 struct seq_file *m = file->private_data;
1757 m->private = trace_types;
1760 return ret;
1763 static struct file_operations tracing_fops = {
1764 .open = tracing_open,
1765 .read = seq_read,
1766 .llseek = seq_lseek,
1767 .release = tracing_release,
1770 static struct file_operations tracing_lt_fops = {
1771 .open = tracing_lt_open,
1772 .read = seq_read,
1773 .llseek = seq_lseek,
1774 .release = tracing_release,
1777 static struct file_operations show_traces_fops = {
1778 .open = show_traces_open,
1779 .read = seq_read,
1780 .release = seq_release,
1784 * Only trace on a CPU if the bitmask is set:
1786 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1789 * When tracing/tracing_cpu_mask is modified then this holds
1790 * the new bitmask we are about to install:
1792 static cpumask_t tracing_cpumask_new;
1795 * The tracer itself will not take this lock, but still we want
1796 * to provide a consistent cpumask to user-space:
1798 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1801 * Temporary storage for the character representation of the
1802 * CPU bitmask (and one more byte for the newline):
1804 static char mask_str[NR_CPUS + 1];
1806 static ssize_t
1807 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1808 size_t count, loff_t *ppos)
1810 int len;
1812 mutex_lock(&tracing_cpumask_update_lock);
1814 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1815 if (count - len < 2) {
1816 count = -EINVAL;
1817 goto out_err;
1819 len += sprintf(mask_str + len, "\n");
1820 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1822 out_err:
1823 mutex_unlock(&tracing_cpumask_update_lock);
1825 return count;
1828 static ssize_t
1829 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1830 size_t count, loff_t *ppos)
1832 int err, cpu;
1834 mutex_lock(&tracing_cpumask_update_lock);
1835 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1836 if (err)
1837 goto err_unlock;
1839 raw_local_irq_disable();
1840 __raw_spin_lock(&ftrace_max_lock);
1841 for_each_possible_cpu(cpu) {
1843 * Increase/decrease the disabled counter if we are
1844 * about to flip a bit in the cpumask:
1846 if (cpu_isset(cpu, tracing_cpumask) &&
1847 !cpu_isset(cpu, tracing_cpumask_new)) {
1848 atomic_inc(&global_trace.data[cpu]->disabled);
1850 if (!cpu_isset(cpu, tracing_cpumask) &&
1851 cpu_isset(cpu, tracing_cpumask_new)) {
1852 atomic_dec(&global_trace.data[cpu]->disabled);
1855 __raw_spin_unlock(&ftrace_max_lock);
1856 raw_local_irq_enable();
1858 tracing_cpumask = tracing_cpumask_new;
1860 mutex_unlock(&tracing_cpumask_update_lock);
1862 return count;
1864 err_unlock:
1865 mutex_unlock(&tracing_cpumask_update_lock);
1867 return err;
1870 static struct file_operations tracing_cpumask_fops = {
1871 .open = tracing_open_generic,
1872 .read = tracing_cpumask_read,
1873 .write = tracing_cpumask_write,
1876 static ssize_t
1877 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1878 size_t cnt, loff_t *ppos)
1880 char *buf;
1881 int r = 0;
1882 int len = 0;
1883 int i;
1885 /* calulate max size */
1886 for (i = 0; trace_options[i]; i++) {
1887 len += strlen(trace_options[i]);
1888 len += 3; /* "no" and space */
1891 /* +2 for \n and \0 */
1892 buf = kmalloc(len + 2, GFP_KERNEL);
1893 if (!buf)
1894 return -ENOMEM;
1896 for (i = 0; trace_options[i]; i++) {
1897 if (trace_flags & (1 << i))
1898 r += sprintf(buf + r, "%s ", trace_options[i]);
1899 else
1900 r += sprintf(buf + r, "no%s ", trace_options[i]);
1903 r += sprintf(buf + r, "\n");
1904 WARN_ON(r >= len + 2);
1906 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1908 kfree(buf);
1910 return r;
1913 static ssize_t
1914 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1915 size_t cnt, loff_t *ppos)
1917 char buf[64];
1918 char *cmp = buf;
1919 int neg = 0;
1920 int i;
1922 if (cnt > 63)
1923 cnt = 63;
1925 if (copy_from_user(&buf, ubuf, cnt))
1926 return -EFAULT;
1928 buf[cnt] = 0;
1930 if (strncmp(buf, "no", 2) == 0) {
1931 neg = 1;
1932 cmp += 2;
1935 for (i = 0; trace_options[i]; i++) {
1936 int len = strlen(trace_options[i]);
1938 if (strncmp(cmp, trace_options[i], len) == 0) {
1939 if (neg)
1940 trace_flags &= ~(1 << i);
1941 else
1942 trace_flags |= (1 << i);
1943 break;
1947 * If no option could be set, return an error:
1949 if (!trace_options[i])
1950 return -EINVAL;
1952 filp->f_pos += cnt;
1954 return cnt;
1957 static struct file_operations tracing_iter_fops = {
1958 .open = tracing_open_generic,
1959 .read = tracing_iter_ctrl_read,
1960 .write = tracing_iter_ctrl_write,
1963 static const char readme_msg[] =
1964 "tracing mini-HOWTO:\n\n"
1965 "# mkdir /debug\n"
1966 "# mount -t debugfs nodev /debug\n\n"
1967 "# cat /debug/tracing/available_tracers\n"
1968 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1969 "# cat /debug/tracing/current_tracer\n"
1970 "none\n"
1971 "# echo sched_switch > /debug/tracing/current_tracer\n"
1972 "# cat /debug/tracing/current_tracer\n"
1973 "sched_switch\n"
1974 "# cat /debug/tracing/iter_ctrl\n"
1975 "noprint-parent nosym-offset nosym-addr noverbose\n"
1976 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1977 "# echo 1 > /debug/tracing/tracing_enabled\n"
1978 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1979 "echo 0 > /debug/tracing/tracing_enabled\n"
1982 static ssize_t
1983 tracing_readme_read(struct file *filp, char __user *ubuf,
1984 size_t cnt, loff_t *ppos)
1986 return simple_read_from_buffer(ubuf, cnt, ppos,
1987 readme_msg, strlen(readme_msg));
1990 static struct file_operations tracing_readme_fops = {
1991 .open = tracing_open_generic,
1992 .read = tracing_readme_read,
1995 static ssize_t
1996 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1997 size_t cnt, loff_t *ppos)
1999 struct trace_array *tr = filp->private_data;
2000 char buf[64];
2001 int r;
2003 r = sprintf(buf, "%ld\n", tr->ctrl);
2004 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2007 static ssize_t
2008 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2009 size_t cnt, loff_t *ppos)
2011 struct trace_array *tr = filp->private_data;
2012 long val;
2013 char buf[64];
2015 if (cnt > 63)
2016 cnt = 63;
2018 if (copy_from_user(&buf, ubuf, cnt))
2019 return -EFAULT;
2021 buf[cnt] = 0;
2023 val = simple_strtoul(buf, NULL, 10);
2025 val = !!val;
2027 mutex_lock(&trace_types_lock);
2028 if (tr->ctrl ^ val) {
2029 if (val)
2030 tracer_enabled = 1;
2031 else
2032 tracer_enabled = 0;
2034 tr->ctrl = val;
2036 if (current_trace && current_trace->ctrl_update)
2037 current_trace->ctrl_update(tr);
2039 mutex_unlock(&trace_types_lock);
2041 filp->f_pos += cnt;
2043 return cnt;
2046 static ssize_t
2047 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2048 size_t cnt, loff_t *ppos)
2050 char buf[max_tracer_type_len+2];
2051 int r;
2053 mutex_lock(&trace_types_lock);
2054 if (current_trace)
2055 r = sprintf(buf, "%s\n", current_trace->name);
2056 else
2057 r = sprintf(buf, "\n");
2058 mutex_unlock(&trace_types_lock);
2060 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2063 static ssize_t
2064 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2065 size_t cnt, loff_t *ppos)
2067 struct trace_array *tr = &global_trace;
2068 struct tracer *t;
2069 char buf[max_tracer_type_len+1];
2070 int i;
2072 if (cnt > max_tracer_type_len)
2073 cnt = max_tracer_type_len;
2075 if (copy_from_user(&buf, ubuf, cnt))
2076 return -EFAULT;
2078 buf[cnt] = 0;
2080 /* strip ending whitespace. */
2081 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2082 buf[i] = 0;
2084 mutex_lock(&trace_types_lock);
2085 for (t = trace_types; t; t = t->next) {
2086 if (strcmp(t->name, buf) == 0)
2087 break;
2089 if (!t || t == current_trace)
2090 goto out;
2092 if (current_trace && current_trace->reset)
2093 current_trace->reset(tr);
2095 current_trace = t;
2096 if (t->init)
2097 t->init(tr);
2099 out:
2100 mutex_unlock(&trace_types_lock);
2102 filp->f_pos += cnt;
2104 return cnt;
2107 static ssize_t
2108 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2109 size_t cnt, loff_t *ppos)
2111 unsigned long *ptr = filp->private_data;
2112 char buf[64];
2113 int r;
2115 r = snprintf(buf, 64, "%ld\n",
2116 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2117 if (r > 64)
2118 r = 64;
2119 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2122 static ssize_t
2123 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2124 size_t cnt, loff_t *ppos)
2126 long *ptr = filp->private_data;
2127 long val;
2128 char buf[64];
2130 if (cnt > 63)
2131 cnt = 63;
2133 if (copy_from_user(&buf, ubuf, cnt))
2134 return -EFAULT;
2136 buf[cnt] = 0;
2138 val = simple_strtoul(buf, NULL, 10);
2140 *ptr = val * 1000;
2142 return cnt;
2145 static atomic_t tracing_reader;
2147 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2149 struct trace_iterator *iter;
2151 if (tracing_disabled)
2152 return -ENODEV;
2154 /* We only allow for reader of the pipe */
2155 if (atomic_inc_return(&tracing_reader) != 1) {
2156 atomic_dec(&tracing_reader);
2157 return -EBUSY;
2160 /* create a buffer to store the information to pass to userspace */
2161 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2162 if (!iter)
2163 return -ENOMEM;
2165 iter->tr = &global_trace;
2166 iter->trace = current_trace;
2168 filp->private_data = iter;
2170 return 0;
2173 static int tracing_release_pipe(struct inode *inode, struct file *file)
2175 struct trace_iterator *iter = file->private_data;
2177 kfree(iter);
2178 atomic_dec(&tracing_reader);
2180 return 0;
2183 static unsigned int
2184 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2186 struct trace_iterator *iter = filp->private_data;
2188 if (trace_flags & TRACE_ITER_BLOCK) {
2190 * Always select as readable when in blocking mode
2192 return POLLIN | POLLRDNORM;
2194 else {
2195 if (!trace_empty(iter))
2196 return POLLIN | POLLRDNORM;
2197 poll_wait(filp, &trace_wait, poll_table);
2198 if (!trace_empty(iter))
2199 return POLLIN | POLLRDNORM;
2201 return 0;
2206 * Consumer reader.
2208 static ssize_t
2209 tracing_read_pipe(struct file *filp, char __user *ubuf,
2210 size_t cnt, loff_t *ppos)
2212 struct trace_iterator *iter = filp->private_data;
2213 struct trace_array_cpu *data;
2214 struct trace_array *tr = iter->tr;
2215 struct tracer *tracer = iter->trace;
2216 static cpumask_t mask;
2217 static int start;
2218 unsigned long flags;
2219 #ifdef CONFIG_FTRACE
2220 int ftrace_save;
2221 #endif
2222 int read = 0;
2223 int cpu;
2224 int len;
2225 int ret;
2227 /* return any leftover data */
2228 if (iter->seq.len > start) {
2229 len = iter->seq.len - start;
2230 if (cnt > len)
2231 cnt = len;
2232 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2233 if (ret)
2234 cnt = -EFAULT;
2236 start += len;
2238 return cnt;
2241 trace_seq_reset(&iter->seq);
2242 start = 0;
2244 while (trace_empty(iter)) {
2246 if ((filp->f_flags & O_NONBLOCK))
2247 return -EAGAIN;
2250 * This is a make-shift waitqueue. The reason we don't use
2251 * an actual wait queue is because:
2252 * 1) we only ever have one waiter
2253 * 2) the tracing, traces all functions, we don't want
2254 * the overhead of calling wake_up and friends
2255 * (and tracing them too)
2256 * Anyway, this is really very primitive wakeup.
2258 set_current_state(TASK_INTERRUPTIBLE);
2259 iter->tr->waiter = current;
2261 /* sleep for one second, and try again. */
2262 schedule_timeout(HZ);
2264 iter->tr->waiter = NULL;
2266 if (signal_pending(current))
2267 return -EINTR;
2269 if (iter->trace != current_trace)
2270 return 0;
2273 * We block until we read something and tracing is disabled.
2274 * We still block if tracing is disabled, but we have never
2275 * read anything. This allows a user to cat this file, and
2276 * then enable tracing. But after we have read something,
2277 * we give an EOF when tracing is again disabled.
2279 * iter->pos will be 0 if we haven't read anything.
2281 if (!tracer_enabled && iter->pos)
2282 break;
2284 continue;
2287 /* stop when tracing is finished */
2288 if (trace_empty(iter))
2289 return 0;
2291 if (cnt >= PAGE_SIZE)
2292 cnt = PAGE_SIZE - 1;
2294 memset(iter, 0, sizeof(*iter));
2295 iter->tr = tr;
2296 iter->trace = tracer;
2297 iter->pos = -1;
2300 * We need to stop all tracing on all CPUS to read the
2301 * the next buffer. This is a bit expensive, but is
2302 * not done often. We fill all what we can read,
2303 * and then release the locks again.
2306 cpus_clear(mask);
2307 local_irq_save(flags);
2308 #ifdef CONFIG_FTRACE
2309 ftrace_save = ftrace_enabled;
2310 ftrace_enabled = 0;
2311 #endif
2312 smp_wmb();
2313 for_each_possible_cpu(cpu) {
2314 data = iter->tr->data[cpu];
2316 if (!head_page(data) || !data->trace_idx)
2317 continue;
2319 atomic_inc(&data->disabled);
2320 cpu_set(cpu, mask);
2323 for_each_cpu_mask(cpu, mask) {
2324 data = iter->tr->data[cpu];
2325 __raw_spin_lock(&data->lock);
2328 while (find_next_entry_inc(iter) != NULL) {
2329 int len = iter->seq.len;
2331 ret = print_trace_line(iter);
2332 if (!ret) {
2333 /* don't print partial lines */
2334 iter->seq.len = len;
2335 break;
2338 trace_consume(iter);
2340 if (iter->seq.len >= cnt)
2341 break;
2344 for_each_cpu_mask(cpu, mask) {
2345 data = iter->tr->data[cpu];
2346 __raw_spin_unlock(&data->lock);
2349 for_each_cpu_mask(cpu, mask) {
2350 data = iter->tr->data[cpu];
2351 atomic_dec(&data->disabled);
2353 #ifdef CONFIG_FTRACE
2354 ftrace_enabled = ftrace_save;
2355 #endif
2356 local_irq_restore(flags);
2358 /* Now copy what we have to the user */
2359 read = iter->seq.len;
2360 if (read > cnt)
2361 read = cnt;
2363 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2365 if (read < iter->seq.len)
2366 start = read;
2367 else
2368 trace_seq_reset(&iter->seq);
2370 if (ret)
2371 read = -EFAULT;
2373 return read;
2376 static ssize_t
2377 tracing_entries_read(struct file *filp, char __user *ubuf,
2378 size_t cnt, loff_t *ppos)
2380 struct trace_array *tr = filp->private_data;
2381 char buf[64];
2382 int r;
2384 r = sprintf(buf, "%lu\n", tr->entries);
2385 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2388 static ssize_t
2389 tracing_entries_write(struct file *filp, const char __user *ubuf,
2390 size_t cnt, loff_t *ppos)
2392 unsigned long val;
2393 char buf[64];
2395 if (cnt > 63)
2396 cnt = 63;
2398 if (copy_from_user(&buf, ubuf, cnt))
2399 return -EFAULT;
2401 buf[cnt] = 0;
2403 val = simple_strtoul(buf, NULL, 10);
2405 /* must have at least 1 entry */
2406 if (!val)
2407 return -EINVAL;
2409 mutex_lock(&trace_types_lock);
2411 if (current_trace != &no_tracer) {
2412 cnt = -EBUSY;
2413 pr_info("ftrace: set current_tracer to none"
2414 " before modifying buffer size\n");
2415 goto out;
2418 if (val > global_trace.entries) {
2419 while (global_trace.entries < val) {
2420 if (trace_alloc_page()) {
2421 cnt = -ENOMEM;
2422 goto out;
2425 } else {
2426 /* include the number of entries in val (inc of page entries) */
2427 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2428 trace_free_page();
2431 filp->f_pos += cnt;
2433 out:
2434 max_tr.entries = global_trace.entries;
2435 mutex_unlock(&trace_types_lock);
2437 return cnt;
2440 static struct file_operations tracing_max_lat_fops = {
2441 .open = tracing_open_generic,
2442 .read = tracing_max_lat_read,
2443 .write = tracing_max_lat_write,
2446 static struct file_operations tracing_ctrl_fops = {
2447 .open = tracing_open_generic,
2448 .read = tracing_ctrl_read,
2449 .write = tracing_ctrl_write,
2452 static struct file_operations set_tracer_fops = {
2453 .open = tracing_open_generic,
2454 .read = tracing_set_trace_read,
2455 .write = tracing_set_trace_write,
2458 static struct file_operations tracing_pipe_fops = {
2459 .open = tracing_open_pipe,
2460 .poll = tracing_poll_pipe,
2461 .read = tracing_read_pipe,
2462 .release = tracing_release_pipe,
2465 static struct file_operations tracing_entries_fops = {
2466 .open = tracing_open_generic,
2467 .read = tracing_entries_read,
2468 .write = tracing_entries_write,
2471 #ifdef CONFIG_DYNAMIC_FTRACE
2473 static ssize_t
2474 tracing_read_long(struct file *filp, char __user *ubuf,
2475 size_t cnt, loff_t *ppos)
2477 unsigned long *p = filp->private_data;
2478 char buf[64];
2479 int r;
2481 r = sprintf(buf, "%ld\n", *p);
2483 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2486 static struct file_operations tracing_read_long_fops = {
2487 .open = tracing_open_generic,
2488 .read = tracing_read_long,
2490 #endif
2492 static struct dentry *d_tracer;
2494 struct dentry *tracing_init_dentry(void)
2496 static int once;
2498 if (d_tracer)
2499 return d_tracer;
2501 d_tracer = debugfs_create_dir("tracing", NULL);
2503 if (!d_tracer && !once) {
2504 once = 1;
2505 pr_warning("Could not create debugfs directory 'tracing'\n");
2506 return NULL;
2509 return d_tracer;
2512 #ifdef CONFIG_FTRACE_SELFTEST
2513 /* Let selftest have access to static functions in this file */
2514 #include "trace_selftest.c"
2515 #endif
2517 static __init void tracer_init_debugfs(void)
2519 struct dentry *d_tracer;
2520 struct dentry *entry;
2522 d_tracer = tracing_init_dentry();
2524 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2525 &global_trace, &tracing_ctrl_fops);
2526 if (!entry)
2527 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2529 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2530 NULL, &tracing_iter_fops);
2531 if (!entry)
2532 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2534 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2535 NULL, &tracing_cpumask_fops);
2536 if (!entry)
2537 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2539 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2540 &global_trace, &tracing_lt_fops);
2541 if (!entry)
2542 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2544 entry = debugfs_create_file("trace", 0444, d_tracer,
2545 &global_trace, &tracing_fops);
2546 if (!entry)
2547 pr_warning("Could not create debugfs 'trace' entry\n");
2549 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2550 &global_trace, &show_traces_fops);
2551 if (!entry)
2552 pr_warning("Could not create debugfs 'trace' entry\n");
2554 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2555 &global_trace, &set_tracer_fops);
2556 if (!entry)
2557 pr_warning("Could not create debugfs 'trace' entry\n");
2559 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2560 &tracing_max_latency,
2561 &tracing_max_lat_fops);
2562 if (!entry)
2563 pr_warning("Could not create debugfs "
2564 "'tracing_max_latency' entry\n");
2566 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2567 &tracing_thresh, &tracing_max_lat_fops);
2568 if (!entry)
2569 pr_warning("Could not create debugfs "
2570 "'tracing_threash' entry\n");
2571 entry = debugfs_create_file("README", 0644, d_tracer,
2572 NULL, &tracing_readme_fops);
2573 if (!entry)
2574 pr_warning("Could not create debugfs 'README' entry\n");
2576 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2577 NULL, &tracing_pipe_fops);
2578 if (!entry)
2579 pr_warning("Could not create debugfs "
2580 "'tracing_threash' entry\n");
2582 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2583 &global_trace, &tracing_entries_fops);
2584 if (!entry)
2585 pr_warning("Could not create debugfs "
2586 "'tracing_threash' entry\n");
2588 #ifdef CONFIG_DYNAMIC_FTRACE
2589 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2590 &ftrace_update_tot_cnt,
2591 &tracing_read_long_fops);
2592 if (!entry)
2593 pr_warning("Could not create debugfs "
2594 "'dyn_ftrace_total_info' entry\n");
2595 #endif
2598 static int trace_alloc_page(void)
2600 struct trace_array_cpu *data;
2601 struct page *page, *tmp;
2602 LIST_HEAD(pages);
2603 void *array;
2604 int i;
2606 /* first allocate a page for each CPU */
2607 for_each_possible_cpu(i) {
2608 array = (void *)__get_free_page(GFP_KERNEL);
2609 if (array == NULL) {
2610 printk(KERN_ERR "tracer: failed to allocate page"
2611 "for trace buffer!\n");
2612 goto free_pages;
2615 page = virt_to_page(array);
2616 list_add(&page->lru, &pages);
2618 /* Only allocate if we are actually using the max trace */
2619 #ifdef CONFIG_TRACER_MAX_TRACE
2620 array = (void *)__get_free_page(GFP_KERNEL);
2621 if (array == NULL) {
2622 printk(KERN_ERR "tracer: failed to allocate page"
2623 "for trace buffer!\n");
2624 goto free_pages;
2626 page = virt_to_page(array);
2627 list_add(&page->lru, &pages);
2628 #endif
2631 /* Now that we successfully allocate a page per CPU, add them */
2632 for_each_possible_cpu(i) {
2633 data = global_trace.data[i];
2634 page = list_entry(pages.next, struct page, lru);
2635 list_del_init(&page->lru);
2636 list_add_tail(&page->lru, &data->trace_pages);
2637 ClearPageLRU(page);
2639 #ifdef CONFIG_TRACER_MAX_TRACE
2640 data = max_tr.data[i];
2641 page = list_entry(pages.next, struct page, lru);
2642 list_del_init(&page->lru);
2643 list_add_tail(&page->lru, &data->trace_pages);
2644 SetPageLRU(page);
2645 #endif
2647 global_trace.entries += ENTRIES_PER_PAGE;
2649 return 0;
2651 free_pages:
2652 list_for_each_entry_safe(page, tmp, &pages, lru) {
2653 list_del_init(&page->lru);
2654 __free_page(page);
2656 return -ENOMEM;
2659 static int trace_free_page(void)
2661 struct trace_array_cpu *data;
2662 struct page *page;
2663 struct list_head *p;
2664 int i;
2665 int ret = 0;
2667 /* free one page from each buffer */
2668 for_each_possible_cpu(i) {
2669 data = global_trace.data[i];
2670 p = data->trace_pages.next;
2671 if (p == &data->trace_pages) {
2672 /* should never happen */
2673 WARN_ON(1);
2674 tracing_disabled = 1;
2675 ret = -1;
2676 break;
2678 page = list_entry(p, struct page, lru);
2679 ClearPageLRU(page);
2680 list_del(&page->lru);
2681 __free_page(page);
2683 tracing_reset(data);
2685 #ifdef CONFIG_TRACER_MAX_TRACE
2686 data = max_tr.data[i];
2687 p = data->trace_pages.next;
2688 if (p == &data->trace_pages) {
2689 /* should never happen */
2690 WARN_ON(1);
2691 tracing_disabled = 1;
2692 ret = -1;
2693 break;
2695 page = list_entry(p, struct page, lru);
2696 ClearPageLRU(page);
2697 list_del(&page->lru);
2698 __free_page(page);
2700 tracing_reset(data);
2701 #endif
2703 global_trace.entries -= ENTRIES_PER_PAGE;
2705 return ret;
2708 __init static int tracer_alloc_buffers(void)
2710 struct trace_array_cpu *data;
2711 void *array;
2712 struct page *page;
2713 int pages = 0;
2714 int ret = -ENOMEM;
2715 int i;
2717 global_trace.ctrl = tracer_enabled;
2719 /* Allocate the first page for all buffers */
2720 for_each_possible_cpu(i) {
2721 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2722 max_tr.data[i] = &per_cpu(max_data, i);
2724 array = (void *)__get_free_page(GFP_KERNEL);
2725 if (array == NULL) {
2726 printk(KERN_ERR "tracer: failed to allocate page"
2727 "for trace buffer!\n");
2728 goto free_buffers;
2731 /* set the array to the list */
2732 INIT_LIST_HEAD(&data->trace_pages);
2733 page = virt_to_page(array);
2734 list_add(&page->lru, &data->trace_pages);
2735 /* use the LRU flag to differentiate the two buffers */
2736 ClearPageLRU(page);
2738 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2739 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2741 /* Only allocate if we are actually using the max trace */
2742 #ifdef CONFIG_TRACER_MAX_TRACE
2743 array = (void *)__get_free_page(GFP_KERNEL);
2744 if (array == NULL) {
2745 printk(KERN_ERR "tracer: failed to allocate page"
2746 "for trace buffer!\n");
2747 goto free_buffers;
2750 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2751 page = virt_to_page(array);
2752 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2753 SetPageLRU(page);
2754 #endif
2758 * Since we allocate by orders of pages, we may be able to
2759 * round up a bit.
2761 global_trace.entries = ENTRIES_PER_PAGE;
2762 pages++;
2764 while (global_trace.entries < trace_nr_entries) {
2765 if (trace_alloc_page())
2766 break;
2767 pages++;
2769 max_tr.entries = global_trace.entries;
2771 pr_info("tracer: %d pages allocated for %ld",
2772 pages, trace_nr_entries);
2773 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2774 pr_info(" actual entries %ld\n", global_trace.entries);
2776 tracer_init_debugfs();
2778 trace_init_cmdlines();
2780 register_tracer(&no_tracer);
2781 current_trace = &no_tracer;
2783 /* All seems OK, enable tracing */
2784 tracing_disabled = 0;
2786 return 0;
2788 free_buffers:
2789 for (i-- ; i >= 0; i--) {
2790 struct page *page, *tmp;
2791 struct trace_array_cpu *data = global_trace.data[i];
2793 if (data) {
2794 list_for_each_entry_safe(page, tmp,
2795 &data->trace_pages, lru) {
2796 list_del_init(&page->lru);
2797 __free_page(page);
2801 #ifdef CONFIG_TRACER_MAX_TRACE
2802 data = max_tr.data[i];
2803 if (data) {
2804 list_for_each_entry_safe(page, tmp,
2805 &data->trace_pages, lru) {
2806 list_del_init(&page->lru);
2807 __free_page(page);
2810 #endif
2812 return ret;
2814 fs_initcall(tracer_alloc_buffers);