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 const int time_sync_freq_max
= 128;
46 static const cycle_t time_sync_thresh
= 100000;
48 static DEFINE_PER_CPU(cycle_t
, time_offset
);
49 static DEFINE_PER_CPU(cycle_t
, prev_cpu_time
);
50 static DEFINE_PER_CPU(int, time_sync_count
);
51 static DEFINE_PER_CPU(int, time_sync_freq
);
54 * Global lock which we take every now and then to synchronize
55 * the CPUs time. This method is not warp-safe, but it's good
56 * enough to synchronize slowly diverging time sources and thus
57 * it's good enough for tracing:
59 static DEFINE_SPINLOCK(time_sync_lock
);
60 static cycle_t prev_global_time
;
62 static notrace cycle_t
__ftrace_now_sync(cycles_t time
, int cpu
)
66 spin_lock_irqsave(&time_sync_lock
, flags
);
69 * Update the synchronization frequency:
71 if (per_cpu(time_sync_freq
, cpu
) < time_sync_freq_max
)
72 per_cpu(time_sync_freq
, cpu
) *= 2;
73 per_cpu(time_sync_count
, cpu
) = per_cpu(time_sync_freq
, cpu
);
75 if (time
< prev_global_time
) {
76 per_cpu(time_offset
, cpu
) += prev_global_time
- time
;
77 time
= prev_global_time
;
79 prev_global_time
= time
;
82 spin_unlock_irqrestore(&time_sync_lock
, flags
);
87 notrace cycle_t
ftrace_now(int cpu
)
89 cycle_t prev_cpu_time
, time
, delta_time
;
91 prev_cpu_time
= per_cpu(prev_cpu_time
, cpu
);
92 time
= sched_clock() + per_cpu(time_offset
, cpu
);
93 delta_time
= time
-prev_cpu_time
;
95 if (unlikely(delta_time
> time_sync_thresh
||
96 --per_cpu(time_sync_count
, cpu
) <= 0))
97 time
= __ftrace_now_sync(time
, cpu
);
102 static struct trace_array global_trace
;
104 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
106 static struct trace_array max_tr
;
108 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
110 static int tracer_enabled
;
111 static unsigned long trace_nr_entries
= 16384UL;
113 static struct tracer
*trace_types __read_mostly
;
114 static struct tracer
*current_trace __read_mostly
;
115 static int max_tracer_type_len
;
117 static DEFINE_MUTEX(trace_types_lock
);
119 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
121 static int __init
set_nr_entries(char *str
)
125 trace_nr_entries
= simple_strtoul(str
, &str
, 0);
128 __setup("trace_entries=", set_nr_entries
);
130 unsigned long nsecs_to_usecs(unsigned long nsecs
)
136 __TRACE_FIRST_TYPE
= 0,
144 enum trace_flag_type
{
145 TRACE_FLAG_IRQS_OFF
= 0x01,
146 TRACE_FLAG_NEED_RESCHED
= 0x02,
147 TRACE_FLAG_HARDIRQ
= 0x04,
148 TRACE_FLAG_SOFTIRQ
= 0x08,
151 enum trace_iterator_flags
{
152 TRACE_ITER_PRINT_PARENT
= 0x01,
153 TRACE_ITER_SYM_OFFSET
= 0x02,
154 TRACE_ITER_SYM_ADDR
= 0x04,
155 TRACE_ITER_VERBOSE
= 0x08,
158 #define TRACE_ITER_SYM_MASK \
159 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
161 /* These must match the bit postions above */
162 static const char *trace_options
[] = {
170 static unsigned trace_flags
;
172 static DEFINE_SPINLOCK(ftrace_max_lock
);
175 * Copy the new maximum trace into the separate maximum-trace
176 * structure. (this way the maximum trace is permanently saved,
177 * for later retrieval via /debugfs/tracing/latency_trace)
180 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
182 struct trace_array_cpu
*data
= tr
->data
[cpu
];
185 max_tr
.time_start
= data
->preempt_timestamp
;
187 data
= max_tr
.data
[cpu
];
188 data
->saved_latency
= tracing_max_latency
;
190 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
191 data
->pid
= tsk
->pid
;
192 data
->uid
= tsk
->uid
;
193 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
194 data
->policy
= tsk
->policy
;
195 data
->rt_priority
= tsk
->rt_priority
;
197 /* record this tasks comm */
198 tracing_record_cmdline(current
);
201 void check_pages(struct trace_array_cpu
*data
)
203 struct page
*page
, *tmp
;
205 BUG_ON(data
->trace_pages
.next
->prev
!= &data
->trace_pages
);
206 BUG_ON(data
->trace_pages
.prev
->next
!= &data
->trace_pages
);
208 list_for_each_entry_safe(page
, tmp
, &data
->trace_pages
, lru
) {
209 BUG_ON(page
->lru
.next
->prev
!= &page
->lru
);
210 BUG_ON(page
->lru
.prev
->next
!= &page
->lru
);
214 void *head_page(struct trace_array_cpu
*data
)
219 if (list_empty(&data
->trace_pages
))
222 page
= list_entry(data
->trace_pages
.next
, struct page
, lru
);
223 BUG_ON(&page
->lru
== &data
->trace_pages
);
225 return page_address(page
);
229 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
231 int len
= (PAGE_SIZE
- 1) - s
->len
;
239 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
242 /* If we can't write it all, don't bother writing anything */
252 trace_seq_puts(struct trace_seq
*s
, const char *str
)
254 int len
= strlen(str
);
256 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
259 memcpy(s
->buffer
+ s
->len
, str
, len
);
266 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
268 if (s
->len
>= (PAGE_SIZE
- 1))
271 s
->buffer
[s
->len
++] = c
;
277 trace_seq_reset(struct trace_seq
*s
)
283 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
285 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
288 seq_puts(m
, s
->buffer
);
294 flip_trace(struct trace_array_cpu
*tr1
, struct trace_array_cpu
*tr2
)
296 struct list_head flip_pages
;
298 INIT_LIST_HEAD(&flip_pages
);
300 memcpy(&tr1
->trace_head_idx
, &tr2
->trace_head_idx
,
301 sizeof(struct trace_array_cpu
) -
302 offsetof(struct trace_array_cpu
, trace_head_idx
));
306 list_splice_init(&tr1
->trace_pages
, &flip_pages
);
307 list_splice_init(&tr2
->trace_pages
, &tr1
->trace_pages
);
308 list_splice_init(&flip_pages
, &tr2
->trace_pages
);
309 BUG_ON(!list_empty(&flip_pages
));
315 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
317 struct trace_array_cpu
*data
;
320 WARN_ON_ONCE(!irqs_disabled());
321 spin_lock(&ftrace_max_lock
);
322 /* clear out all the previous traces */
323 for_each_possible_cpu(i
) {
325 flip_trace(max_tr
.data
[i
], data
);
329 __update_max_tr(tr
, tsk
, cpu
);
330 spin_unlock(&ftrace_max_lock
);
334 * update_max_tr_single - only copy one trace over, and reset the rest
336 * @tsk - task with the latency
337 * @cpu - the cpu of the buffer to copy.
340 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
342 struct trace_array_cpu
*data
= tr
->data
[cpu
];
345 WARN_ON_ONCE(!irqs_disabled());
346 spin_lock(&ftrace_max_lock
);
347 for_each_possible_cpu(i
)
348 tracing_reset(max_tr
.data
[i
]);
350 flip_trace(max_tr
.data
[cpu
], data
);
353 __update_max_tr(tr
, tsk
, cpu
);
354 spin_unlock(&ftrace_max_lock
);
357 int register_tracer(struct tracer
*type
)
364 pr_info("Tracer must have a name\n");
368 mutex_lock(&trace_types_lock
);
369 for (t
= trace_types
; t
; t
= t
->next
) {
370 if (strcmp(type
->name
, t
->name
) == 0) {
372 pr_info("Trace %s already registered\n",
379 #ifdef CONFIG_FTRACE_STARTUP_TEST
380 if (type
->selftest
) {
381 struct tracer
*saved_tracer
= current_trace
;
382 struct trace_array_cpu
*data
;
383 struct trace_array
*tr
= &global_trace
;
384 int saved_ctrl
= tr
->ctrl
;
387 * Run a selftest on this tracer.
388 * Here we reset the trace buffer, and set the current
389 * tracer to be this tracer. The tracer can then run some
390 * internal tracing to verify that everything is in order.
391 * If we fail, we do not register this tracer.
393 for_each_possible_cpu(i
) {
395 if (!head_page(data
))
399 current_trace
= type
;
401 /* the test is responsible for initializing and enabling */
402 pr_info("Testing tracer %s: ", type
->name
);
403 ret
= type
->selftest(type
, tr
);
404 /* the test is responsible for resetting too */
405 current_trace
= saved_tracer
;
406 tr
->ctrl
= saved_ctrl
;
408 printk(KERN_CONT
"FAILED!\n");
411 /* Only reset on passing, to avoid touching corrupted buffers */
412 for_each_possible_cpu(i
) {
414 if (!head_page(data
))
418 printk(KERN_CONT
"PASSED\n");
422 type
->next
= trace_types
;
424 len
= strlen(type
->name
);
425 if (len
> max_tracer_type_len
)
426 max_tracer_type_len
= len
;
429 mutex_unlock(&trace_types_lock
);
434 void unregister_tracer(struct tracer
*type
)
439 mutex_lock(&trace_types_lock
);
440 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
444 pr_info("Trace %s not registered\n", type
->name
);
449 if (strlen(type
->name
) != max_tracer_type_len
)
452 max_tracer_type_len
= 0;
453 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
454 len
= strlen((*t
)->name
);
455 if (len
> max_tracer_type_len
)
456 max_tracer_type_len
= len
;
459 mutex_unlock(&trace_types_lock
);
462 notrace
void tracing_reset(struct trace_array_cpu
*data
)
465 data
->trace_head
= data
->trace_tail
= head_page(data
);
466 data
->trace_head_idx
= 0;
467 data
->trace_tail_idx
= 0;
472 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
474 struct trace_array
*tr
= &global_trace
;
475 struct trace_array_cpu
*data
;
480 if (unlikely(!tracer_enabled
))
483 local_irq_save(flags
);
484 cpu
= raw_smp_processor_id();
485 data
= tr
->data
[cpu
];
486 disabled
= atomic_inc_return(&data
->disabled
);
488 if (likely(disabled
== 1))
489 ftrace(tr
, data
, ip
, parent_ip
, flags
);
491 atomic_dec(&data
->disabled
);
492 local_irq_restore(flags
);
495 static struct ftrace_ops trace_ops __read_mostly
=
497 .func
= function_trace_call
,
501 notrace
void tracing_start_function_trace(void)
503 register_ftrace_function(&trace_ops
);
506 notrace
void tracing_stop_function_trace(void)
508 unregister_ftrace_function(&trace_ops
);
511 #define SAVED_CMDLINES 128
512 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
513 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
514 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
515 static int cmdline_idx
;
516 static DEFINE_SPINLOCK(trace_cmdline_lock
);
517 atomic_t trace_record_cmdline_disabled
;
519 static void trace_init_cmdlines(void)
521 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
522 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
526 notrace
void trace_stop_cmdline_recording(void);
528 static notrace
void trace_save_cmdline(struct task_struct
*tsk
)
533 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
537 * It's not the end of the world if we don't get
538 * the lock, but we also don't want to spin
539 * nor do we want to disable interrupts,
540 * so if we miss here, then better luck next time.
542 if (!spin_trylock(&trace_cmdline_lock
))
545 idx
= map_pid_to_cmdline
[tsk
->pid
];
546 if (idx
>= SAVED_CMDLINES
) {
547 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
549 map
= map_cmdline_to_pid
[idx
];
550 if (map
<= PID_MAX_DEFAULT
)
551 map_pid_to_cmdline
[map
] = (unsigned)-1;
553 map_pid_to_cmdline
[tsk
->pid
] = idx
;
558 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
560 spin_unlock(&trace_cmdline_lock
);
563 static notrace
char *trace_find_cmdline(int pid
)
565 char *cmdline
= "<...>";
571 if (pid
> PID_MAX_DEFAULT
)
574 map
= map_pid_to_cmdline
[pid
];
575 if (map
>= SAVED_CMDLINES
)
578 cmdline
= saved_cmdlines
[map
];
584 notrace
void tracing_record_cmdline(struct task_struct
*tsk
)
586 if (atomic_read(&trace_record_cmdline_disabled
))
589 trace_save_cmdline(tsk
);
592 static inline notrace
struct list_head
*
593 trace_next_list(struct trace_array_cpu
*data
, struct list_head
*next
)
596 * Roundrobin - but skip the head (which is not a real page):
599 if (unlikely(next
== &data
->trace_pages
))
601 BUG_ON(next
== &data
->trace_pages
);
606 static inline notrace
void *
607 trace_next_page(struct trace_array_cpu
*data
, void *addr
)
609 struct list_head
*next
;
612 page
= virt_to_page(addr
);
614 next
= trace_next_list(data
, &page
->lru
);
615 page
= list_entry(next
, struct page
, lru
);
617 return page_address(page
);
620 static inline notrace
struct trace_entry
*
621 tracing_get_trace_entry(struct trace_array
*tr
, struct trace_array_cpu
*data
)
623 unsigned long idx
, idx_next
;
624 struct trace_entry
*entry
;
627 idx
= data
->trace_head_idx
;
630 BUG_ON(idx
* TRACE_ENTRY_SIZE
>= PAGE_SIZE
);
632 entry
= data
->trace_head
+ idx
* TRACE_ENTRY_SIZE
;
634 if (unlikely(idx_next
>= ENTRIES_PER_PAGE
)) {
635 data
->trace_head
= trace_next_page(data
, data
->trace_head
);
639 if (data
->trace_head
== data
->trace_tail
&&
640 idx_next
== data
->trace_tail_idx
) {
642 data
->trace_tail_idx
++;
643 if (data
->trace_tail_idx
>= ENTRIES_PER_PAGE
) {
645 trace_next_page(data
, data
->trace_tail
);
646 data
->trace_tail_idx
= 0;
650 data
->trace_head_idx
= idx_next
;
655 static inline notrace
void
656 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
)
658 struct task_struct
*tsk
= current
;
661 pc
= preempt_count();
663 entry
->preempt_count
= pc
& 0xff;
664 entry
->pid
= tsk
->pid
;
665 entry
->t
= ftrace_now(raw_smp_processor_id());
666 entry
->flags
= (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
667 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
668 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
669 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
673 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
674 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
)
676 struct trace_entry
*entry
;
678 spin_lock(&data
->lock
);
679 entry
= tracing_get_trace_entry(tr
, data
);
680 tracing_generic_entry_update(entry
, flags
);
681 entry
->type
= TRACE_FN
;
683 entry
->fn
.parent_ip
= parent_ip
;
684 spin_unlock(&data
->lock
);
688 tracing_sched_switch_trace(struct trace_array
*tr
,
689 struct trace_array_cpu
*data
,
690 struct task_struct
*prev
, struct task_struct
*next
,
693 struct trace_entry
*entry
;
695 spin_lock(&data
->lock
);
696 entry
= tracing_get_trace_entry(tr
, data
);
697 tracing_generic_entry_update(entry
, flags
);
698 entry
->type
= TRACE_CTX
;
699 entry
->ctx
.prev_pid
= prev
->pid
;
700 entry
->ctx
.prev_prio
= prev
->prio
;
701 entry
->ctx
.prev_state
= prev
->state
;
702 entry
->ctx
.next_pid
= next
->pid
;
703 entry
->ctx
.next_prio
= next
->prio
;
704 spin_unlock(&data
->lock
);
707 enum trace_file_type
{
708 TRACE_FILE_LAT_FMT
= 1,
711 static struct trace_entry
*
712 trace_entry_idx(struct trace_array
*tr
, struct trace_array_cpu
*data
,
713 struct trace_iterator
*iter
, int cpu
)
716 struct trace_entry
*array
;
718 if (iter
->next_idx
[cpu
] >= tr
->entries
||
719 iter
->next_idx
[cpu
] >= data
->trace_idx
||
720 (data
->trace_head
== data
->trace_tail
&&
721 data
->trace_head_idx
== data
->trace_tail_idx
))
724 if (!iter
->next_page
[cpu
]) {
725 /* Initialize the iterator for this cpu trace buffer */
726 WARN_ON(!data
->trace_tail
);
727 page
= virt_to_page(data
->trace_tail
);
728 iter
->next_page
[cpu
] = &page
->lru
;
729 iter
->next_page_idx
[cpu
] = data
->trace_tail_idx
;
732 page
= list_entry(iter
->next_page
[cpu
], struct page
, lru
);
733 BUG_ON(&data
->trace_pages
== &page
->lru
);
735 array
= page_address(page
);
737 /* Still possible to catch up to the tail */
738 if (iter
->next_idx
[cpu
] && array
== data
->trace_tail
&&
739 iter
->next_page_idx
[cpu
] == data
->trace_tail_idx
)
742 WARN_ON(iter
->next_page_idx
[cpu
] >= ENTRIES_PER_PAGE
);
743 return &array
[iter
->next_page_idx
[cpu
]];
746 static struct notrace trace_entry
*
747 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
)
749 struct trace_array
*tr
= iter
->tr
;
750 struct trace_entry
*ent
, *next
= NULL
;
754 for_each_possible_cpu(cpu
) {
755 if (!head_page(tr
->data
[cpu
]))
757 ent
= trace_entry_idx(tr
, tr
->data
[cpu
], iter
, cpu
);
759 * Pick the entry with the smallest timestamp:
761 if (ent
&& (!next
|| ent
->t
< next
->t
)) {
774 trace_iterator_increment(struct trace_iterator
*iter
)
777 iter
->next_idx
[iter
->cpu
]++;
778 iter
->next_page_idx
[iter
->cpu
]++;
779 if (iter
->next_page_idx
[iter
->cpu
] >= ENTRIES_PER_PAGE
) {
780 struct trace_array_cpu
*data
= iter
->tr
->data
[iter
->cpu
];
782 iter
->next_page_idx
[iter
->cpu
] = 0;
783 iter
->next_page
[iter
->cpu
] =
784 trace_next_list(data
, iter
->next_page
[iter
->cpu
]);
789 trace_consume(struct trace_iterator
*iter
)
791 struct trace_array_cpu
*data
= iter
->tr
->data
[iter
->cpu
];
793 data
->trace_tail_idx
++;
794 if (data
->trace_tail_idx
>= ENTRIES_PER_PAGE
) {
795 data
->trace_tail
= trace_next_page(data
, data
->trace_tail
);
796 data
->trace_tail_idx
= 0;
799 /* Check if we empty it, then reset the index */
800 if (data
->trace_head
== data
->trace_tail
&&
801 data
->trace_head_idx
== data
->trace_tail_idx
)
805 static notrace
void *
806 find_next_entry_inc(struct trace_iterator
*iter
)
808 struct trace_entry
*next
;
811 next
= find_next_entry(iter
, &next_cpu
);
813 iter
->prev_ent
= iter
->ent
;
814 iter
->prev_cpu
= iter
->cpu
;
817 iter
->cpu
= next_cpu
;
820 trace_iterator_increment(iter
);
822 return next
? iter
: NULL
;
825 static notrace
void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
827 struct trace_iterator
*iter
= m
->private;
828 void *last_ent
= iter
->ent
;
834 /* can't go backwards */
839 ent
= find_next_entry_inc(iter
);
843 while (ent
&& iter
->idx
< i
)
844 ent
= find_next_entry_inc(iter
);
848 if (last_ent
&& !ent
)
849 seq_puts(m
, "\n\nvim:ft=help\n");
854 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
856 struct trace_iterator
*iter
= m
->private;
861 mutex_lock(&trace_types_lock
);
863 if (!current_trace
|| current_trace
!= iter
->trace
)
866 atomic_inc(&trace_record_cmdline_disabled
);
868 /* let the tracer grab locks here if needed */
869 if (current_trace
->start
)
870 current_trace
->start(iter
);
872 if (*pos
!= iter
->pos
) {
876 iter
->prev_ent
= NULL
;
879 for_each_possible_cpu(i
) {
880 iter
->next_idx
[i
] = 0;
881 iter
->next_page
[i
] = NULL
;
884 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
889 p
= s_next(m
, p
, &l
);
895 static void s_stop(struct seq_file
*m
, void *p
)
897 struct trace_iterator
*iter
= m
->private;
899 atomic_dec(&trace_record_cmdline_disabled
);
901 /* let the tracer release locks here if needed */
902 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
903 iter
->trace
->stop(iter
);
905 mutex_unlock(&trace_types_lock
);
909 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
911 #ifdef CONFIG_KALLSYMS
912 char str
[KSYM_SYMBOL_LEN
];
914 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
916 return trace_seq_printf(s
, fmt
, str
);
922 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
923 unsigned long address
)
925 #ifdef CONFIG_KALLSYMS
926 char str
[KSYM_SYMBOL_LEN
];
928 sprint_symbol(str
, address
);
929 return trace_seq_printf(s
, fmt
, str
);
935 # define IP_FMT "%08lx"
937 # define IP_FMT "%016lx"
941 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
946 return trace_seq_printf(s
, "0");
948 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
949 ret
= seq_print_sym_offset(s
, "%s", ip
);
951 ret
= seq_print_sym_short(s
, "%s", ip
);
956 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
957 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
961 static notrace
void print_lat_help_header(struct seq_file
*m
)
963 seq_puts(m
, "# _------=> CPU# \n");
964 seq_puts(m
, "# / _-----=> irqs-off \n");
965 seq_puts(m
, "# | / _----=> need-resched \n");
966 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
967 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
968 seq_puts(m
, "# |||| / \n");
969 seq_puts(m
, "# ||||| delay \n");
970 seq_puts(m
, "# cmd pid ||||| time | caller \n");
971 seq_puts(m
, "# \\ / ||||| \\ | / \n");
974 static notrace
void print_func_help_header(struct seq_file
*m
)
976 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
977 seq_puts(m
, "# | | | | |\n");
982 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
984 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
985 struct trace_array
*tr
= iter
->tr
;
986 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
987 struct tracer
*type
= current_trace
;
988 unsigned long total
= 0;
989 unsigned long entries
= 0;
991 const char *name
= "preemption";
996 for_each_possible_cpu(cpu
) {
997 if (head_page(tr
->data
[cpu
])) {
998 total
+= tr
->data
[cpu
]->trace_idx
;
999 if (tr
->data
[cpu
]->trace_idx
> tr
->entries
)
1000 entries
+= tr
->entries
;
1002 entries
+= tr
->data
[cpu
]->trace_idx
;
1006 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1008 seq_puts(m
, "-----------------------------------"
1009 "---------------------------------\n");
1010 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1011 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1012 nsecs_to_usecs(data
->saved_latency
),
1016 #if defined(CONFIG_PREEMPT_NONE)
1018 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1020 #elif defined(CONFIG_PREEMPT_DESKTOP)
1025 /* These are reserved for later use */
1028 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1032 seq_puts(m
, " -----------------\n");
1033 seq_printf(m
, " | task: %.16s-%d "
1034 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1035 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1036 data
->policy
, data
->rt_priority
);
1037 seq_puts(m
, " -----------------\n");
1039 if (data
->critical_start
) {
1040 seq_puts(m
, " => started at: ");
1041 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1042 trace_print_seq(m
, &iter
->seq
);
1043 seq_puts(m
, "\n => ended at: ");
1044 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1045 trace_print_seq(m
, &iter
->seq
);
1053 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1055 int hardirq
, softirq
;
1058 comm
= trace_find_cmdline(entry
->pid
);
1060 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1061 trace_seq_printf(s
, "%d", cpu
);
1062 trace_seq_printf(s
, "%c%c",
1063 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
1064 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1066 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1067 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1068 if (hardirq
&& softirq
)
1069 trace_seq_putc(s
, 'H');
1072 trace_seq_putc(s
, 'h');
1075 trace_seq_putc(s
, 's');
1077 trace_seq_putc(s
, '.');
1081 if (entry
->preempt_count
)
1082 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1084 trace_seq_puts(s
, ".");
1087 unsigned long preempt_mark_thresh
= 100;
1090 lat_print_timestamp(struct trace_seq
*s
, unsigned long long abs_usecs
,
1091 unsigned long rel_usecs
)
1093 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1094 if (rel_usecs
> preempt_mark_thresh
)
1095 trace_seq_puts(s
, "!: ");
1096 else if (rel_usecs
> 1)
1097 trace_seq_puts(s
, "+: ");
1099 trace_seq_puts(s
, " : ");
1102 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1105 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1107 struct trace_seq
*s
= &iter
->seq
;
1108 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1109 struct trace_entry
*next_entry
= find_next_entry(iter
, NULL
);
1110 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1111 struct trace_entry
*entry
= iter
->ent
;
1112 unsigned long abs_usecs
;
1113 unsigned long rel_usecs
;
1119 rel_usecs
= ns2usecs(next_entry
->t
- entry
->t
);
1120 abs_usecs
= ns2usecs(entry
->t
- iter
->tr
->time_start
);
1123 comm
= trace_find_cmdline(entry
->pid
);
1124 trace_seq_printf(s
, "%16s %5d %d %d %08x %08x [%08lx]"
1125 " %ld.%03ldms (+%ld.%03ldms): ",
1127 entry
->pid
, cpu
, entry
->flags
,
1128 entry
->preempt_count
, trace_idx
,
1131 abs_usecs
% 1000, rel_usecs
/1000,
1134 lat_print_generic(s
, entry
, cpu
);
1135 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1137 switch (entry
->type
) {
1139 seq_print_ip_sym(s
, entry
->fn
.ip
, sym_flags
);
1140 trace_seq_puts(s
, " (");
1141 seq_print_ip_sym(s
, entry
->fn
.parent_ip
, sym_flags
);
1142 trace_seq_puts(s
, ")\n");
1145 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1146 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1147 comm
= trace_find_cmdline(entry
->ctx
.next_pid
);
1148 trace_seq_printf(s
, " %d:%d:%c --> %d:%d %s\n",
1149 entry
->ctx
.prev_pid
,
1150 entry
->ctx
.prev_prio
,
1152 entry
->ctx
.next_pid
,
1153 entry
->ctx
.next_prio
,
1157 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1162 print_trace_fmt(struct trace_iterator
*iter
)
1164 struct trace_seq
*s
= &iter
->seq
;
1165 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1166 struct trace_entry
*entry
;
1167 unsigned long usec_rem
;
1168 unsigned long long t
;
1176 comm
= trace_find_cmdline(iter
->ent
->pid
);
1178 t
= ns2usecs(entry
->t
);
1179 usec_rem
= do_div(t
, 1000000ULL);
1180 secs
= (unsigned long)t
;
1182 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1185 ret
= trace_seq_printf(s
, "[%02d] ", iter
->cpu
);
1188 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1192 switch (entry
->type
) {
1194 ret
= seq_print_ip_sym(s
, entry
->fn
.ip
, sym_flags
);
1197 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1198 entry
->fn
.parent_ip
) {
1199 ret
= trace_seq_printf(s
, " <-");
1202 ret
= seq_print_ip_sym(s
, entry
->fn
.parent_ip
,
1207 ret
= trace_seq_printf(s
, "\n");
1212 S
= entry
->ctx
.prev_state
< sizeof(state_to_char
) ?
1213 state_to_char
[entry
->ctx
.prev_state
] : 'X';
1214 ret
= trace_seq_printf(s
, " %d:%d:%c ==> %d:%d\n",
1215 entry
->ctx
.prev_pid
,
1216 entry
->ctx
.prev_prio
,
1218 entry
->ctx
.next_pid
,
1219 entry
->ctx
.next_prio
);
1227 static int trace_empty(struct trace_iterator
*iter
)
1229 struct trace_array_cpu
*data
;
1232 for_each_possible_cpu(cpu
) {
1233 data
= iter
->tr
->data
[cpu
];
1235 if (head_page(data
) && data
->trace_idx
&&
1236 (data
->trace_tail
!= data
->trace_head
||
1237 data
->trace_tail_idx
!= data
->trace_head_idx
))
1243 static int s_show(struct seq_file
*m
, void *v
)
1245 struct trace_iterator
*iter
= v
;
1247 if (iter
->ent
== NULL
) {
1249 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1252 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1253 /* print nothing if the buffers are empty */
1254 if (trace_empty(iter
))
1256 print_trace_header(m
, iter
);
1257 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1258 print_lat_help_header(m
);
1260 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1261 print_func_help_header(m
);
1264 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1265 print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1267 print_trace_fmt(iter
);
1268 trace_print_seq(m
, &iter
->seq
);
1274 static struct seq_operations tracer_seq_ops
= {
1281 static struct trace_iterator notrace
*
1282 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1284 struct trace_iterator
*iter
;
1286 if (tracing_disabled
) {
1291 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1297 mutex_lock(&trace_types_lock
);
1298 if (current_trace
&& current_trace
->print_max
)
1301 iter
->tr
= inode
->i_private
;
1302 iter
->trace
= current_trace
;
1305 /* TODO stop tracer */
1306 *ret
= seq_open(file
, &tracer_seq_ops
);
1308 struct seq_file
*m
= file
->private_data
;
1311 /* stop the trace while dumping */
1315 if (iter
->trace
&& iter
->trace
->open
)
1316 iter
->trace
->open(iter
);
1321 mutex_unlock(&trace_types_lock
);
1327 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1329 if (tracing_disabled
)
1332 filp
->private_data
= inode
->i_private
;
1336 int tracing_release(struct inode
*inode
, struct file
*file
)
1338 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1339 struct trace_iterator
*iter
= m
->private;
1341 mutex_lock(&trace_types_lock
);
1342 if (iter
->trace
&& iter
->trace
->close
)
1343 iter
->trace
->close(iter
);
1345 /* reenable tracing if it was previously enabled */
1348 mutex_unlock(&trace_types_lock
);
1350 seq_release(inode
, file
);
1355 static int tracing_open(struct inode
*inode
, struct file
*file
)
1359 __tracing_open(inode
, file
, &ret
);
1364 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1366 struct trace_iterator
*iter
;
1369 iter
= __tracing_open(inode
, file
, &ret
);
1372 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1378 static notrace
void *
1379 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1381 struct tracer
*t
= m
->private;
1393 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1395 struct tracer
*t
= m
->private;
1398 mutex_lock(&trace_types_lock
);
1399 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1405 static void t_stop(struct seq_file
*m
, void *p
)
1407 mutex_unlock(&trace_types_lock
);
1410 static int t_show(struct seq_file
*m
, void *v
)
1412 struct tracer
*t
= v
;
1417 seq_printf(m
, "%s", t
->name
);
1426 static struct seq_operations show_traces_seq_ops
= {
1433 static int show_traces_open(struct inode
*inode
, struct file
*file
)
1437 if (tracing_disabled
)
1440 ret
= seq_open(file
, &show_traces_seq_ops
);
1442 struct seq_file
*m
= file
->private_data
;
1443 m
->private = trace_types
;
1449 static struct file_operations tracing_fops
= {
1450 .open
= tracing_open
,
1452 .llseek
= seq_lseek
,
1453 .release
= tracing_release
,
1456 static struct file_operations tracing_lt_fops
= {
1457 .open
= tracing_lt_open
,
1459 .llseek
= seq_lseek
,
1460 .release
= tracing_release
,
1463 static struct file_operations show_traces_fops
= {
1464 .open
= show_traces_open
,
1466 .release
= seq_release
,
1470 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1471 size_t cnt
, loff_t
*ppos
)
1478 /* calulate max size */
1479 for (i
= 0; trace_options
[i
]; i
++) {
1480 len
+= strlen(trace_options
[i
]);
1481 len
+= 3; /* "no" and space */
1484 /* +2 for \n and \0 */
1485 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
1489 for (i
= 0; trace_options
[i
]; i
++) {
1490 if (trace_flags
& (1 << i
))
1491 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
1493 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
1496 r
+= sprintf(buf
+ r
, "\n");
1497 WARN_ON(r
>= len
+ 2);
1499 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1508 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1509 size_t cnt
, loff_t
*ppos
)
1519 if (copy_from_user(&buf
, ubuf
, cnt
))
1524 if (strncmp(buf
, "no", 2) == 0) {
1529 for (i
= 0; trace_options
[i
]; i
++) {
1530 int len
= strlen(trace_options
[i
]);
1532 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
1534 trace_flags
&= ~(1 << i
);
1536 trace_flags
|= (1 << i
);
1546 static struct file_operations tracing_iter_fops
= {
1547 .open
= tracing_open_generic
,
1548 .read
= tracing_iter_ctrl_read
,
1549 .write
= tracing_iter_ctrl_write
,
1552 static const char readme_msg
[] =
1553 "tracing mini-HOWTO:\n\n"
1555 "# mount -t debugfs nodev /debug\n\n"
1556 "# cat /debug/tracing/available_tracers\n"
1557 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1558 "# cat /debug/tracing/current_tracer\n"
1560 "# echo sched_switch > /debug/tracing/current_tracer\n"
1561 "# cat /debug/tracing/current_tracer\n"
1563 "# cat /debug/tracing/iter_ctrl\n"
1564 "noprint-parent nosym-offset nosym-addr noverbose\n"
1565 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1566 "# echo 1 > /debug/tracing/tracing_enabled\n"
1567 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1568 "echo 0 > /debug/tracing/tracing_enabled\n"
1572 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
1573 size_t cnt
, loff_t
*ppos
)
1575 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
1576 readme_msg
, strlen(readme_msg
));
1579 static struct file_operations tracing_readme_fops
= {
1580 .open
= tracing_open_generic
,
1581 .read
= tracing_readme_read
,
1586 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
1587 size_t cnt
, loff_t
*ppos
)
1589 struct trace_array
*tr
= filp
->private_data
;
1593 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
1594 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1598 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
1599 size_t cnt
, loff_t
*ppos
)
1601 struct trace_array
*tr
= filp
->private_data
;
1608 if (copy_from_user(&buf
, ubuf
, cnt
))
1613 val
= simple_strtoul(buf
, NULL
, 10);
1617 mutex_lock(&trace_types_lock
);
1618 if (tr
->ctrl
^ val
) {
1626 if (current_trace
&& current_trace
->ctrl_update
)
1627 current_trace
->ctrl_update(tr
);
1629 mutex_unlock(&trace_types_lock
);
1637 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
1638 size_t cnt
, loff_t
*ppos
)
1640 char buf
[max_tracer_type_len
+2];
1643 mutex_lock(&trace_types_lock
);
1645 r
= sprintf(buf
, "%s\n", current_trace
->name
);
1647 r
= sprintf(buf
, "\n");
1648 mutex_unlock(&trace_types_lock
);
1650 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1654 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
1655 size_t cnt
, loff_t
*ppos
)
1657 struct trace_array
*tr
= &global_trace
;
1659 char buf
[max_tracer_type_len
+1];
1662 if (cnt
> max_tracer_type_len
)
1663 cnt
= max_tracer_type_len
;
1665 if (copy_from_user(&buf
, ubuf
, cnt
))
1670 /* strip ending whitespace. */
1671 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
1674 mutex_lock(&trace_types_lock
);
1675 for (t
= trace_types
; t
; t
= t
->next
) {
1676 if (strcmp(t
->name
, buf
) == 0)
1679 if (!t
|| t
== current_trace
)
1682 if (current_trace
&& current_trace
->reset
)
1683 current_trace
->reset(tr
);
1690 mutex_unlock(&trace_types_lock
);
1698 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
1699 size_t cnt
, loff_t
*ppos
)
1701 unsigned long *ptr
= filp
->private_data
;
1705 r
= snprintf(buf
, 64, "%ld\n",
1706 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
1709 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1713 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
1714 size_t cnt
, loff_t
*ppos
)
1716 long *ptr
= filp
->private_data
;
1723 if (copy_from_user(&buf
, ubuf
, cnt
))
1728 val
= simple_strtoul(buf
, NULL
, 10);
1735 static atomic_t tracing_reader
;
1737 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
1739 struct trace_iterator
*iter
;
1741 if (tracing_disabled
)
1744 /* We only allow for reader of the pipe */
1745 if (atomic_inc_return(&tracing_reader
) != 1) {
1746 atomic_dec(&tracing_reader
);
1750 /* create a buffer to store the information to pass to userspace */
1751 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1755 iter
->tr
= &global_trace
;
1757 filp
->private_data
= iter
;
1762 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
1764 struct trace_iterator
*iter
= file
->private_data
;
1767 atomic_dec(&tracing_reader
);
1776 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
1777 size_t cnt
, loff_t
*ppos
)
1779 struct trace_iterator
*iter
= filp
->private_data
;
1780 struct trace_array_cpu
*data
;
1781 static cpumask_t mask
;
1782 struct trace_entry
*entry
;
1784 unsigned long flags
;
1790 /* return any leftover data */
1791 if (iter
->seq
.len
> start
) {
1792 len
= iter
->seq
.len
- start
;
1795 ret
= copy_to_user(ubuf
, iter
->seq
.buffer
+ start
, cnt
);
1804 trace_seq_reset(&iter
->seq
);
1807 while (trace_empty(iter
)) {
1809 * This is a make-shift waitqueue. The reason we don't use
1810 * an actual wait queue is because:
1811 * 1) we only ever have one waiter
1812 * 2) the tracing, traces all functions, we don't want
1813 * the overhead of calling wake_up and friends
1814 * (and tracing them too)
1815 * Anyway, this is really very primitive wakeup.
1817 set_current_state(TASK_INTERRUPTIBLE
);
1818 iter
->tr
->waiter
= current
;
1820 /* sleep for one second, and try again. */
1821 schedule_timeout(HZ
);
1823 iter
->tr
->waiter
= NULL
;
1825 if (signal_pending(current
))
1829 * We block until we read something and tracing is disabled.
1830 * We still block if tracing is disabled, but we have never
1831 * read anything. This allows a user to cat this file, and
1832 * then enable tracing. But after we have read something,
1833 * we give an EOF when tracing is again disabled.
1835 * iter->pos will be 0 if we haven't read anything.
1837 if (!tracer_enabled
&& iter
->pos
)
1843 /* stop when tracing is finished */
1844 if (trace_empty(iter
))
1847 if (cnt
>= PAGE_SIZE
)
1848 cnt
= PAGE_SIZE
- 1;
1850 memset(iter
, 0, sizeof(*iter
));
1851 iter
->tr
= &global_trace
;
1855 * We need to stop all tracing on all CPUS to read the
1856 * the next buffer. This is a bit expensive, but is
1857 * not done often. We fill all what we can read,
1858 * and then release the locks again.
1862 local_irq_save(flags
);
1863 for_each_possible_cpu(cpu
) {
1864 data
= iter
->tr
->data
[cpu
];
1866 if (!head_page(data
) || !data
->trace_idx
)
1869 atomic_inc(&data
->disabled
);
1870 spin_lock(&data
->lock
);
1874 while ((entry
= find_next_entry(iter
, &cpu
))) {
1882 ret
= print_trace_fmt(iter
);
1886 trace_consume(iter
);
1888 if (iter
->seq
.len
>= cnt
)
1893 for_each_cpu_mask(cpu
, mask
) {
1894 data
= iter
->tr
->data
[cpu
];
1895 spin_unlock(&data
->lock
);
1896 atomic_dec(&data
->disabled
);
1898 local_irq_restore(flags
);
1900 /* Now copy what we have to the user */
1901 read
= iter
->seq
.len
;
1905 ret
= copy_to_user(ubuf
, iter
->seq
.buffer
, read
);
1907 if (read
< iter
->seq
.len
)
1910 trace_seq_reset(&iter
->seq
);
1918 static struct file_operations tracing_max_lat_fops
= {
1919 .open
= tracing_open_generic
,
1920 .read
= tracing_max_lat_read
,
1921 .write
= tracing_max_lat_write
,
1924 static struct file_operations tracing_ctrl_fops
= {
1925 .open
= tracing_open_generic
,
1926 .read
= tracing_ctrl_read
,
1927 .write
= tracing_ctrl_write
,
1930 static struct file_operations set_tracer_fops
= {
1931 .open
= tracing_open_generic
,
1932 .read
= tracing_set_trace_read
,
1933 .write
= tracing_set_trace_write
,
1936 static struct file_operations tracing_pipe_fops
= {
1937 .open
= tracing_open_pipe
,
1938 .read
= tracing_read_pipe
,
1939 .release
= tracing_release_pipe
,
1942 #ifdef CONFIG_DYNAMIC_FTRACE
1945 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
1946 size_t cnt
, loff_t
*ppos
)
1948 unsigned long *p
= filp
->private_data
;
1952 r
= sprintf(buf
, "%ld\n", *p
);
1954 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1957 static struct file_operations tracing_read_long_fops
= {
1958 .open
= tracing_open_generic
,
1959 .read
= tracing_read_long
,
1963 static struct dentry
*d_tracer
;
1965 struct dentry
*tracing_init_dentry(void)
1972 d_tracer
= debugfs_create_dir("tracing", NULL
);
1974 if (!d_tracer
&& !once
) {
1976 pr_warning("Could not create debugfs directory 'tracing'\n");
1983 #ifdef CONFIG_FTRACE_SELFTEST
1984 /* Let selftest have access to static functions in this file */
1985 #include "trace_selftest.c"
1988 static __init
void tracer_init_debugfs(void)
1990 struct dentry
*d_tracer
;
1991 struct dentry
*entry
;
1993 d_tracer
= tracing_init_dentry();
1995 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
1996 &global_trace
, &tracing_ctrl_fops
);
1998 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2000 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
2001 NULL
, &tracing_iter_fops
);
2003 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2005 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2006 &global_trace
, &tracing_lt_fops
);
2008 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2010 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2011 &global_trace
, &tracing_fops
);
2013 pr_warning("Could not create debugfs 'trace' entry\n");
2015 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2016 &global_trace
, &show_traces_fops
);
2018 pr_warning("Could not create debugfs 'trace' entry\n");
2020 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2021 &global_trace
, &set_tracer_fops
);
2023 pr_warning("Could not create debugfs 'trace' entry\n");
2025 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2026 &tracing_max_latency
,
2027 &tracing_max_lat_fops
);
2029 pr_warning("Could not create debugfs "
2030 "'tracing_max_latency' entry\n");
2032 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2033 &tracing_thresh
, &tracing_max_lat_fops
);
2035 pr_warning("Could not create debugfs "
2036 "'tracing_threash' entry\n");
2037 entry
= debugfs_create_file("README", 0644, d_tracer
,
2038 NULL
, &tracing_readme_fops
);
2040 pr_warning("Could not create debugfs 'README' entry\n");
2042 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2043 NULL
, &tracing_pipe_fops
);
2045 pr_warning("Could not create debugfs "
2046 "'tracing_threash' entry\n");
2048 #ifdef CONFIG_DYNAMIC_FTRACE
2049 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
2050 &ftrace_update_tot_cnt
,
2051 &tracing_read_long_fops
);
2053 pr_warning("Could not create debugfs "
2054 "'dyn_ftrace_total_info' entry\n");
2058 /* dummy trace to disable tracing */
2059 static struct tracer no_tracer __read_mostly
=
2064 static int trace_alloc_page(void)
2066 struct trace_array_cpu
*data
;
2067 struct page
*page
, *tmp
;
2072 /* first allocate a page for each CPU */
2073 for_each_possible_cpu(i
) {
2074 array
= (void *)__get_free_page(GFP_KERNEL
);
2075 if (array
== NULL
) {
2076 printk(KERN_ERR
"tracer: failed to allocate page"
2077 "for trace buffer!\n");
2081 page
= virt_to_page(array
);
2082 list_add(&page
->lru
, &pages
);
2084 /* Only allocate if we are actually using the max trace */
2085 #ifdef CONFIG_TRACER_MAX_TRACE
2086 array
= (void *)__get_free_page(GFP_KERNEL
);
2087 if (array
== NULL
) {
2088 printk(KERN_ERR
"tracer: failed to allocate page"
2089 "for trace buffer!\n");
2092 page
= virt_to_page(array
);
2093 list_add(&page
->lru
, &pages
);
2097 /* Now that we successfully allocate a page per CPU, add them */
2098 for_each_possible_cpu(i
) {
2099 data
= global_trace
.data
[i
];
2100 spin_lock_init(&data
->lock
);
2101 lockdep_set_class(&data
->lock
, &data
->lock_key
);
2102 page
= list_entry(pages
.next
, struct page
, lru
);
2103 list_del_init(&page
->lru
);
2104 list_add_tail(&page
->lru
, &data
->trace_pages
);
2107 #ifdef CONFIG_TRACER_MAX_TRACE
2108 data
= max_tr
.data
[i
];
2109 spin_lock_init(&data
->lock
);
2110 lockdep_set_class(&data
->lock
, &data
->lock_key
);
2111 page
= list_entry(pages
.next
, struct page
, lru
);
2112 list_del_init(&page
->lru
);
2113 list_add_tail(&page
->lru
, &data
->trace_pages
);
2117 global_trace
.entries
+= ENTRIES_PER_PAGE
;
2122 list_for_each_entry_safe(page
, tmp
, &pages
, lru
) {
2123 list_del_init(&page
->lru
);
2129 __init
static int tracer_alloc_buffers(void)
2131 struct trace_array_cpu
*data
;
2138 /* Allocate the first page for all buffers */
2139 for_each_possible_cpu(i
) {
2140 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
2141 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
2143 array
= (void *)__get_free_page(GFP_KERNEL
);
2144 if (array
== NULL
) {
2145 printk(KERN_ERR
"tracer: failed to allocate page"
2146 "for trace buffer!\n");
2150 /* set the array to the list */
2151 INIT_LIST_HEAD(&data
->trace_pages
);
2152 page
= virt_to_page(array
);
2153 list_add(&page
->lru
, &data
->trace_pages
);
2154 /* use the LRU flag to differentiate the two buffers */
2157 /* Only allocate if we are actually using the max trace */
2158 #ifdef CONFIG_TRACER_MAX_TRACE
2159 array
= (void *)__get_free_page(GFP_KERNEL
);
2160 if (array
== NULL
) {
2161 printk(KERN_ERR
"tracer: failed to allocate page"
2162 "for trace buffer!\n");
2166 INIT_LIST_HEAD(&max_tr
.data
[i
]->trace_pages
);
2167 page
= virt_to_page(array
);
2168 list_add(&page
->lru
, &max_tr
.data
[i
]->trace_pages
);
2174 * Since we allocate by orders of pages, we may be able to
2177 global_trace
.entries
= ENTRIES_PER_PAGE
;
2180 while (global_trace
.entries
< trace_nr_entries
) {
2181 if (trace_alloc_page())
2185 max_tr
.entries
= global_trace
.entries
;
2187 pr_info("tracer: %d pages allocated for %ld",
2188 pages
, trace_nr_entries
);
2189 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE
);
2190 pr_info(" actual entries %ld\n", global_trace
.entries
);
2192 tracer_init_debugfs();
2194 trace_init_cmdlines();
2196 register_tracer(&no_tracer
);
2197 current_trace
= &no_tracer
;
2199 /* All seems OK, enable tracing */
2200 tracing_disabled
= 0;
2205 for (i
-- ; i
>= 0; i
--) {
2206 struct page
*page
, *tmp
;
2207 struct trace_array_cpu
*data
= global_trace
.data
[i
];
2210 list_for_each_entry_safe(page
, tmp
,
2211 &data
->trace_pages
, lru
) {
2212 list_del_init(&page
->lru
);
2217 #ifdef CONFIG_TRACER_MAX_TRACE
2218 data
= max_tr
.data
[i
];
2220 list_for_each_entry_safe(page
, tmp
,
2221 &data
->trace_pages
, lru
) {
2222 list_del_init(&page
->lru
);
2230 fs_initcall(tracer_alloc_buffers
);