2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/gfp.h>
32 unsigned long __read_mostly tracing_max_latency
= (cycle_t
)ULONG_MAX
;
33 unsigned long __read_mostly tracing_thresh
;
35 static int tracing_disabled
= 1;
38 ns2usecs(cycle_t nsec
)
45 static atomic_t tracer_counter
;
46 static struct trace_array global_trace
;
48 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
50 static struct trace_array max_tr
;
52 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
54 static int tracer_enabled
;
55 static unsigned long trace_nr_entries
= 16384UL;
57 static struct tracer
*trace_types __read_mostly
;
58 static struct tracer
*current_trace __read_mostly
;
59 static int max_tracer_type_len
;
61 static DEFINE_MUTEX(trace_types_lock
);
63 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
65 static int __init
set_nr_entries(char *str
)
69 trace_nr_entries
= simple_strtoul(str
, &str
, 0);
72 __setup("trace_entries=", set_nr_entries
);
74 unsigned long nsecs_to_usecs(unsigned long nsecs
)
80 __TRACE_FIRST_TYPE
= 0,
88 enum trace_flag_type
{
89 TRACE_FLAG_IRQS_OFF
= 0x01,
90 TRACE_FLAG_NEED_RESCHED
= 0x02,
91 TRACE_FLAG_HARDIRQ
= 0x04,
92 TRACE_FLAG_SOFTIRQ
= 0x08,
95 enum trace_iterator_flags
{
96 TRACE_ITER_PRINT_PARENT
= 0x01,
97 TRACE_ITER_SYM_OFFSET
= 0x02,
98 TRACE_ITER_SYM_ADDR
= 0x04,
99 TRACE_ITER_VERBOSE
= 0x08,
102 #define TRACE_ITER_SYM_MASK \
103 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
105 /* These must match the bit postions above */
106 static const char *trace_options
[] = {
114 static unsigned trace_flags
;
116 static DEFINE_SPINLOCK(ftrace_max_lock
);
119 * Copy the new maximum trace into the separate maximum-trace
120 * structure. (this way the maximum trace is permanently saved,
121 * for later retrieval via /debugfs/tracing/latency_trace)
124 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
126 struct trace_array_cpu
*data
= tr
->data
[cpu
];
129 max_tr
.time_start
= data
->preempt_timestamp
;
131 data
= max_tr
.data
[cpu
];
132 data
->saved_latency
= tracing_max_latency
;
134 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
135 data
->pid
= tsk
->pid
;
136 data
->uid
= tsk
->uid
;
137 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
138 data
->policy
= tsk
->policy
;
139 data
->rt_priority
= tsk
->rt_priority
;
141 /* record this tasks comm */
142 tracing_record_cmdline(current
);
145 void check_pages(struct trace_array_cpu
*data
)
147 struct page
*page
, *tmp
;
149 BUG_ON(data
->trace_pages
.next
->prev
!= &data
->trace_pages
);
150 BUG_ON(data
->trace_pages
.prev
->next
!= &data
->trace_pages
);
152 list_for_each_entry_safe(page
, tmp
, &data
->trace_pages
, lru
) {
153 BUG_ON(page
->lru
.next
->prev
!= &page
->lru
);
154 BUG_ON(page
->lru
.prev
->next
!= &page
->lru
);
158 void *head_page(struct trace_array_cpu
*data
)
163 if (list_empty(&data
->trace_pages
))
166 page
= list_entry(data
->trace_pages
.next
, struct page
, lru
);
167 BUG_ON(&page
->lru
== &data
->trace_pages
);
169 return page_address(page
);
173 flip_trace(struct trace_array_cpu
*tr1
, struct trace_array_cpu
*tr2
)
175 struct list_head flip_pages
;
177 INIT_LIST_HEAD(&flip_pages
);
179 tr1
->trace_current
= NULL
;
180 memcpy(&tr1
->trace_current_idx
, &tr2
->trace_current_idx
,
181 sizeof(struct trace_array_cpu
) -
182 offsetof(struct trace_array_cpu
, trace_current_idx
));
186 list_splice_init(&tr1
->trace_pages
, &flip_pages
);
187 list_splice_init(&tr2
->trace_pages
, &tr1
->trace_pages
);
188 list_splice_init(&flip_pages
, &tr2
->trace_pages
);
189 BUG_ON(!list_empty(&flip_pages
));
195 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
197 struct trace_array_cpu
*data
;
200 WARN_ON_ONCE(!irqs_disabled());
201 spin_lock(&ftrace_max_lock
);
202 /* clear out all the previous traces */
203 for_each_possible_cpu(i
) {
205 flip_trace(max_tr
.data
[i
], data
);
209 __update_max_tr(tr
, tsk
, cpu
);
210 spin_unlock(&ftrace_max_lock
);
214 * update_max_tr_single - only copy one trace over, and reset the rest
216 * @tsk - task with the latency
217 * @cpu - the cpu of the buffer to copy.
220 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
222 struct trace_array_cpu
*data
= tr
->data
[cpu
];
225 WARN_ON_ONCE(!irqs_disabled());
226 spin_lock(&ftrace_max_lock
);
227 for_each_possible_cpu(i
)
228 tracing_reset(max_tr
.data
[i
]);
230 flip_trace(max_tr
.data
[cpu
], data
);
234 __update_max_tr(tr
, tsk
, cpu
);
235 spin_unlock(&ftrace_max_lock
);
238 int register_tracer(struct tracer
*type
)
245 pr_info("Tracer must have a name\n");
249 mutex_lock(&trace_types_lock
);
250 for (t
= trace_types
; t
; t
= t
->next
) {
251 if (strcmp(type
->name
, t
->name
) == 0) {
253 pr_info("Trace %s already registered\n",
260 #ifdef CONFIG_FTRACE_STARTUP_TEST
261 if (type
->selftest
) {
262 struct tracer
*saved_tracer
= current_trace
;
263 struct trace_array_cpu
*data
;
264 struct trace_array
*tr
= &global_trace
;
265 int saved_ctrl
= tr
->ctrl
;
268 * Run a selftest on this tracer.
269 * Here we reset the trace buffer, and set the current
270 * tracer to be this tracer. The tracer can then run some
271 * internal tracing to verify that everything is in order.
272 * If we fail, we do not register this tracer.
274 for_each_possible_cpu(i
) {
276 if (!head_page(data
))
280 current_trace
= type
;
282 /* the test is responsible for initializing and enabling */
283 pr_info("Testing tracer %s: ", type
->name
);
284 ret
= type
->selftest(type
, tr
);
285 /* the test is responsible for resetting too */
286 current_trace
= saved_tracer
;
287 tr
->ctrl
= saved_ctrl
;
289 printk(KERN_CONT
"FAILED!\n");
292 printk(KERN_CONT
"PASSED\n");
296 type
->next
= trace_types
;
298 len
= strlen(type
->name
);
299 if (len
> max_tracer_type_len
)
300 max_tracer_type_len
= len
;
303 mutex_unlock(&trace_types_lock
);
308 void unregister_tracer(struct tracer
*type
)
313 mutex_lock(&trace_types_lock
);
314 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
318 pr_info("Trace %s not registered\n", type
->name
);
323 if (strlen(type
->name
) != max_tracer_type_len
)
326 max_tracer_type_len
= 0;
327 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
328 len
= strlen((*t
)->name
);
329 if (len
> max_tracer_type_len
)
330 max_tracer_type_len
= len
;
333 mutex_unlock(&trace_types_lock
);
336 void notrace
tracing_reset(struct trace_array_cpu
*data
)
339 data
->trace_current
= head_page(data
);
340 data
->trace_current_idx
= 0;
345 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
347 struct trace_array
*tr
= &global_trace
;
348 struct trace_array_cpu
*data
;
353 if (unlikely(!tracer_enabled
))
356 local_irq_save(flags
);
357 cpu
= raw_smp_processor_id();
358 data
= tr
->data
[cpu
];
359 disabled
= atomic_inc_return(&data
->disabled
);
361 if (likely(disabled
== 1))
362 ftrace(tr
, data
, ip
, parent_ip
, flags
);
364 atomic_dec(&data
->disabled
);
365 local_irq_restore(flags
);
368 static struct ftrace_ops trace_ops __read_mostly
=
370 .func
= function_trace_call
,
374 notrace
void tracing_start_function_trace(void)
376 register_ftrace_function(&trace_ops
);
379 notrace
void tracing_stop_function_trace(void)
381 unregister_ftrace_function(&trace_ops
);
384 #define SAVED_CMDLINES 128
385 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
386 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
387 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
388 static int cmdline_idx
;
389 static DEFINE_SPINLOCK(trace_cmdline_lock
);
390 atomic_t trace_record_cmdline_disabled
;
392 static void trace_init_cmdlines(void)
394 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
395 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
399 notrace
void trace_stop_cmdline_recording(void);
401 static void notrace
trace_save_cmdline(struct task_struct
*tsk
)
406 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
410 * It's not the end of the world if we don't get
411 * the lock, but we also don't want to spin
412 * nor do we want to disable interrupts,
413 * so if we miss here, then better luck next time.
415 if (!spin_trylock(&trace_cmdline_lock
))
418 idx
= map_pid_to_cmdline
[tsk
->pid
];
419 if (idx
>= SAVED_CMDLINES
) {
420 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
422 map
= map_cmdline_to_pid
[idx
];
423 if (map
<= PID_MAX_DEFAULT
)
424 map_pid_to_cmdline
[map
] = (unsigned)-1;
426 map_pid_to_cmdline
[tsk
->pid
] = idx
;
431 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
433 spin_unlock(&trace_cmdline_lock
);
436 static notrace
char *trace_find_cmdline(int pid
)
438 char *cmdline
= "<...>";
444 if (pid
> PID_MAX_DEFAULT
)
447 map
= map_pid_to_cmdline
[pid
];
448 if (map
>= SAVED_CMDLINES
)
451 cmdline
= saved_cmdlines
[map
];
457 notrace
void tracing_record_cmdline(struct task_struct
*tsk
)
459 if (atomic_read(&trace_record_cmdline_disabled
))
462 trace_save_cmdline(tsk
);
465 static inline notrace
struct trace_entry
*
466 tracing_get_trace_entry(struct trace_array
*tr
, struct trace_array_cpu
*data
)
468 unsigned long idx
, idx_next
;
469 struct trace_entry
*entry
;
470 struct list_head
*next
;
474 idx
= data
->trace_current_idx
;
477 BUG_ON(idx
* TRACE_ENTRY_SIZE
>= PAGE_SIZE
);
479 entry
= data
->trace_current
+ idx
* TRACE_ENTRY_SIZE
;
481 if (unlikely(idx_next
>= ENTRIES_PER_PAGE
)) {
482 page
= virt_to_page(data
->trace_current
);
484 * Roundrobin - but skip the head (which is not a real page):
486 next
= page
->lru
.next
;
487 if (unlikely(next
== &data
->trace_pages
))
489 BUG_ON(next
== &data
->trace_pages
);
491 page
= list_entry(next
, struct page
, lru
);
492 data
->trace_current
= page_address(page
);
496 data
->trace_current_idx
= idx_next
;
501 static inline notrace
void
502 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
)
504 struct task_struct
*tsk
= current
;
507 pc
= preempt_count();
509 entry
->idx
= atomic_inc_return(&tracer_counter
);
510 entry
->preempt_count
= pc
& 0xff;
511 entry
->pid
= tsk
->pid
;
512 entry
->t
= now(raw_smp_processor_id());
513 entry
->flags
= (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
514 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
515 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
516 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
520 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
521 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
523 struct trace_entry
*entry
;
525 entry
= tracing_get_trace_entry(tr
, data
);
526 tracing_generic_entry_update(entry
, flags
);
527 entry
->type
= TRACE_FN
;
529 entry
->fn
.parent_ip
= parent_ip
;
533 tracing_sched_switch_trace(struct trace_array
*tr
,
534 struct trace_array_cpu
*data
,
535 struct task_struct
*prev
, struct task_struct
*next
,
538 struct trace_entry
*entry
;
540 entry
= tracing_get_trace_entry(tr
, data
);
541 tracing_generic_entry_update(entry
, flags
);
542 entry
->type
= TRACE_CTX
;
543 entry
->ctx
.prev_pid
= prev
->pid
;
544 entry
->ctx
.prev_prio
= prev
->prio
;
545 entry
->ctx
.prev_state
= prev
->state
;
546 entry
->ctx
.next_pid
= next
->pid
;
547 entry
->ctx
.next_prio
= next
->prio
;
550 enum trace_file_type
{
551 TRACE_FILE_LAT_FMT
= 1,
554 static struct trace_entry
*
555 trace_entry_idx(struct trace_array
*tr
, struct trace_array_cpu
*data
,
556 struct trace_iterator
*iter
, int cpu
)
559 struct trace_entry
*array
;
561 if (iter
->next_idx
[cpu
] >= tr
->entries
||
562 iter
->next_idx
[cpu
] >= data
->trace_idx
)
565 if (!iter
->next_page
[cpu
]) {
567 * Initialize. If the count of elements in
568 * this buffer is greater than the max entries
569 * we had an underrun. Which means we looped around.
570 * We can simply use the current pointer as our
573 if (data
->trace_idx
>= tr
->entries
) {
574 page
= virt_to_page(data
->trace_current
);
575 iter
->next_page
[cpu
] = &page
->lru
;
576 iter
->next_page_idx
[cpu
] = data
->trace_current_idx
;
578 iter
->next_page
[cpu
] = data
->trace_pages
.next
;
579 iter
->next_page_idx
[cpu
] = 0;
583 page
= list_entry(iter
->next_page
[cpu
], struct page
, lru
);
584 BUG_ON(&data
->trace_pages
== &page
->lru
);
586 array
= page_address(page
);
588 return &array
[iter
->next_page_idx
[cpu
]];
591 static struct notrace trace_entry
*
592 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
)
594 struct trace_array
*tr
= iter
->tr
;
595 struct trace_entry
*ent
, *next
= NULL
;
599 for_each_possible_cpu(cpu
) {
600 if (!head_page(tr
->data
[cpu
]))
602 ent
= trace_entry_idx(tr
, tr
->data
[cpu
], iter
, cpu
);
604 (!next
|| (long)(next
->idx
- ent
->idx
) > 0)) {
616 static void *find_next_entry_inc(struct trace_iterator
*iter
)
618 struct trace_entry
*next
;
621 next
= find_next_entry(iter
, &next_cpu
);
625 iter
->next_idx
[next_cpu
]++;
626 iter
->next_page_idx
[next_cpu
]++;
627 if (iter
->next_page_idx
[next_cpu
] >= ENTRIES_PER_PAGE
) {
628 struct trace_array_cpu
*data
= iter
->tr
->data
[next_cpu
];
630 iter
->next_page_idx
[next_cpu
] = 0;
631 iter
->next_page
[next_cpu
] =
632 iter
->next_page
[next_cpu
]->next
;
633 if (iter
->next_page
[next_cpu
] == &data
->trace_pages
)
634 iter
->next_page
[next_cpu
] =
635 data
->trace_pages
.next
;
639 iter
->cpu
= next_cpu
;
641 return next
? iter
: NULL
;
644 static void notrace
*
645 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
647 struct trace_iterator
*iter
= m
->private;
649 void *last_ent
= iter
->ent
;
654 /* can't go backwards */
659 ent
= find_next_entry_inc(iter
);
663 while (ent
&& iter
->idx
< i
)
664 ent
= find_next_entry_inc(iter
);
668 if (last_ent
&& !ent
)
669 seq_puts(m
, "\n\nvim:ft=help\n");
674 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
676 struct trace_iterator
*iter
= m
->private;
681 mutex_lock(&trace_types_lock
);
683 if (!current_trace
|| current_trace
!= iter
->trace
)
686 atomic_inc(&trace_record_cmdline_disabled
);
688 /* let the tracer grab locks here if needed */
689 if (current_trace
->start
)
690 current_trace
->start(iter
);
692 if (*pos
!= iter
->pos
) {
697 for_each_possible_cpu(i
) {
698 iter
->next_idx
[i
] = 0;
699 iter
->next_page
[i
] = NULL
;
702 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
707 p
= s_next(m
, p
, &l
);
713 static void s_stop(struct seq_file
*m
, void *p
)
715 struct trace_iterator
*iter
= m
->private;
717 atomic_dec(&trace_record_cmdline_disabled
);
719 /* let the tracer release locks here if needed */
720 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
721 iter
->trace
->stop(iter
);
723 mutex_unlock(&trace_types_lock
);
727 seq_print_sym_short(struct seq_file
*m
, const char *fmt
, unsigned long address
)
729 #ifdef CONFIG_KALLSYMS
730 char str
[KSYM_SYMBOL_LEN
];
732 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
734 seq_printf(m
, fmt
, str
);
739 seq_print_sym_offset(struct seq_file
*m
, const char *fmt
, unsigned long address
)
741 #ifdef CONFIG_KALLSYMS
742 char str
[KSYM_SYMBOL_LEN
];
744 sprint_symbol(str
, address
);
745 seq_printf(m
, fmt
, str
);
750 # define IP_FMT "%08lx"
752 # define IP_FMT "%016lx"
756 seq_print_ip_sym(struct seq_file
*m
, unsigned long ip
, unsigned long sym_flags
)
763 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
764 seq_print_sym_offset(m
, "%s", ip
);
766 seq_print_sym_short(m
, "%s", ip
);
768 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
769 seq_printf(m
, " <" IP_FMT
">", ip
);
772 static void notrace
print_lat_help_header(struct seq_file
*m
)
774 seq_puts(m
, "# _------=> CPU# \n");
775 seq_puts(m
, "# / _-----=> irqs-off \n");
776 seq_puts(m
, "# | / _----=> need-resched \n");
777 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
778 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
779 seq_puts(m
, "# |||| / \n");
780 seq_puts(m
, "# ||||| delay \n");
781 seq_puts(m
, "# cmd pid ||||| time | caller \n");
782 seq_puts(m
, "# \\ / ||||| \\ | / \n");
785 static void notrace
print_func_help_header(struct seq_file
*m
)
787 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
788 seq_puts(m
, "# | | | | |\n");
793 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
795 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
796 struct trace_array
*tr
= iter
->tr
;
797 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
798 struct tracer
*type
= current_trace
;
799 unsigned long total
= 0;
800 unsigned long entries
= 0;
802 const char *name
= "preemption";
807 for_each_possible_cpu(cpu
) {
808 if (head_page(tr
->data
[cpu
])) {
809 total
+= tr
->data
[cpu
]->trace_idx
;
810 if (tr
->data
[cpu
]->trace_idx
> tr
->entries
)
811 entries
+= tr
->entries
;
813 entries
+= tr
->data
[cpu
]->trace_idx
;
817 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
819 seq_puts(m
, "-----------------------------------"
820 "---------------------------------\n");
821 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
822 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
823 nsecs_to_usecs(data
->saved_latency
),
827 #if defined(CONFIG_PREEMPT_NONE)
829 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
831 #elif defined(CONFIG_PREEMPT_DESKTOP)
836 /* These are reserved for later use */
839 seq_printf(m
, " #P:%d)\n", num_online_cpus());
843 seq_puts(m
, " -----------------\n");
844 seq_printf(m
, " | task: %.16s-%d "
845 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
846 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
847 data
->policy
, data
->rt_priority
);
848 seq_puts(m
, " -----------------\n");
850 if (data
->critical_start
) {
851 seq_puts(m
, " => started at: ");
852 seq_print_ip_sym(m
, data
->critical_start
, sym_flags
);
853 seq_puts(m
, "\n => ended at: ");
854 seq_print_ip_sym(m
, data
->critical_end
, sym_flags
);
862 lat_print_generic(struct seq_file
*m
, struct trace_entry
*entry
, int cpu
)
864 int hardirq
, softirq
;
867 comm
= trace_find_cmdline(entry
->pid
);
869 seq_printf(m
, "%8.8s-%-5d ", comm
, entry
->pid
);
870 seq_printf(m
, "%d", cpu
);
871 seq_printf(m
, "%c%c",
872 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
873 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
875 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
876 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
877 if (hardirq
&& softirq
)
890 if (entry
->preempt_count
)
891 seq_printf(m
, "%x", entry
->preempt_count
);
896 unsigned long preempt_mark_thresh
= 100;
899 lat_print_timestamp(struct seq_file
*m
, unsigned long long abs_usecs
,
900 unsigned long rel_usecs
)
902 seq_printf(m
, " %4lldus", abs_usecs
);
903 if (rel_usecs
> preempt_mark_thresh
)
905 else if (rel_usecs
> 1)
911 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
914 print_lat_fmt(struct seq_file
*m
, struct trace_iterator
*iter
,
915 unsigned int trace_idx
, int cpu
)
917 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
918 struct trace_entry
*next_entry
= find_next_entry(iter
, NULL
);
919 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
920 struct trace_entry
*entry
= iter
->ent
;
921 unsigned long abs_usecs
;
922 unsigned long rel_usecs
;
928 rel_usecs
= ns2usecs(next_entry
->t
- entry
->t
);
929 abs_usecs
= ns2usecs(entry
->t
- iter
->tr
->time_start
);
932 comm
= trace_find_cmdline(entry
->pid
);
933 seq_printf(m
, "%16s %5d %d %d %08x %08x [%08lx]"
934 " %ld.%03ldms (+%ld.%03ldms): ",
936 entry
->pid
, cpu
, entry
->flags
,
937 entry
->preempt_count
, trace_idx
,
940 abs_usecs
% 1000, rel_usecs
/1000, rel_usecs
% 1000);
942 lat_print_generic(m
, entry
, cpu
);
943 lat_print_timestamp(m
, abs_usecs
, rel_usecs
);
945 switch (entry
->type
) {
947 seq_print_ip_sym(m
, entry
->fn
.ip
, sym_flags
);
949 seq_print_ip_sym(m
, entry
->fn
.parent_ip
, sym_flags
);
953 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
954 state_to_char
[entry
->ctx
.prev_state
] : 'X';
955 comm
= trace_find_cmdline(entry
->ctx
.next_pid
);
956 seq_printf(m
, " %d:%d:%c --> %d:%d %s\n",
958 entry
->ctx
.prev_prio
,
961 entry
->ctx
.next_prio
,
965 seq_printf(m
, "Unknown type %d\n", entry
->type
);
970 print_trace_fmt(struct seq_file
*m
, struct trace_iterator
*iter
)
972 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
973 struct trace_entry
*entry
= iter
->ent
;
974 unsigned long usec_rem
;
975 unsigned long long t
;
980 comm
= trace_find_cmdline(iter
->ent
->pid
);
982 t
= ns2usecs(entry
->t
);
983 usec_rem
= do_div(t
, 1000000ULL);
984 secs
= (unsigned long)t
;
986 seq_printf(m
, "%16s-%-5d ", comm
, entry
->pid
);
987 seq_printf(m
, "[%02d] ", iter
->cpu
);
988 seq_printf(m
, "%5lu.%06lu: ", secs
, usec_rem
);
990 switch (entry
->type
) {
992 seq_print_ip_sym(m
, entry
->fn
.ip
, sym_flags
);
993 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
994 entry
->fn
.parent_ip
) {
995 seq_printf(m
, " <-");
996 seq_print_ip_sym(m
, entry
->fn
.parent_ip
, sym_flags
);
1001 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1002 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1003 seq_printf(m
, " %d:%d:%c ==> %d:%d\n",
1004 entry
->ctx
.prev_pid
,
1005 entry
->ctx
.prev_prio
,
1007 entry
->ctx
.next_pid
,
1008 entry
->ctx
.next_prio
);
1013 static int trace_empty(struct trace_iterator
*iter
)
1015 struct trace_array_cpu
*data
;
1018 for_each_possible_cpu(cpu
) {
1019 data
= iter
->tr
->data
[cpu
];
1021 if (head_page(data
) && data
->trace_idx
)
1027 static int s_show(struct seq_file
*m
, void *v
)
1029 struct trace_iterator
*iter
= v
;
1031 if (iter
->ent
== NULL
) {
1033 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1036 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1037 /* print nothing if the buffers are empty */
1038 if (trace_empty(iter
))
1040 print_trace_header(m
, iter
);
1041 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1042 print_lat_help_header(m
);
1044 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1045 print_func_help_header(m
);
1048 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1049 print_lat_fmt(m
, iter
, iter
->idx
, iter
->cpu
);
1051 print_trace_fmt(m
, iter
);
1057 static struct seq_operations tracer_seq_ops
= {
1064 static struct trace_iterator notrace
*
1065 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1067 struct trace_iterator
*iter
;
1069 if (tracing_disabled
) {
1074 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1080 mutex_lock(&trace_types_lock
);
1081 if (current_trace
&& current_trace
->print_max
)
1084 iter
->tr
= inode
->i_private
;
1085 iter
->trace
= current_trace
;
1088 /* TODO stop tracer */
1089 *ret
= seq_open(file
, &tracer_seq_ops
);
1091 struct seq_file
*m
= file
->private_data
;
1094 /* stop the trace while dumping */
1098 if (iter
->trace
&& iter
->trace
->open
)
1099 iter
->trace
->open(iter
);
1104 mutex_unlock(&trace_types_lock
);
1110 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1112 if (tracing_disabled
)
1115 filp
->private_data
= inode
->i_private
;
1119 int tracing_release(struct inode
*inode
, struct file
*file
)
1121 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1122 struct trace_iterator
*iter
= m
->private;
1124 mutex_lock(&trace_types_lock
);
1125 if (iter
->trace
&& iter
->trace
->close
)
1126 iter
->trace
->close(iter
);
1128 /* reenable tracing if it was previously enabled */
1131 mutex_unlock(&trace_types_lock
);
1133 seq_release(inode
, file
);
1138 static int tracing_open(struct inode
*inode
, struct file
*file
)
1142 __tracing_open(inode
, file
, &ret
);
1147 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1149 struct trace_iterator
*iter
;
1152 iter
= __tracing_open(inode
, file
, &ret
);
1155 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1161 static void notrace
*
1162 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1164 struct tracer
*t
= m
->private;
1176 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1178 struct tracer
*t
= m
->private;
1181 mutex_lock(&trace_types_lock
);
1182 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1188 static void t_stop(struct seq_file
*m
, void *p
)
1190 mutex_unlock(&trace_types_lock
);
1193 static int t_show(struct seq_file
*m
, void *v
)
1195 struct tracer
*t
= v
;
1200 seq_printf(m
, "%s", t
->name
);
1209 static struct seq_operations show_traces_seq_ops
= {
1216 static int show_traces_open(struct inode
*inode
, struct file
*file
)
1220 if (tracing_disabled
)
1223 ret
= seq_open(file
, &show_traces_seq_ops
);
1225 struct seq_file
*m
= file
->private_data
;
1226 m
->private = trace_types
;
1232 static struct file_operations tracing_fops
= {
1233 .open
= tracing_open
,
1235 .llseek
= seq_lseek
,
1236 .release
= tracing_release
,
1239 static struct file_operations tracing_lt_fops
= {
1240 .open
= tracing_lt_open
,
1242 .llseek
= seq_lseek
,
1243 .release
= tracing_release
,
1246 static struct file_operations show_traces_fops
= {
1247 .open
= show_traces_open
,
1249 .release
= seq_release
,
1253 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1254 size_t cnt
, loff_t
*ppos
)
1261 /* calulate max size */
1262 for (i
= 0; trace_options
[i
]; i
++) {
1263 len
+= strlen(trace_options
[i
]);
1264 len
+= 3; /* "no" and space */
1267 /* +2 for \n and \0 */
1268 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
1272 for (i
= 0; trace_options
[i
]; i
++) {
1273 if (trace_flags
& (1 << i
))
1274 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
1276 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
1279 r
+= sprintf(buf
+ r
, "\n");
1280 WARN_ON(r
>= len
+ 2);
1282 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1291 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1292 size_t cnt
, loff_t
*ppos
)
1302 if (copy_from_user(&buf
, ubuf
, cnt
))
1307 if (strncmp(buf
, "no", 2) == 0) {
1312 for (i
= 0; trace_options
[i
]; i
++) {
1313 int len
= strlen(trace_options
[i
]);
1315 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
1317 trace_flags
&= ~(1 << i
);
1319 trace_flags
|= (1 << i
);
1329 static struct file_operations tracing_iter_fops
= {
1330 .open
= tracing_open_generic
,
1331 .read
= tracing_iter_ctrl_read
,
1332 .write
= tracing_iter_ctrl_write
,
1335 static const char readme_msg
[] =
1336 "tracing mini-HOWTO:\n\n"
1338 "# mount -t debugfs nodev /debug\n\n"
1339 "# cat /debug/tracing/available_tracers\n"
1340 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1341 "# cat /debug/tracing/current_tracer\n"
1343 "# echo sched_switch > /debug/tracing/current_tracer\n"
1344 "# cat /debug/tracing/current_tracer\n"
1346 "# cat /debug/tracing/iter_ctrl\n"
1347 "noprint-parent nosym-offset nosym-addr noverbose\n"
1348 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1349 "# echo 1 > /debug/tracing/tracing_enabled\n"
1350 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1351 "echo 0 > /debug/tracing/tracing_enabled\n"
1355 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
1356 size_t cnt
, loff_t
*ppos
)
1358 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1359 readme_msg
, strlen(readme_msg
));
1362 static struct file_operations tracing_readme_fops
= {
1363 .open
= tracing_open_generic
,
1364 .read
= tracing_readme_read
,
1369 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1370 size_t cnt
, loff_t
*ppos
)
1372 struct trace_array
*tr
= filp
->private_data
;
1376 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
1377 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1382 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1383 size_t cnt
, loff_t
*ppos
)
1385 struct trace_array
*tr
= filp
->private_data
;
1392 if (copy_from_user(&buf
, ubuf
, cnt
))
1397 val
= simple_strtoul(buf
, NULL
, 10);
1401 mutex_lock(&trace_types_lock
);
1402 if (tr
->ctrl
^ val
) {
1410 if (current_trace
&& current_trace
->ctrl_update
)
1411 current_trace
->ctrl_update(tr
);
1413 mutex_unlock(&trace_types_lock
);
1421 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
1422 size_t cnt
, loff_t
*ppos
)
1424 char buf
[max_tracer_type_len
+2];
1427 mutex_lock(&trace_types_lock
);
1429 r
= sprintf(buf
, "%s\n", current_trace
->name
);
1431 r
= sprintf(buf
, "\n");
1432 mutex_unlock(&trace_types_lock
);
1434 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1439 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
1440 size_t cnt
, loff_t
*ppos
)
1442 struct trace_array
*tr
= &global_trace
;
1444 char buf
[max_tracer_type_len
+1];
1447 if (cnt
> max_tracer_type_len
)
1448 cnt
= max_tracer_type_len
;
1450 if (copy_from_user(&buf
, ubuf
, cnt
))
1455 /* strip ending whitespace. */
1456 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
1459 mutex_lock(&trace_types_lock
);
1460 for (t
= trace_types
; t
; t
= t
->next
) {
1461 if (strcmp(t
->name
, buf
) == 0)
1464 if (!t
|| t
== current_trace
)
1467 if (current_trace
&& current_trace
->reset
)
1468 current_trace
->reset(tr
);
1475 mutex_unlock(&trace_types_lock
);
1483 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
1484 size_t cnt
, loff_t
*ppos
)
1486 unsigned long *ptr
= filp
->private_data
;
1490 r
= snprintf(buf
, 64, "%ld\n",
1491 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
1494 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1499 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
1500 size_t cnt
, loff_t
*ppos
)
1502 long *ptr
= filp
->private_data
;
1509 if (copy_from_user(&buf
, ubuf
, cnt
))
1514 val
= simple_strtoul(buf
, NULL
, 10);
1521 static struct file_operations tracing_max_lat_fops
= {
1522 .open
= tracing_open_generic
,
1523 .read
= tracing_max_lat_read
,
1524 .write
= tracing_max_lat_write
,
1527 static struct file_operations tracing_ctrl_fops
= {
1528 .open
= tracing_open_generic
,
1529 .read
= tracing_ctrl_read
,
1530 .write
= tracing_ctrl_write
,
1533 static struct file_operations set_tracer_fops
= {
1534 .open
= tracing_open_generic
,
1535 .read
= tracing_set_trace_read
,
1536 .write
= tracing_set_trace_write
,
1539 #ifdef CONFIG_DYNAMIC_FTRACE
1542 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
1543 size_t cnt
, loff_t
*ppos
)
1545 unsigned long *p
= filp
->private_data
;
1549 r
= sprintf(buf
, "%ld\n", *p
);
1550 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1554 static struct file_operations tracing_read_long_fops
= {
1555 .open
= tracing_open_generic
,
1556 .read
= tracing_read_long
,
1560 static struct dentry
*d_tracer
;
1562 struct dentry
*tracing_init_dentry(void)
1569 d_tracer
= debugfs_create_dir("tracing", NULL
);
1571 if (!d_tracer
&& !once
) {
1573 pr_warning("Could not create debugfs directory 'tracing'\n");
1580 #ifdef CONFIG_FTRACE_SELFTEST
1581 /* Let selftest have access to static functions in this file */
1582 #include "trace_selftest.c"
1585 static __init
void tracer_init_debugfs(void)
1587 struct dentry
*d_tracer
;
1588 struct dentry
*entry
;
1590 d_tracer
= tracing_init_dentry();
1592 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
1593 &global_trace
, &tracing_ctrl_fops
);
1595 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1597 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
1598 NULL
, &tracing_iter_fops
);
1600 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1602 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
1603 &global_trace
, &tracing_lt_fops
);
1605 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1607 entry
= debugfs_create_file("trace", 0444, d_tracer
,
1608 &global_trace
, &tracing_fops
);
1610 pr_warning("Could not create debugfs 'trace' entry\n");
1612 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
1613 &global_trace
, &show_traces_fops
);
1615 pr_warning("Could not create debugfs 'trace' entry\n");
1617 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
1618 &global_trace
, &set_tracer_fops
);
1620 pr_warning("Could not create debugfs 'trace' entry\n");
1622 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
1623 &tracing_max_latency
,
1624 &tracing_max_lat_fops
);
1626 pr_warning("Could not create debugfs "
1627 "'tracing_max_latency' entry\n");
1629 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
1630 &tracing_thresh
, &tracing_max_lat_fops
);
1632 pr_warning("Could not create debugfs "
1633 "'tracing_threash' entry\n");
1634 entry
= debugfs_create_file("README", 0644, d_tracer
,
1635 NULL
, &tracing_readme_fops
);
1637 pr_warning("Could not create debugfs 'README' entry\n");
1640 #ifdef CONFIG_DYNAMIC_FTRACE
1641 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
1642 &ftrace_update_tot_cnt
,
1643 &tracing_read_long_fops
);
1645 pr_warning("Could not create debugfs "
1646 "'dyn_ftrace_total_info' entry\n");
1650 /* dummy trace to disable tracing */
1651 static struct tracer no_tracer __read_mostly
=
1656 static int trace_alloc_page(void)
1658 struct trace_array_cpu
*data
;
1659 struct page
*page
, *tmp
;
1664 /* first allocate a page for each CPU */
1665 for_each_possible_cpu(i
) {
1666 array
= (void *)__get_free_page(GFP_KERNEL
);
1667 if (array
== NULL
) {
1668 printk(KERN_ERR
"tracer: failed to allocate page"
1669 "for trace buffer!\n");
1673 page
= virt_to_page(array
);
1674 list_add(&page
->lru
, &pages
);
1676 /* Only allocate if we are actually using the max trace */
1677 #ifdef CONFIG_TRACER_MAX_TRACE
1678 array
= (void *)__get_free_page(GFP_KERNEL
);
1679 if (array
== NULL
) {
1680 printk(KERN_ERR
"tracer: failed to allocate page"
1681 "for trace buffer!\n");
1684 page
= virt_to_page(array
);
1685 list_add(&page
->lru
, &pages
);
1689 /* Now that we successfully allocate a page per CPU, add them */
1690 for_each_possible_cpu(i
) {
1691 data
= global_trace
.data
[i
];
1692 page
= list_entry(pages
.next
, struct page
, lru
);
1693 list_del_init(&page
->lru
);
1694 list_add_tail(&page
->lru
, &data
->trace_pages
);
1697 #ifdef CONFIG_TRACER_MAX_TRACE
1698 data
= max_tr
.data
[i
];
1699 page
= list_entry(pages
.next
, struct page
, lru
);
1700 list_del_init(&page
->lru
);
1701 list_add_tail(&page
->lru
, &data
->trace_pages
);
1705 global_trace
.entries
+= ENTRIES_PER_PAGE
;
1710 list_for_each_entry_safe(page
, tmp
, &pages
, lru
) {
1711 list_del_init(&page
->lru
);
1717 __init
static int tracer_alloc_buffers(void)
1719 struct trace_array_cpu
*data
;
1726 /* Allocate the first page for all buffers */
1727 for_each_possible_cpu(i
) {
1728 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
1729 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
1731 array
= (void *)__get_free_page(GFP_KERNEL
);
1732 if (array
== NULL
) {
1733 printk(KERN_ERR
"tracer: failed to allocate page"
1734 "for trace buffer!\n");
1738 /* set the array to the list */
1739 INIT_LIST_HEAD(&data
->trace_pages
);
1740 page
= virt_to_page(array
);
1741 list_add(&page
->lru
, &data
->trace_pages
);
1742 /* use the LRU flag to differentiate the two buffers */
1745 /* Only allocate if we are actually using the max trace */
1746 #ifdef CONFIG_TRACER_MAX_TRACE
1747 array
= (void *)__get_free_page(GFP_KERNEL
);
1748 if (array
== NULL
) {
1749 printk(KERN_ERR
"tracer: failed to allocate page"
1750 "for trace buffer!\n");
1754 INIT_LIST_HEAD(&max_tr
.data
[i
]->trace_pages
);
1755 page
= virt_to_page(array
);
1756 list_add(&page
->lru
, &max_tr
.data
[i
]->trace_pages
);
1762 * Since we allocate by orders of pages, we may be able to
1765 global_trace
.entries
= ENTRIES_PER_PAGE
;
1768 while (global_trace
.entries
< trace_nr_entries
) {
1769 if (trace_alloc_page())
1773 max_tr
.entries
= global_trace
.entries
;
1775 pr_info("tracer: %d pages allocated for %ld",
1776 pages
, trace_nr_entries
);
1777 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE
);
1778 pr_info(" actual entries %ld\n", global_trace
.entries
);
1780 tracer_init_debugfs();
1782 trace_init_cmdlines();
1784 register_tracer(&no_tracer
);
1785 current_trace
= &no_tracer
;
1787 /* All seems OK, enable tracing */
1788 tracing_disabled
= 0;
1793 for (i
-- ; i
>= 0; i
--) {
1794 struct page
*page
, *tmp
;
1795 struct trace_array_cpu
*data
= global_trace
.data
[i
];
1798 list_for_each_entry_safe(page
, tmp
,
1799 &data
->trace_pages
, lru
) {
1800 list_del_init(&page
->lru
);
1805 #ifdef CONFIG_TRACER_MAX_TRACE
1806 data
= max_tr
.data
[i
];
1808 list_for_each_entry_safe(page
, tmp
,
1809 &data
->trace_pages
, lru
) {
1810 list_del_init(&page
->lru
);
1819 fs_initcall(tracer_alloc_buffers
);