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