blob: 9e8995eaf2b61332e650fb2c4dfde6d615c6ae3e [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);
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020039 if (self->comm == NULL)
40 return -ENOMEM;
41 self->comm_set = true;
42 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020043}
44
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020045int thread__comm_len(struct thread *self)
46{
47 if (!self->comm_len) {
48 if (!self->comm)
49 return 0;
50 self->comm_len = strlen(self->comm);
51 }
52
53 return self->comm_len;
54}
55
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020056static const char *map_type__name[MAP__NR_TYPES] = {
57 [MAP__FUNCTION] = "Functions",
Arnaldo Carvalho de Melo22ccec52009-12-11 15:23:28 -020058 [MAP__VARIABLE] = "Variables",
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020059};
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020060
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020061static size_t __map_groups__fprintf_maps(struct map_groups *self,
62 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020063{
64 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
65 struct rb_node *nd;
66
67 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
68 struct map *pos = rb_entry(nd, struct map, rb_node);
69 printed += fprintf(fp, "Map:");
70 printed += map__fprintf(pos, fp);
71 if (verbose > 1) {
72 printed += dso__fprintf(pos->dso, type, fp);
73 printed += fprintf(fp, "--\n");
74 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030075 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020076
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020077 return printed;
78}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030079
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020080size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020081{
82 size_t printed = 0, i;
83 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020084 printed += __map_groups__fprintf_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020085 return printed;
86}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030087
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020088static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
89 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020090{
91 struct map *pos;
92 size_t printed = 0;
93
94 list_for_each_entry(pos, &self->removed_maps[type], node) {
95 printed += fprintf(fp, "Map:");
96 printed += map__fprintf(pos, fp);
97 if (verbose > 1) {
98 printed += dso__fprintf(pos->dso, type, fp);
99 printed += fprintf(fp, "--\n");
100 }
101 }
102 return printed;
103}
104
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200105static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200106{
107 size_t printed = 0, i;
108 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200109 printed += __map_groups__fprintf_removed_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200110 return printed;
111}
112
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200113static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
114{
115 size_t printed = map_groups__fprintf_maps(self, fp);
116 printed += fprintf(fp, "Removed maps:\n");
117 return printed + map_groups__fprintf_removed_maps(self, fp);
118}
119
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200120static size_t thread__fprintf(struct thread *self, FILE *fp)
121{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200122 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
123 map_groups__fprintf(&self->mg, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200124}
125
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200126struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200127{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200128 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200129 struct rb_node *parent = NULL;
130 struct thread *th;
131
132 /*
133 * Font-end cache - PID lookups come in blocks,
134 * so most of the time we dont have to look up
135 * the full rbtree:
136 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200137 if (self->last_match && self->last_match->pid == pid)
138 return self->last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200139
140 while (*p != NULL) {
141 parent = *p;
142 th = rb_entry(parent, struct thread, rb_node);
143
144 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200145 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200146 return th;
147 }
148
149 if (pid < th->pid)
150 p = &(*p)->rb_left;
151 else
152 p = &(*p)->rb_right;
153 }
154
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200155 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200156 if (th != NULL) {
157 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200158 rb_insert_color(&th->rb_node, &self->threads);
159 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200160 }
161
162 return th;
163}
164
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200165static void map_groups__remove_overlappings(struct map_groups *self,
166 struct map *map)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200167{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200168 struct rb_root *root = &self->maps[map->type];
169 struct rb_node *next = rb_first(root);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200170
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300171 while (next) {
172 struct map *pos = rb_entry(next, struct map, rb_node);
173 next = rb_next(&pos->rb_node);
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200174
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300175 if (!map__overlap(pos, map))
176 continue;
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200177
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300178 if (verbose >= 2) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200179 fputs("overlapping maps:\n", stderr);
180 map__fprintf(map, stderr);
181 map__fprintf(pos, stderr);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300182 }
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200183
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200184 rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300185 /*
186 * We may have references to this map, for instance in some
187 * hist_entry instances, so just move them to a separate
188 * list.
189 */
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200190 list_add_tail(&pos->node, &self->removed_maps[map->type]);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200191 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300192}
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200193
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300194void maps__insert(struct rb_root *maps, struct map *map)
195{
196 struct rb_node **p = &maps->rb_node;
197 struct rb_node *parent = NULL;
198 const u64 ip = map->start;
199 struct map *m;
200
201 while (*p != NULL) {
202 parent = *p;
203 m = rb_entry(parent, struct map, rb_node);
204 if (ip < m->start)
205 p = &(*p)->rb_left;
206 else
207 p = &(*p)->rb_right;
208 }
209
210 rb_link_node(&map->rb_node, parent, p);
211 rb_insert_color(&map->rb_node, maps);
212}
213
214struct map *maps__find(struct rb_root *maps, u64 ip)
215{
216 struct rb_node **p = &maps->rb_node;
217 struct rb_node *parent = NULL;
218 struct map *m;
219
220 while (*p != NULL) {
221 parent = *p;
222 m = rb_entry(parent, struct map, rb_node);
223 if (ip < m->start)
224 p = &(*p)->rb_left;
225 else if (ip > m->end)
226 p = &(*p)->rb_right;
227 else
228 return m;
229 }
230
231 return NULL;
232}
233
234void thread__insert_map(struct thread *self, struct map *map)
235{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200236 map_groups__remove_overlappings(&self->mg, map);
237 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200238}
239
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200240/*
241 * XXX This should not really _copy_ te maps, but refcount them.
242 */
243static int map_groups__clone(struct map_groups *self,
244 struct map_groups *parent, enum map_type type)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200245{
246 struct rb_node *nd;
247 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
248 struct map *map = rb_entry(nd, struct map, rb_node);
249 struct map *new = map__clone(map);
250 if (new == NULL)
251 return -ENOMEM;
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200252 map_groups__insert(self, new);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200253 }
254 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200255}
256
257int thread__fork(struct thread *self, struct thread *parent)
258{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200259 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200260
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200261 if (parent->comm_set) {
262 if (self->comm)
263 free(self->comm);
264 self->comm = strdup(parent->comm);
265 if (!self->comm)
266 return -ENOMEM;
267 self->comm_set = true;
268 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200269
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200270 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200271 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200272 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200273 return 0;
274}
275
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200276size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200277{
278 size_t ret = 0;
279 struct rb_node *nd;
280
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200281 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200282 struct thread *pos = rb_entry(nd, struct thread, rb_node);
283
284 ret += thread__fprintf(pos, fp);
285 }
286
287 return ret;
288}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200289
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200290struct symbol *map_groups__find_symbol(struct map_groups *self,
291 enum map_type type, u64 addr,
292 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200293{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200294 struct map *map = map_groups__find(self, type, addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200295
296 if (map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200297 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200298
299 return NULL;
300}