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