2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/gfp.h>
32 unsigned long __read_mostly tracing_max_latency
= (cycle_t
)ULONG_MAX
;
33 unsigned long __read_mostly tracing_thresh
;
35 static int tracing_disabled
= 1;
38 ns2usecs(cycle_t nsec
)
45 notrace cycle_t
ftrace_now(int cpu
)
47 return cpu_clock(cpu
);
50 static struct trace_array global_trace
;
52 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
54 static struct trace_array max_tr
;
56 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
58 static int tracer_enabled
= 1;
59 static unsigned long trace_nr_entries
= 16384UL;
61 static struct tracer
*trace_types __read_mostly
;
62 static struct tracer
*current_trace __read_mostly
;
63 static int max_tracer_type_len
;
65 static DEFINE_MUTEX(trace_types_lock
);
67 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
69 static int __init
set_nr_entries(char *str
)
73 trace_nr_entries
= simple_strtoul(str
, &str
, 0);
76 __setup("trace_entries=", set_nr_entries
);
78 unsigned long nsecs_to_usecs(unsigned long nsecs
)
84 __TRACE_FIRST_TYPE
= 0,
93 enum trace_flag_type
{
94 TRACE_FLAG_IRQS_OFF
= 0x01,
95 TRACE_FLAG_NEED_RESCHED
= 0x02,
96 TRACE_FLAG_HARDIRQ
= 0x04,
97 TRACE_FLAG_SOFTIRQ
= 0x08,
100 enum trace_iterator_flags
{
101 TRACE_ITER_PRINT_PARENT
= 0x01,
102 TRACE_ITER_SYM_OFFSET
= 0x02,
103 TRACE_ITER_SYM_ADDR
= 0x04,
104 TRACE_ITER_VERBOSE
= 0x08,
105 TRACE_ITER_RAW
= 0x10,
106 TRACE_ITER_HEX
= 0x20,
107 TRACE_ITER_BIN
= 0x40,
110 #define TRACE_ITER_SYM_MASK \
111 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
113 /* These must match the bit postions above */
114 static const char *trace_options
[] = {
125 static unsigned trace_flags
;
127 static DEFINE_SPINLOCK(ftrace_max_lock
);
130 * Copy the new maximum trace into the separate maximum-trace
131 * structure. (this way the maximum trace is permanently saved,
132 * for later retrieval via /debugfs/tracing/latency_trace)
135 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
137 struct trace_array_cpu
*data
= tr
->data
[cpu
];
140 max_tr
.time_start
= data
->preempt_timestamp
;
142 data
= max_tr
.data
[cpu
];
143 data
->saved_latency
= tracing_max_latency
;
145 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
146 data
->pid
= tsk
->pid
;
147 data
->uid
= tsk
->uid
;
148 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
149 data
->policy
= tsk
->policy
;
150 data
->rt_priority
= tsk
->rt_priority
;
152 /* record this tasks comm */
153 tracing_record_cmdline(current
);
156 void check_pages(struct trace_array_cpu
*data
)
158 struct page
*page
, *tmp
;
160 BUG_ON(data
->trace_pages
.next
->prev
!= &data
->trace_pages
);
161 BUG_ON(data
->trace_pages
.prev
->next
!= &data
->trace_pages
);
163 list_for_each_entry_safe(page
, tmp
, &data
->trace_pages
, lru
) {
164 BUG_ON(page
->lru
.next
->prev
!= &page
->lru
);
165 BUG_ON(page
->lru
.prev
->next
!= &page
->lru
);
169 void *head_page(struct trace_array_cpu
*data
)
174 if (list_empty(&data
->trace_pages
))
177 page
= list_entry(data
->trace_pages
.next
, struct page
, lru
);
178 BUG_ON(&page
->lru
== &data
->trace_pages
);
180 return page_address(page
);
184 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
186 int len
= (PAGE_SIZE
- 1) - s
->len
;
194 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
197 /* If we can't write it all, don't bother writing anything */
207 trace_seq_puts(struct trace_seq
*s
, const char *str
)
209 int len
= strlen(str
);
211 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
214 memcpy(s
->buffer
+ s
->len
, str
, len
);
221 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
223 if (s
->len
>= (PAGE_SIZE
- 1))
226 s
->buffer
[s
->len
++] = c
;
232 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
234 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
237 memcpy(s
->buffer
+ s
->len
, mem
, len
);
246 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
248 unsigned char hex
[HEX_CHARS
];
253 BUG_ON(len
>= HEX_CHARS
);
258 for (i
= 0, j
= 0; i
< len
; i
++) {
260 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
264 hex
[j
] = byte
& 0x0f;
281 return trace_seq_putmem(s
, hex
, j
);
285 trace_seq_reset(struct trace_seq
*s
)
291 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
293 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
296 seq_puts(m
, s
->buffer
);
302 flip_trace(struct trace_array_cpu
*tr1
, struct trace_array_cpu
*tr2
)
304 struct list_head flip_pages
;
306 INIT_LIST_HEAD(&flip_pages
);
308 memcpy(&tr1
->trace_head_idx
, &tr2
->trace_head_idx
,
309 sizeof(struct trace_array_cpu
) -
310 offsetof(struct trace_array_cpu
, trace_head_idx
));
314 list_splice_init(&tr1
->trace_pages
, &flip_pages
);
315 list_splice_init(&tr2
->trace_pages
, &tr1
->trace_pages
);
316 list_splice_init(&flip_pages
, &tr2
->trace_pages
);
317 BUG_ON(!list_empty(&flip_pages
));
323 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
325 struct trace_array_cpu
*data
;
328 WARN_ON_ONCE(!irqs_disabled());
329 spin_lock(&ftrace_max_lock
);
330 /* clear out all the previous traces */
331 for_each_possible_cpu(i
) {
333 flip_trace(max_tr
.data
[i
], data
);
337 __update_max_tr(tr
, tsk
, cpu
);
338 spin_unlock(&ftrace_max_lock
);
342 * update_max_tr_single - only copy one trace over, and reset the rest
344 * @tsk - task with the latency
345 * @cpu - the cpu of the buffer to copy.
348 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
350 struct trace_array_cpu
*data
= tr
->data
[cpu
];
353 WARN_ON_ONCE(!irqs_disabled());
354 spin_lock(&ftrace_max_lock
);
355 for_each_possible_cpu(i
)
356 tracing_reset(max_tr
.data
[i
]);
358 flip_trace(max_tr
.data
[cpu
], data
);
361 __update_max_tr(tr
, tsk
, cpu
);
362 spin_unlock(&ftrace_max_lock
);
365 int register_tracer(struct tracer
*type
)
372 pr_info("Tracer must have a name\n");
376 mutex_lock(&trace_types_lock
);
377 for (t
= trace_types
; t
; t
= t
->next
) {
378 if (strcmp(type
->name
, t
->name
) == 0) {
380 pr_info("Trace %s already registered\n",
387 #ifdef CONFIG_FTRACE_STARTUP_TEST
388 if (type
->selftest
) {
389 struct tracer
*saved_tracer
= current_trace
;
390 struct trace_array_cpu
*data
;
391 struct trace_array
*tr
= &global_trace
;
392 int saved_ctrl
= tr
->ctrl
;
395 * Run a selftest on this tracer.
396 * Here we reset the trace buffer, and set the current
397 * tracer to be this tracer. The tracer can then run some
398 * internal tracing to verify that everything is in order.
399 * If we fail, we do not register this tracer.
401 for_each_possible_cpu(i
) {
403 if (!head_page(data
))
407 current_trace
= type
;
409 /* the test is responsible for initializing and enabling */
410 pr_info("Testing tracer %s: ", type
->name
);
411 ret
= type
->selftest(type
, tr
);
412 /* the test is responsible for resetting too */
413 current_trace
= saved_tracer
;
414 tr
->ctrl
= saved_ctrl
;
416 printk(KERN_CONT
"FAILED!\n");
419 /* Only reset on passing, to avoid touching corrupted buffers */
420 for_each_possible_cpu(i
) {
422 if (!head_page(data
))
426 printk(KERN_CONT
"PASSED\n");
430 type
->next
= trace_types
;
432 len
= strlen(type
->name
);
433 if (len
> max_tracer_type_len
)
434 max_tracer_type_len
= len
;
437 mutex_unlock(&trace_types_lock
);
442 void unregister_tracer(struct tracer
*type
)
447 mutex_lock(&trace_types_lock
);
448 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
452 pr_info("Trace %s not registered\n", type
->name
);
457 if (strlen(type
->name
) != max_tracer_type_len
)
460 max_tracer_type_len
= 0;
461 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
462 len
= strlen((*t
)->name
);
463 if (len
> max_tracer_type_len
)
464 max_tracer_type_len
= len
;
467 mutex_unlock(&trace_types_lock
);
470 notrace
void tracing_reset(struct trace_array_cpu
*data
)
473 data
->trace_head
= data
->trace_tail
= head_page(data
);
474 data
->trace_head_idx
= 0;
475 data
->trace_tail_idx
= 0;
478 #define SAVED_CMDLINES 128
479 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
480 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
481 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
482 static int cmdline_idx
;
483 static DEFINE_SPINLOCK(trace_cmdline_lock
);
484 atomic_t trace_record_cmdline_disabled
;
486 static void trace_init_cmdlines(void)
488 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
489 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
493 notrace
void trace_stop_cmdline_recording(void);
495 static notrace
void trace_save_cmdline(struct task_struct
*tsk
)
500 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
504 * It's not the end of the world if we don't get
505 * the lock, but we also don't want to spin
506 * nor do we want to disable interrupts,
507 * so if we miss here, then better luck next time.
509 if (!spin_trylock(&trace_cmdline_lock
))
512 idx
= map_pid_to_cmdline
[tsk
->pid
];
513 if (idx
>= SAVED_CMDLINES
) {
514 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
516 map
= map_cmdline_to_pid
[idx
];
517 if (map
<= PID_MAX_DEFAULT
)
518 map_pid_to_cmdline
[map
] = (unsigned)-1;
520 map_pid_to_cmdline
[tsk
->pid
] = idx
;
525 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
527 spin_unlock(&trace_cmdline_lock
);
530 static notrace
char *trace_find_cmdline(int pid
)
532 char *cmdline
= "<...>";
538 if (pid
> PID_MAX_DEFAULT
)
541 map
= map_pid_to_cmdline
[pid
];
542 if (map
>= SAVED_CMDLINES
)
545 cmdline
= saved_cmdlines
[map
];
551 notrace
void tracing_record_cmdline(struct task_struct
*tsk
)
553 if (atomic_read(&trace_record_cmdline_disabled
))
556 trace_save_cmdline(tsk
);
559 static inline notrace
struct list_head
*
560 trace_next_list(struct trace_array_cpu
*data
, struct list_head
*next
)
563 * Roundrobin - but skip the head (which is not a real page):
566 if (unlikely(next
== &data
->trace_pages
))
568 BUG_ON(next
== &data
->trace_pages
);
573 static inline notrace
void *
574 trace_next_page(struct trace_array_cpu
*data
, void *addr
)
576 struct list_head
*next
;
579 page
= virt_to_page(addr
);
581 next
= trace_next_list(data
, &page
->lru
);
582 page
= list_entry(next
, struct page
, lru
);
584 return page_address(page
);
587 static inline notrace
struct trace_entry
*
588 tracing_get_trace_entry(struct trace_array
*tr
, struct trace_array_cpu
*data
)
590 unsigned long idx
, idx_next
;
591 struct trace_entry
*entry
;
594 idx
= data
->trace_head_idx
;
597 BUG_ON(idx
* TRACE_ENTRY_SIZE
>= PAGE_SIZE
);
599 entry
= data
->trace_head
+ idx
* TRACE_ENTRY_SIZE
;
601 if (unlikely(idx_next
>= ENTRIES_PER_PAGE
)) {
602 data
->trace_head
= trace_next_page(data
, data
->trace_head
);
606 if (data
->trace_head
== data
->trace_tail
&&
607 idx_next
== data
->trace_tail_idx
) {
609 data
->trace_tail_idx
++;
610 if (data
->trace_tail_idx
>= ENTRIES_PER_PAGE
) {
612 trace_next_page(data
, data
->trace_tail
);
613 data
->trace_tail_idx
= 0;
617 data
->trace_head_idx
= idx_next
;
622 static inline notrace
void
623 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
)
625 struct task_struct
*tsk
= current
;
628 pc
= preempt_count();
630 entry
->preempt_count
= pc
& 0xff;
631 entry
->pid
= tsk
->pid
;
632 entry
->t
= ftrace_now(raw_smp_processor_id());
633 entry
->flags
= (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
634 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
635 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
636 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
640 __ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
641 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
643 struct trace_entry
*entry
;
644 unsigned long irq_flags
;
646 spin_lock_irqsave(&data
->lock
, irq_flags
);
647 entry
= tracing_get_trace_entry(tr
, data
);
648 tracing_generic_entry_update(entry
, flags
);
649 entry
->type
= TRACE_FN
;
651 entry
->fn
.parent_ip
= parent_ip
;
652 spin_unlock_irqrestore(&data
->lock
, irq_flags
);
656 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
657 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
659 if (likely(!atomic_read(&data
->disabled
)))
660 __ftrace(tr
, data
, ip
, parent_ip
, flags
);
664 trace_special(struct trace_array
*tr
, struct trace_array_cpu
*data
,
665 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
667 struct trace_entry
*entry
;
668 unsigned long irq_flags
;
670 spin_lock_irqsave(&data
->lock
, irq_flags
);
671 entry
= tracing_get_trace_entry(tr
, data
);
672 tracing_generic_entry_update(entry
, 0);
673 entry
->type
= TRACE_SPECIAL
;
674 entry
->special
.arg1
= arg1
;
675 entry
->special
.arg2
= arg2
;
676 entry
->special
.arg3
= arg3
;
677 spin_unlock_irqrestore(&data
->lock
, irq_flags
);
681 tracing_sched_switch_trace(struct trace_array
*tr
,
682 struct trace_array_cpu
*data
,
683 struct task_struct
*prev
, struct task_struct
*next
,
686 struct trace_entry
*entry
;
687 unsigned long irq_flags
;
689 spin_lock_irqsave(&data
->lock
, irq_flags
);
690 entry
= tracing_get_trace_entry(tr
, data
);
691 tracing_generic_entry_update(entry
, flags
);
692 entry
->type
= TRACE_CTX
;
693 entry
->ctx
.prev_pid
= prev
->pid
;
694 entry
->ctx
.prev_prio
= prev
->prio
;
695 entry
->ctx
.prev_state
= prev
->state
;
696 entry
->ctx
.next_pid
= next
->pid
;
697 entry
->ctx
.next_prio
= next
->prio
;
698 spin_unlock_irqrestore(&data
->lock
, irq_flags
);
703 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
705 struct trace_array
*tr
= &global_trace
;
706 struct trace_array_cpu
*data
;
711 if (unlikely(!tracer_enabled
))
714 local_irq_save(flags
);
715 cpu
= raw_smp_processor_id();
716 data
= tr
->data
[cpu
];
717 disabled
= atomic_inc_return(&data
->disabled
);
719 if (likely(disabled
== 1))
720 __ftrace(tr
, data
, ip
, parent_ip
, flags
);
722 atomic_dec(&data
->disabled
);
723 local_irq_restore(flags
);
726 static struct ftrace_ops trace_ops __read_mostly
=
728 .func
= function_trace_call
,
731 notrace
void tracing_start_function_trace(void)
733 register_ftrace_function(&trace_ops
);
736 notrace
void tracing_stop_function_trace(void)
738 unregister_ftrace_function(&trace_ops
);
742 enum trace_file_type
{
743 TRACE_FILE_LAT_FMT
= 1,
746 static struct trace_entry
*
747 trace_entry_idx(struct trace_array
*tr
, struct trace_array_cpu
*data
,
748 struct trace_iterator
*iter
, int cpu
)
751 struct trace_entry
*array
;
753 if (iter
->next_idx
[cpu
] >= tr
->entries
||
754 iter
->next_idx
[cpu
] >= data
->trace_idx
||
755 (data
->trace_head
== data
->trace_tail
&&
756 data
->trace_head_idx
== data
->trace_tail_idx
))
759 if (!iter
->next_page
[cpu
]) {
760 /* Initialize the iterator for this cpu trace buffer */
761 WARN_ON(!data
->trace_tail
);
762 page
= virt_to_page(data
->trace_tail
);
763 iter
->next_page
[cpu
] = &page
->lru
;
764 iter
->next_page_idx
[cpu
] = data
->trace_tail_idx
;
767 page
= list_entry(iter
->next_page
[cpu
], struct page
, lru
);
768 BUG_ON(&data
->trace_pages
== &page
->lru
);
770 array
= page_address(page
);
772 WARN_ON(iter
->next_page_idx
[cpu
] >= ENTRIES_PER_PAGE
);
773 return &array
[iter
->next_page_idx
[cpu
]];
776 static struct trace_entry
* notrace
777 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
)
779 struct trace_array
*tr
= iter
->tr
;
780 struct trace_entry
*ent
, *next
= NULL
;
784 for_each_possible_cpu(cpu
) {
785 if (!head_page(tr
->data
[cpu
]))
787 ent
= trace_entry_idx(tr
, tr
->data
[cpu
], iter
, cpu
);
789 * Pick the entry with the smallest timestamp:
791 if (ent
&& (!next
|| ent
->t
< next
->t
)) {
803 static notrace
void trace_iterator_increment(struct trace_iterator
*iter
)
806 iter
->next_idx
[iter
->cpu
]++;
807 iter
->next_page_idx
[iter
->cpu
]++;
809 if (iter
->next_page_idx
[iter
->cpu
] >= ENTRIES_PER_PAGE
) {
810 struct trace_array_cpu
*data
= iter
->tr
->data
[iter
->cpu
];
812 iter
->next_page_idx
[iter
->cpu
] = 0;
813 iter
->next_page
[iter
->cpu
] =
814 trace_next_list(data
, iter
->next_page
[iter
->cpu
]);
818 static notrace
void trace_consume(struct trace_iterator
*iter
)
820 struct trace_array_cpu
*data
= iter
->tr
->data
[iter
->cpu
];
822 data
->trace_tail_idx
++;
823 if (data
->trace_tail_idx
>= ENTRIES_PER_PAGE
) {
824 data
->trace_tail
= trace_next_page(data
, data
->trace_tail
);
825 data
->trace_tail_idx
= 0;
828 /* Check if we empty it, then reset the index */
829 if (data
->trace_head
== data
->trace_tail
&&
830 data
->trace_head_idx
== data
->trace_tail_idx
)
834 static notrace
void *find_next_entry_inc(struct trace_iterator
*iter
)
836 struct trace_entry
*next
;
839 next
= find_next_entry(iter
, &next_cpu
);
841 iter
->prev_ent
= iter
->ent
;
842 iter
->prev_cpu
= iter
->cpu
;
845 iter
->cpu
= next_cpu
;
848 trace_iterator_increment(iter
);
850 return next
? iter
: NULL
;
853 static notrace
void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
855 struct trace_iterator
*iter
= m
->private;
856 void *last_ent
= iter
->ent
;
862 /* can't go backwards */
867 ent
= find_next_entry_inc(iter
);
871 while (ent
&& iter
->idx
< i
)
872 ent
= find_next_entry_inc(iter
);
876 if (last_ent
&& !ent
)
877 seq_puts(m
, "\n\nvim:ft=help\n");
882 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
884 struct trace_iterator
*iter
= m
->private;
889 mutex_lock(&trace_types_lock
);
891 if (!current_trace
|| current_trace
!= iter
->trace
)
894 atomic_inc(&trace_record_cmdline_disabled
);
896 /* let the tracer grab locks here if needed */
897 if (current_trace
->start
)
898 current_trace
->start(iter
);
900 if (*pos
!= iter
->pos
) {
904 iter
->prev_ent
= NULL
;
907 for_each_possible_cpu(i
) {
908 iter
->next_idx
[i
] = 0;
909 iter
->next_page
[i
] = NULL
;
912 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
917 p
= s_next(m
, p
, &l
);
923 static void s_stop(struct seq_file
*m
, void *p
)
925 struct trace_iterator
*iter
= m
->private;
927 atomic_dec(&trace_record_cmdline_disabled
);
929 /* let the tracer release locks here if needed */
930 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
931 iter
->trace
->stop(iter
);
933 mutex_unlock(&trace_types_lock
);
937 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
939 #ifdef CONFIG_KALLSYMS
940 char str
[KSYM_SYMBOL_LEN
];
942 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
944 return trace_seq_printf(s
, fmt
, str
);
950 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
951 unsigned long address
)
953 #ifdef CONFIG_KALLSYMS
954 char str
[KSYM_SYMBOL_LEN
];
956 sprint_symbol(str
, address
);
957 return trace_seq_printf(s
, fmt
, str
);
963 # define IP_FMT "%08lx"
965 # define IP_FMT "%016lx"
969 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
974 return trace_seq_printf(s
, "0");
976 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
977 ret
= seq_print_sym_offset(s
, "%s", ip
);
979 ret
= seq_print_sym_short(s
, "%s", ip
);
984 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
985 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
989 static notrace
void print_lat_help_header(struct seq_file
*m
)
991 seq_puts(m
, "# _------=> CPU# \n");
992 seq_puts(m
, "# / _-----=> irqs-off \n");
993 seq_puts(m
, "# | / _----=> need-resched \n");
994 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
995 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
996 seq_puts(m
, "# |||| / \n");
997 seq_puts(m
, "# ||||| delay \n");
998 seq_puts(m
, "# cmd pid ||||| time | caller \n");
999 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1002 static notrace
void print_func_help_header(struct seq_file
*m
)
1004 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1005 seq_puts(m
, "# | | | | |\n");
1010 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1012 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1013 struct trace_array
*tr
= iter
->tr
;
1014 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1015 struct tracer
*type
= current_trace
;
1016 unsigned long total
= 0;
1017 unsigned long entries
= 0;
1019 const char *name
= "preemption";
1024 for_each_possible_cpu(cpu
) {
1025 if (head_page(tr
->data
[cpu
])) {
1026 total
+= tr
->data
[cpu
]->trace_idx
;
1027 if (tr
->data
[cpu
]->trace_idx
> tr
->entries
)
1028 entries
+= tr
->entries
;
1030 entries
+= tr
->data
[cpu
]->trace_idx
;
1034 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1036 seq_puts(m
, "-----------------------------------"
1037 "---------------------------------\n");
1038 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1039 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1040 nsecs_to_usecs(data
->saved_latency
),
1044 #if defined(CONFIG_PREEMPT_NONE)
1046 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1048 #elif defined(CONFIG_PREEMPT_DESKTOP)
1053 /* These are reserved for later use */
1056 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1060 seq_puts(m
, " -----------------\n");
1061 seq_printf(m
, " | task: %.16s-%d "
1062 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1063 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1064 data
->policy
, data
->rt_priority
);
1065 seq_puts(m
, " -----------------\n");
1067 if (data
->critical_start
) {
1068 seq_puts(m
, " => started at: ");
1069 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1070 trace_print_seq(m
, &iter
->seq
);
1071 seq_puts(m
, "\n => ended at: ");
1072 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1073 trace_print_seq(m
, &iter
->seq
);
1081 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1083 int hardirq
, softirq
;
1086 comm
= trace_find_cmdline(entry
->pid
);
1088 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1089 trace_seq_printf(s
, "%d", cpu
);
1090 trace_seq_printf(s
, "%c%c",
1091 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
1092 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1094 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1095 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1096 if (hardirq
&& softirq
)
1097 trace_seq_putc(s
, 'H');
1100 trace_seq_putc(s
, 'h');
1103 trace_seq_putc(s
, 's');
1105 trace_seq_putc(s
, '.');
1109 if (entry
->preempt_count
)
1110 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1112 trace_seq_puts(s
, ".");
1115 unsigned long preempt_mark_thresh
= 100;
1118 lat_print_timestamp(struct trace_seq
*s
, unsigned long long abs_usecs
,
1119 unsigned long rel_usecs
)
1121 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1122 if (rel_usecs
> preempt_mark_thresh
)
1123 trace_seq_puts(s
, "!: ");
1124 else if (rel_usecs
> 1)
1125 trace_seq_puts(s
, "+: ");
1127 trace_seq_puts(s
, " : ");
1130 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1133 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1135 struct trace_seq
*s
= &iter
->seq
;
1136 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1137 struct trace_entry
*next_entry
= find_next_entry(iter
, NULL
);
1138 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1139 struct trace_entry
*entry
= iter
->ent
;
1140 unsigned long abs_usecs
;
1141 unsigned long rel_usecs
;
1147 rel_usecs
= ns2usecs(next_entry
->t
- entry
->t
);
1148 abs_usecs
= ns2usecs(entry
->t
- iter
->tr
->time_start
);
1151 comm
= trace_find_cmdline(entry
->pid
);
1152 trace_seq_printf(s
, "%16s %5d %d %d %08x %08x [%08lx]"
1153 " %ld.%03ldms (+%ld.%03ldms): ",
1155 entry
->pid
, cpu
, entry
->flags
,
1156 entry
->preempt_count
, trace_idx
,
1159 abs_usecs
% 1000, rel_usecs
/1000,
1162 lat_print_generic(s
, entry
, cpu
);
1163 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1165 switch (entry
->type
) {
1167 seq_print_ip_sym(s
, entry
->fn
.ip
, sym_flags
);
1168 trace_seq_puts(s
, " (");
1169 seq_print_ip_sym(s
, entry
->fn
.parent_ip
, sym_flags
);
1170 trace_seq_puts(s
, ")\n");
1173 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1174 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1175 comm
= trace_find_cmdline(entry
->ctx
.next_pid
);
1176 trace_seq_printf(s
, " %d:%d:%c --> %d:%d %s\n",
1177 entry
->ctx
.prev_pid
,
1178 entry
->ctx
.prev_prio
,
1180 entry
->ctx
.next_pid
,
1181 entry
->ctx
.next_prio
,
1185 trace_seq_printf(s
, " %lx %lx %lx\n",
1186 entry
->special
.arg1
,
1187 entry
->special
.arg2
,
1188 entry
->special
.arg3
);
1191 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1196 static notrace
int print_trace_fmt(struct trace_iterator
*iter
)
1198 struct trace_seq
*s
= &iter
->seq
;
1199 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1200 struct trace_entry
*entry
;
1201 unsigned long usec_rem
;
1202 unsigned long long t
;
1210 comm
= trace_find_cmdline(iter
->ent
->pid
);
1212 t
= ns2usecs(entry
->t
);
1213 usec_rem
= do_div(t
, 1000000ULL);
1214 secs
= (unsigned long)t
;
1216 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1219 ret
= trace_seq_printf(s
, "[%02d] ", iter
->cpu
);
1222 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1226 switch (entry
->type
) {
1228 ret
= seq_print_ip_sym(s
, entry
->fn
.ip
, sym_flags
);
1231 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1232 entry
->fn
.parent_ip
) {
1233 ret
= trace_seq_printf(s
, " <-");
1236 ret
= seq_print_ip_sym(s
, entry
->fn
.parent_ip
,
1241 ret
= trace_seq_printf(s
, "\n");
1246 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1247 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1248 ret
= trace_seq_printf(s
, " %d:%d:%c ==> %d:%d\n",
1249 entry
->ctx
.prev_pid
,
1250 entry
->ctx
.prev_prio
,
1252 entry
->ctx
.next_pid
,
1253 entry
->ctx
.next_prio
);
1258 ret
= trace_seq_printf(s
, " %lx %lx %lx\n",
1259 entry
->special
.arg1
,
1260 entry
->special
.arg2
,
1261 entry
->special
.arg3
);
1269 static notrace
int print_raw_fmt(struct trace_iterator
*iter
)
1271 struct trace_seq
*s
= &iter
->seq
;
1272 struct trace_entry
*entry
;
1278 ret
= trace_seq_printf(s
, "%d %d %llu ",
1279 entry
->pid
, iter
->cpu
, entry
->t
);
1283 switch (entry
->type
) {
1285 ret
= trace_seq_printf(s
, "%x %x\n",
1286 entry
->fn
.ip
, entry
->fn
.parent_ip
);
1291 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1292 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1293 ret
= trace_seq_printf(s
, "%d %d %c %d %d\n",
1294 entry
->ctx
.prev_pid
,
1295 entry
->ctx
.prev_prio
,
1297 entry
->ctx
.next_pid
,
1298 entry
->ctx
.next_prio
);
1303 ret
= trace_seq_printf(s
, " %lx %lx %lx\n",
1304 entry
->special
.arg1
,
1305 entry
->special
.arg2
,
1306 entry
->special
.arg3
);
1314 #define SEQ_PUT_FIELD_RET(s, x) \
1316 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1320 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1322 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1326 static notrace
int print_hex_fmt(struct trace_iterator
*iter
)
1328 struct trace_seq
*s
= &iter
->seq
;
1329 unsigned char newline
= '\n';
1330 struct trace_entry
*entry
;
1335 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1336 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1337 SEQ_PUT_HEX_FIELD_RET(s
, entry
->t
);
1339 switch (entry
->type
) {
1341 SEQ_PUT_HEX_FIELD_RET(s
, entry
->fn
.ip
);
1342 SEQ_PUT_HEX_FIELD_RET(s
, entry
->fn
.parent_ip
);
1345 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1346 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1347 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.prev_pid
);
1348 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.prev_prio
);
1349 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1350 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.next_pid
);
1351 SEQ_PUT_HEX_FIELD_RET(s
, entry
->ctx
.next_prio
);
1352 SEQ_PUT_HEX_FIELD_RET(s
, entry
->fn
.parent_ip
);
1355 SEQ_PUT_HEX_FIELD_RET(s
, entry
->special
.arg1
);
1356 SEQ_PUT_HEX_FIELD_RET(s
, entry
->special
.arg2
);
1357 SEQ_PUT_HEX_FIELD_RET(s
, entry
->special
.arg3
);
1360 SEQ_PUT_FIELD_RET(s
, newline
);
1365 static notrace
int print_bin_fmt(struct trace_iterator
*iter
)
1367 struct trace_seq
*s
= &iter
->seq
;
1368 struct trace_entry
*entry
;
1372 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1373 SEQ_PUT_FIELD_RET(s
, entry
->cpu
);
1374 SEQ_PUT_FIELD_RET(s
, entry
->t
);
1376 switch (entry
->type
) {
1378 SEQ_PUT_FIELD_RET(s
, entry
->fn
.ip
);
1379 SEQ_PUT_FIELD_RET(s
, entry
->fn
.parent_ip
);
1382 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.prev_pid
);
1383 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.prev_prio
);
1384 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.prev_state
);
1385 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.next_pid
);
1386 SEQ_PUT_FIELD_RET(s
, entry
->ctx
.next_prio
);
1389 SEQ_PUT_FIELD_RET(s
, entry
->special
.arg1
);
1390 SEQ_PUT_FIELD_RET(s
, entry
->special
.arg2
);
1391 SEQ_PUT_FIELD_RET(s
, entry
->special
.arg3
);
1397 static int trace_empty(struct trace_iterator
*iter
)
1399 struct trace_array_cpu
*data
;
1402 for_each_possible_cpu(cpu
) {
1403 data
= iter
->tr
->data
[cpu
];
1405 if (head_page(data
) && data
->trace_idx
&&
1406 (data
->trace_tail
!= data
->trace_head
||
1407 data
->trace_tail_idx
!= data
->trace_head_idx
))
1413 static int print_trace_line(struct trace_iterator
*iter
)
1415 if (trace_flags
& TRACE_ITER_BIN
)
1416 return print_bin_fmt(iter
);
1418 if (trace_flags
& TRACE_ITER_HEX
)
1419 return print_hex_fmt(iter
);
1421 if (trace_flags
& TRACE_ITER_RAW
)
1422 return print_raw_fmt(iter
);
1424 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1425 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1427 return print_trace_fmt(iter
);
1430 static int s_show(struct seq_file
*m
, void *v
)
1432 struct trace_iterator
*iter
= v
;
1434 if (iter
->ent
== NULL
) {
1436 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1439 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1440 /* print nothing if the buffers are empty */
1441 if (trace_empty(iter
))
1443 print_trace_header(m
, iter
);
1444 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1445 print_lat_help_header(m
);
1447 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1448 print_func_help_header(m
);
1451 print_trace_line(iter
);
1452 trace_print_seq(m
, &iter
->seq
);
1458 static struct seq_operations tracer_seq_ops
= {
1465 static struct trace_iterator notrace
*
1466 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1468 struct trace_iterator
*iter
;
1470 if (tracing_disabled
) {
1475 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1481 mutex_lock(&trace_types_lock
);
1482 if (current_trace
&& current_trace
->print_max
)
1485 iter
->tr
= inode
->i_private
;
1486 iter
->trace
= current_trace
;
1489 /* TODO stop tracer */
1490 *ret
= seq_open(file
, &tracer_seq_ops
);
1492 struct seq_file
*m
= file
->private_data
;
1495 /* stop the trace while dumping */
1499 if (iter
->trace
&& iter
->trace
->open
)
1500 iter
->trace
->open(iter
);
1505 mutex_unlock(&trace_types_lock
);
1511 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1513 if (tracing_disabled
)
1516 filp
->private_data
= inode
->i_private
;
1520 int tracing_release(struct inode
*inode
, struct file
*file
)
1522 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1523 struct trace_iterator
*iter
= m
->private;
1525 mutex_lock(&trace_types_lock
);
1526 if (iter
->trace
&& iter
->trace
->close
)
1527 iter
->trace
->close(iter
);
1529 /* reenable tracing if it was previously enabled */
1532 mutex_unlock(&trace_types_lock
);
1534 seq_release(inode
, file
);
1539 static int tracing_open(struct inode
*inode
, struct file
*file
)
1543 __tracing_open(inode
, file
, &ret
);
1548 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1550 struct trace_iterator
*iter
;
1553 iter
= __tracing_open(inode
, file
, &ret
);
1556 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1562 static notrace
void *
1563 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1565 struct tracer
*t
= m
->private;
1577 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1579 struct tracer
*t
= m
->private;
1582 mutex_lock(&trace_types_lock
);
1583 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1589 static void t_stop(struct seq_file
*m
, void *p
)
1591 mutex_unlock(&trace_types_lock
);
1594 static int t_show(struct seq_file
*m
, void *v
)
1596 struct tracer
*t
= v
;
1601 seq_printf(m
, "%s", t
->name
);
1610 static struct seq_operations show_traces_seq_ops
= {
1617 static int show_traces_open(struct inode
*inode
, struct file
*file
)
1621 if (tracing_disabled
)
1624 ret
= seq_open(file
, &show_traces_seq_ops
);
1626 struct seq_file
*m
= file
->private_data
;
1627 m
->private = trace_types
;
1633 static struct file_operations tracing_fops
= {
1634 .open
= tracing_open
,
1636 .llseek
= seq_lseek
,
1637 .release
= tracing_release
,
1640 static struct file_operations tracing_lt_fops
= {
1641 .open
= tracing_lt_open
,
1643 .llseek
= seq_lseek
,
1644 .release
= tracing_release
,
1647 static struct file_operations show_traces_fops
= {
1648 .open
= show_traces_open
,
1650 .release
= seq_release
,
1654 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1655 size_t cnt
, loff_t
*ppos
)
1662 /* calulate max size */
1663 for (i
= 0; trace_options
[i
]; i
++) {
1664 len
+= strlen(trace_options
[i
]);
1665 len
+= 3; /* "no" and space */
1668 /* +2 for \n and \0 */
1669 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
1673 for (i
= 0; trace_options
[i
]; i
++) {
1674 if (trace_flags
& (1 << i
))
1675 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
1677 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
1680 r
+= sprintf(buf
+ r
, "\n");
1681 WARN_ON(r
>= len
+ 2);
1683 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1692 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1693 size_t cnt
, loff_t
*ppos
)
1703 if (copy_from_user(&buf
, ubuf
, cnt
))
1708 if (strncmp(buf
, "no", 2) == 0) {
1713 for (i
= 0; trace_options
[i
]; i
++) {
1714 int len
= strlen(trace_options
[i
]);
1716 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
1718 trace_flags
&= ~(1 << i
);
1720 trace_flags
|= (1 << i
);
1730 static struct file_operations tracing_iter_fops
= {
1731 .open
= tracing_open_generic
,
1732 .read
= tracing_iter_ctrl_read
,
1733 .write
= tracing_iter_ctrl_write
,
1736 static const char readme_msg
[] =
1737 "tracing mini-HOWTO:\n\n"
1739 "# mount -t debugfs nodev /debug\n\n"
1740 "# cat /debug/tracing/available_tracers\n"
1741 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1742 "# cat /debug/tracing/current_tracer\n"
1744 "# echo sched_switch > /debug/tracing/current_tracer\n"
1745 "# cat /debug/tracing/current_tracer\n"
1747 "# cat /debug/tracing/iter_ctrl\n"
1748 "noprint-parent nosym-offset nosym-addr noverbose\n"
1749 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1750 "# echo 1 > /debug/tracing/tracing_enabled\n"
1751 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1752 "echo 0 > /debug/tracing/tracing_enabled\n"
1756 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
1757 size_t cnt
, loff_t
*ppos
)
1759 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1760 readme_msg
, strlen(readme_msg
));
1763 static struct file_operations tracing_readme_fops
= {
1764 .open
= tracing_open_generic
,
1765 .read
= tracing_readme_read
,
1770 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1771 size_t cnt
, loff_t
*ppos
)
1773 struct trace_array
*tr
= filp
->private_data
;
1777 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
1778 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1782 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1783 size_t cnt
, loff_t
*ppos
)
1785 struct trace_array
*tr
= filp
->private_data
;
1792 if (copy_from_user(&buf
, ubuf
, cnt
))
1797 val
= simple_strtoul(buf
, NULL
, 10);
1801 mutex_lock(&trace_types_lock
);
1802 if (tr
->ctrl
^ val
) {
1810 if (current_trace
&& current_trace
->ctrl_update
)
1811 current_trace
->ctrl_update(tr
);
1813 mutex_unlock(&trace_types_lock
);
1821 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
1822 size_t cnt
, loff_t
*ppos
)
1824 char buf
[max_tracer_type_len
+2];
1827 mutex_lock(&trace_types_lock
);
1829 r
= sprintf(buf
, "%s\n", current_trace
->name
);
1831 r
= sprintf(buf
, "\n");
1832 mutex_unlock(&trace_types_lock
);
1834 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1838 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
1839 size_t cnt
, loff_t
*ppos
)
1841 struct trace_array
*tr
= &global_trace
;
1843 char buf
[max_tracer_type_len
+1];
1846 if (cnt
> max_tracer_type_len
)
1847 cnt
= max_tracer_type_len
;
1849 if (copy_from_user(&buf
, ubuf
, cnt
))
1854 /* strip ending whitespace. */
1855 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
1858 mutex_lock(&trace_types_lock
);
1859 for (t
= trace_types
; t
; t
= t
->next
) {
1860 if (strcmp(t
->name
, buf
) == 0)
1863 if (!t
|| t
== current_trace
)
1866 if (current_trace
&& current_trace
->reset
)
1867 current_trace
->reset(tr
);
1874 mutex_unlock(&trace_types_lock
);
1882 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
1883 size_t cnt
, loff_t
*ppos
)
1885 unsigned long *ptr
= filp
->private_data
;
1889 r
= snprintf(buf
, 64, "%ld\n",
1890 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
1893 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1897 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
1898 size_t cnt
, loff_t
*ppos
)
1900 long *ptr
= filp
->private_data
;
1907 if (copy_from_user(&buf
, ubuf
, cnt
))
1912 val
= simple_strtoul(buf
, NULL
, 10);
1919 static atomic_t tracing_reader
;
1921 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
1923 struct trace_iterator
*iter
;
1925 if (tracing_disabled
)
1928 /* We only allow for reader of the pipe */
1929 if (atomic_inc_return(&tracing_reader
) != 1) {
1930 atomic_dec(&tracing_reader
);
1934 /* create a buffer to store the information to pass to userspace */
1935 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1939 iter
->tr
= &global_trace
;
1941 filp
->private_data
= iter
;
1946 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
1948 struct trace_iterator
*iter
= file
->private_data
;
1951 atomic_dec(&tracing_reader
);
1960 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
1961 size_t cnt
, loff_t
*ppos
)
1963 struct trace_iterator
*iter
= filp
->private_data
;
1964 struct trace_array_cpu
*data
;
1965 static cpumask_t mask
;
1967 unsigned long flags
;
1968 #ifdef CONFIG_FTRACE
1976 /* return any leftover data */
1977 if (iter
->seq
.len
> start
) {
1978 len
= iter
->seq
.len
- start
;
1981 ret
= copy_to_user(ubuf
, iter
->seq
.buffer
+ start
, cnt
);
1990 trace_seq_reset(&iter
->seq
);
1993 while (trace_empty(iter
)) {
1995 * This is a make-shift waitqueue. The reason we don't use
1996 * an actual wait queue is because:
1997 * 1) we only ever have one waiter
1998 * 2) the tracing, traces all functions, we don't want
1999 * the overhead of calling wake_up and friends
2000 * (and tracing them too)
2001 * Anyway, this is really very primitive wakeup.
2003 set_current_state(TASK_INTERRUPTIBLE
);
2004 iter
->tr
->waiter
= current
;
2006 /* sleep for one second, and try again. */
2007 schedule_timeout(HZ
);
2009 iter
->tr
->waiter
= NULL
;
2011 if (signal_pending(current
))
2015 * We block until we read something and tracing is disabled.
2016 * We still block if tracing is disabled, but we have never
2017 * read anything. This allows a user to cat this file, and
2018 * then enable tracing. But after we have read something,
2019 * we give an EOF when tracing is again disabled.
2021 * iter->pos will be 0 if we haven't read anything.
2023 if (!tracer_enabled
&& iter
->pos
)
2029 /* stop when tracing is finished */
2030 if (trace_empty(iter
))
2033 if (cnt
>= PAGE_SIZE
)
2034 cnt
= PAGE_SIZE
- 1;
2036 memset(iter
, 0, sizeof(*iter
));
2037 iter
->tr
= &global_trace
;
2041 * We need to stop all tracing on all CPUS to read the
2042 * the next buffer. This is a bit expensive, but is
2043 * not done often. We fill all what we can read,
2044 * and then release the locks again.
2048 local_irq_save(flags
);
2049 #ifdef CONFIG_FTRACE
2050 ftrace_save
= ftrace_enabled
;
2054 for_each_possible_cpu(cpu
) {
2055 data
= iter
->tr
->data
[cpu
];
2057 if (!head_page(data
) || !data
->trace_idx
)
2060 atomic_inc(&data
->disabled
);
2064 for_each_cpu_mask(cpu
, mask
) {
2065 data
= iter
->tr
->data
[cpu
];
2066 spin_lock(&data
->lock
);
2069 while (find_next_entry_inc(iter
) != NULL
) {
2070 int len
= iter
->seq
.len
;
2072 ret
= print_trace_line(iter
);
2074 /* don't print partial lines */
2075 iter
->seq
.len
= len
;
2079 trace_consume(iter
);
2081 if (iter
->seq
.len
>= cnt
)
2085 for_each_cpu_mask(cpu
, mask
) {
2086 data
= iter
->tr
->data
[cpu
];
2087 spin_unlock(&data
->lock
);
2090 for_each_cpu_mask(cpu
, mask
) {
2091 data
= iter
->tr
->data
[cpu
];
2092 atomic_dec(&data
->disabled
);
2094 #ifdef CONFIG_FTRACE
2095 ftrace_enabled
= ftrace_save
;
2097 local_irq_restore(flags
);
2099 /* Now copy what we have to the user */
2100 read
= iter
->seq
.len
;
2104 ret
= copy_to_user(ubuf
, iter
->seq
.buffer
, read
);
2106 if (read
< iter
->seq
.len
)
2109 trace_seq_reset(&iter
->seq
);
2117 static struct file_operations tracing_max_lat_fops
= {
2118 .open
= tracing_open_generic
,
2119 .read
= tracing_max_lat_read
,
2120 .write
= tracing_max_lat_write
,
2123 static struct file_operations tracing_ctrl_fops
= {
2124 .open
= tracing_open_generic
,
2125 .read
= tracing_ctrl_read
,
2126 .write
= tracing_ctrl_write
,
2129 static struct file_operations set_tracer_fops
= {
2130 .open
= tracing_open_generic
,
2131 .read
= tracing_set_trace_read
,
2132 .write
= tracing_set_trace_write
,
2135 static struct file_operations tracing_pipe_fops
= {
2136 .open
= tracing_open_pipe
,
2137 .read
= tracing_read_pipe
,
2138 .release
= tracing_release_pipe
,
2141 #ifdef CONFIG_DYNAMIC_FTRACE
2144 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
2145 size_t cnt
, loff_t
*ppos
)
2147 unsigned long *p
= filp
->private_data
;
2151 r
= sprintf(buf
, "%ld\n", *p
);
2153 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2156 static struct file_operations tracing_read_long_fops
= {
2157 .open
= tracing_open_generic
,
2158 .read
= tracing_read_long
,
2162 static struct dentry
*d_tracer
;
2164 struct dentry
*tracing_init_dentry(void)
2171 d_tracer
= debugfs_create_dir("tracing", NULL
);
2173 if (!d_tracer
&& !once
) {
2175 pr_warning("Could not create debugfs directory 'tracing'\n");
2182 #ifdef CONFIG_FTRACE_SELFTEST
2183 /* Let selftest have access to static functions in this file */
2184 #include "trace_selftest.c"
2187 static __init
void tracer_init_debugfs(void)
2189 struct dentry
*d_tracer
;
2190 struct dentry
*entry
;
2192 d_tracer
= tracing_init_dentry();
2194 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
2195 &global_trace
, &tracing_ctrl_fops
);
2197 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2199 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
2200 NULL
, &tracing_iter_fops
);
2202 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2204 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2205 &global_trace
, &tracing_lt_fops
);
2207 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2209 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2210 &global_trace
, &tracing_fops
);
2212 pr_warning("Could not create debugfs 'trace' entry\n");
2214 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2215 &global_trace
, &show_traces_fops
);
2217 pr_warning("Could not create debugfs 'trace' entry\n");
2219 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2220 &global_trace
, &set_tracer_fops
);
2222 pr_warning("Could not create debugfs 'trace' entry\n");
2224 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2225 &tracing_max_latency
,
2226 &tracing_max_lat_fops
);
2228 pr_warning("Could not create debugfs "
2229 "'tracing_max_latency' entry\n");
2231 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2232 &tracing_thresh
, &tracing_max_lat_fops
);
2234 pr_warning("Could not create debugfs "
2235 "'tracing_threash' entry\n");
2236 entry
= debugfs_create_file("README", 0644, d_tracer
,
2237 NULL
, &tracing_readme_fops
);
2239 pr_warning("Could not create debugfs 'README' entry\n");
2241 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2242 NULL
, &tracing_pipe_fops
);
2244 pr_warning("Could not create debugfs "
2245 "'tracing_threash' entry\n");
2247 #ifdef CONFIG_DYNAMIC_FTRACE
2248 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
2249 &ftrace_update_tot_cnt
,
2250 &tracing_read_long_fops
);
2252 pr_warning("Could not create debugfs "
2253 "'dyn_ftrace_total_info' entry\n");
2257 /* dummy trace to disable tracing */
2258 static struct tracer no_tracer __read_mostly
=
2263 static int trace_alloc_page(void)
2265 struct trace_array_cpu
*data
;
2266 struct page
*page
, *tmp
;
2271 /* first allocate a page for each CPU */
2272 for_each_possible_cpu(i
) {
2273 array
= (void *)__get_free_page(GFP_KERNEL
);
2274 if (array
== NULL
) {
2275 printk(KERN_ERR
"tracer: failed to allocate page"
2276 "for trace buffer!\n");
2280 page
= virt_to_page(array
);
2281 list_add(&page
->lru
, &pages
);
2283 /* Only allocate if we are actually using the max trace */
2284 #ifdef CONFIG_TRACER_MAX_TRACE
2285 array
= (void *)__get_free_page(GFP_KERNEL
);
2286 if (array
== NULL
) {
2287 printk(KERN_ERR
"tracer: failed to allocate page"
2288 "for trace buffer!\n");
2291 page
= virt_to_page(array
);
2292 list_add(&page
->lru
, &pages
);
2296 /* Now that we successfully allocate a page per CPU, add them */
2297 for_each_possible_cpu(i
) {
2298 data
= global_trace
.data
[i
];
2299 spin_lock_init(&data
->lock
);
2300 lockdep_set_class(&data
->lock
, &data
->lock_key
);
2301 page
= list_entry(pages
.next
, struct page
, lru
);
2302 list_del_init(&page
->lru
);
2303 list_add_tail(&page
->lru
, &data
->trace_pages
);
2306 #ifdef CONFIG_TRACER_MAX_TRACE
2307 data
= max_tr
.data
[i
];
2308 spin_lock_init(&data
->lock
);
2309 lockdep_set_class(&data
->lock
, &data
->lock_key
);
2310 page
= list_entry(pages
.next
, struct page
, lru
);
2311 list_del_init(&page
->lru
);
2312 list_add_tail(&page
->lru
, &data
->trace_pages
);
2316 global_trace
.entries
+= ENTRIES_PER_PAGE
;
2321 list_for_each_entry_safe(page
, tmp
, &pages
, lru
) {
2322 list_del_init(&page
->lru
);
2328 __init
static int tracer_alloc_buffers(void)
2330 struct trace_array_cpu
*data
;
2337 global_trace
.ctrl
= tracer_enabled
;
2339 /* Allocate the first page for all buffers */
2340 for_each_possible_cpu(i
) {
2341 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
2342 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
2344 array
= (void *)__get_free_page(GFP_KERNEL
);
2345 if (array
== NULL
) {
2346 printk(KERN_ERR
"tracer: failed to allocate page"
2347 "for trace buffer!\n");
2351 /* set the array to the list */
2352 INIT_LIST_HEAD(&data
->trace_pages
);
2353 page
= virt_to_page(array
);
2354 list_add(&page
->lru
, &data
->trace_pages
);
2355 /* use the LRU flag to differentiate the two buffers */
2358 /* Only allocate if we are actually using the max trace */
2359 #ifdef CONFIG_TRACER_MAX_TRACE
2360 array
= (void *)__get_free_page(GFP_KERNEL
);
2361 if (array
== NULL
) {
2362 printk(KERN_ERR
"tracer: failed to allocate page"
2363 "for trace buffer!\n");
2367 INIT_LIST_HEAD(&max_tr
.data
[i
]->trace_pages
);
2368 page
= virt_to_page(array
);
2369 list_add(&page
->lru
, &max_tr
.data
[i
]->trace_pages
);
2375 * Since we allocate by orders of pages, we may be able to
2378 global_trace
.entries
= ENTRIES_PER_PAGE
;
2381 while (global_trace
.entries
< trace_nr_entries
) {
2382 if (trace_alloc_page())
2386 max_tr
.entries
= global_trace
.entries
;
2388 pr_info("tracer: %d pages allocated for %ld",
2389 pages
, trace_nr_entries
);
2390 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE
);
2391 pr_info(" actual entries %ld\n", global_trace
.entries
);
2393 tracer_init_debugfs();
2395 trace_init_cmdlines();
2397 register_tracer(&no_tracer
);
2398 current_trace
= &no_tracer
;
2400 /* All seems OK, enable tracing */
2401 tracing_disabled
= 0;
2406 for (i
-- ; i
>= 0; i
--) {
2407 struct page
*page
, *tmp
;
2408 struct trace_array_cpu
*data
= global_trace
.data
[i
];
2411 list_for_each_entry_safe(page
, tmp
,
2412 &data
->trace_pages
, lru
) {
2413 list_del_init(&page
->lru
);
2418 #ifdef CONFIG_TRACER_MAX_TRACE
2419 data
= max_tr
.data
[i
];
2421 list_for_each_entry_safe(page
, tmp
,
2422 &data
->trace_pages
, lru
) {
2423 list_del_init(&page
->lru
);
2431 fs_initcall(tracer_alloc_buffers
);