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