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