Blame view

tools/perf/util/trace-event-read.c 9.66 KB
538bafb5c   Steven Rostedt   perf tools: Add t...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /*
   * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
   *
   * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; version 2 of the License (not later!)
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   *
   * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   */
f887f3019   Xiao Guangrong   perf tools: Clean...
21
  #define _FILE_OFFSET_BITS 64
538bafb5c   Steven Rostedt   perf tools: Add t...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  
  #include <dirent.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <getopt.h>
  #include <stdarg.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/wait.h>
  #include <sys/mman.h>
  #include <pthread.h>
  #include <fcntl.h>
  #include <unistd.h>
  #include <ctype.h>
  #include <errno.h>
1ef2ed106   Frederic Weisbecker   perf tools: Only ...
38
  #include "../perf.h"
538bafb5c   Steven Rostedt   perf tools: Add t...
39
40
41
42
43
44
45
46
47
48
49
50
  #include "util.h"
  #include "trace-event.h"
  
  static int input_fd;
  
  static int read_page;
  
  int file_bigendian;
  int host_bigendian;
  static int long_size;
  
  static unsigned long	page_size;
9215545e9   Tom Zanussi   perf: Convert per...
51
  static ssize_t calc_data_size;
454c407ec   Tom Zanussi   perf: add perf-in...
52
  static bool repipe;
9215545e9   Tom Zanussi   perf: Convert per...
53
54
55
56
57
58
59
60
61
62
  
  static int do_read(int fd, void *buf, int size)
  {
  	int rsize = size;
  
  	while (size) {
  		int ret = read(fd, buf, size);
  
  		if (ret <= 0)
  			return -1;
454c407ec   Tom Zanussi   perf: add perf-in...
63
64
65
66
67
68
  		if (repipe) {
  			int retw = write(STDOUT_FILENO, buf, ret);
  
  			if (retw <= 0 || retw != ret)
  				die("repiping input file");
  		}
9215545e9   Tom Zanussi   perf: Convert per...
69
70
71
72
73
74
  		size -= ret;
  		buf += ret;
  	}
  
  	return rsize;
  }
538bafb5c   Steven Rostedt   perf tools: Add t...
75
76
77
  static int read_or_die(void *data, int size)
  {
  	int r;
9215545e9   Tom Zanussi   perf: Convert per...
78
79
  	r = do_read(input_fd, data, size);
  	if (r <= 0)
538bafb5c   Steven Rostedt   perf tools: Add t...
80
81
  		die("reading input file (size expected=%d received=%d)",
  		    size, r);
9215545e9   Tom Zanussi   perf: Convert per...
82
83
84
  
  	if (calc_data_size)
  		calc_data_size += r;
538bafb5c   Steven Rostedt   perf tools: Add t...
85
86
  	return r;
  }
cbb5cf7ff   Tom Zanussi   perf: Use read() ...
87
88
89
90
91
92
93
94
95
96
97
98
  /* If it fails, the next read will report it */
  static void skip(int size)
  {
  	char buf[BUFSIZ];
  	int r;
  
  	while (size) {
  		r = size > BUFSIZ ? BUFSIZ : size;
  		read_or_die(buf, r);
  		size -= r;
  	};
  }
