perf-report.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #define _GNU_SOURCE
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <limits.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <ctype.h>
  14. #include <time.h>
  15. #include <getopt.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/poll.h>
  18. #include <sys/prctl.h>
  19. #include <sys/wait.h>
  20. #include <sys/mman.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <linux/unistd.h>
  24. #include <linux/types.h>
  25. #include "../../include/linux/perf_counter.h"
  26. #include <set>
  27. #include <map>
  28. #include <string>
  29. static char const *input_name = "output.perf";
  30. static int input;
  31. static unsigned long page_size;
  32. static unsigned long mmap_window = 32;
  33. struct ip_event {
  34. struct perf_event_header header;
  35. __u64 ip;
  36. __u32 pid, tid;
  37. };
  38. struct mmap_event {
  39. struct perf_event_header header;
  40. __u32 pid, tid;
  41. __u64 start;
  42. __u64 len;
  43. __u64 pgoff;
  44. char filename[PATH_MAX];
  45. };
  46. struct comm_event {
  47. struct perf_event_header header;
  48. __u32 pid,tid;
  49. char comm[16];
  50. };
  51. typedef union event_union {
  52. struct perf_event_header header;
  53. struct ip_event ip;
  54. struct mmap_event mmap;
  55. struct comm_event comm;
  56. } event_t;
  57. struct section {
  58. uint64_t start;
  59. uint64_t end;
  60. uint64_t offset;
  61. std::string name;
  62. section() { };
  63. section(uint64_t stab) : end(stab) { };
  64. section(uint64_t start, uint64_t size, uint64_t offset, std::string name) :
  65. start(start), end(start + size), offset(offset), name(name)
  66. { };
  67. bool operator < (const struct section &s) const {
  68. return end < s.end;
  69. };
  70. };
  71. typedef std::set<struct section> sections_t;
  72. struct symbol {
  73. uint64_t start;
  74. uint64_t end;
  75. std::string name;
  76. symbol() { };
  77. symbol(uint64_t ip) : start(ip) { }
  78. symbol(uint64_t start, uint64_t len, std::string name) :
  79. start(start), end(start + len), name(name)
  80. { };
  81. bool operator < (const struct symbol &s) const {
  82. return start < s.start;
  83. };
  84. };
  85. typedef std::set<struct symbol> symbols_t;
  86. struct dso {
  87. sections_t sections;
  88. symbols_t syms;
  89. };
  90. static std::map<std::string, struct dso> dsos;
  91. static void load_dso_sections(std::string dso_name)
  92. {
  93. struct dso &dso = dsos[dso_name];
  94. std::string cmd = "readelf -DSW " + dso_name;
  95. FILE *file = popen(cmd.c_str(), "r");
  96. if (!file) {
  97. perror("failed to open pipe");
  98. exit(-1);
  99. }
  100. char *line = NULL;
  101. size_t n = 0;
  102. while (!feof(file)) {
  103. uint64_t addr, off, size;
  104. char name[32];
  105. if (getline(&line, &n, file) < 0)
  106. break;
  107. if (!line)
  108. break;
  109. if (sscanf(line, " [%*2d] %16s %*14s %Lx %Lx %Lx",
  110. name, &addr, &off, &size) == 4) {
  111. dso.sections.insert(section(addr, size, addr - off, name));
  112. }
  113. #if 0
  114. /*
  115. * for reading readelf symbols (-s), however these don't seem
  116. * to include nearly everything, so use nm for that.
  117. */
  118. if (sscanf(line, " %*4d %*3d: %Lx %5Lu %*7s %*6s %*7s %3d %s",
  119. &start, &size, &section, sym) == 4) {
  120. start -= dso.section_offsets[section];
  121. dso.syms.insert(symbol(start, size, std::string(sym)));
  122. }
  123. #endif
  124. }
  125. pclose(file);
  126. }
  127. static void load_dso_symbols(std::string dso_name, std::string args)
  128. {
  129. struct dso &dso = dsos[dso_name];
  130. std::string cmd = "nm -nSC " + args + " " + dso_name;
  131. FILE *file = popen(cmd.c_str(), "r");
  132. if (!file) {
  133. perror("failed to open pipe");
  134. exit(-1);
  135. }
  136. char *line = NULL;
  137. size_t n = 0;
  138. while (!feof(file)) {
  139. uint64_t start, size;
  140. char c;
  141. char sym[1024];
  142. if (getline(&line, &n, file) < 0)
  143. break;
  144. if (!line)
  145. break;
  146. if (sscanf(line, "%Lx %Lx %c %s", &start, &size, &c, sym) == 4) {
  147. sections_t::const_iterator si =
  148. dso.sections.upper_bound(section(start));
  149. if (si == dso.sections.end()) {
  150. printf("symbol in unknown section: %s\n", sym);
  151. continue;
  152. }
  153. start -= si->offset;
  154. dso.syms.insert(symbol(start, size, sym));
  155. }
  156. }
  157. pclose(file);
  158. }
  159. static void load_dso(std::string dso_name)
  160. {
  161. load_dso_sections(dso_name);
  162. load_dso_symbols(dso_name, "-D"); /* dynamic symbols */
  163. load_dso_symbols(dso_name, ""); /* regular ones */
  164. }
  165. void load_kallsyms(void)
  166. {
  167. struct dso &dso = dsos["[kernel]"];
  168. FILE *file = fopen("/proc/kallsyms", "r");
  169. if (!file) {
  170. perror("failed to open kallsyms");
  171. exit(-1);
  172. }
  173. char *line;
  174. size_t n;
  175. while (!feof(file)) {
  176. uint64_t start;
  177. char c;
  178. char sym[1024];
  179. if (getline(&line, &n, file) < 0)
  180. break;
  181. if (!line)
  182. break;
  183. if (sscanf(line, "%Lx %c %s", &start, &c, sym) == 3)
  184. dso.syms.insert(symbol(start, 0x1000000, std::string(sym)));
  185. }
  186. fclose(file);
  187. }
  188. struct map {
  189. uint64_t start;
  190. uint64_t end;
  191. uint64_t pgoff;
  192. std::string dso;
  193. map() { };
  194. map(uint64_t ip) : end(ip) { }
  195. map(mmap_event *mmap) {
  196. start = mmap->start;
  197. end = mmap->start + mmap->len;
  198. pgoff = mmap->pgoff;
  199. dso = std::string(mmap->filename);
  200. if (dsos.find(dso) == dsos.end())
  201. load_dso(dso);
  202. };
  203. bool operator < (const struct map &m) const {
  204. return end < m.end;
  205. };
  206. };
  207. typedef std::set<struct map> maps_t;
  208. static std::map<int, maps_t> maps;
  209. static std::map<int, std::string> comms;
  210. static std::map<std::string, int> hist;
  211. static std::multimap<int, std::string> rev_hist;
  212. static std::string resolve_comm(int pid)
  213. {
  214. std::string comm;
  215. std::map<int, std::string>::const_iterator ci = comms.find(pid);
  216. if (ci != comms.end()) {
  217. comm = ci->second;
  218. } else {
  219. char pid_str[30];
  220. sprintf(pid_str, ":%d", pid);
  221. comm = pid_str;
  222. }
  223. return comm;
  224. }
  225. static std::string resolve_user_symbol(int pid, uint64_t ip)
  226. {
  227. std::string sym = "<unknown>";
  228. maps_t &m = maps[pid];
  229. maps_t::const_iterator mi = m.upper_bound(map(ip));
  230. if (mi == m.end())
  231. return sym;
  232. ip -= mi->start + mi->pgoff;
  233. symbols_t &s = dsos[mi->dso].syms;
  234. symbols_t::const_iterator si = s.upper_bound(symbol(ip));
  235. sym = mi->dso + ": <unknown>";
  236. if (si == s.begin())
  237. return sym;
  238. si--;
  239. if (si->start <= ip && ip < si->end)
  240. sym = mi->dso + ": " + si->name;
  241. #if 0
  242. else if (si->start <= ip)
  243. sym = mi->dso + ": ?" + si->name;
  244. #endif
  245. return sym;
  246. }
  247. static std::string resolve_kernel_symbol(uint64_t ip)
  248. {
  249. std::string sym = "<unknown>";
  250. symbols_t &s = dsos["[kernel]"].syms;
  251. symbols_t::const_iterator si = s.upper_bound(symbol(ip));
  252. if (si == s.begin())
  253. return sym;
  254. si--;
  255. if (si->start <= ip && ip < si->end)
  256. sym = si->name;
  257. return sym;
  258. }
  259. static void display_help(void)
  260. {
  261. printf(
  262. "Usage: perf-report [<options>]\n"
  263. " -i file --input=<file> # input file\n"
  264. );
  265. exit(0);
  266. }
  267. static void process_options(int argc, char *argv[])
  268. {
  269. int error = 0;
  270. for (;;) {
  271. int option_index = 0;
  272. /** Options for getopt */
  273. static struct option long_options[] = {
  274. {"input", required_argument, NULL, 'i'},
  275. {NULL, 0, NULL, 0 }
  276. };
  277. int c = getopt_long(argc, argv, "+:i:",
  278. long_options, &option_index);
  279. if (c == -1)
  280. break;
  281. switch (c) {
  282. case 'i': input_name = strdup(optarg); break;
  283. default: error = 1; break;
  284. }
  285. }
  286. if (error)
  287. display_help();
  288. }
  289. int main(int argc, char *argv[])
  290. {
  291. unsigned long offset = 0;
  292. unsigned long head = 0;
  293. struct stat stat;
  294. char *buf;
  295. event_t *event;
  296. int ret;
  297. unsigned long total = 0;
  298. page_size = getpagesize();
  299. process_options(argc, argv);
  300. input = open(input_name, O_RDONLY);
  301. if (input < 0) {
  302. perror("failed to open file");
  303. exit(-1);
  304. }
  305. ret = fstat(input, &stat);
  306. if (ret < 0) {
  307. perror("failed to stat file");
  308. exit(-1);
  309. }
  310. load_kallsyms();
  311. remap:
  312. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  313. MAP_SHARED, input, offset);
  314. if (buf == MAP_FAILED) {
  315. perror("failed to mmap file");
  316. exit(-1);
  317. }
  318. more:
  319. event = (event_t *)(buf + head);
  320. if (head + event->header.size >= page_size * mmap_window) {
  321. unsigned long shift = page_size * (head / page_size);
  322. munmap(buf, page_size * mmap_window);
  323. offset += shift;
  324. head -= shift;
  325. goto remap;
  326. }
  327. head += event->header.size;
  328. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  329. std::string comm, sym, level;
  330. char output[1024];
  331. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  332. level = " [k] ";
  333. sym = resolve_kernel_symbol(event->ip.ip);
  334. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  335. level = " [.] ";
  336. sym = resolve_user_symbol(event->ip.pid, event->ip.ip);
  337. } else {
  338. level = " [H] ";
  339. }
  340. comm = resolve_comm(event->ip.pid);
  341. snprintf(output, sizeof(output), "%16s %s %s",
  342. comm.c_str(), level.c_str(), sym.c_str());
  343. hist[output]++;
  344. total++;
  345. } else switch (event->header.type) {
  346. case PERF_EVENT_MMAP:
  347. maps[event->mmap.pid].insert(map(&event->mmap));
  348. break;
  349. case PERF_EVENT_COMM:
  350. comms[event->comm.pid] = std::string(event->comm.comm);
  351. break;
  352. }
  353. if (offset + head < stat.st_size)
  354. goto more;
  355. close(input);
  356. std::map<std::string, int>::iterator hi = hist.begin();
  357. while (hi != hist.end()) {
  358. rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
  359. hist.erase(hi++);
  360. }
  361. std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
  362. while (ri != rev_hist.end()) {
  363. printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
  364. ri++;
  365. }
  366. return 0;
  367. }