blob: c090654cb6c0f58e9db9f62ea24091dbec1d0439 [file] [log] [blame]
Frederic Weisbecker6baa0a5a2009-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 Weisbecker6baa0a5a2009-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 Weisbecker6baa0a5a2009-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 Weisbecker6baa0a5a2009-08-14 12:21:53 +020020{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020021 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a5a2009-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 Weisbecker6baa0a5a2009-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 Weisbecker6baa0a5a2009-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 Melo9958e1f2009-12-11 14:50:36 -020056static size_t __map_groups__fprintf_maps(struct map_groups *self,
57 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020058{
59 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
60 struct rb_node *nd;
61
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);
66 if (verbose > 1) {
67 printed += dso__fprintf(pos->dso, type, fp);
68 printed += fprintf(fp, "--\n");
69 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030070 }
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +020071
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020072 return printed;
73}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030074
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020075size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020076{
77 size_t printed = 0, i;
78 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020079 printed += __map_groups__fprintf_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020080 return printed;
81}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030082
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020083static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
84 enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020085{
86 struct map *pos;
87 size_t printed = 0;
88
89 list_for_each_entry(pos, &self->removed_maps[type], node) {
90 printed += fprintf(fp, "Map:");
91 printed += map__fprintf(pos, fp);
92 if (verbose > 1) {
93 printed += dso__fprintf(pos->dso, type, fp);
94 printed += fprintf(fp, "--\n");
95 }
96 }
97 return printed;
98}
99
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200100static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200101{
102 size_t printed = 0, i;
103 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200104 printed += __map_groups__fprintf_removed_maps(self, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200105 return printed;
106}
107
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200108static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
109{
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);
113}
114
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200115static size_t thread__fprintf(struct thread *self, FILE *fp)
116{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200117 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
118 map_groups__fprintf(&self->mg, fp);
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200119}
120
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200121struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200122{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200123 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200124 struct rb_node *parent = NULL;
125 struct thread *th;
126
127 /*
128 * Font-end cache - PID lookups come in blocks,
129 * so most of the time we dont have to look up
130 * the full rbtree:
131 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200132 if (self->last_match && self->last_match->pid == pid)
133 return self->last_match;
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200134
135 while (*p != NULL) {
136 parent = *p;
137 th = rb_entry(parent, struct thread, rb_node);
138
139 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200140 self->last_match = th;
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200141 return th;
142 }
143
144 if (pid < th->pid)
145 p = &(*p)->rb_left;
146 else
147 p = &(*p)->rb_right;
148 }
149
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200150 th = thread__new(pid);
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200151 if (th != NULL) {
152 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200153 rb_insert_color(&th->rb_node, &self->threads);
154 self->last_match = th;
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200155 }
156
157 return th;
158}
159
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200160static void map_groups__remove_overlappings(struct map_groups *self,
161 struct map *map)
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200162{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200163 struct rb_root *root = &self->maps[map->type];
164 struct rb_node *next = rb_first(root);
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200165
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300166 while (next) {
167 struct map *pos = rb_entry(next, struct map, rb_node);
168 next = rb_next(&pos->rb_node);
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200169
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300170 if (!map__overlap(pos, map))
171 continue;
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200172
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300173 if (verbose >= 2) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200174 fputs("overlapping maps:\n", stderr);
175 map__fprintf(map, stderr);
176 map__fprintf(pos, stderr);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300177 }
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200178
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200179 rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300180 /*
181 * We may have references to this map, for instance in some
182 * hist_entry instances, so just move them to a separate
183 * list.
184 */
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200185 list_add_tail(&pos->node, &self->removed_maps[map->type]);
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200186 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300187}
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200188
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300189void maps__insert(struct rb_root *maps, struct map *map)
190{
191 struct rb_node **p = &maps->rb_node;
192 struct rb_node *parent = NULL;
193 const u64 ip = map->start;
194 struct map *m;
195
196 while (*p != NULL) {
197 parent = *p;
198 m = rb_entry(parent, struct map, rb_node);
199 if (ip < m->start)
200 p = &(*p)->rb_left;
201 else
202 p = &(*p)->rb_right;
203 }
204
205 rb_link_node(&map->rb_node, parent, p);
206 rb_insert_color(&map->rb_node, maps);
207}
208
209struct map *maps__find(struct rb_root *maps, u64 ip)
210{
211 struct rb_node **p = &maps->rb_node;
212 struct rb_node *parent = NULL;
213 struct map *m;
214
215 while (*p != NULL) {
216 parent = *p;
217 m = rb_entry(parent, struct map, rb_node);
218 if (ip < m->start)
219 p = &(*p)->rb_left;
220 else if (ip > m->end)
221 p = &(*p)->rb_right;
222 else
223 return m;
224 }
225
226 return NULL;
227}
228
229void thread__insert_map(struct thread *self, struct map *map)
230{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200231 map_groups__remove_overlappings(&self->mg, map);
232 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200233}
234
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200235/*
236 * XXX This should not really _copy_ te maps, but refcount them.
237 */
238static int map_groups__clone(struct map_groups *self,
239 struct map_groups *parent, enum map_type type)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200240{
241 struct rb_node *nd;
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);
245 if (new == NULL)
246 return -ENOMEM;
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200247 map_groups__insert(self, new);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200248 }
249 return 0;
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200250}
251
252int thread__fork(struct thread *self, struct thread *parent)
253{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200254 int i;
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200255
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200256 if (parent->comm_set) {
257 if (self->comm)
258 free(self->comm);
259 self->comm = strdup(parent->comm);
260 if (!self->comm)
261 return -ENOMEM;
262 self->comm_set = true;
263 }
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200264
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200265 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200266 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200267 return -ENOMEM;
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200268 return 0;
269}
270
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200271size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200272{
273 size_t ret = 0;
274 struct rb_node *nd;
275
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200276 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a5a2009-08-14 12:21:53 +0200277 struct thread *pos = rb_entry(nd, struct thread, rb_node);
278
279 ret += thread__fprintf(pos, fp);
280 }
281
282 return ret;
283}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200284
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200285struct symbol *map_groups__find_symbol(struct map_groups *self,
286 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 Melo9de89fe2010-02-03 16:52:00 -0200292 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200293
294 return NULL;
295}