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