builtin-trace.c 5.9 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 58 59 60
		return -1;
	}
	total_comm++;

	return 0;
}

static int
process_sample_event(event_t *event, unsigned long offset, unsigned long head)
{
	char level;
	int show = 0;
	struct dso *dso = NULL;
	struct thread *thread;
	u64 ip = event->ip.ip;
61
	u64 timestamp = -1;
I
Ingo Molnar 已提交
62
	u32 cpu = -1;
F
Frederic Weisbecker 已提交
63 64 65 66 67 68
	u64 period = 1;
	void *more_data = event->ip.__more_data;
	int cpumode;

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

69 70 71 72 73
	if (sample_type & PERF_SAMPLE_TIME) {
		timestamp = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

I
Ingo Molnar 已提交
74 75 76 77 78 79
	if (sample_type & PERF_SAMPLE_CPU) {
		cpu = *(u32 *)more_data;
		more_data += sizeof(u32);
		more_data += sizeof(u32); /* reserved */
	}

F
Frederic Weisbecker 已提交
80 81 82 83 84
	if (sample_type & PERF_SAMPLE_PERIOD) {
		period = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

85
	dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
F
Frederic Weisbecker 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		(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;
	}

101
	cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
F
Frederic Weisbecker 已提交
102

103
	if (cpumode == PERF_RECORD_MISC_KERNEL) {
F
Frederic Weisbecker 已提交
104 105 106 107 108 109 110
		show = SHOW_KERNEL;
		level = 'k';

		dso = kernel_dso;

		dump_printf(" ...... dso: %s\n", dso->name);

111
	} else if (cpumode == PERF_RECORD_MISC_USER) {
F
Frederic Weisbecker 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

		show = SHOW_USER;
		level = '.';

	} else {
		show = SHOW_HV;
		level = 'H';

		dso = hypervisor_dso;

		dump_printf(" ...... dso: [hypervisor]\n");
	}

	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
		 */
136
		print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
F
Frederic Weisbecker 已提交
137 138 139 140 141 142 143 144 145 146 147 148
	}
	total += period;

	return 0;
}

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

	switch (event->header.type) {
149
	case PERF_RECORD_MMAP ... PERF_RECORD_LOST:
F
Frederic Weisbecker 已提交
150 151
		return 0;

152
	case PERF_RECORD_COMM:
F
Frederic Weisbecker 已提交
153 154
		return process_comm_event(event, offset, head);

155
	case PERF_RECORD_EXIT ... PERF_RECORD_READ:
F
Frederic Weisbecker 已提交
156 157
		return 0;

158
	case PERF_RECORD_SAMPLE:
F
Frederic Weisbecker 已提交
159 160
		return process_sample_event(event, offset, head);

161
	case PERF_RECORD_MAX:
F
Frederic Weisbecker 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	default:
		return -1;
	}

	return 0;
}

static int __cmd_trace(void)
{
	int ret, rc = EXIT_FAILURE;
	unsigned long offset = 0;
	unsigned long head = 0;
	struct stat perf_stat;
	event_t *event;
	uint32_t size;
	char *buf;

	trace_report();
180
	register_idle_thread(&threads, &last_match);
F
Frederic Weisbecker 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

	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);
199
	head = header->data_offset;
F
Frederic Weisbecker 已提交
200 201
	sample_type = perf_header__sample_type(header);

202 203 204 205
	if (!(sample_type & PERF_SAMPLE_RAW))
		die("No trace sample to read. Did you call perf record "
		    "without -R?");

F
Frederic Weisbecker 已提交
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
	if (load_kernel() < 0) {
		perror("failed to load kernel symbols");
		return EXIT_FAILURE;
	}

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);

	size = event->header.size;
	if (!size)
		size = 8;

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

		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)"),
275
	OPT_END()
F
Frederic Weisbecker 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
};

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();
}