summaryrefslogtreecommitdiffstats
path: root/Documentation/perf_counter/perf-report.cc
blob: 1727317352bf71e4afc2d5c5bda18cdb19014163 (plain) (blame)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
187
188
189
190
191
192
193
194
195
196
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
467
468
469
470
471
472
473
474
475
476
477
478
479
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <getopt.h>

#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <linux/unistd.h>
#include <linux/types.h>

#include "../../include/linux/perf_counter.h"

#include <set>
#include <map>
#include <string>


static char 		const *input_name = "output.perf";
static int		input;

static unsigned long	page_size;
static unsigned long	mmap_window = 32;

struct ip_event {
	struct perf_event_header header;
	__u64 ip;
	__u32 pid, tid;
};
struct mmap_event {
	struct perf_event_header header;
	__u32 pid, tid;
	__u64 start;
	__u64 len;
	__u64 pgoff;
	char filename[PATH_MAX];
};
struct comm_event {
	struct perf_event_header header;
	__u32 pid,tid;
	char comm[16];
};

typedef union event_union {
	struct perf_event_header header;
	struct ip_event ip;
	struct mmap_event mmap;
	struct comm_event comm;
} event_t;

struct section {
	uint64_t start;
	uint64_t end;

	uint64_t offset;

	std::string name;

	section() { };

	section(uint64_t stab) : end(stab) { };

	section(uint64_t start, uint64_t size, uint64_t offset, std::string name) :
		start(start), end(start + size), offset(offset), name(name)
	{ };

	bool operator < (const struct section &s) const {
		return end < s.end;
	};
};

typedef std::set<struct section> sections_t;

struct symbol {
	uint64_t start;
	uint64_t end;

	std::string name;

	symbol() { };

	symbol(uint64_t ip) : start(ip) { }

	symbol(uint64_t start, uint64_t len, std::string name) :
		start(start), end(start + len), name(name)
	{ };

	bool operator < (const struct symbol &s) const {
		return start < s.start;
	};
};

typedef std::set<struct symbol> symbols_t;

struct dso {
	sections_t sections;
	symbols_t syms;
};

static std::map<std::string, struct dso> dsos;

static void load_dso_sections(std::string dso_name)
{
	struct dso &dso = dsos[dso_name];

	std::string cmd = "readelf -DSW " + dso_name;

	FILE *file = popen(cmd.c_str(), "r");
	if (!file) {
		perror("failed to open pipe");
		exit(-1);
	}

	char *line = NULL;
	size_t n = 0;

	while (!feof(file)) {
		uint64_t addr, off, size;
		char name[32];

		if (getline(&line, &n, file) < 0)
			break;
		if (!line)
			break;

		if (sscanf(line, "  [%*2d] %16s %*14s %Lx %Lx %Lx",
					name, &addr, &off, &size) == 4) {

			dso.sections.insert(section(addr, size, addr - off, name));
		}
#if 0
		/*
		 * for reading readelf symbols (-s), however these don't seem
		 * to include nearly everything, so use nm for that.
		 */
		if (sscanf(line, " %*4d %*3d: %Lx %5Lu %*7s %*6s %*7s %3d %s",
			   &start, &size, &section, sym) == 4) {

			start -= dso.section_offsets[section];

			dso.syms.insert(symbol(start, size, std::string(sym)));
		}
#endif
	}
	pclose(file);
}

static void load_dso_symbols(std::string dso_name, std::string args)
{
	struct dso &dso = dsos[dso_name];

	std::string cmd = "nm -nSC " + args + " " + dso_name;

	FILE *file = popen(cmd.c_str(), "r");
	if (!file) {
		perror("failed to open pipe");
		exit(-1);
	}

	char *line = NULL;
	size_t n = 0;

	while (!feof(file)) {
		uint64_t start, size;
		char c;
		char sym[1024];

		if (getline(&line, &n, file) < 0)
			break;
		if (!line)
			break;


		if (sscanf(line, "%Lx %Lx %c %s", &start, &size, &c, sym) == 4) {
			sections_t::const_iterator si =
				dso.sections.upper_bound(section(start));
			if (si == dso.sections.end()) {
				printf("symbol in unknown section: %s\n", sym);
				continue;
			}

			start -= si->offset;

			dso.syms.insert(symbol(start, size, sym));
		}
	}
	pclose(file);
}

static void load_dso(std::string dso_name)
{
	load_dso_sections(dso_name);
	load_dso_symbols(dso_name, "-D"); /* dynamic symbols */
	load_dso_symbols(dso_name, "");   /* regular ones */
}

void load_kallsyms(void)
{
	struct dso &dso = dsos["[kernel]"];

	FILE *file = fopen("/proc/kallsyms", "r");
	if (!file) {
		perror("failed to open kallsyms");
		exit(-1);
	}

	char *line;
	size_t n;

	while (!feof(file)) {
		uint64_t start;
		char c;
		char sym[1024];

		if (getline(&line, &n, file) < 0)
			break;
		if (!line)
			break;

		if (sscanf(line, "%Lx %c %s", &start, &c, sym) == 3)
			dso.syms.insert(symbol(start, 0x1000000, std::string(sym)));
	}
	fclose(file);
}

