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