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