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