9 static struct rb_root threads;
10 static struct thread *last_match;
12 static struct thread *thread__new(pid_t pid)
14 struct thread *self = calloc(1, sizeof(*self));
18 self->comm = malloc(32);
20 snprintf(self->comm, 32, ":%d", self->pid);
22 INIT_LIST_HEAD(&self->removed_maps);
28 int thread__set_comm(struct thread *self, const char *comm)
32 self->comm = strdup(comm);
33 return self->comm ? 0 : -ENOMEM;
36 static size_t thread__fprintf(struct thread *self, FILE *fp)
40 size_t ret = fprintf(fp, "Thread %d %s\nCurrent maps:\n",
41 self->pid, self->comm);
43 for (nd = rb_first(&self->maps); nd; nd = rb_next(nd)) {
44 pos = rb_entry(nd, struct map, rb_node);
45 ret += map__fprintf(pos, fp);
48 ret = fprintf(fp, "Removed maps:\n");
50 list_for_each_entry(pos, &self->removed_maps, node)
51 ret += map__fprintf(pos, fp);
56 struct thread *threads__findnew(pid_t pid)
58 struct rb_node **p = &threads.rb_node;
59 struct rb_node *parent = NULL;
63 * Font-end cache - PID lookups come in blocks,
64 * so most of the time we dont have to look up
67 if (last_match && last_match->pid == pid)
72 th = rb_entry(parent, struct thread, rb_node);
85 th = thread__new(pid);
87 rb_link_node(&th->rb_node, parent, p);
88 rb_insert_color(&th->rb_node, &threads);
95 struct thread *register_idle_thread(void)
97 struct thread *thread = threads__findnew(0);
99 if (!thread || thread__set_comm(thread, "swapper")) {
100 fprintf(stderr, "problem inserting idle task.\n");
107 static void thread__remove_overlappings(struct thread *self, struct map *map)
109 struct rb_node *next = rb_first(&self->maps);
112 struct map *pos = rb_entry(next, struct map, rb_node);
113 next = rb_next(&pos->rb_node);
115 if (!map__overlap(pos, map))
119 printf("overlapping maps:\n");
120 map__fprintf(map, stdout);
121 map__fprintf(pos, stdout);
124 rb_erase(&pos->rb_node, &self->maps);
126 * We may have references to this map, for instance in some
127 * hist_entry instances, so just move them to a separate
130 list_add_tail(&pos->node, &self->removed_maps);
134 void maps__insert(struct rb_root *maps, struct map *map)
136 struct rb_node **p = &maps->rb_node;
137 struct rb_node *parent = NULL;
138 const u64 ip = map->start;
143 m = rb_entry(parent, struct map, rb_node);
150 rb_link_node(&map->rb_node, parent, p);
151 rb_insert_color(&map->rb_node, maps);
154 struct map *maps__find(struct rb_root *maps, u64 ip)
156 struct rb_node **p = &maps->rb_node;
157 struct rb_node *parent = NULL;
162 m = rb_entry(parent, struct map, rb_node);
165 else if (ip > m->end)
174 void thread__insert_map(struct thread *self, struct map *map)
176 thread__remove_overlappings(self, map);
177 maps__insert(&self->maps, map);
180 int thread__fork(struct thread *self, struct thread *parent)
186 self->comm = strdup(parent->comm);
190 for (nd = rb_first(&parent->maps); nd; nd = rb_next(nd)) {
191 struct map *map = rb_entry(nd, struct map, rb_node);
192 struct map *new = map__clone(map);
195 thread__insert_map(self, new);
201 size_t threads__fprintf(FILE *fp)
206 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
207 struct thread *pos = rb_entry(nd, struct thread, rb_node);
209 ret += thread__fprintf(pos, fp);