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