blob: c56fc5e6013324a9f485300dcfe52be0a4f2974e [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 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200774 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200775}
776
777static struct notrace trace_entry *
778find_next_entry(struct trace_iterator *iter, int *ent_cpu)
779{
780 struct trace_array *tr = iter->tr;
781 struct trace_entry *ent, *next = NULL;
782 int next_cpu = -1;
783 int cpu;
784
785 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200786 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200787 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200788 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200789 /*
790 * Pick the entry with the smallest timestamp:
791 */
792 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793 next = ent;
794 next_cpu = cpu;
795 }
796 }
797
798 if (ent_cpu)
799 *ent_cpu = next_cpu;
800
801 return next;
802}
803
Ingo Molnar8c523a92008-05-12 21:20:46 +0200804static notrace void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200805{
806 iter->idx++;
807 iter->next_idx[iter->cpu]++;
808 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200809
Steven Rostedtb3806b42008-05-12 21:20:46 +0200810 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
811 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
812
813 iter->next_page_idx[iter->cpu] = 0;
814 iter->next_page[iter->cpu] =
815 trace_next_list(data, iter->next_page[iter->cpu]);
816 }
817}
818
Ingo Molnar8c523a92008-05-12 21:20:46 +0200819static notrace void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200820{
821 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
822
823 data->trace_tail_idx++;
824 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
825 data->trace_tail = trace_next_page(data, data->trace_tail);
826 data->trace_tail_idx = 0;
827 }
828
829 /* Check if we empty it, then reset the index */
830 if (data->trace_head == data->trace_tail &&
831 data->trace_head_idx == data->trace_tail_idx)
832 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200833}
834
Ingo Molnar8c523a92008-05-12 21:20:46 +0200835static notrace void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200836{
837 struct trace_entry *next;
838 int next_cpu = -1;
839
840 next = find_next_entry(iter, &next_cpu);
841
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200842 iter->prev_ent = iter->ent;
843 iter->prev_cpu = iter->cpu;
844
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200845 iter->ent = next;
846 iter->cpu = next_cpu;
847
Steven Rostedtb3806b42008-05-12 21:20:46 +0200848 if (next)
849 trace_iterator_increment(iter);
850
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200851 return next ? iter : NULL;
852}
853
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200854static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200855{
856 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200857 void *last_ent = iter->ent;
858 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200859 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200860
861 (*pos)++;
862
863 /* can't go backwards */
864 if (iter->idx > i)
865 return NULL;
866
867 if (iter->idx < 0)
868 ent = find_next_entry_inc(iter);
869 else
870 ent = iter;
871
872 while (ent && iter->idx < i)
873 ent = find_next_entry_inc(iter);
874
875 iter->pos = *pos;
876
877 if (last_ent && !ent)
878 seq_puts(m, "\n\nvim:ft=help\n");
879
880 return ent;
881}
882
883static void *s_start(struct seq_file *m, loff_t *pos)
884{
885 struct trace_iterator *iter = m->private;
886 void *p = NULL;
887 loff_t l = 0;
888 int i;
889
890 mutex_lock(&trace_types_lock);
891
892 if (!current_trace || current_trace != iter->trace)
893 return NULL;
894
895 atomic_inc(&trace_record_cmdline_disabled);
896
897 /* let the tracer grab locks here if needed */
898 if (current_trace->start)
899 current_trace->start(iter);
900
901 if (*pos != iter->pos) {
902 iter->ent = NULL;
903 iter->cpu = 0;
904 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200905 iter->prev_ent = NULL;
906 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200907
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200908 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200909 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200910 iter->next_page[i] = NULL;
911 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200912
913 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
914 ;
915
916 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200917 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200918 p = s_next(m, p, &l);
919 }
920
921 return p;
922}
923
924static void s_stop(struct seq_file *m, void *p)
925{
926 struct trace_iterator *iter = m->private;
927
928 atomic_dec(&trace_record_cmdline_disabled);
929
930 /* let the tracer release locks here if needed */
931 if (current_trace && current_trace == iter->trace && iter->trace->stop)
932 iter->trace->stop(iter);
933
934 mutex_unlock(&trace_types_lock);
935}
936
Steven Rostedtb3806b42008-05-12 21:20:46 +0200937static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200938seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200939{
940#ifdef CONFIG_KALLSYMS
941 char str[KSYM_SYMBOL_LEN];
942
943 kallsyms_lookup(address, NULL, NULL, NULL, str);
944
Steven Rostedtb3806b42008-05-12 21:20:46 +0200945 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200946#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200947 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200948}
949
Steven Rostedtb3806b42008-05-12 21:20:46 +0200950static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200951seq_print_sym_offset(struct trace_seq *s, const char *fmt,
952 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200953{
954#ifdef CONFIG_KALLSYMS
955 char str[KSYM_SYMBOL_LEN];
956
957 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200958 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200959#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200960 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200961}
962
963#ifndef CONFIG_64BIT
964# define IP_FMT "%08lx"
965#else
966# define IP_FMT "%016lx"
967#endif
968
Steven Rostedtb3806b42008-05-12 21:20:46 +0200969static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +0200970seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200971{
Steven Rostedtb3806b42008-05-12 21:20:46 +0200972 int ret;
973
974 if (!ip)
975 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200976
977 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200978 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200979 else
Steven Rostedtb3806b42008-05-12 21:20:46 +0200980 ret = seq_print_sym_short(s, "%s", ip);
981
982 if (!ret)
983 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200984
985 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200986 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
987 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200988}
989
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200990static notrace void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200991{
992 seq_puts(m, "# _------=> CPU# \n");
993 seq_puts(m, "# / _-----=> irqs-off \n");
994 seq_puts(m, "# | / _----=> need-resched \n");
995 seq_puts(m, "# || / _---=> hardirq/softirq \n");
996 seq_puts(m, "# ||| / _--=> preempt-depth \n");
997 seq_puts(m, "# |||| / \n");
998 seq_puts(m, "# ||||| delay \n");
999 seq_puts(m, "# cmd pid ||||| time | caller \n");
1000 seq_puts(m, "# \\ / ||||| \\ | / \n");
1001}
1002
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001003static notrace void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001004{
1005 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1006 seq_puts(m, "# | | | | |\n");
1007}
1008
1009
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001010static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001011print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1012{
1013 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1014 struct trace_array *tr = iter->tr;
1015 struct trace_array_cpu *data = tr->data[tr->cpu];
1016 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001017 unsigned long total = 0;
1018 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001019 int cpu;
1020 const char *name = "preemption";
1021
1022 if (type)
1023 name = type->name;
1024
1025 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001026 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001027 total += tr->data[cpu]->trace_idx;
1028 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001029 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001030 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001031 entries += tr->data[cpu]->trace_idx;
1032 }
1033 }
1034
1035 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1036 name, UTS_RELEASE);
1037 seq_puts(m, "-----------------------------------"
1038 "---------------------------------\n");
1039 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1040 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001041 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001042 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001043 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044 tr->cpu,
1045#if defined(CONFIG_PREEMPT_NONE)
1046 "server",
1047#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1048 "desktop",
1049#elif defined(CONFIG_PREEMPT_DESKTOP)
1050 "preempt",
1051#else
1052 "unknown",
1053#endif
1054 /* These are reserved for later use */
1055 0, 0, 0, 0);
1056#ifdef CONFIG_SMP
1057 seq_printf(m, " #P:%d)\n", num_online_cpus());
1058#else
1059 seq_puts(m, ")\n");
1060#endif
1061 seq_puts(m, " -----------------\n");
1062 seq_printf(m, " | task: %.16s-%d "
1063 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1064 data->comm, data->pid, data->uid, data->nice,
1065 data->policy, data->rt_priority);
1066 seq_puts(m, " -----------------\n");
1067
1068 if (data->critical_start) {
1069 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001070 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1071 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001072 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001073 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1074 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001075 seq_puts(m, "\n");
1076 }
1077
1078 seq_puts(m, "\n");
1079}
1080
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001081static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001082lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001083{
1084 int hardirq, softirq;
1085 char *comm;
1086
1087 comm = trace_find_cmdline(entry->pid);
1088
Steven Rostedt214023c2008-05-12 21:20:46 +02001089 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1090 trace_seq_printf(s, "%d", cpu);
1091 trace_seq_printf(s, "%c%c",
1092 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1093 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001094
1095 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1096 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1097 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001098 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099 else {
1100 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001101 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001102 else {
1103 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001104 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001106 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001107 }
1108 }
1109
1110 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001111 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001112 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001113 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001114}
1115
1116unsigned long preempt_mark_thresh = 100;
1117
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001118static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001119lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120 unsigned long rel_usecs)
1121{
Steven Rostedt214023c2008-05-12 21:20:46 +02001122 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001123 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001124 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001125 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001126 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001127 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001128 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001129}
1130
1131static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1132
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001133static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +02001134print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001135{
Steven Rostedt214023c2008-05-12 21:20:46 +02001136 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001137 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1138 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1139 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1140 struct trace_entry *entry = iter->ent;
1141 unsigned long abs_usecs;
1142 unsigned long rel_usecs;
1143 char *comm;
1144 int S;
1145
1146 if (!next_entry)
1147 next_entry = entry;
1148 rel_usecs = ns2usecs(next_entry->t - entry->t);
1149 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1150
1151 if (verbose) {
1152 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001153 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1154 " %ld.%03ldms (+%ld.%03ldms): ",
1155 comm,
1156 entry->pid, cpu, entry->flags,
1157 entry->preempt_count, trace_idx,
1158 ns2usecs(entry->t),
1159 abs_usecs/1000,
1160 abs_usecs % 1000, rel_usecs/1000,
1161 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001162 } else {
Steven Rostedt214023c2008-05-12 21:20:46 +02001163 lat_print_generic(s, entry, cpu);
1164 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001165 }
1166 switch (entry->type) {
1167 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001168 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1169 trace_seq_puts(s, " (");
1170 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1171 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172 break;
1173 case TRACE_CTX:
1174 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1175 state_to_char[entry->ctx.prev_state] : 'X';
1176 comm = trace_find_cmdline(entry->ctx.next_pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001177 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1178 entry->ctx.prev_pid,
1179 entry->ctx.prev_prio,
1180 S,
1181 entry->ctx.next_pid,
1182 entry->ctx.next_prio,
1183 comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001184 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001185 case TRACE_SPECIAL:
1186 trace_seq_printf(s, " %lx %lx %lx\n",
1187 entry->special.arg1,
1188 entry->special.arg2,
1189 entry->special.arg3);
1190 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001191 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001192 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001193 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001194 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001195}
1196
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001197static notrace int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001198{
Steven Rostedt214023c2008-05-12 21:20:46 +02001199 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001200 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001201 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001202 unsigned long usec_rem;
1203 unsigned long long t;
1204 unsigned long secs;
1205 char *comm;
1206 int S;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001207 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001208
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001209 entry = iter->ent;
1210
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001211 comm = trace_find_cmdline(iter->ent->pid);
1212
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001213 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001214 usec_rem = do_div(t, 1000000ULL);
1215 secs = (unsigned long)t;
1216
Steven Rostedtb3806b42008-05-12 21:20:46 +02001217 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1218 if (!ret)
1219 return 0;
1220 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1221 if (!ret)
1222 return 0;
1223 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1224 if (!ret)
1225 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001226
1227 switch (entry->type) {
1228 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001229 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1230 if (!ret)
1231 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001232 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1233 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001234 ret = trace_seq_printf(s, " <-");
1235 if (!ret)
1236 return 0;
1237 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1238 sym_flags);
1239 if (!ret)
1240 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001241 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001242 ret = trace_seq_printf(s, "\n");
1243 if (!ret)
1244 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001245 break;
1246 case TRACE_CTX:
1247 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1248 state_to_char[entry->ctx.prev_state] : 'X';
Steven Rostedtb3806b42008-05-12 21:20:46 +02001249 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1250 entry->ctx.prev_pid,
1251 entry->ctx.prev_prio,
1252 S,
1253 entry->ctx.next_pid,
1254 entry->ctx.next_prio);
1255 if (!ret)
1256 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001257 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001258 case TRACE_SPECIAL:
1259 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1260 entry->special.arg1,
1261 entry->special.arg2,
1262 entry->special.arg3);
1263 if (!ret)
1264 return 0;
1265 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001266 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001267 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001268}
1269
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001270static notrace int print_raw_fmt(struct trace_iterator *iter)
1271{
1272 struct trace_seq *s = &iter->seq;
1273 struct trace_entry *entry;
1274 int ret;
1275 int S;
1276
1277 entry = iter->ent;
1278
1279 ret = trace_seq_printf(s, "%d %d %llu ",
1280 entry->pid, iter->cpu, entry->t);
1281 if (!ret)
1282 return 0;
1283
1284 switch (entry->type) {
1285 case TRACE_FN:
1286 ret = trace_seq_printf(s, "%x %x\n",
1287 entry->fn.ip, entry->fn.parent_ip);
1288 if (!ret)
1289 return 0;
1290 break;
1291 case TRACE_CTX:
1292 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1293 state_to_char[entry->ctx.prev_state] : 'X';
1294 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1295 entry->ctx.prev_pid,
1296 entry->ctx.prev_prio,
1297 S,
1298 entry->ctx.next_pid,
1299 entry->ctx.next_prio);
1300 if (!ret)
1301 return 0;
1302 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001303 case TRACE_SPECIAL:
1304 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1305 entry->special.arg1,
1306 entry->special.arg2,
1307 entry->special.arg3);
1308 if (!ret)
1309 return 0;
1310 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001311 }
1312 return 1;
1313}
1314
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001315#define SEQ_PUT_FIELD_RET(s, x) \
1316do { \
1317 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1318 return 0; \
1319} while (0)
1320
1321static notrace int print_bin_fmt(struct trace_iterator *iter)
1322{
1323 struct trace_seq *s = &iter->seq;
1324 struct trace_entry *entry;
1325
1326 entry = iter->ent;
1327
1328 SEQ_PUT_FIELD_RET(s, entry->pid);
1329 SEQ_PUT_FIELD_RET(s, entry->cpu);
1330 SEQ_PUT_FIELD_RET(s, entry->t);
1331
1332 switch (entry->type) {
1333 case TRACE_FN:
1334 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1335 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1336 break;
1337 case TRACE_CTX:
1338 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1339 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1340 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1341 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1342 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1343 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001344 case TRACE_SPECIAL:
1345 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1346 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1347 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1348 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001349 }
1350 return 1;
1351}
1352
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353static int trace_empty(struct trace_iterator *iter)
1354{
1355 struct trace_array_cpu *data;
1356 int cpu;
1357
1358 for_each_possible_cpu(cpu) {
1359 data = iter->tr->data[cpu];
1360
Steven Rostedtb3806b42008-05-12 21:20:46 +02001361 if (head_page(data) && data->trace_idx &&
1362 (data->trace_tail != data->trace_head ||
1363 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001364 return 0;
1365 }
1366 return 1;
1367}
1368
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001369static int print_trace_line(struct trace_iterator *iter)
1370{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001371 if (trace_flags & TRACE_ITER_BIN)
1372 return print_bin_fmt(iter);
1373
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001374 if (trace_flags & TRACE_ITER_RAW)
1375 return print_raw_fmt(iter);
1376
1377 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1378 return print_lat_fmt(iter, iter->idx, iter->cpu);
1379
1380 return print_trace_fmt(iter);
1381}
1382
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001383static int s_show(struct seq_file *m, void *v)
1384{
1385 struct trace_iterator *iter = v;
1386
1387 if (iter->ent == NULL) {
1388 if (iter->tr) {
1389 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1390 seq_puts(m, "#\n");
1391 }
1392 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1393 /* print nothing if the buffers are empty */
1394 if (trace_empty(iter))
1395 return 0;
1396 print_trace_header(m, iter);
1397 if (!(trace_flags & TRACE_ITER_VERBOSE))
1398 print_lat_help_header(m);
1399 } else {
1400 if (!(trace_flags & TRACE_ITER_VERBOSE))
1401 print_func_help_header(m);
1402 }
1403 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001404 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001405 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001406 }
1407
1408 return 0;
1409}
1410
1411static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001412 .start = s_start,
1413 .next = s_next,
1414 .stop = s_stop,
1415 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001416};
1417
1418static struct trace_iterator notrace *
1419__tracing_open(struct inode *inode, struct file *file, int *ret)
1420{
1421 struct trace_iterator *iter;
1422
Steven Rostedt60a11772008-05-12 21:20:44 +02001423 if (tracing_disabled) {
1424 *ret = -ENODEV;
1425 return NULL;
1426 }
1427
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001428 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1429 if (!iter) {
1430 *ret = -ENOMEM;
1431 goto out;
1432 }
1433
1434 mutex_lock(&trace_types_lock);
1435 if (current_trace && current_trace->print_max)
1436 iter->tr = &max_tr;
1437 else
1438 iter->tr = inode->i_private;
1439 iter->trace = current_trace;
1440 iter->pos = -1;
1441
1442 /* TODO stop tracer */
1443 *ret = seq_open(file, &tracer_seq_ops);
1444 if (!*ret) {
1445 struct seq_file *m = file->private_data;
1446 m->private = iter;
1447
1448 /* stop the trace while dumping */
1449 if (iter->tr->ctrl)
1450 tracer_enabled = 0;
1451
1452 if (iter->trace && iter->trace->open)
1453 iter->trace->open(iter);
1454 } else {
1455 kfree(iter);
1456 iter = NULL;
1457 }
1458 mutex_unlock(&trace_types_lock);
1459
1460 out:
1461 return iter;
1462}
1463
1464int tracing_open_generic(struct inode *inode, struct file *filp)
1465{
Steven Rostedt60a11772008-05-12 21:20:44 +02001466 if (tracing_disabled)
1467 return -ENODEV;
1468
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001469 filp->private_data = inode->i_private;
1470 return 0;
1471}
1472
1473int tracing_release(struct inode *inode, struct file *file)
1474{
1475 struct seq_file *m = (struct seq_file *)file->private_data;
1476 struct trace_iterator *iter = m->private;
1477
1478 mutex_lock(&trace_types_lock);
1479 if (iter->trace && iter->trace->close)
1480 iter->trace->close(iter);
1481
1482 /* reenable tracing if it was previously enabled */
1483 if (iter->tr->ctrl)
1484 tracer_enabled = 1;
1485 mutex_unlock(&trace_types_lock);
1486
1487 seq_release(inode, file);
1488 kfree(iter);
1489 return 0;
1490}
1491
1492static int tracing_open(struct inode *inode, struct file *file)
1493{
1494 int ret;
1495
1496 __tracing_open(inode, file, &ret);
1497
1498 return ret;
1499}
1500
1501static int tracing_lt_open(struct inode *inode, struct file *file)
1502{
1503 struct trace_iterator *iter;
1504 int ret;
1505
1506 iter = __tracing_open(inode, file, &ret);
1507
1508 if (!ret)
1509 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1510
1511 return ret;
1512}
1513
1514
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001515static notrace void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001516t_next(struct seq_file *m, void *v, loff_t *pos)
1517{
1518 struct tracer *t = m->private;
1519
1520 (*pos)++;
1521
1522 if (t)
1523 t = t->next;
1524
1525 m->private = t;
1526
1527 return t;
1528}
1529
1530static void *t_start(struct seq_file *m, loff_t *pos)
1531{
1532 struct tracer *t = m->private;
1533 loff_t l = 0;
1534
1535 mutex_lock(&trace_types_lock);
1536 for (; t && l < *pos; t = t_next(m, t, &l))
1537 ;
1538
1539 return t;
1540}
1541
1542static void t_stop(struct seq_file *m, void *p)
1543{
1544 mutex_unlock(&trace_types_lock);
1545}
1546
1547static int t_show(struct seq_file *m, void *v)
1548{
1549 struct tracer *t = v;
1550
1551 if (!t)
1552 return 0;
1553
1554 seq_printf(m, "%s", t->name);
1555 if (t->next)
1556 seq_putc(m, ' ');
1557 else
1558 seq_putc(m, '\n');
1559
1560 return 0;
1561}
1562
1563static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001564 .start = t_start,
1565 .next = t_next,
1566 .stop = t_stop,
1567 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001568};
1569
1570static int show_traces_open(struct inode *inode, struct file *file)
1571{
1572 int ret;
1573
Steven Rostedt60a11772008-05-12 21:20:44 +02001574 if (tracing_disabled)
1575 return -ENODEV;
1576
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001577 ret = seq_open(file, &show_traces_seq_ops);
1578 if (!ret) {
1579 struct seq_file *m = file->private_data;
1580 m->private = trace_types;
1581 }
1582
1583 return ret;
1584}
1585
1586static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001587 .open = tracing_open,
1588 .read = seq_read,
1589 .llseek = seq_lseek,
1590 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001591};
1592
1593static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001594 .open = tracing_lt_open,
1595 .read = seq_read,
1596 .llseek = seq_lseek,
1597 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001598};
1599
1600static struct file_operations show_traces_fops = {
1601 .open = show_traces_open,
1602 .read = seq_read,
1603 .release = seq_release,
1604};
1605
1606static ssize_t
1607tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1608 size_t cnt, loff_t *ppos)
1609{
1610 char *buf;
1611 int r = 0;
1612 int len = 0;
1613 int i;
1614
1615 /* calulate max size */
1616 for (i = 0; trace_options[i]; i++) {
1617 len += strlen(trace_options[i]);
1618 len += 3; /* "no" and space */
1619 }
1620
1621 /* +2 for \n and \0 */
1622 buf = kmalloc(len + 2, GFP_KERNEL);
1623 if (!buf)
1624 return -ENOMEM;
1625
1626 for (i = 0; trace_options[i]; i++) {
1627 if (trace_flags & (1 << i))
1628 r += sprintf(buf + r, "%s ", trace_options[i]);
1629 else
1630 r += sprintf(buf + r, "no%s ", trace_options[i]);
1631 }
1632
1633 r += sprintf(buf + r, "\n");
1634 WARN_ON(r >= len + 2);
1635
1636 r = simple_read_from_buffer(ubuf, cnt, ppos,
1637 buf, r);
1638
1639 kfree(buf);
1640
1641 return r;
1642}
1643
1644static ssize_t
1645tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1646 size_t cnt, loff_t *ppos)
1647{
1648 char buf[64];
1649 char *cmp = buf;
1650 int neg = 0;
1651 int i;
1652
1653 if (cnt > 63)
1654 cnt = 63;
1655
1656 if (copy_from_user(&buf, ubuf, cnt))
1657 return -EFAULT;
1658
1659 buf[cnt] = 0;
1660
1661 if (strncmp(buf, "no", 2) == 0) {
1662 neg = 1;
1663 cmp += 2;
1664 }
1665
1666 for (i = 0; trace_options[i]; i++) {
1667 int len = strlen(trace_options[i]);
1668
1669 if (strncmp(cmp, trace_options[i], len) == 0) {
1670 if (neg)
1671 trace_flags &= ~(1 << i);
1672 else
1673 trace_flags |= (1 << i);
1674 break;
1675 }
1676 }
1677
1678 filp->f_pos += cnt;
1679
1680 return cnt;
1681}
1682
1683static struct file_operations tracing_iter_fops = {
1684 .open = tracing_open_generic,
1685 .read = tracing_iter_ctrl_read,
1686 .write = tracing_iter_ctrl_write,
1687};
1688
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001689static const char readme_msg[] =
1690 "tracing mini-HOWTO:\n\n"
1691 "# mkdir /debug\n"
1692 "# mount -t debugfs nodev /debug\n\n"
1693 "# cat /debug/tracing/available_tracers\n"
1694 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1695 "# cat /debug/tracing/current_tracer\n"
1696 "none\n"
1697 "# echo sched_switch > /debug/tracing/current_tracer\n"
1698 "# cat /debug/tracing/current_tracer\n"
1699 "sched_switch\n"
1700 "# cat /debug/tracing/iter_ctrl\n"
1701 "noprint-parent nosym-offset nosym-addr noverbose\n"
1702 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1703 "# echo 1 > /debug/tracing/tracing_enabled\n"
1704 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1705 "echo 0 > /debug/tracing/tracing_enabled\n"
1706;
1707
1708static ssize_t
1709tracing_readme_read(struct file *filp, char __user *ubuf,
1710 size_t cnt, loff_t *ppos)
1711{
1712 return simple_read_from_buffer(ubuf, cnt, ppos,
1713 readme_msg, strlen(readme_msg));
1714}
1715
1716static struct file_operations tracing_readme_fops = {
1717 .open = tracing_open_generic,
1718 .read = tracing_readme_read,
1719};
1720
1721
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001722static ssize_t
1723tracing_ctrl_read(struct file *filp, char __user *ubuf,
1724 size_t cnt, loff_t *ppos)
1725{
1726 struct trace_array *tr = filp->private_data;
1727 char buf[64];
1728 int r;
1729
1730 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001731 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732}
1733
1734static ssize_t
1735tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1736 size_t cnt, loff_t *ppos)
1737{
1738 struct trace_array *tr = filp->private_data;
1739 long val;
1740 char buf[64];
1741
1742 if (cnt > 63)
1743 cnt = 63;
1744
1745 if (copy_from_user(&buf, ubuf, cnt))
1746 return -EFAULT;
1747
1748 buf[cnt] = 0;
1749
1750 val = simple_strtoul(buf, NULL, 10);
1751
1752 val = !!val;
1753
1754 mutex_lock(&trace_types_lock);
1755 if (tr->ctrl ^ val) {
1756 if (val)
1757 tracer_enabled = 1;
1758 else
1759 tracer_enabled = 0;
1760
1761 tr->ctrl = val;
1762
1763 if (current_trace && current_trace->ctrl_update)
1764 current_trace->ctrl_update(tr);
1765 }
1766 mutex_unlock(&trace_types_lock);
1767
1768 filp->f_pos += cnt;
1769
1770 return cnt;
1771}
1772
1773static ssize_t
1774tracing_set_trace_read(struct file *filp, char __user *ubuf,
1775 size_t cnt, loff_t *ppos)
1776{
1777 char buf[max_tracer_type_len+2];
1778 int r;
1779
1780 mutex_lock(&trace_types_lock);
1781 if (current_trace)
1782 r = sprintf(buf, "%s\n", current_trace->name);
1783 else
1784 r = sprintf(buf, "\n");
1785 mutex_unlock(&trace_types_lock);
1786
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001787 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001788}
1789
1790static ssize_t
1791tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1792 size_t cnt, loff_t *ppos)
1793{
1794 struct trace_array *tr = &global_trace;
1795 struct tracer *t;
1796 char buf[max_tracer_type_len+1];
1797 int i;
1798
1799 if (cnt > max_tracer_type_len)
1800 cnt = max_tracer_type_len;
1801
1802 if (copy_from_user(&buf, ubuf, cnt))
1803 return -EFAULT;
1804
1805 buf[cnt] = 0;
1806
1807 /* strip ending whitespace. */
1808 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1809 buf[i] = 0;
1810
1811 mutex_lock(&trace_types_lock);
1812 for (t = trace_types; t; t = t->next) {
1813 if (strcmp(t->name, buf) == 0)
1814 break;
1815 }
1816 if (!t || t == current_trace)
1817 goto out;
1818
1819 if (current_trace && current_trace->reset)
1820 current_trace->reset(tr);
1821
1822 current_trace = t;
1823 if (t->init)
1824 t->init(tr);
1825
1826 out:
1827 mutex_unlock(&trace_types_lock);
1828
1829 filp->f_pos += cnt;
1830
1831 return cnt;
1832}
1833
1834static ssize_t
1835tracing_max_lat_read(struct file *filp, char __user *ubuf,
1836 size_t cnt, loff_t *ppos)
1837{
1838 unsigned long *ptr = filp->private_data;
1839 char buf[64];
1840 int r;
1841
1842 r = snprintf(buf, 64, "%ld\n",
1843 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1844 if (r > 64)
1845 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001846 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001847}
1848
1849static ssize_t
1850tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1851 size_t cnt, loff_t *ppos)
1852{
1853 long *ptr = filp->private_data;
1854 long val;
1855 char buf[64];
1856
1857 if (cnt > 63)
1858 cnt = 63;
1859
1860 if (copy_from_user(&buf, ubuf, cnt))
1861 return -EFAULT;
1862
1863 buf[cnt] = 0;
1864
1865 val = simple_strtoul(buf, NULL, 10);
1866
1867 *ptr = val * 1000;
1868
1869 return cnt;
1870}
1871
Steven Rostedtb3806b42008-05-12 21:20:46 +02001872static atomic_t tracing_reader;
1873
1874static int tracing_open_pipe(struct inode *inode, struct file *filp)
1875{
1876 struct trace_iterator *iter;
1877
1878 if (tracing_disabled)
1879 return -ENODEV;
1880
1881 /* We only allow for reader of the pipe */
1882 if (atomic_inc_return(&tracing_reader) != 1) {
1883 atomic_dec(&tracing_reader);
1884 return -EBUSY;
1885 }
1886
1887 /* create a buffer to store the information to pass to userspace */
1888 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1889 if (!iter)
1890 return -ENOMEM;
1891
1892 iter->tr = &global_trace;
1893
1894 filp->private_data = iter;
1895
1896 return 0;
1897}
1898
1899static int tracing_release_pipe(struct inode *inode, struct file *file)
1900{
1901 struct trace_iterator *iter = file->private_data;
1902
1903 kfree(iter);
1904 atomic_dec(&tracing_reader);
1905
1906 return 0;
1907}
1908
1909/*
1910 * Consumer reader.
1911 */
1912static ssize_t
1913tracing_read_pipe(struct file *filp, char __user *ubuf,
1914 size_t cnt, loff_t *ppos)
1915{
1916 struct trace_iterator *iter = filp->private_data;
1917 struct trace_array_cpu *data;
1918 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001919 static int start;
1920 unsigned long flags;
1921 int read = 0;
1922 int cpu;
1923 int len;
1924 int ret;
1925
1926 /* return any leftover data */
1927 if (iter->seq.len > start) {
1928 len = iter->seq.len - start;
1929 if (cnt > len)
1930 cnt = len;
1931 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1932 if (ret)
1933 cnt = -EFAULT;
1934
1935 start += len;
1936
1937 return cnt;
1938 }
1939
1940 trace_seq_reset(&iter->seq);
1941 start = 0;
1942
1943 while (trace_empty(iter)) {
1944 /*
1945 * This is a make-shift waitqueue. The reason we don't use
1946 * an actual wait queue is because:
1947 * 1) we only ever have one waiter
1948 * 2) the tracing, traces all functions, we don't want
1949 * the overhead of calling wake_up and friends
1950 * (and tracing them too)
1951 * Anyway, this is really very primitive wakeup.
1952 */
1953 set_current_state(TASK_INTERRUPTIBLE);
1954 iter->tr->waiter = current;
1955
1956 /* sleep for one second, and try again. */
1957 schedule_timeout(HZ);
1958
1959 iter->tr->waiter = NULL;
1960
1961 if (signal_pending(current))
1962 return -EINTR;
1963
1964 /*
1965 * We block until we read something and tracing is disabled.
1966 * We still block if tracing is disabled, but we have never
1967 * read anything. This allows a user to cat this file, and
1968 * then enable tracing. But after we have read something,
1969 * we give an EOF when tracing is again disabled.
1970 *
1971 * iter->pos will be 0 if we haven't read anything.
1972 */
1973 if (!tracer_enabled && iter->pos)
1974 break;
1975
1976 continue;
1977 }
1978
1979 /* stop when tracing is finished */
1980 if (trace_empty(iter))
1981 return 0;
1982
1983 if (cnt >= PAGE_SIZE)
1984 cnt = PAGE_SIZE - 1;
1985
1986 memset(iter, 0, sizeof(*iter));
1987 iter->tr = &global_trace;
1988 iter->pos = -1;
1989
1990 /*
1991 * We need to stop all tracing on all CPUS to read the
1992 * the next buffer. This is a bit expensive, but is
1993 * not done often. We fill all what we can read,
1994 * and then release the locks again.
1995 */
1996
1997 cpus_clear(mask);
1998 local_irq_save(flags);
1999 for_each_possible_cpu(cpu) {
2000 data = iter->tr->data[cpu];
2001
2002 if (!head_page(data) || !data->trace_idx)
2003 continue;
2004
2005 atomic_inc(&data->disabled);
2006 spin_lock(&data->lock);
2007 cpu_set(cpu, mask);
2008 }
2009
Steven Rostedt088b1e422008-05-12 21:20:48 +02002010 while (find_next_entry_inc(iter) != NULL) {
2011 int len = iter->seq.len;
2012
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002013 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002014 if (!ret) {
2015 /* don't print partial lines */
2016 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002017 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002018 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002019
2020 trace_consume(iter);
2021
2022 if (iter->seq.len >= cnt)
2023 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002024 }
2025
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002026 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002027 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002028 spin_unlock(&data->lock);
2029 atomic_dec(&data->disabled);
2030 }
2031 local_irq_restore(flags);
2032
2033 /* Now copy what we have to the user */
2034 read = iter->seq.len;
2035 if (read > cnt)
2036 read = cnt;
2037
2038 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2039
2040 if (read < iter->seq.len)
2041 start = read;
2042 else
2043 trace_seq_reset(&iter->seq);
2044
2045 if (ret)
2046 read = -EFAULT;
2047
2048 return read;
2049}
2050
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002051static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002052 .open = tracing_open_generic,
2053 .read = tracing_max_lat_read,
2054 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002055};
2056
2057static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002058 .open = tracing_open_generic,
2059 .read = tracing_ctrl_read,
2060 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002061};
2062
2063static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002064 .open = tracing_open_generic,
2065 .read = tracing_set_trace_read,
2066 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002067};
2068
Steven Rostedtb3806b42008-05-12 21:20:46 +02002069static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002070 .open = tracing_open_pipe,
2071 .read = tracing_read_pipe,
2072 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002073};
2074
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002075#ifdef CONFIG_DYNAMIC_FTRACE
2076
2077static ssize_t
2078tracing_read_long(struct file *filp, char __user *ubuf,
2079 size_t cnt, loff_t *ppos)
2080{
2081 unsigned long *p = filp->private_data;
2082 char buf[64];
2083 int r;
2084
2085 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002086
2087 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002088}
2089
2090static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002091 .open = tracing_open_generic,
2092 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002093};
2094#endif
2095
2096static struct dentry *d_tracer;
2097
2098struct dentry *tracing_init_dentry(void)
2099{
2100 static int once;
2101
2102 if (d_tracer)
2103 return d_tracer;
2104
2105 d_tracer = debugfs_create_dir("tracing", NULL);
2106
2107 if (!d_tracer && !once) {
2108 once = 1;
2109 pr_warning("Could not create debugfs directory 'tracing'\n");
2110 return NULL;
2111 }
2112
2113 return d_tracer;
2114}
2115
Steven Rostedt60a11772008-05-12 21:20:44 +02002116#ifdef CONFIG_FTRACE_SELFTEST
2117/* Let selftest have access to static functions in this file */
2118#include "trace_selftest.c"
2119#endif
2120
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002121static __init void tracer_init_debugfs(void)
2122{
2123 struct dentry *d_tracer;
2124 struct dentry *entry;
2125
2126 d_tracer = tracing_init_dentry();
2127
2128 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2129 &global_trace, &tracing_ctrl_fops);
2130 if (!entry)
2131 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2132
2133 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2134 NULL, &tracing_iter_fops);
2135 if (!entry)
2136 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2137
2138 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2139 &global_trace, &tracing_lt_fops);
2140 if (!entry)
2141 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2142
2143 entry = debugfs_create_file("trace", 0444, d_tracer,
2144 &global_trace, &tracing_fops);
2145 if (!entry)
2146 pr_warning("Could not create debugfs 'trace' entry\n");
2147
2148 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2149 &global_trace, &show_traces_fops);
2150 if (!entry)
2151 pr_warning("Could not create debugfs 'trace' entry\n");
2152
2153 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2154 &global_trace, &set_tracer_fops);
2155 if (!entry)
2156 pr_warning("Could not create debugfs 'trace' entry\n");
2157
2158 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2159 &tracing_max_latency,
2160 &tracing_max_lat_fops);
2161 if (!entry)
2162 pr_warning("Could not create debugfs "
2163 "'tracing_max_latency' entry\n");
2164
2165 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2166 &tracing_thresh, &tracing_max_lat_fops);
2167 if (!entry)
2168 pr_warning("Could not create debugfs "
2169 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002170 entry = debugfs_create_file("README", 0644, d_tracer,
2171 NULL, &tracing_readme_fops);
2172 if (!entry)
2173 pr_warning("Could not create debugfs 'README' entry\n");
2174
Steven Rostedtb3806b42008-05-12 21:20:46 +02002175 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2176 NULL, &tracing_pipe_fops);
2177 if (!entry)
2178 pr_warning("Could not create debugfs "
2179 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002180
2181#ifdef CONFIG_DYNAMIC_FTRACE
2182 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2183 &ftrace_update_tot_cnt,
2184 &tracing_read_long_fops);
2185 if (!entry)
2186 pr_warning("Could not create debugfs "
2187 "'dyn_ftrace_total_info' entry\n");
2188#endif
2189}
2190
2191/* dummy trace to disable tracing */
2192static struct tracer no_tracer __read_mostly =
2193{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002194 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002195};
2196
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002197static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002198{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002199 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002200 struct page *page, *tmp;
2201 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002202 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002203 int i;
2204
2205 /* first allocate a page for each CPU */
2206 for_each_possible_cpu(i) {
2207 array = (void *)__get_free_page(GFP_KERNEL);
2208 if (array == NULL) {
2209 printk(KERN_ERR "tracer: failed to allocate page"
2210 "for trace buffer!\n");
2211 goto free_pages;
2212 }
2213
2214 page = virt_to_page(array);
2215 list_add(&page->lru, &pages);
2216
2217/* Only allocate if we are actually using the max trace */
2218#ifdef CONFIG_TRACER_MAX_TRACE
2219 array = (void *)__get_free_page(GFP_KERNEL);
2220 if (array == NULL) {
2221 printk(KERN_ERR "tracer: failed to allocate page"
2222 "for trace buffer!\n");
2223 goto free_pages;
2224 }
2225 page = virt_to_page(array);
2226 list_add(&page->lru, &pages);
2227#endif
2228 }
2229
2230 /* Now that we successfully allocate a page per CPU, add them */
2231 for_each_possible_cpu(i) {
2232 data = global_trace.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002233 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002234 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002235 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002236 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002237 list_add_tail(&page->lru, &data->trace_pages);
2238 ClearPageLRU(page);
2239
2240#ifdef CONFIG_TRACER_MAX_TRACE
2241 data = max_tr.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002242 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002243 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002244 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002245 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002246 list_add_tail(&page->lru, &data->trace_pages);
2247 SetPageLRU(page);
2248#endif
2249 }
2250 global_trace.entries += ENTRIES_PER_PAGE;
2251
2252 return 0;
2253
2254 free_pages:
2255 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002256 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002257 __free_page(page);
2258 }
2259 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002260}
2261
2262__init static int tracer_alloc_buffers(void)
2263{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002264 struct trace_array_cpu *data;
2265 void *array;
2266 struct page *page;
2267 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002268 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002269 int i;
2270
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002271 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002272 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002273 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002274 max_tr.data[i] = &per_cpu(max_data, i);
2275
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002276 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002277 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002278 printk(KERN_ERR "tracer: failed to allocate page"
2279 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002280 goto free_buffers;
2281 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002282
2283 /* set the array to the list */
2284 INIT_LIST_HEAD(&data->trace_pages);
2285 page = virt_to_page(array);
2286 list_add(&page->lru, &data->trace_pages);
2287 /* use the LRU flag to differentiate the two buffers */
2288 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002289
2290/* Only allocate if we are actually using the max trace */
2291#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002292 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002293 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002294 printk(KERN_ERR "tracer: failed to allocate page"
2295 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002296 goto free_buffers;
2297 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002298
2299 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2300 page = virt_to_page(array);
2301 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2302 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002303#endif
2304 }
2305
2306 /*
2307 * Since we allocate by orders of pages, we may be able to
2308 * round up a bit.
2309 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002310 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002311 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002312
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002313 while (global_trace.entries < trace_nr_entries) {
2314 if (trace_alloc_page())
2315 break;
2316 pages++;
2317 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002318 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002319
2320 pr_info("tracer: %d pages allocated for %ld",
2321 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002322 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2323 pr_info(" actual entries %ld\n", global_trace.entries);
2324
2325 tracer_init_debugfs();
2326
2327 trace_init_cmdlines();
2328
2329 register_tracer(&no_tracer);
2330 current_trace = &no_tracer;
2331
Steven Rostedt60a11772008-05-12 21:20:44 +02002332 /* All seems OK, enable tracing */
2333 tracing_disabled = 0;
2334
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002335 return 0;
2336
2337 free_buffers:
2338 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002339 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002340 struct trace_array_cpu *data = global_trace.data[i];
2341
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002342 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002343 list_for_each_entry_safe(page, tmp,
2344 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002345 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002346 __free_page(page);
2347 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002348 }
2349
2350#ifdef CONFIG_TRACER_MAX_TRACE
2351 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002352 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002353 list_for_each_entry_safe(page, tmp,
2354 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002355 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002356 __free_page(page);
2357 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002358 }
2359#endif
2360 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002361 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002362}
Steven Rostedt60a11772008-05-12 21:20:44 +02002363fs_initcall(tracer_alloc_buffers);