builtin-trace.c 5.5 KB
Newer Older
F
Frederic Weisbecker 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "builtin.h"

#include "util/util.h"
#include "util/cache.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"

#include "util/parse-options.h"

#include "perf.h"
#include "util/debug.h"

#include "util/trace-event.h"

static char		const *input_name = "perf.data";
static int		input;
static unsigned long	page_size;
static unsigned long	mmap_window = 32;

static unsigned long	total = 0;
static unsigned long	total_comm = 0;

static struct rb_root	threads;
static struct thread	*last_match;

static struct perf_header *header;
static u64		sample_type;


static int
process_comm_event(event_t *event, unsigned long offset, unsigned long head)
{
	struct thread *thread;

	thread = threads__findnew(event->comm.pid, &threads, &last_match);

38
	dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
F
Frederic Weisbecker 已提交
39 40 41 42 43 44
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->comm.comm, event->comm.pid);

	if (thread == NULL ||
	    thread__set_comm(thread, event->comm.comm)) {
45
		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
F
Frederic Weisbecker 已提交
46 47 48 49 50 51 52 53 54 55 56 57
		return -1;
	}
	total_comm++;

	return 0;
}

static int
process_sample_event(event_t *event, unsigned long offset, unsigned long head)
{
	struct thread *thread;
	u64 ip = event->ip.ip;
58
	u64 timestamp = -1;
I
Ingo Molnar 已提交
59
	u32 cpu = -1;
F
Frederic Weisbecker 已提交
60 61 62 63 64
	u64 period = 1;
	void *more_data = event->ip.__more_data;

	thread = threads__findnew(event->ip.pid, &threads, &last_match);

65 66 67 68 69
	if (sample_type & PERF_SAMPLE_TIME) {
		timestamp = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

I
Ingo Molnar 已提交
70 71 72 73 74 75
	if (sample_type & PERF_SAMPLE_CPU) {
		cpu = *(u32 *)more_data;
		more_data += sizeof(u32);
		more_data += sizeof(u32); /* reserved */
	}

F
Frederic Weisbecker 已提交
76 77 78 79 80
	if (sample_type & PERF_SAMPLE_PERIOD) {
		period = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

81
	dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
F
Frederic Weisbecker 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->header.misc,
		event->ip.pid, event->ip.tid,
		(void *)(long)ip,
		(long long)period);

	dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);

	if (thread == NULL) {
		eprintf("problem processing %d event, skipping it.\n",
			event->header.type);
		return -1;
	}

	if (sample_type & PERF_SAMPLE_RAW) {
		struct {
			u32 size;
			char data[0];
		} *raw = more_data;

		/*
		 * FIXME: better resolve from pid from the struct trace_entry
		 * field, although it should be the same than this perf
		 * event pid
		 */
108
		print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
F
Frederic Weisbecker 已提交
109 110 111 112 113 114 115 116 117 118 119 120
	}
	total += period;

	return 0;
}

static int
process_event(event_t *event, unsigned long offset, unsigned long head)
{
	trace_event(event);

	switch (event->header.type) {
121
	case PERF_RECORD_MMAP ... PERF_RECORD_LOST:
F
Frederic Weisbecker 已提交
122 123
		return 0;

124
	case PERF_RECORD_COMM:
F
Frederic Weisbecker 已提交
125 126
		return process_comm_event(event, offset, head);

127
	case PERF_RECORD_EXIT ... PERF_RECORD_READ:
F
Frederic Weisbecker 已提交
128 129
		return 0;

130
	case PERF_RECORD_SAMPLE:
F
Frederic Weisbecker 已提交
131 132
		return process_sample_event(event, offset, head);

133
	case PERF_RECORD_MAX:
F
Frederic Weisbecker 已提交
134 135 136 137 138 139 140 141 142 143 144 145
	default:
		return -1;
	}

	return 0;
}

static int __cmd_trace(void)
{
	int ret, rc = EXIT_FAILURE;
	unsigned long offset = 0;
	unsigned long head = 0;
146
	unsigned long shift;
F
Frederic Weisbecker 已提交
147 148 149 150 151
	struct stat perf_stat;
	event_t *event;
	uint32_t size;
	char *buf;

152
	register_idle_thread(&threads, &last_match);
F
Frederic Weisbecker 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

	input = open(input_name, O_RDONLY);
	if (input < 0) {
		perror("failed to open file");
		exit(-1);
	}

	ret = fstat(input, &perf_stat);
	if (ret < 0) {
		perror("failed to stat file");
		exit(-1);
	}

	if (!perf_stat.st_size) {
		fprintf(stderr, "zero-sized file, nothing to do!\n");
		exit(0);
	}
	header = perf_header__read(input);
171
	head = header->data_offset;
F
Frederic Weisbecker 已提交
172 173
	sample_type = perf_header__sample_type(header);

174 175 176 177
	if (!(sample_type & PERF_SAMPLE_RAW))
		die("No trace sample to read. Did you call perf record "
		    "without -R?");

F
Frederic Weisbecker 已提交
178 179 180 181 182
	if (load_kernel() < 0) {
		perror("failed to load kernel symbols");
		return EXIT_FAILURE;
	}

183 184 185 186
	shift = page_size * (head / page_size);
	offset += shift;
	head -= shift;

F
Frederic Weisbecker 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200
remap:
	buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
			   MAP_SHARED, input, offset);
	if (buf == MAP_FAILED) {
		perror("failed to mmap file");
		exit(-1);
	}

more:
	event = (event_t *)(buf + head);

	if (head + event->header.size >= page_size * mmap_window) {
		int res;

201
		shift = page_size * (head / page_size);
F
Frederic Weisbecker 已提交
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
		res = munmap(buf, page_size * mmap_window);
		assert(res == 0);

		offset += shift;
		head -= shift;
		goto remap;
	}

	size = event->header.size;

	if (!size || process_event(event, offset, head) < 0) {

		/*
		 * assume we lost track of the stream, check alignment, and
		 * increment a single u64 in the hope to catch on again 'soon'.
		 */

		if (unlikely(head & 7))
			head &= ~7ULL;

		size = 8;
	}

	head += size;

	if (offset + head < (unsigned long)perf_stat.st_size)
		goto more;

	rc = EXIT_SUCCESS;
	close(input);

	return rc;
}

static const char * const annotate_usage[] = {
	"perf trace [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
	OPT_BOOLEAN('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
246
	OPT_END()
F
Frederic Weisbecker 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
};

int cmd_trace(int argc, const char **argv, const char *prefix __used)
{
	symbol__init();
	page_size = getpagesize();

	argc = parse_options(argc, argv, options, annotate_usage, 0);
	if (argc) {
		/*
		 * Special case: if there's an argument left then assume tha
		 * it's a symbol filter:
		 */
		if (argc > 1)
			usage_with_options(annotate_usage, options);
	}

	setup_pager();

	return __cmd_trace();
}