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