]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace.c
ftrace: add raw output
[net-next-2.6.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
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
13 */
14#include <linux/utsrelease.h>
15#include <linux/kallsyms.h>
16#include <linux/seq_file.h>
17#include <linux/debugfs.h>
4c11d7ae 18#include <linux/pagemap.h>
bc0c38d1
SR
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>
28#include <linux/fs.h>
29
30#include "trace.h"
31
32unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33unsigned long __read_mostly tracing_thresh;
34
60a11774
SR
35static int tracing_disabled = 1;
36
bc0c38d1
SR
37static long notrace
38ns2usecs(cycle_t nsec)
39{
40 nsec += 500;
41 do_div(nsec, 1000);
42 return nsec;
43}
44
53c37c17
IM
45static const int time_sync_freq_max = 128;
46static const cycle_t time_sync_thresh = 100000;
47
48static DEFINE_PER_CPU(cycle_t, time_offset);
49static DEFINE_PER_CPU(cycle_t, prev_cpu_time);
50static DEFINE_PER_CPU(int, time_sync_count);
51static DEFINE_PER_CPU(int, time_sync_freq);
52
53/*
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:
58 */
59static DEFINE_SPINLOCK(time_sync_lock);
60static cycle_t prev_global_time;
61
62static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
63{
64 unsigned long flags;
65
66 spin_lock_irqsave(&time_sync_lock, flags);
67
68 /*
69 * Update the synchronization frequency:
70 */
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);
74
75 if (time < prev_global_time) {
76 per_cpu(time_offset, cpu) += prev_global_time - time;
77 time = prev_global_time;
78 } else {
79 prev_global_time = time;
80 }
81
82 spin_unlock_irqrestore(&time_sync_lock, flags);
83
84 return time;
85}
86
750ed1a4
IM
87notrace cycle_t ftrace_now(int cpu)
88{
53c37c17
IM
89 cycle_t prev_cpu_time, time, delta_time;
90
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;
94
95 if (unlikely(delta_time > time_sync_thresh ||
96 --per_cpu(time_sync_count, cpu) <= 0))
97 time = __ftrace_now_sync(time, cpu);
98
99 return time;
750ed1a4
IM
100}
101
bc0c38d1
SR
102static struct trace_array global_trace;
103
104static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
105
106static struct trace_array max_tr;
107
108static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
109
110static int tracer_enabled;
4c11d7ae 111static unsigned long trace_nr_entries = 16384UL;
bc0c38d1
SR
112
113static struct tracer *trace_types __read_mostly;
114static struct tracer *current_trace __read_mostly;
115static int max_tracer_type_len;
116
117static DEFINE_MUTEX(trace_types_lock);
118
4c11d7ae
SR
119#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
120
bc0c38d1
SR
121static int __init set_nr_entries(char *str)
122{
123 if (!str)
124 return 0;
125 trace_nr_entries = simple_strtoul(str, &str, 0);
126 return 1;
127}
128__setup("trace_entries=", set_nr_entries);
129
57f50be1
SR
130unsigned long nsecs_to_usecs(unsigned long nsecs)
131{
132 return nsecs / 1000;
133}
134
bc0c38d1
SR
135enum trace_type {
136 __TRACE_FIRST_TYPE = 0,
137
138 TRACE_FN,
139 TRACE_CTX,
140
141 __TRACE_LAST_TYPE
142};
143
144enum 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,
149};
150
151enum 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,
f9896bf3 156 TRACE_ITER_RAW = 0x10,
bc0c38d1
SR
157};
158
159#define TRACE_ITER_SYM_MASK \
160 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
161
162/* These must match the bit postions above */
163static const char *trace_options[] = {
164 "print-parent",
165 "sym-offset",
166 "sym-addr",
167 "verbose",
f9896bf3 168 "raw",
bc0c38d1
SR
169 NULL
170};
171
172static unsigned trace_flags;
173
4c11d7ae 174static DEFINE_SPINLOCK(ftrace_max_lock);
bc0c38d1
SR
175
176/*
177 * Copy the new maximum trace into the separate maximum-trace
178 * structure. (this way the maximum trace is permanently saved,
179 * for later retrieval via /debugfs/tracing/latency_trace)
180 */
4e3c3333 181static notrace void
bc0c38d1
SR
182__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
183{
184 struct trace_array_cpu *data = tr->data[cpu];
185
186 max_tr.cpu = cpu;
187 max_tr.time_start = data->preempt_timestamp;
188
189 data = max_tr.data[cpu];
190 data->saved_latency = tracing_max_latency;
191
192 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
193 data->pid = tsk->pid;
194 data->uid = tsk->uid;
195 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
196 data->policy = tsk->policy;
197 data->rt_priority = tsk->rt_priority;
198
199 /* record this tasks comm */
200 tracing_record_cmdline(current);
201}
202
c7aafc54
IM
203void check_pages(struct trace_array_cpu *data)
204{
205 struct page *page, *tmp;
206
207 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
208 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
209
210 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
211 BUG_ON(page->lru.next->prev != &page->lru);
212 BUG_ON(page->lru.prev->next != &page->lru);
213 }
214}
215
216void *head_page(struct trace_array_cpu *data)
217{
218 struct page *page;
219
220 check_pages(data);
221 if (list_empty(&data->trace_pages))
222 return NULL;
223
224 page = list_entry(data->trace_pages.next, struct page, lru);
225 BUG_ON(&page->lru == &data->trace_pages);
226
227 return page_address(page);
228}
229
214023c3
SR
230static notrace int
231trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
232{
233 int len = (PAGE_SIZE - 1) - s->len;
234 va_list ap;
b3806b43 235 int ret;
214023c3
SR
236
237 if (!len)
238 return 0;
239
240 va_start(ap, fmt);
b3806b43 241 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
214023c3
SR
242 va_end(ap);
243
b3806b43
SR
244 /* If we can't write it all, don't bother writing anything */
245 if (ret > len)
246 return 0;
247
248 s->len += ret;
214023c3
SR
249
250 return len;
251}
252
253static notrace int
254trace_seq_puts(struct trace_seq *s, const char *str)
255{
256 int len = strlen(str);
257
258 if (len > ((PAGE_SIZE - 1) - s->len))
b3806b43 259 return 0;
214023c3
SR
260
261 memcpy(s->buffer + s->len, str, len);
262 s->len += len;
263
264 return len;
265}
266
267static notrace int
268trace_seq_putc(struct trace_seq *s, unsigned char c)
269{
270 if (s->len >= (PAGE_SIZE - 1))
271 return 0;
272
273 s->buffer[s->len++] = c;
274
275 return 1;
276}
277
278static notrace void
279trace_seq_reset(struct trace_seq *s)
280{
281 s->len = 0;
282}
283
284static notrace void
285trace_print_seq(struct seq_file *m, struct trace_seq *s)
286{
287 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
288
289 s->buffer[len] = 0;
290 seq_puts(m, s->buffer);
291
292 trace_seq_reset(s);
293}
294
c7aafc54
IM
295notrace static void
296flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
297{
298 struct list_head flip_pages;
299
300 INIT_LIST_HEAD(&flip_pages);
301
93a588f4 302 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
c7aafc54 303 sizeof(struct trace_array_cpu) -
93a588f4 304 offsetof(struct trace_array_cpu, trace_head_idx));
c7aafc54
IM
305
306 check_pages(tr1);
307 check_pages(tr2);
308 list_splice_init(&tr1->trace_pages, &flip_pages);
309 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
310 list_splice_init(&flip_pages, &tr2->trace_pages);
311 BUG_ON(!list_empty(&flip_pages));
312 check_pages(tr1);
313 check_pages(tr2);
314}
315
bc0c38d1
SR
316notrace void
317update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
318{
319 struct trace_array_cpu *data;
bc0c38d1
SR
320 int i;
321
4c11d7ae
SR
322 WARN_ON_ONCE(!irqs_disabled());
323 spin_lock(&ftrace_max_lock);
bc0c38d1
SR
324 /* clear out all the previous traces */
325 for_each_possible_cpu(i) {
326 data = tr->data[i];
c7aafc54 327 flip_trace(max_tr.data[i], data);
89b2f978 328 tracing_reset(data);
bc0c38d1
SR
329 }
330
331 __update_max_tr(tr, tsk, cpu);
4c11d7ae 332 spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
333}
334
335/**
336 * update_max_tr_single - only copy one trace over, and reset the rest
337 * @tr - tracer
338 * @tsk - task with the latency
339 * @cpu - the cpu of the buffer to copy.
340 */
341notrace void
342update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
343{
344 struct trace_array_cpu *data = tr->data[cpu];
bc0c38d1
SR
345 int i;
346
4c11d7ae
SR
347 WARN_ON_ONCE(!irqs_disabled());
348 spin_lock(&ftrace_max_lock);
bc0c38d1
SR
349 for_each_possible_cpu(i)
350 tracing_reset(max_tr.data[i]);
351
c7aafc54 352 flip_trace(max_tr.data[cpu], data);
89b2f978 353 tracing_reset(data);
bc0c38d1
SR
354
355 __update_max_tr(tr, tsk, cpu);
4c11d7ae 356 spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
357}
358
359int register_tracer(struct tracer *type)
360{
361 struct tracer *t;
362 int len;
363 int ret = 0;
364
365 if (!type->name) {
366 pr_info("Tracer must have a name\n");
367 return -1;
368 }
369
370 mutex_lock(&trace_types_lock);
371 for (t = trace_types; t; t = t->next) {
372 if (strcmp(type->name, t->name) == 0) {
373 /* already found */
374 pr_info("Trace %s already registered\n",
375 type->name);
376 ret = -1;
377 goto out;
378 }
379 }
380
60a11774
SR
381#ifdef CONFIG_FTRACE_STARTUP_TEST
382 if (type->selftest) {
383 struct tracer *saved_tracer = current_trace;
384 struct trace_array_cpu *data;
385 struct trace_array *tr = &global_trace;
386 int saved_ctrl = tr->ctrl;
387 int i;
388 /*
389 * Run a selftest on this tracer.
390 * Here we reset the trace buffer, and set the current
391 * tracer to be this tracer. The tracer can then run some
392 * internal tracing to verify that everything is in order.
393 * If we fail, we do not register this tracer.
394 */
395 for_each_possible_cpu(i) {
60a11774 396 data = tr->data[i];
c7aafc54
IM
397 if (!head_page(data))
398 continue;
60a11774
SR
399 tracing_reset(data);
400 }
401 current_trace = type;
402 tr->ctrl = 0;
403 /* the test is responsible for initializing and enabling */
404 pr_info("Testing tracer %s: ", type->name);
405 ret = type->selftest(type, tr);
406 /* the test is responsible for resetting too */
407 current_trace = saved_tracer;
408 tr->ctrl = saved_ctrl;
409 if (ret) {
410 printk(KERN_CONT "FAILED!\n");
411 goto out;
412 }
1d4db00a
SR
413 /* Only reset on passing, to avoid touching corrupted buffers */
414 for_each_possible_cpu(i) {
415 data = tr->data[i];
416 if (!head_page(data))
417 continue;
418 tracing_reset(data);
419 }
60a11774
SR
420 printk(KERN_CONT "PASSED\n");
421 }
422#endif
423
bc0c38d1
SR
424 type->next = trace_types;
425 trace_types = type;
426 len = strlen(type->name);
427 if (len > max_tracer_type_len)
428 max_tracer_type_len = len;
60a11774 429
bc0c38d1
SR
430 out:
431 mutex_unlock(&trace_types_lock);
432
433 return ret;
434}
435
436void unregister_tracer(struct tracer *type)
437{
438 struct tracer **t;
439 int len;
440
441 mutex_lock(&trace_types_lock);
442 for (t = &trace_types; *t; t = &(*t)->next) {
443 if (*t == type)
444 goto found;
445 }
446 pr_info("Trace %s not registered\n", type->name);
447 goto out;
448
449 found:
450 *t = (*t)->next;
451 if (strlen(type->name) != max_tracer_type_len)
452 goto out;
453
454 max_tracer_type_len = 0;
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 len = strlen((*t)->name);
457 if (len > max_tracer_type_len)
458 max_tracer_type_len = len;
459 }
460 out:
461 mutex_unlock(&trace_types_lock);
462}
463
4e3c3333 464notrace void tracing_reset(struct trace_array_cpu *data)
bc0c38d1
SR
465{
466 data->trace_idx = 0;
93a588f4
SR
467 data->trace_head = data->trace_tail = head_page(data);
468 data->trace_head_idx = 0;
469 data->trace_tail_idx = 0;
bc0c38d1
SR
470}
471
472#ifdef CONFIG_FTRACE
4e3c3333 473static notrace void
bc0c38d1
SR
474function_trace_call(unsigned long ip, unsigned long parent_ip)
475{
476 struct trace_array *tr = &global_trace;
477 struct trace_array_cpu *data;
478 unsigned long flags;
479 long disabled;
480 int cpu;
481
482 if (unlikely(!tracer_enabled))
483 return;
484
18cef379 485 local_irq_save(flags);
bc0c38d1
SR
486 cpu = raw_smp_processor_id();
487 data = tr->data[cpu];
488 disabled = atomic_inc_return(&data->disabled);
489
490 if (likely(disabled == 1))
491 ftrace(tr, data, ip, parent_ip, flags);
492
493 atomic_dec(&data->disabled);
18cef379 494 local_irq_restore(flags);
bc0c38d1
SR
495}
496
497static struct ftrace_ops trace_ops __read_mostly =
498{
499 .func = function_trace_call,
500};
501#endif
502
503notrace void tracing_start_function_trace(void)
504{
505 register_ftrace_function(&trace_ops);
506}
507
508notrace void tracing_stop_function_trace(void)
509{
510 unregister_ftrace_function(&trace_ops);
511}
512
513#define SAVED_CMDLINES 128
514static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
515static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
516static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
517static int cmdline_idx;
518static DEFINE_SPINLOCK(trace_cmdline_lock);
519atomic_t trace_record_cmdline_disabled;
520
521static void trace_init_cmdlines(void)
522{
523 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
524 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
525 cmdline_idx = 0;
526}
527
528notrace void trace_stop_cmdline_recording(void);
529
4e3c3333 530static notrace void trace_save_cmdline(struct task_struct *tsk)
bc0c38d1
SR
531{
532 unsigned map;
533 unsigned idx;
534
535 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
536 return;
537
538 /*
539 * It's not the end of the world if we don't get
540 * the lock, but we also don't want to spin
541 * nor do we want to disable interrupts,
542 * so if we miss here, then better luck next time.
543 */
544 if (!spin_trylock(&trace_cmdline_lock))
545 return;
546
547 idx = map_pid_to_cmdline[tsk->pid];
548 if (idx >= SAVED_CMDLINES) {
549 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
550
551 map = map_cmdline_to_pid[idx];
552 if (map <= PID_MAX_DEFAULT)
553 map_pid_to_cmdline[map] = (unsigned)-1;
554
555 map_pid_to_cmdline[tsk->pid] = idx;
556
557 cmdline_idx = idx;
558 }
559
560 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
561
562 spin_unlock(&trace_cmdline_lock);
563}
564
565static notrace char *trace_find_cmdline(int pid)
566{
567 char *cmdline = "<...>";
568 unsigned map;
569
570 if (!pid)
571 return "<idle>";
572
573 if (pid > PID_MAX_DEFAULT)
574 goto out;
575
576 map = map_pid_to_cmdline[pid];
577 if (map >= SAVED_CMDLINES)
578 goto out;
579
580 cmdline = saved_cmdlines[map];
581
582 out:
583 return cmdline;
584}
585
586notrace void tracing_record_cmdline(struct task_struct *tsk)
587{
588 if (atomic_read(&trace_record_cmdline_disabled))
589 return;
590
591 trace_save_cmdline(tsk);
592}
593
93a588f4
SR
594static inline notrace struct list_head *
595trace_next_list(struct trace_array_cpu *data, struct list_head *next)
596{
597 /*
598 * Roundrobin - but skip the head (which is not a real page):
599 */
600 next = next->next;
601 if (unlikely(next == &data->trace_pages))
602 next = next->next;
603 BUG_ON(next == &data->trace_pages);
604
605 return next;
606}
607
608static inline notrace void *
609trace_next_page(struct trace_array_cpu *data, void *addr)
610{
611 struct list_head *next;
612 struct page *page;
613
614 page = virt_to_page(addr);
615
616 next = trace_next_list(data, &page->lru);
617 page = list_entry(next, struct page, lru);
618
619 return page_address(page);
620}
621
bc0c38d1 622static inline notrace struct trace_entry *
c7aafc54 623tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
bc0c38d1
SR
624{
625 unsigned long idx, idx_next;
626 struct trace_entry *entry;
627
4c11d7ae 628 data->trace_idx++;
93a588f4 629 idx = data->trace_head_idx;
bc0c38d1
SR
630 idx_next = idx + 1;
631
c7aafc54
IM
632 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
633
93a588f4 634 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
4c11d7ae
SR
635
636 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
93a588f4 637 data->trace_head = trace_next_page(data, data->trace_head);
bc0c38d1
SR
638 idx_next = 0;
639 }
640
93a588f4
SR
641 if (data->trace_head == data->trace_tail &&
642 idx_next == data->trace_tail_idx) {
643 /* overrun */
644 data->trace_tail_idx++;
645 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
646 data->trace_tail =
647 trace_next_page(data, data->trace_tail);
648 data->trace_tail_idx = 0;
649 }
650 }
651
652 data->trace_head_idx = idx_next;
bc0c38d1
SR
653
654 return entry;
655}
656
657static inline notrace void
c7aafc54 658tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
bc0c38d1
SR
659{
660 struct task_struct *tsk = current;
661 unsigned long pc;
662
663 pc = preempt_count();
664
c7aafc54
IM
665 entry->preempt_count = pc & 0xff;
666 entry->pid = tsk->pid;
750ed1a4 667 entry->t = ftrace_now(raw_smp_processor_id());
bc0c38d1
SR
668 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
669 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
670 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
671 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
672}
673
674notrace void
675ftrace(struct trace_array *tr, struct trace_array_cpu *data,
c7aafc54 676 unsigned long ip, unsigned long parent_ip, unsigned long flags)
bc0c38d1
SR
677{
678 struct trace_entry *entry;
679
b3806b43 680 spin_lock(&data->lock);
c7aafc54 681 entry = tracing_get_trace_entry(tr, data);
bc0c38d1 682 tracing_generic_entry_update(entry, flags);
c7aafc54
IM
683 entry->type = TRACE_FN;
684 entry->fn.ip = ip;
685 entry->fn.parent_ip = parent_ip;
b3806b43 686 spin_unlock(&data->lock);
bc0c38d1
SR
687}
688
689notrace void
690tracing_sched_switch_trace(struct trace_array *tr,
691 struct trace_array_cpu *data,
692 struct task_struct *prev, struct task_struct *next,
693 unsigned long flags)
694{
695 struct trace_entry *entry;
696
b3806b43 697 spin_lock(&data->lock);
c7aafc54 698 entry = tracing_get_trace_entry(tr, data);
bc0c38d1
SR
699 tracing_generic_entry_update(entry, flags);
700 entry->type = TRACE_CTX;
701 entry->ctx.prev_pid = prev->pid;
702 entry->ctx.prev_prio = prev->prio;
703 entry->ctx.prev_state = prev->state;
704 entry->ctx.next_pid = next->pid;
705 entry->ctx.next_prio = next->prio;
b3806b43 706 spin_unlock(&data->lock);
bc0c38d1
SR
707}
708
709enum trace_file_type {
710 TRACE_FILE_LAT_FMT = 1,
711};
712
713static struct trace_entry *
4c11d7ae
SR
714trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
715 struct trace_iterator *iter, int cpu)
bc0c38d1 716{
4c11d7ae
SR
717 struct page *page;
718 struct trace_entry *array;
bc0c38d1 719
4c11d7ae 720 if (iter->next_idx[cpu] >= tr->entries ||
b3806b43
SR
721 iter->next_idx[cpu] >= data->trace_idx ||
722 (data->trace_head == data->trace_tail &&
723 data->trace_head_idx == data->trace_tail_idx))
bc0c38d1
SR
724 return NULL;
725
4c11d7ae 726 if (!iter->next_page[cpu]) {
93a588f4
SR
727 /* Initialize the iterator for this cpu trace buffer */
728 WARN_ON(!data->trace_tail);
729 page = virt_to_page(data->trace_tail);
730 iter->next_page[cpu] = &page->lru;
731 iter->next_page_idx[cpu] = data->trace_tail_idx;
4c11d7ae 732 }
bc0c38d1 733
4c11d7ae 734 page = list_entry(iter->next_page[cpu], struct page, lru);
c7aafc54
IM
735 BUG_ON(&data->trace_pages == &page->lru);
736
4c11d7ae
SR
737 array = page_address(page);
738
93a588f4
SR
739 /* Still possible to catch up to the tail */
740 if (iter->next_idx[cpu] && array == data->trace_tail &&
741 iter->next_page_idx[cpu] == data->trace_tail_idx)
742 return NULL;
743
744 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
4c11d7ae 745 return &array[iter->next_page_idx[cpu]];
bc0c38d1
SR
746}
747
748static struct notrace trace_entry *
749find_next_entry(struct trace_iterator *iter, int *ent_cpu)
750{
751 struct trace_array *tr = iter->tr;
752 struct trace_entry *ent, *next = NULL;
753 int next_cpu = -1;
754 int cpu;
755
756 for_each_possible_cpu(cpu) {
c7aafc54 757 if (!head_page(tr->data[cpu]))
bc0c38d1 758 continue;
4c11d7ae 759 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
cdd31cd2
IM
760 /*
761 * Pick the entry with the smallest timestamp:
762 */
763 if (ent && (!next || ent->t < next->t)) {
bc0c38d1
SR
764 next = ent;
765 next_cpu = cpu;
766 }
767 }
768
769 if (ent_cpu)
770 *ent_cpu = next_cpu;
771
772 return next;
773}
774
8c523a9d 775static notrace void trace_iterator_increment(struct trace_iterator *iter)
bc0c38d1 776{
b3806b43
SR
777 iter->idx++;
778 iter->next_idx[iter->cpu]++;
779 iter->next_page_idx[iter->cpu]++;
8c523a9d 780
b3806b43
SR
781 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
782 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
bc0c38d1 783
b3806b43
SR
784 iter->next_page_idx[iter->cpu] = 0;
785 iter->next_page[iter->cpu] =
786 trace_next_list(data, iter->next_page[iter->cpu]);
787 }
788}
bc0c38d1 789
8c523a9d 790static notrace void trace_consume(struct trace_iterator *iter)
b3806b43
SR
791{
792 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
793
794 data->trace_tail_idx++;
795 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
796 data->trace_tail = trace_next_page(data, data->trace_tail);
797 data->trace_tail_idx = 0;
798 }
4e3c3333 799
b3806b43
SR
800 /* Check if we empty it, then reset the index */
801 if (data->trace_head == data->trace_tail &&
802 data->trace_head_idx == data->trace_tail_idx)
803 data->trace_idx = 0;
b3806b43
SR
804}
805
8c523a9d 806static notrace void *find_next_entry_inc(struct trace_iterator *iter)
b3806b43
SR
807{
808 struct trace_entry *next;
809 int next_cpu = -1;
810
811 next = find_next_entry(iter, &next_cpu);
93a588f4 812
4e3c3333
IM
813 iter->prev_ent = iter->ent;
814 iter->prev_cpu = iter->cpu;
815
bc0c38d1
SR
816 iter->ent = next;
817 iter->cpu = next_cpu;
818
b3806b43
SR
819 if (next)
820 trace_iterator_increment(iter);
821
bc0c38d1
SR
822 return next ? iter : NULL;
823}
824
4e3c3333 825static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
826{
827 struct trace_iterator *iter = m->private;
bc0c38d1
SR
828 void *last_ent = iter->ent;
829 int i = (int)*pos;
4e3c3333 830 void *ent;
bc0c38d1
SR
831
832 (*pos)++;
833
834 /* can't go backwards */
835 if (iter->idx > i)
836 return NULL;
837
838 if (iter->idx < 0)
839 ent = find_next_entry_inc(iter);
840 else
841 ent = iter;
842
843 while (ent && iter->idx < i)
844 ent = find_next_entry_inc(iter);
845
846 iter->pos = *pos;
847
848 if (last_ent && !ent)
849 seq_puts(m, "\n\nvim:ft=help\n");
850
851 return ent;
852}
853
854static void *s_start(struct seq_file *m, loff_t *pos)
855{
856 struct trace_iterator *iter = m->private;
857 void *p = NULL;
858 loff_t l = 0;
859 int i;
860
861 mutex_lock(&trace_types_lock);
862
863 if (!current_trace || current_trace != iter->trace)
864 return NULL;
865
866 atomic_inc(&trace_record_cmdline_disabled);
867
868 /* let the tracer grab locks here if needed */
869 if (current_trace->start)
870 current_trace->start(iter);
871
872 if (*pos != iter->pos) {
873 iter->ent = NULL;
874 iter->cpu = 0;
875 iter->idx = -1;
4e3c3333
IM
876 iter->prev_ent = NULL;
877 iter->prev_cpu = -1;
bc0c38d1 878
4c11d7ae 879 for_each_possible_cpu(i) {
bc0c38d1 880 iter->next_idx[i] = 0;
4c11d7ae
SR
881 iter->next_page[i] = NULL;
882 }
bc0c38d1
SR
883
884 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
885 ;
886
887 } else {
4c11d7ae 888 l = *pos - 1;
bc0c38d1
SR
889 p = s_next(m, p, &l);
890 }
891
892 return p;
893}
894
895static void s_stop(struct seq_file *m, void *p)
896{
897 struct trace_iterator *iter = m->private;
898
899 atomic_dec(&trace_record_cmdline_disabled);
900
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);
904
905 mutex_unlock(&trace_types_lock);
906}
907
b3806b43 908static int
214023c3 909seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
bc0c38d1
SR
910{
911#ifdef CONFIG_KALLSYMS
912 char str[KSYM_SYMBOL_LEN];
913
914 kallsyms_lookup(address, NULL, NULL, NULL, str);
915
b3806b43 916 return trace_seq_printf(s, fmt, str);
bc0c38d1 917#endif
b3806b43 918 return 1;
bc0c38d1
SR
919}
920
b3806b43 921static int
214023c3
SR
922seq_print_sym_offset(struct trace_seq *s, const char *fmt,
923 unsigned long address)
bc0c38d1
SR
924{
925#ifdef CONFIG_KALLSYMS
926 char str[KSYM_SYMBOL_LEN];
927
928 sprint_symbol(str, address);
b3806b43 929 return trace_seq_printf(s, fmt, str);
bc0c38d1 930#endif
b3806b43 931 return 1;
bc0c38d1
SR
932}
933
934#ifndef CONFIG_64BIT
935# define IP_FMT "%08lx"
936#else
937# define IP_FMT "%016lx"
938#endif
939
b3806b43 940static notrace int
214023c3 941seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
bc0c38d1 942{
b3806b43
SR
943 int ret;
944
945 if (!ip)
946 return trace_seq_printf(s, "0");
bc0c38d1
SR
947
948 if (sym_flags & TRACE_ITER_SYM_OFFSET)
b3806b43 949 ret = seq_print_sym_offset(s, "%s", ip);
bc0c38d1 950 else
b3806b43
SR
951 ret = seq_print_sym_short(s, "%s", ip);
952
953 if (!ret)
954 return 0;
bc0c38d1
SR
955
956 if (sym_flags & TRACE_ITER_SYM_ADDR)
b3806b43
SR
957 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
958 return ret;
bc0c38d1
SR
959}
960
4e3c3333 961static notrace void print_lat_help_header(struct seq_file *m)
bc0c38d1
SR
962{
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");
972}
973
4e3c3333 974static notrace void print_func_help_header(struct seq_file *m)
bc0c38d1
SR
975{
976 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
977 seq_puts(m, "# | | | | |\n");
978}
979
980
4e3c3333 981static notrace void
bc0c38d1
SR
982print_trace_header(struct seq_file *m, struct trace_iterator *iter)
983{
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;
4c11d7ae
SR
988 unsigned long total = 0;
989 unsigned long entries = 0;
bc0c38d1
SR
990 int cpu;
991 const char *name = "preemption";
992
993 if (type)
994 name = type->name;
995
996 for_each_possible_cpu(cpu) {
c7aafc54 997 if (head_page(tr->data[cpu])) {
4c11d7ae
SR
998 total += tr->data[cpu]->trace_idx;
999 if (tr->data[cpu]->trace_idx > tr->entries)
bc0c38d1 1000 entries += tr->entries;
4c11d7ae 1001 else
bc0c38d1
SR
1002 entries += tr->data[cpu]->trace_idx;
1003 }
1004 }
1005
1006 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1007 name, UTS_RELEASE);
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",
57f50be1 1012 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1013 entries,
4c11d7ae 1014 total,
bc0c38d1
SR
1015 tr->cpu,
1016#if defined(CONFIG_PREEMPT_NONE)
1017 "server",
1018#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1019 "desktop",
1020#elif defined(CONFIG_PREEMPT_DESKTOP)
1021 "preempt",
1022#else
1023 "unknown",
1024#endif
1025 /* These are reserved for later use */
1026 0, 0, 0, 0);
1027#ifdef CONFIG_SMP
1028 seq_printf(m, " #P:%d)\n", num_online_cpus());
1029#else
1030 seq_puts(m, ")\n");
1031#endif
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");
1038
1039 if (data->critical_start) {
1040 seq_puts(m, " => started at: ");
214023c3
SR
1041 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1042 trace_print_seq(m, &iter->seq);
bc0c38d1 1043 seq_puts(m, "\n => ended at: ");
214023c3
SR
1044 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1045 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1046 seq_puts(m, "\n");
1047 }
1048
1049 seq_puts(m, "\n");
1050}
1051
4e3c3333 1052static notrace void
214023c3 1053lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
bc0c38d1
SR
1054{
1055 int hardirq, softirq;
1056 char *comm;
1057
1058 comm = trace_find_cmdline(entry->pid);
1059
214023c3
SR
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' : '.'));
bc0c38d1
SR
1065
1066 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1067 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1068 if (hardirq && softirq)
214023c3 1069 trace_seq_putc(s, 'H');
bc0c38d1
SR
1070 else {
1071 if (hardirq)
214023c3 1072 trace_seq_putc(s, 'h');
bc0c38d1
SR
1073 else {
1074 if (softirq)
214023c3 1075 trace_seq_putc(s, 's');
bc0c38d1 1076 else
214023c3 1077 trace_seq_putc(s, '.');
bc0c38d1
SR
1078 }
1079 }
1080
1081 if (entry->preempt_count)
214023c3 1082 trace_seq_printf(s, "%x", entry->preempt_count);
bc0c38d1 1083 else
214023c3 1084 trace_seq_puts(s, ".");
bc0c38d1
SR
1085}
1086
1087unsigned long preempt_mark_thresh = 100;
1088
4e3c3333 1089static notrace void
214023c3 1090lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
bc0c38d1
SR
1091 unsigned long rel_usecs)
1092{
214023c3 1093 trace_seq_printf(s, " %4lldus", abs_usecs);
bc0c38d1 1094 if (rel_usecs > preempt_mark_thresh)
214023c3 1095 trace_seq_puts(s, "!: ");
bc0c38d1 1096 else if (rel_usecs > 1)
214023c3 1097 trace_seq_puts(s, "+: ");
bc0c38d1 1098 else
214023c3 1099 trace_seq_puts(s, " : ");
bc0c38d1
SR
1100}
1101
1102static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1103
f9896bf3 1104static notrace int
214023c3 1105print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
bc0c38d1 1106{
214023c3 1107 struct trace_seq *s = &iter->seq;
bc0c38d1
SR
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;
1114 char *comm;
1115 int S;
1116
1117 if (!next_entry)
1118 next_entry = entry;
1119 rel_usecs = ns2usecs(next_entry->t - entry->t);
1120 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1121
1122 if (verbose) {
1123 comm = trace_find_cmdline(entry->pid);
214023c3
SR
1124 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1125 " %ld.%03ldms (+%ld.%03ldms): ",
1126 comm,
1127 entry->pid, cpu, entry->flags,
1128 entry->preempt_count, trace_idx,
1129 ns2usecs(entry->t),
1130 abs_usecs/1000,
1131 abs_usecs % 1000, rel_usecs/1000,
1132 rel_usecs % 1000);
bc0c38d1 1133 } else {
214023c3
SR
1134 lat_print_generic(s, entry, cpu);
1135 lat_print_timestamp(s, abs_usecs, rel_usecs);
bc0c38d1
SR
1136 }
1137 switch (entry->type) {
1138 case TRACE_FN:
214023c3
SR
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");
bc0c38d1
SR
1143 break;
1144 case TRACE_CTX:
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);
214023c3
SR
1148 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1149 entry->ctx.prev_pid,
1150 entry->ctx.prev_prio,
1151 S,
1152 entry->ctx.next_pid,
1153 entry->ctx.next_prio,
1154 comm);
bc0c38d1 1155 break;
89b2f978 1156 default:
214023c3 1157 trace_seq_printf(s, "Unknown type %d\n", entry->type);
bc0c38d1 1158 }
f9896bf3 1159 return 1;
bc0c38d1
SR
1160}
1161
f9896bf3 1162static notrace int print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1163{
214023c3 1164 struct trace_seq *s = &iter->seq;
bc0c38d1 1165 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1166 struct trace_entry *entry;
bc0c38d1
SR
1167 unsigned long usec_rem;
1168 unsigned long long t;
1169 unsigned long secs;
1170 char *comm;
1171 int S;
b3806b43 1172 int ret;
bc0c38d1 1173
4e3c3333
IM
1174 entry = iter->ent;
1175
bc0c38d1
SR
1176 comm = trace_find_cmdline(iter->ent->pid);
1177
cdd31cd2 1178 t = ns2usecs(entry->t);
bc0c38d1
SR
1179 usec_rem = do_div(t, 1000000ULL);
1180 secs = (unsigned long)t;
1181
b3806b43
SR
1182 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1183 if (!ret)
1184 return 0;
1185 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1186 if (!ret)
1187 return 0;
1188 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1189 if (!ret)
1190 return 0;
bc0c38d1
SR
1191
1192 switch (entry->type) {
1193 case TRACE_FN:
b3806b43
SR
1194 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1195 if (!ret)
1196 return 0;
bc0c38d1
SR
1197 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1198 entry->fn.parent_ip) {
b3806b43
SR
1199 ret = trace_seq_printf(s, " <-");
1200 if (!ret)
1201 return 0;
1202 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1203 sym_flags);
1204 if (!ret)
1205 return 0;
bc0c38d1 1206 }
b3806b43
SR
1207 ret = trace_seq_printf(s, "\n");
1208 if (!ret)
1209 return 0;
bc0c38d1
SR
1210 break;
1211 case TRACE_CTX:
1212 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1213 state_to_char[entry->ctx.prev_state] : 'X';
b3806b43
SR
1214 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1215 entry->ctx.prev_pid,
1216 entry->ctx.prev_prio,
1217 S,
1218 entry->ctx.next_pid,
1219 entry->ctx.next_prio);
1220 if (!ret)
1221 return 0;
bc0c38d1
SR
1222 break;
1223 }
b3806b43 1224 return 1;
bc0c38d1
SR
1225}
1226
f9896bf3
IM
1227static notrace int print_raw_fmt(struct trace_iterator *iter)
1228{
1229 struct trace_seq *s = &iter->seq;
1230 struct trace_entry *entry;
1231 int ret;
1232 int S;
1233
1234 entry = iter->ent;
1235
1236 ret = trace_seq_printf(s, "%d %d %llu ",
1237 entry->pid, iter->cpu, entry->t);
1238 if (!ret)
1239 return 0;
1240
1241 switch (entry->type) {
1242 case TRACE_FN:
1243 ret = trace_seq_printf(s, "%x %x\n",
1244 entry->fn.ip, entry->fn.parent_ip);
1245 if (!ret)
1246 return 0;
1247 break;
1248 case TRACE_CTX:
1249 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1250 state_to_char[entry->ctx.prev_state] : 'X';
1251 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1252 entry->ctx.prev_pid,
1253 entry->ctx.prev_prio,
1254 S,
1255 entry->ctx.next_pid,
1256 entry->ctx.next_prio);
1257 if (!ret)
1258 return 0;
1259 break;
1260 }
1261 return 1;
1262}
1263
bc0c38d1
SR
1264static int trace_empty(struct trace_iterator *iter)
1265{
1266 struct trace_array_cpu *data;
1267 int cpu;
1268
1269 for_each_possible_cpu(cpu) {
1270 data = iter->tr->data[cpu];
1271
b3806b43
SR
1272 if (head_page(data) && data->trace_idx &&
1273 (data->trace_tail != data->trace_head ||
1274 data->trace_tail_idx != data->trace_head_idx))
bc0c38d1
SR
1275 return 0;
1276 }
1277 return 1;
1278}
1279
f9896bf3
IM
1280static int print_trace_line(struct trace_iterator *iter)
1281{
1282 if (trace_flags & TRACE_ITER_RAW)
1283 return print_raw_fmt(iter);
1284
1285 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1286 return print_lat_fmt(iter, iter->idx, iter->cpu);
1287
1288 return print_trace_fmt(iter);
1289}
1290
bc0c38d1
SR
1291static int s_show(struct seq_file *m, void *v)
1292{
1293 struct trace_iterator *iter = v;
1294
1295 if (iter->ent == NULL) {
1296 if (iter->tr) {
1297 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1298 seq_puts(m, "#\n");
1299 }
1300 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1301 /* print nothing if the buffers are empty */
1302 if (trace_empty(iter))
1303 return 0;
1304 print_trace_header(m, iter);
1305 if (!(trace_flags & TRACE_ITER_VERBOSE))
1306 print_lat_help_header(m);
1307 } else {
1308 if (!(trace_flags & TRACE_ITER_VERBOSE))
1309 print_func_help_header(m);
1310 }
1311 } else {
f9896bf3 1312 print_trace_line(iter);
214023c3 1313 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1314 }
1315
1316 return 0;
1317}
1318
1319static struct seq_operations tracer_seq_ops = {
4bf39a94
IM
1320 .start = s_start,
1321 .next = s_next,
1322 .stop = s_stop,
1323 .show = s_show,
bc0c38d1
SR
1324};
1325
1326static struct trace_iterator notrace *
1327__tracing_open(struct inode *inode, struct file *file, int *ret)
1328{
1329 struct trace_iterator *iter;
1330
60a11774
SR
1331 if (tracing_disabled) {
1332 *ret = -ENODEV;
1333 return NULL;
1334 }
1335
bc0c38d1
SR
1336 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1337 if (!iter) {
1338 *ret = -ENOMEM;
1339 goto out;
1340 }
1341
1342 mutex_lock(&trace_types_lock);
1343 if (current_trace && current_trace->print_max)
1344 iter->tr = &max_tr;
1345 else
1346 iter->tr = inode->i_private;
1347 iter->trace = current_trace;
1348 iter->pos = -1;
1349
1350 /* TODO stop tracer */
1351 *ret = seq_open(file, &tracer_seq_ops);
1352 if (!*ret) {
1353 struct seq_file *m = file->private_data;
1354 m->private = iter;
1355
1356 /* stop the trace while dumping */
1357 if (iter->tr->ctrl)
1358 tracer_enabled = 0;
1359
1360 if (iter->trace && iter->trace->open)
1361 iter->trace->open(iter);
1362 } else {
1363 kfree(iter);
1364 iter = NULL;
1365 }
1366 mutex_unlock(&trace_types_lock);
1367
1368 out:
1369 return iter;
1370}
1371
1372int tracing_open_generic(struct inode *inode, struct file *filp)
1373{
60a11774
SR
1374 if (tracing_disabled)
1375 return -ENODEV;
1376
bc0c38d1
SR
1377 filp->private_data = inode->i_private;
1378 return 0;
1379}
1380
1381int tracing_release(struct inode *inode, struct file *file)
1382{
1383 struct seq_file *m = (struct seq_file *)file->private_data;
1384 struct trace_iterator *iter = m->private;
1385
1386 mutex_lock(&trace_types_lock);
1387 if (iter->trace && iter->trace->close)
1388 iter->trace->close(iter);
1389
1390 /* reenable tracing if it was previously enabled */
1391 if (iter->tr->ctrl)
1392 tracer_enabled = 1;
1393 mutex_unlock(&trace_types_lock);
1394
1395 seq_release(inode, file);
1396 kfree(iter);
1397 return 0;
1398}
1399
1400static int tracing_open(struct inode *inode, struct file *file)
1401{
1402 int ret;
1403
1404 __tracing_open(inode, file, &ret);
1405
1406 return ret;
1407}
1408
1409static int tracing_lt_open(struct inode *inode, struct file *file)
1410{
1411 struct trace_iterator *iter;
1412 int ret;
1413
1414 iter = __tracing_open(inode, file, &ret);
1415
1416 if (!ret)
1417 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1418
1419 return ret;
1420}
1421
1422
4e3c3333 1423static notrace void *
bc0c38d1
SR
1424t_next(struct seq_file *m, void *v, loff_t *pos)
1425{
1426 struct tracer *t = m->private;
1427
1428 (*pos)++;
1429
1430 if (t)
1431 t = t->next;
1432
1433 m->private = t;
1434
1435 return t;
1436}
1437
1438static void *t_start(struct seq_file *m, loff_t *pos)
1439{
1440 struct tracer *t = m->private;
1441 loff_t l = 0;
1442
1443 mutex_lock(&trace_types_lock);
1444 for (; t && l < *pos; t = t_next(m, t, &l))
1445 ;
1446
1447 return t;
1448}
1449
1450static void t_stop(struct seq_file *m, void *p)
1451{
1452 mutex_unlock(&trace_types_lock);
1453}
1454
1455static int t_show(struct seq_file *m, void *v)
1456{
1457 struct tracer *t = v;
1458
1459 if (!t)
1460 return 0;
1461
1462 seq_printf(m, "%s", t->name);
1463 if (t->next)
1464 seq_putc(m, ' ');
1465 else
1466 seq_putc(m, '\n');
1467
1468 return 0;
1469}
1470
1471static struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
1472 .start = t_start,
1473 .next = t_next,
1474 .stop = t_stop,
1475 .show = t_show,
bc0c38d1
SR
1476};
1477
1478static int show_traces_open(struct inode *inode, struct file *file)
1479{
1480 int ret;
1481
60a11774
SR
1482 if (tracing_disabled)
1483 return -ENODEV;
1484
bc0c38d1
SR
1485 ret = seq_open(file, &show_traces_seq_ops);
1486 if (!ret) {
1487 struct seq_file *m = file->private_data;
1488 m->private = trace_types;
1489 }
1490
1491 return ret;
1492}
1493
1494static struct file_operations tracing_fops = {
4bf39a94
IM
1495 .open = tracing_open,
1496 .read = seq_read,
1497 .llseek = seq_lseek,
1498 .release = tracing_release,
bc0c38d1
SR
1499};
1500
1501static struct file_operations tracing_lt_fops = {
4bf39a94
IM
1502 .open = tracing_lt_open,
1503 .read = seq_read,
1504 .llseek = seq_lseek,
1505 .release = tracing_release,
bc0c38d1
SR
1506};
1507
1508static struct file_operations show_traces_fops = {
1509 .open = show_traces_open,
1510 .read = seq_read,
1511 .release = seq_release,
1512};
1513
1514static ssize_t
1515tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1516 size_t cnt, loff_t *ppos)
1517{
1518 char *buf;
1519 int r = 0;
1520 int len = 0;
1521 int i;
1522
1523 /* calulate max size */
1524 for (i = 0; trace_options[i]; i++) {
1525 len += strlen(trace_options[i]);
1526 len += 3; /* "no" and space */
1527 }
1528
1529 /* +2 for \n and \0 */
1530 buf = kmalloc(len + 2, GFP_KERNEL);
1531 if (!buf)
1532 return -ENOMEM;
1533
1534 for (i = 0; trace_options[i]; i++) {
1535 if (trace_flags & (1 << i))
1536 r += sprintf(buf + r, "%s ", trace_options[i]);
1537 else
1538 r += sprintf(buf + r, "no%s ", trace_options[i]);
1539 }
1540
1541 r += sprintf(buf + r, "\n");
1542 WARN_ON(r >= len + 2);
1543
1544 r = simple_read_from_buffer(ubuf, cnt, ppos,
1545 buf, r);
1546
1547 kfree(buf);
1548
1549 return r;
1550}
1551
1552static ssize_t
1553tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1554 size_t cnt, loff_t *ppos)
1555{
1556 char buf[64];
1557 char *cmp = buf;
1558 int neg = 0;
1559 int i;
1560
1561 if (cnt > 63)
1562 cnt = 63;
1563
1564 if (copy_from_user(&buf, ubuf, cnt))
1565 return -EFAULT;
1566
1567 buf[cnt] = 0;
1568
1569 if (strncmp(buf, "no", 2) == 0) {
1570 neg = 1;
1571 cmp += 2;
1572 }
1573
1574 for (i = 0; trace_options[i]; i++) {
1575 int len = strlen(trace_options[i]);
1576
1577 if (strncmp(cmp, trace_options[i], len) == 0) {
1578 if (neg)
1579 trace_flags &= ~(1 << i);
1580 else
1581 trace_flags |= (1 << i);
1582 break;
1583 }
1584 }
1585
1586 filp->f_pos += cnt;
1587
1588 return cnt;
1589}
1590
1591static struct file_operations tracing_iter_fops = {
1592 .open = tracing_open_generic,
1593 .read = tracing_iter_ctrl_read,
1594 .write = tracing_iter_ctrl_write,
1595};
1596
7bd2f24c
IM
1597static const char readme_msg[] =
1598 "tracing mini-HOWTO:\n\n"
1599 "# mkdir /debug\n"
1600 "# mount -t debugfs nodev /debug\n\n"
1601 "# cat /debug/tracing/available_tracers\n"
1602 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1603 "# cat /debug/tracing/current_tracer\n"
1604 "none\n"
1605 "# echo sched_switch > /debug/tracing/current_tracer\n"
1606 "# cat /debug/tracing/current_tracer\n"
1607 "sched_switch\n"
1608 "# cat /debug/tracing/iter_ctrl\n"
1609 "noprint-parent nosym-offset nosym-addr noverbose\n"
1610 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1611 "# echo 1 > /debug/tracing/tracing_enabled\n"
1612 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1613 "echo 0 > /debug/tracing/tracing_enabled\n"
1614;
1615
1616static ssize_t
1617tracing_readme_read(struct file *filp, char __user *ubuf,
1618 size_t cnt, loff_t *ppos)
1619{
1620 return simple_read_from_buffer(ubuf, cnt, ppos,
1621 readme_msg, strlen(readme_msg));
1622}
1623
1624static struct file_operations tracing_readme_fops = {
1625 .open = tracing_open_generic,
1626 .read = tracing_readme_read,
1627};
1628
1629
bc0c38d1
SR
1630static ssize_t
1631tracing_ctrl_read(struct file *filp, char __user *ubuf,
1632 size_t cnt, loff_t *ppos)
1633{
1634 struct trace_array *tr = filp->private_data;
1635 char buf[64];
1636 int r;
1637
1638 r = sprintf(buf, "%ld\n", tr->ctrl);
4e3c3333 1639 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1640}
1641
1642static ssize_t
1643tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1644 size_t cnt, loff_t *ppos)
1645{
1646 struct trace_array *tr = filp->private_data;
1647 long val;
1648 char buf[64];
1649
1650 if (cnt > 63)
1651 cnt = 63;
1652
1653 if (copy_from_user(&buf, ubuf, cnt))
1654 return -EFAULT;
1655
1656 buf[cnt] = 0;
1657
1658 val = simple_strtoul(buf, NULL, 10);
1659
1660 val = !!val;
1661
1662 mutex_lock(&trace_types_lock);
1663 if (tr->ctrl ^ val) {
1664 if (val)
1665 tracer_enabled = 1;
1666 else
1667 tracer_enabled = 0;
1668
1669 tr->ctrl = val;
1670
1671 if (current_trace && current_trace->ctrl_update)
1672 current_trace->ctrl_update(tr);
1673 }
1674 mutex_unlock(&trace_types_lock);
1675
1676 filp->f_pos += cnt;
1677
1678 return cnt;
1679}
1680
1681static ssize_t
1682tracing_set_trace_read(struct file *filp, char __user *ubuf,
1683 size_t cnt, loff_t *ppos)
1684{
1685 char buf[max_tracer_type_len+2];
1686 int r;
1687
1688 mutex_lock(&trace_types_lock);
1689 if (current_trace)
1690 r = sprintf(buf, "%s\n", current_trace->name);
1691 else
1692 r = sprintf(buf, "\n");
1693 mutex_unlock(&trace_types_lock);
1694
4bf39a94 1695 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1696}
1697
1698static ssize_t
1699tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1700 size_t cnt, loff_t *ppos)
1701{
1702 struct trace_array *tr = &global_trace;
1703 struct tracer *t;
1704 char buf[max_tracer_type_len+1];
1705 int i;
1706
1707 if (cnt > max_tracer_type_len)
1708 cnt = max_tracer_type_len;
1709
1710 if (copy_from_user(&buf, ubuf, cnt))
1711 return -EFAULT;
1712
1713 buf[cnt] = 0;
1714
1715 /* strip ending whitespace. */
1716 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1717 buf[i] = 0;
1718
1719 mutex_lock(&trace_types_lock);
1720 for (t = trace_types; t; t = t->next) {
1721 if (strcmp(t->name, buf) == 0)
1722 break;
1723 }
1724 if (!t || t == current_trace)
1725 goto out;
1726
1727 if (current_trace && current_trace->reset)
1728 current_trace->reset(tr);
1729
1730 current_trace = t;
1731 if (t->init)
1732 t->init(tr);
1733
1734 out:
1735 mutex_unlock(&trace_types_lock);
1736
1737 filp->f_pos += cnt;
1738
1739 return cnt;
1740}
1741
1742static ssize_t
1743tracing_max_lat_read(struct file *filp, char __user *ubuf,
1744 size_t cnt, loff_t *ppos)
1745{
1746 unsigned long *ptr = filp->private_data;
1747 char buf[64];
1748 int r;
1749
1750 r = snprintf(buf, 64, "%ld\n",
1751 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1752 if (r > 64)
1753 r = 64;
4bf39a94 1754 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1755}
1756
1757static ssize_t
1758tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1759 size_t cnt, loff_t *ppos)
1760{
1761 long *ptr = filp->private_data;
1762 long val;
1763 char buf[64];
1764
1765 if (cnt > 63)
1766 cnt = 63;
1767
1768 if (copy_from_user(&buf, ubuf, cnt))
1769 return -EFAULT;
1770
1771 buf[cnt] = 0;
1772
1773 val = simple_strtoul(buf, NULL, 10);
1774
1775 *ptr = val * 1000;
1776
1777 return cnt;
1778}
1779
b3806b43
SR
1780static atomic_t tracing_reader;
1781
1782static int tracing_open_pipe(struct inode *inode, struct file *filp)
1783{
1784 struct trace_iterator *iter;
1785
1786 if (tracing_disabled)
1787 return -ENODEV;
1788
1789 /* We only allow for reader of the pipe */
1790 if (atomic_inc_return(&tracing_reader) != 1) {
1791 atomic_dec(&tracing_reader);
1792 return -EBUSY;
1793 }
1794
1795 /* create a buffer to store the information to pass to userspace */
1796 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1797 if (!iter)
1798 return -ENOMEM;
1799
1800 iter->tr = &global_trace;
1801
1802 filp->private_data = iter;
1803
1804 return 0;
1805}
1806
1807static int tracing_release_pipe(struct inode *inode, struct file *file)
1808{
1809 struct trace_iterator *iter = file->private_data;
1810
1811 kfree(iter);
1812 atomic_dec(&tracing_reader);
1813
1814 return 0;
1815}
1816
1817/*
1818 * Consumer reader.
1819 */
1820static ssize_t
1821tracing_read_pipe(struct file *filp, char __user *ubuf,
1822 size_t cnt, loff_t *ppos)
1823{
1824 struct trace_iterator *iter = filp->private_data;
1825 struct trace_array_cpu *data;
1826 static cpumask_t mask;
1827 struct trace_entry *entry;
1828 static int start;
1829 unsigned long flags;
1830 int read = 0;
1831 int cpu;
1832 int len;
1833 int ret;
1834
1835 /* return any leftover data */
1836 if (iter->seq.len > start) {
1837 len = iter->seq.len - start;
1838 if (cnt > len)
1839 cnt = len;
1840 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1841 if (ret)
1842 cnt = -EFAULT;
1843
1844 start += len;
1845
1846 return cnt;
1847 }
1848
1849 trace_seq_reset(&iter->seq);
1850 start = 0;
1851
1852 while (trace_empty(iter)) {
1853 /*
1854 * This is a make-shift waitqueue. The reason we don't use
1855 * an actual wait queue is because:
1856 * 1) we only ever have one waiter
1857 * 2) the tracing, traces all functions, we don't want
1858 * the overhead of calling wake_up and friends
1859 * (and tracing them too)
1860 * Anyway, this is really very primitive wakeup.
1861 */
1862 set_current_state(TASK_INTERRUPTIBLE);
1863 iter->tr->waiter = current;
1864
1865 /* sleep for one second, and try again. */
1866 schedule_timeout(HZ);
1867
1868 iter->tr->waiter = NULL;
1869
1870 if (signal_pending(current))
1871 return -EINTR;
1872
1873 /*
1874 * We block until we read something and tracing is disabled.
1875 * We still block if tracing is disabled, but we have never
1876 * read anything. This allows a user to cat this file, and
1877 * then enable tracing. But after we have read something,
1878 * we give an EOF when tracing is again disabled.
1879 *
1880 * iter->pos will be 0 if we haven't read anything.
1881 */
1882 if (!tracer_enabled && iter->pos)
1883 break;
1884
1885 continue;
1886 }
1887
1888 /* stop when tracing is finished */
1889 if (trace_empty(iter))
1890 return 0;
1891
1892 if (cnt >= PAGE_SIZE)
1893 cnt = PAGE_SIZE - 1;
1894
1895 memset(iter, 0, sizeof(*iter));
1896 iter->tr = &global_trace;
1897 iter->pos = -1;
1898
1899 /*
1900 * We need to stop all tracing on all CPUS to read the
1901 * the next buffer. This is a bit expensive, but is
1902 * not done often. We fill all what we can read,
1903 * and then release the locks again.
1904 */
1905
1906 cpus_clear(mask);
1907 local_irq_save(flags);
1908 for_each_possible_cpu(cpu) {
1909 data = iter->tr->data[cpu];
1910
1911 if (!head_page(data) || !data->trace_idx)
1912 continue;
1913
1914 atomic_inc(&data->disabled);
1915 spin_lock(&data->lock);
1916 cpu_set(cpu, mask);
1917 }
1918
8c523a9d 1919 while ((entry = find_next_entry_inc(iter)) != NULL) {
f9896bf3 1920 ret = print_trace_line(iter);
b3806b43
SR
1921 if (!ret)
1922 break;
1923
1924 trace_consume(iter);
1925
1926 if (iter->seq.len >= cnt)
1927 break;
b3806b43
SR
1928 }
1929
d4c5a2f5 1930 for_each_cpu_mask(cpu, mask) {
b3806b43 1931 data = iter->tr->data[cpu];
b3806b43
SR
1932 spin_unlock(&data->lock);
1933 atomic_dec(&data->disabled);
1934 }
1935 local_irq_restore(flags);
1936
1937 /* Now copy what we have to the user */
1938 read = iter->seq.len;
1939 if (read > cnt)
1940 read = cnt;
1941
1942 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1943
1944 if (read < iter->seq.len)
1945 start = read;
1946 else
1947 trace_seq_reset(&iter->seq);
1948
1949 if (ret)
1950 read = -EFAULT;
1951
1952 return read;
1953}
1954
bc0c38d1 1955static struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
1956 .open = tracing_open_generic,
1957 .read = tracing_max_lat_read,
1958 .write = tracing_max_lat_write,
bc0c38d1
SR
1959};
1960
1961static struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
1962 .open = tracing_open_generic,
1963 .read = tracing_ctrl_read,
1964 .write = tracing_ctrl_write,
bc0c38d1
SR
1965};
1966
1967static struct file_operations set_tracer_fops = {
4bf39a94
IM
1968 .open = tracing_open_generic,
1969 .read = tracing_set_trace_read,
1970 .write = tracing_set_trace_write,
bc0c38d1
SR
1971};
1972
b3806b43 1973static struct file_operations tracing_pipe_fops = {
4bf39a94
IM
1974 .open = tracing_open_pipe,
1975 .read = tracing_read_pipe,
1976 .release = tracing_release_pipe,
b3806b43
SR
1977};
1978
bc0c38d1
SR
1979#ifdef CONFIG_DYNAMIC_FTRACE
1980
1981static ssize_t
1982tracing_read_long(struct file *filp, char __user *ubuf,
1983 size_t cnt, loff_t *ppos)
1984{
1985 unsigned long *p = filp->private_data;
1986 char buf[64];
1987 int r;
1988
1989 r = sprintf(buf, "%ld\n", *p);
4bf39a94
IM
1990
1991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1992}
1993
1994static struct file_operations tracing_read_long_fops = {
4bf39a94
IM
1995 .open = tracing_open_generic,
1996 .read = tracing_read_long,
bc0c38d1
SR
1997};
1998#endif
1999
2000static struct dentry *d_tracer;
2001
2002struct dentry *tracing_init_dentry(void)
2003{
2004 static int once;
2005
2006 if (d_tracer)
2007 return d_tracer;
2008
2009 d_tracer = debugfs_create_dir("tracing", NULL);
2010
2011 if (!d_tracer && !once) {
2012 once = 1;
2013 pr_warning("Could not create debugfs directory 'tracing'\n");
2014 return NULL;
2015 }
2016
2017 return d_tracer;
2018}
2019
60a11774
SR
2020#ifdef CONFIG_FTRACE_SELFTEST
2021/* Let selftest have access to static functions in this file */
2022#include "trace_selftest.c"
2023#endif
2024
bc0c38d1
SR
2025static __init void tracer_init_debugfs(void)
2026{
2027 struct dentry *d_tracer;
2028 struct dentry *entry;
2029
2030 d_tracer = tracing_init_dentry();
2031
2032 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2033 &global_trace, &tracing_ctrl_fops);
2034 if (!entry)
2035 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2036
2037 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2038 NULL, &tracing_iter_fops);
2039 if (!entry)
2040 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2041
2042 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2043 &global_trace, &tracing_lt_fops);
2044 if (!entry)
2045 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2046
2047 entry = debugfs_create_file("trace", 0444, d_tracer,
2048 &global_trace, &tracing_fops);
2049 if (!entry)
2050 pr_warning("Could not create debugfs 'trace' entry\n");
2051
2052 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2053 &global_trace, &show_traces_fops);
2054 if (!entry)
2055 pr_warning("Could not create debugfs 'trace' entry\n");
2056
2057 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2058 &global_trace, &set_tracer_fops);
2059 if (!entry)
2060 pr_warning("Could not create debugfs 'trace' entry\n");
2061
2062 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2063 &tracing_max_latency,
2064 &tracing_max_lat_fops);
2065 if (!entry)
2066 pr_warning("Could not create debugfs "
2067 "'tracing_max_latency' entry\n");
2068
2069 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2070 &tracing_thresh, &tracing_max_lat_fops);
2071 if (!entry)
2072 pr_warning("Could not create debugfs "
2073 "'tracing_threash' entry\n");
7bd2f24c
IM
2074 entry = debugfs_create_file("README", 0644, d_tracer,
2075 NULL, &tracing_readme_fops);
2076 if (!entry)
2077 pr_warning("Could not create debugfs 'README' entry\n");
2078
b3806b43
SR
2079 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2080 NULL, &tracing_pipe_fops);
2081 if (!entry)
2082 pr_warning("Could not create debugfs "
2083 "'tracing_threash' entry\n");
bc0c38d1
SR
2084
2085#ifdef CONFIG_DYNAMIC_FTRACE
2086 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2087 &ftrace_update_tot_cnt,
2088 &tracing_read_long_fops);
2089 if (!entry)
2090 pr_warning("Could not create debugfs "
2091 "'dyn_ftrace_total_info' entry\n");
2092#endif
2093}
2094
2095/* dummy trace to disable tracing */
2096static struct tracer no_tracer __read_mostly =
2097{
4bf39a94 2098 .name = "none",
bc0c38d1
SR
2099};
2100
4c11d7ae 2101static int trace_alloc_page(void)
bc0c38d1 2102{
4c11d7ae 2103 struct trace_array_cpu *data;
4c11d7ae
SR
2104 struct page *page, *tmp;
2105 LIST_HEAD(pages);
c7aafc54 2106 void *array;
4c11d7ae
SR
2107 int i;
2108
2109 /* first allocate a page for each CPU */
2110 for_each_possible_cpu(i) {
2111 array = (void *)__get_free_page(GFP_KERNEL);
2112 if (array == NULL) {
2113 printk(KERN_ERR "tracer: failed to allocate page"
2114 "for trace buffer!\n");
2115 goto free_pages;
2116 }
2117
2118 page = virt_to_page(array);
2119 list_add(&page->lru, &pages);
2120
2121/* Only allocate if we are actually using the max trace */
2122#ifdef CONFIG_TRACER_MAX_TRACE
2123 array = (void *)__get_free_page(GFP_KERNEL);
2124 if (array == NULL) {
2125 printk(KERN_ERR "tracer: failed to allocate page"
2126 "for trace buffer!\n");
2127 goto free_pages;
2128 }
2129 page = virt_to_page(array);
2130 list_add(&page->lru, &pages);
2131#endif
2132 }
2133
2134 /* Now that we successfully allocate a page per CPU, add them */
2135 for_each_possible_cpu(i) {
2136 data = global_trace.data[i];
b3806b43 2137 spin_lock_init(&data->lock);
d4c5a2f5 2138 lockdep_set_class(&data->lock, &data->lock_key);
4c11d7ae 2139 page = list_entry(pages.next, struct page, lru);
c7aafc54 2140 list_del_init(&page->lru);
4c11d7ae
SR
2141 list_add_tail(&page->lru, &data->trace_pages);
2142 ClearPageLRU(page);
2143
2144#ifdef CONFIG_TRACER_MAX_TRACE
2145 data = max_tr.data[i];
b3806b43 2146 spin_lock_init(&data->lock);
d4c5a2f5 2147 lockdep_set_class(&data->lock, &data->lock_key);
4c11d7ae 2148 page = list_entry(pages.next, struct page, lru);
c7aafc54 2149 list_del_init(&page->lru);
4c11d7ae
SR
2150 list_add_tail(&page->lru, &data->trace_pages);
2151 SetPageLRU(page);
2152#endif
2153 }
2154 global_trace.entries += ENTRIES_PER_PAGE;
2155
2156 return 0;
2157
2158 free_pages:
2159 list_for_each_entry_safe(page, tmp, &pages, lru) {
c7aafc54 2160 list_del_init(&page->lru);
4c11d7ae
SR
2161 __free_page(page);
2162 }
2163 return -ENOMEM;
bc0c38d1
SR
2164}
2165
2166__init static int tracer_alloc_buffers(void)
2167{
4c11d7ae
SR
2168 struct trace_array_cpu *data;
2169 void *array;
2170 struct page *page;
2171 int pages = 0;
60a11774 2172 int ret = -ENOMEM;
bc0c38d1
SR
2173 int i;
2174
4c11d7ae 2175 /* Allocate the first page for all buffers */
bc0c38d1 2176 for_each_possible_cpu(i) {
4c11d7ae 2177 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1
SR
2178 max_tr.data[i] = &per_cpu(max_data, i);
2179
4c11d7ae 2180 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 2181 if (array == NULL) {
4c11d7ae
SR
2182 printk(KERN_ERR "tracer: failed to allocate page"
2183 "for trace buffer!\n");
bc0c38d1
SR
2184 goto free_buffers;
2185 }
4c11d7ae
SR
2186
2187 /* set the array to the list */
2188 INIT_LIST_HEAD(&data->trace_pages);
2189 page = virt_to_page(array);
2190 list_add(&page->lru, &data->trace_pages);
2191 /* use the LRU flag to differentiate the two buffers */
2192 ClearPageLRU(page);
bc0c38d1
SR
2193
2194/* Only allocate if we are actually using the max trace */
2195#ifdef CONFIG_TRACER_MAX_TRACE
4c11d7ae 2196 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 2197 if (array == NULL) {
4c11d7ae
SR
2198 printk(KERN_ERR "tracer: failed to allocate page"
2199 "for trace buffer!\n");
bc0c38d1
SR
2200 goto free_buffers;
2201 }
4c11d7ae
SR
2202
2203 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2204 page = virt_to_page(array);
2205 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2206 SetPageLRU(page);
bc0c38d1
SR
2207#endif
2208 }
2209
2210 /*
2211 * Since we allocate by orders of pages, we may be able to
2212 * round up a bit.
2213 */
4c11d7ae 2214 global_trace.entries = ENTRIES_PER_PAGE;
4c11d7ae
SR
2215 pages++;
2216
2217 while (global_trace.entries < trace_nr_entries) {
2218 if (trace_alloc_page())
2219 break;
2220 pages++;
2221 }
89b2f978 2222 max_tr.entries = global_trace.entries;
bc0c38d1 2223
4c11d7ae
SR
2224 pr_info("tracer: %d pages allocated for %ld",
2225 pages, trace_nr_entries);
bc0c38d1
SR
2226 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2227 pr_info(" actual entries %ld\n", global_trace.entries);
2228
2229 tracer_init_debugfs();
2230
2231 trace_init_cmdlines();
2232
2233 register_tracer(&no_tracer);
2234 current_trace = &no_tracer;
2235
60a11774
SR
2236 /* All seems OK, enable tracing */
2237 tracing_disabled = 0;
2238
bc0c38d1
SR
2239 return 0;
2240
2241 free_buffers:
2242 for (i-- ; i >= 0; i--) {
4c11d7ae 2243 struct page *page, *tmp;
bc0c38d1
SR
2244 struct trace_array_cpu *data = global_trace.data[i];
2245
c7aafc54 2246 if (data) {
4c11d7ae
SR
2247 list_for_each_entry_safe(page, tmp,
2248 &data->trace_pages, lru) {
c7aafc54 2249 list_del_init(&page->lru);
4c11d7ae
SR
2250 __free_page(page);
2251 }
bc0c38d1
SR
2252 }
2253
2254#ifdef CONFIG_TRACER_MAX_TRACE
2255 data = max_tr.data[i];
c7aafc54 2256 if (data) {
4c11d7ae
SR
2257 list_for_each_entry_safe(page, tmp,
2258 &data->trace_pages, lru) {
c7aafc54 2259 list_del_init(&page->lru);
4c11d7ae
SR
2260 __free_page(page);
2261 }
bc0c38d1
SR
2262 }
2263#endif
2264 }
60a11774 2265 return ret;
bc0c38d1 2266}
60a11774 2267fs_initcall(tracer_alloc_buffers);