538bafb5c   Steven Rostedt   perf tools: Add t...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  static unsigned int read4(void)
  {
  	unsigned int data;
  
  	read_or_die(&data, 4);
  	return __data2host4(data);
  }
  
  static unsigned long long read8(void)
  {
  	unsigned long long data;
  
  	read_or_die(&data, 8);
  	return __data2host8(data);
  }
  
  static char *read_string(void)
  {
  	char buf[BUFSIZ];
  	char *str = NULL;
  	int size = 0;
f887f3019   Xiao Guangrong   perf tools: Clean...
120
  	off_t r;
9215545e9   Tom Zanussi   perf: Convert per...
121
  	char c;
538bafb5c   Steven Rostedt   perf tools: Add t...
122
123
  
  	for (;;) {
9215545e9   Tom Zanussi   perf: Convert per...
124
  		r = read(input_fd, &c, 1);
538bafb5c   Steven Rostedt   perf tools: Add t...
125
126
127
128
129
  		if (r < 0)
  			die("reading input file");
  
  		if (!r)
  			die("no data");
454c407ec   Tom Zanussi   perf: add perf-in...
130
131
132
133
134
135
  		if (repipe) {
  			int retw = write(STDOUT_FILENO, &c, 1);
  
  			if (retw <= 0 || retw != r)
  				die("repiping input file string");
  		}
9215545e9   Tom Zanussi   perf: Convert per...
136
  		buf[size++] = c;
538bafb5c   Steven Rostedt   perf tools: Add t...
137

9215545e9   Tom Zanussi   perf: Convert per...
138
139
  		if (!c)
  			break;
538bafb5c   Steven Rostedt   perf tools: Add t...
140
  	}
9215545e9   Tom Zanussi   perf: Convert per...
141
142
143
144
145
  	if (calc_data_size)
  		calc_data_size += size;
  
  	str = malloc_or_die(size);
  	memcpy(str, buf, size);
538bafb5c   Steven Rostedt   perf tools: Add t...
146
147
148
149
150
151
152
153
154
155
156
157
  
  	return str;
  }
  
  static void read_proc_kallsyms(void)
  {
  	unsigned int size;
  	char *buf;
  
  	size = read4();
  	if (!size)
  		return;
7691b1ec2   OGAWA Hirofumi   perf tools: Misc ...
158
  	buf = malloc_or_die(size + 1);
538bafb5c   Steven Rostedt   perf tools: Add t...
159
  	read_or_die(buf, size);
7691b1ec2   OGAWA Hirofumi   perf tools: Misc ...
160
  	buf[size] = '\0';
538bafb5c   Steven Rostedt   perf tools: Add t...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  
  	parse_proc_kallsyms(buf, size);
  
  	free(buf);
  }
  
  static void read_ftrace_printk(void)
  {
  	unsigned int size;
  	char *buf;
  
  	size = read4();
  	if (!size)
  		return;
  
  	buf = malloc_or_die(size);
  	read_or_die(buf, size);
  
  	parse_ftrace_printk(buf, size);
  
  	free(buf);
  }
  
  static void read_header_files(void)
  {
  	unsigned long long size;
538bafb5c   Steven Rostedt   perf tools: Add t...
187
188
189
190
191
192
193
194
195
  	char *header_event;
  	char buf[BUFSIZ];
  
  	read_or_die(buf, 12);
  
  	if (memcmp(buf, "header_page", 12) != 0)
  		die("did not read header page");
  
  	size = read8();
d00a47cce   Frederic Weisbecker   perf: Fix warning...
196
  	skip(size);
538bafb5c   Steven Rostedt   perf tools: Add t...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  
  	/*
  	 * The size field in the page is of type long,
  	 * use that instead, since it represents the kernel.
  	 */
  	long_size = header_page_size_size;
  
  	read_or_die(buf, 13);
  	if (memcmp(buf, "header_event", 13) != 0)
  		die("did not read header event");
  
  	size = read8();
  	header_event = malloc_or_die(size);
  	read_or_die(header_event, size);
  	free(header_event);
  }
  
  static void read_ftrace_file(unsigned long long size)
  {
  	char *buf;
  
  	buf = malloc_or_die(size);
  	read_or_die(buf, size);
  	parse_ftrace_file(buf, size);
  	free(buf);
  }
  
  static void read_event_file(char *sys, unsigned long long size)
  {
  	char *buf;
  
  	buf = malloc_or_die(size);
  	read_or_die(buf, size);
  	parse_event_file(buf, size, sys);
  	free(buf);
  }
  
  static void read_ftrace_files(void)
  {
  	unsigned long long size;
  	int count;
  	int i;
  
  	count = read4();
  
  	for (i = 0; i < count; i++) {
  		size = read8();
  		read_ftrace_file(size);
  	}
  }
  
  static void read_event_files(void)
  {
  	unsigned long long size;
  	char *sys;
  	int systems;
  	int count;
  	int i,x;
  
  	systems = read4();
  
  	for (i = 0; i < systems; i++) {
  		sys = read_string();
  
  		count = read4();
  		for (x=0; x < count; x++) {
  			size = read8();
  			read_event_file(sys, size);
  		}
  	}
  }
  
  struct cpu_data {
  	unsigned long long	offset;
  	unsigned long long	size;
  	unsigned long long	timestamp;
  	struct record		*next;
  	char			*page;
  	int			cpu;
  	int			index;
  	int			page_size;
  };
  
  static struct cpu_data *cpu_data;
  
  static void update_cpu_data_index(int cpu)
  {
  	cpu_data[cpu].offset += page_size;
  	cpu_data[cpu].size -= page_size;
  	cpu_data[cpu].index = 0;
  }
  
  static void get_next_page(int cpu)
  {
f887f3019   Xiao Guangrong   perf tools: Clean...
291
292
  	off_t save_seek;
  	off_t ret;
538bafb5c   Steven Rostedt   perf tools: Add t...
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  
  	if (!cpu_data[cpu].page)
  		return;
  
  	if (read_page) {
  		if (cpu_data[cpu].size <= page_size) {
  			free(cpu_data[cpu].page);
  			cpu_data[cpu].page = NULL;
  			return;
  		}
  
  		update_cpu_data_index(cpu);
  
  		/* other parts of the code may expect the pointer to not move */
f887f3019   Xiao Guangrong   perf tools: Clean...
307
  		save_seek = lseek(input_fd, 0, SEEK_CUR);
538bafb5c   Steven Rostedt   perf tools: Add t...
308

f887f3019   Xiao Guangrong   perf tools: Clean...
309
310
  		ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
  		if (ret == (off_t)-1)
538bafb5c   Steven Rostedt   perf tools: Add t...
311
312
313
314
315
316
  			die("failed to lseek");
  		ret = read(input_fd, cpu_data[cpu].page, page_size);
  		if (ret < 0)
  			die("failed to read page");
  
  		/* reset the file pointer back */
f887f3019   Xiao Guangrong   perf tools: Clean...
317
  		lseek(input_fd, save_seek, SEEK_SET);
538bafb5c   Steven Rostedt   perf tools: Add t...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
  
  		return;
  	}
  
  	munmap(cpu_data[cpu].page, page_size);
  	cpu_data[cpu].page = NULL;
  
  	if (cpu_data[cpu].size <= page_size)
  		return;
  
  	update_cpu_data_index(cpu);
  
  	cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
  				  input_fd, cpu_data[cpu].offset);
  	if (cpu_data[cpu].page == MAP_FAILED)
  		die("failed to mmap cpu %d at offset 0x%llx",
  		    cpu, cpu_data[cpu].offset);
  }
  
  static unsigned int type_len4host(unsigned int type_len_ts)
  {
  	if (file_bigendian)
  		return (type_len_ts >> 27) & ((1 << 5) - 1);
  	else
  		return type_len_ts & ((1 << 5) - 1);
  }
  
  static unsigned int ts4host(unsigned int type_len_ts)
  {
  	if (file_bigendian)
  		return type_len_ts & ((1 << 27) - 1);
  	else
  		return type_len_ts >> 5;
  }
  
  static int calc_index(void *ptr, int cpu)
  {
  	return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
  }
  
  struct record *trace_peek_data(int cpu)
  {
  	struct record *data;
  	void *page = cpu_data[cpu].page;
  	int idx = cpu_data[cpu].index;
  	void *ptr = page + idx;
  	unsigned long long extend;
  	unsigned int type_len_ts;
  	unsigned int type_len;
  	unsigned int delta;
  	unsigned int length = 0;
  
  	if (cpu_data[cpu].next)
  		return cpu_data[cpu].next;
  
  	if (!page)
  		return NULL;
  
  	if (!idx) {
  		/* FIXME: handle header page */
  		if (header_page_ts_size != 8)
  			die("expected a long long type for timestamp");
  		cpu_data[cpu].timestamp = data2host8(ptr);
  		ptr += 8;
  		switch (header_page_size_size) {
  		case 4:
  			cpu_data[cpu].page_size = data2host4(ptr);
  			ptr += 4;
  			break;
  		case 8:
  			cpu_data[cpu].page_size = data2host8(ptr);
  			ptr += 8;
  			break;
  		default:
  			die("bad long size");
  		}
  		ptr = cpu_data[cpu].page + header_page_data_offset;
  	}
  
  read_again:
  	idx = calc_index(ptr, cpu);
  
  	if (idx >= cpu_data[cpu].page_size) {
  		get_next_page(cpu);
  		return trace_peek_data(cpu);
  	}
  
  	type_len_ts = data2host4(ptr);
  	ptr += 4;
  
  	type_len = type_len4host(type_len_ts);
  	delta = ts4host(type_len_ts);
  
  	switch (type_len) {
  	case RINGBUF_TYPE_PADDING:
  		if (!delta)
  			die("error, hit unexpected end of page");
  		length = data2host4(ptr);
  		ptr += 4;
  		length *= 4;
  		ptr += length;
  		goto read_again;
  
  	case RINGBUF_TYPE_TIME_EXTEND:
  		extend = data2host4(ptr);
  		ptr += 4;
  		extend <<= TS_SHIFT;
  		extend += delta;
  		cpu_data[cpu].timestamp += extend;
  		goto read_again;
  
  	case RINGBUF_TYPE_TIME_STAMP:
  		ptr += 12;
  		break;
  	case 0:
  		length = data2host4(ptr);
  		ptr += 4;
  		die("here! length=%d", length);
  		break;
  	default:
  		length = type_len * 4;
  		break;
  	}
  
  	cpu_data[cpu].timestamp += delta;
  
  	data = malloc_or_die(sizeof(*data));
  	memset(data, 0, sizeof(*data));
  
  	data->ts = cpu_data[cpu].timestamp;
  	data->size = length;
  	data->data = ptr;
  	ptr += length;
  
  	cpu_data[cpu].index = calc_index(ptr, cpu);
  	cpu_data[cpu].next = data;
  
  	return data;
  }
  
  struct record *trace_read_data(int cpu)
  {
  	struct record *data;
  
  	data = trace_peek_data(cpu);
  	cpu_data[cpu].next = NULL;
  
  	return data;
  }
