blob: 4a08dcf50b68c4bd9858c17fd569ae6acdec8257 [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>
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02005#include "session.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02006#include "thread.h"
7#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02008#include "debug.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02009
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020010void map_groups__init(struct map_groups *self)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020011{
12 int i;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020013 for (i = 0; i < MAP__NR_TYPES; ++i) {
14 self->maps[i] = RB_ROOT;
15 INIT_LIST_HEAD(&self->removed_maps[i]);
16 }
17}
18
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020019static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020020{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020021 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020022
23 if (self != NULL) {
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020024 map_groups__init(&self->mg);
25 self->pid = pid;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020026 self->comm = malloc(32);
27 if (self->comm)
28 snprintf(self->comm, 32, ":%d", self->pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020029 }
30
31 return self;
32}
33
34int thread__set_comm(struct thread *self, const char *comm)
35{
36 if (self->comm)
37 free(self->comm);
38 self->comm = strdup(comm);
39 return self->comm ? 0 : -ENOMEM;
40}
41
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020042int thread__comm_len(struct thread *self)
43{
44 if (!self->comm_len) {
45 if (!self->comm)
46 return 0;
47 self->comm_len = strlen(self->comm);
48 }
49
50 return self->comm_len;
51}
52
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020053static const char *map_type__name[MAP__NR_TYPES] = {
54 [MAP__FUNCTION] = "Functions",
Arnaldo Carvalho de Melo22ccec52009-12-11 15:23:28 -020055 [MAP__VARIABLE] = "Variables",
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020056};
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020057
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020058static size_t __map_groups__fprintf_maps(struct map_groups *self,
59 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020060{
61 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
62 struct rb_node *nd;
63
64 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
65 struct map *pos = rb_entry(nd, struct map, rb_node);
66 printed += fprintf(fp, "Map:");
67 printed += map__fprintf(pos, fp);
68 if (verbose > 1) {
69 printed += dso__fprintf(pos->dso, type, fp);
70 printed += fprintf(fp, "--\n");
71 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030072 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020073
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020074 return printed;
75}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030076
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020077size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020078{
79 size_t printed = 0, i;
80 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020081 printed += __map_groups__fprintf_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020082 return printed;
83}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030084
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020085static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
86 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020087{
88 struct map *pos;
89 size_t printed = 0;
90
91 list_for_each_entry(pos, &self->removed_maps[type], node) {
92 printed += fprintf(fp, "Map:");
93 printed += map__fprintf(pos, fp);
94 if (verbose > 1) {
95 printed += dso__fprintf(pos->dso, type, fp);
96 printed += fprintf(fp, "--\n");
97 }
98 }
99 return printed;
100}
101
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200102static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200103{
104 size_t printed = 0, i;
105 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200106 printed += __map_groups__fprintf_removed_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200107 return printed;
108}
109
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200110static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
111{
112 size_t printed = map_groups__fprintf_maps(self, fp);
113 printed += fprintf(fp, "Removed maps:\n");
114 return printed + map_groups__fprintf_removed_maps(self, fp);
115}
116
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200117static size_t thread__fprintf(struct thread *self, FILE *fp)
118{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200119 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
120 map_groups__fprintf(&self->mg, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200121}
122
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200123struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200124{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200125 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200126 struct rb_node *parent = NULL;
127 struct thread *th;
128
129 /*
130 * Font-end cache - PID lookups come in blocks,
131 * so most of the time we dont have to look up
132 * the full rbtree:
133 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200134 if (self->last_match && self->last_match->pid == pid)
135 return self->last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200136
137 while (*p != NULL) {
138 parent = *p;
139 th = rb_entry(parent, struct thread, rb_node);
140
141 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200142 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200143 return th;
144 }
145
146 if (pid < th->pid)
147 p = &(*p)->rb_left;
148 else
149 p = &(*p)->rb_right;
150 }
151
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200152 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200153 if (th != NULL) {
154 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200155 rb_insert_color(&th->rb_node, &self->threads);
156 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200157 }
158
159 return th;
160}
161
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200162static void map_groups__remove_overlappings(struct map_groups *self,
163 struct map *map)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200164{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200165 struct rb_root *root = &self->maps[map->type];
166 struct rb_node *next = rb_first(root);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200167
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300168 while (next) {
169 struct map *pos = rb_entry(next, struct map, rb_node);
170 next = rb_next(&pos->rb_node);
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200171
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300172 if (!map__overlap(pos, map))
173 continue;
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200174
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300175 if (verbose >= 2) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200176 fputs("overlapping maps:\n", stderr);
177 map__fprintf(map, stderr);
178 map__fprintf(pos, stderr);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300179 }
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200180
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200181 rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300182 /*
183 * We may have references to this map, for instance in some
184 * hist_entry instances, so just move them to a separate
185 * list.
186 */
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200187 list_add_tail(&pos->node, &self->removed_maps[map->type]);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200188 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300189}
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200190
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300191void maps__insert(struct rb_root *maps, struct map *map)
192{
193 struct rb_node **p = &maps->rb_node;
194 struct rb_node *parent = NULL;
195 const u64 ip = map->start;
196 struct map *m;
197
198 while (*p != NULL) {
199 parent = *p;
200 m = rb_entry(parent, struct map, rb_node);
201 if (ip < m->start)
202 p = &(*p)->rb_left;
203 else
204 p = &(*p)->rb_right;
205 }
206
207 rb_link_node(&map->rb_node, parent, p);
208 rb_insert_color(&map->rb_node, maps);
209}
210
211struct map *maps__find(struct rb_root *maps, u64 ip)
212{
213 struct rb_node **p = &maps->rb_node;
214 struct rb_node *parent = NULL;
215 struct map *m;
216
217 while (*p != NULL) {
218 parent = *p;
219 m = rb_entry(parent, struct map, rb_node);
220 if (ip < m->start)
221 p = &(*p)->rb_left;
222 else if (ip > m->end)
223 p = &(*p)->rb_right;
224 else
225 return m;
226 }
227
228 return NULL;
229}
230
231void thread__insert_map(struct thread *self, struct map *map)
232{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200233 map_groups__remove_overlappings(&self->mg, map);
234 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200235}
236
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200237/*
238 * XXX This should not really _copy_ te maps, but refcount them.
239 */
240static int map_groups__clone(struct map_groups *self,
241 struct map_groups *parent, enum map_type type)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200242{
243 struct rb_node *nd;
244 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
245 struct map *map = rb_entry(nd, struct map, rb_node);
246 struct map *new = map__clone(map);
247 if (new == NULL)
248 return -ENOMEM;
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200249 map_groups__insert(self, new);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200250 }
251 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200252}
253
254int thread__fork(struct thread *self, struct thread *parent)
255{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200256 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200257
258 if (self->comm)
259 free(self->comm);
260 self->comm = strdup(parent->comm);
261 if (!self->comm)
262 return -ENOMEM;
263
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200264 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200265 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200266 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200267 return 0;
268}
269
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200270size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200271{
272 size_t ret = 0;
273 struct rb_node *nd;
274
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200275 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200276 struct thread *pos = rb_entry(nd, struct thread, rb_node);
277
278 ret += thread__fprintf(pos, fp);
279 }
280
281 return ret;
282}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200283
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200284struct symbol *map_groups__find_symbol(struct map_groups *self,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200285 struct perf_session *session,
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200286 enum map_type type, u64 addr,
287 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200288{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200289 struct map *map = map_groups__find(self, type, addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200290
291 if (map != NULL)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200292 return map__find_symbol(map, session, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200293
294 return NULL;
295}