On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / tools / perf / builtin-trace.c
blobfb3f3c220211839c8eb79aaa6765749ef87ef806
1 #include "builtin.h"
3 #include "util/util.h"
4 #include "util/cache.h"
5 #include "util/symbol.h"
6 #include "util/thread.h"
7 #include "util/header.h"
9 #include "util/parse-options.h"
11 #include "perf.h"
12 #include "util/debug.h"
14 #include "util/trace-event.h"
15 #include "util/data_map.h"
17 static char const *input_name = "perf.data";
19 static unsigned long total = 0;
20 static unsigned long total_comm = 0;
22 static struct rb_root threads;
23 static struct thread *last_match;
25 static struct perf_header *header;
26 static u64 sample_type;
28 static char *cwd;
29 static int cwdlen;
32 static int
33 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
35 struct thread *thread;
37 thread = threads__findnew(event->comm.pid, &threads, &last_match);
39 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
40 (void *)(offset + head),
41 (void *)(long)(event->header.size),
42 event->comm.comm, event->comm.pid);
44 if (thread == NULL ||
45 thread__set_comm(thread, event->comm.comm)) {
46 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
47 return -1;
49 total_comm++;
51 return 0;
54 static int
55 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
57 struct thread *thread;
58 u64 ip = event->ip.ip;
59 u64 timestamp = -1;
60 u32 cpu = -1;
61 u64 period = 1;
62 void *more_data = event->ip.__more_data;
64 thread = threads__findnew(event->ip.pid, &threads, &last_match);
66 if (sample_type & PERF_SAMPLE_TIME) {
67 timestamp = *(u64 *)more_data;
68 more_data += sizeof(u64);
71 if (sample_type & PERF_SAMPLE_CPU) {
72 cpu = *(u32 *)more_data;
73 more_data += sizeof(u32);
74 more_data += sizeof(u32); /* reserved */
77 if (sample_type & PERF_SAMPLE_PERIOD) {
78 period = *(u64 *)more_data;
79 more_data += sizeof(u64);
82 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
83 (void *)(offset + head),
84 (void *)(long)(event->header.size),
85 event->header.misc,
86 event->ip.pid, event->ip.tid,
87 (void *)(long)ip,
88 (long long)period);
90 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
92 if (thread == NULL) {
93 eprintf("problem processing %d event, skipping it.\n",
94 event->header.type);
95 return -1;
98 if (sample_type & PERF_SAMPLE_RAW) {
99 struct {
100 u32 size;
101 char data[0];
102 } *raw = more_data;
105 * FIXME: better resolve from pid from the struct trace_entry
106 * field, although it should be the same than this perf
107 * event pid
109 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
111 total += period;
113 return 0;
116 static int sample_type_check(u64 type)
118 sample_type = type;
120 if (!(sample_type & PERF_SAMPLE_RAW)) {
121 fprintf(stderr,
122 "No trace sample to read. Did you call perf record "
123 "without -R?");
124 return -1;
127 return 0;
130 static struct perf_file_handler file_handler = {
131 .process_sample_event = process_sample_event,
132 .process_comm_event = process_comm_event,
133 .sample_type_check = sample_type_check,
136 static int __cmd_trace(void)
138 register_idle_thread(&threads, &last_match);
139 register_perf_file_handler(&file_handler);
141 return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
144 static const char * const annotate_usage[] = {
145 "perf trace [<options>] <command>",
146 NULL
149 static const struct option options[] = {
150 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
151 "dump raw trace in ASCII"),
152 OPT_BOOLEAN('v', "verbose", &verbose,
153 "be more verbose (show symbol address, etc)"),
154 OPT_END()
157 int cmd_trace(int argc, const char **argv, const char *prefix __used)
159 symbol__init();
161 argc = parse_options(argc, argv, options, annotate_usage, 0);
162 if (argc) {
164 * Special case: if there's an argument left then assume tha
165 * it's a symbol filter:
167 if (argc > 1)
168 usage_with_options(annotate_usage, options);
171 setup_pager();
173 return __cmd_trace();