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