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