struct map {
	uint64_t start;
	uint64_t end;
	uint64_t pgoff;

	std::string dso;

	map() { };

	map(uint64_t ip) : end(ip) { }

	map(mmap_event *mmap) {
		start = mmap->start;
		end = mmap->start + mmap->len;
		pgoff = mmap->pgoff;

		dso = std::string(mmap->filename);

		if (dsos.find(dso) == dsos.end())
			load_dso(dso);
	};

	bool operator < (const struct map &m) const {
		return end < m.end;
	};
};

typedef std::set<struct map> maps_t;

static std::map<int, maps_t> maps;

static std::map<int, std::string> comms;

static std::map<std::string, int> hist;
static std::multimap<int, std::string> rev_hist;

static std::string resolve_comm(int pid)
{
	std::string comm;

	std::map<int, std::string>::const_iterator ci = comms.find(pid);
	if (ci != comms.end()) {
		comm = ci->second;
	} else {
		char pid_str[30];

		sprintf(pid_str, ":%d", pid);
		comm = pid_str;
	}

	return comm;
}

static std::string resolve_user_symbol(int pid, uint64_t ip)
{
	std::string sym = "<unknown>";

	maps_t &m = maps[pid];
	maps_t::const_iterator mi = m.upper_bound(map(ip));
	if (mi == m.end())
		return sym;

	ip -= mi->start + mi->pgoff;

	symbols_t &s = dsos[mi->dso].syms;
	symbols_t::const_iterator si = s.upper_bound(symbol(ip));

	sym = mi->dso + ": <unknown>";

	if (si == s.begin())
		return sym;
	si--;

	if (si->start <= ip && ip < si->end)
		sym = mi->dso + ": " + si->name;
#if 0
	else if (si->start <= ip)
		sym = mi->dso + ": ?" + si->name;
#endif

	return sym;
}

static std::string resolve_kernel_symbol(uint64_t ip)
{
	std::string sym = "<unknown>";

	symbols_t &s = dsos["[kernel]"].syms;
	symbols_t::const_iterator si = s.upper_bound(symbol(ip));

	if (si == s.begin())
		return sym;
	si--;

	if (si->start <= ip && ip < si->end)
		sym = si->name;

	return sym;
}

static void display_help(void)
{
	printf(
	"Usage: perf-report [<options>]\n"
	" -i file   --input=<file>      # input file\n"
	);

	exit(0);
}

static void process_options(int argc, char *argv[])
{
	int error = 0;

	for (;;) {
		int option_index = 0;
		/** Options for getopt */
		static struct option long_options[] = {
			{"input",	required_argument,	NULL, 'i'},
			{NULL,		0,			NULL,  0 }
		};
		int c = getopt_long(argc, argv, "+:i:",
				    long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'i': input_name			= strdup(optarg); break;
		default: error = 1; break;
		}
	}

	if (error)
		display_help();
}

int main(int argc, char *argv[])
{
	unsigned long offset = 0;
	unsigned long head = 0;
	struct stat stat;
	char *buf;
	event_t *event;
	int ret;
	unsigned long total = 0;

	page_size = getpagesize();

	process_options(argc, argv);

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

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

	load_kallsyms();

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) {
		unsigned long shift = page_size * (head / page_size);

		munmap(buf, page_size * mmap_window);
		offset += shift;
		head -= shift;
		goto remap;
	}
	head += event->header.size;

	if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
		std::string comm, sym, level;
		char output[1024];

		if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
			level = " [k] ";
			sym = resolve_kernel_symbol(event->ip.ip);
		} else if (event->header.misc & PERF_EVENT_MISC_USER) {
			level = " [.] ";
			sym = resolve_user_symbol(event->ip.pid, event->ip.ip);
		} else {
			level = " [H] ";
		}
		comm = resolve_comm(event->ip.pid);

		snprintf(output, sizeof(output), "%16s %s %s",
				comm.c_str(), level.c_str(), sym.c_str());
		hist[output]++;

		total++;

	} else switch (event->header.type) {
	case PERF_EVENT_MMAP:
		maps[event->mmap.pid].insert(map(&event->mmap));
		break;

	case PERF_EVENT_COMM:
		comms[event->comm.pid] = std::string(event->comm.comm);
		break;
	}

	if (offset + head < stat.st_size)
		goto more;

	close(input);

	std::map<std::string, int>::iterator hi = hist.begin();

	while (hi != hist.end()) {
		rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
		hist.erase(hi++);
	}

	std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();

	while (ri != rev_hist.end()) {
		printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
		ri++;
	}

	return 0;
}