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