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 _FILE_OFFSET_BITS 64
29 #include <sys/types.h>
40 #include "trace-event.h"
50 static unsigned long page_size
;
52 static ssize_t calc_data_size
;
55 static void *malloc_or_die(int size
)
65 static int do_read(int fd
, void *buf
, int size
)
70 int ret
= read(fd
, buf
, size
);
76 int retw
= write(STDOUT_FILENO
, buf
, ret
);
78 if (retw
<= 0 || retw
!= ret
)
79 die("repiping input file");
89 static int read_or_die(void *data
, int size
)
93 r
= do_read(input_fd
, data
, size
);
95 die("reading input file (size expected=%d received=%d)",
104 /* If it fails, the next read will report it */
105 static void skip(int size
)
111 r
= size
> BUFSIZ
? BUFSIZ
: size
;
117 static unsigned int read4(void)
121 read_or_die(&data
, 4);
122 return __data2host4(perf_pevent
, data
);
125 static unsigned long long read8(void)
127 unsigned long long data
;
129 read_or_die(&data
, 8);
130 return __data2host8(perf_pevent
, data
);
133 static char *read_string(void)
142 r
= read(input_fd
, &c
, 1);
144 die("reading input file");
150 int retw
= write(STDOUT_FILENO
, &c
, 1);
152 if (retw
<= 0 || retw
!= r
)
153 die("repiping input file string");
163 calc_data_size
+= size
;
165 str
= malloc_or_die(size
);
166 memcpy(str
, buf
, size
);
171 static void read_proc_kallsyms(void)
180 buf
= malloc_or_die(size
+ 1);
181 read_or_die(buf
, size
);
184 parse_proc_kallsyms(buf
, size
);
189 static void read_ftrace_printk(void)
198 buf
= malloc_or_die(size
);
199 read_or_die(buf
, size
);
201 parse_ftrace_printk(buf
, size
);
206 static void read_header_files(void)
208 unsigned long long size
;
212 read_or_die(buf
, 12);
214 if (memcmp(buf
, "header_page", 12) != 0)
215 die("did not read header page");
221 * The size field in the page is of type long,
222 * use that instead, since it represents the kernel.
224 long_size
= header_page_size_size
;
226 read_or_die(buf
, 13);
227 if (memcmp(buf
, "header_event", 13) != 0)
228 die("did not read header event");
231 header_event
= malloc_or_die(size
);
232 read_or_die(header_event
, size
);
236 static void read_ftrace_file(unsigned long long size
)
240 buf
= malloc_or_die(size
);
241 read_or_die(buf
, size
);
242 parse_ftrace_file(buf
, size
);
246 static void read_event_file(char *sys
, unsigned long long size
)
250 buf
= malloc_or_die(size
);
251 read_or_die(buf
, size
);
252 parse_event_file(buf
, size
, sys
);
256 static void read_ftrace_files(void)
258 unsigned long long size
;
264 for (i
= 0; i
< count
; i
++) {
266 read_ftrace_file(size
);
270 static void read_event_files(void)
272 unsigned long long size
;
280 for (i
= 0; i
< systems
; i
++) {
284 for (x
=0; x
< count
; x
++) {
286 read_event_file(sys
, size
);
292 unsigned long long offset
;
293 unsigned long long size
;
294 unsigned long long timestamp
;
295 struct pevent_record
*next
;
302 static struct cpu_data
*cpu_data
;
304 static void update_cpu_data_index(int cpu
)
306 cpu_data
[cpu
].offset
+= page_size
;
307 cpu_data
[cpu
].size
-= page_size
;
308 cpu_data
[cpu
].index
= 0;
311 static void get_next_page(int cpu
)
316 if (!cpu_data
[cpu
].page
)
320 if (cpu_data
[cpu
].size
<= page_size
) {
321 free(cpu_data
[cpu
].page
);
322 cpu_data
[cpu
].page
= NULL
;
326 update_cpu_data_index(cpu
);
328 /* other parts of the code may expect the pointer to not move */
329 save_seek
= lseek(input_fd
, 0, SEEK_CUR
);
331 ret
= lseek(input_fd
, cpu_data
[cpu
].offset
, SEEK_SET
);
332 if (ret
== (off_t
)-1)
333 die("failed to lseek");
334 ret
= read(input_fd
, cpu_data
[cpu
].page
, page_size
);
336 die("failed to read page");
338 /* reset the file pointer back */
339 lseek(input_fd
, save_seek
, SEEK_SET
);
344 munmap(cpu_data
[cpu
].page
, page_size
);
345 cpu_data
[cpu
].page
= NULL
;
347 if (cpu_data
[cpu
].size
<= page_size
)
350 update_cpu_data_index(cpu
);
352 cpu_data
[cpu
].page
= mmap(NULL
, page_size
, PROT_READ
, MAP_PRIVATE
,
353 input_fd
, cpu_data
[cpu
].offset
);
354 if (cpu_data
[cpu
].page
== MAP_FAILED
)
355 die("failed to mmap cpu %d at offset 0x%llx",
356 cpu
, cpu_data
[cpu
].offset
);
359 static unsigned int type_len4host(unsigned int type_len_ts
)
362 return (type_len_ts
>> 27) & ((1 << 5) - 1);
364 return type_len_ts
& ((1 << 5) - 1);
367 static unsigned int ts4host(unsigned int type_len_ts
)
370 return type_len_ts
& ((1 << 27) - 1);
372 return type_len_ts
>> 5;
375 static int calc_index(void *ptr
, int cpu
)
377 return (unsigned long)ptr
- (unsigned long)cpu_data
[cpu
].page
;
380 struct pevent_record
*trace_peek_data(int cpu
)
382 struct pevent_record
*data
;
383 void *page
= cpu_data
[cpu
].page
;
384 int idx
= cpu_data
[cpu
].index
;
385 void *ptr
= page
+ idx
;
386 unsigned long long extend
;
387 unsigned int type_len_ts
;
388 unsigned int type_len
;
390 unsigned int length
= 0;
392 if (cpu_data
[cpu
].next
)
393 return cpu_data
[cpu
].next
;
399 /* FIXME: handle header page */
400 if (header_page_ts_size
!= 8)
401 die("expected a long long type for timestamp");
402 cpu_data
[cpu
].timestamp
= data2host8(perf_pevent
, ptr
);
404 switch (header_page_size_size
) {
406 cpu_data
[cpu
].page_size
= data2host4(perf_pevent
, ptr
);
410 cpu_data
[cpu
].page_size
= data2host8(perf_pevent
, ptr
);
414 die("bad long size");
416 ptr
= cpu_data
[cpu
].page
+ header_page_data_offset
;
420 idx
= calc_index(ptr
, cpu
);
422 if (idx
>= cpu_data
[cpu
].page_size
) {
424 return trace_peek_data(cpu
);
427 type_len_ts
= data2host4(perf_pevent
, ptr
);
430 type_len
= type_len4host(type_len_ts
);
431 delta
= ts4host(type_len_ts
);
434 case RINGBUF_TYPE_PADDING
:
436 die("error, hit unexpected end of page");
437 length
= data2host4(perf_pevent
, ptr
);
443 case RINGBUF_TYPE_TIME_EXTEND
:
444 extend
= data2host4(perf_pevent
, ptr
);
448 cpu_data
[cpu
].timestamp
+= extend
;
451 case RINGBUF_TYPE_TIME_STAMP
:
455 length
= data2host4(perf_pevent
, ptr
);
457 die("here! length=%d", length
);
460 length
= type_len
* 4;
464 cpu_data
[cpu
].timestamp
+= delta
;
466 data
= malloc_or_die(sizeof(*data
));
467 memset(data
, 0, sizeof(*data
));
469 data
->ts
= cpu_data
[cpu
].timestamp
;
474 cpu_data
[cpu
].index
= calc_index(ptr
, cpu
);
475 cpu_data
[cpu
].next
= data
;
480 struct pevent_record
*trace_read_data(int cpu
)
482 struct pevent_record
*data
;
484 data
= trace_peek_data(cpu
);
485 cpu_data
[cpu
].next
= NULL
;
490 ssize_t
trace_report(int fd
, bool __repipe
)
493 char test
[] = { 23, 8, 68 };
495 int show_version
= 0;
506 if (memcmp(buf
, test
, 3) != 0)
507 die("no trace data in the file");
510 if (memcmp(buf
, "tracing", 7) != 0)
511 die("not a trace file (missing 'tracing' tag)");
513 version
= read_string();
515 printf("version = %s\n", version
);
519 file_bigendian
= buf
[0];
520 host_bigendian
= bigendian();
522 read_trace_init(file_bigendian
, host_bigendian
);
533 read_proc_kallsyms();
534 read_ftrace_printk();
536 size
= calc_data_size
- 1;
541 pevent_print_funcs(perf_pevent
);
545 pevent_print_printk(perf_pevent
);
This page took 0.043707 seconds and 6 git commands to generate.