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