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