Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1 | #include "util/util.h" |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 2 | #include "builtin.h" |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 3 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 4 | #include "util/list.h" |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 5 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 6 | #include "util/rbtree.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 7 | #include "util/symbol.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 8 | #include "util/string.h" |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 9 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 10 | #include "perf.h" |
| 11 | |
| 12 | #include "util/parse-options.h" |
| 13 | #include "util/parse-events.h" |
| 14 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 15 | #define SHOW_KERNEL 1 |
| 16 | #define SHOW_USER 2 |
| 17 | #define SHOW_HV 4 |
| 18 | |
Ingo Molnar | 23ac9cb | 2009-05-27 09:33:18 +0200 | [diff] [blame] | 19 | static char const *input_name = "perf.data"; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 20 | static char *vmlinux = NULL; |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 21 | static char *sort_order = "comm,dso"; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 22 | static int input; |
| 23 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 24 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 25 | static int dump_trace = 0; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 26 | static int verbose; |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 27 | static int full_paths; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 28 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 29 | static unsigned long page_size; |
| 30 | static unsigned long mmap_window = 32; |
| 31 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 32 | const char *perf_event_names[] = { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 33 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
| 34 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", |
| 35 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", |
| 36 | }; |
| 37 | |
| 38 | struct ip_event { |
| 39 | struct perf_event_header header; |
| 40 | __u64 ip; |
| 41 | __u32 pid, tid; |
| 42 | }; |
| 43 | struct mmap_event { |
| 44 | struct perf_event_header header; |
| 45 | __u32 pid, tid; |
| 46 | __u64 start; |
| 47 | __u64 len; |
| 48 | __u64 pgoff; |
| 49 | char filename[PATH_MAX]; |
| 50 | }; |
| 51 | struct comm_event { |
| 52 | struct perf_event_header header; |
| 53 | __u32 pid,tid; |
| 54 | char comm[16]; |
| 55 | }; |
| 56 | |
| 57 | typedef union event_union { |
| 58 | struct perf_event_header header; |
| 59 | struct ip_event ip; |
| 60 | struct mmap_event mmap; |
| 61 | struct comm_event comm; |
| 62 | } event_t; |
| 63 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 64 | static LIST_HEAD(dsos); |
| 65 | static struct dso *kernel_dso; |
| 66 | |
| 67 | static void dsos__add(struct dso *dso) |
| 68 | { |
| 69 | list_add_tail(&dso->node, &dsos); |
| 70 | } |
| 71 | |
| 72 | static struct dso *dsos__find(const char *name) |
| 73 | { |
| 74 | struct dso *pos; |
| 75 | |
| 76 | list_for_each_entry(pos, &dsos, node) |
| 77 | if (strcmp(pos->name, name) == 0) |
| 78 | return pos; |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static struct dso *dsos__findnew(const char *name) |
| 83 | { |
| 84 | struct dso *dso = dsos__find(name); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 85 | int nr; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 86 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 87 | if (dso) |
| 88 | return dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 89 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 90 | dso = dso__new(name, 0); |
| 91 | if (!dso) |
| 92 | goto out_delete_dso; |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 93 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 94 | nr = dso__load(dso, NULL); |
| 95 | if (nr < 0) { |
| 96 | fprintf(stderr, "Failed to open: %s\n", name); |
| 97 | goto out_delete_dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 98 | } |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 99 | if (!nr && verbose) { |
| 100 | fprintf(stderr, |
| 101 | "No symbols found in: %s, maybe install a debug package?\n", |
| 102 | name); |
| 103 | } |
| 104 | |
| 105 | dsos__add(dso); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 106 | |
| 107 | return dso; |
| 108 | |
| 109 | out_delete_dso: |
| 110 | dso__delete(dso); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 114 | static void dsos__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 115 | { |
| 116 | struct dso *pos; |
| 117 | |
| 118 | list_for_each_entry(pos, &dsos, node) |
| 119 | dso__fprintf(pos, fp); |
| 120 | } |
| 121 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 122 | static int load_kernel(void) |
| 123 | { |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 124 | int err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 125 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 126 | kernel_dso = dso__new("[kernel]", 0); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 127 | if (!kernel_dso) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 128 | return -1; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 129 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 130 | err = dso__load_kernel(kernel_dso, vmlinux, NULL); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 131 | if (err) { |
| 132 | dso__delete(kernel_dso); |
| 133 | kernel_dso = NULL; |
| 134 | } else |
| 135 | dsos__add(kernel_dso); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 136 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 137 | return err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 140 | static int strcommon(const char *pathname, const char *cwd, int cwdlen) |
| 141 | { |
| 142 | int n = 0; |
| 143 | |
| 144 | while (pathname[n] == cwd[n] && n < cwdlen) |
| 145 | ++n; |
| 146 | |
| 147 | return n; |
| 148 | } |
| 149 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 150 | struct map { |
| 151 | struct list_head node; |
| 152 | uint64_t start; |
| 153 | uint64_t end; |
| 154 | uint64_t pgoff; |
| 155 | struct dso *dso; |
| 156 | }; |
| 157 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 158 | static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 159 | { |
| 160 | struct map *self = malloc(sizeof(*self)); |
| 161 | |
| 162 | if (self != NULL) { |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 163 | const char *filename = event->filename; |
| 164 | char newfilename[PATH_MAX]; |
| 165 | |
| 166 | if (cwd) { |
| 167 | int n = strcommon(filename, cwd, cwdlen); |
| 168 | if (n == cwdlen) { |
| 169 | snprintf(newfilename, sizeof(newfilename), |
| 170 | ".%s", filename + n); |
| 171 | filename = newfilename; |
| 172 | } |
| 173 | } |
| 174 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 175 | self->start = event->start; |
| 176 | self->end = event->start + event->len; |
| 177 | self->pgoff = event->pgoff; |
| 178 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 179 | self->dso = dsos__findnew(filename); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 180 | if (self->dso == NULL) |
| 181 | goto out_delete; |
| 182 | } |
| 183 | return self; |
| 184 | out_delete: |
| 185 | free(self); |
| 186 | return NULL; |
| 187 | } |
| 188 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 189 | struct thread; |
| 190 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 191 | struct thread { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 192 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 193 | struct list_head maps; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 194 | pid_t pid; |
| 195 | char *comm; |
| 196 | }; |
| 197 | |
| 198 | static struct thread *thread__new(pid_t pid) |
| 199 | { |
| 200 | struct thread *self = malloc(sizeof(*self)); |
| 201 | |
| 202 | if (self != NULL) { |
| 203 | self->pid = pid; |
| 204 | self->comm = NULL; |
| 205 | INIT_LIST_HEAD(&self->maps); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return self; |
| 209 | } |
| 210 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 211 | static int thread__set_comm(struct thread *self, const char *comm) |
| 212 | { |
| 213 | self->comm = strdup(comm); |
| 214 | return self->comm ? 0 : -ENOMEM; |
| 215 | } |
| 216 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 217 | static struct rb_root threads; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 218 | |
| 219 | static struct thread *threads__findnew(pid_t pid) |
| 220 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 221 | struct rb_node **p = &threads.rb_node; |
| 222 | struct rb_node *parent = NULL; |
| 223 | struct thread *th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 224 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 225 | while (*p != NULL) { |
| 226 | parent = *p; |
| 227 | th = rb_entry(parent, struct thread, rb_node); |
| 228 | |
| 229 | if (th->pid == pid) |
| 230 | return th; |
| 231 | |
| 232 | if (pid < th->pid) |
| 233 | p = &(*p)->rb_left; |
| 234 | else |
| 235 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 236 | } |
| 237 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 238 | th = thread__new(pid); |
| 239 | if (th != NULL) { |
| 240 | rb_link_node(&th->rb_node, parent, p); |
| 241 | rb_insert_color(&th->rb_node, &threads); |
| 242 | } |
| 243 | return th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void thread__insert_map(struct thread *self, struct map *map) |
| 247 | { |
| 248 | list_add_tail(&map->node, &self->maps); |
| 249 | } |
| 250 | |
| 251 | static struct map *thread__find_map(struct thread *self, uint64_t ip) |
| 252 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 253 | struct map *pos; |
| 254 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 255 | if (self == NULL) |
| 256 | return NULL; |
| 257 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 258 | list_for_each_entry(pos, &self->maps, node) |
| 259 | if (ip >= pos->start && ip <= pos->end) |
| 260 | return pos; |
| 261 | |
| 262 | return NULL; |
| 263 | } |
| 264 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 265 | /* |
| 266 | * histogram, sorted on item, collects counts |
| 267 | */ |
| 268 | |
| 269 | static struct rb_root hist; |
| 270 | |
| 271 | struct hist_entry { |
| 272 | struct rb_node rb_node; |
| 273 | |
| 274 | struct thread *thread; |
| 275 | struct map *map; |
| 276 | struct dso *dso; |
| 277 | struct symbol *sym; |
| 278 | uint64_t ip; |
| 279 | char level; |
| 280 | |
| 281 | uint32_t count; |
| 282 | }; |
| 283 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 284 | /* |
| 285 | * configurable sorting bits |
| 286 | */ |
| 287 | |
| 288 | struct sort_entry { |
| 289 | struct list_head list; |
| 290 | |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 291 | char *header; |
| 292 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 293 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
| 294 | size_t (*print)(FILE *fp, struct hist_entry *); |
| 295 | }; |
| 296 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 297 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 298 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 299 | { |
| 300 | return right->thread->pid - left->thread->pid; |
| 301 | } |
| 302 | |
| 303 | static size_t |
| 304 | sort__thread_print(FILE *fp, struct hist_entry *self) |
| 305 | { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 306 | return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static struct sort_entry sort_thread = { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 310 | .header = " Command: Pid ", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 311 | .cmp = sort__thread_cmp, |
| 312 | .print = sort__thread_print, |
| 313 | }; |
| 314 | |
| 315 | static int64_t |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 316 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 317 | { |
| 318 | char *comm_l = left->thread->comm; |
| 319 | char *comm_r = right->thread->comm; |
| 320 | |
| 321 | if (!comm_l || !comm_r) { |
| 322 | if (!comm_l && !comm_r) |
| 323 | return 0; |
| 324 | else if (!comm_l) |
| 325 | return -1; |
| 326 | else |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | return strcmp(comm_l, comm_r); |
| 331 | } |
| 332 | |
| 333 | static size_t |
| 334 | sort__comm_print(FILE *fp, struct hist_entry *self) |
| 335 | { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 336 | return fprintf(fp, " %16s", self->thread->comm ?: "<unknown>"); |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static struct sort_entry sort_comm = { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 340 | .header = " Command", |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 341 | .cmp = sort__comm_cmp, |
| 342 | .print = sort__comm_print, |
| 343 | }; |
| 344 | |
| 345 | static int64_t |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 346 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
| 347 | { |
| 348 | struct dso *dso_l = left->dso; |
| 349 | struct dso *dso_r = right->dso; |
| 350 | |
| 351 | if (!dso_l || !dso_r) { |
| 352 | if (!dso_l && !dso_r) |
| 353 | return 0; |
| 354 | else if (!dso_l) |
| 355 | return -1; |
| 356 | else |
| 357 | return 1; |
| 358 | } |
| 359 | |
| 360 | return strcmp(dso_l->name, dso_r->name); |
| 361 | } |
| 362 | |
| 363 | static size_t |
| 364 | sort__dso_print(FILE *fp, struct hist_entry *self) |
| 365 | { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 366 | return fprintf(fp, " %s", self->dso ? self->dso->name : "<unknown>"); |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static struct sort_entry sort_dso = { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 370 | .header = " Shared Object", |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 371 | .cmp = sort__dso_cmp, |
| 372 | .print = sort__dso_print, |
| 373 | }; |
| 374 | |
| 375 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 376 | 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] | 377 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 378 | uint64_t ip_l, ip_r; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 379 | |
| 380 | if (left->sym == right->sym) |
| 381 | return 0; |
| 382 | |
| 383 | ip_l = left->sym ? left->sym->start : left->ip; |
| 384 | ip_r = right->sym ? right->sym->start : right->ip; |
| 385 | |
| 386 | return (int64_t)(ip_r - ip_l); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 387 | } |
| 388 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 389 | static size_t |
| 390 | sort__sym_print(FILE *fp, struct hist_entry *self) |
| 391 | { |
| 392 | size_t ret = 0; |
| 393 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 394 | if (verbose) |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 395 | ret += fprintf(fp, " %#018llx", (unsigned long long)self->ip); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 396 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 397 | ret += fprintf(fp, " %s: %s", |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 398 | self->dso ? self->dso->name : "<unknown>", |
| 399 | self->sym ? self->sym->name : "<unknown>"); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 400 | |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | static struct sort_entry sort_sym = { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 405 | .header = " Shared Object: Symbol", |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 406 | .cmp = sort__sym_cmp, |
| 407 | .print = sort__sym_print, |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 408 | }; |
| 409 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 410 | struct sort_dimension { |
| 411 | char *name; |
| 412 | struct sort_entry *entry; |
| 413 | int taken; |
| 414 | }; |
| 415 | |
| 416 | static struct sort_dimension sort_dimensions[] = { |
| 417 | { .name = "pid", .entry = &sort_thread, }, |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 418 | { .name = "comm", .entry = &sort_comm, }, |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 419 | { .name = "dso", .entry = &sort_dso, }, |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 420 | { .name = "symbol", .entry = &sort_sym, }, |
| 421 | }; |
| 422 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 423 | static LIST_HEAD(hist_entry__sort_list); |
| 424 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 425 | static int sort_dimension__add(char *tok) |
| 426 | { |
| 427 | int i; |
| 428 | |
| 429 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { |
| 430 | struct sort_dimension *sd = &sort_dimensions[i]; |
| 431 | |
| 432 | if (sd->taken) |
| 433 | continue; |
| 434 | |
| 435 | if (strcmp(tok, sd->name)) |
| 436 | continue; |
| 437 | |
| 438 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
| 439 | sd->taken = 1; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | return -ESRCH; |
| 444 | } |
| 445 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 446 | static void setup_sorting(void) |
| 447 | { |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 448 | char *tmp, *tok, *str = strdup(sort_order); |
| 449 | |
| 450 | for (tok = strtok_r(str, ", ", &tmp); |
| 451 | tok; tok = strtok_r(NULL, ", ", &tmp)) |
| 452 | sort_dimension__add(tok); |
| 453 | |
| 454 | free(str); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int64_t |
| 458 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 459 | { |
| 460 | struct sort_entry *se; |
| 461 | int64_t cmp = 0; |
| 462 | |
| 463 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 464 | cmp = se->cmp(left, right); |
| 465 | if (cmp) |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | return cmp; |
| 470 | } |
| 471 | |
| 472 | static size_t |
| 473 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples) |
| 474 | { |
| 475 | struct sort_entry *se; |
| 476 | size_t ret; |
| 477 | |
| 478 | if (total_samples) { |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 479 | ret = fprintf(fp, " %5.2f%%", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 480 | (self->count * 100.0) / total_samples); |
| 481 | } else |
| 482 | ret = fprintf(fp, "%12d ", self->count); |
| 483 | |
| 484 | list_for_each_entry(se, &hist_entry__sort_list, list) |
| 485 | ret += se->print(fp, self); |
| 486 | |
| 487 | ret += fprintf(fp, "\n"); |
| 488 | |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * collect histogram counts |
| 494 | */ |
| 495 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 496 | static int |
| 497 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, |
| 498 | struct symbol *sym, uint64_t ip, char level) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 499 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 500 | struct rb_node **p = &hist.rb_node; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 501 | struct rb_node *parent = NULL; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 502 | struct hist_entry *he; |
| 503 | struct hist_entry entry = { |
| 504 | .thread = thread, |
| 505 | .map = map, |
| 506 | .dso = dso, |
| 507 | .sym = sym, |
| 508 | .ip = ip, |
| 509 | .level = level, |
| 510 | .count = 1, |
| 511 | }; |
| 512 | int cmp; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 513 | |
| 514 | while (*p != NULL) { |
| 515 | parent = *p; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 516 | he = rb_entry(parent, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 517 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 518 | cmp = hist_entry__cmp(&entry, he); |
| 519 | |
| 520 | if (!cmp) { |
| 521 | he->count++; |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | if (cmp < 0) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 526 | p = &(*p)->rb_left; |
| 527 | else |
| 528 | p = &(*p)->rb_right; |
| 529 | } |
| 530 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 531 | he = malloc(sizeof(*he)); |
| 532 | if (!he) |
| 533 | return -ENOMEM; |
| 534 | *he = entry; |
| 535 | rb_link_node(&he->rb_node, parent, p); |
| 536 | rb_insert_color(&he->rb_node, &hist); |
| 537 | |
| 538 | return 0; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 539 | } |
| 540 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 541 | /* |
| 542 | * reverse the map, sort on count. |
| 543 | */ |
| 544 | |
| 545 | static struct rb_root output_hists; |
| 546 | |
| 547 | static void output__insert_entry(struct hist_entry *he) |
| 548 | { |
| 549 | struct rb_node **p = &output_hists.rb_node; |
| 550 | struct rb_node *parent = NULL; |
| 551 | struct hist_entry *iter; |
| 552 | |
| 553 | while (*p != NULL) { |
| 554 | parent = *p; |
| 555 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 556 | |
| 557 | if (he->count > iter->count) |
| 558 | p = &(*p)->rb_left; |
| 559 | else |
| 560 | p = &(*p)->rb_right; |
| 561 | } |
| 562 | |
| 563 | rb_link_node(&he->rb_node, parent, p); |
| 564 | rb_insert_color(&he->rb_node, &output_hists); |
| 565 | } |
| 566 | |
| 567 | static void output__resort(void) |
| 568 | { |
| 569 | struct rb_node *next = rb_first(&hist); |
| 570 | struct hist_entry *n; |
| 571 | |
| 572 | while (next) { |
| 573 | n = rb_entry(next, struct hist_entry, rb_node); |
| 574 | next = rb_next(&n->rb_node); |
| 575 | |
| 576 | rb_erase(&n->rb_node, &hist); |
| 577 | output__insert_entry(n); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 581 | 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] | 582 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 583 | struct hist_entry *pos; |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 584 | struct sort_entry *se; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 585 | struct rb_node *nd; |
| 586 | size_t ret = 0; |
| 587 | |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 588 | fprintf(fp, "#\n"); |
| 589 | |
| 590 | fprintf(fp, "# Overhead"); |
| 591 | list_for_each_entry(se, &hist_entry__sort_list, list) |
| 592 | fprintf(fp, " %s", se->header); |
| 593 | fprintf(fp, "\n"); |
| 594 | |
| 595 | fprintf(fp, "# ........"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 596 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 597 | int i; |
| 598 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame^] | 599 | fprintf(fp, " "); |
| 600 | for (i = 0; i < strlen(se->header)-1; i++) |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 601 | fprintf(fp, "."); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 602 | } |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 603 | fprintf(fp, "\n"); |
| 604 | |
| 605 | fprintf(fp, "#\n"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 606 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 607 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
| 608 | pos = rb_entry(nd, struct hist_entry, rb_node); |
| 609 | ret += hist_entry__fprintf(fp, pos, total_samples); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | return ret; |
| 613 | } |
| 614 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 615 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 616 | static int __cmd_report(void) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 617 | { |
| 618 | unsigned long offset = 0; |
| 619 | unsigned long head = 0; |
| 620 | struct stat stat; |
| 621 | char *buf; |
| 622 | event_t *event; |
| 623 | int ret, rc = EXIT_FAILURE; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 624 | uint32_t size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 625 | unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 626 | char cwd[PATH_MAX], *cwdp = cwd; |
| 627 | int cwdlen; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 628 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 629 | input = open(input_name, O_RDONLY); |
| 630 | if (input < 0) { |
| 631 | perror("failed to open file"); |
| 632 | exit(-1); |
| 633 | } |
| 634 | |
| 635 | ret = fstat(input, &stat); |
| 636 | if (ret < 0) { |
| 637 | perror("failed to stat file"); |
| 638 | exit(-1); |
| 639 | } |
| 640 | |
| 641 | if (!stat.st_size) { |
| 642 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 643 | exit(0); |
| 644 | } |
| 645 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 646 | if (load_kernel() < 0) { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 647 | perror("failed to load kernel symbols"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 648 | return EXIT_FAILURE; |
| 649 | } |
| 650 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 651 | if (!full_paths) { |
| 652 | if (getcwd(cwd, sizeof(cwd)) == NULL) { |
| 653 | perror("failed to get the current directory"); |
| 654 | return EXIT_FAILURE; |
| 655 | } |
| 656 | cwdlen = strlen(cwd); |
Mike Galbraith | 10a2825 | 2009-06-02 11:04:44 +0200 | [diff] [blame] | 657 | } else { |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 658 | cwdp = NULL; |
Mike Galbraith | 10a2825 | 2009-06-02 11:04:44 +0200 | [diff] [blame] | 659 | cwdlen = 0; |
| 660 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 661 | remap: |
| 662 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 663 | MAP_SHARED, input, offset); |
| 664 | if (buf == MAP_FAILED) { |
| 665 | perror("failed to mmap file"); |
| 666 | exit(-1); |
| 667 | } |
| 668 | |
| 669 | more: |
| 670 | event = (event_t *)(buf + head); |
| 671 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 672 | size = event->header.size; |
| 673 | if (!size) |
| 674 | size = 8; |
| 675 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 676 | if (head + event->header.size >= page_size * mmap_window) { |
| 677 | unsigned long shift = page_size * (head / page_size); |
| 678 | int ret; |
| 679 | |
| 680 | ret = munmap(buf, page_size * mmap_window); |
| 681 | assert(ret == 0); |
| 682 | |
| 683 | offset += shift; |
| 684 | head -= shift; |
| 685 | goto remap; |
| 686 | } |
| 687 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 688 | size = event->header.size; |
| 689 | if (!size) |
| 690 | goto broken_event; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 691 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 692 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
| 693 | char level; |
| 694 | int show = 0; |
| 695 | struct dso *dso = NULL; |
| 696 | struct thread *thread = threads__findnew(event->ip.pid); |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 697 | uint64_t ip = event->ip.ip; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 698 | struct map *map = NULL; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 699 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 700 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 701 | fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
| 702 | (void *)(offset + head), |
| 703 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 704 | event->header.misc, |
| 705 | event->ip.pid, |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 706 | (void *)(long)ip); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 709 | if (thread == NULL) { |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 710 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 711 | event->header.type); |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 712 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 713 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 714 | |
| 715 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
| 716 | show = SHOW_KERNEL; |
| 717 | level = 'k'; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 718 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 719 | dso = kernel_dso; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 720 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 721 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 722 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 723 | show = SHOW_USER; |
| 724 | level = '.'; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 725 | |
| 726 | map = thread__find_map(thread, ip); |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 727 | if (map != NULL) { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 728 | dso = map->dso; |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 729 | ip -= map->start + map->pgoff; |
| 730 | } |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 731 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 732 | } else { |
| 733 | show = SHOW_HV; |
| 734 | level = 'H'; |
| 735 | } |
| 736 | |
| 737 | if (show & show_mask) { |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 738 | struct symbol *sym = dso__find_symbol(dso, ip); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 739 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 740 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
| 741 | fprintf(stderr, |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 742 | "problem incrementing symbol count, skipping event\n"); |
| 743 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 744 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 745 | } |
| 746 | total++; |
| 747 | } else switch (event->header.type) { |
| 748 | case PERF_EVENT_MMAP: { |
| 749 | struct thread *thread = threads__findnew(event->mmap.pid); |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 750 | struct map *map = map__new(&event->mmap, cwdp, cwdlen); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 751 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 752 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 753 | fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
| 754 | (void *)(offset + head), |
| 755 | (void *)(long)(event->header.size), |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 756 | (void *)(long)event->mmap.start, |
| 757 | (void *)(long)event->mmap.len, |
| 758 | (void *)(long)event->mmap.pgoff, |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 759 | event->mmap.filename); |
| 760 | } |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 761 | if (thread == NULL || map == NULL) { |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 762 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n"); |
| 763 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 764 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 765 | thread__insert_map(thread, map); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 766 | total_mmap++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 767 | break; |
| 768 | } |
| 769 | case PERF_EVENT_COMM: { |
| 770 | struct thread *thread = threads__findnew(event->comm.pid); |
| 771 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 772 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 773 | fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 774 | (void *)(offset + head), |
| 775 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 776 | event->comm.comm, event->comm.pid); |
| 777 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 778 | if (thread == NULL || |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 779 | thread__set_comm(thread, event->comm.comm)) { |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 780 | fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n"); |
| 781 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 782 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 783 | total_comm++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 784 | break; |
| 785 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 786 | default: { |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 787 | broken_event: |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 788 | if (dump_trace) |
| 789 | fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n", |
| 790 | (void *)(offset + head), |
| 791 | (void *)(long)(event->header.size), |
| 792 | event->header.type); |
| 793 | |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 794 | total_unknown++; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 795 | |
| 796 | /* |
| 797 | * assume we lost track of the stream, check alignment, and |
| 798 | * increment a single u64 in the hope to catch on again 'soon'. |
| 799 | */ |
| 800 | |
| 801 | if (unlikely(head & 7)) |
| 802 | head &= ~7ULL; |
| 803 | |
| 804 | size = 8; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 805 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 806 | } |
| 807 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 808 | head += size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 809 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 810 | if (offset + head < stat.st_size) |
| 811 | goto more; |
| 812 | |
| 813 | rc = EXIT_SUCCESS; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 814 | close(input); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 815 | |
| 816 | if (dump_trace) { |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 817 | fprintf(stderr, " IP events: %10ld\n", total); |
| 818 | fprintf(stderr, " mmap events: %10ld\n", total_mmap); |
| 819 | fprintf(stderr, " comm events: %10ld\n", total_comm); |
| 820 | fprintf(stderr, " unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 825 | if (verbose >= 2) |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 826 | dsos__fprintf(stdout); |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 827 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 828 | output__resort(); |
| 829 | output__fprintf(stdout, total); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 830 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 831 | return rc; |
| 832 | } |
| 833 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 834 | static const char * const report_usage[] = { |
| 835 | "perf report [<options>] <command>", |
| 836 | NULL |
| 837 | }; |
| 838 | |
| 839 | static const struct option options[] = { |
| 840 | OPT_STRING('i', "input", &input_name, "file", |
| 841 | "input file name"), |
Arnaldo Carvalho de Melo | 815e777 | 2009-05-26 19:46:14 -0300 | [diff] [blame] | 842 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 843 | "be more verbose (show symbol address, etc)"), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 844 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 845 | "dump raw trace in ASCII"), |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 846 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
Ingo Molnar | 63299f0 | 2009-05-28 10:52:00 +0200 | [diff] [blame] | 847 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
| 848 | "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] | 849 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 850 | "Don't shorten the pathnames taking into account the cwd"), |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 851 | OPT_END() |
| 852 | }; |
| 853 | |
| 854 | int cmd_report(int argc, const char **argv, const char *prefix) |
| 855 | { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 856 | symbol__init(); |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 857 | |
| 858 | page_size = getpagesize(); |
| 859 | |
| 860 | parse_options(argc, argv, options, report_usage, 0); |
| 861 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 862 | setup_sorting(); |
| 863 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 864 | setup_pager(); |
| 865 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 866 | return __cmd_report(); |
| 867 | } |