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>
31 #include <linux/stacktrace.h>
35 unsigned long __read_mostly tracing_max_latency
= (cycle_t
)ULONG_MAX
;
36 unsigned long __read_mostly tracing_thresh
;
38 static unsigned long __read_mostly tracing_nr_buffers
;
39 static cpumask_t __read_mostly tracing_buffer_mask
;
41 #define for_each_tracing_cpu(cpu) \
42 for_each_cpu_mask(cpu, tracing_buffer_mask)
44 /* dummy trace to disable tracing */
45 static struct tracer no_tracer __read_mostly
= {
49 static int trace_alloc_page(void);
50 static int trace_free_page(void);
52 static int tracing_disabled
= 1;
55 ns2usecs(cycle_t nsec
)
62 cycle_t
ftrace_now(int cpu
)
64 return cpu_clock(cpu
);
68 * The global_trace is the descriptor that holds the tracing
69 * buffers for the live tracing. For each CPU, it contains
70 * a link list of pages that will store trace entries. The
71 * page descriptor of the pages in the memory is used to hold
72 * the link list by linking the lru item in the page descriptor
73 * to each of the pages in the buffer per CPU.
75 * For each active CPU there is a data field that holds the
76 * pages for the buffer for that CPU. Each CPU has the same number
77 * of pages allocated for its buffer.
79 static struct trace_array global_trace
;
81 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
84 * The max_tr is used to snapshot the global_trace when a maximum
85 * latency is reached. Some tracers will use this to store a maximum
86 * trace while it continues examining live traces.
88 * The buffers for the max_tr are set up the same as the global_trace.
89 * When a snapshot is taken, the link list of the max_tr is swapped
90 * with the link list of the global_trace and the buffers are reset for
91 * the global_trace so the tracing can continue.
93 static struct trace_array max_tr
;
95 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
97 /* tracer_enabled is used to toggle activation of a tracer */
98 static int tracer_enabled
= 1;
101 * trace_nr_entries is the number of entries that is allocated
102 * for a buffer. Note, the number of entries is always rounded
103 * to ENTRIES_PER_PAGE.
105 static unsigned long trace_nr_entries
= 65536UL;
107 /* trace_types holds a link list of available tracers. */
108 static struct tracer
*trace_types __read_mostly
;
110 /* current_trace points to the tracer that is currently active */
111 static struct tracer
*current_trace __read_mostly
;
114 * max_tracer_type_len is used to simplify the allocating of
115 * buffers to read userspace tracer names. We keep track of
116 * the longest tracer name registered.
118 static int max_tracer_type_len
;
121 * trace_types_lock is used to protect the trace_types list.
122 * This lock is also used to keep user access serialized.
123 * Accesses from userspace will grab this lock while userspace
124 * activities happen inside the kernel.
126 static DEFINE_MUTEX(trace_types_lock
);
128 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
129 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
131 /* trace_flags holds iter_ctrl options */
132 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
;
135 * trace_wake_up - wake up tasks waiting for trace input
137 * Simply wakes up any task that is blocked on the trace_wait
138 * queue. These is used with trace_poll for tasks polling the trace.
140 void trace_wake_up(void)
143 * The runqueue_is_locked() can fail, but this is the best we
146 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
147 wake_up(&trace_wait
);
150 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
152 static int __init
set_nr_entries(char *str
)
154 unsigned long nr_entries
;
159 ret
= strict_strtoul(str
, 0, &nr_entries
);
160 /* nr_entries can not be zero */
161 if (ret
< 0 || nr_entries
== 0)
163 trace_nr_entries
= nr_entries
;
166 __setup("trace_entries=", set_nr_entries
);
168 unsigned long nsecs_to_usecs(unsigned long nsecs
)
174 * trace_flag_type is an enumeration that holds different
175 * states when a trace occurs. These are:
176 * IRQS_OFF - interrupts were disabled
177 * NEED_RESCED - reschedule is requested
178 * HARDIRQ - inside an interrupt handler
179 * SOFTIRQ - inside a softirq handler
181 enum trace_flag_type
{
182 TRACE_FLAG_IRQS_OFF
= 0x01,
183 TRACE_FLAG_NEED_RESCHED
= 0x02,
184 TRACE_FLAG_HARDIRQ
= 0x04,
185 TRACE_FLAG_SOFTIRQ
= 0x08,
189 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
190 * control the output of kernel symbols.
192 #define TRACE_ITER_SYM_MASK \
193 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
195 /* These must match the bit postions in trace_iterator_flags */
196 static const char *trace_options
[] = {
211 * ftrace_max_lock is used to protect the swapping of buffers
212 * when taking a max snapshot. The buffers themselves are
213 * protected by per_cpu spinlocks. But the action of the swap
214 * needs its own lock.
216 * This is defined as a raw_spinlock_t in order to help
217 * with performance when lockdep debugging is enabled.
219 static raw_spinlock_t ftrace_max_lock
=
220 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
223 * Copy the new maximum trace into the separate maximum-trace
224 * structure. (this way the maximum trace is permanently saved,
225 * for later retrieval via /debugfs/tracing/latency_trace)
228 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
230 struct trace_array_cpu
*data
= tr
->data
[cpu
];
233 max_tr
.time_start
= data
->preempt_timestamp
;
235 data
= max_tr
.data
[cpu
];
236 data
->saved_latency
= tracing_max_latency
;
238 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
239 data
->pid
= tsk
->pid
;
240 data
->uid
= tsk
->uid
;
241 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
242 data
->policy
= tsk
->policy
;
243 data
->rt_priority
= tsk
->rt_priority
;
245 /* record this tasks comm */
246 tracing_record_cmdline(current
);
250 * check_pages - integrity check of trace buffers
252 * As a safty measure we check to make sure the data pages have not
253 * been corrupted. TODO: configure to disable this because it adds
256 void check_pages(struct trace_array_cpu
*data
)
258 struct page
*page
, *tmp
;
260 BUG_ON(data
->trace_pages
.next
->prev
!= &data
->trace_pages
);
261 BUG_ON(data
->trace_pages
.prev
->next
!= &data
->trace_pages
);
263 list_for_each_entry_safe(page
, tmp
, &data
->trace_pages
, lru
) {
264 BUG_ON(page
->lru
.next
->prev
!= &page
->lru
);
265 BUG_ON(page
->lru
.prev
->next
!= &page
->lru
);
270 * head_page - page address of the first page in per_cpu buffer.
272 * head_page returns the page address of the first page in
273 * a per_cpu buffer. This also preforms various consistency
274 * checks to make sure the buffer has not been corrupted.
276 void *head_page(struct trace_array_cpu
*data
)
281 if (list_empty(&data
->trace_pages
))
284 page
= list_entry(data
->trace_pages
.next
, struct page
, lru
);
285 BUG_ON(&page
->lru
== &data
->trace_pages
);
287 return page_address(page
);
291 * trace_seq_printf - sequence printing of trace information
292 * @s: trace sequence descriptor
293 * @fmt: printf format string
295 * The tracer may use either sequence operations or its own
296 * copy to user routines. To simplify formating of a trace
297 * trace_seq_printf is used to store strings into a special
298 * buffer (@s). Then the output may be either used by
299 * the sequencer or pulled into another buffer.
302 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
304 int len
= (PAGE_SIZE
- 1) - s
->len
;
312 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
315 /* If we can't write it all, don't bother writing anything */
325 * trace_seq_puts - trace sequence printing of simple string
326 * @s: trace sequence descriptor
327 * @str: simple string to record
329 * The tracer may use either the sequence operations or its own
330 * copy to user routines. This function records a simple string
331 * into a special buffer (@s) for later retrieval by a sequencer
332 * or other mechanism.
335 trace_seq_puts(struct trace_seq
*s
, const char *str
)
337 int len
= strlen(str
);
339 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
342 memcpy(s
->buffer
+ s
->len
, str
, len
);
349 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
351 if (s
->len
>= (PAGE_SIZE
- 1))
354 s
->buffer
[s
->len
++] = c
;
360 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
362 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
365 memcpy(s
->buffer
+ s
->len
, mem
, len
);
372 static const char hex2asc
[] = "0123456789abcdef";
375 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
377 unsigned char hex
[HEX_CHARS
];
378 unsigned char *data
= mem
;
382 BUG_ON(len
>= HEX_CHARS
);
385 for (i
= 0, j
= 0; i
< len
; i
++) {
387 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
391 hex
[j
++] = hex2asc
[byte
& 0x0f];
392 hex
[j
++] = hex2asc
[byte
>> 4];
396 return trace_seq_putmem(s
, hex
, j
);
400 trace_seq_reset(struct trace_seq
*s
)
406 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
408 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
411 seq_puts(m
, s
->buffer
);
417 * flip the trace buffers between two trace descriptors.
418 * This usually is the buffers between the global_trace and
419 * the max_tr to record a snapshot of a current trace.
421 * The ftrace_max_lock must be held.
424 flip_trace(struct trace_array_cpu
*tr1
, struct trace_array_cpu
*tr2
)
426 struct list_head flip_pages
;
428 INIT_LIST_HEAD(&flip_pages
);
430 memcpy(&tr1
->trace_head_idx
, &tr2
->trace_head_idx
,
431 sizeof(struct trace_array_cpu
) -
432 offsetof(struct trace_array_cpu
, trace_head_idx
));
436 list_splice_init(&tr1
->trace_pages
, &flip_pages
);
437 list_splice_init(&tr2
->trace_pages
, &tr1
->trace_pages
);
438 list_splice_init(&flip_pages
, &tr2
->trace_pages
);
439 BUG_ON(!list_empty(&flip_pages
));
445 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
447 * @tsk: the task with the latency
448 * @cpu: The cpu that initiated the trace.
450 * Flip the buffers between the @tr and the max_tr and record information
451 * about which task was the cause of this latency.
454 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
456 struct trace_array_cpu
*data
;
459 WARN_ON_ONCE(!irqs_disabled());
460 __raw_spin_lock(&ftrace_max_lock
);
461 /* clear out all the previous traces */
462 for_each_tracing_cpu(i
) {
464 flip_trace(max_tr
.data
[i
], data
);
468 __update_max_tr(tr
, tsk
, cpu
);
469 __raw_spin_unlock(&ftrace_max_lock
);
473 * update_max_tr_single - only copy one trace over, and reset the rest
475 * @tsk - task with the latency
476 * @cpu - the cpu of the buffer to copy.
478 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
481 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
483 struct trace_array_cpu
*data
= tr
->data
[cpu
];
486 WARN_ON_ONCE(!irqs_disabled());
487 __raw_spin_lock(&ftrace_max_lock
);
488 for_each_tracing_cpu(i
)
489 tracing_reset(max_tr
.data
[i
]);
491 flip_trace(max_tr
.data
[cpu
], data
);
494 __update_max_tr(tr
, tsk
, cpu
);
495 __raw_spin_unlock(&ftrace_max_lock
);
499 * register_tracer - register a tracer with the ftrace system.
500 * @type - the plugin for the tracer
502 * Register a new plugin tracer.
504 int register_tracer(struct tracer
*type
)
511 pr_info("Tracer must have a name\n");
515 mutex_lock(&trace_types_lock
);
516 for (t
= trace_types
; t
; t
= t
->next
) {
517 if (strcmp(type
->name
, t
->name
) == 0) {
519 pr_info("Trace %s already registered\n",
526 #ifdef CONFIG_FTRACE_STARTUP_TEST
527 if (type
->selftest
) {
528 struct tracer
*saved_tracer
= current_trace
;
529 struct trace_array_cpu
*data
;
530 struct trace_array
*tr
= &global_trace
;
531 int saved_ctrl
= tr
->ctrl
;
534 * Run a selftest on this tracer.
535 * Here we reset the trace buffer, and set the current
536 * tracer to be this tracer. The tracer can then run some
537 * internal tracing to verify that everything is in order.
538 * If we fail, we do not register this tracer.
540 for_each_tracing_cpu(i
) {
542 if (!head_page(data
))
546 current_trace
= type
;
548 /* the test is responsible for initializing and enabling */
549 pr_info("Testing tracer %s: ", type
->name
);
550 ret
= type
->selftest(type
, tr
);
551 /* the test is responsible for resetting too */
552 current_trace
= saved_tracer
;
553 tr
->ctrl
= saved_ctrl
;
555 printk(KERN_CONT
"FAILED!\n");
558 /* Only reset on passing, to avoid touching corrupted buffers */
559 for_each_tracing_cpu(i
) {
561 if (!head_page(data
))
565 printk(KERN_CONT
"PASSED\n");
569 type
->next
= trace_types
;
571 len
= strlen(type
->name
);
572 if (len
> max_tracer_type_len
)
573 max_tracer_type_len
= len
;
576 mutex_unlock(&trace_types_lock
);
581 void unregister_tracer(struct tracer
*type
)
586 mutex_lock(&trace_types_lock
);
587 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
591 pr_info("Trace %s not registered\n", type
->name
);
596 if (strlen(type
->name
) != max_tracer_type_len
)
599 max_tracer_type_len
= 0;
600 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
601 len
= strlen((*t
)->name
);
602 if (len
> max_tracer_type_len
)
603 max_tracer_type_len
= len
;
606 mutex_unlock(&trace_types_lock
);
609 void tracing_reset(struct trace_array_cpu
*data
)
613 data
->trace_head
= data
->trace_tail
= head_page(data
);
614 data
->trace_head_idx
= 0;
615 data
->trace_tail_idx
= 0;
618 #define SAVED_CMDLINES 128
619 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
620 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
621 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
622 static int cmdline_idx
;
623 static DEFINE_SPINLOCK(trace_cmdline_lock
);
625 /* trace in all context switches */
626 atomic_t trace_record_cmdline_enabled __read_mostly
;
628 /* temporary disable recording */
629 atomic_t trace_record_cmdline_disabled __read_mostly
;
631 static void trace_init_cmdlines(void)
633 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
634 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
638 void trace_stop_cmdline_recording(void);
640 static void trace_save_cmdline(struct task_struct
*tsk
)
645 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
649 * It's not the end of the world if we don't get
650 * the lock, but we also don't want to spin
651 * nor do we want to disable interrupts,
652 * so if we miss here, then better luck next time.
654 if (!spin_trylock(&trace_cmdline_lock
))
657 idx
= map_pid_to_cmdline
[tsk
->pid
];
658 if (idx
>= SAVED_CMDLINES
) {
659 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
661 map
= map_cmdline_to_pid
[idx
];
662 if (map
<= PID_MAX_DEFAULT
)
663 map_pid_to_cmdline
[map
] = (unsigned)-1;
665 map_pid_to_cmdline
[tsk
->pid
] = idx
;
670 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
672 spin_unlock(&trace_cmdline_lock
);
675 static char *trace_find_cmdline(int pid
)
677 char *cmdline
= "<...>";
683 if (pid
> PID_MAX_DEFAULT
)
686 map
= map_pid_to_cmdline
[pid
];
687 if (map
>= SAVED_CMDLINES
)
690 cmdline
= saved_cmdlines
[map
];
696 void tracing_record_cmdline(struct task_struct
*tsk
)
698 if (atomic_read(&trace_record_cmdline_disabled
))
701 trace_save_cmdline(tsk
);
704 static inline struct list_head
*
705 trace_next_list(struct trace_array_cpu
*data
, struct list_head
*next
)
708 * Roundrobin - but skip the head (which is not a real page):
711 if (unlikely(next
== &data
->trace_pages
))
713 BUG_ON(next
== &data
->trace_pages
);
719 trace_next_page(struct trace_array_cpu
*data
, void *addr
)
721 struct list_head
*next
;
724 page
= virt_to_page(addr
);
726 next
= trace_next_list(data
, &page
->lru
);
727 page
= list_entry(next
, struct page
, lru
);
729 return page_address(page
);
732 static inline struct trace_entry
*
733 tracing_get_trace_entry(struct trace_array
*tr
, struct trace_array_cpu
*data
)
735 unsigned long idx
, idx_next
;
736 struct trace_entry
*entry
;
739 idx
= data
->trace_head_idx
;
742 BUG_ON(idx
* TRACE_ENTRY_SIZE
>= PAGE_SIZE
);
744 entry
= data
->trace_head
+ idx
* TRACE_ENTRY_SIZE
;
746 if (unlikely(idx_next
>= ENTRIES_PER_PAGE
)) {
747 data
->trace_head
= trace_next_page(data
, data
->trace_head
);
751 if (data
->trace_head
== data
->trace_tail
&&
752 idx_next
== data
->trace_tail_idx
) {
755 data
->trace_tail_idx
++;
756 if (data
->trace_tail_idx
>= ENTRIES_PER_PAGE
) {
758 trace_next_page(data
, data
->trace_tail
);
759 data
->trace_tail_idx
= 0;
763 data
->trace_head_idx
= idx_next
;
769 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
)
771 struct task_struct
*tsk
= current
;
774 pc
= preempt_count();
776 entry
->preempt_count
= pc
& 0xff;
777 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
778 entry
->t
= ftrace_now(raw_smp_processor_id());
779 entry
->flags
= (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
780 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
781 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
782 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
786 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
787 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
789 struct trace_entry
*entry
;
790 unsigned long irq_flags
;
792 raw_local_irq_save(irq_flags
);
793 __raw_spin_lock(&data
->lock
);
794 entry
= tracing_get_trace_entry(tr
, data
);
795 tracing_generic_entry_update(entry
, flags
);
796 entry
->type
= TRACE_FN
;
798 entry
->fn
.parent_ip
= parent_ip
;
799 __raw_spin_unlock(&data
->lock
);
800 raw_local_irq_restore(irq_flags
);
804 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
805 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
807 if (likely(!atomic_read(&data
->disabled
)))
808 trace_function(tr
, data
, ip
, parent_ip
, flags
);
812 __trace_special(void *__tr
, void *__data
,
813 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
815 struct trace_array_cpu
*data
= __data
;
816 struct trace_array
*tr
= __tr
;
817 struct trace_entry
*entry
;
818 unsigned long irq_flags
;
820 raw_local_irq_save(irq_flags
);
821 __raw_spin_lock(&data
->lock
);
822 entry
= tracing_get_trace_entry(tr
, data
);
823 tracing_generic_entry_update(entry
, 0);
824 entry
->type
= TRACE_SPECIAL
;
825 entry
->special
.arg1
= arg1
;
826 entry
->special
.arg2
= arg2
;
827 entry
->special
.arg3
= arg3
;
828 __raw_spin_unlock(&data
->lock
);
829 raw_local_irq_restore(irq_flags
);
834 void __trace_stack(struct trace_array
*tr
,
835 struct trace_array_cpu
*data
,
839 struct trace_entry
*entry
;
840 struct stack_trace trace
;
842 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
845 entry
= tracing_get_trace_entry(tr
, data
);
846 tracing_generic_entry_update(entry
, flags
);
847 entry
->type
= TRACE_STACK
;
849 memset(&entry
->stack
, 0, sizeof(entry
->stack
));
851 trace
.nr_entries
= 0;
852 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
854 trace
.entries
= entry
->stack
.caller
;
856 save_stack_trace(&trace
);
860 tracing_sched_switch_trace(struct trace_array
*tr
,
861 struct trace_array_cpu
*data
,
862 struct task_struct
*prev
,
863 struct task_struct
*next
,
866 struct trace_entry
*entry
;
867 unsigned long irq_flags
;
869 raw_local_irq_save(irq_flags
);
870 __raw_spin_lock(&data
->lock
);
871 entry
= tracing_get_trace_entry(tr
, data
);
872 tracing_generic_entry_update(entry
, flags
);
873 entry
->type
= TRACE_CTX
;
874 entry
->ctx
.prev_pid
= prev
->pid
;
875 entry
->ctx
.prev_prio
= prev
->prio
;
876 entry
->ctx
.prev_state
= prev
->state
;
877 entry
->ctx
.next_pid
= next
->pid
;
878 entry
->ctx
.next_prio
= next
->prio
;
879 entry
->ctx
.next_state
= next
->state
;
880 __trace_stack(tr
, data
, flags
, 4);
881 __raw_spin_unlock(&data
->lock
);
882 raw_local_irq_restore(irq_flags
);
886 tracing_sched_wakeup_trace(struct trace_array
*tr
,
887 struct trace_array_cpu
*data
,
888 struct task_struct
*wakee
,
889 struct task_struct
*curr
,
892 struct trace_entry
*entry
;
893 unsigned long irq_flags
;
895 raw_local_irq_save(irq_flags
);
896 __raw_spin_lock(&data
->lock
);
897 entry
= tracing_get_trace_entry(tr
, data
);
898 tracing_generic_entry_update(entry
, flags
);
899 entry
->type
= TRACE_WAKE
;
900 entry
->ctx
.prev_pid
= curr
->pid
;
901 entry
->ctx
.prev_prio
= curr
->prio
;
902 entry
->ctx
.prev_state
= curr
->state
;
903 entry
->ctx
.next_pid
= wakee
->pid
;
904 entry
->ctx
.next_prio
= wakee
->prio
;
905 entry
->ctx
.next_state
= wakee
->state
;
906 __trace_stack(tr
, data
, flags
, 5);
907 __raw_spin_unlock(&data
->lock
);
908 raw_local_irq_restore(irq_flags
);
915 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
917 struct trace_array
*tr
= &global_trace
;
918 struct trace_array_cpu
*data
;
923 if (unlikely(!tracer_enabled
))
926 local_irq_save(flags
);
927 cpu
= raw_smp_processor_id();
928 data
= tr
->data
[cpu
];
929 disabled
= atomic_inc_return(&data
->disabled
);
931 if (likely(disabled
== 1))
932 trace_function(tr
, data
, ip
, parent_ip
, flags
);
934 atomic_dec(&data
->disabled
);
935 local_irq_restore(flags
);
938 static struct ftrace_ops trace_ops __read_mostly
=
940 .func
= function_trace_call
,
943 void tracing_start_function_trace(void)
945 register_ftrace_function(&trace_ops
);
948 void tracing_stop_function_trace(void)
950 unregister_ftrace_function(&trace_ops
);
954 enum trace_file_type
{
955 TRACE_FILE_LAT_FMT
= 1,
958 static struct trace_entry
*
959 trace_entry_idx(struct trace_array
*tr
, struct trace_array_cpu
*data
,
960 struct trace_iterator
*iter
, int cpu
)
963 struct trace_entry
*array
;
965 if (iter
->next_idx
[cpu
] >= tr
->entries
||
966 iter
->next_idx
[cpu
] >= data
->trace_idx
||
967 (data
->trace_head
== data
->trace_tail
&&
968 data
->trace_head_idx
== data
->trace_tail_idx
))
971 if (!iter
->next_page
[cpu
]) {
972 /* Initialize the iterator for this cpu trace buffer */
973 WARN_ON(!data
->trace_tail
);
974 page
= virt_to_page(data
->trace_tail
);
975 iter
->next_page
[cpu
] = &page
->lru
;
976 iter
->next_page_idx
[cpu
] = data
->trace_tail_idx
;
979 page
= list_entry(iter
->next_page
[cpu
], struct page
, lru
);
980 BUG_ON(&data
->trace_pages
== &page
->lru
);
982 array
= page_address(page
);
984 WARN_ON(iter
->next_page_idx
[cpu
] >= ENTRIES_PER_PAGE
);
985 return &array
[iter
->next_page_idx
[cpu
]];
988 static struct trace_entry
*
989 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
)
991 struct trace_array
*tr
= iter
->tr
;
992 struct trace_entry
*ent
, *next
= NULL
;
996 for_each_tracing_cpu(cpu
) {
997 if (!head_page(tr
->data
[cpu
]))
999 ent
= trace_entry_idx(tr
, tr
->data
[cpu
], iter
, cpu
);
1001 * Pick the entry with the smallest timestamp:
1003 if (ent
&& (!next
|| ent
->t
< next
->t
)) {
1010 *ent_cpu
= next_cpu
;
1015 static void trace_iterator_increment(struct trace_iterator
*iter
)
1018 iter
->next_idx
[iter
->cpu
]++;
1019 iter
->next_page_idx
[iter
->cpu
]++;
1021 if (iter
->next_page_idx
[iter
->cpu
] >= ENTRIES_PER_PAGE
) {
1022 struct trace_array_cpu
*data
= iter
->tr
->data
[iter
->cpu
];
1024 iter
->next_page_idx
[iter
->cpu
] = 0;
1025 iter
->next_page
[iter
->cpu
] =
1026 trace_next_list(data
, iter
->next_page
[iter
->cpu
]);
1030 static void trace_consume(struct trace_iterator
*iter
)
1032 struct trace_array_cpu
*data
= iter
->tr
->data
[iter
->cpu
];
1034 data
->trace_tail_idx
++;
1035 if (data
->trace_tail_idx
>= ENTRIES_PER_PAGE
) {
1036 data
->trace_tail
= trace_next_page(data
, data
->trace_tail
);
1037 data
->trace_tail_idx
= 0;
1040 /* Check if we empty it, then reset the index */
1041 if (data
->trace_head
== data
->trace_tail
&&
1042 data
->trace_head_idx
== data
->trace_tail_idx
)
1043 data
->trace_idx
= 0;
1046 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1048 struct trace_entry
*next
;
1051 next
= find_next_entry(iter
, &next_cpu
);
1053 iter
->prev_ent
= iter
->ent
;
1054 iter
->prev_cpu
= iter
->cpu
;
1057 iter
->cpu
= next_cpu
;
1060 trace_iterator_increment(iter
);
1062 return next
? iter
: NULL
;
1065 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1067 struct trace_iterator
*iter
= m
->private;
1068 void *last_ent
= iter
->ent
;
1074 /* can't go backwards */
1079 ent
= find_next_entry_inc(iter
);
1083 while (ent
&& iter
->idx
< i
)
1084 ent
= find_next_entry_inc(iter
);
1088 if (last_ent
&& !ent
)
1089 seq_puts(m
, "\n\nvim:ft=help\n");
1094 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1096 struct trace_iterator
*iter
= m
->private;
1101 mutex_lock(&trace_types_lock
);
1103 if (!current_trace
|| current_trace
!= iter
->trace
) {
1104 mutex_unlock(&trace_types_lock
);
1108 atomic_inc(&trace_record_cmdline_disabled
);
1110 /* let the tracer grab locks here if needed */
1111 if (current_trace
->start
)
1112 current_trace
->start(iter
);
1114 if (*pos
!= iter
->pos
) {
1118 iter
->prev_ent
= NULL
;
1119 iter
->prev_cpu
= -1;
1121 for_each_tracing_cpu(i
) {
1122 iter
->next_idx
[i
] = 0;
1123 iter
->next_page
[i
] = NULL
;
1126 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1131 p
= s_next(m
, p
, &l
);
1137 static void s_stop(struct seq_file
*m
, void *p
)
1139 struct trace_iterator
*iter
= m
->private;
1141 atomic_dec(&trace_record_cmdline_disabled
);
1143 /* let the tracer release locks here if needed */
1144 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
1145 iter
->trace
->stop(iter
);
1147 mutex_unlock(&trace_types_lock
);
1151 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1153 #ifdef CONFIG_KALLSYMS
1154 char str
[KSYM_SYMBOL_LEN
];
1156 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1158 return trace_seq_printf(s
, fmt
, str
);
1164 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1165 unsigned long address
)
1167 #ifdef CONFIG_KALLSYMS
1168 char str
[KSYM_SYMBOL_LEN
];
1170 sprint_symbol(str
, address
);
1171 return trace_seq_printf(s
, fmt
, str
);
1176 #ifndef CONFIG_64BIT
1177 # define IP_FMT "%08lx"
1179 # define IP_FMT "%016lx"
1183 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1188 return trace_seq_printf(s
, "0");
1190 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1191 ret
= seq_print_sym_offset(s
, "%s", ip
);
1193 ret
= seq_print_sym_short(s
, "%s", ip
);
1198 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1199 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1203 static void print_lat_help_header(struct seq_file
*m
)
1205 seq_puts(m
, "# _------=> CPU# \n");
1206 seq_puts(m
, "# / _-----=> irqs-off \n");
1207 seq_puts(m
, "# | / _----=> need-resched \n");
1208 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1209 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1210 seq_puts(m
, "# |||| / \n");
1211 seq_puts(m
, "# ||||| delay \n");
1212 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1213 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1216 static void print_func_help_header(struct seq_file
*m
)
1218 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1219 seq_puts(m
, "# | | | | |\n");
1224 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1226 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1227 struct trace_array
*tr
= iter
->tr
;
1228 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1229 struct tracer
*type
= current_trace
;
1230 unsigned long total
= 0;
1231 unsigned long entries
= 0;
1233 const char *name
= "preemption";
1238 for_each_tracing_cpu(cpu
) {
1239 if (head_page(tr
->data
[cpu
])) {
1240 total
+= tr
->data
[cpu
]->trace_idx
;
1241 if (tr
->data
[cpu
]->trace_idx
> tr
->entries
)
1242 entries
+= tr
->entries
;
1244 entries
+= tr
->data
[cpu
]->trace_idx
;
1248 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1250 seq_puts(m
, "-----------------------------------"
1251 "---------------------------------\n");
1252 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1253 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1254 nsecs_to_usecs(data
->saved_latency
),
1258 #if defined(CONFIG_PREEMPT_NONE)
1260 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1262 #elif defined(CONFIG_PREEMPT_DESKTOP)
1267 /* These are reserved for later use */
1270 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1274 seq_puts(m
, " -----------------\n");
1275 seq_printf(m
, " | task: %.16s-%d "
1276 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1277 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1278 data
->policy
, data
->rt_priority
);
1279 seq_puts(m
, " -----------------\n");
1281 if (data
->critical_start
) {
1282 seq_puts(m
, " => started at: ");
1283 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1284 trace_print_seq(m
, &iter
->seq
);
1285 seq_puts(m
, "\n => ended at: ");
1286 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1287 trace_print_seq(m
, &iter
->seq
);
1295 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1297 int hardirq
, softirq
;
1300 comm
= trace_find_cmdline(entry
->pid
);
1302 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1303 trace_seq_printf(s
, "%d", cpu
);
1304 trace_seq_printf(s
, "%c%c",
1305 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
1306 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1308 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1309 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1310 if (hardirq
&& softirq
) {
1311 trace_seq_putc(s
, 'H');
1314 trace_seq_putc(s
, 'h');
1317 trace_seq_putc(s
, 's');
1319 trace_seq_putc(s
, '.');
1323 if (entry
->preempt_count
)
1324 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1326 trace_seq_puts(s
, ".");
1329 unsigned long preempt_mark_thresh
= 100;
1332 lat_print_timestamp(struct trace_seq
*s
, unsigned long long abs_usecs
,
1333 unsigned long rel_usecs
)
1335 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1336 if (rel_usecs
> preempt_mark_thresh
)
1337 trace_seq_puts(s
, "!: ");
1338 else if (rel_usecs
> 1)
1339 trace_seq_puts(s
, "+: ");
1341 trace_seq_puts(s
, " : ");
1344 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1347 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1349 struct trace_seq
*s
= &iter
->seq
;
1350 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1351 struct trace_entry
*next_entry
= find_next_entry(iter
, NULL
);
1352 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1353 struct trace_entry
*entry
= iter
->ent
;
1354 unsigned long abs_usecs
;
1355 unsigned long rel_usecs
;
1363 rel_usecs
= ns2usecs(next_entry
->t
- entry
->t
);
1364 abs_usecs
= ns2usecs(entry
->t
- iter
->tr
->time_start
);
1367 comm
= trace_find_cmdline(entry
->pid
);
1368 trace_seq_printf(s
, "%16s %5d %d %d %08x %08x [%08lx]"
1369 " %ld.%03ldms (+%ld.%03ldms): ",
1371 entry
->pid
, cpu
, entry
->flags
,
1372 entry
->preempt_count
, trace_idx
,
1375 abs_usecs
% 1000, rel_usecs
/1000,
1378 lat_print_generic(s
, entry
, cpu
);
1379 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1381 switch (entry
->type
) {
1383 seq_print_ip_sym(s
, entry
->fn
.ip
, sym_flags
);
1384 trace_seq_puts(s
, " (");
1385 seq_print_ip_sym(s
, entry
->fn
.parent_ip
, sym_flags
);
1386 trace_seq_puts(s
, ")\n");
1390 T
= entry
->ctx
.next_state
< sizeof(state_to_char
) ?
1391 state_to_char
[entry
->ctx
.next_state
] : 'X';
1393 state
= entry
->ctx
.prev_state
? __ffs(entry
->ctx
.prev_state
) + 1 : 0;
1394 S
= state
< sizeof(state_to_char
) - 1 ? state_to_char
[state
] : 'X';
1395 comm
= trace_find_cmdline(entry
->ctx
.next_pid
);
1396 trace_seq_printf(s
, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1397 entry
->ctx
.prev_pid
,
1398 entry
->ctx
.prev_prio
,
1399 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1400 entry
->ctx
.next_pid
,
1401 entry
->ctx
.next_prio
,
1405 trace_seq_printf(s
, "# %ld %ld %ld\n",
1406 entry
->special
.arg1
,
1407 entry
->special
.arg2
,
1408 entry
->special
.arg3
);
1411 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1413 trace_seq_puts(s
, " <= ");
1414 seq_print_ip_sym(s
, entry
->stack
.caller
[i
], sym_flags
);
1416 trace_seq_puts(s
, "\n");
1419 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1424 static int print_trace_fmt(struct trace_iterator
*iter
)
1426 struct trace_seq
*s
= &iter
->seq
;
1427 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1428 struct trace_entry
*entry
;
1429 unsigned long usec_rem
;
1430 unsigned long long t
;
1439 comm
= trace_find_cmdline(iter
->ent
->pid
);
1441 t
= ns2usecs(entry
->t
);
1442 usec_rem
= do_div(t
, 1000000ULL);
1443 secs
= (unsigned long)t
;
1445 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1448 ret
= trace_seq_printf(s
, "[%02d] ", iter
->cpu
);
1451 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1455 switch (entry
->type
) {
1457 ret
= seq_print_ip_sym(s
, entry
->fn
.ip
, sym_flags
);
1460 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1461 entry
->fn
.parent_ip
) {
1462 ret
= trace_seq_printf(s
, " <-");
1465 ret
= seq_print_ip_sym(s
, entry
->fn
.parent_ip
,
1470 ret
= trace_seq_printf(s
, "\n");
1476 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1477 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1478 T
= entry
->ctx
.next_state
< sizeof(state_to_char
) ?
1479 state_to_char
[entry
->ctx
.next_state
] : 'X';
1480 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s %5d:%3d:%c\n",
1481 entry
->ctx
.prev_pid
,
1482 entry
->ctx
.prev_prio
,
1484 entry
->type
== TRACE_CTX
? "==>" : " +",
1485 entry
->ctx
.next_pid
,
1486 entry
->ctx
.next_prio
,
1492 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1493 entry
->special
.arg1
,
1494 entry
->special
.arg2
,
1495 entry
->special
.arg3
);
1500 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1502 ret
= trace_seq_puts(s
, " <= ");
1506 ret
= seq_print_ip_sym(s
, entry
->stack
.caller
[i
],
1511 ret
= trace_seq_puts(s
, "\n");
1519 static int print_raw_fmt(struct trace_iterator
*iter
)
1521 struct trace_seq
*s
= &iter
->seq
;
1522 struct trace_entry
*entry
;
1528 ret
= trace_seq_printf(s
, "%d %d %llu ",
1529 entry
->pid
, iter
->cpu
, entry
->t
);
1533 switch (entry
->type
) {
1535 ret
= trace_seq_printf(s
, "%x %x\n",
1536 entry
->fn
.ip
, entry
->fn
.parent_ip
);
1542 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1543 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1544 T
= entry
->ctx
.next_state
< sizeof(state_to_char
) ?
1545 state_to_char
[entry
->ctx
.next_state
] : 'X';
1546 if (entry
->type
== TRACE_WAKE
)
1548 ret
= trace_seq_printf(s
, "%d %d %c %d %d %c\n",
1549 entry
->ctx
.prev_pid
,
1550 entry
->ctx
.prev_prio
,
1552 entry
->ctx
.next_pid
,
1553 entry
->ctx
.next_prio
,
1560 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1561 entry
->special
.arg1
,
1562 entry
->special
.arg2
,
1563 entry
->special
.arg3
);
1571 #define SEQ_PUT_FIELD_RET(s, x) \
1573 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1577 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1579 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1583 static int print_hex_fmt(struct trace_iterator
*iter
)
1585 struct trace_seq
*s
= &iter
->seq
;
1586 unsigned char newline
= '\n';
1587 struct trace_entry
*entry
;
1592 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1593 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1594 SEQ_PUT_HEX_FIELD_RET(s
, entry
->t
);
1596 switch (entry
->type
) {
1598 SEQ_PUT_HEX_FIELD_RET(s
, entry
->fn
.ip
);
1599 SEQ_PUT_HEX_FIELD_RET(s
, entry
->fn
.parent_ip
);
1603 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1604 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1605 T
= entry
->ctx
.next_state
< sizeof(state_to_char
) ?
1606 state_to_char
[entry
->ctx
.next_state
] : 'X';
1607 if (entry
->type
== TRACE_WAKE
)
1609 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.prev_pid
);
1610 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.prev_prio
);
1611 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1612 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.next_pid
);
1613 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.next_prio
);
1614 SEQ_PUT_HEX_FIELD_RET(s
, entry
->fn
.parent_ip
);
1615 SEQ_PUT_HEX_FIELD_RET(s
, T
);
1619 SEQ_PUT_HEX_FIELD_RET(s
, entry
->special
.arg1
);
1620 SEQ_PUT_HEX_FIELD_RET(s
, entry
->special
.arg2
);
1621 SEQ_PUT_HEX_FIELD_RET(s
, entry
->special
.arg3
);
1624 SEQ_PUT_FIELD_RET(s
, newline
);
1629 static int print_bin_fmt(struct trace_iterator
*iter
)
1631 struct trace_seq
*s
= &iter
->seq
;
1632 struct trace_entry
*entry
;
1636 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1637 SEQ_PUT_FIELD_RET(s
, entry
->cpu
);
1638 SEQ_PUT_FIELD_RET(s
, entry
->t
);
1640 switch (entry
->type
) {
1642 SEQ_PUT_FIELD_RET(s
, entry
->fn
.ip
);
1643 SEQ_PUT_FIELD_RET(s
, entry
->fn
.parent_ip
);
1646 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.prev_pid
);
1647 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.prev_prio
);
1648 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.prev_state
);
1649 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.next_pid
);
1650 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.next_prio
);
1651 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.next_state
);
1655 SEQ_PUT_FIELD_RET(s
, entry
->special
.arg1
);
1656 SEQ_PUT_FIELD_RET(s
, entry
->special
.arg2
);
1657 SEQ_PUT_FIELD_RET(s
, entry
->special
.arg3
);
1663 static int trace_empty(struct trace_iterator
*iter
)
1665 struct trace_array_cpu
*data
;
1668 for_each_tracing_cpu(cpu
) {
1669 data
= iter
->tr
->data
[cpu
];
1671 if (head_page(data
) && data
->trace_idx
&&
1672 (data
->trace_tail
!= data
->trace_head
||
1673 data
->trace_tail_idx
!= data
->trace_head_idx
))
1679 static int print_trace_line(struct trace_iterator
*iter
)
1681 if (iter
->trace
&& iter
->trace
->print_line
)
1682 return iter
->trace
->print_line(iter
);
1684 if (trace_flags
& TRACE_ITER_BIN
)
1685 return print_bin_fmt(iter
);
1687 if (trace_flags
& TRACE_ITER_HEX
)
1688 return print_hex_fmt(iter
);
1690 if (trace_flags
& TRACE_ITER_RAW
)
1691 return print_raw_fmt(iter
);
1693 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1694 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1696 return print_trace_fmt(iter
);
1699 static int s_show(struct seq_file
*m
, void *v
)
1701 struct trace_iterator
*iter
= v
;
1703 if (iter
->ent
== NULL
) {
1705 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1708 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1709 /* print nothing if the buffers are empty */
1710 if (trace_empty(iter
))
1712 print_trace_header(m
, iter
);
1713 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1714 print_lat_help_header(m
);
1716 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1717 print_func_help_header(m
);
1720 print_trace_line(iter
);
1721 trace_print_seq(m
, &iter
->seq
);
1727 static struct seq_operations tracer_seq_ops
= {
1734 static struct trace_iterator
*
1735 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1737 struct trace_iterator
*iter
;
1739 if (tracing_disabled
) {
1744 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1750 mutex_lock(&trace_types_lock
);
1751 if (current_trace
&& current_trace
->print_max
)
1754 iter
->tr
= inode
->i_private
;
1755 iter
->trace
= current_trace
;
1758 /* TODO stop tracer */
1759 *ret
= seq_open(file
, &tracer_seq_ops
);
1761 struct seq_file
*m
= file
->private_data
;
1764 /* stop the trace while dumping */
1768 if (iter
->trace
&& iter
->trace
->open
)
1769 iter
->trace
->open(iter
);
1774 mutex_unlock(&trace_types_lock
);
1780 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1782 if (tracing_disabled
)
1785 filp
->private_data
= inode
->i_private
;
1789 int tracing_release(struct inode
*inode
, struct file
*file
)
1791 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1792 struct trace_iterator
*iter
= m
->private;
1794 mutex_lock(&trace_types_lock
);
1795 if (iter
->trace
&& iter
->trace
->close
)
1796 iter
->trace
->close(iter
);
1798 /* reenable tracing if it was previously enabled */
1801 mutex_unlock(&trace_types_lock
);
1803 seq_release(inode
, file
);
1808 static int tracing_open(struct inode
*inode
, struct file
*file
)
1812 __tracing_open(inode
, file
, &ret
);
1817 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1819 struct trace_iterator
*iter
;
1822 iter
= __tracing_open(inode
, file
, &ret
);
1825 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1832 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1834 struct tracer
*t
= m
->private;
1846 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1848 struct tracer
*t
= m
->private;
1851 mutex_lock(&trace_types_lock
);
1852 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1858 static void t_stop(struct seq_file
*m
, void *p
)
1860 mutex_unlock(&trace_types_lock
);
1863 static int t_show(struct seq_file
*m
, void *v
)
1865 struct tracer
*t
= v
;
1870 seq_printf(m
, "%s", t
->name
);
1879 static struct seq_operations show_traces_seq_ops
= {
1886 static int show_traces_open(struct inode
*inode
, struct file
*file
)
1890 if (tracing_disabled
)
1893 ret
= seq_open(file
, &show_traces_seq_ops
);
1895 struct seq_file
*m
= file
->private_data
;
1896 m
->private = trace_types
;
1902 static struct file_operations tracing_fops
= {
1903 .open
= tracing_open
,
1905 .llseek
= seq_lseek
,
1906 .release
= tracing_release
,
1909 static struct file_operations tracing_lt_fops
= {
1910 .open
= tracing_lt_open
,
1912 .llseek
= seq_lseek
,
1913 .release
= tracing_release
,
1916 static struct file_operations show_traces_fops
= {
1917 .open
= show_traces_open
,
1919 .release
= seq_release
,
1923 * Only trace on a CPU if the bitmask is set:
1925 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
1928 * When tracing/tracing_cpu_mask is modified then this holds
1929 * the new bitmask we are about to install:
1931 static cpumask_t tracing_cpumask_new
;
1934 * The tracer itself will not take this lock, but still we want
1935 * to provide a consistent cpumask to user-space:
1937 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
1940 * Temporary storage for the character representation of the
1941 * CPU bitmask (and one more byte for the newline):
1943 static char mask_str
[NR_CPUS
+ 1];
1946 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
1947 size_t count
, loff_t
*ppos
)
1951 mutex_lock(&tracing_cpumask_update_lock
);
1953 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
1954 if (count
- len
< 2) {
1958 len
+= sprintf(mask_str
+ len
, "\n");
1959 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
1962 mutex_unlock(&tracing_cpumask_update_lock
);
1968 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
1969 size_t count
, loff_t
*ppos
)
1973 mutex_lock(&tracing_cpumask_update_lock
);
1974 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
1978 raw_local_irq_disable();
1979 __raw_spin_lock(&ftrace_max_lock
);
1980 for_each_tracing_cpu(cpu
) {
1982 * Increase/decrease the disabled counter if we are
1983 * about to flip a bit in the cpumask:
1985 if (cpu_isset(cpu
, tracing_cpumask
) &&
1986 !cpu_isset(cpu
, tracing_cpumask_new
)) {
1987 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
1989 if (!cpu_isset(cpu
, tracing_cpumask
) &&
1990 cpu_isset(cpu
, tracing_cpumask_new
)) {
1991 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
1994 __raw_spin_unlock(&ftrace_max_lock
);
1995 raw_local_irq_enable();
1997 tracing_cpumask
= tracing_cpumask_new
;
1999 mutex_unlock(&tracing_cpumask_update_lock
);
2004 mutex_unlock(&tracing_cpumask_update_lock
);
2009 static struct file_operations tracing_cpumask_fops
= {
2010 .open
= tracing_open_generic
,
2011 .read
= tracing_cpumask_read
,
2012 .write
= tracing_cpumask_write
,
2016 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2017 size_t cnt
, loff_t
*ppos
)
2024 /* calulate max size */
2025 for (i
= 0; trace_options
[i
]; i
++) {
2026 len
+= strlen(trace_options
[i
]);
2027 len
+= 3; /* "no" and space */
2030 /* +2 for \n and \0 */
2031 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2035 for (i
= 0; trace_options
[i
]; i
++) {
2036 if (trace_flags
& (1 << i
))
2037 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2039 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2042 r
+= sprintf(buf
+ r
, "\n");
2043 WARN_ON(r
>= len
+ 2);
2045 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2053 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2054 size_t cnt
, loff_t
*ppos
)
2061 if (cnt
>= sizeof(buf
))
2064 if (copy_from_user(&buf
, ubuf
, cnt
))
2069 if (strncmp(buf
, "no", 2) == 0) {
2074 for (i
= 0; trace_options
[i
]; i
++) {
2075 int len
= strlen(trace_options
[i
]);
2077 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2079 trace_flags
&= ~(1 << i
);
2081 trace_flags
|= (1 << i
);
2086 * If no option could be set, return an error:
2088 if (!trace_options
[i
])
2096 static struct file_operations tracing_iter_fops
= {
2097 .open
= tracing_open_generic
,
2098 .read
= tracing_iter_ctrl_read
,
2099 .write
= tracing_iter_ctrl_write
,
2102 static const char readme_msg
[] =
2103 "tracing mini-HOWTO:\n\n"
2105 "# mount -t debugfs nodev /debug\n\n"
2106 "# cat /debug/tracing/available_tracers\n"
2107 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2108 "# cat /debug/tracing/current_tracer\n"
2110 "# echo sched_switch > /debug/tracing/current_tracer\n"
2111 "# cat /debug/tracing/current_tracer\n"
2113 "# cat /debug/tracing/iter_ctrl\n"
2114 "noprint-parent nosym-offset nosym-addr noverbose\n"
2115 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2116 "# echo 1 > /debug/tracing/tracing_enabled\n"
2117 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2118 "echo 0 > /debug/tracing/tracing_enabled\n"
2122 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2123 size_t cnt
, loff_t
*ppos
)
2125 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2126 readme_msg
, strlen(readme_msg
));
2129 static struct file_operations tracing_readme_fops
= {
2130 .open
= tracing_open_generic
,
2131 .read
= tracing_readme_read
,
2135 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2136 size_t cnt
, loff_t
*ppos
)
2138 struct trace_array
*tr
= filp
->private_data
;
2142 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
2143 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2147 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2148 size_t cnt
, loff_t
*ppos
)
2150 struct trace_array
*tr
= filp
->private_data
;
2155 if (cnt
>= sizeof(buf
))
2158 if (copy_from_user(&buf
, ubuf
, cnt
))
2163 ret
= strict_strtoul(buf
, 10, &val
);
2169 mutex_lock(&trace_types_lock
);
2170 if (tr
->ctrl
^ val
) {
2178 if (current_trace
&& current_trace
->ctrl_update
)
2179 current_trace
->ctrl_update(tr
);
2181 mutex_unlock(&trace_types_lock
);
2189 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2190 size_t cnt
, loff_t
*ppos
)
2192 char buf
[max_tracer_type_len
+2];
2195 mutex_lock(&trace_types_lock
);
2197 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2199 r
= sprintf(buf
, "\n");
2200 mutex_unlock(&trace_types_lock
);
2202 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2206 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2207 size_t cnt
, loff_t
*ppos
)
2209 struct trace_array
*tr
= &global_trace
;
2211 char buf
[max_tracer_type_len
+1];
2214 if (cnt
> max_tracer_type_len
)
2215 cnt
= max_tracer_type_len
;
2217 if (copy_from_user(&buf
, ubuf
, cnt
))
2222 /* strip ending whitespace. */
2223 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2226 mutex_lock(&trace_types_lock
);
2227 for (t
= trace_types
; t
; t
= t
->next
) {
2228 if (strcmp(t
->name
, buf
) == 0)
2231 if (!t
|| t
== current_trace
)
2234 if (current_trace
&& current_trace
->reset
)
2235 current_trace
->reset(tr
);
2242 mutex_unlock(&trace_types_lock
);
2250 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2251 size_t cnt
, loff_t
*ppos
)
2253 unsigned long *ptr
= filp
->private_data
;
2257 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2258 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2259 if (r
> sizeof(buf
))
2261 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2265 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2266 size_t cnt
, loff_t
*ppos
)
2268 long *ptr
= filp
->private_data
;
2273 if (cnt
>= sizeof(buf
))
2276 if (copy_from_user(&buf
, ubuf
, cnt
))
2281 ret
= strict_strtoul(buf
, 10, &val
);
2290 static atomic_t tracing_reader
;
2292 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2294 struct trace_iterator
*iter
;
2296 if (tracing_disabled
)
2299 /* We only allow for reader of the pipe */
2300 if (atomic_inc_return(&tracing_reader
) != 1) {
2301 atomic_dec(&tracing_reader
);
2305 /* create a buffer to store the information to pass to userspace */
2306 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2310 mutex_lock(&trace_types_lock
);
2311 iter
->tr
= &global_trace
;
2312 iter
->trace
= current_trace
;
2313 filp
->private_data
= iter
;
2315 if (iter
->trace
->pipe_open
)
2316 iter
->trace
->pipe_open(iter
);
2317 mutex_unlock(&trace_types_lock
);
2322 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2324 struct trace_iterator
*iter
= file
->private_data
;
2327 atomic_dec(&tracing_reader
);
2333 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2335 struct trace_iterator
*iter
= filp
->private_data
;
2337 if (trace_flags
& TRACE_ITER_BLOCK
) {
2339 * Always select as readable when in blocking mode
2341 return POLLIN
| POLLRDNORM
;
2343 if (!trace_empty(iter
))
2344 return POLLIN
| POLLRDNORM
;
2345 poll_wait(filp
, &trace_wait
, poll_table
);
2346 if (!trace_empty(iter
))
2347 return POLLIN
| POLLRDNORM
;
2357 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2358 size_t cnt
, loff_t
*ppos
)
2360 struct trace_iterator
*iter
= filp
->private_data
;
2361 struct trace_array_cpu
*data
;
2362 static cpumask_t mask
;
2364 unsigned long flags
;
2365 #ifdef CONFIG_FTRACE
2373 /* return any leftover data */
2374 if (iter
->seq
.len
> start
) {
2375 len
= iter
->seq
.len
- start
;
2378 ret
= copy_to_user(ubuf
, iter
->seq
.buffer
+ start
, cnt
);
2387 mutex_lock(&trace_types_lock
);
2388 if (iter
->trace
->read
) {
2389 ret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2396 trace_seq_reset(&iter
->seq
);
2399 while (trace_empty(iter
)) {
2401 if ((filp
->f_flags
& O_NONBLOCK
)) {
2407 * This is a make-shift waitqueue. The reason we don't use
2408 * an actual wait queue is because:
2409 * 1) we only ever have one waiter
2410 * 2) the tracing, traces all functions, we don't want
2411 * the overhead of calling wake_up and friends
2412 * (and tracing them too)
2413 * Anyway, this is really very primitive wakeup.
2415 set_current_state(TASK_INTERRUPTIBLE
);
2416 iter
->tr
->waiter
= current
;
2418 mutex_unlock(&trace_types_lock
);
2420 /* sleep for one second, and try again. */
2421 schedule_timeout(HZ
);
2423 mutex_lock(&trace_types_lock
);
2425 iter
->tr
->waiter
= NULL
;
2427 if (signal_pending(current
)) {
2432 if (iter
->trace
!= current_trace
)
2436 * We block until we read something and tracing is disabled.
2437 * We still block if tracing is disabled, but we have never
2438 * read anything. This allows a user to cat this file, and
2439 * then enable tracing. But after we have read something,
2440 * we give an EOF when tracing is again disabled.
2442 * iter->pos will be 0 if we haven't read anything.
2444 if (!tracer_enabled
&& iter
->pos
)
2450 /* stop when tracing is finished */
2451 if (trace_empty(iter
))
2454 if (cnt
>= PAGE_SIZE
)
2455 cnt
= PAGE_SIZE
- 1;
2457 /* reset all but tr, trace, and overruns */
2458 memset(&iter
->seq
, 0,
2459 sizeof(struct trace_iterator
) -
2460 offsetof(struct trace_iterator
, seq
));
2464 * We need to stop all tracing on all CPUS to read the
2465 * the next buffer. This is a bit expensive, but is
2466 * not done often. We fill all what we can read,
2467 * and then release the locks again.
2471 local_irq_save(flags
);
2472 #ifdef CONFIG_FTRACE
2473 ftrace_save
= ftrace_enabled
;
2477 for_each_tracing_cpu(cpu
) {
2478 data
= iter
->tr
->data
[cpu
];
2480 if (!head_page(data
) || !data
->trace_idx
)
2483 atomic_inc(&data
->disabled
);
2487 for_each_cpu_mask(cpu
, mask
) {
2488 data
= iter
->tr
->data
[cpu
];
2489 __raw_spin_lock(&data
->lock
);
2491 if (data
->overrun
> iter
->last_overrun
[cpu
])
2492 iter
->overrun
[cpu
] +=
2493 data
->overrun
- iter
->last_overrun
[cpu
];
2494 iter
->last_overrun
[cpu
] = data
->overrun
;
2497 while (find_next_entry_inc(iter
) != NULL
) {
2498 int len
= iter
->seq
.len
;
2500 ret
= print_trace_line(iter
);
2502 /* don't print partial lines */
2503 iter
->seq
.len
= len
;
2507 trace_consume(iter
);
2509 if (iter
->seq
.len
>= cnt
)
2513 for_each_cpu_mask(cpu
, mask
) {
2514 data
= iter
->tr
->data
[cpu
];
2515 __raw_spin_unlock(&data
->lock
);
2518 for_each_cpu_mask(cpu
, mask
) {
2519 data
= iter
->tr
->data
[cpu
];
2520 atomic_dec(&data
->disabled
);
2522 #ifdef CONFIG_FTRACE
2523 ftrace_enabled
= ftrace_save
;
2525 local_irq_restore(flags
);
2527 /* Now copy what we have to the user */
2528 read
= iter
->seq
.len
;
2532 ret
= copy_to_user(ubuf
, iter
->seq
.buffer
, read
);
2534 if (read
< iter
->seq
.len
)
2537 trace_seq_reset(&iter
->seq
);
2543 mutex_unlock(&trace_types_lock
);
2549 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2550 size_t cnt
, loff_t
*ppos
)
2552 struct trace_array
*tr
= filp
->private_data
;
2556 r
= sprintf(buf
, "%lu\n", tr
->entries
);
2557 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2561 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2562 size_t cnt
, loff_t
*ppos
)
2568 if (cnt
>= sizeof(buf
))
2571 if (copy_from_user(&buf
, ubuf
, cnt
))
2576 ret
= strict_strtoul(buf
, 10, &val
);
2580 /* must have at least 1 entry */
2584 mutex_lock(&trace_types_lock
);
2586 if (current_trace
!= &no_tracer
) {
2588 pr_info("ftrace: set current_tracer to none"
2589 " before modifying buffer size\n");
2593 if (val
> global_trace
.entries
) {
2594 while (global_trace
.entries
< val
) {
2595 if (trace_alloc_page()) {
2601 /* include the number of entries in val (inc of page entries) */
2602 while (global_trace
.entries
> val
+ (ENTRIES_PER_PAGE
- 1))
2609 max_tr
.entries
= global_trace
.entries
;
2610 mutex_unlock(&trace_types_lock
);
2615 static struct file_operations tracing_max_lat_fops
= {
2616 .open
= tracing_open_generic
,
2617 .read
= tracing_max_lat_read
,
2618 .write
= tracing_max_lat_write
,
2621 static struct file_operations tracing_ctrl_fops
= {
2622 .open
= tracing_open_generic
,
2623 .read
= tracing_ctrl_read
,
2624 .write
= tracing_ctrl_write
,
2627 static struct file_operations set_tracer_fops
= {
2628 .open
= tracing_open_generic
,
2629 .read
= tracing_set_trace_read
,
2630 .write
= tracing_set_trace_write
,
2633 static struct file_operations tracing_pipe_fops
= {
2634 .open
= tracing_open_pipe
,
2635 .poll
= tracing_poll_pipe
,
2636 .read
= tracing_read_pipe
,
2637 .release
= tracing_release_pipe
,
2640 static struct file_operations tracing_entries_fops
= {
2641 .open
= tracing_open_generic
,
2642 .read
= tracing_entries_read
,
2643 .write
= tracing_entries_write
,
2646 #ifdef CONFIG_DYNAMIC_FTRACE
2649 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
2650 size_t cnt
, loff_t
*ppos
)
2652 unsigned long *p
= filp
->private_data
;
2656 r
= sprintf(buf
, "%ld\n", *p
);
2658 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2661 static struct file_operations tracing_read_long_fops
= {
2662 .open
= tracing_open_generic
,
2663 .read
= tracing_read_long
,
2667 static struct dentry
*d_tracer
;
2669 struct dentry
*tracing_init_dentry(void)
2676 d_tracer
= debugfs_create_dir("tracing", NULL
);
2678 if (!d_tracer
&& !once
) {
2680 pr_warning("Could not create debugfs directory 'tracing'\n");
2687 #ifdef CONFIG_FTRACE_SELFTEST
2688 /* Let selftest have access to static functions in this file */
2689 #include "trace_selftest.c"
2692 static __init
void tracer_init_debugfs(void)
2694 struct dentry
*d_tracer
;
2695 struct dentry
*entry
;
2697 d_tracer
= tracing_init_dentry();
2699 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
2700 &global_trace
, &tracing_ctrl_fops
);
2702 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2704 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
2705 NULL
, &tracing_iter_fops
);
2707 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2709 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
2710 NULL
, &tracing_cpumask_fops
);
2712 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2714 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2715 &global_trace
, &tracing_lt_fops
);
2717 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2719 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2720 &global_trace
, &tracing_fops
);
2722 pr_warning("Could not create debugfs 'trace' entry\n");
2724 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2725 &global_trace
, &show_traces_fops
);
2727 pr_warning("Could not create debugfs 'trace' entry\n");
2729 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2730 &global_trace
, &set_tracer_fops
);
2732 pr_warning("Could not create debugfs 'trace' entry\n");
2734 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2735 &tracing_max_latency
,
2736 &tracing_max_lat_fops
);
2738 pr_warning("Could not create debugfs "
2739 "'tracing_max_latency' entry\n");
2741 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2742 &tracing_thresh
, &tracing_max_lat_fops
);
2744 pr_warning("Could not create debugfs "
2745 "'tracing_threash' entry\n");
2746 entry
= debugfs_create_file("README", 0644, d_tracer
,
2747 NULL
, &tracing_readme_fops
);
2749 pr_warning("Could not create debugfs 'README' entry\n");
2751 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2752 NULL
, &tracing_pipe_fops
);
2754 pr_warning("Could not create debugfs "
2755 "'tracing_threash' entry\n");
2757 entry
= debugfs_create_file("trace_entries", 0644, d_tracer
,
2758 &global_trace
, &tracing_entries_fops
);
2760 pr_warning("Could not create debugfs "
2761 "'tracing_threash' entry\n");
2763 #ifdef CONFIG_DYNAMIC_FTRACE
2764 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
2765 &ftrace_update_tot_cnt
,
2766 &tracing_read_long_fops
);
2768 pr_warning("Could not create debugfs "
2769 "'dyn_ftrace_total_info' entry\n");
2773 static int trace_alloc_page(void)
2775 struct trace_array_cpu
*data
;
2776 struct page
*page
, *tmp
;
2781 /* first allocate a page for each CPU */
2782 for_each_tracing_cpu(i
) {
2783 array
= (void *)__get_free_page(GFP_KERNEL
);
2784 if (array
== NULL
) {
2785 printk(KERN_ERR
"tracer: failed to allocate page"
2786 "for trace buffer!\n");
2790 page
= virt_to_page(array
);
2791 list_add(&page
->lru
, &pages
);
2793 /* Only allocate if we are actually using the max trace */
2794 #ifdef CONFIG_TRACER_MAX_TRACE
2795 array
= (void *)__get_free_page(GFP_KERNEL
);
2796 if (array
== NULL
) {
2797 printk(KERN_ERR
"tracer: failed to allocate page"
2798 "for trace buffer!\n");
2801 page
= virt_to_page(array
);
2802 list_add(&page
->lru
, &pages
);
2806 /* Now that we successfully allocate a page per CPU, add them */
2807 for_each_tracing_cpu(i
) {
2808 data
= global_trace
.data
[i
];
2809 page
= list_entry(pages
.next
, struct page
, lru
);
2810 list_del_init(&page
->lru
);
2811 list_add_tail(&page
->lru
, &data
->trace_pages
);
2814 #ifdef CONFIG_TRACER_MAX_TRACE
2815 data
= max_tr
.data
[i
];
2816 page
= list_entry(pages
.next
, struct page
, lru
);
2817 list_del_init(&page
->lru
);
2818 list_add_tail(&page
->lru
, &data
->trace_pages
);
2822 global_trace
.entries
+= ENTRIES_PER_PAGE
;
2827 list_for_each_entry_safe(page
, tmp
, &pages
, lru
) {
2828 list_del_init(&page
->lru
);
2834 static int trace_free_page(void)
2836 struct trace_array_cpu
*data
;
2838 struct list_head
*p
;
2842 /* free one page from each buffer */
2843 for_each_tracing_cpu(i
) {
2844 data
= global_trace
.data
[i
];
2845 p
= data
->trace_pages
.next
;
2846 if (p
== &data
->trace_pages
) {
2847 /* should never happen */
2849 tracing_disabled
= 1;
2853 page
= list_entry(p
, struct page
, lru
);
2855 list_del(&page
->lru
);
2858 tracing_reset(data
);
2860 #ifdef CONFIG_TRACER_MAX_TRACE
2861 data
= max_tr
.data
[i
];
2862 p
= data
->trace_pages
.next
;
2863 if (p
== &data
->trace_pages
) {
2864 /* should never happen */
2866 tracing_disabled
= 1;
2870 page
= list_entry(p
, struct page
, lru
);
2872 list_del(&page
->lru
);
2875 tracing_reset(data
);
2878 global_trace
.entries
-= ENTRIES_PER_PAGE
;
2883 __init
static int tracer_alloc_buffers(void)
2885 struct trace_array_cpu
*data
;
2892 global_trace
.ctrl
= tracer_enabled
;
2894 /* TODO: make the number of buffers hot pluggable with CPUS */
2895 tracing_nr_buffers
= num_possible_cpus();
2896 tracing_buffer_mask
= cpu_possible_map
;
2898 /* Allocate the first page for all buffers */
2899 for_each_tracing_cpu(i
) {
2900 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
2901 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
2903 array
= (void *)__get_free_page(GFP_KERNEL
);
2904 if (array
== NULL
) {
2905 printk(KERN_ERR
"tracer: failed to allocate page"
2906 "for trace buffer!\n");
2910 /* set the array to the list */
2911 INIT_LIST_HEAD(&data
->trace_pages
);
2912 page
= virt_to_page(array
);
2913 list_add(&page
->lru
, &data
->trace_pages
);
2914 /* use the LRU flag to differentiate the two buffers */
2917 data
->lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
2918 max_tr
.data
[i
]->lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
2920 /* Only allocate if we are actually using the max trace */
2921 #ifdef CONFIG_TRACER_MAX_TRACE
2922 array
= (void *)__get_free_page(GFP_KERNEL
);
2923 if (array
== NULL
) {
2924 printk(KERN_ERR
"tracer: failed to allocate page"
2925 "for trace buffer!\n");
2929 INIT_LIST_HEAD(&max_tr
.data
[i
]->trace_pages
);
2930 page
= virt_to_page(array
);
2931 list_add(&page
->lru
, &max_tr
.data
[i
]->trace_pages
);
2937 * Since we allocate by orders of pages, we may be able to
2940 global_trace
.entries
= ENTRIES_PER_PAGE
;
2943 while (global_trace
.entries
< trace_nr_entries
) {
2944 if (trace_alloc_page())
2948 max_tr
.entries
= global_trace
.entries
;
2950 pr_info("tracer: %d pages allocated for %ld",
2951 pages
, trace_nr_entries
);
2952 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE
);
2953 pr_info(" actual entries %ld\n", global_trace
.entries
);
2955 tracer_init_debugfs();
2957 trace_init_cmdlines();
2959 register_tracer(&no_tracer
);
2960 current_trace
= &no_tracer
;
2962 /* All seems OK, enable tracing */
2963 tracing_disabled
= 0;
2968 for (i
-- ; i
>= 0; i
--) {
2969 struct page
*page
, *tmp
;
2970 struct trace_array_cpu
*data
= global_trace
.data
[i
];
2973 list_for_each_entry_safe(page
, tmp
,
2974 &data
->trace_pages
, lru
) {
2975 list_del_init(&page
->lru
);
2980 #ifdef CONFIG_TRACER_MAX_TRACE
2981 data
= max_tr
.data
[i
];
2983 list_for_each_entry_safe(page
, tmp
,
2984 &data
->trace_pages
, lru
) {
2985 list_del_init(&page
->lru
);
2993 fs_initcall(tracer_alloc_buffers
);