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