blob: d78cbc4fc519d2f9f68033fd4b2991f22e7fb3b1 [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,
140
141 __TRACE_LAST_TYPE
142};
143
144enum trace_flag_type {
145 TRACE_FLAG_IRQS_OFF = 0x01,
146 TRACE_FLAG_NEED_RESCHED = 0x02,
147 TRACE_FLAG_HARDIRQ = 0x04,
148 TRACE_FLAG_SOFTIRQ = 0x08,
149};
150
151enum trace_iterator_flags {
152 TRACE_ITER_PRINT_PARENT = 0x01,
153 TRACE_ITER_SYM_OFFSET = 0x02,
154 TRACE_ITER_SYM_ADDR = 0x04,
155 TRACE_ITER_VERBOSE = 0x08,
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200156 TRACE_ITER_RAW = 0x10,
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200157 TRACE_ITER_BIN = 0x20,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200158};
159
160#define TRACE_ITER_SYM_MASK \
161 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
162
163/* These must match the bit postions above */
164static const char *trace_options[] = {
165 "print-parent",
166 "sym-offset",
167 "sym-addr",
168 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200169 "raw",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200170 "bin",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200171 NULL
172};
173
174static unsigned trace_flags;
175
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200176static DEFINE_SPINLOCK(ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200177
178/*
179 * Copy the new maximum trace into the separate maximum-trace
180 * structure. (this way the maximum trace is permanently saved,
181 * for later retrieval via /debugfs/tracing/latency_trace)
182 */
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200183static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200184__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
185{
186 struct trace_array_cpu *data = tr->data[cpu];
187
188 max_tr.cpu = cpu;
189 max_tr.time_start = data->preempt_timestamp;
190
191 data = max_tr.data[cpu];
192 data->saved_latency = tracing_max_latency;
193
194 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
195 data->pid = tsk->pid;
196 data->uid = tsk->uid;
197 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
198 data->policy = tsk->policy;
199 data->rt_priority = tsk->rt_priority;
200
201 /* record this tasks comm */
202 tracing_record_cmdline(current);
203}
204
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200205void check_pages(struct trace_array_cpu *data)
206{
207 struct page *page, *tmp;
208
209 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
210 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
211
212 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
213 BUG_ON(page->lru.next->prev != &page->lru);
214 BUG_ON(page->lru.prev->next != &page->lru);
215 }
216}
217
218void *head_page(struct trace_array_cpu *data)
219{
220 struct page *page;
221
222 check_pages(data);
223 if (list_empty(&data->trace_pages))
224 return NULL;
225
226 page = list_entry(data->trace_pages.next, struct page, lru);
227 BUG_ON(&page->lru == &data->trace_pages);
228
229 return page_address(page);
230}
231
Steven Rostedt214023c2008-05-12 21:20:46 +0200232static notrace int
233trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
234{
235 int len = (PAGE_SIZE - 1) - s->len;
236 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200237 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200238
239 if (!len)
240 return 0;
241
242 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200243 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200244 va_end(ap);
245
Steven Rostedtb3806b42008-05-12 21:20:46 +0200246 /* If we can't write it all, don't bother writing anything */
247 if (ret > len)
248 return 0;
249
250 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200251
252 return len;
253}
254
255static notrace int
256trace_seq_puts(struct trace_seq *s, const char *str)
257{
258 int len = strlen(str);
259
260 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200261 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200262
263 memcpy(s->buffer + s->len, str, len);
264 s->len += len;
265
266 return len;
267}
268
269static notrace int
270trace_seq_putc(struct trace_seq *s, unsigned char c)
271{
272 if (s->len >= (PAGE_SIZE - 1))
273 return 0;
274
275 s->buffer[s->len++] = c;
276
277 return 1;
278}
279
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200280static notrace int
281trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
282{
283 if (len > ((PAGE_SIZE - 1) - s->len))
284 return 0;
285
286 memcpy(s->buffer + s->len, mem, len);
287 s->len += len;
288
289 return len;
290}
291
Steven Rostedt214023c2008-05-12 21:20:46 +0200292static notrace void
293trace_seq_reset(struct trace_seq *s)
294{
295 s->len = 0;
296}
297
298static notrace void
299trace_print_seq(struct seq_file *m, struct trace_seq *s)
300{
301 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
302
303 s->buffer[len] = 0;
304 seq_puts(m, s->buffer);
305
306 trace_seq_reset(s);
307}
308
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200309notrace static void
310flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
311{
312 struct list_head flip_pages;
313
314 INIT_LIST_HEAD(&flip_pages);
315
Steven Rostedt93a588f2008-05-12 21:20:45 +0200316 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200317 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200318 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200319
320 check_pages(tr1);
321 check_pages(tr2);
322 list_splice_init(&tr1->trace_pages, &flip_pages);
323 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
324 list_splice_init(&flip_pages, &tr2->trace_pages);
325 BUG_ON(!list_empty(&flip_pages));
326 check_pages(tr1);
327 check_pages(tr2);
328}
329
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200330notrace void
331update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
332{
333 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200334 int i;
335
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200336 WARN_ON_ONCE(!irqs_disabled());
337 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200338 /* clear out all the previous traces */
339 for_each_possible_cpu(i) {
340 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200341 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200342 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200343 }
344
345 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200346 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200347}
348
349/**
350 * update_max_tr_single - only copy one trace over, and reset the rest
351 * @tr - tracer
352 * @tsk - task with the latency
353 * @cpu - the cpu of the buffer to copy.
354 */
355notrace void
356update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
357{
358 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200359 int i;
360
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200361 WARN_ON_ONCE(!irqs_disabled());
362 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200363 for_each_possible_cpu(i)
364 tracing_reset(max_tr.data[i]);
365
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200366 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200367 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200368
369 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200370 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200371}
372
373int register_tracer(struct tracer *type)
374{
375 struct tracer *t;
376 int len;
377 int ret = 0;
378
379 if (!type->name) {
380 pr_info("Tracer must have a name\n");
381 return -1;
382 }
383
384 mutex_lock(&trace_types_lock);
385 for (t = trace_types; t; t = t->next) {
386 if (strcmp(type->name, t->name) == 0) {
387 /* already found */
388 pr_info("Trace %s already registered\n",
389 type->name);
390 ret = -1;
391 goto out;
392 }
393 }
394
Steven Rostedt60a11772008-05-12 21:20:44 +0200395#ifdef CONFIG_FTRACE_STARTUP_TEST
396 if (type->selftest) {
397 struct tracer *saved_tracer = current_trace;
398 struct trace_array_cpu *data;
399 struct trace_array *tr = &global_trace;
400 int saved_ctrl = tr->ctrl;
401 int i;
402 /*
403 * Run a selftest on this tracer.
404 * Here we reset the trace buffer, and set the current
405 * tracer to be this tracer. The tracer can then run some
406 * internal tracing to verify that everything is in order.
407 * If we fail, we do not register this tracer.
408 */
409 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200410 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200411 if (!head_page(data))
412 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200413 tracing_reset(data);
414 }
415 current_trace = type;
416 tr->ctrl = 0;
417 /* the test is responsible for initializing and enabling */
418 pr_info("Testing tracer %s: ", type->name);
419 ret = type->selftest(type, tr);
420 /* the test is responsible for resetting too */
421 current_trace = saved_tracer;
422 tr->ctrl = saved_ctrl;
423 if (ret) {
424 printk(KERN_CONT "FAILED!\n");
425 goto out;
426 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200427 /* Only reset on passing, to avoid touching corrupted buffers */
428 for_each_possible_cpu(i) {
429 data = tr->data[i];
430 if (!head_page(data))
431 continue;
432 tracing_reset(data);
433 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200434 printk(KERN_CONT "PASSED\n");
435 }
436#endif
437
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200438 type->next = trace_types;
439 trace_types = type;
440 len = strlen(type->name);
441 if (len > max_tracer_type_len)
442 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200443
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200444 out:
445 mutex_unlock(&trace_types_lock);
446
447 return ret;
448}
449
450void unregister_tracer(struct tracer *type)
451{
452 struct tracer **t;
453 int len;
454
455 mutex_lock(&trace_types_lock);
456 for (t = &trace_types; *t; t = &(*t)->next) {
457 if (*t == type)
458 goto found;
459 }
460 pr_info("Trace %s not registered\n", type->name);
461 goto out;
462
463 found:
464 *t = (*t)->next;
465 if (strlen(type->name) != max_tracer_type_len)
466 goto out;
467
468 max_tracer_type_len = 0;
469 for (t = &trace_types; *t; t = &(*t)->next) {
470 len = strlen((*t)->name);
471 if (len > max_tracer_type_len)
472 max_tracer_type_len = len;
473 }
474 out:
475 mutex_unlock(&trace_types_lock);
476}
477
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200478notrace void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200479{
480 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200481 data->trace_head = data->trace_tail = head_page(data);
482 data->trace_head_idx = 0;
483 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200484}
485
486#ifdef CONFIG_FTRACE
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200487static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200488function_trace_call(unsigned long ip, unsigned long parent_ip)
489{
490 struct trace_array *tr = &global_trace;
491 struct trace_array_cpu *data;
492 unsigned long flags;
493 long disabled;
494 int cpu;
495
496 if (unlikely(!tracer_enabled))
497 return;
498
Steven Rostedt18cef372008-05-12 21:20:44 +0200499 local_irq_save(flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200500 cpu = raw_smp_processor_id();
501 data = tr->data[cpu];
502 disabled = atomic_inc_return(&data->disabled);
503
504 if (likely(disabled == 1))
505 ftrace(tr, data, ip, parent_ip, flags);
506
507 atomic_dec(&data->disabled);
Steven Rostedt18cef372008-05-12 21:20:44 +0200508 local_irq_restore(flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200509}
510
511static struct ftrace_ops trace_ops __read_mostly =
512{
513 .func = function_trace_call,
514};
515#endif
516
517notrace void tracing_start_function_trace(void)
518{
519 register_ftrace_function(&trace_ops);
520}
521
522notrace void tracing_stop_function_trace(void)
523{
524 unregister_ftrace_function(&trace_ops);
525}
526
527#define SAVED_CMDLINES 128
528static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
529static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
530static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
531static int cmdline_idx;
532static DEFINE_SPINLOCK(trace_cmdline_lock);
533atomic_t trace_record_cmdline_disabled;
534
535static void trace_init_cmdlines(void)
536{
537 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
538 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
539 cmdline_idx = 0;
540}
541
542notrace void trace_stop_cmdline_recording(void);
543
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200544static notrace void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200545{
546 unsigned map;
547 unsigned idx;
548
549 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
550 return;
551
552 /*
553 * It's not the end of the world if we don't get
554 * the lock, but we also don't want to spin
555 * nor do we want to disable interrupts,
556 * so if we miss here, then better luck next time.
557 */
558 if (!spin_trylock(&trace_cmdline_lock))
559 return;
560
561 idx = map_pid_to_cmdline[tsk->pid];
562 if (idx >= SAVED_CMDLINES) {
563 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
564
565 map = map_cmdline_to_pid[idx];
566 if (map <= PID_MAX_DEFAULT)
567 map_pid_to_cmdline[map] = (unsigned)-1;
568
569 map_pid_to_cmdline[tsk->pid] = idx;
570
571 cmdline_idx = idx;
572 }
573
574 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
575
576 spin_unlock(&trace_cmdline_lock);
577}
578
579static notrace char *trace_find_cmdline(int pid)
580{
581 char *cmdline = "<...>";
582 unsigned map;
583
584 if (!pid)
585 return "<idle>";
586
587 if (pid > PID_MAX_DEFAULT)
588 goto out;
589
590 map = map_pid_to_cmdline[pid];
591 if (map >= SAVED_CMDLINES)
592 goto out;
593
594 cmdline = saved_cmdlines[map];
595
596 out:
597 return cmdline;
598}
599
600notrace void tracing_record_cmdline(struct task_struct *tsk)
601{
602 if (atomic_read(&trace_record_cmdline_disabled))
603 return;
604
605 trace_save_cmdline(tsk);
606}
607
Steven Rostedt93a588f2008-05-12 21:20:45 +0200608static inline notrace struct list_head *
609trace_next_list(struct trace_array_cpu *data, struct list_head *next)
610{
611 /*
612 * Roundrobin - but skip the head (which is not a real page):
613 */
614 next = next->next;
615 if (unlikely(next == &data->trace_pages))
616 next = next->next;
617 BUG_ON(next == &data->trace_pages);
618
619 return next;
620}
621
622static inline notrace void *
623trace_next_page(struct trace_array_cpu *data, void *addr)
624{
625 struct list_head *next;
626 struct page *page;
627
628 page = virt_to_page(addr);
629
630 next = trace_next_list(data, &page->lru);
631 page = list_entry(next, struct page, lru);
632
633 return page_address(page);
634}
635
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200636static inline notrace struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200637tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200638{
639 unsigned long idx, idx_next;
640 struct trace_entry *entry;
641
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200642 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200643 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200644 idx_next = idx + 1;
645
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200646 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
647
Steven Rostedt93a588f2008-05-12 21:20:45 +0200648 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200649
650 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200651 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652 idx_next = 0;
653 }
654
Steven Rostedt93a588f2008-05-12 21:20:45 +0200655 if (data->trace_head == data->trace_tail &&
656 idx_next == data->trace_tail_idx) {
657 /* overrun */
658 data->trace_tail_idx++;
659 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
660 data->trace_tail =
661 trace_next_page(data, data->trace_tail);
662 data->trace_tail_idx = 0;
663 }
664 }
665
666 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200667
668 return entry;
669}
670
671static inline notrace void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200672tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200673{
674 struct task_struct *tsk = current;
675 unsigned long pc;
676
677 pc = preempt_count();
678
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200679 entry->preempt_count = pc & 0xff;
680 entry->pid = tsk->pid;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200681 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200682 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
683 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
684 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
685 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
686}
687
688notrace void
689ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200690 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200691{
692 struct trace_entry *entry;
693
Steven Rostedtb3806b42008-05-12 21:20:46 +0200694 spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200695 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200696 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200697 entry->type = TRACE_FN;
698 entry->fn.ip = ip;
699 entry->fn.parent_ip = parent_ip;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200700 spin_unlock(&data->lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200701}
702
703notrace void
704tracing_sched_switch_trace(struct trace_array *tr,
705 struct trace_array_cpu *data,
706 struct task_struct *prev, struct task_struct *next,
707 unsigned long flags)
708{
709 struct trace_entry *entry;
710
Steven Rostedtb3806b42008-05-12 21:20:46 +0200711 spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200712 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200713 tracing_generic_entry_update(entry, flags);
714 entry->type = TRACE_CTX;
715 entry->ctx.prev_pid = prev->pid;
716 entry->ctx.prev_prio = prev->prio;
717 entry->ctx.prev_state = prev->state;
718 entry->ctx.next_pid = next->pid;
719 entry->ctx.next_prio = next->prio;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200720 spin_unlock(&data->lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200721}
722
723enum trace_file_type {
724 TRACE_FILE_LAT_FMT = 1,
725};
726
727static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200728trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
729 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200731 struct page *page;
732 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200733
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200734 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200735 iter->next_idx[cpu] >= data->trace_idx ||
736 (data->trace_head == data->trace_tail &&
737 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200738 return NULL;
739
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200740 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200741 /* Initialize the iterator for this cpu trace buffer */
742 WARN_ON(!data->trace_tail);
743 page = virt_to_page(data->trace_tail);
744 iter->next_page[cpu] = &page->lru;
745 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200746 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200747
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200748 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200749 BUG_ON(&data->trace_pages == &page->lru);
750
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200751 array = page_address(page);
752
Steven Rostedt93a588f2008-05-12 21:20:45 +0200753 /* Still possible to catch up to the tail */
754 if (iter->next_idx[cpu] && array == data->trace_tail &&
755 iter->next_page_idx[cpu] == data->trace_tail_idx)
756 return NULL;
757
758 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200759 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200760}
761
762static struct notrace trace_entry *
763find_next_entry(struct trace_iterator *iter, int *ent_cpu)
764{
765 struct trace_array *tr = iter->tr;
766 struct trace_entry *ent, *next = NULL;
767 int next_cpu = -1;
768 int cpu;
769
770 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200771 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200772 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200773 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200774 /*
775 * Pick the entry with the smallest timestamp:
776 */
777 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200778 next = ent;
779 next_cpu = cpu;
780 }
781 }
782
783 if (ent_cpu)
784 *ent_cpu = next_cpu;
785
786 return next;
787}
788
Ingo Molnar8c523a92008-05-12 21:20:46 +0200789static notrace void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200790{
791 iter->idx++;
792 iter->next_idx[iter->cpu]++;
793 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200794
Steven Rostedtb3806b42008-05-12 21:20:46 +0200795 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
796 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
797
798 iter->next_page_idx[iter->cpu] = 0;
799 iter->next_page[iter->cpu] =
800 trace_next_list(data, iter->next_page[iter->cpu]);
801 }
802}
803
Ingo Molnar8c523a92008-05-12 21:20:46 +0200804static notrace void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200805{
806 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
807
808 data->trace_tail_idx++;
809 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
810 data->trace_tail = trace_next_page(data, data->trace_tail);
811 data->trace_tail_idx = 0;
812 }
813
814 /* Check if we empty it, then reset the index */
815 if (data->trace_head == data->trace_tail &&
816 data->trace_head_idx == data->trace_tail_idx)
817 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200818}
819
Ingo Molnar8c523a92008-05-12 21:20:46 +0200820static notrace void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200821{
822 struct trace_entry *next;
823 int next_cpu = -1;
824
825 next = find_next_entry(iter, &next_cpu);
826
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200827 iter->prev_ent = iter->ent;
828 iter->prev_cpu = iter->cpu;
829
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200830 iter->ent = next;
831 iter->cpu = next_cpu;
832
Steven Rostedtb3806b42008-05-12 21:20:46 +0200833 if (next)
834 trace_iterator_increment(iter);
835
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200836 return next ? iter : NULL;
837}
838
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200839static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200840{
841 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200842 void *last_ent = iter->ent;
843 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200844 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200845
846 (*pos)++;
847
848 /* can't go backwards */
849 if (iter->idx > i)
850 return NULL;
851
852 if (iter->idx < 0)
853 ent = find_next_entry_inc(iter);
854 else
855 ent = iter;
856
857 while (ent && iter->idx < i)
858 ent = find_next_entry_inc(iter);
859
860 iter->pos = *pos;
861
862 if (last_ent && !ent)
863 seq_puts(m, "\n\nvim:ft=help\n");
864
865 return ent;
866}
867
868static void *s_start(struct seq_file *m, loff_t *pos)
869{
870 struct trace_iterator *iter = m->private;
871 void *p = NULL;
872 loff_t l = 0;
873 int i;
874
875 mutex_lock(&trace_types_lock);
876
877 if (!current_trace || current_trace != iter->trace)
878 return NULL;
879
880 atomic_inc(&trace_record_cmdline_disabled);
881
882 /* let the tracer grab locks here if needed */
883 if (current_trace->start)
884 current_trace->start(iter);
885
886 if (*pos != iter->pos) {
887 iter->ent = NULL;
888 iter->cpu = 0;
889 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200890 iter->prev_ent = NULL;
891 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200892
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200893 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200894 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200895 iter->next_page[i] = NULL;
896 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200897
898 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
899 ;
900
901 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200902 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200903 p = s_next(m, p, &l);
904 }
905
906 return p;
907}
908
909static void s_stop(struct seq_file *m, void *p)
910{
911 struct trace_iterator *iter = m->private;
912
913 atomic_dec(&trace_record_cmdline_disabled);
914
915 /* let the tracer release locks here if needed */
916 if (current_trace && current_trace == iter->trace && iter->trace->stop)
917 iter->trace->stop(iter);
918
919 mutex_unlock(&trace_types_lock);
920}
921
Steven Rostedtb3806b42008-05-12 21:20:46 +0200922static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200923seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200924{
925#ifdef CONFIG_KALLSYMS
926 char str[KSYM_SYMBOL_LEN];
927
928 kallsyms_lookup(address, NULL, NULL, NULL, str);
929
Steven Rostedtb3806b42008-05-12 21:20:46 +0200930 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200931#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200932 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200933}
934
Steven Rostedtb3806b42008-05-12 21:20:46 +0200935static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200936seq_print_sym_offset(struct trace_seq *s, const char *fmt,
937 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200938{
939#ifdef CONFIG_KALLSYMS
940 char str[KSYM_SYMBOL_LEN];
941
942 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200943 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200944#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200945 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200946}
947
948#ifndef CONFIG_64BIT
949# define IP_FMT "%08lx"
950#else
951# define IP_FMT "%016lx"
952#endif
953
Steven Rostedtb3806b42008-05-12 21:20:46 +0200954static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +0200955seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200956{
Steven Rostedtb3806b42008-05-12 21:20:46 +0200957 int ret;
958
959 if (!ip)
960 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200961
962 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200963 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200964 else
Steven Rostedtb3806b42008-05-12 21:20:46 +0200965 ret = seq_print_sym_short(s, "%s", ip);
966
967 if (!ret)
968 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200969
970 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200971 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
972 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200973}
974
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200975static notrace void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200976{
977 seq_puts(m, "# _------=> CPU# \n");
978 seq_puts(m, "# / _-----=> irqs-off \n");
979 seq_puts(m, "# | / _----=> need-resched \n");
980 seq_puts(m, "# || / _---=> hardirq/softirq \n");
981 seq_puts(m, "# ||| / _--=> preempt-depth \n");
982 seq_puts(m, "# |||| / \n");
983 seq_puts(m, "# ||||| delay \n");
984 seq_puts(m, "# cmd pid ||||| time | caller \n");
985 seq_puts(m, "# \\ / ||||| \\ | / \n");
986}
987
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200988static notrace void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200989{
990 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
991 seq_puts(m, "# | | | | |\n");
992}
993
994
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200995static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200996print_trace_header(struct seq_file *m, struct trace_iterator *iter)
997{
998 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
999 struct trace_array *tr = iter->tr;
1000 struct trace_array_cpu *data = tr->data[tr->cpu];
1001 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001002 unsigned long total = 0;
1003 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001004 int cpu;
1005 const char *name = "preemption";
1006
1007 if (type)
1008 name = type->name;
1009
1010 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001011 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001012 total += tr->data[cpu]->trace_idx;
1013 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001014 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001015 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016 entries += tr->data[cpu]->trace_idx;
1017 }
1018 }
1019
1020 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1021 name, UTS_RELEASE);
1022 seq_puts(m, "-----------------------------------"
1023 "---------------------------------\n");
1024 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1025 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001026 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001027 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001028 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001029 tr->cpu,
1030#if defined(CONFIG_PREEMPT_NONE)
1031 "server",
1032#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1033 "desktop",
1034#elif defined(CONFIG_PREEMPT_DESKTOP)
1035 "preempt",
1036#else
1037 "unknown",
1038#endif
1039 /* These are reserved for later use */
1040 0, 0, 0, 0);
1041#ifdef CONFIG_SMP
1042 seq_printf(m, " #P:%d)\n", num_online_cpus());
1043#else
1044 seq_puts(m, ")\n");
1045#endif
1046 seq_puts(m, " -----------------\n");
1047 seq_printf(m, " | task: %.16s-%d "
1048 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1049 data->comm, data->pid, data->uid, data->nice,
1050 data->policy, data->rt_priority);
1051 seq_puts(m, " -----------------\n");
1052
1053 if (data->critical_start) {
1054 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001055 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1056 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001057 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001058 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1059 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001060 seq_puts(m, "\n");
1061 }
1062
1063 seq_puts(m, "\n");
1064}
1065
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001066static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001067lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068{
1069 int hardirq, softirq;
1070 char *comm;
1071
1072 comm = trace_find_cmdline(entry->pid);
1073
Steven Rostedt214023c2008-05-12 21:20:46 +02001074 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1075 trace_seq_printf(s, "%d", cpu);
1076 trace_seq_printf(s, "%c%c",
1077 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1078 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001079
1080 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1081 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1082 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001083 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001084 else {
1085 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001086 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001087 else {
1088 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001089 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001090 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001091 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001092 }
1093 }
1094
1095 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001096 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001097 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001098 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099}
1100
1101unsigned long preempt_mark_thresh = 100;
1102
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001103static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001104lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105 unsigned long rel_usecs)
1106{
Steven Rostedt214023c2008-05-12 21:20:46 +02001107 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001108 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001109 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001110 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001111 trace_seq_puts(s, "+: ");
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
1116static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1117
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001118static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +02001119print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120{
Steven Rostedt214023c2008-05-12 21:20:46 +02001121 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001122 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1123 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1124 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1125 struct trace_entry *entry = iter->ent;
1126 unsigned long abs_usecs;
1127 unsigned long rel_usecs;
1128 char *comm;
1129 int S;
1130
1131 if (!next_entry)
1132 next_entry = entry;
1133 rel_usecs = ns2usecs(next_entry->t - entry->t);
1134 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1135
1136 if (verbose) {
1137 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001138 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1139 " %ld.%03ldms (+%ld.%03ldms): ",
1140 comm,
1141 entry->pid, cpu, entry->flags,
1142 entry->preempt_count, trace_idx,
1143 ns2usecs(entry->t),
1144 abs_usecs/1000,
1145 abs_usecs % 1000, rel_usecs/1000,
1146 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001147 } else {
Steven Rostedt214023c2008-05-12 21:20:46 +02001148 lat_print_generic(s, entry, cpu);
1149 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001150 }
1151 switch (entry->type) {
1152 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001153 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1154 trace_seq_puts(s, " (");
1155 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1156 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001157 break;
1158 case TRACE_CTX:
1159 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1160 state_to_char[entry->ctx.prev_state] : 'X';
1161 comm = trace_find_cmdline(entry->ctx.next_pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001162 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1163 entry->ctx.prev_pid,
1164 entry->ctx.prev_prio,
1165 S,
1166 entry->ctx.next_pid,
1167 entry->ctx.next_prio,
1168 comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001169 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001170 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001171 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001173 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174}
1175
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001176static notrace int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177{
Steven Rostedt214023c2008-05-12 21:20:46 +02001178 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001179 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001180 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001181 unsigned long usec_rem;
1182 unsigned long long t;
1183 unsigned long secs;
1184 char *comm;
1185 int S;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001186 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001187
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001188 entry = iter->ent;
1189
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001190 comm = trace_find_cmdline(iter->ent->pid);
1191
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001192 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001193 usec_rem = do_div(t, 1000000ULL);
1194 secs = (unsigned long)t;
1195
Steven Rostedtb3806b42008-05-12 21:20:46 +02001196 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1197 if (!ret)
1198 return 0;
1199 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1200 if (!ret)
1201 return 0;
1202 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1203 if (!ret)
1204 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001205
1206 switch (entry->type) {
1207 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001208 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1209 if (!ret)
1210 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001211 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1212 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001213 ret = trace_seq_printf(s, " <-");
1214 if (!ret)
1215 return 0;
1216 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1217 sym_flags);
1218 if (!ret)
1219 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001220 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001221 ret = trace_seq_printf(s, "\n");
1222 if (!ret)
1223 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001224 break;
1225 case TRACE_CTX:
1226 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1227 state_to_char[entry->ctx.prev_state] : 'X';
Steven Rostedtb3806b42008-05-12 21:20:46 +02001228 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1229 entry->ctx.prev_pid,
1230 entry->ctx.prev_prio,
1231 S,
1232 entry->ctx.next_pid,
1233 entry->ctx.next_prio);
1234 if (!ret)
1235 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001236 break;
1237 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001238 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001239}
1240
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001241static notrace int print_raw_fmt(struct trace_iterator *iter)
1242{
1243 struct trace_seq *s = &iter->seq;
1244 struct trace_entry *entry;
1245 int ret;
1246 int S;
1247
1248 entry = iter->ent;
1249
1250 ret = trace_seq_printf(s, "%d %d %llu ",
1251 entry->pid, iter->cpu, entry->t);
1252 if (!ret)
1253 return 0;
1254
1255 switch (entry->type) {
1256 case TRACE_FN:
1257 ret = trace_seq_printf(s, "%x %x\n",
1258 entry->fn.ip, entry->fn.parent_ip);
1259 if (!ret)
1260 return 0;
1261 break;
1262 case TRACE_CTX:
1263 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1264 state_to_char[entry->ctx.prev_state] : 'X';
1265 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1266 entry->ctx.prev_pid,
1267 entry->ctx.prev_prio,
1268 S,
1269 entry->ctx.next_pid,
1270 entry->ctx.next_prio);
1271 if (!ret)
1272 return 0;
1273 break;
1274 }
1275 return 1;
1276}
1277
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001278#define SEQ_PUT_FIELD_RET(s, x) \
1279do { \
1280 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1281 return 0; \
1282} while (0)
1283
1284static notrace int print_bin_fmt(struct trace_iterator *iter)
1285{
1286 struct trace_seq *s = &iter->seq;
1287 struct trace_entry *entry;
1288
1289 entry = iter->ent;
1290
1291 SEQ_PUT_FIELD_RET(s, entry->pid);
1292 SEQ_PUT_FIELD_RET(s, entry->cpu);
1293 SEQ_PUT_FIELD_RET(s, entry->t);
1294
1295 switch (entry->type) {
1296 case TRACE_FN:
1297 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1298 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1299 break;
1300 case TRACE_CTX:
1301 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1302 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1303 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1304 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1305 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1306 break;
1307 }
1308 return 1;
1309}
1310
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001311static int trace_empty(struct trace_iterator *iter)
1312{
1313 struct trace_array_cpu *data;
1314 int cpu;
1315
1316 for_each_possible_cpu(cpu) {
1317 data = iter->tr->data[cpu];
1318
Steven Rostedtb3806b42008-05-12 21:20:46 +02001319 if (head_page(data) && data->trace_idx &&
1320 (data->trace_tail != data->trace_head ||
1321 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322 return 0;
1323 }
1324 return 1;
1325}
1326
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001327static int print_trace_line(struct trace_iterator *iter)
1328{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001329 if (trace_flags & TRACE_ITER_BIN)
1330 return print_bin_fmt(iter);
1331
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001332 if (trace_flags & TRACE_ITER_RAW)
1333 return print_raw_fmt(iter);
1334
1335 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1336 return print_lat_fmt(iter, iter->idx, iter->cpu);
1337
1338 return print_trace_fmt(iter);
1339}
1340
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001341static int s_show(struct seq_file *m, void *v)
1342{
1343 struct trace_iterator *iter = v;
1344
1345 if (iter->ent == NULL) {
1346 if (iter->tr) {
1347 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1348 seq_puts(m, "#\n");
1349 }
1350 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1351 /* print nothing if the buffers are empty */
1352 if (trace_empty(iter))
1353 return 0;
1354 print_trace_header(m, iter);
1355 if (!(trace_flags & TRACE_ITER_VERBOSE))
1356 print_lat_help_header(m);
1357 } else {
1358 if (!(trace_flags & TRACE_ITER_VERBOSE))
1359 print_func_help_header(m);
1360 }
1361 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001362 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001363 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001364 }
1365
1366 return 0;
1367}
1368
1369static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001370 .start = s_start,
1371 .next = s_next,
1372 .stop = s_stop,
1373 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001374};
1375
1376static struct trace_iterator notrace *
1377__tracing_open(struct inode *inode, struct file *file, int *ret)
1378{
1379 struct trace_iterator *iter;
1380
Steven Rostedt60a11772008-05-12 21:20:44 +02001381 if (tracing_disabled) {
1382 *ret = -ENODEV;
1383 return NULL;
1384 }
1385
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1387 if (!iter) {
1388 *ret = -ENOMEM;
1389 goto out;
1390 }
1391
1392 mutex_lock(&trace_types_lock);
1393 if (current_trace && current_trace->print_max)
1394 iter->tr = &max_tr;
1395 else
1396 iter->tr = inode->i_private;
1397 iter->trace = current_trace;
1398 iter->pos = -1;
1399
1400 /* TODO stop tracer */
1401 *ret = seq_open(file, &tracer_seq_ops);
1402 if (!*ret) {
1403 struct seq_file *m = file->private_data;
1404 m->private = iter;
1405
1406 /* stop the trace while dumping */
1407 if (iter->tr->ctrl)
1408 tracer_enabled = 0;
1409
1410 if (iter->trace && iter->trace->open)
1411 iter->trace->open(iter);
1412 } else {
1413 kfree(iter);
1414 iter = NULL;
1415 }
1416 mutex_unlock(&trace_types_lock);
1417
1418 out:
1419 return iter;
1420}
1421
1422int tracing_open_generic(struct inode *inode, struct file *filp)
1423{
Steven Rostedt60a11772008-05-12 21:20:44 +02001424 if (tracing_disabled)
1425 return -ENODEV;
1426
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427 filp->private_data = inode->i_private;
1428 return 0;
1429}
1430
1431int tracing_release(struct inode *inode, struct file *file)
1432{
1433 struct seq_file *m = (struct seq_file *)file->private_data;
1434 struct trace_iterator *iter = m->private;
1435
1436 mutex_lock(&trace_types_lock);
1437 if (iter->trace && iter->trace->close)
1438 iter->trace->close(iter);
1439
1440 /* reenable tracing if it was previously enabled */
1441 if (iter->tr->ctrl)
1442 tracer_enabled = 1;
1443 mutex_unlock(&trace_types_lock);
1444
1445 seq_release(inode, file);
1446 kfree(iter);
1447 return 0;
1448}
1449
1450static int tracing_open(struct inode *inode, struct file *file)
1451{
1452 int ret;
1453
1454 __tracing_open(inode, file, &ret);
1455
1456 return ret;
1457}
1458
1459static int tracing_lt_open(struct inode *inode, struct file *file)
1460{
1461 struct trace_iterator *iter;
1462 int ret;
1463
1464 iter = __tracing_open(inode, file, &ret);
1465
1466 if (!ret)
1467 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1468
1469 return ret;
1470}
1471
1472
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001473static notrace void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001474t_next(struct seq_file *m, void *v, loff_t *pos)
1475{
1476 struct tracer *t = m->private;
1477
1478 (*pos)++;
1479
1480 if (t)
1481 t = t->next;
1482
1483 m->private = t;
1484
1485 return t;
1486}
1487
1488static void *t_start(struct seq_file *m, loff_t *pos)
1489{
1490 struct tracer *t = m->private;
1491 loff_t l = 0;
1492
1493 mutex_lock(&trace_types_lock);
1494 for (; t && l < *pos; t = t_next(m, t, &l))
1495 ;
1496
1497 return t;
1498}
1499
1500static void t_stop(struct seq_file *m, void *p)
1501{
1502 mutex_unlock(&trace_types_lock);
1503}
1504
1505static int t_show(struct seq_file *m, void *v)
1506{
1507 struct tracer *t = v;
1508
1509 if (!t)
1510 return 0;
1511
1512 seq_printf(m, "%s", t->name);
1513 if (t->next)
1514 seq_putc(m, ' ');
1515 else
1516 seq_putc(m, '\n');
1517
1518 return 0;
1519}
1520
1521static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001522 .start = t_start,
1523 .next = t_next,
1524 .stop = t_stop,
1525 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001526};
1527
1528static int show_traces_open(struct inode *inode, struct file *file)
1529{
1530 int ret;
1531
Steven Rostedt60a11772008-05-12 21:20:44 +02001532 if (tracing_disabled)
1533 return -ENODEV;
1534
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535 ret = seq_open(file, &show_traces_seq_ops);
1536 if (!ret) {
1537 struct seq_file *m = file->private_data;
1538 m->private = trace_types;
1539 }
1540
1541 return ret;
1542}
1543
1544static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001545 .open = tracing_open,
1546 .read = seq_read,
1547 .llseek = seq_lseek,
1548 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001549};
1550
1551static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001552 .open = tracing_lt_open,
1553 .read = seq_read,
1554 .llseek = seq_lseek,
1555 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001556};
1557
1558static struct file_operations show_traces_fops = {
1559 .open = show_traces_open,
1560 .read = seq_read,
1561 .release = seq_release,
1562};
1563
1564static ssize_t
1565tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1566 size_t cnt, loff_t *ppos)
1567{
1568 char *buf;
1569 int r = 0;
1570 int len = 0;
1571 int i;
1572
1573 /* calulate max size */
1574 for (i = 0; trace_options[i]; i++) {
1575 len += strlen(trace_options[i]);
1576 len += 3; /* "no" and space */
1577 }
1578
1579 /* +2 for \n and \0 */
1580 buf = kmalloc(len + 2, GFP_KERNEL);
1581 if (!buf)
1582 return -ENOMEM;
1583
1584 for (i = 0; trace_options[i]; i++) {
1585 if (trace_flags & (1 << i))
1586 r += sprintf(buf + r, "%s ", trace_options[i]);
1587 else
1588 r += sprintf(buf + r, "no%s ", trace_options[i]);
1589 }
1590
1591 r += sprintf(buf + r, "\n");
1592 WARN_ON(r >= len + 2);
1593
1594 r = simple_read_from_buffer(ubuf, cnt, ppos,
1595 buf, r);
1596
1597 kfree(buf);
1598
1599 return r;
1600}
1601
1602static ssize_t
1603tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1604 size_t cnt, loff_t *ppos)
1605{
1606 char buf[64];
1607 char *cmp = buf;
1608 int neg = 0;
1609 int i;
1610
1611 if (cnt > 63)
1612 cnt = 63;
1613
1614 if (copy_from_user(&buf, ubuf, cnt))
1615 return -EFAULT;
1616
1617 buf[cnt] = 0;
1618
1619 if (strncmp(buf, "no", 2) == 0) {
1620 neg = 1;
1621 cmp += 2;
1622 }
1623
1624 for (i = 0; trace_options[i]; i++) {
1625 int len = strlen(trace_options[i]);
1626
1627 if (strncmp(cmp, trace_options[i], len) == 0) {
1628 if (neg)
1629 trace_flags &= ~(1 << i);
1630 else
1631 trace_flags |= (1 << i);
1632 break;
1633 }
1634 }
1635
1636 filp->f_pos += cnt;
1637
1638 return cnt;
1639}
1640
1641static struct file_operations tracing_iter_fops = {
1642 .open = tracing_open_generic,
1643 .read = tracing_iter_ctrl_read,
1644 .write = tracing_iter_ctrl_write,
1645};
1646
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001647static const char readme_msg[] =
1648 "tracing mini-HOWTO:\n\n"
1649 "# mkdir /debug\n"
1650 "# mount -t debugfs nodev /debug\n\n"
1651 "# cat /debug/tracing/available_tracers\n"
1652 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1653 "# cat /debug/tracing/current_tracer\n"
1654 "none\n"
1655 "# echo sched_switch > /debug/tracing/current_tracer\n"
1656 "# cat /debug/tracing/current_tracer\n"
1657 "sched_switch\n"
1658 "# cat /debug/tracing/iter_ctrl\n"
1659 "noprint-parent nosym-offset nosym-addr noverbose\n"
1660 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1661 "# echo 1 > /debug/tracing/tracing_enabled\n"
1662 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1663 "echo 0 > /debug/tracing/tracing_enabled\n"
1664;
1665
1666static ssize_t
1667tracing_readme_read(struct file *filp, char __user *ubuf,
1668 size_t cnt, loff_t *ppos)
1669{
1670 return simple_read_from_buffer(ubuf, cnt, ppos,
1671 readme_msg, strlen(readme_msg));
1672}
1673
1674static struct file_operations tracing_readme_fops = {
1675 .open = tracing_open_generic,
1676 .read = tracing_readme_read,
1677};
1678
1679
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001680static ssize_t
1681tracing_ctrl_read(struct file *filp, char __user *ubuf,
1682 size_t cnt, loff_t *ppos)
1683{
1684 struct trace_array *tr = filp->private_data;
1685 char buf[64];
1686 int r;
1687
1688 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001689 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001690}
1691
1692static ssize_t
1693tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1694 size_t cnt, loff_t *ppos)
1695{
1696 struct trace_array *tr = filp->private_data;
1697 long val;
1698 char buf[64];
1699
1700 if (cnt > 63)
1701 cnt = 63;
1702
1703 if (copy_from_user(&buf, ubuf, cnt))
1704 return -EFAULT;
1705
1706 buf[cnt] = 0;
1707
1708 val = simple_strtoul(buf, NULL, 10);
1709
1710 val = !!val;
1711
1712 mutex_lock(&trace_types_lock);
1713 if (tr->ctrl ^ val) {
1714 if (val)
1715 tracer_enabled = 1;
1716 else
1717 tracer_enabled = 0;
1718
1719 tr->ctrl = val;
1720
1721 if (current_trace && current_trace->ctrl_update)
1722 current_trace->ctrl_update(tr);
1723 }
1724 mutex_unlock(&trace_types_lock);
1725
1726 filp->f_pos += cnt;
1727
1728 return cnt;
1729}
1730
1731static ssize_t
1732tracing_set_trace_read(struct file *filp, char __user *ubuf,
1733 size_t cnt, loff_t *ppos)
1734{
1735 char buf[max_tracer_type_len+2];
1736 int r;
1737
1738 mutex_lock(&trace_types_lock);
1739 if (current_trace)
1740 r = sprintf(buf, "%s\n", current_trace->name);
1741 else
1742 r = sprintf(buf, "\n");
1743 mutex_unlock(&trace_types_lock);
1744
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001745 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001746}
1747
1748static ssize_t
1749tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1750 size_t cnt, loff_t *ppos)
1751{
1752 struct trace_array *tr = &global_trace;
1753 struct tracer *t;
1754 char buf[max_tracer_type_len+1];
1755 int i;
1756
1757 if (cnt > max_tracer_type_len)
1758 cnt = max_tracer_type_len;
1759
1760 if (copy_from_user(&buf, ubuf, cnt))
1761 return -EFAULT;
1762
1763 buf[cnt] = 0;
1764
1765 /* strip ending whitespace. */
1766 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1767 buf[i] = 0;
1768
1769 mutex_lock(&trace_types_lock);
1770 for (t = trace_types; t; t = t->next) {
1771 if (strcmp(t->name, buf) == 0)
1772 break;
1773 }
1774 if (!t || t == current_trace)
1775 goto out;
1776
1777 if (current_trace && current_trace->reset)
1778 current_trace->reset(tr);
1779
1780 current_trace = t;
1781 if (t->init)
1782 t->init(tr);
1783
1784 out:
1785 mutex_unlock(&trace_types_lock);
1786
1787 filp->f_pos += cnt;
1788
1789 return cnt;
1790}
1791
1792static ssize_t
1793tracing_max_lat_read(struct file *filp, char __user *ubuf,
1794 size_t cnt, loff_t *ppos)
1795{
1796 unsigned long *ptr = filp->private_data;
1797 char buf[64];
1798 int r;
1799
1800 r = snprintf(buf, 64, "%ld\n",
1801 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1802 if (r > 64)
1803 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001804 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001805}
1806
1807static ssize_t
1808tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1809 size_t cnt, loff_t *ppos)
1810{
1811 long *ptr = filp->private_data;
1812 long val;
1813 char buf[64];
1814
1815 if (cnt > 63)
1816 cnt = 63;
1817
1818 if (copy_from_user(&buf, ubuf, cnt))
1819 return -EFAULT;
1820
1821 buf[cnt] = 0;
1822
1823 val = simple_strtoul(buf, NULL, 10);
1824
1825 *ptr = val * 1000;
1826
1827 return cnt;
1828}
1829
Steven Rostedtb3806b42008-05-12 21:20:46 +02001830static atomic_t tracing_reader;
1831
1832static int tracing_open_pipe(struct inode *inode, struct file *filp)
1833{
1834 struct trace_iterator *iter;
1835
1836 if (tracing_disabled)
1837 return -ENODEV;
1838
1839 /* We only allow for reader of the pipe */
1840 if (atomic_inc_return(&tracing_reader) != 1) {
1841 atomic_dec(&tracing_reader);
1842 return -EBUSY;
1843 }
1844
1845 /* create a buffer to store the information to pass to userspace */
1846 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1847 if (!iter)
1848 return -ENOMEM;
1849
1850 iter->tr = &global_trace;
1851
1852 filp->private_data = iter;
1853
1854 return 0;
1855}
1856
1857static int tracing_release_pipe(struct inode *inode, struct file *file)
1858{
1859 struct trace_iterator *iter = file->private_data;
1860
1861 kfree(iter);
1862 atomic_dec(&tracing_reader);
1863
1864 return 0;
1865}
1866
1867/*
1868 * Consumer reader.
1869 */
1870static ssize_t
1871tracing_read_pipe(struct file *filp, char __user *ubuf,
1872 size_t cnt, loff_t *ppos)
1873{
1874 struct trace_iterator *iter = filp->private_data;
1875 struct trace_array_cpu *data;
1876 static cpumask_t mask;
1877 struct trace_entry *entry;
1878 static int start;
1879 unsigned long flags;
1880 int read = 0;
1881 int cpu;
1882 int len;
1883 int ret;
1884
1885 /* return any leftover data */
1886 if (iter->seq.len > start) {
1887 len = iter->seq.len - start;
1888 if (cnt > len)
1889 cnt = len;
1890 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1891 if (ret)
1892 cnt = -EFAULT;
1893
1894 start += len;
1895
1896 return cnt;
1897 }
1898
1899 trace_seq_reset(&iter->seq);
1900 start = 0;
1901
1902 while (trace_empty(iter)) {
1903 /*
1904 * This is a make-shift waitqueue. The reason we don't use
1905 * an actual wait queue is because:
1906 * 1) we only ever have one waiter
1907 * 2) the tracing, traces all functions, we don't want
1908 * the overhead of calling wake_up and friends
1909 * (and tracing them too)
1910 * Anyway, this is really very primitive wakeup.
1911 */
1912 set_current_state(TASK_INTERRUPTIBLE);
1913 iter->tr->waiter = current;
1914
1915 /* sleep for one second, and try again. */
1916 schedule_timeout(HZ);
1917
1918 iter->tr->waiter = NULL;
1919
1920 if (signal_pending(current))
1921 return -EINTR;
1922
1923 /*
1924 * We block until we read something and tracing is disabled.
1925 * We still block if tracing is disabled, but we have never
1926 * read anything. This allows a user to cat this file, and
1927 * then enable tracing. But after we have read something,
1928 * we give an EOF when tracing is again disabled.
1929 *
1930 * iter->pos will be 0 if we haven't read anything.
1931 */
1932 if (!tracer_enabled && iter->pos)
1933 break;
1934
1935 continue;
1936 }
1937
1938 /* stop when tracing is finished */
1939 if (trace_empty(iter))
1940 return 0;
1941
1942 if (cnt >= PAGE_SIZE)
1943 cnt = PAGE_SIZE - 1;
1944
1945 memset(iter, 0, sizeof(*iter));
1946 iter->tr = &global_trace;
1947 iter->pos = -1;
1948
1949 /*
1950 * We need to stop all tracing on all CPUS to read the
1951 * the next buffer. This is a bit expensive, but is
1952 * not done often. We fill all what we can read,
1953 * and then release the locks again.
1954 */
1955
1956 cpus_clear(mask);
1957 local_irq_save(flags);
1958 for_each_possible_cpu(cpu) {
1959 data = iter->tr->data[cpu];
1960
1961 if (!head_page(data) || !data->trace_idx)
1962 continue;
1963
1964 atomic_inc(&data->disabled);
1965 spin_lock(&data->lock);
1966 cpu_set(cpu, mask);
1967 }
1968
Ingo Molnar8c523a92008-05-12 21:20:46 +02001969 while ((entry = find_next_entry_inc(iter)) != NULL) {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001970 ret = print_trace_line(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001971 if (!ret)
1972 break;
1973
1974 trace_consume(iter);
1975
1976 if (iter->seq.len >= cnt)
1977 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001978 }
1979
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02001980 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001981 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02001982 spin_unlock(&data->lock);
1983 atomic_dec(&data->disabled);
1984 }
1985 local_irq_restore(flags);
1986
1987 /* Now copy what we have to the user */
1988 read = iter->seq.len;
1989 if (read > cnt)
1990 read = cnt;
1991
1992 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1993
1994 if (read < iter->seq.len)
1995 start = read;
1996 else
1997 trace_seq_reset(&iter->seq);
1998
1999 if (ret)
2000 read = -EFAULT;
2001
2002 return read;
2003}
2004
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002005static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002006 .open = tracing_open_generic,
2007 .read = tracing_max_lat_read,
2008 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002009};
2010
2011static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002012 .open = tracing_open_generic,
2013 .read = tracing_ctrl_read,
2014 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002015};
2016
2017static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002018 .open = tracing_open_generic,
2019 .read = tracing_set_trace_read,
2020 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002021};
2022
Steven Rostedtb3806b42008-05-12 21:20:46 +02002023static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002024 .open = tracing_open_pipe,
2025 .read = tracing_read_pipe,
2026 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002027};
2028
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002029#ifdef CONFIG_DYNAMIC_FTRACE
2030
2031static ssize_t
2032tracing_read_long(struct file *filp, char __user *ubuf,
2033 size_t cnt, loff_t *ppos)
2034{
2035 unsigned long *p = filp->private_data;
2036 char buf[64];
2037 int r;
2038
2039 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002040
2041 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002042}
2043
2044static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002045 .open = tracing_open_generic,
2046 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002047};
2048#endif
2049
2050static struct dentry *d_tracer;
2051
2052struct dentry *tracing_init_dentry(void)
2053{
2054 static int once;
2055
2056 if (d_tracer)
2057 return d_tracer;
2058
2059 d_tracer = debugfs_create_dir("tracing", NULL);
2060
2061 if (!d_tracer && !once) {
2062 once = 1;
2063 pr_warning("Could not create debugfs directory 'tracing'\n");
2064 return NULL;
2065 }
2066
2067 return d_tracer;
2068}
2069
Steven Rostedt60a11772008-05-12 21:20:44 +02002070#ifdef CONFIG_FTRACE_SELFTEST
2071/* Let selftest have access to static functions in this file */
2072#include "trace_selftest.c"
2073#endif
2074
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002075static __init void tracer_init_debugfs(void)
2076{
2077 struct dentry *d_tracer;
2078 struct dentry *entry;
2079
2080 d_tracer = tracing_init_dentry();
2081
2082 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2083 &global_trace, &tracing_ctrl_fops);
2084 if (!entry)
2085 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2086
2087 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2088 NULL, &tracing_iter_fops);
2089 if (!entry)
2090 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2091
2092 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2093 &global_trace, &tracing_lt_fops);
2094 if (!entry)
2095 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2096
2097 entry = debugfs_create_file("trace", 0444, d_tracer,
2098 &global_trace, &tracing_fops);
2099 if (!entry)
2100 pr_warning("Could not create debugfs 'trace' entry\n");
2101
2102 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2103 &global_trace, &show_traces_fops);
2104 if (!entry)
2105 pr_warning("Could not create debugfs 'trace' entry\n");
2106
2107 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2108 &global_trace, &set_tracer_fops);
2109 if (!entry)
2110 pr_warning("Could not create debugfs 'trace' entry\n");
2111
2112 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2113 &tracing_max_latency,
2114 &tracing_max_lat_fops);
2115 if (!entry)
2116 pr_warning("Could not create debugfs "
2117 "'tracing_max_latency' entry\n");
2118
2119 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2120 &tracing_thresh, &tracing_max_lat_fops);
2121 if (!entry)
2122 pr_warning("Could not create debugfs "
2123 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002124 entry = debugfs_create_file("README", 0644, d_tracer,
2125 NULL, &tracing_readme_fops);
2126 if (!entry)
2127 pr_warning("Could not create debugfs 'README' entry\n");
2128
Steven Rostedtb3806b42008-05-12 21:20:46 +02002129 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2130 NULL, &tracing_pipe_fops);
2131 if (!entry)
2132 pr_warning("Could not create debugfs "
2133 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002134
2135#ifdef CONFIG_DYNAMIC_FTRACE
2136 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2137 &ftrace_update_tot_cnt,
2138 &tracing_read_long_fops);
2139 if (!entry)
2140 pr_warning("Could not create debugfs "
2141 "'dyn_ftrace_total_info' entry\n");
2142#endif
2143}
2144
2145/* dummy trace to disable tracing */
2146static struct tracer no_tracer __read_mostly =
2147{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002148 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002149};
2150
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002151static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002152{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002153 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002154 struct page *page, *tmp;
2155 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002156 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002157 int i;
2158
2159 /* first allocate a page for each CPU */
2160 for_each_possible_cpu(i) {
2161 array = (void *)__get_free_page(GFP_KERNEL);
2162 if (array == NULL) {
2163 printk(KERN_ERR "tracer: failed to allocate page"
2164 "for trace buffer!\n");
2165 goto free_pages;
2166 }
2167
2168 page = virt_to_page(array);
2169 list_add(&page->lru, &pages);
2170
2171/* Only allocate if we are actually using the max trace */
2172#ifdef CONFIG_TRACER_MAX_TRACE
2173 array = (void *)__get_free_page(GFP_KERNEL);
2174 if (array == NULL) {
2175 printk(KERN_ERR "tracer: failed to allocate page"
2176 "for trace buffer!\n");
2177 goto free_pages;
2178 }
2179 page = virt_to_page(array);
2180 list_add(&page->lru, &pages);
2181#endif
2182 }
2183
2184 /* Now that we successfully allocate a page per CPU, add them */
2185 for_each_possible_cpu(i) {
2186 data = global_trace.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002187 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002188 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002189 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002190 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002191 list_add_tail(&page->lru, &data->trace_pages);
2192 ClearPageLRU(page);
2193
2194#ifdef CONFIG_TRACER_MAX_TRACE
2195 data = max_tr.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002196 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002197 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002198 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002199 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002200 list_add_tail(&page->lru, &data->trace_pages);
2201 SetPageLRU(page);
2202#endif
2203 }
2204 global_trace.entries += ENTRIES_PER_PAGE;
2205
2206 return 0;
2207
2208 free_pages:
2209 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002210 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002211 __free_page(page);
2212 }
2213 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002214}
2215
2216__init static int tracer_alloc_buffers(void)
2217{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002218 struct trace_array_cpu *data;
2219 void *array;
2220 struct page *page;
2221 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002222 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002223 int i;
2224
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002225 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002226 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002227 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002228 max_tr.data[i] = &per_cpu(max_data, i);
2229
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002230 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002231 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002232 printk(KERN_ERR "tracer: failed to allocate page"
2233 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002234 goto free_buffers;
2235 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002236
2237 /* set the array to the list */
2238 INIT_LIST_HEAD(&data->trace_pages);
2239 page = virt_to_page(array);
2240 list_add(&page->lru, &data->trace_pages);
2241 /* use the LRU flag to differentiate the two buffers */
2242 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002243
2244/* Only allocate if we are actually using the max trace */
2245#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002246 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002247 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002248 printk(KERN_ERR "tracer: failed to allocate page"
2249 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002250 goto free_buffers;
2251 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002252
2253 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2254 page = virt_to_page(array);
2255 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2256 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002257#endif
2258 }
2259
2260 /*
2261 * Since we allocate by orders of pages, we may be able to
2262 * round up a bit.
2263 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002264 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002265 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002266
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002267 while (global_trace.entries < trace_nr_entries) {
2268 if (trace_alloc_page())
2269 break;
2270 pages++;
2271 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002272 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002273
2274 pr_info("tracer: %d pages allocated for %ld",
2275 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002276 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2277 pr_info(" actual entries %ld\n", global_trace.entries);
2278
2279 tracer_init_debugfs();
2280
2281 trace_init_cmdlines();
2282
2283 register_tracer(&no_tracer);
2284 current_trace = &no_tracer;
2285
Steven Rostedt60a11772008-05-12 21:20:44 +02002286 /* All seems OK, enable tracing */
2287 tracing_disabled = 0;
2288
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002289 return 0;
2290
2291 free_buffers:
2292 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002293 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002294 struct trace_array_cpu *data = global_trace.data[i];
2295
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002296 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002297 list_for_each_entry_safe(page, tmp,
2298 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002299 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002300 __free_page(page);
2301 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002302 }
2303
2304#ifdef CONFIG_TRACER_MAX_TRACE
2305 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002306 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002307 list_for_each_entry_safe(page, tmp,
2308 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002309 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002310 __free_page(page);
2311 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002312 }
2313#endif
2314 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002315 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002316}
Steven Rostedt60a11772008-05-12 21:20:44 +02002317fs_initcall(tracer_alloc_buffers);