Bumping manifests a=b2g-bump
[gecko.git] / tools / reorder / histogram.cpp
blobaa0c73a78da528cacc756c18fb036f06818661d8
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /*
7 A program that computes a call histogram from runtime call
8 information. It reads a list of address references (e.g., as
9 computed by libcygprof.so), and uses an ELF image to map the
10 addresses to functions.
14 #include <fstream>
15 #include <hash_map>
16 #include <limits.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
21 #include "elf_symbol_table.h"
23 #define _GNU_SOURCE
24 #include <getopt.h>
26 const char *opt_exe;
27 int opt_tick = 0;
29 elf_symbol_table table;
31 typedef hash_map<unsigned int, unsigned int> histogram_t;
32 histogram_t histogram;
34 static struct option long_options[] = {
35 { "exe", required_argument, 0, 'e' },
36 { "tick", optional_argument, 0, 't' },
37 { 0, 0, 0, 0 }
40 static void
41 usage(const char *name)
43 cerr << "usage: " << name << " --exe=<image> [--tick[=count]]" << endl;
46 static void
47 map_addrs(int fd)
49 // Read the binary addresses from stdin.
50 unsigned int buf[128];
51 ssize_t cb;
53 unsigned int count = 0;
54 while ((cb = read(fd, buf, sizeof buf)) > 0) {
55 if (cb % sizeof buf[0])
56 fprintf(stderr, "unaligned read\n");
58 unsigned int *addr = buf;
59 unsigned int *limit = buf + (cb / 4);
61 for (; addr < limit; ++addr) {
62 const Elf32_Sym *sym = table.lookup(*addr);
63 if (sym)
64 ++histogram[reinterpret_cast<unsigned int>(sym)];
66 if (opt_tick && (++count % opt_tick == 0)) {
67 cerr << ".";
68 flush(cerr);
73 if (opt_tick)
74 cerr << endl;
77 int
78 main(int argc, char *argv[])
80 int c;
81 while (1) {
82 int option_index = 0;
83 c = getopt_long(argc, argv, "e:t", long_options, &option_index);
85 if (c < 0)
86 break;
88 switch (c) {
89 case 'e':
90 opt_exe = optarg;
91 break;
93 case 't':
94 opt_tick = optarg ? atoi(optarg) : 1000000;
95 break;
97 default:
98 usage(argv[0]);
99 return 1;
103 if (! opt_exe) {
104 usage(argv[0]);
105 return 1;
108 table.init(opt_exe);
110 // Process addresses.
111 if (optind >= argc) {
112 map_addrs(STDIN_FILENO);
114 else {
115 do {
116 int fd = open(argv[optind], O_RDONLY);
117 if (fd < 0) {
118 perror(argv[optind]);
119 return 1;
122 map_addrs(fd);
123 close(fd);
124 } while (++optind < argc);
127 // Emit the histogram.
128 histogram_t::const_iterator limit = histogram.end();
129 histogram_t::const_iterator i;
130 for (i = histogram.begin(); i != limit; ++i) {
131 const Elf32_Sym *sym = reinterpret_cast<const Elf32_Sym *>(i->first);
132 cout.form("%08x %6d %2d %10d ",
133 sym->st_value,
134 sym->st_size,
135 sym->st_shndx,
136 i->second);
138 cout << table.get_symbol_name(sym) << endl;
141 return 0;