Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 1 | #include "../perf.h" |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 5 | #include "session.h" |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 6 | #include "thread.h" |
| 7 | #include "util.h" |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 8 | #include "debug.h" |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 10 | void map_groups__init(struct map_groups *self) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 11 | { |
| 12 | int i; |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 13 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
| 14 | self->maps[i] = RB_ROOT; |
| 15 | INIT_LIST_HEAD(&self->removed_maps[i]); |
| 16 | } |
| 17 | } |
| 18 | |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 19 | static struct thread *thread__new(pid_t pid) |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 20 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 21 | struct thread *self = zalloc(sizeof(*self)); |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 22 | |
| 23 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 24 | map_groups__init(&self->mg); |
| 25 | self->pid = pid; |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 26 | self->comm = malloc(32); |
| 27 | if (self->comm) |
| 28 | snprintf(self->comm, 32, ":%d", self->pid); |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | return self; |
| 32 | } |
| 33 | |
| 34 | int thread__set_comm(struct thread *self, const char *comm) |
| 35 | { |
| 36 | if (self->comm) |
| 37 | free(self->comm); |
| 38 | self->comm = strdup(comm); |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 39 | if (self->comm == NULL) |
| 40 | return -ENOMEM; |
| 41 | self->comm_set = true; |
| 42 | return 0; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 45 | int thread__comm_len(struct thread *self) |
| 46 | { |
| 47 | if (!self->comm_len) { |
| 48 | if (!self->comm) |
| 49 | return 0; |
| 50 | self->comm_len = strlen(self->comm); |
| 51 | } |
| 52 | |
| 53 | return self->comm_len; |
| 54 | } |
| 55 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 56 | static size_t __map_groups__fprintf_maps(struct map_groups *self, |
| 57 | enum map_type type, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 58 | { |
| 59 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); |
| 60 | struct rb_node *nd; |
| 61 | |
| 62 | for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) { |
| 63 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 64 | printed += fprintf(fp, "Map:"); |
| 65 | printed += map__fprintf(pos, fp); |
| 66 | if (verbose > 1) { |
| 67 | printed += dso__fprintf(pos->dso, type, fp); |
| 68 | printed += fprintf(fp, "--\n"); |
| 69 | } |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 70 | } |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 71 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 72 | return printed; |
| 73 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 74 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 75 | size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 76 | { |
| 77 | size_t printed = 0, i; |
| 78 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 79 | printed += __map_groups__fprintf_maps(self, i, fp); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 80 | return printed; |
| 81 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 82 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 83 | static size_t __map_groups__fprintf_removed_maps(struct map_groups *self, |
| 84 | enum map_type type, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 85 | { |
| 86 | struct map *pos; |
| 87 | size_t printed = 0; |
| 88 | |
| 89 | list_for_each_entry(pos, &self->removed_maps[type], node) { |
| 90 | printed += fprintf(fp, "Map:"); |
| 91 | printed += map__fprintf(pos, fp); |
| 92 | if (verbose > 1) { |
| 93 | printed += dso__fprintf(pos->dso, type, fp); |
| 94 | printed += fprintf(fp, "--\n"); |
| 95 | } |
| 96 | } |
| 97 | return printed; |
| 98 | } |
| 99 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 100 | static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 101 | { |
| 102 | size_t printed = 0, i; |
| 103 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 104 | printed += __map_groups__fprintf_removed_maps(self, i, fp); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 105 | return printed; |
| 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 108 | static size_t map_groups__fprintf(struct map_groups *self, FILE *fp) |
| 109 | { |
| 110 | size_t printed = map_groups__fprintf_maps(self, fp); |
| 111 | printed += fprintf(fp, "Removed maps:\n"); |
| 112 | return printed + map_groups__fprintf_removed_maps(self, fp); |
| 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 115 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 116 | { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 117 | return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) + |
| 118 | map_groups__fprintf(&self->mg, fp); |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 121 | struct thread *perf_session__findnew(struct perf_session *self, pid_t pid) |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 122 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 123 | struct rb_node **p = &self->threads.rb_node; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 124 | struct rb_node *parent = NULL; |
| 125 | struct thread *th; |
| 126 | |
| 127 | /* |
| 128 | * Font-end cache - PID lookups come in blocks, |
| 129 | * so most of the time we dont have to look up |
| 130 | * the full rbtree: |
| 131 | */ |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 132 | if (self->last_match && self->last_match->pid == pid) |
| 133 | return self->last_match; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 134 | |
| 135 | while (*p != NULL) { |
| 136 | parent = *p; |
| 137 | th = rb_entry(parent, struct thread, rb_node); |
| 138 | |
| 139 | if (th->pid == pid) { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 140 | self->last_match = th; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 141 | return th; |
| 142 | } |
| 143 | |
| 144 | if (pid < th->pid) |
| 145 | p = &(*p)->rb_left; |
| 146 | else |
| 147 | p = &(*p)->rb_right; |
| 148 | } |
| 149 | |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 150 | th = thread__new(pid); |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 151 | if (th != NULL) { |
| 152 | rb_link_node(&th->rb_node, parent, p); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 153 | rb_insert_color(&th->rb_node, &self->threads); |
| 154 | self->last_match = th; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return th; |
| 158 | } |
| 159 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 160 | static void map_groups__remove_overlappings(struct map_groups *self, |
| 161 | struct map *map) |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 162 | { |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 163 | struct rb_root *root = &self->maps[map->type]; |
| 164 | struct rb_node *next = rb_first(root); |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 165 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 166 | while (next) { |
| 167 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 168 | next = rb_next(&pos->rb_node); |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 169 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 170 | if (!map__overlap(pos, map)) |
| 171 | continue; |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 172 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 173 | if (verbose >= 2) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 174 | fputs("overlapping maps:\n", stderr); |
| 175 | map__fprintf(map, stderr); |
| 176 | map__fprintf(pos, stderr); |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 177 | } |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 178 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 179 | rb_erase(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 180 | /* |
| 181 | * We may have references to this map, for instance in some |
| 182 | * hist_entry instances, so just move them to a separate |
| 183 | * list. |
| 184 | */ |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 185 | list_add_tail(&pos->node, &self->removed_maps[map->type]); |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 186 | } |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 187 | } |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 188 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 189 | void maps__insert(struct rb_root *maps, struct map *map) |
| 190 | { |
| 191 | struct rb_node **p = &maps->rb_node; |
| 192 | struct rb_node *parent = NULL; |
| 193 | const u64 ip = map->start; |
| 194 | struct map *m; |
| 195 | |
| 196 | while (*p != NULL) { |
| 197 | parent = *p; |
| 198 | m = rb_entry(parent, struct map, rb_node); |
| 199 | if (ip < m->start) |
| 200 | p = &(*p)->rb_left; |
| 201 | else |
| 202 | p = &(*p)->rb_right; |
| 203 | } |
| 204 | |
| 205 | rb_link_node(&map->rb_node, parent, p); |
| 206 | rb_insert_color(&map->rb_node, maps); |
| 207 | } |
| 208 | |
| 209 | struct map *maps__find(struct rb_root *maps, u64 ip) |
| 210 | { |
| 211 | struct rb_node **p = &maps->rb_node; |
| 212 | struct rb_node *parent = NULL; |
| 213 | struct map *m; |
| 214 | |
| 215 | while (*p != NULL) { |
| 216 | parent = *p; |
| 217 | m = rb_entry(parent, struct map, rb_node); |
| 218 | if (ip < m->start) |
| 219 | p = &(*p)->rb_left; |
| 220 | else if (ip > m->end) |
| 221 | p = &(*p)->rb_right; |
| 222 | else |
| 223 | return m; |
| 224 | } |
| 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | void thread__insert_map(struct thread *self, struct map *map) |
| 230 | { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 231 | map_groups__remove_overlappings(&self->mg, map); |
| 232 | map_groups__insert(&self->mg, map); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 233 | } |
| 234 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 235 | /* |
| 236 | * XXX This should not really _copy_ te maps, but refcount them. |
| 237 | */ |
| 238 | static int map_groups__clone(struct map_groups *self, |
| 239 | struct map_groups *parent, enum map_type type) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 240 | { |
| 241 | struct rb_node *nd; |
| 242 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { |
| 243 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 244 | struct map *new = map__clone(map); |
| 245 | if (new == NULL) |
| 246 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 247 | map_groups__insert(self, new); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 248 | } |
| 249 | return 0; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | int thread__fork(struct thread *self, struct thread *parent) |
| 253 | { |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 254 | int i; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 255 | |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 256 | if (parent->comm_set) { |
| 257 | if (self->comm) |
| 258 | free(self->comm); |
| 259 | self->comm = strdup(parent->comm); |
| 260 | if (!self->comm) |
| 261 | return -ENOMEM; |
| 262 | self->comm_set = true; |
| 263 | } |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 264 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 265 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 266 | if (map_groups__clone(&self->mg, &parent->mg, i) < 0) |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 267 | return -ENOMEM; |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 271 | size_t perf_session__fprintf(struct perf_session *self, FILE *fp) |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 272 | { |
| 273 | size_t ret = 0; |
| 274 | struct rb_node *nd; |
| 275 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 276 | for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) { |
Frederic Weisbecker | 6baa0a5a | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 277 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 278 | |
| 279 | ret += thread__fprintf(pos, fp); |
| 280 | } |
| 281 | |
| 282 | return ret; |
| 283 | } |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 284 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 285 | struct symbol *map_groups__find_symbol(struct map_groups *self, |
| 286 | enum map_type type, u64 addr, |
| 287 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 288 | { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 289 | struct map *map = map_groups__find(self, type, addr); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 290 | |
| 291 | if (map != NULL) |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 292 | return map__find_symbol(map, map->map_ip(map, addr), filter); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 293 | |
| 294 | return NULL; |
| 295 | } |