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
;
36 ns2usecs(cycle_t nsec
)
43 static atomic_t tracer_counter
;
44 static struct trace_array global_trace
;
46 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
48 static struct trace_array max_tr
;
50 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
52 static int tracer_enabled
;
53 static unsigned long trace_nr_entries
= 16384UL;
55 static struct tracer
*trace_types __read_mostly
;
56 static struct tracer
*current_trace __read_mostly
;
57 static int max_tracer_type_len
;
59 static DEFINE_MUTEX(trace_types_lock
);
61 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
63 static int __init
set_nr_entries(char *str
)
67 trace_nr_entries
= simple_strtoul(str
, &str
, 0);
70 __setup("trace_entries=", set_nr_entries
);
72 unsigned long nsecs_to_usecs(unsigned long nsecs
)
78 __TRACE_FIRST_TYPE
= 0,
86 enum trace_flag_type
{
87 TRACE_FLAG_IRQS_OFF
= 0x01,
88 TRACE_FLAG_NEED_RESCHED
= 0x02,
89 TRACE_FLAG_HARDIRQ
= 0x04,
90 TRACE_FLAG_SOFTIRQ
= 0x08,
93 enum trace_iterator_flags
{
94 TRACE_ITER_PRINT_PARENT
= 0x01,
95 TRACE_ITER_SYM_OFFSET
= 0x02,
96 TRACE_ITER_SYM_ADDR
= 0x04,
97 TRACE_ITER_VERBOSE
= 0x08,
100 #define TRACE_ITER_SYM_MASK \
101 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
103 /* These must match the bit postions above */
104 static const char *trace_options
[] = {
112 static unsigned trace_flags
;
114 static DEFINE_SPINLOCK(ftrace_max_lock
);
117 * Copy the new maximum trace into the separate maximum-trace
118 * structure. (this way the maximum trace is permanently saved,
119 * for later retrieval via /debugfs/tracing/latency_trace)
122 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
124 struct trace_array_cpu
*data
= tr
->data
[cpu
];
127 max_tr
.time_start
= data
->preempt_timestamp
;
129 data
= max_tr
.data
[cpu
];
130 data
->saved_latency
= tracing_max_latency
;
132 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
133 data
->pid
= tsk
->pid
;
134 data
->uid
= tsk
->uid
;
135 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
136 data
->policy
= tsk
->policy
;
137 data
->rt_priority
= tsk
->rt_priority
;
139 /* record this tasks comm */
140 tracing_record_cmdline(current
);
144 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
146 struct trace_array_cpu
*data
;
148 struct list_head save_pages
;
151 WARN_ON_ONCE(!irqs_disabled());
152 spin_lock(&ftrace_max_lock
);
153 /* clear out all the previous traces */
154 for_each_possible_cpu(i
) {
156 save_trace
= max_tr
.data
[i
]->trace
;
157 save_pages
= max_tr
.data
[i
]->trace_pages
;
158 memcpy(max_tr
.data
[i
], data
, sizeof(*data
));
159 data
->trace
= save_trace
;
160 data
->trace_pages
= save_pages
;
164 __update_max_tr(tr
, tsk
, cpu
);
165 spin_unlock(&ftrace_max_lock
);
169 * update_max_tr_single - only copy one trace over, and reset the rest
171 * @tsk - task with the latency
172 * @cpu - the cpu of the buffer to copy.
175 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
177 struct trace_array_cpu
*data
= tr
->data
[cpu
];
179 struct list_head save_pages
;
182 WARN_ON_ONCE(!irqs_disabled());
183 spin_lock(&ftrace_max_lock
);
184 for_each_possible_cpu(i
)
185 tracing_reset(max_tr
.data
[i
]);
187 save_trace
= max_tr
.data
[cpu
]->trace
;
188 save_pages
= max_tr
.data
[cpu
]->trace_pages
;
189 memcpy(max_tr
.data
[cpu
], data
, sizeof(*data
));
190 data
->trace
= save_trace
;
191 data
->trace_pages
= save_pages
;
194 __update_max_tr(tr
, tsk
, cpu
);
195 spin_unlock(&ftrace_max_lock
);
198 int register_tracer(struct tracer
*type
)
205 pr_info("Tracer must have a name\n");
209 mutex_lock(&trace_types_lock
);
210 for (t
= trace_types
; t
; t
= t
->next
) {
211 if (strcmp(type
->name
, t
->name
) == 0) {
213 pr_info("Trace %s already registered\n",
220 type
->next
= trace_types
;
222 len
= strlen(type
->name
);
223 if (len
> max_tracer_type_len
)
224 max_tracer_type_len
= len
;
226 mutex_unlock(&trace_types_lock
);
231 void unregister_tracer(struct tracer
*type
)
236 mutex_lock(&trace_types_lock
);
237 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
241 pr_info("Trace %s not registered\n", type
->name
);
246 if (strlen(type
->name
) != max_tracer_type_len
)
249 max_tracer_type_len
= 0;
250 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
251 len
= strlen((*t
)->name
);
252 if (len
> max_tracer_type_len
)
253 max_tracer_type_len
= len
;
256 mutex_unlock(&trace_types_lock
);
259 void notrace
tracing_reset(struct trace_array_cpu
*data
)
262 data
->trace_current
= data
->trace
;
263 data
->trace_current_idx
= 0;
268 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
270 struct trace_array
*tr
= &global_trace
;
271 struct trace_array_cpu
*data
;
276 if (unlikely(!tracer_enabled
))
279 local_irq_save(flags
);
280 cpu
= raw_smp_processor_id();
281 data
= tr
->data
[cpu
];
282 disabled
= atomic_inc_return(&data
->disabled
);
284 if (likely(disabled
== 1))
285 ftrace(tr
, data
, ip
, parent_ip
, flags
);
287 atomic_dec(&data
->disabled
);
288 local_irq_restore(flags
);
291 static struct ftrace_ops trace_ops __read_mostly
=
293 .func
= function_trace_call
,
297 notrace
void tracing_start_function_trace(void)
299 register_ftrace_function(&trace_ops
);
302 notrace
void tracing_stop_function_trace(void)
304 unregister_ftrace_function(&trace_ops
);
307 #define SAVED_CMDLINES 128
308 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
309 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
310 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
311 static int cmdline_idx
;
312 static DEFINE_SPINLOCK(trace_cmdline_lock
);
313 atomic_t trace_record_cmdline_disabled
;
315 static void trace_init_cmdlines(void)
317 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
318 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
322 notrace
void trace_stop_cmdline_recording(void);
324 static void notrace
trace_save_cmdline(struct task_struct
*tsk
)
329 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
333 * It's not the end of the world if we don't get
334 * the lock, but we also don't want to spin
335 * nor do we want to disable interrupts,
336 * so if we miss here, then better luck next time.
338 if (!spin_trylock(&trace_cmdline_lock
))
341 idx
= map_pid_to_cmdline
[tsk
->pid
];
342 if (idx
>= SAVED_CMDLINES
) {
343 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
345 map
= map_cmdline_to_pid
[idx
];
346 if (map
<= PID_MAX_DEFAULT
)
347 map_pid_to_cmdline
[map
] = (unsigned)-1;
349 map_pid_to_cmdline
[tsk
->pid
] = idx
;
354 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
356 spin_unlock(&trace_cmdline_lock
);
359 static notrace
char *trace_find_cmdline(int pid
)
361 char *cmdline
= "<...>";
367 if (pid
> PID_MAX_DEFAULT
)
370 map
= map_pid_to_cmdline
[pid
];
371 if (map
>= SAVED_CMDLINES
)
374 cmdline
= saved_cmdlines
[map
];
380 notrace
void tracing_record_cmdline(struct task_struct
*tsk
)
382 if (atomic_read(&trace_record_cmdline_disabled
))
385 trace_save_cmdline(tsk
);
388 static inline notrace
struct trace_entry
*
389 tracing_get_trace_entry(struct trace_array
*tr
,
390 struct trace_array_cpu
*data
)
392 unsigned long idx
, idx_next
;
393 struct trace_entry
*entry
;
395 struct list_head
*next
;
398 idx
= data
->trace_current_idx
;
401 entry
= data
->trace_current
+ idx
* TRACE_ENTRY_SIZE
;
403 if (unlikely(idx_next
>= ENTRIES_PER_PAGE
)) {
404 page
= virt_to_page(data
->trace_current
);
405 if (unlikely(&page
->lru
== data
->trace_pages
.prev
))
406 next
= data
->trace_pages
.next
;
408 next
= page
->lru
.next
;
409 page
= list_entry(next
, struct page
, lru
);
410 data
->trace_current
= page_address(page
);
414 data
->trace_current_idx
= idx_next
;
419 static inline notrace
void
420 tracing_generic_entry_update(struct trace_entry
*entry
,
423 struct task_struct
*tsk
= current
;
426 pc
= preempt_count();
428 entry
->idx
= atomic_inc_return(&tracer_counter
);
429 entry
->preempt_count
= pc
& 0xff;
430 entry
->pid
= tsk
->pid
;
431 entry
->t
= now(raw_smp_processor_id());
432 entry
->flags
= (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
433 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
434 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
435 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
439 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
440 unsigned long ip
, unsigned long parent_ip
,
443 struct trace_entry
*entry
;
445 entry
= tracing_get_trace_entry(tr
, data
);
446 tracing_generic_entry_update(entry
, flags
);
447 entry
->type
= TRACE_FN
;
449 entry
->fn
.parent_ip
= parent_ip
;
453 tracing_sched_switch_trace(struct trace_array
*tr
,
454 struct trace_array_cpu
*data
,
455 struct task_struct
*prev
, struct task_struct
*next
,
458 struct trace_entry
*entry
;
460 entry
= tracing_get_trace_entry(tr
, data
);
461 tracing_generic_entry_update(entry
, flags
);
462 entry
->type
= TRACE_CTX
;
463 entry
->ctx
.prev_pid
= prev
->pid
;
464 entry
->ctx
.prev_prio
= prev
->prio
;
465 entry
->ctx
.prev_state
= prev
->state
;
466 entry
->ctx
.next_pid
= next
->pid
;
467 entry
->ctx
.next_prio
= next
->prio
;
470 enum trace_file_type
{
471 TRACE_FILE_LAT_FMT
= 1,
474 static struct trace_entry
*
475 trace_entry_idx(struct trace_array
*tr
, struct trace_array_cpu
*data
,
476 struct trace_iterator
*iter
, int cpu
)
479 struct trace_entry
*array
;
481 if (iter
->next_idx
[cpu
] >= tr
->entries
||
482 iter
->next_idx
[cpu
] >= data
->trace_idx
)
485 if (!iter
->next_page
[cpu
]) {
487 * Initialize. If the count of elements in
488 * this buffer is greater than the max entries
489 * we had an underrun. Which means we looped around.
490 * We can simply use the current pointer as our
493 if (data
->trace_idx
>= tr
->entries
) {
494 page
= virt_to_page(data
->trace_current
);
495 iter
->next_page
[cpu
] = &page
->lru
;
496 iter
->next_page_idx
[cpu
] = data
->trace_current_idx
;
498 iter
->next_page
[cpu
] = data
->trace_pages
.next
;
499 iter
->next_page_idx
[cpu
] = 0;
503 page
= list_entry(iter
->next_page
[cpu
], struct page
, lru
);
504 array
= page_address(page
);
506 return &array
[iter
->next_page_idx
[cpu
]];
509 static struct notrace trace_entry
*
510 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
)
512 struct trace_array
*tr
= iter
->tr
;
513 struct trace_entry
*ent
, *next
= NULL
;
517 for_each_possible_cpu(cpu
) {
518 if (!tr
->data
[cpu
]->trace
)
520 ent
= trace_entry_idx(tr
, tr
->data
[cpu
], iter
, cpu
);
522 (!next
|| (long)(next
->idx
- ent
->idx
) > 0)) {
534 static void *find_next_entry_inc(struct trace_iterator
*iter
)
536 struct trace_entry
*next
;
539 next
= find_next_entry(iter
, &next_cpu
);
543 iter
->next_idx
[next_cpu
]++;
544 iter
->next_page_idx
[next_cpu
]++;
545 if (iter
->next_page_idx
[next_cpu
] >= ENTRIES_PER_PAGE
) {
546 struct trace_array_cpu
*data
= iter
->tr
->data
[next_cpu
];
548 iter
->next_page_idx
[next_cpu
] = 0;
549 iter
->next_page
[next_cpu
] =
550 iter
->next_page
[next_cpu
]->next
;
551 if (iter
->next_page
[next_cpu
] == &data
->trace_pages
)
552 iter
->next_page
[next_cpu
] =
553 data
->trace_pages
.next
;
557 iter
->cpu
= next_cpu
;
559 return next
? iter
: NULL
;
562 static void notrace
*
563 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
565 struct trace_iterator
*iter
= m
->private;
567 void *last_ent
= iter
->ent
;
572 /* can't go backwards */
577 ent
= find_next_entry_inc(iter
);
581 while (ent
&& iter
->idx
< i
)
582 ent
= find_next_entry_inc(iter
);
586 if (last_ent
&& !ent
)
587 seq_puts(m
, "\n\nvim:ft=help\n");
592 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
594 struct trace_iterator
*iter
= m
->private;
599 mutex_lock(&trace_types_lock
);
601 if (!current_trace
|| current_trace
!= iter
->trace
)
604 atomic_inc(&trace_record_cmdline_disabled
);
606 /* let the tracer grab locks here if needed */
607 if (current_trace
->start
)
608 current_trace
->start(iter
);
610 if (*pos
!= iter
->pos
) {
615 for_each_possible_cpu(i
) {
616 iter
->next_idx
[i
] = 0;
617 iter
->next_page
[i
] = NULL
;
620 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
625 p
= s_next(m
, p
, &l
);
631 static void s_stop(struct seq_file
*m
, void *p
)
633 struct trace_iterator
*iter
= m
->private;
635 atomic_dec(&trace_record_cmdline_disabled
);
637 /* let the tracer release locks here if needed */
638 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
639 iter
->trace
->stop(iter
);
641 mutex_unlock(&trace_types_lock
);
645 seq_print_sym_short(struct seq_file
*m
, const char *fmt
, unsigned long address
)
647 #ifdef CONFIG_KALLSYMS
648 char str
[KSYM_SYMBOL_LEN
];
650 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
652 seq_printf(m
, fmt
, str
);
657 seq_print_sym_offset(struct seq_file
*m
, const char *fmt
, unsigned long address
)
659 #ifdef CONFIG_KALLSYMS
660 char str
[KSYM_SYMBOL_LEN
];
662 sprint_symbol(str
, address
);
663 seq_printf(m
, fmt
, str
);
668 # define IP_FMT "%08lx"
670 # define IP_FMT "%016lx"
674 seq_print_ip_sym(struct seq_file
*m
, unsigned long ip
, unsigned long sym_flags
)
681 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
682 seq_print_sym_offset(m
, "%s", ip
);
684 seq_print_sym_short(m
, "%s", ip
);
686 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
687 seq_printf(m
, " <" IP_FMT
">", ip
);
690 static void notrace
print_lat_help_header(struct seq_file
*m
)
692 seq_puts(m
, "# _------=> CPU# \n");
693 seq_puts(m
, "# / _-----=> irqs-off \n");
694 seq_puts(m
, "# | / _----=> need-resched \n");
695 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
696 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
697 seq_puts(m
, "# |||| / \n");
698 seq_puts(m
, "# ||||| delay \n");
699 seq_puts(m
, "# cmd pid ||||| time | caller \n");
700 seq_puts(m
, "# \\ / ||||| \\ | / \n");
703 static void notrace
print_func_help_header(struct seq_file
*m
)
705 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
706 seq_puts(m
, "# | | | | |\n");
711 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
713 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
714 struct trace_array
*tr
= iter
->tr
;
715 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
716 struct tracer
*type
= current_trace
;
717 unsigned long total
= 0;
718 unsigned long entries
= 0;
720 const char *name
= "preemption";
725 for_each_possible_cpu(cpu
) {
726 if (tr
->data
[cpu
]->trace
) {
727 total
+= tr
->data
[cpu
]->trace_idx
;
728 if (tr
->data
[cpu
]->trace_idx
> tr
->entries
)
729 entries
+= tr
->entries
;
731 entries
+= tr
->data
[cpu
]->trace_idx
;
735 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
737 seq_puts(m
, "-----------------------------------"
738 "---------------------------------\n");
739 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
740 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
741 nsecs_to_usecs(data
->saved_latency
),
745 #if defined(CONFIG_PREEMPT_NONE)
747 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
749 #elif defined(CONFIG_PREEMPT_DESKTOP)
754 /* These are reserved for later use */
757 seq_printf(m
, " #P:%d)\n", num_online_cpus());
761 seq_puts(m
, " -----------------\n");
762 seq_printf(m
, " | task: %.16s-%d "
763 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
764 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
765 data
->policy
, data
->rt_priority
);
766 seq_puts(m
, " -----------------\n");
768 if (data
->critical_start
) {
769 seq_puts(m
, " => started at: ");
770 seq_print_ip_sym(m
, data
->critical_start
, sym_flags
);
771 seq_puts(m
, "\n => ended at: ");
772 seq_print_ip_sym(m
, data
->critical_end
, sym_flags
);
780 lat_print_generic(struct seq_file
*m
, struct trace_entry
*entry
, int cpu
)
782 int hardirq
, softirq
;
785 comm
= trace_find_cmdline(entry
->pid
);
787 seq_printf(m
, "%8.8s-%-5d ", comm
, entry
->pid
);
788 seq_printf(m
, "%d", cpu
);
789 seq_printf(m
, "%c%c",
790 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
791 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
793 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
794 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
795 if (hardirq
&& softirq
)
808 if (entry
->preempt_count
)
809 seq_printf(m
, "%x", entry
->preempt_count
);
814 unsigned long preempt_mark_thresh
= 100;
817 lat_print_timestamp(struct seq_file
*m
, unsigned long long abs_usecs
,
818 unsigned long rel_usecs
)
820 seq_printf(m
, " %4lldus", abs_usecs
);
821 if (rel_usecs
> preempt_mark_thresh
)
823 else if (rel_usecs
> 1)
829 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
832 print_lat_fmt(struct seq_file
*m
, struct trace_iterator
*iter
,
833 unsigned int trace_idx
, int cpu
)
835 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
836 struct trace_entry
*next_entry
= find_next_entry(iter
, NULL
);
837 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
838 struct trace_entry
*entry
= iter
->ent
;
839 unsigned long abs_usecs
;
840 unsigned long rel_usecs
;
846 rel_usecs
= ns2usecs(next_entry
->t
- entry
->t
);
847 abs_usecs
= ns2usecs(entry
->t
- iter
->tr
->time_start
);
850 comm
= trace_find_cmdline(entry
->pid
);
851 seq_printf(m
, "%16s %5d %d %d %08x %08x [%08lx]"
852 " %ld.%03ldms (+%ld.%03ldms): ",
854 entry
->pid
, cpu
, entry
->flags
,
855 entry
->preempt_count
, trace_idx
,
858 abs_usecs
% 1000, rel_usecs
/1000, rel_usecs
% 1000);
860 lat_print_generic(m
, entry
, cpu
);
861 lat_print_timestamp(m
, abs_usecs
, rel_usecs
);
863 switch (entry
->type
) {
865 seq_print_ip_sym(m
, entry
->fn
.ip
, sym_flags
);
867 seq_print_ip_sym(m
, entry
->fn
.parent_ip
, sym_flags
);
871 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
872 state_to_char
[entry
->ctx
.prev_state
] : 'X';
873 comm
= trace_find_cmdline(entry
->ctx
.next_pid
);
874 seq_printf(m
, " %d:%d:%c --> %d:%d %s\n",
876 entry
->ctx
.prev_prio
,
879 entry
->ctx
.next_prio
,
883 seq_printf(m
, "Unknown type %d\n", entry
->type
);
888 print_trace_fmt(struct seq_file
*m
, struct trace_iterator
*iter
)
890 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
891 struct trace_entry
*entry
= iter
->ent
;
892 unsigned long usec_rem
;
893 unsigned long long t
;
898 comm
= trace_find_cmdline(iter
->ent
->pid
);
900 t
= ns2usecs(entry
->t
);
901 usec_rem
= do_div(t
, 1000000ULL);
902 secs
= (unsigned long)t
;
904 seq_printf(m
, "%16s-%-5d ", comm
, entry
->pid
);
905 seq_printf(m
, "[%02d] ", iter
->cpu
);
906 seq_printf(m
, "%5lu.%06lu: ", secs
, usec_rem
);
908 switch (entry
->type
) {
910 seq_print_ip_sym(m
, entry
->fn
.ip
, sym_flags
);
911 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
912 entry
->fn
.parent_ip
) {
913 seq_printf(m
, " <-");
914 seq_print_ip_sym(m
, entry
->fn
.parent_ip
, sym_flags
);
918 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
919 state_to_char
[entry
->ctx
.prev_state
] : 'X';
920 seq_printf(m
, " %d:%d:%c ==> %d:%d\n",
922 entry
->ctx
.prev_prio
,
925 entry
->ctx
.next_prio
);
931 static int trace_empty(struct trace_iterator
*iter
)
933 struct trace_array_cpu
*data
;
936 for_each_possible_cpu(cpu
) {
937 data
= iter
->tr
->data
[cpu
];
946 static int s_show(struct seq_file
*m
, void *v
)
948 struct trace_iterator
*iter
= v
;
950 if (iter
->ent
== NULL
) {
952 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
955 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
956 /* print nothing if the buffers are empty */
957 if (trace_empty(iter
))
959 print_trace_header(m
, iter
);
960 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
961 print_lat_help_header(m
);
963 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
964 print_func_help_header(m
);
967 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
968 print_lat_fmt(m
, iter
, iter
->idx
, iter
->cpu
);
970 print_trace_fmt(m
, iter
);
976 static struct seq_operations tracer_seq_ops
= {
983 static struct trace_iterator notrace
*
984 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
986 struct trace_iterator
*iter
;
988 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
994 mutex_lock(&trace_types_lock
);
995 if (current_trace
&& current_trace
->print_max
)
998 iter
->tr
= inode
->i_private
;
999 iter
->trace
= current_trace
;
1002 /* TODO stop tracer */
1003 *ret
= seq_open(file
, &tracer_seq_ops
);
1005 struct seq_file
*m
= file
->private_data
;
1008 /* stop the trace while dumping */
1012 if (iter
->trace
&& iter
->trace
->open
)
1013 iter
->trace
->open(iter
);
1018 mutex_unlock(&trace_types_lock
);
1024 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1026 filp
->private_data
= inode
->i_private
;
1030 int tracing_release(struct inode
*inode
, struct file
*file
)
1032 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1033 struct trace_iterator
*iter
= m
->private;
1035 mutex_lock(&trace_types_lock
);
1036 if (iter
->trace
&& iter
->trace
->close
)
1037 iter
->trace
->close(iter
);
1039 /* reenable tracing if it was previously enabled */
1042 mutex_unlock(&trace_types_lock
);
1044 seq_release(inode
, file
);
1049 static int tracing_open(struct inode
*inode
, struct file
*file
)
1053 __tracing_open(inode
, file
, &ret
);
1058 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1060 struct trace_iterator
*iter
;
1063 iter
= __tracing_open(inode
, file
, &ret
);
1066 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1072 static void notrace
*
1073 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1075 struct tracer
*t
= m
->private;
1087 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1089 struct tracer
*t
= m
->private;
1092 mutex_lock(&trace_types_lock
);
1093 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1099 static void t_stop(struct seq_file
*m
, void *p
)
1101 mutex_unlock(&trace_types_lock
);
1104 static int t_show(struct seq_file
*m
, void *v
)
1106 struct tracer
*t
= v
;
1111 seq_printf(m
, "%s", t
->name
);
1120 static struct seq_operations show_traces_seq_ops
= {
1127 static int show_traces_open(struct inode
*inode
, struct file
*file
)
1131 ret
= seq_open(file
, &show_traces_seq_ops
);
1133 struct seq_file
*m
= file
->private_data
;
1134 m
->private = trace_types
;
1140 static struct file_operations tracing_fops
= {
1141 .open
= tracing_open
,
1143 .llseek
= seq_lseek
,
1144 .release
= tracing_release
,
1147 static struct file_operations tracing_lt_fops
= {
1148 .open
= tracing_lt_open
,
1150 .llseek
= seq_lseek
,
1151 .release
= tracing_release
,
1154 static struct file_operations show_traces_fops
= {
1155 .open
= show_traces_open
,
1157 .release
= seq_release
,
1161 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1162 size_t cnt
, loff_t
*ppos
)
1169 /* calulate max size */
1170 for (i
= 0; trace_options
[i
]; i
++) {
1171 len
+= strlen(trace_options
[i
]);
1172 len
+= 3; /* "no" and space */
1175 /* +2 for \n and \0 */
1176 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
1180 for (i
= 0; trace_options
[i
]; i
++) {
1181 if (trace_flags
& (1 << i
))
1182 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
1184 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
1187 r
+= sprintf(buf
+ r
, "\n");
1188 WARN_ON(r
>= len
+ 2);
1190 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1199 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1200 size_t cnt
, loff_t
*ppos
)
1210 if (copy_from_user(&buf
, ubuf
, cnt
))
1215 if (strncmp(buf
, "no", 2) == 0) {
1220 for (i
= 0; trace_options
[i
]; i
++) {
1221 int len
= strlen(trace_options
[i
]);
1223 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
1225 trace_flags
&= ~(1 << i
);
1227 trace_flags
|= (1 << i
);
1237 static struct file_operations tracing_iter_fops
= {
1238 .open
= tracing_open_generic
,
1239 .read
= tracing_iter_ctrl_read
,
1240 .write
= tracing_iter_ctrl_write
,
1244 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1245 size_t cnt
, loff_t
*ppos
)
1247 struct trace_array
*tr
= filp
->private_data
;
1251 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
1252 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1257 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1258 size_t cnt
, loff_t
*ppos
)
1260 struct trace_array
*tr
= filp
->private_data
;
1267 if (copy_from_user(&buf
, ubuf
, cnt
))
1272 val
= simple_strtoul(buf
, NULL
, 10);
1276 mutex_lock(&trace_types_lock
);
1277 if (tr
->ctrl
^ val
) {
1285 if (current_trace
&& current_trace
->ctrl_update
)
1286 current_trace
->ctrl_update(tr
);
1288 mutex_unlock(&trace_types_lock
);
1296 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
1297 size_t cnt
, loff_t
*ppos
)
1299 char buf
[max_tracer_type_len
+2];
1302 mutex_lock(&trace_types_lock
);
1304 r
= sprintf(buf
, "%s\n", current_trace
->name
);
1306 r
= sprintf(buf
, "\n");
1307 mutex_unlock(&trace_types_lock
);
1309 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1314 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
1315 size_t cnt
, loff_t
*ppos
)
1317 struct trace_array
*tr
= &global_trace
;
1319 char buf
[max_tracer_type_len
+1];
1322 if (cnt
> max_tracer_type_len
)
1323 cnt
= max_tracer_type_len
;
1325 if (copy_from_user(&buf
, ubuf
, cnt
))
1330 /* strip ending whitespace. */
1331 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
1334 mutex_lock(&trace_types_lock
);
1335 for (t
= trace_types
; t
; t
= t
->next
) {
1336 if (strcmp(t
->name
, buf
) == 0)
1339 if (!t
|| t
== current_trace
)
1342 if (current_trace
&& current_trace
->reset
)
1343 current_trace
->reset(tr
);
1350 mutex_unlock(&trace_types_lock
);
1358 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
1359 size_t cnt
, loff_t
*ppos
)
1361 unsigned long *ptr
= filp
->private_data
;
1365 r
= snprintf(buf
, 64, "%ld\n",
1366 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
1369 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1374 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
1375 size_t cnt
, loff_t
*ppos
)
1377 long *ptr
= filp
->private_data
;
1384 if (copy_from_user(&buf
, ubuf
, cnt
))
1389 val
= simple_strtoul(buf
, NULL
, 10);
1396 static struct file_operations tracing_max_lat_fops
= {
1397 .open
= tracing_open_generic
,
1398 .read
= tracing_max_lat_read
,
1399 .write
= tracing_max_lat_write
,
1402 static struct file_operations tracing_ctrl_fops
= {
1403 .open
= tracing_open_generic
,
1404 .read
= tracing_ctrl_read
,
1405 .write
= tracing_ctrl_write
,
1408 static struct file_operations set_tracer_fops
= {
1409 .open
= tracing_open_generic
,
1410 .read
= tracing_set_trace_read
,
1411 .write
= tracing_set_trace_write
,
1414 #ifdef CONFIG_DYNAMIC_FTRACE
1417 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
1418 size_t cnt
, loff_t
*ppos
)
1420 unsigned long *p
= filp
->private_data
;
1424 r
= sprintf(buf
, "%ld\n", *p
);
1425 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1429 static struct file_operations tracing_read_long_fops
= {
1430 .open
= tracing_open_generic
,
1431 .read
= tracing_read_long
,
1435 static struct dentry
*d_tracer
;
1437 struct dentry
*tracing_init_dentry(void)
1444 d_tracer
= debugfs_create_dir("tracing", NULL
);
1446 if (!d_tracer
&& !once
) {
1448 pr_warning("Could not create debugfs directory 'tracing'\n");
1455 static __init
void tracer_init_debugfs(void)
1457 struct dentry
*d_tracer
;
1458 struct dentry
*entry
;
1460 d_tracer
= tracing_init_dentry();
1462 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
1463 &global_trace
, &tracing_ctrl_fops
);
1465 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1467 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
1468 NULL
, &tracing_iter_fops
);
1470 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1472 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
1473 &global_trace
, &tracing_lt_fops
);
1475 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1477 entry
= debugfs_create_file("trace", 0444, d_tracer
,
1478 &global_trace
, &tracing_fops
);
1480 pr_warning("Could not create debugfs 'trace' entry\n");
1482 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
1483 &global_trace
, &show_traces_fops
);
1485 pr_warning("Could not create debugfs 'trace' entry\n");
1487 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
1488 &global_trace
, &set_tracer_fops
);
1490 pr_warning("Could not create debugfs 'trace' entry\n");
1492 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
1493 &tracing_max_latency
,
1494 &tracing_max_lat_fops
);
1496 pr_warning("Could not create debugfs "
1497 "'tracing_max_latency' entry\n");
1499 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
1500 &tracing_thresh
, &tracing_max_lat_fops
);
1502 pr_warning("Could not create debugfs "
1503 "'tracing_threash' entry\n");
1505 #ifdef CONFIG_DYNAMIC_FTRACE
1506 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
1507 &ftrace_update_tot_cnt
,
1508 &tracing_read_long_fops
);
1510 pr_warning("Could not create debugfs "
1511 "'dyn_ftrace_total_info' entry\n");
1515 /* dummy trace to disable tracing */
1516 static struct tracer no_tracer __read_mostly
=
1521 static int trace_alloc_page(void)
1523 struct trace_array_cpu
*data
;
1525 struct page
*page
, *tmp
;
1529 /* first allocate a page for each CPU */
1530 for_each_possible_cpu(i
) {
1531 array
= (void *)__get_free_page(GFP_KERNEL
);
1532 if (array
== NULL
) {
1533 printk(KERN_ERR
"tracer: failed to allocate page"
1534 "for trace buffer!\n");
1538 page
= virt_to_page(array
);
1539 list_add(&page
->lru
, &pages
);
1541 /* Only allocate if we are actually using the max trace */
1542 #ifdef CONFIG_TRACER_MAX_TRACE
1543 array
= (void *)__get_free_page(GFP_KERNEL
);
1544 if (array
== NULL
) {
1545 printk(KERN_ERR
"tracer: failed to allocate page"
1546 "for trace buffer!\n");
1549 page
= virt_to_page(array
);
1550 list_add(&page
->lru
, &pages
);
1554 /* Now that we successfully allocate a page per CPU, add them */
1555 for_each_possible_cpu(i
) {
1556 data
= global_trace
.data
[i
];
1557 page
= list_entry(pages
.next
, struct page
, lru
);
1558 list_del(&page
->lru
);
1559 list_add_tail(&page
->lru
, &data
->trace_pages
);
1562 #ifdef CONFIG_TRACER_MAX_TRACE
1563 data
= max_tr
.data
[i
];
1564 page
= list_entry(pages
.next
, struct page
, lru
);
1565 list_del(&page
->lru
);
1566 list_add_tail(&page
->lru
, &data
->trace_pages
);
1570 global_trace
.entries
+= ENTRIES_PER_PAGE
;
1575 list_for_each_entry_safe(page
, tmp
, &pages
, lru
) {
1576 list_del(&page
->lru
);
1582 __init
static int tracer_alloc_buffers(void)
1584 struct trace_array_cpu
*data
;
1590 /* Allocate the first page for all buffers */
1591 for_each_possible_cpu(i
) {
1592 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
1593 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
1595 array
= (void *)__get_free_page(GFP_KERNEL
);
1596 if (array
== NULL
) {
1597 printk(KERN_ERR
"tracer: failed to allocate page"
1598 "for trace buffer!\n");
1601 data
->trace
= array
;
1603 /* set the array to the list */
1604 INIT_LIST_HEAD(&data
->trace_pages
);
1605 page
= virt_to_page(array
);
1606 list_add(&page
->lru
, &data
->trace_pages
);
1607 /* use the LRU flag to differentiate the two buffers */
1610 /* Only allocate if we are actually using the max trace */
1611 #ifdef CONFIG_TRACER_MAX_TRACE
1612 array
= (void *)__get_free_page(GFP_KERNEL
);
1613 if (array
== NULL
) {
1614 printk(KERN_ERR
"tracer: failed to allocate page"
1615 "for trace buffer!\n");
1618 max_tr
.data
[i
]->trace
= array
;
1620 INIT_LIST_HEAD(&max_tr
.data
[i
]->trace_pages
);
1621 page
= virt_to_page(array
);
1622 list_add(&page
->lru
, &max_tr
.data
[i
]->trace_pages
);
1628 * Since we allocate by orders of pages, we may be able to
1631 global_trace
.entries
= ENTRIES_PER_PAGE
;
1634 while (global_trace
.entries
< trace_nr_entries
) {
1635 if (trace_alloc_page())
1639 max_tr
.entries
= global_trace
.entries
;
1641 pr_info("tracer: %d pages allocated for %ld",
1642 pages
, trace_nr_entries
);
1643 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE
);
1644 pr_info(" actual entries %ld\n", global_trace
.entries
);
1646 tracer_init_debugfs();
1648 trace_init_cmdlines();
1650 register_tracer(&no_tracer
);
1651 current_trace
= &no_tracer
;
1656 for (i
-- ; i
>= 0; i
--) {
1657 struct page
*page
, *tmp
;
1658 struct trace_array_cpu
*data
= global_trace
.data
[i
];
1660 if (data
&& data
->trace
) {
1661 list_for_each_entry_safe(page
, tmp
,
1662 &data
->trace_pages
, lru
) {
1663 list_del(&page
->lru
);
1669 #ifdef CONFIG_TRACER_MAX_TRACE
1670 data
= max_tr
.data
[i
];
1671 if (data
&& data
->trace
) {
1672 list_for_each_entry_safe(page
, tmp
,
1673 &data
->trace_pages
, lru
) {
1674 list_del(&page
->lru
);
1684 device_initcall(tracer_alloc_buffers
);