10 void map_groups__init(struct map_groups *self)
13 for (i = 0; i < MAP__NR_TYPES; ++i) {
14 self->maps[i] = RB_ROOT;
15 INIT_LIST_HEAD(&self->removed_maps[i]);
19 static struct thread *thread__new(pid_t pid)
21 struct thread *self = zalloc(sizeof(*self));
24 map_groups__init(&self->mg);
26 self->comm = malloc(32);
28 snprintf(self->comm, 32, ":%d", self->pid);
34 int thread__set_comm(struct thread *self, const char *comm)
38 self->comm = strdup(comm);
39 if (self->comm == NULL)
41 self->comm_set = true;
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 size_t __map_groups__fprintf_maps(struct map_groups *self,
57 enum map_type type, FILE *fp)
59 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
62 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
63 struct map *pos = rb_entry(nd, struct map, rb_node);
64 printed += fprintf(fp, "Map:");
65 printed += map__fprintf(pos, fp);
67 printed += dso__fprintf(pos->dso, type, fp);
68 printed += fprintf(fp, "--\n");
75 size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
77 size_t printed = 0, i;
78 for (i = 0; i < MAP__NR_TYPES; ++i)
79 printed += __map_groups__fprintf_maps(self, i, fp);
83 static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
84 enum map_type type, FILE *fp)
89 list_for_each_entry(pos, &self->removed_maps[type], node) {
90 printed += fprintf(fp, "Map:");
91 printed += map__fprintf(pos, fp);
93 printed += dso__fprintf(pos->dso, type, fp);
94 printed += fprintf(fp, "--\n");
100 static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
102 size_t printed = 0, i;
103 for (i = 0; i < MAP__NR_TYPES; ++i)
104 printed += __map_groups__fprintf_removed_maps(self, i, fp);
108 static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
110 size_t printed = map_groups__fprintf_maps(self, fp);
111 printed += fprintf(fp, "Removed maps:\n");
112 return printed + map_groups__fprintf_removed_maps(self, fp);
115 static size_t thread__fprintf(struct thread *self, FILE *fp)
117 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
118 map_groups__fprintf(&self->mg, fp);
121 struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
123 struct rb_node **p = &self->threads.rb_node;
124 struct rb_node *parent = NULL;
128 * Font-end cache - PID lookups come in blocks,
129 * so most of the time we dont have to look up
132 if (self->last_match && self->last_match->pid == pid)
133 return self->last_match;
137 th = rb_entry(parent, struct thread, rb_node);
139 if (th->pid == pid) {
140 self->last_match = th;
150 th = thread__new(pid);
152 rb_link_node(&th->rb_node, parent, p);
153 rb_insert_color(&th->rb_node, &self->threads);
154 self->last_match = th;
160 static void map_groups__remove_overlappings(struct map_groups *self,
163 struct rb_root *root = &self->maps[map->type];
164 struct rb_node *next = rb_first(root);
167 struct map *pos = rb_entry(next, struct map, rb_node);
168 next = rb_next(&pos->rb_node);
170 if (!map__overlap(pos, map))
174 fputs("overlapping maps:\n", stderr);
175 map__fprintf(map, stderr);
176 map__fprintf(pos, stderr);
179 rb_erase(&pos->rb_node, root);
181 * We may have references to this map, for instance in some
182 * hist_entry instances, so just move them to a separate
185 list_add_tail(&pos->node, &self->removed_maps[map->type]);
189 void maps__insert(struct rb_root *maps, struct map *map)
191 struct rb_node **p = &maps->rb_node;
192 struct rb_node *parent = NULL;
193 const u64 ip = map->start;
198 m = rb_entry(parent, struct map, rb_node);
205 rb_link_node(&map->rb_node, parent, p);
206 rb_insert_color(&map->rb_node, maps);
209 struct map *maps__find(struct rb_root *maps, u64 ip)
211 struct rb_node **p = &maps->rb_node;
212 struct rb_node *parent = NULL;
217 m = rb_entry(parent, struct map, rb_node);
220 else if (ip > m->end)
229 void thread__insert_map(struct thread *self, struct map *map)
231 map_groups__remove_overlappings(&self->mg, map);
232 map_groups__insert(&self->mg, map);
236 * XXX This should not really _copy_ te maps, but refcount them.
238 static int map_groups__clone(struct map_groups *self,
239 struct map_groups *parent, enum map_type type)
242 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
243 struct map *map = rb_entry(nd, struct map, rb_node);
244 struct map *new = map__clone(map);
247 map_groups__insert(self, new);
252 int thread__fork(struct thread *self, struct thread *parent)
256 if (parent->comm_set) {
259 self->comm = strdup(parent->comm);
262 self->comm_set = true;
265 for (i = 0; i < MAP__NR_TYPES; ++i)
266 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
271 size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
276 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
277 struct thread *pos = rb_entry(nd, struct thread, rb_node);
279 ret += thread__fprintf(pos, fp);
285 struct symbol *map_groups__find_symbol(struct map_groups *self,
286 enum map_type type, u64 addr,
287 symbol_filter_t filter)
289 struct map *map = map_groups__find(self, type, addr);
292 return map__find_symbol(map, map->map_ip(map, addr), filter);