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 | 5da5025 | 2009-07-01 14:46:08 -0300 | [diff] [blame] | 13 | #include <linux/list.h> |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 14 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 15 | #include <linux/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" |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 18 | #include "util/callchain.h" |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 19 | #include "util/strlist.h" |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 20 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 21 | #include "perf.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 22 | #include "util/header.h" |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 23 | |
| 24 | #include "util/parse-options.h" |
| 25 | #include "util/parse-events.h" |
| 26 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 27 | #define SHOW_KERNEL 1 |
| 28 | #define SHOW_USER 2 |
| 29 | #define SHOW_HV 4 |
| 30 | |
Ingo Molnar | 23ac9cb | 2009-05-27 09:33:18 +0200 | [diff] [blame] | 31 | static char const *input_name = "perf.data"; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 32 | static char *vmlinux = NULL; |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 33 | |
| 34 | static char default_sort_order[] = "comm,dso"; |
| 35 | static char *sort_order = default_sort_order; |
Arnaldo Carvalho de Melo | 7bec7a9 | 2009-06-30 19:01:22 -0300 | [diff] [blame] | 36 | static char *dso_list_str, *comm_list_str, *sym_list_str; |
| 37 | static struct strlist *dso_list, *comm_list, *sym_list; |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 38 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 39 | static int input; |
| 40 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 41 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 42 | static int dump_trace = 0; |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 43 | #define dprintf(x...) do { if (dump_trace) printf(x); } while (0) |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 44 | #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] | 45 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 46 | static int verbose; |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 47 | #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0) |
| 48 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 49 | static int modules; |
| 50 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 51 | static int full_paths; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 52 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 53 | static unsigned long page_size; |
| 54 | static unsigned long mmap_window = 32; |
| 55 | |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 56 | static char default_parent_pattern[] = "^sys_|^do_page_fault"; |
| 57 | static char *parent_pattern = default_parent_pattern; |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 58 | static regex_t parent_regex; |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 59 | |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 60 | static int exclude_other = 1; |
Frederic Weisbecker | be90388 | 2009-07-05 07:39:18 +0200 | [diff] [blame^] | 61 | |
| 62 | static char callchain_default_opt[] = "flat,0"; |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 63 | static int callchain; |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 64 | static enum chain_mode callchain_mode; |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 65 | static double callchain_min_percent = 0.0; |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 66 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 67 | static u64 sample_type; |
| 68 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 69 | struct ip_event { |
| 70 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 71 | u64 ip; |
| 72 | u32 pid, tid; |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 73 | unsigned char __more_data[]; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 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 mmap_event { |
| 77 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 78 | u32 pid, tid; |
| 79 | u64 start; |
| 80 | u64 len; |
| 81 | u64 pgoff; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 82 | char filename[PATH_MAX]; |
| 83 | }; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 84 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 85 | struct comm_event { |
| 86 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 87 | u32 pid, tid; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 88 | char comm[16]; |
| 89 | }; |
| 90 | |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 91 | struct fork_event { |
| 92 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 93 | u32 pid, ppid; |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
Ingo Molnar | b2fef07 | 2009-06-05 18:07:51 +0200 | [diff] [blame] | 96 | struct period_event { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 97 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 98 | u64 time; |
| 99 | u64 id; |
| 100 | u64 sample_period; |
Ingo Molnar | b2fef07 | 2009-06-05 18:07:51 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 103 | struct lost_event { |
| 104 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 105 | u64 id; |
| 106 | u64 lost; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Peter Zijlstra | e9ea2fd | 2009-06-24 22:46:04 +0200 | [diff] [blame] | 109 | struct read_event { |
| 110 | struct perf_event_header header; |
| 111 | u32 pid,tid; |
| 112 | u64 value; |
| 113 | u64 format[3]; |
| 114 | }; |
| 115 | |
Ingo Molnar | b2fef07 | 2009-06-05 18:07:51 +0200 | [diff] [blame] | 116 | typedef union event_union { |
| 117 | struct perf_event_header header; |
| 118 | struct ip_event ip; |
| 119 | struct mmap_event mmap; |
| 120 | struct comm_event comm; |
| 121 | struct fork_event fork; |
| 122 | struct period_event period; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 123 | struct lost_event lost; |
Peter Zijlstra | e9ea2fd | 2009-06-24 22:46:04 +0200 | [diff] [blame] | 124 | struct read_event read; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 125 | } event_t; |
| 126 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 127 | static LIST_HEAD(dsos); |
| 128 | static struct dso *kernel_dso; |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 129 | static struct dso *vdso; |
Anton Blanchard | fb9c818 | 2009-07-01 09:00:49 +1000 | [diff] [blame] | 130 | static struct dso *hypervisor_dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 131 | |
| 132 | static void dsos__add(struct dso *dso) |
| 133 | { |
| 134 | list_add_tail(&dso->node, &dsos); |
| 135 | } |
| 136 | |
| 137 | static struct dso *dsos__find(const char *name) |
| 138 | { |
| 139 | struct dso *pos; |
| 140 | |
| 141 | list_for_each_entry(pos, &dsos, node) |
| 142 | if (strcmp(pos->name, name) == 0) |
| 143 | return pos; |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | static struct dso *dsos__findnew(const char *name) |
| 148 | { |
| 149 | struct dso *dso = dsos__find(name); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 150 | int nr; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 151 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 152 | if (dso) |
| 153 | return dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 154 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 155 | dso = dso__new(name, 0); |
| 156 | if (!dso) |
| 157 | goto out_delete_dso; |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 158 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 159 | nr = dso__load(dso, NULL, verbose); |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 160 | if (nr < 0) { |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 161 | eprintf("Failed to open: %s\n", name); |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 162 | goto out_delete_dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 163 | } |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 164 | if (!nr) |
| 165 | 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] | 166 | |
| 167 | dsos__add(dso); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 168 | |
| 169 | return dso; |
| 170 | |
| 171 | out_delete_dso: |
| 172 | dso__delete(dso); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 176 | static void dsos__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 177 | { |
| 178 | struct dso *pos; |
| 179 | |
| 180 | list_for_each_entry(pos, &dsos, node) |
| 181 | dso__fprintf(pos, fp); |
| 182 | } |
| 183 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 184 | static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip) |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 185 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 186 | return dso__find_symbol(dso, ip); |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 189 | static int load_kernel(void) |
| 190 | { |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 191 | int err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 192 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 193 | kernel_dso = dso__new("[kernel]", 0); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 194 | if (!kernel_dso) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 195 | return -1; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 196 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 197 | err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules); |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 198 | if (err <= 0) { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 199 | dso__delete(kernel_dso); |
| 200 | kernel_dso = NULL; |
| 201 | } else |
| 202 | dsos__add(kernel_dso); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 203 | |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 204 | vdso = dso__new("[vdso]", 0); |
| 205 | if (!vdso) |
| 206 | return -1; |
| 207 | |
| 208 | vdso->find_symbol = vdso__find_symbol; |
| 209 | |
| 210 | dsos__add(vdso); |
| 211 | |
Anton Blanchard | fb9c818 | 2009-07-01 09:00:49 +1000 | [diff] [blame] | 212 | hypervisor_dso = dso__new("[hypervisor]", 0); |
| 213 | if (!hypervisor_dso) |
| 214 | return -1; |
| 215 | dsos__add(hypervisor_dso); |
| 216 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 217 | return err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 220 | static char __cwd[PATH_MAX]; |
| 221 | static char *cwd = __cwd; |
| 222 | static int cwdlen; |
| 223 | |
| 224 | static int strcommon(const char *pathname) |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 225 | { |
| 226 | int n = 0; |
| 227 | |
| 228 | while (pathname[n] == cwd[n] && n < cwdlen) |
| 229 | ++n; |
| 230 | |
| 231 | return n; |
| 232 | } |
| 233 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 234 | struct map { |
| 235 | struct list_head node; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 236 | u64 start; |
| 237 | u64 end; |
| 238 | u64 pgoff; |
| 239 | u64 (*map_ip)(struct map *, u64); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 240 | struct dso *dso; |
| 241 | }; |
| 242 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 243 | static u64 map__map_ip(struct map *map, u64 ip) |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 244 | { |
| 245 | return ip - map->start + map->pgoff; |
| 246 | } |
| 247 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 248 | static u64 vdso__map_ip(struct map *map __used, u64 ip) |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 249 | { |
| 250 | return ip; |
| 251 | } |
| 252 | |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 253 | static inline int is_anon_memory(const char *filename) |
| 254 | { |
Arnaldo Carvalho de Melo | cc8b88b | 2009-06-30 19:01:21 -0300 | [diff] [blame] | 255 | return strcmp(filename, "//anon") == 0; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 256 | } |
| 257 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 258 | static struct map *map__new(struct mmap_event *event) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 259 | { |
| 260 | struct map *self = malloc(sizeof(*self)); |
| 261 | |
| 262 | if (self != NULL) { |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 263 | const char *filename = event->filename; |
| 264 | char newfilename[PATH_MAX]; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 265 | int anon; |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 266 | |
| 267 | if (cwd) { |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 268 | int n = strcommon(filename); |
| 269 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 270 | if (n == cwdlen) { |
| 271 | snprintf(newfilename, sizeof(newfilename), |
| 272 | ".%s", filename + n); |
| 273 | filename = newfilename; |
| 274 | } |
| 275 | } |
| 276 | |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 277 | anon = is_anon_memory(filename); |
| 278 | |
| 279 | if (anon) { |
| 280 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid); |
| 281 | filename = newfilename; |
| 282 | } |
| 283 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 284 | self->start = event->start; |
| 285 | self->end = event->start + event->len; |
| 286 | self->pgoff = event->pgoff; |
| 287 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 288 | self->dso = dsos__findnew(filename); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 289 | if (self->dso == NULL) |
| 290 | goto out_delete; |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 291 | |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 292 | if (self->dso == vdso || anon) |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 293 | self->map_ip = vdso__map_ip; |
| 294 | else |
| 295 | self->map_ip = map__map_ip; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 296 | } |
| 297 | return self; |
| 298 | out_delete: |
| 299 | free(self); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 303 | static struct map *map__clone(struct map *self) |
| 304 | { |
| 305 | struct map *map = malloc(sizeof(*self)); |
| 306 | |
| 307 | if (!map) |
| 308 | return NULL; |
| 309 | |
| 310 | memcpy(map, self, sizeof(*self)); |
| 311 | |
| 312 | return map; |
| 313 | } |
| 314 | |
| 315 | static int map__overlap(struct map *l, struct map *r) |
| 316 | { |
| 317 | if (l->start > r->start) { |
| 318 | struct map *t = l; |
| 319 | l = r; |
| 320 | r = t; |
| 321 | } |
| 322 | |
| 323 | if (l->end > r->start) |
| 324 | return 1; |
| 325 | |
| 326 | return 0; |
| 327 | } |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 328 | |
Arnaldo Carvalho de Melo | 9ac9954 | 2009-06-04 13:54:00 -0300 | [diff] [blame] | 329 | static size_t map__fprintf(struct map *self, FILE *fp) |
| 330 | { |
Ingo Molnar | 729ff5e2 | 2009-06-11 14:16:15 +0200 | [diff] [blame] | 331 | return fprintf(fp, " %Lx-%Lx %Lx %s\n", |
Arnaldo Carvalho de Melo | 9ac9954 | 2009-06-04 13:54:00 -0300 | [diff] [blame] | 332 | self->start, self->end, self->pgoff, self->dso->name); |
| 333 | } |
| 334 | |
| 335 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 336 | struct thread { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 337 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 338 | struct list_head maps; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 339 | pid_t pid; |
| 340 | char *comm; |
| 341 | }; |
| 342 | |
| 343 | static struct thread *thread__new(pid_t pid) |
| 344 | { |
| 345 | struct thread *self = malloc(sizeof(*self)); |
| 346 | |
| 347 | if (self != NULL) { |
| 348 | self->pid = pid; |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 349 | self->comm = malloc(32); |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 350 | if (self->comm) |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 351 | snprintf(self->comm, 32, ":%d", self->pid); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 352 | INIT_LIST_HEAD(&self->maps); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return self; |
| 356 | } |
| 357 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 358 | static int thread__set_comm(struct thread *self, const char *comm) |
| 359 | { |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 360 | if (self->comm) |
| 361 | free(self->comm); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 362 | self->comm = strdup(comm); |
| 363 | return self->comm ? 0 : -ENOMEM; |
| 364 | } |
| 365 | |
Arnaldo Carvalho de Melo | 9ac9954 | 2009-06-04 13:54:00 -0300 | [diff] [blame] | 366 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 367 | { |
| 368 | struct map *pos; |
| 369 | size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm); |
| 370 | |
| 371 | list_for_each_entry(pos, &self->maps, node) |
| 372 | ret += map__fprintf(pos, fp); |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 378 | static struct rb_root threads; |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 379 | static struct thread *last_match; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 380 | |
| 381 | static struct thread *threads__findnew(pid_t pid) |
| 382 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 383 | struct rb_node **p = &threads.rb_node; |
| 384 | struct rb_node *parent = NULL; |
| 385 | struct thread *th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 386 | |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 387 | /* |
| 388 | * Font-end cache - PID lookups come in blocks, |
| 389 | * so most of the time we dont have to look up |
| 390 | * the full rbtree: |
| 391 | */ |
| 392 | if (last_match && last_match->pid == pid) |
| 393 | return last_match; |
| 394 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 395 | while (*p != NULL) { |
| 396 | parent = *p; |
| 397 | th = rb_entry(parent, struct thread, rb_node); |
| 398 | |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 399 | if (th->pid == pid) { |
| 400 | last_match = th; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 401 | return th; |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 402 | } |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 403 | |
| 404 | if (pid < th->pid) |
| 405 | p = &(*p)->rb_left; |
| 406 | else |
| 407 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 408 | } |
| 409 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 410 | th = thread__new(pid); |
| 411 | if (th != NULL) { |
| 412 | rb_link_node(&th->rb_node, parent, p); |
| 413 | rb_insert_color(&th->rb_node, &threads); |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 414 | last_match = th; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 415 | } |
Ingo Molnar | eed4dcd | 2009-06-03 19:59:24 +0200 | [diff] [blame] | 416 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 417 | return th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static void thread__insert_map(struct thread *self, struct map *map) |
| 421 | { |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 422 | struct map *pos, *tmp; |
| 423 | |
| 424 | list_for_each_entry_safe(pos, tmp, &self->maps, node) { |
| 425 | if (map__overlap(pos, map)) { |
Peter Zijlstra | 3d906ef | 2009-06-23 11:23:07 +0200 | [diff] [blame] | 426 | if (verbose >= 2) { |
| 427 | printf("overlapping maps:\n"); |
| 428 | map__fprintf(map, stdout); |
| 429 | map__fprintf(pos, stdout); |
| 430 | } |
| 431 | |
| 432 | if (map->start <= pos->start && map->end > pos->start) |
| 433 | pos->start = map->end; |
| 434 | |
| 435 | if (map->end >= pos->end && map->start < pos->end) |
| 436 | pos->end = map->start; |
| 437 | |
| 438 | if (verbose >= 2) { |
| 439 | printf("after collision:\n"); |
| 440 | map__fprintf(pos, stdout); |
| 441 | } |
| 442 | |
| 443 | if (pos->start >= pos->end) { |
| 444 | list_del_init(&pos->node); |
| 445 | free(pos); |
| 446 | } |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 450 | list_add_tail(&map->node, &self->maps); |
| 451 | } |
| 452 | |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 453 | static int thread__fork(struct thread *self, struct thread *parent) |
| 454 | { |
| 455 | struct map *map; |
| 456 | |
| 457 | if (self->comm) |
| 458 | free(self->comm); |
| 459 | self->comm = strdup(parent->comm); |
| 460 | if (!self->comm) |
| 461 | return -ENOMEM; |
| 462 | |
| 463 | list_for_each_entry(map, &parent->maps, node) { |
| 464 | struct map *new = map__clone(map); |
| 465 | if (!new) |
| 466 | return -ENOMEM; |
| 467 | thread__insert_map(self, new); |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 473 | 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] | 474 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 475 | struct map *pos; |
| 476 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 477 | if (self == NULL) |
| 478 | return NULL; |
| 479 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 480 | list_for_each_entry(pos, &self->maps, node) |
| 481 | if (ip >= pos->start && ip <= pos->end) |
| 482 | return pos; |
| 483 | |
| 484 | return NULL; |
| 485 | } |
| 486 | |
Arnaldo Carvalho de Melo | 9ac9954 | 2009-06-04 13:54:00 -0300 | [diff] [blame] | 487 | static size_t threads__fprintf(FILE *fp) |
| 488 | { |
| 489 | size_t ret = 0; |
| 490 | struct rb_node *nd; |
| 491 | |
| 492 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { |
| 493 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 494 | |
| 495 | ret += thread__fprintf(pos, fp); |
| 496 | } |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 501 | /* |
| 502 | * histogram, sorted on item, collects counts |
| 503 | */ |
| 504 | |
| 505 | static struct rb_root hist; |
| 506 | |
| 507 | struct hist_entry { |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 508 | struct rb_node rb_node; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 509 | |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 510 | struct thread *thread; |
| 511 | struct map *map; |
| 512 | struct dso *dso; |
| 513 | struct symbol *sym; |
| 514 | struct symbol *parent; |
| 515 | u64 ip; |
| 516 | char level; |
| 517 | struct callchain_node callchain; |
| 518 | struct rb_root sorted_chain; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 519 | |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 520 | u64 count; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 521 | }; |
| 522 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 523 | /* |
| 524 | * configurable sorting bits |
| 525 | */ |
| 526 | |
| 527 | struct sort_entry { |
| 528 | struct list_head list; |
| 529 | |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 530 | char *header; |
| 531 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 532 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 533 | int64_t (*collapse)(struct hist_entry *, struct hist_entry *); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 534 | size_t (*print)(FILE *fp, struct hist_entry *); |
| 535 | }; |
| 536 | |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 537 | static int64_t cmp_null(void *l, void *r) |
| 538 | { |
| 539 | if (!l && !r) |
| 540 | return 0; |
| 541 | else if (!l) |
| 542 | return -1; |
| 543 | else |
| 544 | return 1; |
| 545 | } |
| 546 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 547 | /* --sort pid */ |
| 548 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 549 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 550 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 551 | { |
| 552 | return right->thread->pid - left->thread->pid; |
| 553 | } |
| 554 | |
| 555 | static size_t |
| 556 | sort__thread_print(FILE *fp, struct hist_entry *self) |
| 557 | { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 558 | return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static struct sort_entry sort_thread = { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 562 | .header = " Command: Pid", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 563 | .cmp = sort__thread_cmp, |
| 564 | .print = sort__thread_print, |
| 565 | }; |
| 566 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 567 | /* --sort comm */ |
| 568 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 569 | static int64_t |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 570 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 571 | { |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 572 | return right->thread->pid - left->thread->pid; |
| 573 | } |
| 574 | |
| 575 | static int64_t |
| 576 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) |
| 577 | { |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 578 | char *comm_l = left->thread->comm; |
| 579 | char *comm_r = right->thread->comm; |
| 580 | |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 581 | if (!comm_l || !comm_r) |
| 582 | return cmp_null(comm_l, comm_r); |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 583 | |
| 584 | return strcmp(comm_l, comm_r); |
| 585 | } |
| 586 | |
| 587 | static size_t |
| 588 | sort__comm_print(FILE *fp, struct hist_entry *self) |
| 589 | { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 590 | return fprintf(fp, "%16s", self->thread->comm); |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static struct sort_entry sort_comm = { |
Ingo Molnar | 8edd428 | 2009-06-05 14:13:18 +0200 | [diff] [blame] | 594 | .header = " Command", |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 595 | .cmp = sort__comm_cmp, |
| 596 | .collapse = sort__comm_collapse, |
| 597 | .print = sort__comm_print, |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 598 | }; |
| 599 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 600 | /* --sort dso */ |
| 601 | |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 602 | static int64_t |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 603 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
| 604 | { |
| 605 | struct dso *dso_l = left->dso; |
| 606 | struct dso *dso_r = right->dso; |
| 607 | |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 608 | if (!dso_l || !dso_r) |
| 609 | return cmp_null(dso_l, dso_r); |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 610 | |
| 611 | return strcmp(dso_l->name, dso_r->name); |
| 612 | } |
| 613 | |
| 614 | static size_t |
| 615 | sort__dso_print(FILE *fp, struct hist_entry *self) |
| 616 | { |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 617 | if (self->dso) |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 618 | return fprintf(fp, "%-25s", self->dso->name); |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 619 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 620 | return fprintf(fp, "%016llx ", (u64)self->ip); |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | static struct sort_entry sort_dso = { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 624 | .header = "Shared Object ", |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 625 | .cmp = sort__dso_cmp, |
| 626 | .print = sort__dso_print, |
| 627 | }; |
| 628 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 629 | /* --sort symbol */ |
| 630 | |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 631 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 632 | 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] | 633 | { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 634 | u64 ip_l, ip_r; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 635 | |
| 636 | if (left->sym == right->sym) |
| 637 | return 0; |
| 638 | |
| 639 | ip_l = left->sym ? left->sym->start : left->ip; |
| 640 | ip_r = right->sym ? right->sym->start : right->ip; |
| 641 | |
| 642 | return (int64_t)(ip_r - ip_l); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 643 | } |
| 644 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 645 | static size_t |
| 646 | sort__sym_print(FILE *fp, struct hist_entry *self) |
| 647 | { |
| 648 | size_t ret = 0; |
| 649 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 650 | if (verbose) |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 651 | ret += fprintf(fp, "%#018llx ", (u64)self->ip); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 652 | |
Ingo Molnar | 8edd428 | 2009-06-05 14:13:18 +0200 | [diff] [blame] | 653 | if (self->sym) { |
| 654 | ret += fprintf(fp, "[%c] %s", |
Anton Blanchard | fb9c818 | 2009-07-01 09:00:49 +1000 | [diff] [blame] | 655 | self->dso == kernel_dso ? 'k' : |
| 656 | self->dso == hypervisor_dso ? 'h' : '.', self->sym->name); |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 657 | |
| 658 | if (self->sym->module) |
| 659 | ret += fprintf(fp, "\t[%s]", self->sym->module->name); |
Ingo Molnar | 8edd428 | 2009-06-05 14:13:18 +0200 | [diff] [blame] | 660 | } else { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 661 | ret += fprintf(fp, "%#016llx", (u64)self->ip); |
Ingo Molnar | 8edd428 | 2009-06-05 14:13:18 +0200 | [diff] [blame] | 662 | } |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 663 | |
| 664 | return ret; |
| 665 | } |
| 666 | |
| 667 | static struct sort_entry sort_sym = { |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 668 | .header = "Symbol", |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 669 | .cmp = sort__sym_cmp, |
| 670 | .print = sort__sym_print, |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 671 | }; |
| 672 | |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 673 | /* --sort parent */ |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 674 | |
| 675 | static int64_t |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 676 | sort__parent_cmp(struct hist_entry *left, struct hist_entry *right) |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 677 | { |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 678 | struct symbol *sym_l = left->parent; |
| 679 | struct symbol *sym_r = right->parent; |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 680 | |
| 681 | if (!sym_l || !sym_r) |
| 682 | return cmp_null(sym_l, sym_r); |
| 683 | |
| 684 | return strcmp(sym_l->name, sym_r->name); |
| 685 | } |
| 686 | |
| 687 | static size_t |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 688 | sort__parent_print(FILE *fp, struct hist_entry *self) |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 689 | { |
| 690 | size_t ret = 0; |
| 691 | |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 692 | ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]"); |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 693 | |
| 694 | return ret; |
| 695 | } |
| 696 | |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 697 | static struct sort_entry sort_parent = { |
| 698 | .header = "Parent symbol ", |
| 699 | .cmp = sort__parent_cmp, |
| 700 | .print = sort__parent_print, |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 701 | }; |
| 702 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 703 | static int sort__need_collapse = 0; |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 704 | static int sort__has_parent = 0; |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 705 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 706 | struct sort_dimension { |
Ingo Molnar | 8edd428 | 2009-06-05 14:13:18 +0200 | [diff] [blame] | 707 | char *name; |
| 708 | struct sort_entry *entry; |
| 709 | int taken; |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 710 | }; |
| 711 | |
| 712 | static struct sort_dimension sort_dimensions[] = { |
| 713 | { .name = "pid", .entry = &sort_thread, }, |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 714 | { .name = "comm", .entry = &sort_comm, }, |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 715 | { .name = "dso", .entry = &sort_dso, }, |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 716 | { .name = "symbol", .entry = &sort_sym, }, |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 717 | { .name = "parent", .entry = &sort_parent, }, |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 718 | }; |
| 719 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 720 | static LIST_HEAD(hist_entry__sort_list); |
| 721 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 722 | static int sort_dimension__add(char *tok) |
| 723 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 724 | unsigned int i; |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 725 | |
| 726 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { |
| 727 | struct sort_dimension *sd = &sort_dimensions[i]; |
| 728 | |
| 729 | if (sd->taken) |
| 730 | continue; |
| 731 | |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame] | 732 | if (strncasecmp(tok, sd->name, strlen(tok))) |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 733 | continue; |
| 734 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 735 | if (sd->entry->collapse) |
| 736 | sort__need_collapse = 1; |
| 737 | |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 738 | if (sd->entry == &sort_parent) { |
| 739 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 740 | if (ret) { |
| 741 | char err[BUFSIZ]; |
| 742 | |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 743 | regerror(ret, &parent_regex, err, sizeof(err)); |
| 744 | fprintf(stderr, "Invalid regex: %s\n%s", |
| 745 | parent_pattern, err); |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 746 | exit(-1); |
| 747 | } |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 748 | sort__has_parent = 1; |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 749 | } |
| 750 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 751 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
| 752 | sd->taken = 1; |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame] | 753 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | return -ESRCH; |
| 758 | } |
| 759 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 760 | static int64_t |
| 761 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 762 | { |
| 763 | struct sort_entry *se; |
| 764 | int64_t cmp = 0; |
| 765 | |
| 766 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 767 | cmp = se->cmp(left, right); |
| 768 | if (cmp) |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | return cmp; |
| 773 | } |
| 774 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 775 | static int64_t |
| 776 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 777 | { |
| 778 | struct sort_entry *se; |
| 779 | int64_t cmp = 0; |
| 780 | |
| 781 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 782 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 783 | |
| 784 | f = se->collapse ?: se->cmp; |
| 785 | |
| 786 | cmp = f(left, right); |
| 787 | if (cmp) |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | return cmp; |
| 792 | } |
| 793 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 794 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask) |
| 795 | { |
| 796 | int i; |
| 797 | size_t ret = 0; |
| 798 | |
| 799 | ret += fprintf(fp, "%s", " "); |
| 800 | |
| 801 | for (i = 0; i < depth; i++) |
| 802 | if (depth_mask & (1 << i)) |
| 803 | ret += fprintf(fp, "| "); |
| 804 | else |
| 805 | ret += fprintf(fp, " "); |
| 806 | |
| 807 | ret += fprintf(fp, "\n"); |
| 808 | |
| 809 | return ret; |
| 810 | } |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 811 | static size_t |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 812 | ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth, |
| 813 | int depth_mask, int count, u64 total_samples, |
| 814 | int hits) |
| 815 | { |
| 816 | int i; |
| 817 | size_t ret = 0; |
| 818 | |
| 819 | ret += fprintf(fp, "%s", " "); |
| 820 | for (i = 0; i < depth; i++) { |
| 821 | if (depth_mask & (1 << i)) |
| 822 | ret += fprintf(fp, "|"); |
| 823 | else |
| 824 | ret += fprintf(fp, " "); |
| 825 | if (!count && i == depth - 1) { |
| 826 | double percent; |
| 827 | |
| 828 | percent = hits * 100.0 / total_samples; |
Frederic Weisbecker | 24b57c6 | 2009-07-02 20:14:35 +0200 | [diff] [blame] | 829 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 830 | } else |
| 831 | ret += fprintf(fp, "%s", " "); |
| 832 | } |
| 833 | if (chain->sym) |
| 834 | ret += fprintf(fp, "%s\n", chain->sym->name); |
| 835 | else |
| 836 | ret += fprintf(fp, "%p\n", (void *)(long)chain->ip); |
| 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static size_t |
| 842 | callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
| 843 | u64 total_samples, int depth, int depth_mask) |
| 844 | { |
| 845 | struct rb_node *node, *next; |
| 846 | struct callchain_node *child; |
| 847 | struct callchain_list *chain; |
| 848 | int new_depth_mask = depth_mask; |
| 849 | size_t ret = 0; |
| 850 | int i; |
| 851 | |
| 852 | node = rb_first(&self->rb_root); |
| 853 | while (node) { |
| 854 | child = rb_entry(node, struct callchain_node, rb_node); |
| 855 | |
| 856 | /* |
| 857 | * The depth mask manages the output of pipes that show |
| 858 | * the depth. We don't want to keep the pipes of the current |
| 859 | * level for the last child of this depth |
| 860 | */ |
| 861 | next = rb_next(node); |
| 862 | if (!next) |
| 863 | new_depth_mask &= ~(1 << (depth - 1)); |
| 864 | |
| 865 | /* |
| 866 | * But we keep the older depth mask for the line seperator |
| 867 | * to keep the level link until we reach the last child |
| 868 | */ |
| 869 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask); |
| 870 | i = 0; |
| 871 | list_for_each_entry(chain, &child->val, list) { |
| 872 | if (chain->ip >= PERF_CONTEXT_MAX) |
| 873 | continue; |
| 874 | ret += ipchain__fprintf_graph(fp, chain, depth, |
| 875 | new_depth_mask, i++, |
| 876 | total_samples, |
| 877 | child->cumul_hit); |
| 878 | } |
| 879 | ret += callchain__fprintf_graph(fp, child, total_samples, |
| 880 | depth + 1, |
| 881 | new_depth_mask | (1 << depth)); |
| 882 | node = next; |
| 883 | } |
| 884 | |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | static size_t |
| 889 | callchain__fprintf_flat(FILE *fp, struct callchain_node *self, |
| 890 | u64 total_samples) |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 891 | { |
| 892 | struct callchain_list *chain; |
| 893 | size_t ret = 0; |
| 894 | |
| 895 | if (!self) |
| 896 | return 0; |
| 897 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 898 | ret += callchain__fprintf_flat(fp, self->parent, total_samples); |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 899 | |
| 900 | |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 901 | list_for_each_entry(chain, &self->val, list) { |
| 902 | if (chain->ip >= PERF_CONTEXT_MAX) |
| 903 | continue; |
| 904 | if (chain->sym) |
| 905 | ret += fprintf(fp, " %s\n", chain->sym->name); |
| 906 | else |
| 907 | ret += fprintf(fp, " %p\n", |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 908 | (void *)(long)chain->ip); |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 909 | } |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | static size_t |
| 915 | hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, |
| 916 | u64 total_samples) |
| 917 | { |
| 918 | struct rb_node *rb_node; |
| 919 | struct callchain_node *chain; |
| 920 | size_t ret = 0; |
| 921 | |
| 922 | rb_node = rb_first(&self->sorted_chain); |
| 923 | while (rb_node) { |
| 924 | double percent; |
| 925 | |
| 926 | chain = rb_entry(rb_node, struct callchain_node, rb_node); |
| 927 | percent = chain->hit * 100.0 / total_samples; |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 928 | if (callchain_mode == FLAT) { |
Frederic Weisbecker | 24b57c6 | 2009-07-02 20:14:35 +0200 | [diff] [blame] | 929 | ret += percent_color_fprintf(fp, " %6.2f%%\n", |
| 930 | percent); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 931 | ret += callchain__fprintf_flat(fp, chain, total_samples); |
| 932 | } else if (callchain_mode == GRAPH) { |
| 933 | ret += callchain__fprintf_graph(fp, chain, |
| 934 | total_samples, 1, 1); |
| 935 | } |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 936 | ret += fprintf(fp, "\n"); |
| 937 | rb_node = rb_next(rb_node); |
| 938 | } |
| 939 | |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | |
| 944 | static size_t |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 945 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples) |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 946 | { |
| 947 | struct sort_entry *se; |
| 948 | size_t ret; |
| 949 | |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 950 | if (exclude_other && !self->parent) |
| 951 | return 0; |
| 952 | |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 953 | if (total_samples) |
| 954 | ret = percent_color_fprintf(fp, " %6.2f%%", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 955 | (self->count * 100.0) / total_samples); |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 956 | else |
Ingo Molnar | 729ff5e2 | 2009-06-11 14:16:15 +0200 | [diff] [blame] | 957 | ret = fprintf(fp, "%12Ld ", self->count); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 958 | |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 959 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 960 | if (exclude_other && (se == &sort_parent)) |
| 961 | continue; |
| 962 | |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 963 | fprintf(fp, " "); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 964 | ret += se->print(fp, self); |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 965 | } |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 966 | |
| 967 | ret += fprintf(fp, "\n"); |
| 968 | |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 969 | if (callchain) |
| 970 | hist_entry_callchain__fprintf(fp, self, total_samples); |
| 971 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | /* |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 976 | * |
| 977 | */ |
| 978 | |
| 979 | static struct symbol * |
| 980 | resolve_symbol(struct thread *thread, struct map **mapp, |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 981 | struct dso **dsop, u64 *ipp) |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 982 | { |
| 983 | struct dso *dso = dsop ? *dsop : NULL; |
| 984 | struct map *map = mapp ? *mapp : NULL; |
Peter Zijlstra | 520f2c3 | 2009-06-22 16:52:51 +0200 | [diff] [blame] | 985 | u64 ip = *ipp; |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 986 | |
| 987 | if (!thread) |
| 988 | return NULL; |
| 989 | |
| 990 | if (dso) |
| 991 | goto got_dso; |
| 992 | |
| 993 | if (map) |
| 994 | goto got_map; |
| 995 | |
| 996 | map = thread__find_map(thread, ip); |
| 997 | if (map != NULL) { |
| 998 | if (mapp) |
| 999 | *mapp = map; |
| 1000 | got_map: |
| 1001 | ip = map->map_ip(map, ip); |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1002 | |
| 1003 | dso = map->dso; |
| 1004 | } else { |
| 1005 | /* |
| 1006 | * If this is outside of all known maps, |
| 1007 | * and is a negative address, try to look it |
| 1008 | * up in the kernel dso, as it might be a |
| 1009 | * vsyscall (which executes in user-mode): |
| 1010 | */ |
| 1011 | if ((long long)ip < 0) |
| 1012 | dso = kernel_dso; |
| 1013 | } |
| 1014 | dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); |
Peter Zijlstra | 520f2c3 | 2009-06-22 16:52:51 +0200 | [diff] [blame] | 1015 | dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip); |
| 1016 | *ipp = ip; |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1017 | |
| 1018 | if (dsop) |
| 1019 | *dsop = dso; |
| 1020 | |
| 1021 | if (!dso) |
| 1022 | return NULL; |
| 1023 | got_dso: |
| 1024 | return dso->find_symbol(dso, ip); |
| 1025 | } |
| 1026 | |
Peter Zijlstra | 2a0a50f | 2009-06-18 22:20:45 +0200 | [diff] [blame] | 1027 | static int call__match(struct symbol *sym) |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1028 | { |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 1029 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
Peter Zijlstra | 2a0a50f | 2009-06-18 22:20:45 +0200 | [diff] [blame] | 1030 | return 1; |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1031 | |
Peter Zijlstra | 2a0a50f | 2009-06-18 22:20:45 +0200 | [diff] [blame] | 1032 | return 0; |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1035 | static struct symbol ** |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1036 | resolve_callchain(struct thread *thread, struct map *map __used, |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1037 | struct ip_callchain *chain, struct hist_entry *entry) |
| 1038 | { |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1039 | u64 context = PERF_CONTEXT_MAX; |
Ingo Molnar | 029e5b1 | 2009-07-03 13:17:28 +0200 | [diff] [blame] | 1040 | struct symbol **syms = NULL; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1041 | unsigned int i; |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1042 | |
| 1043 | if (callchain) { |
| 1044 | syms = calloc(chain->nr, sizeof(*syms)); |
| 1045 | if (!syms) { |
| 1046 | fprintf(stderr, "Can't allocate memory for symbols\n"); |
| 1047 | exit(-1); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | for (i = 0; i < chain->nr; i++) { |
| 1052 | u64 ip = chain->ips[i]; |
| 1053 | struct dso *dso = NULL; |
| 1054 | struct symbol *sym; |
| 1055 | |
| 1056 | if (ip >= PERF_CONTEXT_MAX) { |
| 1057 | context = ip; |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | switch (context) { |
Ingo Molnar | 88a69df | 2009-07-01 11:17:20 +0200 | [diff] [blame] | 1062 | case PERF_CONTEXT_HV: |
| 1063 | dso = hypervisor_dso; |
| 1064 | break; |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1065 | case PERF_CONTEXT_KERNEL: |
| 1066 | dso = kernel_dso; |
| 1067 | break; |
| 1068 | default: |
| 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | sym = resolve_symbol(thread, NULL, &dso, &ip); |
| 1073 | |
| 1074 | if (sym) { |
| 1075 | if (sort__has_parent && call__match(sym) && |
| 1076 | !entry->parent) |
| 1077 | entry->parent = sym; |
| 1078 | if (!callchain) |
| 1079 | break; |
| 1080 | syms[i] = sym; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | return syms; |
| 1085 | } |
| 1086 | |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1087 | /* |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 1088 | * collect histogram counts |
| 1089 | */ |
| 1090 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1091 | static int |
| 1092 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1093 | struct symbol *sym, u64 ip, struct ip_callchain *chain, |
| 1094 | char level, u64 count) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1095 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1096 | struct rb_node **p = &hist.rb_node; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1097 | struct rb_node *parent = NULL; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1098 | struct hist_entry *he; |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1099 | struct symbol **syms = NULL; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1100 | struct hist_entry entry = { |
| 1101 | .thread = thread, |
| 1102 | .map = map, |
| 1103 | .dso = dso, |
| 1104 | .sym = sym, |
| 1105 | .ip = ip, |
| 1106 | .level = level, |
Peter Zijlstra | ea1900e | 2009-06-10 21:45:22 +0200 | [diff] [blame] | 1107 | .count = count, |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 1108 | .parent = NULL, |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 1109 | .sorted_chain = RB_ROOT |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1110 | }; |
| 1111 | int cmp; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1112 | |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1113 | if ((sort__has_parent || callchain) && chain) |
| 1114 | syms = resolve_callchain(thread, map, chain, &entry); |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1115 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1116 | while (*p != NULL) { |
| 1117 | parent = *p; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1118 | he = rb_entry(parent, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1119 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1120 | cmp = hist_entry__cmp(&entry, he); |
| 1121 | |
| 1122 | if (!cmp) { |
Peter Zijlstra | ea1900e | 2009-06-10 21:45:22 +0200 | [diff] [blame] | 1123 | he->count += count; |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1124 | if (callchain) { |
| 1125 | append_chain(&he->callchain, chain, syms); |
| 1126 | free(syms); |
| 1127 | } |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | if (cmp < 0) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1132 | p = &(*p)->rb_left; |
| 1133 | else |
| 1134 | p = &(*p)->rb_right; |
| 1135 | } |
| 1136 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1137 | he = malloc(sizeof(*he)); |
| 1138 | if (!he) |
| 1139 | return -ENOMEM; |
| 1140 | *he = entry; |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 1141 | if (callchain) { |
| 1142 | callchain_init(&he->callchain); |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 1143 | append_chain(&he->callchain, chain, syms); |
| 1144 | free(syms); |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 1145 | } |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1146 | rb_link_node(&he->rb_node, parent, p); |
| 1147 | rb_insert_color(&he->rb_node, &hist); |
| 1148 | |
| 1149 | return 0; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1150 | } |
| 1151 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 1152 | static void hist_entry__free(struct hist_entry *he) |
| 1153 | { |
| 1154 | free(he); |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * collapse the histogram |
| 1159 | */ |
| 1160 | |
| 1161 | static struct rb_root collapse_hists; |
| 1162 | |
| 1163 | static void collapse__insert_entry(struct hist_entry *he) |
| 1164 | { |
| 1165 | struct rb_node **p = &collapse_hists.rb_node; |
| 1166 | struct rb_node *parent = NULL; |
| 1167 | struct hist_entry *iter; |
| 1168 | int64_t cmp; |
| 1169 | |
| 1170 | while (*p != NULL) { |
| 1171 | parent = *p; |
| 1172 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 1173 | |
| 1174 | cmp = hist_entry__collapse(iter, he); |
| 1175 | |
| 1176 | if (!cmp) { |
| 1177 | iter->count += he->count; |
| 1178 | hist_entry__free(he); |
| 1179 | return; |
| 1180 | } |
| 1181 | |
| 1182 | if (cmp < 0) |
| 1183 | p = &(*p)->rb_left; |
| 1184 | else |
| 1185 | p = &(*p)->rb_right; |
| 1186 | } |
| 1187 | |
| 1188 | rb_link_node(&he->rb_node, parent, p); |
| 1189 | rb_insert_color(&he->rb_node, &collapse_hists); |
| 1190 | } |
| 1191 | |
| 1192 | static void collapse__resort(void) |
| 1193 | { |
| 1194 | struct rb_node *next; |
| 1195 | struct hist_entry *n; |
| 1196 | |
| 1197 | if (!sort__need_collapse) |
| 1198 | return; |
| 1199 | |
| 1200 | next = rb_first(&hist); |
| 1201 | while (next) { |
| 1202 | n = rb_entry(next, struct hist_entry, rb_node); |
| 1203 | next = rb_next(&n->rb_node); |
| 1204 | |
| 1205 | rb_erase(&n->rb_node, &hist); |
| 1206 | collapse__insert_entry(n); |
| 1207 | } |
| 1208 | } |
| 1209 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1210 | /* |
| 1211 | * reverse the map, sort on count. |
| 1212 | */ |
| 1213 | |
| 1214 | static struct rb_root output_hists; |
| 1215 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1216 | static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits) |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1217 | { |
| 1218 | struct rb_node **p = &output_hists.rb_node; |
| 1219 | struct rb_node *parent = NULL; |
| 1220 | struct hist_entry *iter; |
| 1221 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1222 | if (callchain) { |
| 1223 | if (callchain_mode == FLAT) |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1224 | sort_chain_flat(&he->sorted_chain, &he->callchain, |
| 1225 | min_callchain_hits); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1226 | else if (callchain_mode == GRAPH) |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1227 | sort_chain_graph(&he->sorted_chain, &he->callchain, |
| 1228 | min_callchain_hits); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1229 | } |
Frederic Weisbecker | f55c555 | 2009-06-26 16:28:01 +0200 | [diff] [blame] | 1230 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1231 | while (*p != NULL) { |
| 1232 | parent = *p; |
| 1233 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 1234 | |
| 1235 | if (he->count > iter->count) |
| 1236 | p = &(*p)->rb_left; |
| 1237 | else |
| 1238 | p = &(*p)->rb_right; |
| 1239 | } |
| 1240 | |
| 1241 | rb_link_node(&he->rb_node, parent, p); |
| 1242 | rb_insert_color(&he->rb_node, &output_hists); |
| 1243 | } |
| 1244 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1245 | static void output__resort(u64 total_samples) |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1246 | { |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 1247 | struct rb_node *next; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1248 | struct hist_entry *n; |
Arnaldo Carvalho de Melo | a4c43be | 2009-06-03 23:02:33 -0300 | [diff] [blame] | 1249 | struct rb_root *tree = &hist; |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1250 | u64 min_callchain_hits; |
| 1251 | |
| 1252 | min_callchain_hits = total_samples * (callchain_min_percent / 100); |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1253 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 1254 | if (sort__need_collapse) |
Arnaldo Carvalho de Melo | a4c43be | 2009-06-03 23:02:33 -0300 | [diff] [blame] | 1255 | tree = &collapse_hists; |
| 1256 | |
| 1257 | next = rb_first(tree); |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 1258 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1259 | while (next) { |
| 1260 | n = rb_entry(next, struct hist_entry, rb_node); |
| 1261 | next = rb_next(&n->rb_node); |
| 1262 | |
Arnaldo Carvalho de Melo | a4c43be | 2009-06-03 23:02:33 -0300 | [diff] [blame] | 1263 | rb_erase(&n->rb_node, tree); |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1264 | output__insert_entry(n, min_callchain_hits); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1268 | static size_t output__fprintf(FILE *fp, u64 total_samples) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1269 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1270 | struct hist_entry *pos; |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 1271 | struct sort_entry *se; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1272 | struct rb_node *nd; |
| 1273 | size_t ret = 0; |
| 1274 | |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 1275 | fprintf(fp, "\n"); |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 1276 | fprintf(fp, "#\n"); |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1277 | fprintf(fp, "# (%Ld samples)\n", (u64)total_samples); |
Ingo Molnar | 05ca061 | 2009-06-04 14:21:16 +0200 | [diff] [blame] | 1278 | fprintf(fp, "#\n"); |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 1279 | |
| 1280 | fprintf(fp, "# Overhead"); |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 1281 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 1282 | if (exclude_other && (se == &sort_parent)) |
| 1283 | continue; |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 1284 | fprintf(fp, " %s", se->header); |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 1285 | } |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 1286 | fprintf(fp, "\n"); |
| 1287 | |
| 1288 | fprintf(fp, "# ........"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 1289 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1290 | unsigned int i; |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 1291 | |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 1292 | if (exclude_other && (se == &sort_parent)) |
| 1293 | continue; |
| 1294 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 1295 | fprintf(fp, " "); |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 1296 | for (i = 0; i < strlen(se->header); i++) |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 1297 | fprintf(fp, "."); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 1298 | } |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 1299 | fprintf(fp, "\n"); |
| 1300 | |
| 1301 | fprintf(fp, "#\n"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 1302 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1303 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
| 1304 | pos = rb_entry(nd, struct hist_entry, rb_node); |
| 1305 | ret += hist_entry__fprintf(fp, pos, total_samples); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1306 | } |
| 1307 | |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 1308 | if (sort_order == default_sort_order && |
| 1309 | parent_pattern == default_parent_pattern) { |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 1310 | fprintf(fp, "#\n"); |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 1311 | 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] | 1312 | fprintf(fp, "#\n"); |
| 1313 | } |
Peter Zijlstra | 71dd894 | 2009-06-04 15:16:56 +0200 | [diff] [blame] | 1314 | fprintf(fp, "\n"); |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 1315 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 1316 | return ret; |
| 1317 | } |
| 1318 | |
Peter Zijlstra | 436224a | 2009-06-02 21:02:36 +0200 | [diff] [blame] | 1319 | static void register_idle_thread(void) |
| 1320 | { |
| 1321 | struct thread *thread = threads__findnew(0); |
| 1322 | |
| 1323 | if (thread == NULL || |
| 1324 | thread__set_comm(thread, "[idle]")) { |
| 1325 | fprintf(stderr, "problem inserting idle task.\n"); |
| 1326 | exit(-1); |
| 1327 | } |
| 1328 | } |
| 1329 | |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 1330 | static unsigned long total = 0, |
| 1331 | total_mmap = 0, |
| 1332 | total_comm = 0, |
| 1333 | total_fork = 0, |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 1334 | total_unknown = 0, |
| 1335 | total_lost = 0; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1336 | |
Peter Zijlstra | 2a0a50f | 2009-06-18 22:20:45 +0200 | [diff] [blame] | 1337 | static int validate_chain(struct ip_callchain *chain, event_t *event) |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 1338 | { |
| 1339 | unsigned int chain_size; |
| 1340 | |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 1341 | chain_size = event->header.size; |
| 1342 | chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event; |
| 1343 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1344 | if (chain->nr*sizeof(u64) > chain_size) |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 1345 | return -1; |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1350 | static int |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1351 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1352 | { |
| 1353 | char level; |
| 1354 | int show = 0; |
| 1355 | struct dso *dso = NULL; |
| 1356 | struct thread *thread = threads__findnew(event->ip.pid); |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1357 | u64 ip = event->ip.ip; |
| 1358 | u64 period = 1; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1359 | struct map *map = NULL; |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1360 | void *more_data = event->ip.__more_data; |
Peter Zijlstra | 2a0a50f | 2009-06-18 22:20:45 +0200 | [diff] [blame] | 1361 | struct ip_callchain *chain = NULL; |
Anton Blanchard | d8db1b5 | 2009-07-01 09:00:48 +1000 | [diff] [blame] | 1362 | int cpumode; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1363 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1364 | if (sample_type & PERF_SAMPLE_PERIOD) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1365 | period = *(u64 *)more_data; |
| 1366 | more_data += sizeof(u64); |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1367 | } |
Peter Zijlstra | ea1900e | 2009-06-10 21:45:22 +0200 | [diff] [blame] | 1368 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1369 | dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n", |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1370 | (void *)(offset + head), |
| 1371 | (void *)(long)(event->header.size), |
| 1372 | event->header.misc, |
| 1373 | event->ip.pid, |
Peter Zijlstra | 4502d77 | 2009-06-10 15:03:06 +0200 | [diff] [blame] | 1374 | (void *)(long)ip, |
Peter Zijlstra | ea1900e | 2009-06-10 21:45:22 +0200 | [diff] [blame] | 1375 | (long long)period); |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1376 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1377 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1378 | unsigned int i; |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1379 | |
| 1380 | chain = (void *)more_data; |
| 1381 | |
Peter Zijlstra | 2a0a50f | 2009-06-18 22:20:45 +0200 | [diff] [blame] | 1382 | dprintf("... chain: nr:%Lu\n", chain->nr); |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1383 | |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 1384 | if (validate_chain(chain, event) < 0) { |
| 1385 | eprintf("call-chain problem with event, skipping it.\n"); |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | if (dump_trace) { |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1390 | for (i = 0; i < chain->nr; i++) |
Peter Zijlstra | 2a0a50f | 2009-06-18 22:20:45 +0200 | [diff] [blame] | 1391 | dprintf("..... %2d: %016Lx\n", i, chain->ips[i]); |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1395 | dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 1396 | |
| 1397 | if (thread == NULL) { |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 1398 | eprintf("problem processing %d event, skipping it.\n", |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1399 | event->header.type); |
| 1400 | return -1; |
| 1401 | } |
| 1402 | |
Arnaldo Carvalho de Melo | cc8b88b | 2009-06-30 19:01:21 -0300 | [diff] [blame] | 1403 | if (comm_list && !strlist__has_entry(comm_list, thread->comm)) |
| 1404 | return 0; |
| 1405 | |
Anton Blanchard | d8db1b5 | 2009-07-01 09:00:48 +1000 | [diff] [blame] | 1406 | cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK; |
| 1407 | |
| 1408 | if (cpumode == PERF_EVENT_MISC_KERNEL) { |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1409 | show = SHOW_KERNEL; |
| 1410 | level = 'k'; |
| 1411 | |
| 1412 | dso = kernel_dso; |
| 1413 | |
| 1414 | dprintf(" ...... dso: %s\n", dso->name); |
| 1415 | |
Anton Blanchard | d8db1b5 | 2009-07-01 09:00:48 +1000 | [diff] [blame] | 1416 | } else if (cpumode == PERF_EVENT_MISC_USER) { |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1417 | |
| 1418 | show = SHOW_USER; |
| 1419 | level = '.'; |
| 1420 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1421 | } else { |
| 1422 | show = SHOW_HV; |
| 1423 | level = 'H'; |
Anton Blanchard | fb9c818 | 2009-07-01 09:00:49 +1000 | [diff] [blame] | 1424 | |
| 1425 | dso = hypervisor_dso; |
| 1426 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1427 | dprintf(" ...... dso: [hypervisor]\n"); |
| 1428 | } |
| 1429 | |
| 1430 | if (show & show_mask) { |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1431 | struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip); |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 1432 | |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 1433 | if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name)) |
| 1434 | return 0; |
| 1435 | |
Arnaldo Carvalho de Melo | 7bec7a9 | 2009-06-30 19:01:22 -0300 | [diff] [blame] | 1436 | if (sym_list && sym && !strlist__has_entry(sym_list, sym->name)) |
| 1437 | return 0; |
| 1438 | |
Peter Zijlstra | 6e7d6fd | 2009-06-17 15:51:44 +0200 | [diff] [blame] | 1439 | if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) { |
Ingo Molnar | 7522060 | 2009-06-18 08:00:17 +0200 | [diff] [blame] | 1440 | eprintf("problem incrementing symbol count, skipping event\n"); |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1441 | return -1; |
| 1442 | } |
| 1443 | } |
Peter Zijlstra | ea1900e | 2009-06-10 21:45:22 +0200 | [diff] [blame] | 1444 | total += period; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | static int |
| 1450 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) |
| 1451 | { |
| 1452 | struct thread *thread = threads__findnew(event->mmap.pid); |
| 1453 | struct map *map = map__new(&event->mmap); |
| 1454 | |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 1455 | dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n", |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1456 | (void *)(offset + head), |
| 1457 | (void *)(long)(event->header.size), |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 1458 | event->mmap.pid, |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1459 | (void *)(long)event->mmap.start, |
| 1460 | (void *)(long)event->mmap.len, |
| 1461 | (void *)(long)event->mmap.pgoff, |
| 1462 | event->mmap.filename); |
| 1463 | |
| 1464 | if (thread == NULL || map == NULL) { |
| 1465 | dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n"); |
Ingo Molnar | df97992 | 2009-06-04 13:41:22 +0200 | [diff] [blame] | 1466 | return 0; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | thread__insert_map(thread, map); |
| 1470 | total_mmap++; |
| 1471 | |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | static int |
| 1476 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 1477 | { |
| 1478 | struct thread *thread = threads__findnew(event->comm.pid); |
| 1479 | |
| 1480 | dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 1481 | (void *)(offset + head), |
| 1482 | (void *)(long)(event->header.size), |
| 1483 | event->comm.comm, event->comm.pid); |
| 1484 | |
| 1485 | if (thread == NULL || |
| 1486 | thread__set_comm(thread, event->comm.comm)) { |
| 1487 | dprintf("problem processing PERF_EVENT_COMM, skipping event.\n"); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | total_comm++; |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | static int |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 1496 | process_fork_event(event_t *event, unsigned long offset, unsigned long head) |
| 1497 | { |
| 1498 | struct thread *thread = threads__findnew(event->fork.pid); |
| 1499 | struct thread *parent = threads__findnew(event->fork.ppid); |
| 1500 | |
| 1501 | dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n", |
| 1502 | (void *)(offset + head), |
| 1503 | (void *)(long)(event->header.size), |
| 1504 | event->fork.pid, event->fork.ppid); |
| 1505 | |
| 1506 | if (!thread || !parent || thread__fork(thread, parent)) { |
| 1507 | dprintf("problem processing PERF_EVENT_FORK, skipping event.\n"); |
| 1508 | return -1; |
| 1509 | } |
| 1510 | total_fork++; |
| 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | static int |
Ingo Molnar | b2fef07 | 2009-06-05 18:07:51 +0200 | [diff] [blame] | 1516 | process_period_event(event_t *event, unsigned long offset, unsigned long head) |
| 1517 | { |
| 1518 | dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n", |
| 1519 | (void *)(offset + head), |
| 1520 | (void *)(long)(event->header.size), |
| 1521 | event->period.time, |
| 1522 | event->period.id, |
| 1523 | event->period.sample_period); |
| 1524 | |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 1528 | static int |
| 1529 | process_lost_event(event_t *event, unsigned long offset, unsigned long head) |
| 1530 | { |
| 1531 | dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n", |
| 1532 | (void *)(offset + head), |
| 1533 | (void *)(long)(event->header.size), |
| 1534 | event->lost.id, |
| 1535 | event->lost.lost); |
| 1536 | |
| 1537 | total_lost += event->lost.lost; |
| 1538 | |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1542 | static void trace_event(event_t *event) |
| 1543 | { |
| 1544 | unsigned char *raw_event = (void *)event; |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1545 | char *color = PERF_COLOR_BLUE; |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1546 | int i, j; |
| 1547 | |
| 1548 | if (!dump_trace) |
| 1549 | return; |
| 1550 | |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1551 | dprintf("."); |
| 1552 | cdprintf("\n. ... raw event: size %d bytes\n", event->header.size); |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1553 | |
| 1554 | for (i = 0; i < event->header.size; i++) { |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1555 | if ((i & 15) == 0) { |
| 1556 | dprintf("."); |
| 1557 | cdprintf(" %04x: ", i); |
| 1558 | } |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1559 | |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1560 | cdprintf(" %02x", raw_event[i]); |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1561 | |
| 1562 | if (((i & 15) == 15) || i == event->header.size-1) { |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1563 | cdprintf(" "); |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1564 | for (j = 0; j < 15-(i & 15); j++) |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1565 | cdprintf(" "); |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1566 | for (j = 0; j < (i & 15); j++) { |
Peter Zijlstra | a73c7d8 | 2009-06-18 09:44:20 +0200 | [diff] [blame] | 1567 | if (isprint(raw_event[i-15+j])) |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1568 | cdprintf("%c", raw_event[i-15+j]); |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1569 | else |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1570 | cdprintf("."); |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1571 | } |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 1572 | cdprintf("\n"); |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | dprintf(".\n"); |
| 1576 | } |
| 1577 | |
Ingo Molnar | b2fef07 | 2009-06-05 18:07:51 +0200 | [diff] [blame] | 1578 | static int |
Peter Zijlstra | e9ea2fd | 2009-06-24 22:46:04 +0200 | [diff] [blame] | 1579 | process_read_event(event_t *event, unsigned long offset, unsigned long head) |
| 1580 | { |
| 1581 | dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n", |
| 1582 | (void *)(offset + head), |
| 1583 | (void *)(long)(event->header.size), |
| 1584 | event->read.pid, |
| 1585 | event->read.tid, |
| 1586 | event->read.value); |
| 1587 | |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
| 1591 | static int |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1592 | 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] | 1593 | { |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1594 | trace_event(event); |
| 1595 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1596 | switch (event->header.type) { |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1597 | case PERF_EVENT_SAMPLE: |
| 1598 | return process_sample_event(event, offset, head); |
| 1599 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1600 | case PERF_EVENT_MMAP: |
| 1601 | return process_mmap_event(event, offset, head); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1602 | |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1603 | case PERF_EVENT_COMM: |
| 1604 | return process_comm_event(event, offset, head); |
Ingo Molnar | ed966aa | 2009-06-03 10:39:26 +0200 | [diff] [blame] | 1605 | |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 1606 | case PERF_EVENT_FORK: |
| 1607 | return process_fork_event(event, offset, head); |
| 1608 | |
Ingo Molnar | b2fef07 | 2009-06-05 18:07:51 +0200 | [diff] [blame] | 1609 | case PERF_EVENT_PERIOD: |
| 1610 | return process_period_event(event, offset, head); |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 1611 | |
| 1612 | case PERF_EVENT_LOST: |
| 1613 | return process_lost_event(event, offset, head); |
| 1614 | |
Peter Zijlstra | e9ea2fd | 2009-06-24 22:46:04 +0200 | [diff] [blame] | 1615 | case PERF_EVENT_READ: |
| 1616 | return process_read_event(event, offset, head); |
| 1617 | |
Ingo Molnar | d11444d | 2009-06-03 23:29:14 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * We dont process them right now but they are fine: |
| 1620 | */ |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 1621 | |
Ingo Molnar | d11444d | 2009-06-03 23:29:14 +0200 | [diff] [blame] | 1622 | case PERF_EVENT_THROTTLE: |
| 1623 | case PERF_EVENT_UNTHROTTLE: |
| 1624 | return 0; |
| 1625 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1626 | default: |
| 1627 | return -1; |
| 1628 | } |
| 1629 | |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1633 | static struct perf_header *header; |
| 1634 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1635 | static u64 perf_header__sample_type(void) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1636 | { |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1637 | u64 sample_type = 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1638 | int i; |
| 1639 | |
| 1640 | for (i = 0; i < header->attrs; i++) { |
| 1641 | struct perf_header_attr *attr = header->attr[i]; |
| 1642 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1643 | if (!sample_type) |
| 1644 | sample_type = attr->attr.sample_type; |
| 1645 | else if (sample_type != attr->attr.sample_type) |
| 1646 | die("non matching sample_type"); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1649 | return sample_type; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1650 | } |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 1651 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1652 | static int __cmd_report(void) |
| 1653 | { |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1654 | int ret, rc = EXIT_FAILURE; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1655 | unsigned long offset = 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1656 | unsigned long head, shift; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1657 | struct stat stat; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1658 | event_t *event; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1659 | uint32_t size; |
Ingo Molnar | 7505172 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1660 | char *buf; |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1661 | |
| 1662 | register_idle_thread(); |
| 1663 | |
| 1664 | input = open(input_name, O_RDONLY); |
| 1665 | if (input < 0) { |
Ingo Molnar | a14832f | 2009-06-07 17:58:23 +0200 | [diff] [blame] | 1666 | fprintf(stderr, " failed to open file: %s", input_name); |
| 1667 | if (!strcmp(input_name, "perf.data")) |
| 1668 | fprintf(stderr, " (try 'perf record' first)"); |
| 1669 | fprintf(stderr, "\n"); |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1670 | exit(-1); |
| 1671 | } |
| 1672 | |
| 1673 | ret = fstat(input, &stat); |
| 1674 | if (ret < 0) { |
| 1675 | perror("failed to stat file"); |
| 1676 | exit(-1); |
| 1677 | } |
| 1678 | |
| 1679 | if (!stat.st_size) { |
| 1680 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 1681 | exit(0); |
| 1682 | } |
| 1683 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1684 | header = perf_header__read(input); |
| 1685 | head = header->data_offset; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 1686 | |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1687 | sample_type = perf_header__sample_type(); |
| 1688 | |
Frederic Weisbecker | 91b4eae | 2009-07-05 07:39:17 +0200 | [diff] [blame] | 1689 | if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { |
| 1690 | if (sort__has_parent) { |
| 1691 | fprintf(stderr, "selected --sort parent, but no" |
| 1692 | " callchain data. Did you call" |
| 1693 | " perf record without -g?\n"); |
| 1694 | exit(-1); |
| 1695 | } |
| 1696 | if (callchain) { |
| 1697 | fprintf(stderr, "selected -c but no callchain data." |
| 1698 | " Did you call perf record without" |
| 1699 | " -g?\n"); |
| 1700 | exit(-1); |
| 1701 | } |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 1702 | } |
| 1703 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1704 | if (load_kernel() < 0) { |
| 1705 | perror("failed to load kernel symbols"); |
| 1706 | return EXIT_FAILURE; |
| 1707 | } |
| 1708 | |
| 1709 | if (!full_paths) { |
| 1710 | if (getcwd(__cwd, sizeof(__cwd)) == NULL) { |
| 1711 | perror("failed to get the current directory"); |
| 1712 | return EXIT_FAILURE; |
| 1713 | } |
| 1714 | cwdlen = strlen(cwd); |
| 1715 | } else { |
| 1716 | cwd = NULL; |
| 1717 | cwdlen = 0; |
| 1718 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1719 | |
| 1720 | shift = page_size * (head / page_size); |
| 1721 | offset += shift; |
| 1722 | head -= shift; |
| 1723 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1724 | remap: |
| 1725 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 1726 | MAP_SHARED, input, offset); |
| 1727 | if (buf == MAP_FAILED) { |
| 1728 | perror("failed to mmap file"); |
| 1729 | exit(-1); |
| 1730 | } |
| 1731 | |
| 1732 | more: |
| 1733 | event = (event_t *)(buf + head); |
| 1734 | |
| 1735 | size = event->header.size; |
| 1736 | if (!size) |
| 1737 | size = 8; |
| 1738 | |
| 1739 | if (head + event->header.size >= page_size * mmap_window) { |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1740 | int ret; |
| 1741 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1742 | shift = page_size * (head / page_size); |
| 1743 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1744 | ret = munmap(buf, page_size * mmap_window); |
| 1745 | assert(ret == 0); |
| 1746 | |
| 1747 | offset += shift; |
| 1748 | head -= shift; |
| 1749 | goto remap; |
| 1750 | } |
| 1751 | |
| 1752 | size = event->header.size; |
| 1753 | |
Ingo Molnar | 8465b05 | 2009-06-14 14:44:07 +0200 | [diff] [blame] | 1754 | dprintf("\n%p [%p]: event: %d\n", |
Ingo Molnar | b2fef07 | 2009-06-05 18:07:51 +0200 | [diff] [blame] | 1755 | (void *)(offset + head), |
| 1756 | (void *)(long)event->header.size, |
| 1757 | event->header.type); |
| 1758 | |
Ingo Molnar | d80d338 | 2009-06-03 23:14:49 +0200 | [diff] [blame] | 1759 | if (!size || process_event(event, offset, head) < 0) { |
| 1760 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 1761 | dprintf("%p [%p]: skipping unknown header type: %d\n", |
| 1762 | (void *)(offset + head), |
| 1763 | (void *)(long)(event->header.size), |
| 1764 | event->header.type); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 1765 | |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 1766 | total_unknown++; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1767 | |
| 1768 | /* |
| 1769 | * assume we lost track of the stream, check alignment, and |
| 1770 | * increment a single u64 in the hope to catch on again 'soon'. |
| 1771 | */ |
| 1772 | |
| 1773 | if (unlikely(head & 7)) |
| 1774 | head &= ~7ULL; |
| 1775 | |
| 1776 | size = 8; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1777 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1778 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1779 | head += size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 1780 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1781 | if (offset + head >= header->data_offset + header->data_size) |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 1782 | goto done; |
| 1783 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1784 | if (offset + head < (unsigned long)stat.st_size) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1785 | goto more; |
| 1786 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 1787 | done: |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1788 | rc = EXIT_SUCCESS; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1789 | close(input); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1790 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 1791 | dprintf(" IP events: %10ld\n", total); |
| 1792 | dprintf(" mmap events: %10ld\n", total_mmap); |
| 1793 | dprintf(" comm events: %10ld\n", total_comm); |
Peter Zijlstra | 62fc445 | 2009-06-04 16:53:49 +0200 | [diff] [blame] | 1794 | dprintf(" fork events: %10ld\n", total_fork); |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 1795 | dprintf(" lost events: %10ld\n", total_lost); |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 1796 | dprintf(" unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1797 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 1798 | if (dump_trace) |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1799 | return 0; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1800 | |
Arnaldo Carvalho de Melo | 9ac9954 | 2009-06-04 13:54:00 -0300 | [diff] [blame] | 1801 | if (verbose >= 3) |
| 1802 | threads__fprintf(stdout); |
| 1803 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1804 | if (verbose >= 2) |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1805 | dsos__fprintf(stdout); |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1806 | |
Peter Zijlstra | 8229289 | 2009-06-03 12:37:36 +0200 | [diff] [blame] | 1807 | collapse__resort(); |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1808 | output__resort(total); |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1809 | output__fprintf(stdout, total); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1810 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1811 | return rc; |
| 1812 | } |
| 1813 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1814 | static int |
| 1815 | parse_callchain_opt(const struct option *opt __used, const char *arg, |
| 1816 | int unset __used) |
| 1817 | { |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1818 | char *tok; |
| 1819 | char *endptr; |
| 1820 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1821 | callchain = 1; |
| 1822 | |
| 1823 | if (!arg) |
| 1824 | return 0; |
| 1825 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1826 | tok = strtok((char *)arg, ","); |
| 1827 | if (!tok) |
| 1828 | return -1; |
| 1829 | |
| 1830 | /* get the output mode */ |
| 1831 | if (!strncmp(tok, "graph", strlen(arg))) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1832 | callchain_mode = GRAPH; |
| 1833 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1834 | else if (!strncmp(tok, "flat", strlen(arg))) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1835 | callchain_mode = FLAT; |
| 1836 | else |
| 1837 | return -1; |
| 1838 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1839 | /* get the min percentage */ |
| 1840 | tok = strtok(NULL, ","); |
| 1841 | if (!tok) |
| 1842 | return 0; |
| 1843 | |
| 1844 | callchain_min_percent = strtod(tok, &endptr); |
| 1845 | if (tok == endptr) |
| 1846 | return -1; |
| 1847 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 1848 | return 0; |
| 1849 | } |
| 1850 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1851 | static const char * const report_usage[] = { |
| 1852 | "perf report [<options>] <command>", |
| 1853 | NULL |
| 1854 | }; |
| 1855 | |
| 1856 | static const struct option options[] = { |
| 1857 | OPT_STRING('i', "input", &input_name, "file", |
| 1858 | "input file name"), |
Arnaldo Carvalho de Melo | 815e777 | 2009-05-26 19:46:14 -0300 | [diff] [blame] | 1859 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1860 | "be more verbose (show symbol address, etc)"), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1861 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1862 | "dump raw trace in ASCII"), |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 1863 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 1864 | OPT_BOOLEAN('m', "modules", &modules, |
| 1865 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
Ingo Molnar | 63299f0 | 2009-05-28 10:52:00 +0200 | [diff] [blame] | 1866 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 1867 | "sort by key(s): pid, comm, dso, symbol, parent"), |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 1868 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 1869 | "Don't shorten the pathnames taking into account the cwd"), |
Ingo Molnar | b25bcf2 | 2009-06-18 07:01:03 +0200 | [diff] [blame] | 1870 | OPT_STRING('p', "parent", &parent_pattern, "regex", |
| 1871 | "regex filter to identify parent, see: '--sort parent'"), |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 1872 | OPT_BOOLEAN('x', "exclude-other", &exclude_other, |
| 1873 | "Only display entries with parent-match"), |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 1874 | OPT_CALLBACK_DEFAULT('c', "callchain", NULL, "output_type,min_percent", |
| 1875 | "Display callchains using output_type and min percent threshold. " |
Frederic Weisbecker | be90388 | 2009-07-05 07:39:18 +0200 | [diff] [blame^] | 1876 | "Default: flat,0", &parse_callchain_opt, callchain_default_opt), |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 1877 | OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]", |
| 1878 | "only consider symbols in these dsos"), |
Arnaldo Carvalho de Melo | cc8b88b | 2009-06-30 19:01:21 -0300 | [diff] [blame] | 1879 | OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]", |
| 1880 | "only consider symbols in these comms"), |
Arnaldo Carvalho de Melo | 7bec7a9 | 2009-06-30 19:01:22 -0300 | [diff] [blame] | 1881 | OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]", |
| 1882 | "only consider these symbols"), |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1883 | OPT_END() |
| 1884 | }; |
| 1885 | |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame] | 1886 | static void setup_sorting(void) |
| 1887 | { |
| 1888 | char *tmp, *tok, *str = strdup(sort_order); |
| 1889 | |
| 1890 | for (tok = strtok_r(str, ", ", &tmp); |
| 1891 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 1892 | if (sort_dimension__add(tok) < 0) { |
| 1893 | error("Unknown --sort key: `%s'", tok); |
| 1894 | usage_with_options(report_usage, options); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | free(str); |
| 1899 | } |
| 1900 | |
Arnaldo Carvalho de Melo | cc8b88b | 2009-06-30 19:01:21 -0300 | [diff] [blame] | 1901 | static void setup_list(struct strlist **list, const char *list_str, |
| 1902 | const char *list_name) |
| 1903 | { |
| 1904 | if (list_str) { |
| 1905 | *list = strlist__new(true, list_str); |
| 1906 | if (!*list) { |
| 1907 | fprintf(stderr, "problems parsing %s list\n", |
| 1908 | list_name); |
| 1909 | exit(129); |
| 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1914 | int cmd_report(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1915 | { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1916 | symbol__init(); |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1917 | |
| 1918 | page_size = getpagesize(); |
| 1919 | |
Ingo Molnar | edc52de | 2009-06-04 16:24:37 +0200 | [diff] [blame] | 1920 | argc = parse_options(argc, argv, options, report_usage, 0); |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1921 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 1922 | setup_sorting(); |
| 1923 | |
Ingo Molnar | b8e6d82 | 2009-06-18 14:32:19 +0200 | [diff] [blame] | 1924 | if (parent_pattern != default_parent_pattern) |
| 1925 | sort_dimension__add("parent"); |
| 1926 | else |
| 1927 | exclude_other = 0; |
| 1928 | |
Ingo Molnar | edc52de | 2009-06-04 16:24:37 +0200 | [diff] [blame] | 1929 | /* |
| 1930 | * Any (unrecognized) arguments left? |
| 1931 | */ |
| 1932 | if (argc) |
| 1933 | usage_with_options(report_usage, options); |
| 1934 | |
Arnaldo Carvalho de Melo | cc8b88b | 2009-06-30 19:01:21 -0300 | [diff] [blame] | 1935 | setup_list(&dso_list, dso_list_str, "dso"); |
| 1936 | setup_list(&comm_list, comm_list_str, "comm"); |
Arnaldo Carvalho de Melo | 7bec7a9 | 2009-06-30 19:01:22 -0300 | [diff] [blame] | 1937 | setup_list(&sym_list, sym_list_str, "symbol"); |
Arnaldo Carvalho de Melo | 2590340 | 2009-06-30 19:01:20 -0300 | [diff] [blame] | 1938 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 1939 | setup_pager(); |
| 1940 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1941 | return __cmd_report(); |
| 1942 | } |