2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #define _LARGEFILE64_SOURCE
29 #include <sys/types.h>
41 #include "trace-event.h"
51 static unsigned long page_size
;
53 static int read_or_die(void *data
, int size
)
57 r
= read(input_fd
, data
, size
);
59 die("reading input file (size expected=%d received=%d)",
64 static unsigned int read4(void)
68 read_or_die(&data
, 4);
69 return __data2host4(data
);
72 static unsigned long long read8(void)
74 unsigned long long data
;
76 read_or_die(&data
, 8);
77 return __data2host8(data
);
80 static char *read_string(void)
89 r
= read(input_fd
, buf
, BUFSIZ
);
91 die("reading input file");
96 for (i
= 0; i
< r
; i
++) {
105 str
= realloc(str
, size
);
107 die("malloc of size %d", size
);
108 memcpy(str
+ (size
- BUFSIZ
), buf
, BUFSIZ
);
111 str
= malloc_or_die(size
);
112 memcpy(str
, buf
, size
);
119 /* move the file descriptor to the end of the string */
120 r
= lseek(input_fd
, -(r
- i
), SEEK_CUR
);
126 str
= realloc(str
, size
);
128 die("malloc of size %d", size
);
129 memcpy(str
+ (size
- i
), buf
, i
);
132 str
= malloc_or_die(i
);
139 static void read_proc_kallsyms(void)
148 buf
= malloc_or_die(size
);
149 read_or_die(buf
, size
);
151 parse_proc_kallsyms(buf
, size
);
156 static void read_ftrace_printk(void)
165 buf
= malloc_or_die(size
);
166 read_or_die(buf
, size
);
168 parse_ftrace_printk(buf
, size
);
173 static void read_header_files(void)
175 unsigned long long size
;
180 read_or_die(buf
, 12);
182 if (memcmp(buf
, "header_page", 12) != 0)
183 die("did not read header page");
186 header_page
= malloc_or_die(size
);
187 read_or_die(header_page
, size
);
188 parse_header_page(header_page
, size
);
192 * The size field in the page is of type long,
193 * use that instead, since it represents the kernel.
195 long_size
= header_page_size_size
;
197 read_or_die(buf
, 13);
198 if (memcmp(buf
, "header_event", 13) != 0)
199 die("did not read header event");
202 header_event
= malloc_or_die(size
);
203 read_or_die(header_event
, size
);
207 static void read_ftrace_file(unsigned long long size
)
211 buf
= malloc_or_die(size
);
212 read_or_die(buf
, size
);
213 parse_ftrace_file(buf
, size
);
217 static void read_event_file(char *sys
, unsigned long long size
)
221 buf
= malloc_or_die(size
);
222 read_or_die(buf
, size
);
223 parse_event_file(buf
, size
, sys
);
227 static void read_ftrace_files(void)
229 unsigned long long size
;
235 for (i
= 0; i
< count
; i
++) {
237 read_ftrace_file(size
);
241 static void read_event_files(void)
243 unsigned long long size
;
251 for (i
= 0; i
< systems
; i
++) {
255 for (x
=0; x
< count
; x
++) {
257 read_event_file(sys
, size
);
263 unsigned long long offset
;
264 unsigned long long size
;
265 unsigned long long timestamp
;
273 static struct cpu_data
*cpu_data
;
275 static void update_cpu_data_index(int cpu
)
277 cpu_data
[cpu
].offset
+= page_size
;
278 cpu_data
[cpu
].size
-= page_size
;
279 cpu_data
[cpu
].index
= 0;
282 static void get_next_page(int cpu
)
287 if (!cpu_data
[cpu
].page
)
291 if (cpu_data
[cpu
].size
<= page_size
) {
292 free(cpu_data
[cpu
].page
);
293 cpu_data
[cpu
].page
= NULL
;
297 update_cpu_data_index(cpu
);
299 /* other parts of the code may expect the pointer to not move */
300 save_seek
= lseek64(input_fd
, 0, SEEK_CUR
);
302 ret
= lseek64(input_fd
, cpu_data
[cpu
].offset
, SEEK_SET
);
304 die("failed to lseek");
305 ret
= read(input_fd
, cpu_data
[cpu
].page
, page_size
);
307 die("failed to read page");
309 /* reset the file pointer back */
310 lseek64(input_fd
, save_seek
, SEEK_SET
);
315 munmap(cpu_data
[cpu
].page
, page_size
);
316 cpu_data
[cpu
].page
= NULL
;
318 if (cpu_data
[cpu
].size
<= page_size
)
321 update_cpu_data_index(cpu
);
323 cpu_data
[cpu
].page
= mmap(NULL
, page_size
, PROT_READ
, MAP_PRIVATE
,
324 input_fd
, cpu_data
[cpu
].offset
);
325 if (cpu_data
[cpu
].page
== MAP_FAILED
)
326 die("failed to mmap cpu %d at offset 0x%llx",
327 cpu
, cpu_data
[cpu
].offset
);
330 static unsigned int type_len4host(unsigned int type_len_ts
)
333 return (type_len_ts
>> 27) & ((1 << 5) - 1);
335 return type_len_ts
& ((1 << 5) - 1);
338 static unsigned int ts4host(unsigned int type_len_ts
)
341 return type_len_ts
& ((1 << 27) - 1);
343 return type_len_ts
>> 5;
346 static int calc_index(void *ptr
, int cpu
)
348 return (unsigned long)ptr
- (unsigned long)cpu_data
[cpu
].page
;
351 struct record
*trace_peek_data(int cpu
)
354 void *page
= cpu_data
[cpu
].page
;
355 int idx
= cpu_data
[cpu
].index
;
356 void *ptr
= page
+ idx
;
357 unsigned long long extend
;
358 unsigned int type_len_ts
;
359 unsigned int type_len
;
361 unsigned int length
= 0;
363 if (cpu_data
[cpu
].next
)
364 return cpu_data
[cpu
].next
;
370 /* FIXME: handle header page */
371 if (header_page_ts_size
!= 8)
372 die("expected a long long type for timestamp");
373 cpu_data
[cpu
].timestamp
= data2host8(ptr
);
375 switch (header_page_size_size
) {
377 cpu_data
[cpu
].page_size
= data2host4(ptr
);
381 cpu_data
[cpu
].page_size
= data2host8(ptr
);
385 die("bad long size");
387 ptr
= cpu_data
[cpu
].page
+ header_page_data_offset
;
391 idx
= calc_index(ptr
, cpu
);
393 if (idx
>= cpu_data
[cpu
].page_size
) {
395 return trace_peek_data(cpu
);
398 type_len_ts
= data2host4(ptr
);
401 type_len
= type_len4host(type_len_ts
);
402 delta
= ts4host(type_len_ts
);
405 case RINGBUF_TYPE_PADDING
:
407 die("error, hit unexpected end of page");
408 length
= data2host4(ptr
);
414 case RINGBUF_TYPE_TIME_EXTEND
:
415 extend
= data2host4(ptr
);
419 cpu_data
[cpu
].timestamp
+= extend
;
422 case RINGBUF_TYPE_TIME_STAMP
:
426 length
= data2host4(ptr
);
428 die("here! length=%d", length
);
431 length
= type_len
* 4;
435 cpu_data
[cpu
].timestamp
+= delta
;
437 data
= malloc_or_die(sizeof(*data
));
438 memset(data
, 0, sizeof(*data
));
440 data
->ts
= cpu_data
[cpu
].timestamp
;
445 cpu_data
[cpu
].index
= calc_index(ptr
, cpu
);
446 cpu_data
[cpu
].next
= data
;
451 struct record
*trace_read_data(int cpu
)
455 data
= trace_peek_data(cpu
);
456 cpu_data
[cpu
].next
= NULL
;
461 void trace_report(void)
463 const char *input_file
= "trace.info";
465 char test
[] = { 23, 8, 68 };
467 int show_version
= 0;
471 input_fd
= open(input_file
, O_RDONLY
);
473 die("opening '%s'\n", input_file
);
476 if (memcmp(buf
, test
, 3) != 0)
477 die("not an trace data file");
480 if (memcmp(buf
, "tracing", 7) != 0)
481 die("not a trace file (missing tracing)");
483 version
= read_string();
485 printf("version = %s\n", version
);
489 file_bigendian
= buf
[0];
490 host_bigendian
= bigendian();
501 read_proc_kallsyms();
502 read_ftrace_printk();