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