454c407ec   Tom Zanussi   perf: add perf-in...
467
  ssize_t trace_report(int fd, bool __repipe)
538bafb5c   Steven Rostedt   perf tools: Add t...
468
  {
538bafb5c   Steven Rostedt   perf tools: Add t...
469
470
471
  	char buf[BUFSIZ];
  	char test[] = { 23, 8, 68 };
  	char *version;
d9340c1db   Ingo Molnar   perf sched: Displ...
472
  	int show_version = 0;
538bafb5c   Steven Rostedt   perf tools: Add t...
473
474
  	int show_funcs = 0;
  	int show_printk = 0;
9215545e9   Tom Zanussi   perf: Convert per...
475
476
477
  	ssize_t size;
  
  	calc_data_size = 1;
454c407ec   Tom Zanussi   perf: add perf-in...
478
  	repipe = __repipe;
538bafb5c   Steven Rostedt   perf tools: Add t...
479

03456a158   Frederic Weisbecker   perf tools: Merge...
480
  	input_fd = fd;
538bafb5c   Steven Rostedt   perf tools: Add t...
481
482
483
  
  	read_or_die(buf, 3);
  	if (memcmp(buf, test, 3) != 0)
e25613683   Arnaldo Carvalho de Melo   perf trace: Read_...
484
  		die("no trace data in the file");
538bafb5c   Steven Rostedt   perf tools: Add t...
485
486
487
  
  	read_or_die(buf, 7);
  	if (memcmp(buf, "tracing", 7) != 0)
e25613683   Arnaldo Carvalho de Melo   perf trace: Read_...
488
  		die("not a trace file (missing 'tracing' tag)");
538bafb5c   Steven Rostedt   perf tools: Add t...
489
490
  
  	version = read_string();
d9340c1db   Ingo Molnar   perf sched: Displ...
491
492
493
  	if (show_version)
  		printf("version = %s
  ", version);
538bafb5c   Steven Rostedt   perf tools: Add t...
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  	free(version);
  
  	read_or_die(buf, 1);
  	file_bigendian = buf[0];
  	host_bigendian = bigendian();
  
  	read_or_die(buf, 1);
  	long_size = buf[0];
  
  	page_size = read4();
  
  	read_header_files();
  
  	read_ftrace_files();
  	read_event_files();
  	read_proc_kallsyms();
  	read_ftrace_printk();
9215545e9   Tom Zanussi   perf: Convert per...
511
512
  	size = calc_data_size - 1;
  	calc_data_size = 0;
454c407ec   Tom Zanussi   perf: add perf-in...
513
  	repipe = false;
9215545e9   Tom Zanussi   perf: Convert per...
514

538bafb5c   Steven Rostedt   perf tools: Add t...
515
516
  	if (show_funcs) {
  		print_funcs();
9215545e9   Tom Zanussi   perf: Convert per...
517
  		return size;
538bafb5c   Steven Rostedt   perf tools: Add t...
518
519
520
  	}
  	if (show_printk) {
  		print_printk();
9215545e9   Tom Zanussi   perf: Convert per...
521
  		return size;
538bafb5c   Steven Rostedt   perf tools: Add t...
522
  	}
9215545e9   Tom Zanussi   perf: Convert per...
523
  	return size;
538bafb5c   Steven Rostedt   perf tools: Add t...
524
  }