]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace.c
ftrace: cleanups
[net-next-2.6.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
14#include <linux/utsrelease.h>
15#include <linux/kallsyms.h>
16#include <linux/seq_file.h>
17#include <linux/debugfs.h>
4c11d7ae 18#include <linux/pagemap.h>
bc0c38d1
SR
19#include <linux/hardirq.h>
20#include <linux/linkage.h>
21#include <linux/uaccess.h>
22#include <linux/ftrace.h>
23#include <linux/module.h>
24#include <linux/percpu.h>
25#include <linux/ctype.h>
26#include <linux/init.h>
27#include <linux/gfp.h>
28#include <linux/fs.h>
29
30#include "trace.h"
31
32unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33unsigned long __read_mostly tracing_thresh;
34
60a11774
SR
35static int tracing_disabled = 1;
36
bc0c38d1
SR
37static long notrace
38ns2usecs(cycle_t nsec)
39{
40 nsec += 500;
41 do_div(nsec, 1000);
42 return nsec;
43}
44
45static atomic_t tracer_counter;
46static struct trace_array global_trace;
47
48static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
49
50static struct trace_array max_tr;
51
52static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
53
54static int tracer_enabled;
4c11d7ae 55static unsigned long trace_nr_entries = 16384UL;
bc0c38d1
SR
56
57static struct tracer *trace_types __read_mostly;
58static struct tracer *current_trace __read_mostly;
59static int max_tracer_type_len;
60
61static DEFINE_MUTEX(trace_types_lock);
62
4c11d7ae
SR
63#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
64
bc0c38d1
SR
65static int __init set_nr_entries(char *str)
66{
67 if (!str)
68 return 0;
69 trace_nr_entries = simple_strtoul(str, &str, 0);
70 return 1;
71}
72__setup("trace_entries=", set_nr_entries);
73
57f50be1
SR
74unsigned long nsecs_to_usecs(unsigned long nsecs)
75{
76 return nsecs / 1000;
77}
78
bc0c38d1
SR
79enum trace_type {
80 __TRACE_FIRST_TYPE = 0,
81
82 TRACE_FN,
83 TRACE_CTX,
84
85 __TRACE_LAST_TYPE
86};
87
88enum trace_flag_type {
89 TRACE_FLAG_IRQS_OFF = 0x01,
90 TRACE_FLAG_NEED_RESCHED = 0x02,
91 TRACE_FLAG_HARDIRQ = 0x04,
92 TRACE_FLAG_SOFTIRQ = 0x08,
93};
94
95enum trace_iterator_flags {
96 TRACE_ITER_PRINT_PARENT = 0x01,
97 TRACE_ITER_SYM_OFFSET = 0x02,
98 TRACE_ITER_SYM_ADDR = 0x04,
99 TRACE_ITER_VERBOSE = 0x08,
100};
101
102#define TRACE_ITER_SYM_MASK \
103 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
104
105/* These must match the bit postions above */
106static const char *trace_options[] = {
107 "print-parent",
108 "sym-offset",
109 "sym-addr",
110 "verbose",
111 NULL
112};
113
114static unsigned trace_flags;
115
4c11d7ae 116static DEFINE_SPINLOCK(ftrace_max_lock);
bc0c38d1
SR
117
118/*
119 * Copy the new maximum trace into the separate maximum-trace
120 * structure. (this way the maximum trace is permanently saved,
121 * for later retrieval via /debugfs/tracing/latency_trace)
122 */
123static void notrace
124__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
125{
126 struct trace_array_cpu *data = tr->data[cpu];
127
128 max_tr.cpu = cpu;
129 max_tr.time_start = data->preempt_timestamp;
130
131 data = max_tr.data[cpu];
132 data->saved_latency = tracing_max_latency;
133
134 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
135 data->pid = tsk->pid;
136 data->uid = tsk->uid;
137 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
138 data->policy = tsk->policy;
139 data->rt_priority = tsk->rt_priority;
140
141 /* record this tasks comm */
142 tracing_record_cmdline(current);
143}
144
c7aafc54
IM
145void check_pages(struct trace_array_cpu *data)
146{
147 struct page *page, *tmp;
148
149 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
150 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
151
152 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
153 BUG_ON(page->lru.next->prev != &page->lru);
154 BUG_ON(page->lru.prev->next != &page->lru);
155 }
156}
157
158void *head_page(struct trace_array_cpu *data)
159{
160 struct page *page;
161
162 check_pages(data);
163 if (list_empty(&data->trace_pages))
164 return NULL;
165
166 page = list_entry(data->trace_pages.next, struct page, lru);
167 BUG_ON(&page->lru == &data->trace_pages);
168
169 return page_address(page);
170}
171
172notrace static void
173flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
174{
175 struct list_head flip_pages;
176
177 INIT_LIST_HEAD(&flip_pages);
178
179 tr1->trace_current = NULL;
180 memcpy(&tr1->trace_current_idx, &tr2->trace_current_idx,
181 sizeof(struct trace_array_cpu) -
182 offsetof(struct trace_array_cpu, trace_current_idx));
183
184 check_pages(tr1);
185 check_pages(tr2);
186 list_splice_init(&tr1->trace_pages, &flip_pages);
187 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
188 list_splice_init(&flip_pages, &tr2->trace_pages);
189 BUG_ON(!list_empty(&flip_pages));
190 check_pages(tr1);
191 check_pages(tr2);
192}
193
bc0c38d1
SR
194notrace void
195update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
196{
197 struct trace_array_cpu *data;
bc0c38d1
SR
198 int i;
199
4c11d7ae
SR
200 WARN_ON_ONCE(!irqs_disabled());
201 spin_lock(&ftrace_max_lock);
bc0c38d1
SR
202 /* clear out all the previous traces */
203 for_each_possible_cpu(i) {
204 data = tr->data[i];
c7aafc54 205 flip_trace(max_tr.data[i], data);
89b2f978 206 tracing_reset(data);
bc0c38d1
SR
207 }
208
209 __update_max_tr(tr, tsk, cpu);
4c11d7ae 210 spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
211}
212
213/**
214 * update_max_tr_single - only copy one trace over, and reset the rest
215 * @tr - tracer
216 * @tsk - task with the latency
217 * @cpu - the cpu of the buffer to copy.
218 */
219notrace void
220update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
221{
222 struct trace_array_cpu *data = tr->data[cpu];
bc0c38d1
SR
223 int i;
224
4c11d7ae
SR
225 WARN_ON_ONCE(!irqs_disabled());
226 spin_lock(&ftrace_max_lock);
bc0c38d1
SR
227 for_each_possible_cpu(i)
228 tracing_reset(max_tr.data[i]);
229
c7aafc54
IM
230 flip_trace(max_tr.data[cpu], data);
231
89b2f978 232 tracing_reset(data);
bc0c38d1
SR
233
234 __update_max_tr(tr, tsk, cpu);
4c11d7ae 235 spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
236}
237
238int register_tracer(struct tracer *type)
239{
240 struct tracer *t;
241 int len;
242 int ret = 0;
243
244 if (!type->name) {
245 pr_info("Tracer must have a name\n");
246 return -1;
247 }
248
249 mutex_lock(&trace_types_lock);
250 for (t = trace_types; t; t = t->next) {
251 if (strcmp(type->name, t->name) == 0) {
252 /* already found */
253 pr_info("Trace %s already registered\n",
254 type->name);
255 ret = -1;
256 goto out;
257 }
258 }
259
60a11774
SR
260#ifdef CONFIG_FTRACE_STARTUP_TEST
261 if (type->selftest) {
262 struct tracer *saved_tracer = current_trace;
263 struct trace_array_cpu *data;
264 struct trace_array *tr = &global_trace;
265 int saved_ctrl = tr->ctrl;
266 int i;
267 /*
268 * Run a selftest on this tracer.
269 * Here we reset the trace buffer, and set the current
270 * tracer to be this tracer. The tracer can then run some
271 * internal tracing to verify that everything is in order.
272 * If we fail, we do not register this tracer.
273 */
274 for_each_possible_cpu(i) {
60a11774 275 data = tr->data[i];
c7aafc54
IM
276 if (!head_page(data))
277 continue;
60a11774
SR
278 tracing_reset(data);
279 }
280 current_trace = type;
281 tr->ctrl = 0;
282 /* the test is responsible for initializing and enabling */
283 pr_info("Testing tracer %s: ", type->name);
284 ret = type->selftest(type, tr);
285 /* the test is responsible for resetting too */
286 current_trace = saved_tracer;
287 tr->ctrl = saved_ctrl;
288 if (ret) {
289 printk(KERN_CONT "FAILED!\n");
290 goto out;
291 }
292 printk(KERN_CONT "PASSED\n");
293 }
294#endif
295
bc0c38d1
SR
296 type->next = trace_types;
297 trace_types = type;
298 len = strlen(type->name);
299 if (len > max_tracer_type_len)
300 max_tracer_type_len = len;
60a11774 301
bc0c38d1
SR
302 out:
303 mutex_unlock(&trace_types_lock);
304
305 return ret;
306}
307
308void unregister_tracer(struct tracer *type)
309{
310 struct tracer **t;
311 int len;
312
313 mutex_lock(&trace_types_lock);
314 for (t = &trace_types; *t; t = &(*t)->next) {
315 if (*t == type)
316 goto found;
317 }
318 pr_info("Trace %s not registered\n", type->name);
319 goto out;
320
321 found:
322 *t = (*t)->next;
323 if (strlen(type->name) != max_tracer_type_len)
324 goto out;
325
326 max_tracer_type_len = 0;
327 for (t = &trace_types; *t; t = &(*t)->next) {
328 len = strlen((*t)->name);
329 if (len > max_tracer_type_len)
330 max_tracer_type_len = len;
331 }
332 out:
333 mutex_unlock(&trace_types_lock);
334}
335
336void notrace tracing_reset(struct trace_array_cpu *data)
337{
338 data->trace_idx = 0;
c7aafc54 339 data->trace_current = head_page(data);
4c11d7ae 340 data->trace_current_idx = 0;
bc0c38d1
SR
341}
342
343#ifdef CONFIG_FTRACE
344static void notrace
345function_trace_call(unsigned long ip, unsigned long parent_ip)
346{
347 struct trace_array *tr = &global_trace;
348 struct trace_array_cpu *data;
349 unsigned long flags;
350 long disabled;
351 int cpu;
352
353 if (unlikely(!tracer_enabled))
354 return;
355
18cef379 356 local_irq_save(flags);
bc0c38d1
SR
357 cpu = raw_smp_processor_id();
358 data = tr->data[cpu];
359 disabled = atomic_inc_return(&data->disabled);
360
361 if (likely(disabled == 1))
362 ftrace(tr, data, ip, parent_ip, flags);
363
364 atomic_dec(&data->disabled);
18cef379 365 local_irq_restore(flags);
bc0c38d1
SR
366}
367
368static struct ftrace_ops trace_ops __read_mostly =
369{
370 .func = function_trace_call,
371};
372#endif
373
374notrace void tracing_start_function_trace(void)
375{
376 register_ftrace_function(&trace_ops);
377}
378
379notrace void tracing_stop_function_trace(void)
380{
381 unregister_ftrace_function(&trace_ops);
382}
383
384#define SAVED_CMDLINES 128
385static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
386static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
387static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
388static int cmdline_idx;
389static DEFINE_SPINLOCK(trace_cmdline_lock);
390atomic_t trace_record_cmdline_disabled;
391
392static void trace_init_cmdlines(void)
393{
394 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
395 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
396 cmdline_idx = 0;
397}
398
399notrace void trace_stop_cmdline_recording(void);
400
401static void notrace trace_save_cmdline(struct task_struct *tsk)
402{
403 unsigned map;
404 unsigned idx;
405
406 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
407 return;
408
409 /*
410 * It's not the end of the world if we don't get
411 * the lock, but we also don't want to spin
412 * nor do we want to disable interrupts,
413 * so if we miss here, then better luck next time.
414 */
415 if (!spin_trylock(&trace_cmdline_lock))
416 return;
417
418 idx = map_pid_to_cmdline[tsk->pid];
419 if (idx >= SAVED_CMDLINES) {
420 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
421
422 map = map_cmdline_to_pid[idx];
423 if (map <= PID_MAX_DEFAULT)
424 map_pid_to_cmdline[map] = (unsigned)-1;
425
426 map_pid_to_cmdline[tsk->pid] = idx;
427
428 cmdline_idx = idx;
429 }
430
431 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
432
433 spin_unlock(&trace_cmdline_lock);
434}
435
436static notrace char *trace_find_cmdline(int pid)
437{
438 char *cmdline = "<...>";
439 unsigned map;
440
441 if (!pid)
442 return "<idle>";
443
444 if (pid > PID_MAX_DEFAULT)
445 goto out;
446
447 map = map_pid_to_cmdline[pid];
448 if (map >= SAVED_CMDLINES)
449 goto out;
450
451 cmdline = saved_cmdlines[map];
452
453 out:
454 return cmdline;
455}
456
457notrace void tracing_record_cmdline(struct task_struct *tsk)
458{
459 if (atomic_read(&trace_record_cmdline_disabled))
460 return;
461
462 trace_save_cmdline(tsk);
463}
464
465static inline notrace struct trace_entry *
c7aafc54 466tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
bc0c38d1
SR
467{
468 unsigned long idx, idx_next;
469 struct trace_entry *entry;
4c11d7ae 470 struct list_head *next;
c7aafc54 471 struct page *page;
bc0c38d1 472
4c11d7ae
SR
473 data->trace_idx++;
474 idx = data->trace_current_idx;
bc0c38d1
SR
475 idx_next = idx + 1;
476
c7aafc54
IM
477 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
478
4c11d7ae
SR
479 entry = data->trace_current + idx * TRACE_ENTRY_SIZE;
480
481 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
482 page = virt_to_page(data->trace_current);
c7aafc54
IM
483 /*
484 * Roundrobin - but skip the head (which is not a real page):
485 */
486 next = page->lru.next;
487 if (unlikely(next == &data->trace_pages))
488 next = next->next;
489 BUG_ON(next == &data->trace_pages);
490
4c11d7ae
SR
491 page = list_entry(next, struct page, lru);
492 data->trace_current = page_address(page);
bc0c38d1
SR
493 idx_next = 0;
494 }
495
4c11d7ae 496 data->trace_current_idx = idx_next;
bc0c38d1
SR
497
498 return entry;
499}
500
501static inline notrace void
c7aafc54 502tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
bc0c38d1
SR
503{
504 struct task_struct *tsk = current;
505 unsigned long pc;
506
507 pc = preempt_count();
508
c7aafc54
IM
509 entry->idx = atomic_inc_return(&tracer_counter);
510 entry->preempt_count = pc & 0xff;
511 entry->pid = tsk->pid;
512 entry->t = now(raw_smp_processor_id());
bc0c38d1
SR
513 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
514 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
515 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
516 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
517}
518
519notrace void
520ftrace(struct trace_array *tr, struct trace_array_cpu *data,
c7aafc54 521 unsigned long ip, unsigned long parent_ip, unsigned long flags)
bc0c38d1
SR
522{
523 struct trace_entry *entry;
524
c7aafc54 525 entry = tracing_get_trace_entry(tr, data);
bc0c38d1 526 tracing_generic_entry_update(entry, flags);
c7aafc54
IM
527 entry->type = TRACE_FN;
528 entry->fn.ip = ip;
529 entry->fn.parent_ip = parent_ip;
bc0c38d1
SR
530}
531
532notrace void
533tracing_sched_switch_trace(struct trace_array *tr,
534 struct trace_array_cpu *data,
535 struct task_struct *prev, struct task_struct *next,
536 unsigned long flags)
537{
538 struct trace_entry *entry;
539
c7aafc54 540 entry = tracing_get_trace_entry(tr, data);
bc0c38d1
SR
541 tracing_generic_entry_update(entry, flags);
542 entry->type = TRACE_CTX;
543 entry->ctx.prev_pid = prev->pid;
544 entry->ctx.prev_prio = prev->prio;
545 entry->ctx.prev_state = prev->state;
546 entry->ctx.next_pid = next->pid;
547 entry->ctx.next_prio = next->prio;
548}
549
550enum trace_file_type {
551 TRACE_FILE_LAT_FMT = 1,
552};
553
554static struct trace_entry *
4c11d7ae
SR
555trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
556 struct trace_iterator *iter, int cpu)
bc0c38d1 557{
4c11d7ae
SR
558 struct page *page;
559 struct trace_entry *array;
bc0c38d1 560
4c11d7ae
SR
561 if (iter->next_idx[cpu] >= tr->entries ||
562 iter->next_idx[cpu] >= data->trace_idx)
bc0c38d1
SR
563 return NULL;
564
4c11d7ae
SR
565 if (!iter->next_page[cpu]) {
566 /*
567 * Initialize. If the count of elements in
568 * this buffer is greater than the max entries
569 * we had an underrun. Which means we looped around.
570 * We can simply use the current pointer as our
571 * starting point.
572 */
573 if (data->trace_idx >= tr->entries) {
574 page = virt_to_page(data->trace_current);
575 iter->next_page[cpu] = &page->lru;
576 iter->next_page_idx[cpu] = data->trace_current_idx;
577 } else {
578 iter->next_page[cpu] = data->trace_pages.next;
579 iter->next_page_idx[cpu] = 0;
580 }
581 }
bc0c38d1 582
4c11d7ae 583 page = list_entry(iter->next_page[cpu], struct page, lru);
c7aafc54
IM
584 BUG_ON(&data->trace_pages == &page->lru);
585
4c11d7ae
SR
586 array = page_address(page);
587
588 return &array[iter->next_page_idx[cpu]];
bc0c38d1
SR
589}
590
591static struct notrace trace_entry *
592find_next_entry(struct trace_iterator *iter, int *ent_cpu)
593{
594 struct trace_array *tr = iter->tr;
595 struct trace_entry *ent, *next = NULL;
596 int next_cpu = -1;
597 int cpu;
598
599 for_each_possible_cpu(cpu) {
c7aafc54 600 if (!head_page(tr->data[cpu]))
bc0c38d1 601 continue;
4c11d7ae 602 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
bc0c38d1
SR
603 if (ent &&
604 (!next || (long)(next->idx - ent->idx) > 0)) {
605 next = ent;
606 next_cpu = cpu;
607 }
608 }
609
610 if (ent_cpu)
611 *ent_cpu = next_cpu;
612
613 return next;
614}
615
616static void *find_next_entry_inc(struct trace_iterator *iter)
617{
618 struct trace_entry *next;
619 int next_cpu = -1;
620
621 next = find_next_entry(iter, &next_cpu);
622
623 if (next) {
bc0c38d1 624 iter->idx++;
4c11d7ae
SR
625 iter->next_idx[next_cpu]++;
626 iter->next_page_idx[next_cpu]++;
627 if (iter->next_page_idx[next_cpu] >= ENTRIES_PER_PAGE) {
628 struct trace_array_cpu *data = iter->tr->data[next_cpu];
629
630 iter->next_page_idx[next_cpu] = 0;
631 iter->next_page[next_cpu] =
632 iter->next_page[next_cpu]->next;
633 if (iter->next_page[next_cpu] == &data->trace_pages)
634 iter->next_page[next_cpu] =
635 data->trace_pages.next;
636 }
bc0c38d1
SR
637 }
638 iter->ent = next;
639 iter->cpu = next_cpu;
640
641 return next ? iter : NULL;
642}
643
644static void notrace *
645s_next(struct seq_file *m, void *v, loff_t *pos)
646{
647 struct trace_iterator *iter = m->private;
648 void *ent;
649 void *last_ent = iter->ent;
650 int i = (int)*pos;
651
652 (*pos)++;
653
654 /* can't go backwards */
655 if (iter->idx > i)
656 return NULL;
657
658 if (iter->idx < 0)
659 ent = find_next_entry_inc(iter);
660 else
661 ent = iter;
662
663 while (ent && iter->idx < i)
664 ent = find_next_entry_inc(iter);
665
666 iter->pos = *pos;
667
668 if (last_ent && !ent)
669 seq_puts(m, "\n\nvim:ft=help\n");
670
671 return ent;
672}
673
674static void *s_start(struct seq_file *m, loff_t *pos)
675{
676 struct trace_iterator *iter = m->private;
677 void *p = NULL;
678 loff_t l = 0;
679 int i;
680
681 mutex_lock(&trace_types_lock);
682
683 if (!current_trace || current_trace != iter->trace)
684 return NULL;
685
686 atomic_inc(&trace_record_cmdline_disabled);
687
688 /* let the tracer grab locks here if needed */
689 if (current_trace->start)
690 current_trace->start(iter);
691
692 if (*pos != iter->pos) {
693 iter->ent = NULL;
694 iter->cpu = 0;
695 iter->idx = -1;
696
4c11d7ae 697 for_each_possible_cpu(i) {
bc0c38d1 698 iter->next_idx[i] = 0;
4c11d7ae
SR
699 iter->next_page[i] = NULL;
700 }
bc0c38d1
SR
701
702 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
703 ;
704
705 } else {
4c11d7ae 706 l = *pos - 1;
bc0c38d1
SR
707 p = s_next(m, p, &l);
708 }
709
710 return p;
711}
712
713static void s_stop(struct seq_file *m, void *p)
714{
715 struct trace_iterator *iter = m->private;
716
717 atomic_dec(&trace_record_cmdline_disabled);
718
719 /* let the tracer release locks here if needed */
720 if (current_trace && current_trace == iter->trace && iter->trace->stop)
721 iter->trace->stop(iter);
722
723 mutex_unlock(&trace_types_lock);
724}
725
726static void
727seq_print_sym_short(struct seq_file *m, const char *fmt, unsigned long address)
728{
729#ifdef CONFIG_KALLSYMS
730 char str[KSYM_SYMBOL_LEN];
731
732 kallsyms_lookup(address, NULL, NULL, NULL, str);
733
734 seq_printf(m, fmt, str);
735#endif
736}
737
738static void
739seq_print_sym_offset(struct seq_file *m, const char *fmt, unsigned long address)
740{
741#ifdef CONFIG_KALLSYMS
742 char str[KSYM_SYMBOL_LEN];
743
744 sprint_symbol(str, address);
745 seq_printf(m, fmt, str);
746#endif
747}
748
749#ifndef CONFIG_64BIT
750# define IP_FMT "%08lx"
751#else
752# define IP_FMT "%016lx"
753#endif
754
755static void notrace
756seq_print_ip_sym(struct seq_file *m, unsigned long ip, unsigned long sym_flags)
757{
758 if (!ip) {
759 seq_printf(m, "0");
760 return;
761 }
762
763 if (sym_flags & TRACE_ITER_SYM_OFFSET)
764 seq_print_sym_offset(m, "%s", ip);
765 else
766 seq_print_sym_short(m, "%s", ip);
767
768 if (sym_flags & TRACE_ITER_SYM_ADDR)
769 seq_printf(m, " <" IP_FMT ">", ip);
770}
771
772static void notrace print_lat_help_header(struct seq_file *m)
773{
774 seq_puts(m, "# _------=> CPU# \n");
775 seq_puts(m, "# / _-----=> irqs-off \n");
776 seq_puts(m, "# | / _----=> need-resched \n");
777 seq_puts(m, "# || / _---=> hardirq/softirq \n");
778 seq_puts(m, "# ||| / _--=> preempt-depth \n");
779 seq_puts(m, "# |||| / \n");
780 seq_puts(m, "# ||||| delay \n");
781 seq_puts(m, "# cmd pid ||||| time | caller \n");
782 seq_puts(m, "# \\ / ||||| \\ | / \n");
783}
784
785static void notrace print_func_help_header(struct seq_file *m)
786{
787 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
788 seq_puts(m, "# | | | | |\n");
789}
790
791
792static void notrace
793print_trace_header(struct seq_file *m, struct trace_iterator *iter)
794{
795 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
796 struct trace_array *tr = iter->tr;
797 struct trace_array_cpu *data = tr->data[tr->cpu];
798 struct tracer *type = current_trace;
4c11d7ae
SR
799 unsigned long total = 0;
800 unsigned long entries = 0;
bc0c38d1
SR
801 int cpu;
802 const char *name = "preemption";
803
804 if (type)
805 name = type->name;
806
807 for_each_possible_cpu(cpu) {
c7aafc54 808 if (head_page(tr->data[cpu])) {
4c11d7ae
SR
809 total += tr->data[cpu]->trace_idx;
810 if (tr->data[cpu]->trace_idx > tr->entries)
bc0c38d1 811 entries += tr->entries;
4c11d7ae 812 else
bc0c38d1
SR
813 entries += tr->data[cpu]->trace_idx;
814 }
815 }
816
817 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
818 name, UTS_RELEASE);
819 seq_puts(m, "-----------------------------------"
820 "---------------------------------\n");
821 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
822 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 823 nsecs_to_usecs(data->saved_latency),
bc0c38d1 824 entries,
4c11d7ae 825 total,
bc0c38d1
SR
826 tr->cpu,
827#if defined(CONFIG_PREEMPT_NONE)
828 "server",
829#elif defined(CONFIG_PREEMPT_VOLUNTARY)
830 "desktop",
831#elif defined(CONFIG_PREEMPT_DESKTOP)
832 "preempt",
833#else
834 "unknown",
835#endif
836 /* These are reserved for later use */
837 0, 0, 0, 0);
838#ifdef CONFIG_SMP
839 seq_printf(m, " #P:%d)\n", num_online_cpus());
840#else
841 seq_puts(m, ")\n");
842#endif
843 seq_puts(m, " -----------------\n");
844 seq_printf(m, " | task: %.16s-%d "
845 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
846 data->comm, data->pid, data->uid, data->nice,
847 data->policy, data->rt_priority);
848 seq_puts(m, " -----------------\n");
849
850 if (data->critical_start) {
851 seq_puts(m, " => started at: ");
852 seq_print_ip_sym(m, data->critical_start, sym_flags);
853 seq_puts(m, "\n => ended at: ");
854 seq_print_ip_sym(m, data->critical_end, sym_flags);
855 seq_puts(m, "\n");
856 }
857
858 seq_puts(m, "\n");
859}
860
bc0c38d1
SR
861static void notrace
862lat_print_generic(struct seq_file *m, struct trace_entry *entry, int cpu)
863{
864 int hardirq, softirq;
865 char *comm;
866
867 comm = trace_find_cmdline(entry->pid);
868
869 seq_printf(m, "%8.8s-%-5d ", comm, entry->pid);
870 seq_printf(m, "%d", cpu);
871 seq_printf(m, "%c%c",
872 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
873 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
874
875 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
876 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
877 if (hardirq && softirq)
878 seq_putc(m, 'H');
879 else {
880 if (hardirq)
881 seq_putc(m, 'h');
882 else {
883 if (softirq)
884 seq_putc(m, 's');
885 else
886 seq_putc(m, '.');
887 }
888 }
889
890 if (entry->preempt_count)
891 seq_printf(m, "%x", entry->preempt_count);
892 else
893 seq_puts(m, ".");
894}
895
896unsigned long preempt_mark_thresh = 100;
897
898static void notrace
899lat_print_timestamp(struct seq_file *m, unsigned long long abs_usecs,
900 unsigned long rel_usecs)
901{
902 seq_printf(m, " %4lldus", abs_usecs);
903 if (rel_usecs > preempt_mark_thresh)
904 seq_puts(m, "!: ");
905 else if (rel_usecs > 1)
906 seq_puts(m, "+: ");
907 else
908 seq_puts(m, " : ");
909}
910
911static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
912
913static void notrace
914print_lat_fmt(struct seq_file *m, struct trace_iterator *iter,
915 unsigned int trace_idx, int cpu)
916{
917 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
918 struct trace_entry *next_entry = find_next_entry(iter, NULL);
919 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
920 struct trace_entry *entry = iter->ent;
921 unsigned long abs_usecs;
922 unsigned long rel_usecs;
923 char *comm;
924 int S;
925
926 if (!next_entry)
927 next_entry = entry;
928 rel_usecs = ns2usecs(next_entry->t - entry->t);
929 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
930
931 if (verbose) {
932 comm = trace_find_cmdline(entry->pid);
933 seq_printf(m, "%16s %5d %d %d %08x %08x [%08lx]"
934 " %ld.%03ldms (+%ld.%03ldms): ",
935 comm,
936 entry->pid, cpu, entry->flags,
937 entry->preempt_count, trace_idx,
938 ns2usecs(entry->t),
939 abs_usecs/1000,
940 abs_usecs % 1000, rel_usecs/1000, rel_usecs % 1000);
941 } else {
942 lat_print_generic(m, entry, cpu);
943 lat_print_timestamp(m, abs_usecs, rel_usecs);
944 }
945 switch (entry->type) {
946 case TRACE_FN:
947 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
948 seq_puts(m, " (");
949 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
950 seq_puts(m, ")\n");
951 break;
952 case TRACE_CTX:
953 S = entry->ctx.prev_state < sizeof(state_to_char) ?
954 state_to_char[entry->ctx.prev_state] : 'X';
955 comm = trace_find_cmdline(entry->ctx.next_pid);
956 seq_printf(m, " %d:%d:%c --> %d:%d %s\n",
957 entry->ctx.prev_pid,
958 entry->ctx.prev_prio,
959 S,
960 entry->ctx.next_pid,
961 entry->ctx.next_prio,
962 comm);
963 break;
89b2f978
SR
964 default:
965 seq_printf(m, "Unknown type %d\n", entry->type);
bc0c38d1
SR
966 }
967}
968
969static void notrace
970print_trace_fmt(struct seq_file *m, struct trace_iterator *iter)
971{
972 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
973 struct trace_entry *entry = iter->ent;
974 unsigned long usec_rem;
975 unsigned long long t;
976 unsigned long secs;
977 char *comm;
978 int S;
979
980 comm = trace_find_cmdline(iter->ent->pid);
981
982 t = ns2usecs(entry->t);
983 usec_rem = do_div(t, 1000000ULL);
984 secs = (unsigned long)t;
985
986 seq_printf(m, "%16s-%-5d ", comm, entry->pid);
987 seq_printf(m, "[%02d] ", iter->cpu);
988 seq_printf(m, "%5lu.%06lu: ", secs, usec_rem);
989
990 switch (entry->type) {
991 case TRACE_FN:
992 seq_print_ip_sym(m, entry->fn.ip, sym_flags);
993 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
994 entry->fn.parent_ip) {
995 seq_printf(m, " <-");
996 seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
997 }
998 break;
999 case TRACE_CTX:
1000 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1001 state_to_char[entry->ctx.prev_state] : 'X';
1002 seq_printf(m, " %d:%d:%c ==> %d:%d\n",
1003 entry->ctx.prev_pid,
1004 entry->ctx.prev_prio,
1005 S,
1006 entry->ctx.next_pid,
1007 entry->ctx.next_prio);
1008 break;
1009 }
1010 seq_printf(m, "\n");
1011}
1012
1013static int trace_empty(struct trace_iterator *iter)
1014{
1015 struct trace_array_cpu *data;
1016 int cpu;
1017
1018 for_each_possible_cpu(cpu) {
1019 data = iter->tr->data[cpu];
1020
c7aafc54 1021 if (head_page(data) && data->trace_idx)
bc0c38d1
SR
1022 return 0;
1023 }
1024 return 1;
1025}
1026
1027static int s_show(struct seq_file *m, void *v)
1028{
1029 struct trace_iterator *iter = v;
1030
1031 if (iter->ent == NULL) {
1032 if (iter->tr) {
1033 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1034 seq_puts(m, "#\n");
1035 }
1036 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1037 /* print nothing if the buffers are empty */
1038 if (trace_empty(iter))
1039 return 0;
1040 print_trace_header(m, iter);
1041 if (!(trace_flags & TRACE_ITER_VERBOSE))
1042 print_lat_help_header(m);
1043 } else {
1044 if (!(trace_flags & TRACE_ITER_VERBOSE))
1045 print_func_help_header(m);
1046 }
1047 } else {
1048 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1049 print_lat_fmt(m, iter, iter->idx, iter->cpu);
1050 else
1051 print_trace_fmt(m, iter);
1052 }
1053
1054 return 0;
1055}
1056
1057static struct seq_operations tracer_seq_ops = {
1058 .start = s_start,
1059 .next = s_next,
1060 .stop = s_stop,
1061 .show = s_show,
1062};
1063
1064static struct trace_iterator notrace *
1065__tracing_open(struct inode *inode, struct file *file, int *ret)
1066{
1067 struct trace_iterator *iter;
1068
60a11774
SR
1069 if (tracing_disabled) {
1070 *ret = -ENODEV;
1071 return NULL;
1072 }
1073
bc0c38d1
SR
1074 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1075 if (!iter) {
1076 *ret = -ENOMEM;
1077 goto out;
1078 }
1079
1080 mutex_lock(&trace_types_lock);
1081 if (current_trace && current_trace->print_max)
1082 iter->tr = &max_tr;
1083 else
1084 iter->tr = inode->i_private;
1085 iter->trace = current_trace;
1086 iter->pos = -1;
1087
1088 /* TODO stop tracer */
1089 *ret = seq_open(file, &tracer_seq_ops);
1090 if (!*ret) {
1091 struct seq_file *m = file->private_data;
1092 m->private = iter;
1093
1094 /* stop the trace while dumping */
1095 if (iter->tr->ctrl)
1096 tracer_enabled = 0;
1097
1098 if (iter->trace && iter->trace->open)
1099 iter->trace->open(iter);
1100 } else {
1101 kfree(iter);
1102 iter = NULL;
1103 }
1104 mutex_unlock(&trace_types_lock);
1105
1106 out:
1107 return iter;
1108}
1109
1110int tracing_open_generic(struct inode *inode, struct file *filp)
1111{
60a11774
SR
1112 if (tracing_disabled)
1113 return -ENODEV;
1114
bc0c38d1
SR
1115 filp->private_data = inode->i_private;
1116 return 0;
1117}
1118
1119int tracing_release(struct inode *inode, struct file *file)
1120{
1121 struct seq_file *m = (struct seq_file *)file->private_data;
1122 struct trace_iterator *iter = m->private;
1123
1124 mutex_lock(&trace_types_lock);
1125 if (iter->trace && iter->trace->close)
1126 iter->trace->close(iter);
1127
1128 /* reenable tracing if it was previously enabled */
1129 if (iter->tr->ctrl)
1130 tracer_enabled = 1;
1131 mutex_unlock(&trace_types_lock);
1132
1133 seq_release(inode, file);
1134 kfree(iter);
1135 return 0;
1136}
1137
1138static int tracing_open(struct inode *inode, struct file *file)
1139{
1140 int ret;
1141
1142 __tracing_open(inode, file, &ret);
1143
1144 return ret;
1145}
1146
1147static int tracing_lt_open(struct inode *inode, struct file *file)
1148{
1149 struct trace_iterator *iter;
1150 int ret;
1151
1152 iter = __tracing_open(inode, file, &ret);
1153
1154 if (!ret)
1155 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1156
1157 return ret;
1158}
1159
1160
1161static void notrace *
1162t_next(struct seq_file *m, void *v, loff_t *pos)
1163{
1164 struct tracer *t = m->private;
1165
1166 (*pos)++;
1167
1168 if (t)
1169 t = t->next;
1170
1171 m->private = t;
1172
1173 return t;
1174}
1175
1176static void *t_start(struct seq_file *m, loff_t *pos)
1177{
1178 struct tracer *t = m->private;
1179 loff_t l = 0;
1180
1181 mutex_lock(&trace_types_lock);
1182 for (; t && l < *pos; t = t_next(m, t, &l))
1183 ;
1184
1185 return t;
1186}
1187
1188static void t_stop(struct seq_file *m, void *p)
1189{
1190 mutex_unlock(&trace_types_lock);
1191}
1192
1193static int t_show(struct seq_file *m, void *v)
1194{
1195 struct tracer *t = v;
1196
1197 if (!t)
1198 return 0;
1199
1200 seq_printf(m, "%s", t->name);
1201 if (t->next)
1202 seq_putc(m, ' ');
1203 else
1204 seq_putc(m, '\n');
1205
1206 return 0;
1207}
1208
1209static struct seq_operations show_traces_seq_ops = {
1210 .start = t_start,
1211 .next = t_next,
1212 .stop = t_stop,
1213 .show = t_show,
1214};
1215
1216static int show_traces_open(struct inode *inode, struct file *file)
1217{
1218 int ret;
1219
60a11774
SR
1220 if (tracing_disabled)
1221 return -ENODEV;
1222
bc0c38d1
SR
1223 ret = seq_open(file, &show_traces_seq_ops);
1224 if (!ret) {
1225 struct seq_file *m = file->private_data;
1226 m->private = trace_types;
1227 }
1228
1229 return ret;
1230}
1231
1232static struct file_operations tracing_fops = {
1233 .open = tracing_open,
1234 .read = seq_read,
1235 .llseek = seq_lseek,
1236 .release = tracing_release,
1237};
1238
1239static struct file_operations tracing_lt_fops = {
1240 .open = tracing_lt_open,
1241 .read = seq_read,
1242 .llseek = seq_lseek,
1243 .release = tracing_release,
1244};
1245
1246static struct file_operations show_traces_fops = {
1247 .open = show_traces_open,
1248 .read = seq_read,
1249 .release = seq_release,
1250};
1251
1252static ssize_t
1253tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1254 size_t cnt, loff_t *ppos)
1255{
1256 char *buf;
1257 int r = 0;
1258 int len = 0;
1259 int i;
1260
1261 /* calulate max size */
1262 for (i = 0; trace_options[i]; i++) {
1263 len += strlen(trace_options[i]);
1264 len += 3; /* "no" and space */
1265 }
1266
1267 /* +2 for \n and \0 */
1268 buf = kmalloc(len + 2, GFP_KERNEL);
1269 if (!buf)
1270 return -ENOMEM;
1271
1272 for (i = 0; trace_options[i]; i++) {
1273 if (trace_flags & (1 << i))
1274 r += sprintf(buf + r, "%s ", trace_options[i]);
1275 else
1276 r += sprintf(buf + r, "no%s ", trace_options[i]);
1277 }
1278
1279 r += sprintf(buf + r, "\n");
1280 WARN_ON(r >= len + 2);
1281
1282 r = simple_read_from_buffer(ubuf, cnt, ppos,
1283 buf, r);
1284
1285 kfree(buf);
1286
1287 return r;
1288}
1289
1290static ssize_t
1291tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1292 size_t cnt, loff_t *ppos)
1293{
1294 char buf[64];
1295 char *cmp = buf;
1296 int neg = 0;
1297 int i;
1298
1299 if (cnt > 63)
1300 cnt = 63;
1301
1302 if (copy_from_user(&buf, ubuf, cnt))
1303 return -EFAULT;
1304
1305 buf[cnt] = 0;
1306
1307 if (strncmp(buf, "no", 2) == 0) {
1308 neg = 1;
1309 cmp += 2;
1310 }
1311
1312 for (i = 0; trace_options[i]; i++) {
1313 int len = strlen(trace_options[i]);
1314
1315 if (strncmp(cmp, trace_options[i], len) == 0) {
1316 if (neg)
1317 trace_flags &= ~(1 << i);
1318 else
1319 trace_flags |= (1 << i);
1320 break;
1321 }
1322 }
1323
1324 filp->f_pos += cnt;
1325
1326 return cnt;
1327}
1328
1329static struct file_operations tracing_iter_fops = {
1330 .open = tracing_open_generic,
1331 .read = tracing_iter_ctrl_read,
1332 .write = tracing_iter_ctrl_write,
1333};
1334
1335static ssize_t
1336tracing_ctrl_read(struct file *filp, char __user *ubuf,
1337 size_t cnt, loff_t *ppos)
1338{
1339 struct trace_array *tr = filp->private_data;
1340 char buf[64];
1341 int r;
1342
1343 r = sprintf(buf, "%ld\n", tr->ctrl);
1344 return simple_read_from_buffer(ubuf, cnt, ppos,
1345 buf, r);
1346}
1347
1348static ssize_t
1349tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1350 size_t cnt, loff_t *ppos)
1351{
1352 struct trace_array *tr = filp->private_data;
1353 long val;
1354 char buf[64];
1355
1356 if (cnt > 63)
1357 cnt = 63;
1358
1359 if (copy_from_user(&buf, ubuf, cnt))
1360 return -EFAULT;
1361
1362 buf[cnt] = 0;
1363
1364 val = simple_strtoul(buf, NULL, 10);
1365
1366 val = !!val;
1367
1368 mutex_lock(&trace_types_lock);
1369 if (tr->ctrl ^ val) {
1370 if (val)
1371 tracer_enabled = 1;
1372 else
1373 tracer_enabled = 0;
1374
1375 tr->ctrl = val;
1376
1377 if (current_trace && current_trace->ctrl_update)
1378 current_trace->ctrl_update(tr);
1379 }
1380 mutex_unlock(&trace_types_lock);
1381
1382 filp->f_pos += cnt;
1383
1384 return cnt;
1385}
1386
1387static ssize_t
1388tracing_set_trace_read(struct file *filp, char __user *ubuf,
1389 size_t cnt, loff_t *ppos)
1390{
1391 char buf[max_tracer_type_len+2];
1392 int r;
1393
1394 mutex_lock(&trace_types_lock);
1395 if (current_trace)
1396 r = sprintf(buf, "%s\n", current_trace->name);
1397 else
1398 r = sprintf(buf, "\n");
1399 mutex_unlock(&trace_types_lock);
1400
1401 return simple_read_from_buffer(ubuf, cnt, ppos,
1402 buf, r);
1403}
1404
1405static ssize_t
1406tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1407 size_t cnt, loff_t *ppos)
1408{
1409 struct trace_array *tr = &global_trace;
1410 struct tracer *t;
1411 char buf[max_tracer_type_len+1];
1412 int i;
1413
1414 if (cnt > max_tracer_type_len)
1415 cnt = max_tracer_type_len;
1416
1417 if (copy_from_user(&buf, ubuf, cnt))
1418 return -EFAULT;
1419
1420 buf[cnt] = 0;
1421
1422 /* strip ending whitespace. */
1423 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1424 buf[i] = 0;
1425
1426 mutex_lock(&trace_types_lock);
1427 for (t = trace_types; t; t = t->next) {
1428 if (strcmp(t->name, buf) == 0)
1429 break;
1430 }
1431 if (!t || t == current_trace)
1432 goto out;
1433
1434 if (current_trace && current_trace->reset)
1435 current_trace->reset(tr);
1436
1437 current_trace = t;
1438 if (t->init)
1439 t->init(tr);
1440
1441 out:
1442 mutex_unlock(&trace_types_lock);
1443
1444 filp->f_pos += cnt;
1445
1446 return cnt;
1447}
1448
1449static ssize_t
1450tracing_max_lat_read(struct file *filp, char __user *ubuf,
1451 size_t cnt, loff_t *ppos)
1452{
1453 unsigned long *ptr = filp->private_data;
1454 char buf[64];
1455 int r;
1456
1457 r = snprintf(buf, 64, "%ld\n",
1458 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1459 if (r > 64)
1460 r = 64;
1461 return simple_read_from_buffer(ubuf, cnt, ppos,
1462 buf, r);
1463}
1464
1465static ssize_t
1466tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1467 size_t cnt, loff_t *ppos)
1468{
1469 long *ptr = filp->private_data;
1470 long val;
1471 char buf[64];
1472
1473 if (cnt > 63)
1474 cnt = 63;
1475
1476 if (copy_from_user(&buf, ubuf, cnt))
1477 return -EFAULT;
1478
1479 buf[cnt] = 0;
1480
1481 val = simple_strtoul(buf, NULL, 10);
1482
1483 *ptr = val * 1000;
1484
1485 return cnt;
1486}
1487
1488static struct file_operations tracing_max_lat_fops = {
1489 .open = tracing_open_generic,
1490 .read = tracing_max_lat_read,
1491 .write = tracing_max_lat_write,
1492};
1493
1494static struct file_operations tracing_ctrl_fops = {
1495 .open = tracing_open_generic,
1496 .read = tracing_ctrl_read,
1497 .write = tracing_ctrl_write,
1498};
1499
1500static struct file_operations set_tracer_fops = {
1501 .open = tracing_open_generic,
1502 .read = tracing_set_trace_read,
1503 .write = tracing_set_trace_write,
1504};
1505
1506#ifdef CONFIG_DYNAMIC_FTRACE
1507
1508static ssize_t
1509tracing_read_long(struct file *filp, char __user *ubuf,
1510 size_t cnt, loff_t *ppos)
1511{
1512 unsigned long *p = filp->private_data;
1513 char buf[64];
1514 int r;
1515
1516 r = sprintf(buf, "%ld\n", *p);
1517 return simple_read_from_buffer(ubuf, cnt, ppos,
1518 buf, r);
1519}
1520
1521static struct file_operations tracing_read_long_fops = {
1522 .open = tracing_open_generic,
1523 .read = tracing_read_long,
1524};
1525#endif
1526
1527static struct dentry *d_tracer;
1528
1529struct dentry *tracing_init_dentry(void)
1530{
1531 static int once;
1532
1533 if (d_tracer)
1534 return d_tracer;
1535
1536 d_tracer = debugfs_create_dir("tracing", NULL);
1537
1538 if (!d_tracer && !once) {
1539 once = 1;
1540 pr_warning("Could not create debugfs directory 'tracing'\n");
1541 return NULL;
1542 }
1543
1544 return d_tracer;
1545}
1546
60a11774
SR
1547#ifdef CONFIG_FTRACE_SELFTEST
1548/* Let selftest have access to static functions in this file */
1549#include "trace_selftest.c"
1550#endif
1551
bc0c38d1
SR
1552static __init void tracer_init_debugfs(void)
1553{
1554 struct dentry *d_tracer;
1555 struct dentry *entry;
1556
1557 d_tracer = tracing_init_dentry();
1558
1559 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1560 &global_trace, &tracing_ctrl_fops);
1561 if (!entry)
1562 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1563
1564 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
1565 NULL, &tracing_iter_fops);
1566 if (!entry)
1567 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
1568
1569 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
1570 &global_trace, &tracing_lt_fops);
1571 if (!entry)
1572 pr_warning("Could not create debugfs 'latency_trace' entry\n");
1573
1574 entry = debugfs_create_file("trace", 0444, d_tracer,
1575 &global_trace, &tracing_fops);
1576 if (!entry)
1577 pr_warning("Could not create debugfs 'trace' entry\n");
1578
1579 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
1580 &global_trace, &show_traces_fops);
1581 if (!entry)
1582 pr_warning("Could not create debugfs 'trace' entry\n");
1583
1584 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
1585 &global_trace, &set_tracer_fops);
1586 if (!entry)
1587 pr_warning("Could not create debugfs 'trace' entry\n");
1588
1589 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
1590 &tracing_max_latency,
1591 &tracing_max_lat_fops);
1592 if (!entry)
1593 pr_warning("Could not create debugfs "
1594 "'tracing_max_latency' entry\n");
1595
1596 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
1597 &tracing_thresh, &tracing_max_lat_fops);
1598 if (!entry)
1599 pr_warning("Could not create debugfs "
1600 "'tracing_threash' entry\n");
1601
1602#ifdef CONFIG_DYNAMIC_FTRACE
1603 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
1604 &ftrace_update_tot_cnt,
1605 &tracing_read_long_fops);
1606 if (!entry)
1607 pr_warning("Could not create debugfs "
1608 "'dyn_ftrace_total_info' entry\n");
1609#endif
1610}
1611
1612/* dummy trace to disable tracing */
1613static struct tracer no_tracer __read_mostly =
1614{
1615 .name = "none",
1616};
1617
4c11d7ae 1618static int trace_alloc_page(void)
bc0c38d1 1619{
4c11d7ae 1620 struct trace_array_cpu *data;
4c11d7ae
SR
1621 struct page *page, *tmp;
1622 LIST_HEAD(pages);
c7aafc54 1623 void *array;
4c11d7ae
SR
1624 int i;
1625
1626 /* first allocate a page for each CPU */
1627 for_each_possible_cpu(i) {
1628 array = (void *)__get_free_page(GFP_KERNEL);
1629 if (array == NULL) {
1630 printk(KERN_ERR "tracer: failed to allocate page"
1631 "for trace buffer!\n");
1632 goto free_pages;
1633 }
1634
1635 page = virt_to_page(array);
1636 list_add(&page->lru, &pages);
1637
1638/* Only allocate if we are actually using the max trace */
1639#ifdef CONFIG_TRACER_MAX_TRACE
1640 array = (void *)__get_free_page(GFP_KERNEL);
1641 if (array == NULL) {
1642 printk(KERN_ERR "tracer: failed to allocate page"
1643 "for trace buffer!\n");
1644 goto free_pages;
1645 }
1646 page = virt_to_page(array);
1647 list_add(&page->lru, &pages);
1648#endif
1649 }
1650
1651 /* Now that we successfully allocate a page per CPU, add them */
1652 for_each_possible_cpu(i) {
1653 data = global_trace.data[i];
1654 page = list_entry(pages.next, struct page, lru);
c7aafc54 1655 list_del_init(&page->lru);
4c11d7ae
SR
1656 list_add_tail(&page->lru, &data->trace_pages);
1657 ClearPageLRU(page);
1658
1659#ifdef CONFIG_TRACER_MAX_TRACE
1660 data = max_tr.data[i];
1661 page = list_entry(pages.next, struct page, lru);
c7aafc54 1662 list_del_init(&page->lru);
4c11d7ae
SR
1663 list_add_tail(&page->lru, &data->trace_pages);
1664 SetPageLRU(page);
1665#endif
1666 }
1667 global_trace.entries += ENTRIES_PER_PAGE;
1668
1669 return 0;
1670
1671 free_pages:
1672 list_for_each_entry_safe(page, tmp, &pages, lru) {
c7aafc54 1673 list_del_init(&page->lru);
4c11d7ae
SR
1674 __free_page(page);
1675 }
1676 return -ENOMEM;
bc0c38d1
SR
1677}
1678
1679__init static int tracer_alloc_buffers(void)
1680{
4c11d7ae
SR
1681 struct trace_array_cpu *data;
1682 void *array;
1683 struct page *page;
1684 int pages = 0;
60a11774 1685 int ret = -ENOMEM;
bc0c38d1
SR
1686 int i;
1687
4c11d7ae 1688 /* Allocate the first page for all buffers */
bc0c38d1 1689 for_each_possible_cpu(i) {
4c11d7ae 1690 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1
SR
1691 max_tr.data[i] = &per_cpu(max_data, i);
1692
4c11d7ae 1693 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 1694 if (array == NULL) {
4c11d7ae
SR
1695 printk(KERN_ERR "tracer: failed to allocate page"
1696 "for trace buffer!\n");
bc0c38d1
SR
1697 goto free_buffers;
1698 }
4c11d7ae
SR
1699
1700 /* set the array to the list */
1701 INIT_LIST_HEAD(&data->trace_pages);
1702 page = virt_to_page(array);
1703 list_add(&page->lru, &data->trace_pages);
1704 /* use the LRU flag to differentiate the two buffers */
1705 ClearPageLRU(page);
bc0c38d1
SR
1706
1707/* Only allocate if we are actually using the max trace */
1708#ifdef CONFIG_TRACER_MAX_TRACE
4c11d7ae 1709 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 1710 if (array == NULL) {
4c11d7ae
SR
1711 printk(KERN_ERR "tracer: failed to allocate page"
1712 "for trace buffer!\n");
bc0c38d1
SR
1713 goto free_buffers;
1714 }
4c11d7ae
SR
1715
1716 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
1717 page = virt_to_page(array);
1718 list_add(&page->lru, &max_tr.data[i]->trace_pages);
1719 SetPageLRU(page);
bc0c38d1
SR
1720#endif
1721 }
1722
1723 /*
1724 * Since we allocate by orders of pages, we may be able to
1725 * round up a bit.
1726 */
4c11d7ae 1727 global_trace.entries = ENTRIES_PER_PAGE;
4c11d7ae
SR
1728 pages++;
1729
1730 while (global_trace.entries < trace_nr_entries) {
1731 if (trace_alloc_page())
1732 break;
1733 pages++;
1734 }
89b2f978 1735 max_tr.entries = global_trace.entries;
bc0c38d1 1736
4c11d7ae
SR
1737 pr_info("tracer: %d pages allocated for %ld",
1738 pages, trace_nr_entries);
bc0c38d1
SR
1739 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
1740 pr_info(" actual entries %ld\n", global_trace.entries);
1741
1742 tracer_init_debugfs();
1743
1744 trace_init_cmdlines();
1745
1746 register_tracer(&no_tracer);
1747 current_trace = &no_tracer;
1748
60a11774
SR
1749 /* All seems OK, enable tracing */
1750 tracing_disabled = 0;
1751
bc0c38d1
SR
1752 return 0;
1753
1754 free_buffers:
1755 for (i-- ; i >= 0; i--) {
4c11d7ae 1756 struct page *page, *tmp;
bc0c38d1
SR
1757 struct trace_array_cpu *data = global_trace.data[i];
1758
c7aafc54 1759 if (data) {
4c11d7ae
SR
1760 list_for_each_entry_safe(page, tmp,
1761 &data->trace_pages, lru) {
c7aafc54 1762 list_del_init(&page->lru);
4c11d7ae
SR
1763 __free_page(page);
1764 }
bc0c38d1
SR
1765 }
1766
1767#ifdef CONFIG_TRACER_MAX_TRACE
1768 data = max_tr.data[i];
c7aafc54 1769 if (data) {
4c11d7ae
SR
1770 list_for_each_entry_safe(page, tmp,
1771 &data->trace_pages, lru) {
c7aafc54 1772 list_del_init(&page->lru);
4c11d7ae
SR
1773 __free_page(page);
1774 }
bc0c38d1
SR
1775 }
1776#endif
1777 }
60a11774 1778 return ret;
bc0c38d1
SR
1779}
1780
60a11774 1781fs_initcall(tracer_alloc_buffers);