Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * builtin-report.c |
| 3 | * |
| 4 | * Builtin report command: Analyze the perf.data input file, |
| 5 | * look up and read DSOs and symbol information and display |
| 6 | * a histogram of results, along various sorting keys. |
| 7 | */ |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 8 | #include "builtin.h" |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 9 | |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 10 | #include "util/util.h" |
| 11 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 12 | #include "util/list.h" |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 13 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 14 | #include "util/rbtree.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 15 | #include "util/symbol.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 16 | #include "util/string.h" |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 17 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 18 | #include "perf.h" |
| 19 | |
| 20 | #include "util/parse-options.h" |
| 21 | #include "util/parse-events.h" |
| 22 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 23 | #define SHOW_KERNEL 1 |
| 24 | #define SHOW_USER 2 |
| 25 | #define SHOW_HV 4 |
| 26 | |
Ingo Molnar | 23ac9cb | 2009-05-27 09:33:18 +0200 | [diff] [blame] | 27 | static char const *input_name = "perf.data"; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 28 | static char *vmlinux = NULL; |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 29 | |
| 30 | static char default_sort_order[] = "comm,dso"; |
| 31 | static char *sort_order = default_sort_order; |
| 32 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 33 | static int input; |
| 34 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 35 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 36 | static int dump_trace = 0; |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 37 | #define dprintf(x...) do { if (dump_trace) printf(x); } while (0) |
| 38 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 39 | static int verbose; |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 40 | static int full_paths; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 41 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 42 | static unsigned long page_size; |
| 43 | static unsigned long mmap_window = 32; |
| 44 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 45 | const char *perf_event_names[] = { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 46 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
| 47 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", |
| 48 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", |
| 49 | }; |
| 50 | |
| 51 | struct ip_event { |
| 52 | struct perf_event_header header; |
| 53 | __u64 ip; |
| 54 | __u32 pid, tid; |
| 55 | }; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 56 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 57 | struct mmap_event { |
| 58 | struct perf_event_header header; |
| 59 | __u32 pid, tid; |
| 60 | __u64 start; |
| 61 | __u64 len; |
| 62 | __u64 pgoff; |
| 63 | char filename[PATH_MAX]; |
| 64 | }; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 65 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 66 | struct comm_event { |
| 67 | struct perf_event_header header; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 68 | __u32 pid, tid; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 69 | char comm[16]; |
| 70 | }; |
| 71 | |
| 72 | typedef union event_union { |
| 73 | struct perf_event_header header; |
| 74 | struct ip_event ip; |
| 75 | struct mmap_event mmap; |
| 76 | struct comm_event comm; |
| 77 | } event_t; |
| 78 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 79 | static LIST_HEAD(dsos); |
| 80 | static struct dso *kernel_dso; |
| 81 | |
| 82 | static void dsos__add(struct dso *dso) |
| 83 | { |
| 84 | list_add_tail(&dso->node, &dsos); |
| 85 | } |
| 86 | |
| 87 | static struct dso *dsos__find(const char *name) |
| 88 | { |
| 89 | struct dso *pos; |
| 90 | |
| 91 | list_for_each_entry(pos, &dsos, node) |
| 92 | if (strcmp(pos->name, name) == 0) |
| 93 | return pos; |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | static struct dso *dsos__findnew(const char *name) |
| 98 | { |
| 99 | struct dso *dso = dsos__find(name); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 100 | int nr; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 101 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 102 | if (dso) |
| 103 | return dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 104 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 105 | dso = dso__new(name, 0); |
| 106 | if (!dso) |
| 107 | goto out_delete_dso; |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 108 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 109 | nr = dso__load(dso, NULL, verbose); |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 110 | if (nr < 0) { |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 111 | if (verbose) |
| 112 | fprintf(stderr, "Failed to open: %s\n", name); |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 113 | goto out_delete_dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 114 | } |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 115 | if (!nr && verbose) { |
| 116 | fprintf(stderr, |
| 117 | "No symbols found in: %s, maybe install a debug package?\n", |
| 118 | name); |
| 119 | } |
| 120 | |
| 121 | dsos__add(dso); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 122 | |
| 123 | return dso; |
| 124 | |
| 125 | out_delete_dso: |
| 126 | dso__delete(dso); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 130 | static void dsos__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 131 | { |
| 132 | struct dso *pos; |
| 133 | |
| 134 | list_for_each_entry(pos, &dsos, node) |
| 135 | dso__fprintf(pos, fp); |
| 136 | } |
| 137 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 138 | static int load_kernel(void) |
| 139 | { |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 140 | int err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 141 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 142 | kernel_dso = dso__new("[kernel]", 0); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 143 | if (!kernel_dso) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 144 | return -1; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 145 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 146 | err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 147 | if (err) { |
| 148 | dso__delete(kernel_dso); |
| 149 | kernel_dso = NULL; |
| 150 | } else |
| 151 | dsos__add(kernel_dso); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 152 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 153 | return err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 156 | static char __cwd[PATH_MAX]; |
| 157 | static char *cwd = __cwd; |
| 158 | static int cwdlen; |
| 159 | |
| 160 | static int strcommon(const char *pathname) |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 161 | { |
| 162 | int n = 0; |
| 163 | |
| 164 | while (pathname[n] == cwd[n] && n < cwdlen) |
| 165 | ++n; |
| 166 | |
| 167 | return n; |
| 168 | } |
| 169 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 170 | struct map { |
| 171 | struct list_head node; |
| 172 | uint64_t start; |
| 173 | uint64_t end; |
| 174 | uint64_t pgoff; |
| 175 | struct dso *dso; |
| 176 | }; |
| 177 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 178 | static struct map *map__new(struct mmap_event *event) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 179 | { |
| 180 | struct map *self = malloc(sizeof(*self)); |
| 181 | |
| 182 | if (self != NULL) { |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 183 | const char *filename = event->filename; |
| 184 | char newfilename[PATH_MAX]; |
| 185 | |
| 186 | if (cwd) { |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 187 | int n = strcommon(filename); |
| 188 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 189 | if (n == cwdlen) { |
| 190 | snprintf(newfilename, sizeof(newfilename), |
| 191 | ".%s", filename + n); |
| 192 | filename = newfilename; |
| 193 | } |
| 194 | } |
| 195 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 196 | self->start = event->start; |
| 197 | self->end = event->start + event->len; |
| 198 | self->pgoff = event->pgoff; |
| 199 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 200 | self->dso = dsos__findnew(filename); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 201 | if (self->dso == NULL) |
| 202 | goto out_delete; |
| 203 | } |
| 204 | return self; |
| 205 | out_delete: |
| 206 | free(self); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 210 | struct thread; |
| 211 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 212 | struct thread { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 213 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 214 | struct list_head maps; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 215 | pid_t pid; |
| 216 | char *comm; |
| 217 | }; |
| 218 | |
| 219 | static struct thread *thread__new(pid_t pid) |
| 220 | { |
| 221 | struct thread *self = malloc(sizeof(*self)); |
| 222 | |
| 223 | if (self != NULL) { |
| 224 | self->pid = pid; |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 225 | self->comm = malloc(32); |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 226 | if (self->comm) |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 227 | snprintf(self->comm, 32, ":%d", self->pid); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 228 | INIT_LIST_HEAD(&self->maps); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return self; |
| 232 | } |
| 233 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 234 | static int thread__set_comm(struct thread *self, const char *comm) |
| 235 | { |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 236 | if (self->comm) |
| 237 | free(self->comm); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 238 | self->comm = strdup(comm); |
| 239 | return self->comm ? 0 : -ENOMEM; |
| 240 | } |
| 241 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 242 | static struct rb_root threads; |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 243 | static struct thread *last_match; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 244 | |
| 245 | static struct thread *threads__findnew(pid_t pid) |
| 246 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 247 | struct rb_node **p = &threads.rb_node; |
| 248 | struct rb_node *parent = NULL; |
| 249 | struct thread *th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 250 | |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 251 | /* |
| 252 | * Font-end cache - PID lookups come in blocks, |
| 253 | * so most of the time we dont have to look up |
| 254 | * the full rbtree: |
| 255 | */ |
| 256 | if (last_match && last_match->pid == pid) |
| 257 | return last_match; |
| 258 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 259 | while (*p != NULL) { |
| 260 | parent = *p; |
| 261 | th = rb_entry(parent, struct thread, rb_node); |
| 262 | |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 263 | if (th->pid == pid) { |
| 264 | last_match = th; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 265 | return th; |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 266 | } |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 267 | |
| 268 | if (pid < th->pid) |
| 269 | p = &(*p)->rb_left; |
| 270 | else |
| 271 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 272 | } |
| 273 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 274 | th = thread__new(pid); |
| 275 | if (th != NULL) { |
| 276 | rb_link_node(&th->rb_node, parent, p); |
| 277 | rb_insert_color(&th->rb_node, &threads); |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 278 | last_match = th; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 279 | } |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 280 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 281 | return th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void thread__insert_map(struct thread *self, struct map *map) |
| 285 | { |
| 286 | list_add_tail(&map->node, &self->maps); |
| 287 | } |
| 288 | |
| 289 | static struct map *thread__find_map(struct thread *self, uint64_t ip) |
| 290 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 291 | struct map *pos; |
| 292 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 293 | if (self == NULL) |
| 294 | return NULL; |
| 295 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 296 | list_for_each_entry(pos, &self->maps, node) |
| 297 | if (ip >= pos->start && ip <= pos->end) |
| 298 | return pos; |
| 299 | |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 303 | /* |
| 304 | * histogram, sorted on item, collects counts |
| 305 | */ |
| 306 | |
| 307 | static struct rb_root hist; |
| 308 | |
| 309 | struct hist_entry { |
| 310 | struct rb_node rb_node; |
| 311 | |
| 312 | struct thread *thread; |
| 313 | struct map *map; |
| 314 | struct dso *dso; |
| 315 | struct symbol *sym; |
| 316 | uint64_t ip; |
| 317 | char level; |
| 318 | |
| 319 | uint32_t count; |
| 320 | }; |
| 321 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 322 | /* |
| 323 | * configurable sorting bits |
| 324 | */ |
| 325 | |
| 326 | struct sort_entry { |
| 327 | struct list_head list; |
| 328 | |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 329 | char *header; |
| 330 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 331 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 332 | int64_t (*collapse)(struct hist_entry *, struct hist_entry *); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 333 | size_t (*print)(FILE *fp, struct hist_entry *); |
| 334 | }; |
| 335 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 336 | /* --sort pid */ |
| 337 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 338 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 339 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 340 | { |
| 341 | return right->thread->pid - left->thread->pid; |
| 342 | } |
| 343 | |
| 344 | static size_t |
| 345 | sort__thread_print(FILE *fp, struct hist_entry *self) |
| 346 | { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 347 | return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static struct sort_entry sort_thread = { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 351 | .header = " Command: Pid", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 352 | .cmp = sort__thread_cmp, |
| 353 | .print = sort__thread_print, |
| 354 | }; |
| 355 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 356 | /* --sort comm */ |
| 357 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 358 | static int64_t |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 359 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 360 | { |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 361 | return right->thread->pid - left->thread->pid; |
| 362 | } |
| 363 | |
| 364 | static int64_t |
| 365 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) |
| 366 | { |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 367 | char *comm_l = left->thread->comm; |
| 368 | char *comm_r = right->thread->comm; |
| 369 | |
| 370 | if (!comm_l || !comm_r) { |
| 371 | if (!comm_l && !comm_r) |
| 372 | return 0; |
| 373 | else if (!comm_l) |
| 374 | return -1; |
| 375 | else |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | return strcmp(comm_l, comm_r); |
| 380 | } |
| 381 | |
| 382 | static size_t |
| 383 | sort__comm_print(FILE *fp, struct hist_entry *self) |
| 384 | { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 385 | return fprintf(fp, "%16s", self->thread->comm); |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static struct sort_entry sort_comm = { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 389 | .header = " Command", |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 390 | .cmp = sort__comm_cmp, |
| 391 | .collapse = sort__comm_collapse, |
| 392 | .print = sort__comm_print, |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 393 | }; |
| 394 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 395 | /* --sort dso */ |
| 396 | |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 397 | static int64_t |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 398 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
| 399 | { |
| 400 | struct dso *dso_l = left->dso; |
| 401 | struct dso *dso_r = right->dso; |
| 402 | |
| 403 | if (!dso_l || !dso_r) { |
| 404 | if (!dso_l && !dso_r) |
| 405 | return 0; |
| 406 | else if (!dso_l) |
| 407 | return -1; |
| 408 | else |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | return strcmp(dso_l->name, dso_r->name); |
| 413 | } |
| 414 | |
| 415 | static size_t |
| 416 | sort__dso_print(FILE *fp, struct hist_entry *self) |
| 417 | { |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 418 | if (self->dso) |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 419 | return fprintf(fp, "%-25s", self->dso->name); |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 420 | |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 421 | return fprintf(fp, "%016llx ", (__u64)self->ip); |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static struct sort_entry sort_dso = { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 425 | .header = "Shared Object ", |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 426 | .cmp = sort__dso_cmp, |
| 427 | .print = sort__dso_print, |
| 428 | }; |
| 429 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 430 | /* --sort symbol */ |
| 431 | |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 432 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 433 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 434 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 435 | uint64_t ip_l, ip_r; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 436 | |
| 437 | if (left->sym == right->sym) |
| 438 | return 0; |
| 439 | |
| 440 | ip_l = left->sym ? left->sym->start : left->ip; |
| 441 | ip_r = right->sym ? right->sym->start : right->ip; |
| 442 | |
| 443 | return (int64_t)(ip_r - ip_l); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 444 | } |
| 445 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 446 | static size_t |
| 447 | sort__sym_print(FILE *fp, struct hist_entry *self) |
| 448 | { |
| 449 | size_t ret = 0; |
| 450 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 451 | if (verbose) |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 452 | ret += fprintf(fp, "%#018llx ", (__u64)self->ip); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 453 | |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 454 | if (self->sym) |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 455 | ret += fprintf(fp, "%s", self->sym->name); |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 456 | else |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 457 | ret += fprintf(fp, "%#016llx", (__u64)self->ip); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 458 | |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | static struct sort_entry sort_sym = { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 463 | .header = "Symbol", |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 464 | .cmp = sort__sym_cmp, |
| 465 | .print = sort__sym_print, |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 466 | }; |
| 467 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 468 | static int sort__need_collapse = 0; |
| 469 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 470 | struct sort_dimension { |
| 471 | char *name; |
| 472 | struct sort_entry *entry; |
| 473 | int taken; |
| 474 | }; |
| 475 | |
| 476 | static struct sort_dimension sort_dimensions[] = { |
| 477 | { .name = "pid", .entry = &sort_thread, }, |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 478 | { .name = "comm", .entry = &sort_comm, }, |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 479 | { .name = "dso", .entry = &sort_dso, }, |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 480 | { .name = "symbol", .entry = &sort_sym, }, |
| 481 | }; |
| 482 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 483 | static LIST_HEAD(hist_entry__sort_list); |
| 484 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 485 | static int sort_dimension__add(char *tok) |
| 486 | { |
| 487 | int i; |
| 488 | |
| 489 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { |
| 490 | struct sort_dimension *sd = &sort_dimensions[i]; |
| 491 | |
| 492 | if (sd->taken) |
| 493 | continue; |
| 494 | |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame] | 495 | if (strncasecmp(tok, sd->name, strlen(tok))) |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 496 | continue; |
| 497 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 498 | if (sd->entry->collapse) |
| 499 | sort__need_collapse = 1; |
| 500 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 501 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
| 502 | sd->taken = 1; |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame] | 503 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | return -ESRCH; |
| 508 | } |
| 509 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 510 | static int64_t |
| 511 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 512 | { |
| 513 | struct sort_entry *se; |
| 514 | int64_t cmp = 0; |
| 515 | |
| 516 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 517 | cmp = se->cmp(left, right); |
| 518 | if (cmp) |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | return cmp; |
| 523 | } |
| 524 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 525 | static int64_t |
| 526 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 527 | { |
| 528 | struct sort_entry *se; |
| 529 | int64_t cmp = 0; |
| 530 | |
| 531 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 532 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 533 | |
| 534 | f = se->collapse ?: se->cmp; |
| 535 | |
| 536 | cmp = f(left, right); |
| 537 | if (cmp) |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | return cmp; |
| 542 | } |
| 543 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 544 | static size_t |
| 545 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples) |
| 546 | { |
| 547 | struct sort_entry *se; |
| 548 | size_t ret; |
| 549 | |
| 550 | if (total_samples) { |
Ingo Molnar | e98e96f | 2009-06-03 19:30:38 +0200 | [diff] [blame] | 551 | ret = fprintf(fp, " %6.2f%%", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 552 | (self->count * 100.0) / total_samples); |
| 553 | } else |
| 554 | ret = fprintf(fp, "%12d ", self->count); |
| 555 | |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 556 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 557 | fprintf(fp, " "); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 558 | ret += se->print(fp, self); |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 559 | } |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 560 | |
| 561 | ret += fprintf(fp, "\n"); |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * collect histogram counts |
| 568 | */ |
| 569 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 570 | static int |
| 571 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, |
| 572 | struct symbol *sym, uint64_t ip, char level) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 573 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 574 | struct rb_node **p = &hist.rb_node; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 575 | struct rb_node *parent = NULL; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 576 | struct hist_entry *he; |
| 577 | struct hist_entry entry = { |
| 578 | .thread = thread, |
| 579 | .map = map, |
| 580 | .dso = dso, |
| 581 | .sym = sym, |
| 582 | .ip = ip, |
| 583 | .level = level, |
| 584 | .count = 1, |
| 585 | }; |
| 586 | int cmp; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 587 | |
| 588 | while (*p != NULL) { |
| 589 | parent = *p; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 590 | he = rb_entry(parent, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 591 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 592 | cmp = hist_entry__cmp(&entry, he); |
| 593 | |
| 594 | if (!cmp) { |
| 595 | he->count++; |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | if (cmp < 0) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 600 | p = &(*p)->rb_left; |
| 601 | else |
| 602 | p = &(*p)->rb_right; |
| 603 | } |
| 604 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 605 | he = malloc(sizeof(*he)); |
| 606 | if (!he) |
| 607 | return -ENOMEM; |
| 608 | *he = entry; |
| 609 | rb_link_node(&he->rb_node, parent, p); |
| 610 | rb_insert_color(&he->rb_node, &hist); |
| 611 | |
| 612 | return 0; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 613 | } |
| 614 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 615 | static void hist_entry__free(struct hist_entry *he) |
| 616 | { |
| 617 | free(he); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * collapse the histogram |
| 622 | */ |
| 623 | |
| 624 | static struct rb_root collapse_hists; |
| 625 | |
| 626 | static void collapse__insert_entry(struct hist_entry *he) |
| 627 | { |
| 628 | struct rb_node **p = &collapse_hists.rb_node; |
| 629 | struct rb_node *parent = NULL; |
| 630 | struct hist_entry *iter; |
| 631 | int64_t cmp; |
| 632 | |
| 633 | while (*p != NULL) { |
| 634 | parent = *p; |
| 635 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 636 | |
| 637 | cmp = hist_entry__collapse(iter, he); |
| 638 | |
| 639 | if (!cmp) { |
| 640 | iter->count += he->count; |
| 641 | hist_entry__free(he); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | if (cmp < 0) |
| 646 | p = &(*p)->rb_left; |
| 647 | else |
| 648 | p = &(*p)->rb_right; |
| 649 | } |
| 650 | |
| 651 | rb_link_node(&he->rb_node, parent, p); |
| 652 | rb_insert_color(&he->rb_node, &collapse_hists); |
| 653 | } |
| 654 | |
| 655 | static void collapse__resort(void) |
| 656 | { |
| 657 | struct rb_node *next; |
| 658 | struct hist_entry *n; |
| 659 | |
| 660 | if (!sort__need_collapse) |
| 661 | return; |
| 662 | |
| 663 | next = rb_first(&hist); |
| 664 | while (next) { |
| 665 | n = rb_entry(next, struct hist_entry, rb_node); |
| 666 | next = rb_next(&n->rb_node); |
| 667 | |
| 668 | rb_erase(&n->rb_node, &hist); |
| 669 | collapse__insert_entry(n); |
| 670 | } |
| 671 | } |
| 672 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 673 | /* |
| 674 | * reverse the map, sort on count. |
| 675 | */ |
| 676 | |
| 677 | static struct rb_root output_hists; |
| 678 | |
| 679 | static void output__insert_entry(struct hist_entry *he) |
| 680 | { |
| 681 | struct rb_node **p = &output_hists.rb_node; |
| 682 | struct rb_node *parent = NULL; |
| 683 | struct hist_entry *iter; |
| 684 | |
| 685 | while (*p != NULL) { |
| 686 | parent = *p; |
| 687 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 688 | |
| 689 | if (he->count > iter->count) |
| 690 | p = &(*p)->rb_left; |
| 691 | else |
| 692 | p = &(*p)->rb_right; |
| 693 | } |
| 694 | |
| 695 | rb_link_node(&he->rb_node, parent, p); |
| 696 | rb_insert_color(&he->rb_node, &output_hists); |
| 697 | } |
| 698 | |
| 699 | static void output__resort(void) |
| 700 | { |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 701 | struct rb_node *next; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 702 | struct hist_entry *n; |
Arnaldo Carvalho de Melo | a4c43be | 2009-06-03 23:02:33 -0300 | [diff] [blame] | 703 | struct rb_root *tree = &hist; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 704 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 705 | if (sort__need_collapse) |
Arnaldo Carvalho de Melo | a4c43be | 2009-06-03 23:02:33 -0300 | [diff] [blame] | 706 | tree = &collapse_hists; |
| 707 | |
| 708 | next = rb_first(tree); |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 709 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 710 | while (next) { |
| 711 | n = rb_entry(next, struct hist_entry, rb_node); |
| 712 | next = rb_next(&n->rb_node); |
| 713 | |
Arnaldo Carvalho de Melo | a4c43be | 2009-06-03 23:02:33 -0300 | [diff] [blame] | 714 | rb_erase(&n->rb_node, tree); |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 715 | output__insert_entry(n); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 719 | static size_t output__fprintf(FILE *fp, uint64_t total_samples) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 720 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 721 | struct hist_entry *pos; |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 722 | struct sort_entry *se; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 723 | struct rb_node *nd; |
| 724 | size_t ret = 0; |
| 725 | |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 726 | fprintf(fp, "\n"); |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 727 | fprintf(fp, "#\n"); |
Ingo Molnar | 05ca061 | 2009-06-04 14:21:16 +0200 | [diff] [blame] | 728 | fprintf(fp, "# (%Ld profiler events)\n", (__u64)total_samples); |
| 729 | fprintf(fp, "#\n"); |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 730 | |
| 731 | fprintf(fp, "# Overhead"); |
| 732 | list_for_each_entry(se, &hist_entry__sort_list, list) |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 733 | fprintf(fp, " %s", se->header); |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 734 | fprintf(fp, "\n"); |
| 735 | |
| 736 | fprintf(fp, "# ........"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 737 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 738 | int i; |
| 739 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 740 | fprintf(fp, " "); |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 741 | for (i = 0; i < strlen(se->header); i++) |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 742 | fprintf(fp, "."); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 743 | } |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 744 | fprintf(fp, "\n"); |
| 745 | |
| 746 | fprintf(fp, "#\n"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 747 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 748 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
| 749 | pos = rb_entry(nd, struct hist_entry, rb_node); |
| 750 | ret += hist_entry__fprintf(fp, pos, total_samples); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 751 | } |
| 752 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 753 | if (!strcmp(sort_order, default_sort_order)) { |
| 754 | fprintf(fp, "#\n"); |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 755 | fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n"); |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 756 | fprintf(fp, "#\n"); |
| 757 | } |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame^] | 758 | fprintf(fp, "\n"); |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 759 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
Peter Zijlstra | 436224a | 2009-06-02 21:02:36 +0200 | [diff] [blame] | 763 | static void register_idle_thread(void) |
| 764 | { |
| 765 | struct thread *thread = threads__findnew(0); |
| 766 | |
| 767 | if (thread == NULL || |
| 768 | thread__set_comm(thread, "[idle]")) { |
| 769 | fprintf(stderr, "problem inserting idle task.\n"); |
| 770 | exit(-1); |
| 771 | } |
| 772 | } |
| 773 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 774 | static unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 775 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 776 | static int |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 777 | process_overflow_event(event_t *event, unsigned long offset, unsigned long head) |
| 778 | { |
| 779 | char level; |
| 780 | int show = 0; |
| 781 | struct dso *dso = NULL; |
| 782 | struct thread *thread = threads__findnew(event->ip.pid); |
| 783 | uint64_t ip = event->ip.ip; |
| 784 | struct map *map = NULL; |
| 785 | |
| 786 | dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
| 787 | (void *)(offset + head), |
| 788 | (void *)(long)(event->header.size), |
| 789 | event->header.misc, |
| 790 | event->ip.pid, |
| 791 | (void *)(long)ip); |
| 792 | |
| 793 | dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 794 | |
| 795 | if (thread == NULL) { |
| 796 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
| 797 | event->header.type); |
| 798 | return -1; |
| 799 | } |
| 800 | |
| 801 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
| 802 | show = SHOW_KERNEL; |
| 803 | level = 'k'; |
| 804 | |
| 805 | dso = kernel_dso; |
| 806 | |
| 807 | dprintf(" ...... dso: %s\n", dso->name); |
| 808 | |
| 809 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
| 810 | |
| 811 | show = SHOW_USER; |
| 812 | level = '.'; |
| 813 | |
| 814 | map = thread__find_map(thread, ip); |
| 815 | if (map != NULL) { |
| 816 | dso = map->dso; |
| 817 | ip -= map->start + map->pgoff; |
| 818 | } else { |
| 819 | /* |
| 820 | * If this is outside of all known maps, |
| 821 | * and is a negative address, try to look it |
| 822 | * up in the kernel dso, as it might be a |
| 823 | * vsyscall (which executes in user-mode): |
| 824 | */ |
| 825 | if ((long long)ip < 0) |
| 826 | dso = kernel_dso; |
| 827 | } |
| 828 | dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); |
| 829 | |
| 830 | } else { |
| 831 | show = SHOW_HV; |
| 832 | level = 'H'; |
| 833 | dprintf(" ...... dso: [hypervisor]\n"); |
| 834 | } |
| 835 | |
| 836 | if (show & show_mask) { |
| 837 | struct symbol *sym = dso__find_symbol(dso, ip); |
| 838 | |
| 839 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
| 840 | fprintf(stderr, |
| 841 | "problem incrementing symbol count, skipping event\n"); |
| 842 | return -1; |
| 843 | } |
| 844 | } |
| 845 | total++; |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | static int |
| 851 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) |
| 852 | { |
| 853 | struct thread *thread = threads__findnew(event->mmap.pid); |
| 854 | struct map *map = map__new(&event->mmap); |
| 855 | |
| 856 | dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
| 857 | (void *)(offset + head), |
| 858 | (void *)(long)(event->header.size), |
| 859 | (void *)(long)event->mmap.start, |
| 860 | (void *)(long)event->mmap.len, |
| 861 | (void *)(long)event->mmap.pgoff, |
| 862 | event->mmap.filename); |
| 863 | |
| 864 | if (thread == NULL || map == NULL) { |
| 865 | dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n"); |
Ingo Molnar | df97992 | 2009-06-04 13:41:22 +0200 | [diff] [blame] | 866 | return 0; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | thread__insert_map(thread, map); |
| 870 | total_mmap++; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int |
| 876 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 877 | { |
| 878 | struct thread *thread = threads__findnew(event->comm.pid); |
| 879 | |
| 880 | dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 881 | (void *)(offset + head), |
| 882 | (void *)(long)(event->header.size), |
| 883 | event->comm.comm, event->comm.pid); |
| 884 | |
| 885 | if (thread == NULL || |
| 886 | thread__set_comm(thread, event->comm.comm)) { |
| 887 | dprintf("problem processing PERF_EVENT_COMM, skipping event.\n"); |
| 888 | return -1; |
| 889 | } |
| 890 | total_comm++; |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static int |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 896 | process_event(event_t *event, unsigned long offset, unsigned long head) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 897 | { |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 898 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) |
| 899 | return process_overflow_event(event, offset, head); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 900 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 901 | switch (event->header.type) { |
| 902 | case PERF_EVENT_MMAP: |
| 903 | return process_mmap_event(event, offset, head); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 904 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 905 | case PERF_EVENT_COMM: |
| 906 | return process_comm_event(event, offset, head); |
Ingo Molnar | ed966aa | 2009-06-03 10:39:26 +0200 | [diff] [blame] | 907 | |
Ingo Molnar | d11444d | 2009-06-03 23:29:14 +0200 | [diff] [blame] | 908 | /* |
| 909 | * We dont process them right now but they are fine: |
| 910 | */ |
| 911 | case PERF_EVENT_MUNMAP: |
| 912 | case PERF_EVENT_PERIOD: |
| 913 | case PERF_EVENT_THROTTLE: |
| 914 | case PERF_EVENT_UNTHROTTLE: |
| 915 | return 0; |
| 916 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 917 | default: |
| 918 | return -1; |
| 919 | } |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | static int __cmd_report(void) |
| 925 | { |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 926 | int ret, rc = EXIT_FAILURE; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 927 | unsigned long offset = 0; |
| 928 | unsigned long head = 0; |
| 929 | struct stat stat; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 930 | event_t *event; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 931 | uint32_t size; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 932 | char *buf; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 933 | |
| 934 | register_idle_thread(); |
| 935 | |
| 936 | input = open(input_name, O_RDONLY); |
| 937 | if (input < 0) { |
| 938 | perror("failed to open file"); |
| 939 | exit(-1); |
| 940 | } |
| 941 | |
| 942 | ret = fstat(input, &stat); |
| 943 | if (ret < 0) { |
| 944 | perror("failed to stat file"); |
| 945 | exit(-1); |
| 946 | } |
| 947 | |
| 948 | if (!stat.st_size) { |
| 949 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 950 | exit(0); |
| 951 | } |
| 952 | |
| 953 | if (load_kernel() < 0) { |
| 954 | perror("failed to load kernel symbols"); |
| 955 | return EXIT_FAILURE; |
| 956 | } |
| 957 | |
| 958 | if (!full_paths) { |
| 959 | if (getcwd(__cwd, sizeof(__cwd)) == NULL) { |
| 960 | perror("failed to get the current directory"); |
| 961 | return EXIT_FAILURE; |
| 962 | } |
| 963 | cwdlen = strlen(cwd); |
| 964 | } else { |
| 965 | cwd = NULL; |
| 966 | cwdlen = 0; |
| 967 | } |
| 968 | remap: |
| 969 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 970 | MAP_SHARED, input, offset); |
| 971 | if (buf == MAP_FAILED) { |
| 972 | perror("failed to mmap file"); |
| 973 | exit(-1); |
| 974 | } |
| 975 | |
| 976 | more: |
| 977 | event = (event_t *)(buf + head); |
| 978 | |
| 979 | size = event->header.size; |
| 980 | if (!size) |
| 981 | size = 8; |
| 982 | |
| 983 | if (head + event->header.size >= page_size * mmap_window) { |
| 984 | unsigned long shift = page_size * (head / page_size); |
| 985 | int ret; |
| 986 | |
| 987 | ret = munmap(buf, page_size * mmap_window); |
| 988 | assert(ret == 0); |
| 989 | |
| 990 | offset += shift; |
| 991 | head -= shift; |
| 992 | goto remap; |
| 993 | } |
| 994 | |
| 995 | size = event->header.size; |
| 996 | |
| 997 | if (!size || process_event(event, offset, head) < 0) { |
| 998 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 999 | dprintf("%p [%p]: skipping unknown header type: %d\n", |
| 1000 | (void *)(offset + head), |
| 1001 | (void *)(long)(event->header.size), |
| 1002 | event->header.type); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 1003 | |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 1004 | total_unknown++; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1005 | |
| 1006 | /* |
| 1007 | * assume we lost track of the stream, check alignment, and |
| 1008 | * increment a single u64 in the hope to catch on again 'soon'. |
| 1009 | */ |
| 1010 | |
| 1011 | if (unlikely(head & 7)) |
| 1012 | head &= ~7ULL; |
| 1013 | |
| 1014 | size = 8; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1015 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1016 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1017 | head += size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 1018 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1019 | if (offset + head < stat.st_size) |
| 1020 | goto more; |
| 1021 | |
| 1022 | rc = EXIT_SUCCESS; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1023 | close(input); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1024 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 1025 | dprintf(" IP events: %10ld\n", total); |
| 1026 | dprintf(" mmap events: %10ld\n", total_mmap); |
| 1027 | dprintf(" comm events: %10ld\n", total_comm); |
| 1028 | dprintf(" unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1029 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 1030 | if (dump_trace) |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1031 | return 0; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1032 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1033 | if (verbose >= 2) |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1034 | dsos__fprintf(stdout); |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1035 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 1036 | collapse__resort(); |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1037 | output__resort(); |
| 1038 | output__fprintf(stdout, total); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1039 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1040 | return rc; |
| 1041 | } |
| 1042 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1043 | static const char * const report_usage[] = { |
| 1044 | "perf report [<options>] <command>", |
| 1045 | NULL |
| 1046 | }; |
| 1047 | |
| 1048 | static const struct option options[] = { |
| 1049 | OPT_STRING('i', "input", &input_name, "file", |
| 1050 | "input file name"), |
Arnaldo Carvalho de Melo | 815e777 | 2009-05-26 19:46:14 -0300 | [diff] [blame] | 1051 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1052 | "be more verbose (show symbol address, etc)"), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1053 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1054 | "dump raw trace in ASCII"), |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 1055 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
Ingo Molnar | 63299f0 | 2009-05-28 10:52:00 +0200 | [diff] [blame] | 1056 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
| 1057 | "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"), |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 1058 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 1059 | "Don't shorten the pathnames taking into account the cwd"), |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1060 | OPT_END() |
| 1061 | }; |
| 1062 | |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame] | 1063 | static void setup_sorting(void) |
| 1064 | { |
| 1065 | char *tmp, *tok, *str = strdup(sort_order); |
| 1066 | |
| 1067 | for (tok = strtok_r(str, ", ", &tmp); |
| 1068 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 1069 | if (sort_dimension__add(tok) < 0) { |
| 1070 | error("Unknown --sort key: `%s'", tok); |
| 1071 | usage_with_options(report_usage, options); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | free(str); |
| 1076 | } |
| 1077 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1078 | int cmd_report(int argc, const char **argv, const char *prefix) |
| 1079 | { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1080 | symbol__init(); |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1081 | |
| 1082 | page_size = getpagesize(); |
| 1083 | |
| 1084 | parse_options(argc, argv, options, report_usage, 0); |
| 1085 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 1086 | setup_sorting(); |
| 1087 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 1088 | setup_pager(); |
| 1089 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1090 | return __cmd_report(); |
| 1091 | } |