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