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