blob: 70f94fa92c105f2c47951f89b9ac5c6e61067aed [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
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>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020018#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020019#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
Steven Rostedt60a11772008-05-12 21:20:44 +020035static int tracing_disabled = 1;
36
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020037static long notrace
38ns2usecs(cycle_t nsec)
39{
40 nsec += 500;
41 do_div(nsec, 1000);
42 return nsec;
43}
44
Ingo Molnar53c37c12008-05-12 21:20:46 +020045static 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
Ingo Molnar750ed1a2008-05-12 21:20:46 +020087notrace cycle_t ftrace_now(int cpu)
88{
Ingo Molnar53c37c12008-05-12 21:20:46 +020089 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;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200100}
101
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200102static 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;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200111static unsigned long trace_nr_entries = 16384UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200112
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
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200119#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
120
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200121static 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
Steven Rostedt57f50be2008-05-12 21:20:44 +0200130unsigned long nsecs_to_usecs(unsigned long nsecs)
131{
132 return nsecs / 1000;
133}
134
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200135enum trace_type {
136 __TRACE_FIRST_TYPE = 0,
137
138 TRACE_FN,
139 TRACE_CTX,
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200140 TRACE_SPECIAL,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200141
142 __TRACE_LAST_TYPE
143};
144
145enum trace_flag_type {
146 TRACE_FLAG_IRQS_OFF = 0x01,
147 TRACE_FLAG_NEED_RESCHED = 0x02,
148 TRACE_FLAG_HARDIRQ = 0x04,
149 TRACE_FLAG_SOFTIRQ = 0x08,
150};
151
152enum trace_iterator_flags {
153 TRACE_ITER_PRINT_PARENT = 0x01,
154 TRACE_ITER_SYM_OFFSET = 0x02,
155 TRACE_ITER_SYM_ADDR = 0x04,
156 TRACE_ITER_VERBOSE = 0x08,
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200157 TRACE_ITER_RAW = 0x10,
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200158 TRACE_ITER_BIN = 0x20,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200159};
160
161#define TRACE_ITER_SYM_MASK \
162 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
163
164/* These must match the bit postions above */
165static const char *trace_options[] = {
166 "print-parent",
167 "sym-offset",
168 "sym-addr",
169 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200170 "raw",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200171 "bin",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200172 NULL
173};
174
175static unsigned trace_flags;
176
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200177static DEFINE_SPINLOCK(ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200178
179/*
180 * Copy the new maximum trace into the separate maximum-trace
181 * structure. (this way the maximum trace is permanently saved,
182 * for later retrieval via /debugfs/tracing/latency_trace)
183 */
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200184static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200185__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
186{
187 struct trace_array_cpu *data = tr->data[cpu];
188
189 max_tr.cpu = cpu;
190 max_tr.time_start = data->preempt_timestamp;
191
192 data = max_tr.data[cpu];
193 data->saved_latency = tracing_max_latency;
194
195 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
196 data->pid = tsk->pid;
197 data->uid = tsk->uid;
198 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
199 data->policy = tsk->policy;
200 data->rt_priority = tsk->rt_priority;
201
202 /* record this tasks comm */
203 tracing_record_cmdline(current);
204}
205
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200206void check_pages(struct trace_array_cpu *data)
207{
208 struct page *page, *tmp;
209
210 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
211 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
212
213 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
214 BUG_ON(page->lru.next->prev != &page->lru);
215 BUG_ON(page->lru.prev->next != &page->lru);
216 }
217}
218
219void *head_page(struct trace_array_cpu *data)
220{
221 struct page *page;
222
223 check_pages(data);
224 if (list_empty(&data->trace_pages))
225 return NULL;
226
227 page = list_entry(data->trace_pages.next, struct page, lru);
228 BUG_ON(&page->lru == &data->trace_pages);
229
230 return page_address(page);
231}
232
Steven Rostedt214023c2008-05-12 21:20:46 +0200233static notrace int
234trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
235{
236 int len = (PAGE_SIZE - 1) - s->len;
237 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200238 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200239
240 if (!len)
241 return 0;
242
243 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200244 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200245 va_end(ap);
246
Steven Rostedtb3806b42008-05-12 21:20:46 +0200247 /* If we can't write it all, don't bother writing anything */
248 if (ret > len)
249 return 0;
250
251 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200252
253 return len;
254}
255
256static notrace int
257trace_seq_puts(struct trace_seq *s, const char *str)
258{
259 int len = strlen(str);
260
261 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200262 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200263
264 memcpy(s->buffer + s->len, str, len);
265 s->len += len;
266
267 return len;
268}
269
270static notrace int
271trace_seq_putc(struct trace_seq *s, unsigned char c)
272{
273 if (s->len >= (PAGE_SIZE - 1))
274 return 0;
275
276 s->buffer[s->len++] = c;
277
278 return 1;
279}
280
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200281static notrace int
282trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
283{
284 if (len > ((PAGE_SIZE - 1) - s->len))
285 return 0;
286
287 memcpy(s->buffer + s->len, mem, len);
288 s->len += len;
289
290 return len;
291}
292
Steven Rostedt214023c2008-05-12 21:20:46 +0200293static notrace void
294trace_seq_reset(struct trace_seq *s)
295{
296 s->len = 0;
297}
298
299static notrace void
300trace_print_seq(struct seq_file *m, struct trace_seq *s)
301{
302 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
303
304 s->buffer[len] = 0;
305 seq_puts(m, s->buffer);
306
307 trace_seq_reset(s);
308}
309
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200310notrace static void
311flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
312{
313 struct list_head flip_pages;
314
315 INIT_LIST_HEAD(&flip_pages);
316
Steven Rostedt93a588f2008-05-12 21:20:45 +0200317 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200318 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200319 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200320
321 check_pages(tr1);
322 check_pages(tr2);
323 list_splice_init(&tr1->trace_pages, &flip_pages);
324 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
325 list_splice_init(&flip_pages, &tr2->trace_pages);
326 BUG_ON(!list_empty(&flip_pages));
327 check_pages(tr1);
328 check_pages(tr2);
329}
330
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200331notrace void
332update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
333{
334 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200335 int i;
336
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200337 WARN_ON_ONCE(!irqs_disabled());
338 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200339 /* clear out all the previous traces */
340 for_each_possible_cpu(i) {
341 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200342 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200343 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200344 }
345
346 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200347 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200348}
349
350/**
351 * update_max_tr_single - only copy one trace over, and reset the rest
352 * @tr - tracer
353 * @tsk - task with the latency
354 * @cpu - the cpu of the buffer to copy.
355 */
356notrace void
357update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
358{
359 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200360 int i;
361
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200362 WARN_ON_ONCE(!irqs_disabled());
363 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200364 for_each_possible_cpu(i)
365 tracing_reset(max_tr.data[i]);
366
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200367 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200368 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200369
370 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200371 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200372}
373
374int register_tracer(struct tracer *type)
375{
376 struct tracer *t;
377 int len;
378 int ret = 0;
379
380 if (!type->name) {
381 pr_info("Tracer must have a name\n");
382 return -1;
383 }
384
385 mutex_lock(&trace_types_lock);
386 for (t = trace_types; t; t = t->next) {
387 if (strcmp(type->name, t->name) == 0) {
388 /* already found */
389 pr_info("Trace %s already registered\n",
390 type->name);
391 ret = -1;
392 goto out;
393 }
394 }
395
Steven Rostedt60a11772008-05-12 21:20:44 +0200396#ifdef CONFIG_FTRACE_STARTUP_TEST
397 if (type->selftest) {
398 struct tracer *saved_tracer = current_trace;
399 struct trace_array_cpu *data;
400 struct trace_array *tr = &global_trace;
401 int saved_ctrl = tr->ctrl;
402 int i;
403 /*
404 * Run a selftest on this tracer.
405 * Here we reset the trace buffer, and set the current
406 * tracer to be this tracer. The tracer can then run some
407 * internal tracing to verify that everything is in order.
408 * If we fail, we do not register this tracer.
409 */
410 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200411 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200412 if (!head_page(data))
413 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200414 tracing_reset(data);
415 }
416 current_trace = type;
417 tr->ctrl = 0;
418 /* the test is responsible for initializing and enabling */
419 pr_info("Testing tracer %s: ", type->name);
420 ret = type->selftest(type, tr);
421 /* the test is responsible for resetting too */
422 current_trace = saved_tracer;
423 tr->ctrl = saved_ctrl;
424 if (ret) {
425 printk(KERN_CONT "FAILED!\n");
426 goto out;
427 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200428 /* Only reset on passing, to avoid touching corrupted buffers */
429 for_each_possible_cpu(i) {
430 data = tr->data[i];
431 if (!head_page(data))
432 continue;
433 tracing_reset(data);
434 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200435 printk(KERN_CONT "PASSED\n");
436 }
437#endif
438
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200439 type->next = trace_types;
440 trace_types = type;
441 len = strlen(type->name);
442 if (len > max_tracer_type_len)
443 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200444
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200445 out:
446 mutex_unlock(&trace_types_lock);
447
448 return ret;
449}
450
451void unregister_tracer(struct tracer *type)
452{
453 struct tracer **t;
454 int len;
455
456 mutex_lock(&trace_types_lock);
457 for (t = &trace_types; *t; t = &(*t)->next) {
458 if (*t == type)
459 goto found;
460 }
461 pr_info("Trace %s not registered\n", type->name);
462 goto out;
463
464 found:
465 *t = (*t)->next;
466 if (strlen(type->name) != max_tracer_type_len)
467 goto out;
468
469 max_tracer_type_len = 0;
470 for (t = &trace_types; *t; t = &(*t)->next) {
471 len = strlen((*t)->name);
472 if (len > max_tracer_type_len)
473 max_tracer_type_len = len;
474 }
475 out:
476 mutex_unlock(&trace_types_lock);
477}
478
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200479notrace void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200480{
481 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200482 data->trace_head = data->trace_tail = head_page(data);
483 data->trace_head_idx = 0;
484 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200485}
486
487#ifdef CONFIG_FTRACE
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200488static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200489function_trace_call(unsigned long ip, unsigned long parent_ip)
490{
491 struct trace_array *tr = &global_trace;
492 struct trace_array_cpu *data;
493 unsigned long flags;
494 long disabled;
495 int cpu;
496
497 if (unlikely(!tracer_enabled))
498 return;
499
Steven Rostedt18cef372008-05-12 21:20:44 +0200500 local_irq_save(flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501 cpu = raw_smp_processor_id();
502 data = tr->data[cpu];
503 disabled = atomic_inc_return(&data->disabled);
504
505 if (likely(disabled == 1))
506 ftrace(tr, data, ip, parent_ip, flags);
507
508 atomic_dec(&data->disabled);
Steven Rostedt18cef372008-05-12 21:20:44 +0200509 local_irq_restore(flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200510}
511
512static struct ftrace_ops trace_ops __read_mostly =
513{
514 .func = function_trace_call,
515};
516#endif
517
518notrace void tracing_start_function_trace(void)
519{
520 register_ftrace_function(&trace_ops);
521}
522
523notrace void tracing_stop_function_trace(void)
524{
525 unregister_ftrace_function(&trace_ops);
526}
527
528#define SAVED_CMDLINES 128
529static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
530static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
531static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
532static int cmdline_idx;
533static DEFINE_SPINLOCK(trace_cmdline_lock);
534atomic_t trace_record_cmdline_disabled;
535
536static void trace_init_cmdlines(void)
537{
538 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
539 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
540 cmdline_idx = 0;
541}
542
543notrace void trace_stop_cmdline_recording(void);
544
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200545static notrace void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200546{
547 unsigned map;
548 unsigned idx;
549
550 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
551 return;
552
553 /*
554 * It's not the end of the world if we don't get
555 * the lock, but we also don't want to spin
556 * nor do we want to disable interrupts,
557 * so if we miss here, then better luck next time.
558 */
559 if (!spin_trylock(&trace_cmdline_lock))
560 return;
561
562 idx = map_pid_to_cmdline[tsk->pid];
563 if (idx >= SAVED_CMDLINES) {
564 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
565
566 map = map_cmdline_to_pid[idx];
567 if (map <= PID_MAX_DEFAULT)
568 map_pid_to_cmdline[map] = (unsigned)-1;
569
570 map_pid_to_cmdline[tsk->pid] = idx;
571
572 cmdline_idx = idx;
573 }
574
575 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
576
577 spin_unlock(&trace_cmdline_lock);
578}
579
580static notrace char *trace_find_cmdline(int pid)
581{
582 char *cmdline = "<...>";
583 unsigned map;
584
585 if (!pid)
586 return "<idle>";
587
588 if (pid > PID_MAX_DEFAULT)
589 goto out;
590
591 map = map_pid_to_cmdline[pid];
592 if (map >= SAVED_CMDLINES)
593 goto out;
594
595 cmdline = saved_cmdlines[map];
596
597 out:
598 return cmdline;
599}
600
601notrace void tracing_record_cmdline(struct task_struct *tsk)
602{
603 if (atomic_read(&trace_record_cmdline_disabled))
604 return;
605
606 trace_save_cmdline(tsk);
607}
608
Steven Rostedt93a588f2008-05-12 21:20:45 +0200609static inline notrace struct list_head *
610trace_next_list(struct trace_array_cpu *data, struct list_head *next)
611{
612 /*
613 * Roundrobin - but skip the head (which is not a real page):
614 */
615 next = next->next;
616 if (unlikely(next == &data->trace_pages))
617 next = next->next;
618 BUG_ON(next == &data->trace_pages);
619
620 return next;
621}
622
623static inline notrace void *
624trace_next_page(struct trace_array_cpu *data, void *addr)
625{
626 struct list_head *next;
627 struct page *page;
628
629 page = virt_to_page(addr);
630
631 next = trace_next_list(data, &page->lru);
632 page = list_entry(next, struct page, lru);
633
634 return page_address(page);
635}
636
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200637static inline notrace struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200638tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200639{
640 unsigned long idx, idx_next;
641 struct trace_entry *entry;
642
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200643 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200644 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200645 idx_next = idx + 1;
646
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200647 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
648
Steven Rostedt93a588f2008-05-12 21:20:45 +0200649 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200650
651 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200652 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653 idx_next = 0;
654 }
655
Steven Rostedt93a588f2008-05-12 21:20:45 +0200656 if (data->trace_head == data->trace_tail &&
657 idx_next == data->trace_tail_idx) {
658 /* overrun */
659 data->trace_tail_idx++;
660 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
661 data->trace_tail =
662 trace_next_page(data, data->trace_tail);
663 data->trace_tail_idx = 0;
664 }
665 }
666
667 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200668
669 return entry;
670}
671
672static inline notrace void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200673tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200674{
675 struct task_struct *tsk = current;
676 unsigned long pc;
677
678 pc = preempt_count();
679
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200680 entry->preempt_count = pc & 0xff;
681 entry->pid = tsk->pid;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200682 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200683 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
684 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
685 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
686 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
687}
688
689notrace void
690ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200691 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200692{
693 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200694 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200695
Ingo Molnardcb63082008-05-12 21:20:48 +0200696 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200697 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200698 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200699 entry->type = TRACE_FN;
700 entry->fn.ip = ip;
701 entry->fn.parent_ip = parent_ip;
Ingo Molnardcb63082008-05-12 21:20:48 +0200702 spin_unlock_irqrestore(&data->lock, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200703}
704
705notrace void
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200706trace_special(struct trace_array *tr, struct trace_array_cpu *data,
707 unsigned long arg1, unsigned long arg2, unsigned long arg3)
708{
709 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200710 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200711
Ingo Molnardcb63082008-05-12 21:20:48 +0200712 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200713 entry = tracing_get_trace_entry(tr, data);
714 tracing_generic_entry_update(entry, 0);
715 entry->type = TRACE_SPECIAL;
716 entry->special.arg1 = arg1;
717 entry->special.arg2 = arg2;
718 entry->special.arg3 = arg3;
Ingo Molnardcb63082008-05-12 21:20:48 +0200719 spin_unlock_irqrestore(&data->lock, irq_flags);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200720}
721
722notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200723tracing_sched_switch_trace(struct trace_array *tr,
724 struct trace_array_cpu *data,
725 struct task_struct *prev, struct task_struct *next,
726 unsigned long flags)
727{
728 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200729 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730
Ingo Molnardcb63082008-05-12 21:20:48 +0200731 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200732 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200733 tracing_generic_entry_update(entry, flags);
734 entry->type = TRACE_CTX;
735 entry->ctx.prev_pid = prev->pid;
736 entry->ctx.prev_prio = prev->prio;
737 entry->ctx.prev_state = prev->state;
738 entry->ctx.next_pid = next->pid;
739 entry->ctx.next_prio = next->prio;
Ingo Molnardcb63082008-05-12 21:20:48 +0200740 spin_unlock_irqrestore(&data->lock, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200741}
742
743enum trace_file_type {
744 TRACE_FILE_LAT_FMT = 1,
745};
746
747static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200748trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
749 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200750{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200751 struct page *page;
752 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200753
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200754 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200755 iter->next_idx[cpu] >= data->trace_idx ||
756 (data->trace_head == data->trace_tail &&
757 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200758 return NULL;
759
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200760 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200761 /* Initialize the iterator for this cpu trace buffer */
762 WARN_ON(!data->trace_tail);
763 page = virt_to_page(data->trace_tail);
764 iter->next_page[cpu] = &page->lru;
765 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200766 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200767
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200768 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200769 BUG_ON(&data->trace_pages == &page->lru);
770
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200771 array = page_address(page);
772
Steven Rostedt93a588f2008-05-12 21:20:45 +0200773 /* Still possible to catch up to the tail */
774 if (iter->next_idx[cpu] && array == data->trace_tail &&
775 iter->next_page_idx[cpu] == data->trace_tail_idx)
776 return NULL;
777
778 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200779 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200780}
781
782static struct notrace trace_entry *
783find_next_entry(struct trace_iterator *iter, int *ent_cpu)
784{
785 struct trace_array *tr = iter->tr;
786 struct trace_entry *ent, *next = NULL;
787 int next_cpu = -1;
788 int cpu;
789
790 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200791 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200792 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200793 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200794 /*
795 * Pick the entry with the smallest timestamp:
796 */
797 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200798 next = ent;
799 next_cpu = cpu;
800 }
801 }
802
803 if (ent_cpu)
804 *ent_cpu = next_cpu;
805
806 return next;
807}
808
Ingo Molnar8c523a92008-05-12 21:20:46 +0200809static notrace void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200810{
811 iter->idx++;
812 iter->next_idx[iter->cpu]++;
813 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200814
Steven Rostedtb3806b42008-05-12 21:20:46 +0200815 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
816 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
817
818 iter->next_page_idx[iter->cpu] = 0;
819 iter->next_page[iter->cpu] =
820 trace_next_list(data, iter->next_page[iter->cpu]);
821 }
822}
823
Ingo Molnar8c523a92008-05-12 21:20:46 +0200824static notrace void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200825{
826 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
827
828 data->trace_tail_idx++;
829 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
830 data->trace_tail = trace_next_page(data, data->trace_tail);
831 data->trace_tail_idx = 0;
832 }
833
834 /* Check if we empty it, then reset the index */
835 if (data->trace_head == data->trace_tail &&
836 data->trace_head_idx == data->trace_tail_idx)
837 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200838}
839
Ingo Molnar8c523a92008-05-12 21:20:46 +0200840static notrace void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200841{
842 struct trace_entry *next;
843 int next_cpu = -1;
844
845 next = find_next_entry(iter, &next_cpu);
846
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200847 iter->prev_ent = iter->ent;
848 iter->prev_cpu = iter->cpu;
849
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200850 iter->ent = next;
851 iter->cpu = next_cpu;
852
Steven Rostedtb3806b42008-05-12 21:20:46 +0200853 if (next)
854 trace_iterator_increment(iter);
855
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200856 return next ? iter : NULL;
857}
858
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200859static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200860{
861 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200862 void *last_ent = iter->ent;
863 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200864 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200865
866 (*pos)++;
867
868 /* can't go backwards */
869 if (iter->idx > i)
870 return NULL;
871
872 if (iter->idx < 0)
873 ent = find_next_entry_inc(iter);
874 else
875 ent = iter;
876
877 while (ent && iter->idx < i)
878 ent = find_next_entry_inc(iter);
879
880 iter->pos = *pos;
881
882 if (last_ent && !ent)
883 seq_puts(m, "\n\nvim:ft=help\n");
884
885 return ent;
886}
887
888static void *s_start(struct seq_file *m, loff_t *pos)
889{
890 struct trace_iterator *iter = m->private;
891 void *p = NULL;
892 loff_t l = 0;
893 int i;
894
895 mutex_lock(&trace_types_lock);
896
897 if (!current_trace || current_trace != iter->trace)
898 return NULL;
899
900 atomic_inc(&trace_record_cmdline_disabled);
901
902 /* let the tracer grab locks here if needed */
903 if (current_trace->start)
904 current_trace->start(iter);
905
906 if (*pos != iter->pos) {
907 iter->ent = NULL;
908 iter->cpu = 0;
909 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200910 iter->prev_ent = NULL;
911 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200912
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200913 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200914 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200915 iter->next_page[i] = NULL;
916 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200917
918 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
919 ;
920
921 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200922 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200923 p = s_next(m, p, &l);
924 }
925
926 return p;
927}
928
929static void s_stop(struct seq_file *m, void *p)
930{
931 struct trace_iterator *iter = m->private;
932
933 atomic_dec(&trace_record_cmdline_disabled);
934
935 /* let the tracer release locks here if needed */
936 if (current_trace && current_trace == iter->trace && iter->trace->stop)
937 iter->trace->stop(iter);
938
939 mutex_unlock(&trace_types_lock);
940}
941
Steven Rostedtb3806b42008-05-12 21:20:46 +0200942static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200943seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200944{
945#ifdef CONFIG_KALLSYMS
946 char str[KSYM_SYMBOL_LEN];
947
948 kallsyms_lookup(address, NULL, NULL, NULL, str);
949
Steven Rostedtb3806b42008-05-12 21:20:46 +0200950 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200951#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200952 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200953}
954
Steven Rostedtb3806b42008-05-12 21:20:46 +0200955static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200956seq_print_sym_offset(struct trace_seq *s, const char *fmt,
957 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200958{
959#ifdef CONFIG_KALLSYMS
960 char str[KSYM_SYMBOL_LEN];
961
962 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200963 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200964#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200965 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200966}
967
968#ifndef CONFIG_64BIT
969# define IP_FMT "%08lx"
970#else
971# define IP_FMT "%016lx"
972#endif
973
Steven Rostedtb3806b42008-05-12 21:20:46 +0200974static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +0200975seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200976{
Steven Rostedtb3806b42008-05-12 21:20:46 +0200977 int ret;
978
979 if (!ip)
980 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200981
982 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200983 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200984 else
Steven Rostedtb3806b42008-05-12 21:20:46 +0200985 ret = seq_print_sym_short(s, "%s", ip);
986
987 if (!ret)
988 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200989
990 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200991 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
992 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200993}
994
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200995static notrace void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200996{
997 seq_puts(m, "# _------=> CPU# \n");
998 seq_puts(m, "# / _-----=> irqs-off \n");
999 seq_puts(m, "# | / _----=> need-resched \n");
1000 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1001 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1002 seq_puts(m, "# |||| / \n");
1003 seq_puts(m, "# ||||| delay \n");
1004 seq_puts(m, "# cmd pid ||||| time | caller \n");
1005 seq_puts(m, "# \\ / ||||| \\ | / \n");
1006}
1007
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001008static notrace void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001009{
1010 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1011 seq_puts(m, "# | | | | |\n");
1012}
1013
1014
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001015static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1017{
1018 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1019 struct trace_array *tr = iter->tr;
1020 struct trace_array_cpu *data = tr->data[tr->cpu];
1021 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001022 unsigned long total = 0;
1023 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001024 int cpu;
1025 const char *name = "preemption";
1026
1027 if (type)
1028 name = type->name;
1029
1030 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001031 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001032 total += tr->data[cpu]->trace_idx;
1033 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001034 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001035 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001036 entries += tr->data[cpu]->trace_idx;
1037 }
1038 }
1039
1040 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1041 name, UTS_RELEASE);
1042 seq_puts(m, "-----------------------------------"
1043 "---------------------------------\n");
1044 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1045 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001046 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001047 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001048 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001049 tr->cpu,
1050#if defined(CONFIG_PREEMPT_NONE)
1051 "server",
1052#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1053 "desktop",
1054#elif defined(CONFIG_PREEMPT_DESKTOP)
1055 "preempt",
1056#else
1057 "unknown",
1058#endif
1059 /* These are reserved for later use */
1060 0, 0, 0, 0);
1061#ifdef CONFIG_SMP
1062 seq_printf(m, " #P:%d)\n", num_online_cpus());
1063#else
1064 seq_puts(m, ")\n");
1065#endif
1066 seq_puts(m, " -----------------\n");
1067 seq_printf(m, " | task: %.16s-%d "
1068 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1069 data->comm, data->pid, data->uid, data->nice,
1070 data->policy, data->rt_priority);
1071 seq_puts(m, " -----------------\n");
1072
1073 if (data->critical_start) {
1074 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001075 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1076 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001077 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001078 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1079 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001080 seq_puts(m, "\n");
1081 }
1082
1083 seq_puts(m, "\n");
1084}
1085
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001086static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001087lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001088{
1089 int hardirq, softirq;
1090 char *comm;
1091
1092 comm = trace_find_cmdline(entry->pid);
1093
Steven Rostedt214023c2008-05-12 21:20:46 +02001094 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1095 trace_seq_printf(s, "%d", cpu);
1096 trace_seq_printf(s, "%c%c",
1097 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1098 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099
1100 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1101 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1102 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001103 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001104 else {
1105 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001106 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001107 else {
1108 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001109 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001110 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001111 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001112 }
1113 }
1114
1115 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001116 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001117 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001118 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001119}
1120
1121unsigned long preempt_mark_thresh = 100;
1122
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001123static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001124lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001125 unsigned long rel_usecs)
1126{
Steven Rostedt214023c2008-05-12 21:20:46 +02001127 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001128 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001129 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001130 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001131 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001132 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001133 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001134}
1135
1136static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1137
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001138static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +02001139print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001140{
Steven Rostedt214023c2008-05-12 21:20:46 +02001141 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001142 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1143 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1144 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1145 struct trace_entry *entry = iter->ent;
1146 unsigned long abs_usecs;
1147 unsigned long rel_usecs;
1148 char *comm;
1149 int S;
1150
1151 if (!next_entry)
1152 next_entry = entry;
1153 rel_usecs = ns2usecs(next_entry->t - entry->t);
1154 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1155
1156 if (verbose) {
1157 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001158 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1159 " %ld.%03ldms (+%ld.%03ldms): ",
1160 comm,
1161 entry->pid, cpu, entry->flags,
1162 entry->preempt_count, trace_idx,
1163 ns2usecs(entry->t),
1164 abs_usecs/1000,
1165 abs_usecs % 1000, rel_usecs/1000,
1166 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167 } else {
Steven Rostedt214023c2008-05-12 21:20:46 +02001168 lat_print_generic(s, entry, cpu);
1169 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001170 }
1171 switch (entry->type) {
1172 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001173 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1174 trace_seq_puts(s, " (");
1175 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1176 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177 break;
1178 case TRACE_CTX:
1179 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1180 state_to_char[entry->ctx.prev_state] : 'X';
1181 comm = trace_find_cmdline(entry->ctx.next_pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001182 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1183 entry->ctx.prev_pid,
1184 entry->ctx.prev_prio,
1185 S,
1186 entry->ctx.next_pid,
1187 entry->ctx.next_prio,
1188 comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001189 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001190 case TRACE_SPECIAL:
1191 trace_seq_printf(s, " %lx %lx %lx\n",
1192 entry->special.arg1,
1193 entry->special.arg2,
1194 entry->special.arg3);
1195 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001196 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001197 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001198 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001199 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001200}
1201
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001202static notrace int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001203{
Steven Rostedt214023c2008-05-12 21:20:46 +02001204 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001205 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001206 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001207 unsigned long usec_rem;
1208 unsigned long long t;
1209 unsigned long secs;
1210 char *comm;
1211 int S;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001212 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001213
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001214 entry = iter->ent;
1215
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001216 comm = trace_find_cmdline(iter->ent->pid);
1217
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001218 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001219 usec_rem = do_div(t, 1000000ULL);
1220 secs = (unsigned long)t;
1221
Steven Rostedtb3806b42008-05-12 21:20:46 +02001222 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1223 if (!ret)
1224 return 0;
1225 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1226 if (!ret)
1227 return 0;
1228 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1229 if (!ret)
1230 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001231
1232 switch (entry->type) {
1233 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001234 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1235 if (!ret)
1236 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001237 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1238 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001239 ret = trace_seq_printf(s, " <-");
1240 if (!ret)
1241 return 0;
1242 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1243 sym_flags);
1244 if (!ret)
1245 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001246 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001247 ret = trace_seq_printf(s, "\n");
1248 if (!ret)
1249 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001250 break;
1251 case TRACE_CTX:
1252 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1253 state_to_char[entry->ctx.prev_state] : 'X';
Steven Rostedtb3806b42008-05-12 21:20:46 +02001254 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1255 entry->ctx.prev_pid,
1256 entry->ctx.prev_prio,
1257 S,
1258 entry->ctx.next_pid,
1259 entry->ctx.next_prio);
1260 if (!ret)
1261 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001262 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001263 case TRACE_SPECIAL:
1264 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1265 entry->special.arg1,
1266 entry->special.arg2,
1267 entry->special.arg3);
1268 if (!ret)
1269 return 0;
1270 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001271 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001272 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001273}
1274
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001275static notrace int print_raw_fmt(struct trace_iterator *iter)
1276{
1277 struct trace_seq *s = &iter->seq;
1278 struct trace_entry *entry;
1279 int ret;
1280 int S;
1281
1282 entry = iter->ent;
1283
1284 ret = trace_seq_printf(s, "%d %d %llu ",
1285 entry->pid, iter->cpu, entry->t);
1286 if (!ret)
1287 return 0;
1288
1289 switch (entry->type) {
1290 case TRACE_FN:
1291 ret = trace_seq_printf(s, "%x %x\n",
1292 entry->fn.ip, entry->fn.parent_ip);
1293 if (!ret)
1294 return 0;
1295 break;
1296 case TRACE_CTX:
1297 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1298 state_to_char[entry->ctx.prev_state] : 'X';
1299 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1300 entry->ctx.prev_pid,
1301 entry->ctx.prev_prio,
1302 S,
1303 entry->ctx.next_pid,
1304 entry->ctx.next_prio);
1305 if (!ret)
1306 return 0;
1307 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001308 case TRACE_SPECIAL:
1309 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1310 entry->special.arg1,
1311 entry->special.arg2,
1312 entry->special.arg3);
1313 if (!ret)
1314 return 0;
1315 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001316 }
1317 return 1;
1318}
1319
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001320#define SEQ_PUT_FIELD_RET(s, x) \
1321do { \
1322 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1323 return 0; \
1324} while (0)
1325
1326static notrace int print_bin_fmt(struct trace_iterator *iter)
1327{
1328 struct trace_seq *s = &iter->seq;
1329 struct trace_entry *entry;
1330
1331 entry = iter->ent;
1332
1333 SEQ_PUT_FIELD_RET(s, entry->pid);
1334 SEQ_PUT_FIELD_RET(s, entry->cpu);
1335 SEQ_PUT_FIELD_RET(s, entry->t);
1336
1337 switch (entry->type) {
1338 case TRACE_FN:
1339 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1340 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1341 break;
1342 case TRACE_CTX:
1343 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1344 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1345 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1346 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1347 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1348 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001349 case TRACE_SPECIAL:
1350 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1351 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1352 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1353 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001354 }
1355 return 1;
1356}
1357
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358static int trace_empty(struct trace_iterator *iter)
1359{
1360 struct trace_array_cpu *data;
1361 int cpu;
1362
1363 for_each_possible_cpu(cpu) {
1364 data = iter->tr->data[cpu];
1365
Steven Rostedtb3806b42008-05-12 21:20:46 +02001366 if (head_page(data) && data->trace_idx &&
1367 (data->trace_tail != data->trace_head ||
1368 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001369 return 0;
1370 }
1371 return 1;
1372}
1373
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001374static int print_trace_line(struct trace_iterator *iter)
1375{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001376 if (trace_flags & TRACE_ITER_BIN)
1377 return print_bin_fmt(iter);
1378
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001379 if (trace_flags & TRACE_ITER_RAW)
1380 return print_raw_fmt(iter);
1381
1382 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1383 return print_lat_fmt(iter, iter->idx, iter->cpu);
1384
1385 return print_trace_fmt(iter);
1386}
1387
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001388static int s_show(struct seq_file *m, void *v)
1389{
1390 struct trace_iterator *iter = v;
1391
1392 if (iter->ent == NULL) {
1393 if (iter->tr) {
1394 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1395 seq_puts(m, "#\n");
1396 }
1397 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1398 /* print nothing if the buffers are empty */
1399 if (trace_empty(iter))
1400 return 0;
1401 print_trace_header(m, iter);
1402 if (!(trace_flags & TRACE_ITER_VERBOSE))
1403 print_lat_help_header(m);
1404 } else {
1405 if (!(trace_flags & TRACE_ITER_VERBOSE))
1406 print_func_help_header(m);
1407 }
1408 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001409 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001410 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001411 }
1412
1413 return 0;
1414}
1415
1416static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001417 .start = s_start,
1418 .next = s_next,
1419 .stop = s_stop,
1420 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001421};
1422
1423static struct trace_iterator notrace *
1424__tracing_open(struct inode *inode, struct file *file, int *ret)
1425{
1426 struct trace_iterator *iter;
1427
Steven Rostedt60a11772008-05-12 21:20:44 +02001428 if (tracing_disabled) {
1429 *ret = -ENODEV;
1430 return NULL;
1431 }
1432
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001433 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1434 if (!iter) {
1435 *ret = -ENOMEM;
1436 goto out;
1437 }
1438
1439 mutex_lock(&trace_types_lock);
1440 if (current_trace && current_trace->print_max)
1441 iter->tr = &max_tr;
1442 else
1443 iter->tr = inode->i_private;
1444 iter->trace = current_trace;
1445 iter->pos = -1;
1446
1447 /* TODO stop tracer */
1448 *ret = seq_open(file, &tracer_seq_ops);
1449 if (!*ret) {
1450 struct seq_file *m = file->private_data;
1451 m->private = iter;
1452
1453 /* stop the trace while dumping */
1454 if (iter->tr->ctrl)
1455 tracer_enabled = 0;
1456
1457 if (iter->trace && iter->trace->open)
1458 iter->trace->open(iter);
1459 } else {
1460 kfree(iter);
1461 iter = NULL;
1462 }
1463 mutex_unlock(&trace_types_lock);
1464
1465 out:
1466 return iter;
1467}
1468
1469int tracing_open_generic(struct inode *inode, struct file *filp)
1470{
Steven Rostedt60a11772008-05-12 21:20:44 +02001471 if (tracing_disabled)
1472 return -ENODEV;
1473
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001474 filp->private_data = inode->i_private;
1475 return 0;
1476}
1477
1478int tracing_release(struct inode *inode, struct file *file)
1479{
1480 struct seq_file *m = (struct seq_file *)file->private_data;
1481 struct trace_iterator *iter = m->private;
1482
1483 mutex_lock(&trace_types_lock);
1484 if (iter->trace && iter->trace->close)
1485 iter->trace->close(iter);
1486
1487 /* reenable tracing if it was previously enabled */
1488 if (iter->tr->ctrl)
1489 tracer_enabled = 1;
1490 mutex_unlock(&trace_types_lock);
1491
1492 seq_release(inode, file);
1493 kfree(iter);
1494 return 0;
1495}
1496
1497static int tracing_open(struct inode *inode, struct file *file)
1498{
1499 int ret;
1500
1501 __tracing_open(inode, file, &ret);
1502
1503 return ret;
1504}
1505
1506static int tracing_lt_open(struct inode *inode, struct file *file)
1507{
1508 struct trace_iterator *iter;
1509 int ret;
1510
1511 iter = __tracing_open(inode, file, &ret);
1512
1513 if (!ret)
1514 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1515
1516 return ret;
1517}
1518
1519
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001520static notrace void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521t_next(struct seq_file *m, void *v, loff_t *pos)
1522{
1523 struct tracer *t = m->private;
1524
1525 (*pos)++;
1526
1527 if (t)
1528 t = t->next;
1529
1530 m->private = t;
1531
1532 return t;
1533}
1534
1535static void *t_start(struct seq_file *m, loff_t *pos)
1536{
1537 struct tracer *t = m->private;
1538 loff_t l = 0;
1539
1540 mutex_lock(&trace_types_lock);
1541 for (; t && l < *pos; t = t_next(m, t, &l))
1542 ;
1543
1544 return t;
1545}
1546
1547static void t_stop(struct seq_file *m, void *p)
1548{
1549 mutex_unlock(&trace_types_lock);
1550}
1551
1552static int t_show(struct seq_file *m, void *v)
1553{
1554 struct tracer *t = v;
1555
1556 if (!t)
1557 return 0;
1558
1559 seq_printf(m, "%s", t->name);
1560 if (t->next)
1561 seq_putc(m, ' ');
1562 else
1563 seq_putc(m, '\n');
1564
1565 return 0;
1566}
1567
1568static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001569 .start = t_start,
1570 .next = t_next,
1571 .stop = t_stop,
1572 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001573};
1574
1575static int show_traces_open(struct inode *inode, struct file *file)
1576{
1577 int ret;
1578
Steven Rostedt60a11772008-05-12 21:20:44 +02001579 if (tracing_disabled)
1580 return -ENODEV;
1581
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001582 ret = seq_open(file, &show_traces_seq_ops);
1583 if (!ret) {
1584 struct seq_file *m = file->private_data;
1585 m->private = trace_types;
1586 }
1587
1588 return ret;
1589}
1590
1591static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001592 .open = tracing_open,
1593 .read = seq_read,
1594 .llseek = seq_lseek,
1595 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001596};
1597
1598static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001599 .open = tracing_lt_open,
1600 .read = seq_read,
1601 .llseek = seq_lseek,
1602 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001603};
1604
1605static struct file_operations show_traces_fops = {
1606 .open = show_traces_open,
1607 .read = seq_read,
1608 .release = seq_release,
1609};
1610
1611static ssize_t
1612tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1613 size_t cnt, loff_t *ppos)
1614{
1615 char *buf;
1616 int r = 0;
1617 int len = 0;
1618 int i;
1619
1620 /* calulate max size */
1621 for (i = 0; trace_options[i]; i++) {
1622 len += strlen(trace_options[i]);
1623 len += 3; /* "no" and space */
1624 }
1625
1626 /* +2 for \n and \0 */
1627 buf = kmalloc(len + 2, GFP_KERNEL);
1628 if (!buf)
1629 return -ENOMEM;
1630
1631 for (i = 0; trace_options[i]; i++) {
1632 if (trace_flags & (1 << i))
1633 r += sprintf(buf + r, "%s ", trace_options[i]);
1634 else
1635 r += sprintf(buf + r, "no%s ", trace_options[i]);
1636 }
1637
1638 r += sprintf(buf + r, "\n");
1639 WARN_ON(r >= len + 2);
1640
1641 r = simple_read_from_buffer(ubuf, cnt, ppos,
1642 buf, r);
1643
1644 kfree(buf);
1645
1646 return r;
1647}
1648
1649static ssize_t
1650tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1651 size_t cnt, loff_t *ppos)
1652{
1653 char buf[64];
1654 char *cmp = buf;
1655 int neg = 0;
1656 int i;
1657
1658 if (cnt > 63)
1659 cnt = 63;
1660
1661 if (copy_from_user(&buf, ubuf, cnt))
1662 return -EFAULT;
1663
1664 buf[cnt] = 0;
1665
1666 if (strncmp(buf, "no", 2) == 0) {
1667 neg = 1;
1668 cmp += 2;
1669 }
1670
1671 for (i = 0; trace_options[i]; i++) {
1672 int len = strlen(trace_options[i]);
1673
1674 if (strncmp(cmp, trace_options[i], len) == 0) {
1675 if (neg)
1676 trace_flags &= ~(1 << i);
1677 else
1678 trace_flags |= (1 << i);
1679 break;
1680 }
1681 }
1682
1683 filp->f_pos += cnt;
1684
1685 return cnt;
1686}
1687
1688static struct file_operations tracing_iter_fops = {
1689 .open = tracing_open_generic,
1690 .read = tracing_iter_ctrl_read,
1691 .write = tracing_iter_ctrl_write,
1692};
1693
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001694static const char readme_msg[] =
1695 "tracing mini-HOWTO:\n\n"
1696 "# mkdir /debug\n"
1697 "# mount -t debugfs nodev /debug\n\n"
1698 "# cat /debug/tracing/available_tracers\n"
1699 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1700 "# cat /debug/tracing/current_tracer\n"
1701 "none\n"
1702 "# echo sched_switch > /debug/tracing/current_tracer\n"
1703 "# cat /debug/tracing/current_tracer\n"
1704 "sched_switch\n"
1705 "# cat /debug/tracing/iter_ctrl\n"
1706 "noprint-parent nosym-offset nosym-addr noverbose\n"
1707 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1708 "# echo 1 > /debug/tracing/tracing_enabled\n"
1709 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1710 "echo 0 > /debug/tracing/tracing_enabled\n"
1711;
1712
1713static ssize_t
1714tracing_readme_read(struct file *filp, char __user *ubuf,
1715 size_t cnt, loff_t *ppos)
1716{
1717 return simple_read_from_buffer(ubuf, cnt, ppos,
1718 readme_msg, strlen(readme_msg));
1719}
1720
1721static struct file_operations tracing_readme_fops = {
1722 .open = tracing_open_generic,
1723 .read = tracing_readme_read,
1724};
1725
1726
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001727static ssize_t
1728tracing_ctrl_read(struct file *filp, char __user *ubuf,
1729 size_t cnt, loff_t *ppos)
1730{
1731 struct trace_array *tr = filp->private_data;
1732 char buf[64];
1733 int r;
1734
1735 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001736 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001737}
1738
1739static ssize_t
1740tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1741 size_t cnt, loff_t *ppos)
1742{
1743 struct trace_array *tr = filp->private_data;
1744 long val;
1745 char buf[64];
1746
1747 if (cnt > 63)
1748 cnt = 63;
1749
1750 if (copy_from_user(&buf, ubuf, cnt))
1751 return -EFAULT;
1752
1753 buf[cnt] = 0;
1754
1755 val = simple_strtoul(buf, NULL, 10);
1756
1757 val = !!val;
1758
1759 mutex_lock(&trace_types_lock);
1760 if (tr->ctrl ^ val) {
1761 if (val)
1762 tracer_enabled = 1;
1763 else
1764 tracer_enabled = 0;
1765
1766 tr->ctrl = val;
1767
1768 if (current_trace && current_trace->ctrl_update)
1769 current_trace->ctrl_update(tr);
1770 }
1771 mutex_unlock(&trace_types_lock);
1772
1773 filp->f_pos += cnt;
1774
1775 return cnt;
1776}
1777
1778static ssize_t
1779tracing_set_trace_read(struct file *filp, char __user *ubuf,
1780 size_t cnt, loff_t *ppos)
1781{
1782 char buf[max_tracer_type_len+2];
1783 int r;
1784
1785 mutex_lock(&trace_types_lock);
1786 if (current_trace)
1787 r = sprintf(buf, "%s\n", current_trace->name);
1788 else
1789 r = sprintf(buf, "\n");
1790 mutex_unlock(&trace_types_lock);
1791
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001792 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001793}
1794
1795static ssize_t
1796tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1797 size_t cnt, loff_t *ppos)
1798{
1799 struct trace_array *tr = &global_trace;
1800 struct tracer *t;
1801 char buf[max_tracer_type_len+1];
1802 int i;
1803
1804 if (cnt > max_tracer_type_len)
1805 cnt = max_tracer_type_len;
1806
1807 if (copy_from_user(&buf, ubuf, cnt))
1808 return -EFAULT;
1809
1810 buf[cnt] = 0;
1811
1812 /* strip ending whitespace. */
1813 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1814 buf[i] = 0;
1815
1816 mutex_lock(&trace_types_lock);
1817 for (t = trace_types; t; t = t->next) {
1818 if (strcmp(t->name, buf) == 0)
1819 break;
1820 }
1821 if (!t || t == current_trace)
1822 goto out;
1823
1824 if (current_trace && current_trace->reset)
1825 current_trace->reset(tr);
1826
1827 current_trace = t;
1828 if (t->init)
1829 t->init(tr);
1830
1831 out:
1832 mutex_unlock(&trace_types_lock);
1833
1834 filp->f_pos += cnt;
1835
1836 return cnt;
1837}
1838
1839static ssize_t
1840tracing_max_lat_read(struct file *filp, char __user *ubuf,
1841 size_t cnt, loff_t *ppos)
1842{
1843 unsigned long *ptr = filp->private_data;
1844 char buf[64];
1845 int r;
1846
1847 r = snprintf(buf, 64, "%ld\n",
1848 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1849 if (r > 64)
1850 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001851 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001852}
1853
1854static ssize_t
1855tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1856 size_t cnt, loff_t *ppos)
1857{
1858 long *ptr = filp->private_data;
1859 long val;
1860 char buf[64];
1861
1862 if (cnt > 63)
1863 cnt = 63;
1864
1865 if (copy_from_user(&buf, ubuf, cnt))
1866 return -EFAULT;
1867
1868 buf[cnt] = 0;
1869
1870 val = simple_strtoul(buf, NULL, 10);
1871
1872 *ptr = val * 1000;
1873
1874 return cnt;
1875}
1876
Steven Rostedtb3806b42008-05-12 21:20:46 +02001877static atomic_t tracing_reader;
1878
1879static int tracing_open_pipe(struct inode *inode, struct file *filp)
1880{
1881 struct trace_iterator *iter;
1882
1883 if (tracing_disabled)
1884 return -ENODEV;
1885
1886 /* We only allow for reader of the pipe */
1887 if (atomic_inc_return(&tracing_reader) != 1) {
1888 atomic_dec(&tracing_reader);
1889 return -EBUSY;
1890 }
1891
1892 /* create a buffer to store the information to pass to userspace */
1893 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1894 if (!iter)
1895 return -ENOMEM;
1896
1897 iter->tr = &global_trace;
1898
1899 filp->private_data = iter;
1900
1901 return 0;
1902}
1903
1904static int tracing_release_pipe(struct inode *inode, struct file *file)
1905{
1906 struct trace_iterator *iter = file->private_data;
1907
1908 kfree(iter);
1909 atomic_dec(&tracing_reader);
1910
1911 return 0;
1912}
1913
1914/*
1915 * Consumer reader.
1916 */
1917static ssize_t
1918tracing_read_pipe(struct file *filp, char __user *ubuf,
1919 size_t cnt, loff_t *ppos)
1920{
1921 struct trace_iterator *iter = filp->private_data;
1922 struct trace_array_cpu *data;
1923 static cpumask_t mask;
1924 struct trace_entry *entry;
1925 static int start;
1926 unsigned long flags;
1927 int read = 0;
1928 int cpu;
1929 int len;
1930 int ret;
1931
1932 /* return any leftover data */
1933 if (iter->seq.len > start) {
1934 len = iter->seq.len - start;
1935 if (cnt > len)
1936 cnt = len;
1937 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1938 if (ret)
1939 cnt = -EFAULT;
1940
1941 start += len;
1942
1943 return cnt;
1944 }
1945
1946 trace_seq_reset(&iter->seq);
1947 start = 0;
1948
1949 while (trace_empty(iter)) {
1950 /*
1951 * This is a make-shift waitqueue. The reason we don't use
1952 * an actual wait queue is because:
1953 * 1) we only ever have one waiter
1954 * 2) the tracing, traces all functions, we don't want
1955 * the overhead of calling wake_up and friends
1956 * (and tracing them too)
1957 * Anyway, this is really very primitive wakeup.
1958 */
1959 set_current_state(TASK_INTERRUPTIBLE);
1960 iter->tr->waiter = current;
1961
1962 /* sleep for one second, and try again. */
1963 schedule_timeout(HZ);
1964
1965 iter->tr->waiter = NULL;
1966
1967 if (signal_pending(current))
1968 return -EINTR;
1969
1970 /*
1971 * We block until we read something and tracing is disabled.
1972 * We still block if tracing is disabled, but we have never
1973 * read anything. This allows a user to cat this file, and
1974 * then enable tracing. But after we have read something,
1975 * we give an EOF when tracing is again disabled.
1976 *
1977 * iter->pos will be 0 if we haven't read anything.
1978 */
1979 if (!tracer_enabled && iter->pos)
1980 break;
1981
1982 continue;
1983 }
1984
1985 /* stop when tracing is finished */
1986 if (trace_empty(iter))
1987 return 0;
1988
1989 if (cnt >= PAGE_SIZE)
1990 cnt = PAGE_SIZE - 1;
1991
1992 memset(iter, 0, sizeof(*iter));
1993 iter->tr = &global_trace;
1994 iter->pos = -1;
1995
1996 /*
1997 * We need to stop all tracing on all CPUS to read the
1998 * the next buffer. This is a bit expensive, but is
1999 * not done often. We fill all what we can read,
2000 * and then release the locks again.
2001 */
2002
2003 cpus_clear(mask);
2004 local_irq_save(flags);
2005 for_each_possible_cpu(cpu) {
2006 data = iter->tr->data[cpu];
2007
2008 if (!head_page(data) || !data->trace_idx)
2009 continue;
2010
2011 atomic_inc(&data->disabled);
2012 spin_lock(&data->lock);
2013 cpu_set(cpu, mask);
2014 }
2015
Ingo Molnar8c523a92008-05-12 21:20:46 +02002016 while ((entry = find_next_entry_inc(iter)) != NULL) {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002017 ret = print_trace_line(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002018 if (!ret)
2019 break;
2020
2021 trace_consume(iter);
2022
2023 if (iter->seq.len >= cnt)
2024 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002025 }
2026
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002027 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002028 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002029 spin_unlock(&data->lock);
2030 atomic_dec(&data->disabled);
2031 }
2032 local_irq_restore(flags);
2033
2034 /* Now copy what we have to the user */
2035 read = iter->seq.len;
2036 if (read > cnt)
2037 read = cnt;
2038
2039 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2040
2041 if (read < iter->seq.len)
2042 start = read;
2043 else
2044 trace_seq_reset(&iter->seq);
2045
2046 if (ret)
2047 read = -EFAULT;
2048
2049 return read;
2050}
2051
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002052static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002053 .open = tracing_open_generic,
2054 .read = tracing_max_lat_read,
2055 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002056};
2057
2058static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002059 .open = tracing_open_generic,
2060 .read = tracing_ctrl_read,
2061 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062};
2063
2064static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002065 .open = tracing_open_generic,
2066 .read = tracing_set_trace_read,
2067 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002068};
2069
Steven Rostedtb3806b42008-05-12 21:20:46 +02002070static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002071 .open = tracing_open_pipe,
2072 .read = tracing_read_pipe,
2073 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002074};
2075
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002076#ifdef CONFIG_DYNAMIC_FTRACE
2077
2078static ssize_t
2079tracing_read_long(struct file *filp, char __user *ubuf,
2080 size_t cnt, loff_t *ppos)
2081{
2082 unsigned long *p = filp->private_data;
2083 char buf[64];
2084 int r;
2085
2086 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002087
2088 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002089}
2090
2091static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002092 .open = tracing_open_generic,
2093 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002094};
2095#endif
2096
2097static struct dentry *d_tracer;
2098
2099struct dentry *tracing_init_dentry(void)
2100{
2101 static int once;
2102
2103 if (d_tracer)
2104 return d_tracer;
2105
2106 d_tracer = debugfs_create_dir("tracing", NULL);
2107
2108 if (!d_tracer && !once) {
2109 once = 1;
2110 pr_warning("Could not create debugfs directory 'tracing'\n");
2111 return NULL;
2112 }
2113
2114 return d_tracer;
2115}
2116
Steven Rostedt60a11772008-05-12 21:20:44 +02002117#ifdef CONFIG_FTRACE_SELFTEST
2118/* Let selftest have access to static functions in this file */
2119#include "trace_selftest.c"
2120#endif
2121
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002122static __init void tracer_init_debugfs(void)
2123{
2124 struct dentry *d_tracer;
2125 struct dentry *entry;
2126
2127 d_tracer = tracing_init_dentry();
2128
2129 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2130 &global_trace, &tracing_ctrl_fops);
2131 if (!entry)
2132 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2133
2134 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2135 NULL, &tracing_iter_fops);
2136 if (!entry)
2137 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2138
2139 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2140 &global_trace, &tracing_lt_fops);
2141 if (!entry)
2142 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2143
2144 entry = debugfs_create_file("trace", 0444, d_tracer,
2145 &global_trace, &tracing_fops);
2146 if (!entry)
2147 pr_warning("Could not create debugfs 'trace' entry\n");
2148
2149 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2150 &global_trace, &show_traces_fops);
2151 if (!entry)
2152 pr_warning("Could not create debugfs 'trace' entry\n");
2153
2154 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2155 &global_trace, &set_tracer_fops);
2156 if (!entry)
2157 pr_warning("Could not create debugfs 'trace' entry\n");
2158
2159 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2160 &tracing_max_latency,
2161 &tracing_max_lat_fops);
2162 if (!entry)
2163 pr_warning("Could not create debugfs "
2164 "'tracing_max_latency' entry\n");
2165
2166 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2167 &tracing_thresh, &tracing_max_lat_fops);
2168 if (!entry)
2169 pr_warning("Could not create debugfs "
2170 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002171 entry = debugfs_create_file("README", 0644, d_tracer,
2172 NULL, &tracing_readme_fops);
2173 if (!entry)
2174 pr_warning("Could not create debugfs 'README' entry\n");
2175
Steven Rostedtb3806b42008-05-12 21:20:46 +02002176 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2177 NULL, &tracing_pipe_fops);
2178 if (!entry)
2179 pr_warning("Could not create debugfs "
2180 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002181
2182#ifdef CONFIG_DYNAMIC_FTRACE
2183 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2184 &ftrace_update_tot_cnt,
2185 &tracing_read_long_fops);
2186 if (!entry)
2187 pr_warning("Could not create debugfs "
2188 "'dyn_ftrace_total_info' entry\n");
2189#endif
2190}
2191
2192/* dummy trace to disable tracing */
2193static struct tracer no_tracer __read_mostly =
2194{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002195 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002196};
2197
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002198static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002199{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002200 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002201 struct page *page, *tmp;
2202 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002203 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002204 int i;
2205
2206 /* first allocate a page for each CPU */
2207 for_each_possible_cpu(i) {
2208 array = (void *)__get_free_page(GFP_KERNEL);
2209 if (array == NULL) {
2210 printk(KERN_ERR "tracer: failed to allocate page"
2211 "for trace buffer!\n");
2212 goto free_pages;
2213 }
2214
2215 page = virt_to_page(array);
2216 list_add(&page->lru, &pages);
2217
2218/* Only allocate if we are actually using the max trace */
2219#ifdef CONFIG_TRACER_MAX_TRACE
2220 array = (void *)__get_free_page(GFP_KERNEL);
2221 if (array == NULL) {
2222 printk(KERN_ERR "tracer: failed to allocate page"
2223 "for trace buffer!\n");
2224 goto free_pages;
2225 }
2226 page = virt_to_page(array);
2227 list_add(&page->lru, &pages);
2228#endif
2229 }
2230
2231 /* Now that we successfully allocate a page per CPU, add them */
2232 for_each_possible_cpu(i) {
2233 data = global_trace.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002234 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002235 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002236 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002237 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002238 list_add_tail(&page->lru, &data->trace_pages);
2239 ClearPageLRU(page);
2240
2241#ifdef CONFIG_TRACER_MAX_TRACE
2242 data = max_tr.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002243 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002244 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002245 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002246 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002247 list_add_tail(&page->lru, &data->trace_pages);
2248 SetPageLRU(page);
2249#endif
2250 }
2251 global_trace.entries += ENTRIES_PER_PAGE;
2252
2253 return 0;
2254
2255 free_pages:
2256 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002257 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002258 __free_page(page);
2259 }
2260 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002261}
2262
2263__init static int tracer_alloc_buffers(void)
2264{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002265 struct trace_array_cpu *data;
2266 void *array;
2267 struct page *page;
2268 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002269 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270 int i;
2271
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002272 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002274 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002275 max_tr.data[i] = &per_cpu(max_data, i);
2276
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002277 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002278 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002279 printk(KERN_ERR "tracer: failed to allocate page"
2280 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002281 goto free_buffers;
2282 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002283
2284 /* set the array to the list */
2285 INIT_LIST_HEAD(&data->trace_pages);
2286 page = virt_to_page(array);
2287 list_add(&page->lru, &data->trace_pages);
2288 /* use the LRU flag to differentiate the two buffers */
2289 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002290
2291/* Only allocate if we are actually using the max trace */
2292#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002293 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002294 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002295 printk(KERN_ERR "tracer: failed to allocate page"
2296 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002297 goto free_buffers;
2298 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002299
2300 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2301 page = virt_to_page(array);
2302 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2303 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002304#endif
2305 }
2306
2307 /*
2308 * Since we allocate by orders of pages, we may be able to
2309 * round up a bit.
2310 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002311 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002312 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002313
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002314 while (global_trace.entries < trace_nr_entries) {
2315 if (trace_alloc_page())
2316 break;
2317 pages++;
2318 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002319 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002320
2321 pr_info("tracer: %d pages allocated for %ld",
2322 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002323 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2324 pr_info(" actual entries %ld\n", global_trace.entries);
2325
2326 tracer_init_debugfs();
2327
2328 trace_init_cmdlines();
2329
2330 register_tracer(&no_tracer);
2331 current_trace = &no_tracer;
2332
Steven Rostedt60a11772008-05-12 21:20:44 +02002333 /* All seems OK, enable tracing */
2334 tracing_disabled = 0;
2335
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002336 return 0;
2337
2338 free_buffers:
2339 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002340 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002341 struct trace_array_cpu *data = global_trace.data[i];
2342
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002343 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002344 list_for_each_entry_safe(page, tmp,
2345 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002346 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002347 __free_page(page);
2348 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002349 }
2350
2351#ifdef CONFIG_TRACER_MAX_TRACE
2352 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002353 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002354 list_for_each_entry_safe(page, tmp,
2355 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002356 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002357 __free_page(page);
2358 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002359 }
2360#endif
2361 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002362 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363}
Steven Rostedt60a11772008-05-12 21:20:44 +02002364fs_initcall(tracer_alloc_buffers);