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