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