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