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