9 static struct rb_root threads;
10 static struct thread *last_match;
12 void thread__init(struct thread *self, pid_t pid)
17 for (i = 0; i < MAP__NR_TYPES; ++i) {
18 self->maps[i] = RB_ROOT;
19 INIT_LIST_HEAD(&self->removed_maps[i]);
23 static struct thread *thread__new(pid_t pid)
25 struct thread *self = zalloc(sizeof(*self));
28 thread__init(self, pid);
29 self->comm = malloc(32);
31 snprintf(self->comm, 32, ":%d", self->pid);
37 int thread__set_comm(struct thread *self, const char *comm)
41 self->comm = strdup(comm);
42 return self->comm ? 0 : -ENOMEM;
45 int thread__comm_len(struct thread *self)
47 if (!self->comm_len) {
50 self->comm_len = strlen(self->comm);
53 return self->comm_len;
56 static const char *map_type__name[MAP__NR_TYPES] = {
57 [MAP__FUNCTION] = "Functions",
60 static size_t __thread__fprintf_maps(struct thread *self,
61 enum map_type type, FILE *fp)
63 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
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);
71 printed += dso__fprintf(pos->dso, type, fp);
72 printed += fprintf(fp, "--\n");
79 size_t thread__fprintf_maps(struct thread *self, FILE *fp)
81 size_t printed = 0, i;
82 for (i = 0; i < MAP__NR_TYPES; ++i)
83 printed += __thread__fprintf_maps(self, i, fp);
87 static size_t __thread__fprintf_removed_maps(struct thread *self,
88 enum map_type type, FILE *fp)
93 list_for_each_entry(pos, &self->removed_maps[type], node) {
94 printed += fprintf(fp, "Map:");
95 printed += map__fprintf(pos, fp);
97 printed += dso__fprintf(pos->dso, type, fp);
98 printed += fprintf(fp, "--\n");
104 static size_t thread__fprintf_removed_maps(struct thread *self, FILE *fp)
106 size_t printed = 0, i;
107 for (i = 0; i < MAP__NR_TYPES; ++i)
108 printed += __thread__fprintf_removed_maps(self, i, fp);
112 static size_t thread__fprintf(struct thread *self, FILE *fp)
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);
120 struct thread *threads__findnew(pid_t pid)
122 struct rb_node **p = &threads.rb_node;
123 struct rb_node *parent = NULL;
127 * Font-end cache - PID lookups come in blocks,
128 * so most of the time we dont have to look up
131 if (last_match && last_match->pid == pid)
136 th = rb_entry(parent, struct thread, rb_node);
138 if (th->pid == pid) {
149 th = thread__new(pid);
151 rb_link_node(&th->rb_node, parent, p);
152 rb_insert_color(&th->rb_node, &threads);
159 struct thread *register_idle_thread(void)
161 struct thread *thread = threads__findnew(0);
163 if (!thread || thread__set_comm(thread, "swapper")) {
164 fprintf(stderr, "problem inserting idle task.\n");
171 static void thread__remove_overlappings(struct thread *self, struct map *map)
173 struct rb_root *root = &self->maps[map->type];
174 struct rb_node *next = rb_first(root);
177 struct map *pos = rb_entry(next, struct map, rb_node);
178 next = rb_next(&pos->rb_node);
180 if (!map__overlap(pos, map))
184 fputs("overlapping maps:\n", stderr);
185 map__fprintf(map, stderr);
186 map__fprintf(pos, stderr);
189 rb_erase(&pos->rb_node, root);
191 * We may have references to this map, for instance in some
192 * hist_entry instances, so just move them to a separate
195 list_add_tail(&pos->node, &self->removed_maps[map->type]);
199 void maps__insert(struct rb_root *maps, struct map *map)
201 struct rb_node **p = &maps->rb_node;
202 struct rb_node *parent = NULL;
203 const u64 ip = map->start;
208 m = rb_entry(parent, struct map, rb_node);
215 rb_link_node(&map->rb_node, parent, p);
216 rb_insert_color(&map->rb_node, maps);
219 struct map *maps__find(struct rb_root *maps, u64 ip)
221 struct rb_node **p = &maps->rb_node;
222 struct rb_node *parent = NULL;
227 m = rb_entry(parent, struct map, rb_node);
230 else if (ip > m->end)
239 void thread__insert_map(struct thread *self, struct map *map)
241 thread__remove_overlappings(self, map);
242 maps__insert(&self->maps[map->type], map);
245 static int thread__clone_maps(struct thread *self, struct thread *parent,
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);
254 thread__insert_map(self, new);
259 int thread__fork(struct thread *self, struct thread *parent)
265 self->comm = strdup(parent->comm);
269 for (i = 0; i < MAP__NR_TYPES; ++i)
270 if (thread__clone_maps(self, parent, i) < 0)
275 size_t threads__fprintf(FILE *fp)
280 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
281 struct thread *pos = rb_entry(nd, struct thread, rb_node);
283 ret += thread__fprintf(pos, fp);
289 struct symbol *thread__find_symbol(struct thread *self,
290 enum map_type type, u64 addr,
291 symbol_filter_t filter)
293 struct map *map = thread__find_map(self, type, addr);
296 return map__find_symbol(map, map->map_ip(map, addr), filter);