blob: b68a00ea41210cbb69a80d41794700d0bfaccb97 [file] [log] [blame]
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include "thread.h"
6#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02007#include "debug.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02008
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03009static struct rb_root threads;
10static struct thread *last_match;
11
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020012void map_groups__init(struct map_groups *self)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020013{
14 int i;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020015 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 Weisbecker97ea1a72009-10-08 21:04:17 +020021static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020022{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020023 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020024
25 if (self != NULL) {
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020026 map_groups__init(&self->mg);
27 self->pid = pid;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020028 self->comm = malloc(32);
29 if (self->comm)
30 snprintf(self->comm, 32, ":%d", self->pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020031 }
32
33 return self;
34}
35
36int 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 Weisbeckera4fb5812009-10-22 23:23:23 +020044int 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 Melo95011c62009-11-27 16:29:20 -020055static const char *map_type__name[MAP__NR_TYPES] = {
56 [MAP__FUNCTION] = "Functions",
Arnaldo Carvalho de Melo22ccec52009-12-11 15:23:28 -020057 [MAP__VARIABLE] = "Variables",
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020058};
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020059
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020060static size_t __map_groups__fprintf_maps(struct map_groups *self,
61 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020062{
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 Melo1b46cdd2009-09-28 14:48:46 -030074 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020075
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020076 return printed;
77}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030078
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020079size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020080{
81 size_t printed = 0, i;
82 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020083 printed += __map_groups__fprintf_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020084 return printed;
85}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030086
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020087static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
88 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020089{
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
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200104static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200105{
106 size_t printed = 0, i;
107 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200108 printed += __map_groups__fprintf_removed_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200109 return printed;
110}
111
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200112static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
113{
114 size_t printed = map_groups__fprintf_maps(self, fp);
115 printed += fprintf(fp, "Removed maps:\n");
116 return printed + map_groups__fprintf_removed_maps(self, fp);
117}
118
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200119static size_t thread__fprintf(struct thread *self, FILE *fp)
120{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200121 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
122 map_groups__fprintf(&self->mg, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200123}
124
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300125struct thread *threads__findnew(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200126{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300127 struct rb_node **p = &threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200128 struct rb_node *parent = NULL;
129 struct thread *th;
130
131 /*
132 * Font-end cache - PID lookups come in blocks,
133 * so most of the time we dont have to look up
134 * the full rbtree:
135 */
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300136 if (last_match && last_match->pid == pid)
137 return last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200138
139 while (*p != NULL) {
140 parent = *p;
141 th = rb_entry(parent, struct thread, rb_node);
142
143 if (th->pid == pid) {
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300144 last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200145 return th;
146 }
147
148 if (pid < th->pid)
149 p = &(*p)->rb_left;
150 else
151 p = &(*p)->rb_right;
152 }
153
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200154 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200155 if (th != NULL) {
156 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300157 rb_insert_color(&th->rb_node, &threads);
158 last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200159 }
160
161 return th;
162}
163
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300164struct thread *register_idle_thread(void)
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200165{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300166 struct thread *thread = threads__findnew(0);
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200167
Ingo Molnar80ed0982009-09-16 14:12:36 +0200168 if (!thread || thread__set_comm(thread, "swapper")) {
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200169 fprintf(stderr, "problem inserting idle task.\n");
170 exit(-1);
171 }
172
173 return thread;
174}
175
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200176static void map_groups__remove_overlappings(struct map_groups *self,
177 struct map *map)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200178{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200179 struct rb_root *root = &self->maps[map->type];
180 struct rb_node *next = rb_first(root);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200181
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300182 while (next) {
183 struct map *pos = rb_entry(next, struct map, rb_node);
184 next = rb_next(&pos->rb_node);
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200185
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300186 if (!map__overlap(pos, map))
187 continue;
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200188
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300189 if (verbose >= 2) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200190 fputs("overlapping maps:\n", stderr);
191 map__fprintf(map, stderr);
192 map__fprintf(pos, stderr);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300193 }
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200194
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200195 rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300196 /*
197 * We may have references to this map, for instance in some
198 * hist_entry instances, so just move them to a separate
199 * list.
200 */
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200201 list_add_tail(&pos->node, &self->removed_maps[map->type]);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200202 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300203}
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200204
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300205void maps__insert(struct rb_root *maps, struct map *map)
206{
207 struct rb_node **p = &maps->rb_node;
208 struct rb_node *parent = NULL;
209 const u64 ip = map->start;
210 struct map *m;
211
212 while (*p != NULL) {
213 parent = *p;
214 m = rb_entry(parent, struct map, rb_node);
215 if (ip < m->start)
216 p = &(*p)->rb_left;
217 else
218 p = &(*p)->rb_right;
219 }
220
221 rb_link_node(&map->rb_node, parent, p);
222 rb_insert_color(&map->rb_node, maps);
223}
224
225struct map *maps__find(struct rb_root *maps, u64 ip)
226{
227 struct rb_node **p = &maps->rb_node;
228 struct rb_node *parent = NULL;
229 struct map *m;
230
231 while (*p != NULL) {
232 parent = *p;
233 m = rb_entry(parent, struct map, rb_node);
234 if (ip < m->start)
235 p = &(*p)->rb_left;
236 else if (ip > m->end)
237 p = &(*p)->rb_right;
238 else
239 return m;
240 }
241
242 return NULL;
243}
244
245void thread__insert_map(struct thread *self, struct map *map)
246{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200247 map_groups__remove_overlappings(&self->mg, map);
248 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200249}
250
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200251/*
252 * XXX This should not really _copy_ te maps, but refcount them.
253 */
254static int map_groups__clone(struct map_groups *self,
255 struct map_groups *parent, enum map_type type)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200256{
257 struct rb_node *nd;
258 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
259 struct map *map = rb_entry(nd, struct map, rb_node);
260 struct map *new = map__clone(map);
261 if (new == NULL)
262 return -ENOMEM;
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200263 map_groups__insert(self, new);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200264 }
265 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200266}
267
268int thread__fork(struct thread *self, struct thread *parent)
269{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200270 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200271
272 if (self->comm)
273 free(self->comm);
274 self->comm = strdup(parent->comm);
275 if (!self->comm)
276 return -ENOMEM;
277
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200278 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200279 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200280 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200281 return 0;
282}
283
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300284size_t threads__fprintf(FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200285{
286 size_t ret = 0;
287 struct rb_node *nd;
288
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300289 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200290 struct thread *pos = rb_entry(nd, struct thread, rb_node);
291
292 ret += thread__fprintf(pos, fp);
293 }
294
295 return ret;
296}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200297
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200298struct symbol *map_groups__find_symbol(struct map_groups *self,
299 enum map_type type, u64 addr,
300 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200301{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200302 struct map *map = map_groups__find(self, type, addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200303
304 if (map != NULL)
305 return map__find_symbol(map, map->map_ip(map, addr), filter);
306
307 return NULL;
308}