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