tools/reorder/histogram.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial