c090654cb6c0f58e9db9f62ea24091dbec1d0439
[safe/jmp/linux-2.6] / tools / perf / util / thread.c
1 #include "../perf.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "session.h"
6 #include "thread.h"
7 #include "util.h"
8 #include "debug.h"
9
10 void map_groups__init(struct map_groups *self)
11 {
12         int i;
13         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
19 static struct thread *thread__new(pid_t pid)
20 {
21         struct thread *self = zalloc(sizeof(*self));
22
23         if (self != NULL) {
24                 map_groups__init(&self->mg);
25                 self->pid = pid;
26                 self->comm = malloc(32);
27                 if (self->comm)
28                         snprintf(self->comm, 32, ":%d", self->pid);
29         }
30
31         return self;
32 }
33
34 int thread__set_comm(struct thread *self, const char *comm)
35 {
36         if (self->comm)
37                 free(self->comm);
38         self->comm = strdup(comm);
39         if (self->comm == NULL)
40                 return -ENOMEM;
41         self->comm_set = true;
42         return 0;
43 }
44
45 int 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
56 static size_t __map_groups__fprintf_maps(struct map_groups *self,
57                                          enum map_type type, FILE *fp)
58 {
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                 }
70         }
71
72         return printed;
73 }
74
75 size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
76 {
77         size_t printed = 0, i;
78         for (i = 0; i < MAP__NR_TYPES; ++i)
79                 printed += __map_groups__fprintf_maps(self, i, fp);
80         return printed;
81 }
82
83 static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
84                                                  enum map_type type, FILE *fp)
85 {
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
100 static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
101 {
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);
105         return printed;
106 }
107
108 static 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
115 static size_t thread__fprintf(struct thread *self, FILE *fp)
116 {
117         return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
118                map_groups__fprintf(&self->mg, fp);
119 }
120
121 struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
122 {
123         struct rb_node **p = &self->threads.rb_node;
124         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          */
132         if (self->last_match && self->last_match->pid == pid)
133                 return self->last_match;
134
135         while (*p != NULL) {
136                 parent = *p;
137                 th = rb_entry(parent, struct thread, rb_node);
138
139                 if (th->pid == pid) {
140                         self->last_match = th;
141                         return th;
142                 }
143
144                 if (pid < th->pid)
145                         p = &(*p)->rb_left;
146                 else
147                         p = &(*p)->rb_right;
148         }
149
150         th = thread__new(pid);
151         if (th != NULL) {
152                 rb_link_node(&th->rb_node, parent, p);
153                 rb_insert_color(&th->rb_node, &self->threads);
154                 self->last_match = th;
155         }
156
157         return th;
158 }
159
160 static void map_groups__remove_overlappings(struct map_groups *self,
161                                             struct map *map)
162 {
163         struct rb_root *root = &self->maps[map->type];
164         struct rb_node *next = rb_first(root);
165
166         while (next) {
167                 struct map *pos = rb_entry(next, struct map, rb_node);
168                 next = rb_next(&pos->rb_node);
169
170                 if (!map__overlap(pos, map))
171                         continue;
172
173                 if (verbose >= 2) {
174                         fputs("overlapping maps:\n", stderr);
175                         map__fprintf(map, stderr);
176                         map__fprintf(pos, stderr);
177                 }
178
179                 rb_erase(&pos->rb_node, root);
180                 /*
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                  */
185                 list_add_tail(&pos->node, &self->removed_maps[map->type]);
186         }
187 }
188
189 void 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
209 struct 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
229 void thread__insert_map(struct thread *self, struct map *map)
230 {
231         map_groups__remove_overlappings(&self->mg, map);
232         map_groups__insert(&self->mg, map);
233 }
234
235 /*
236  * XXX This should not really _copy_ te maps, but refcount them.
237  */
238 static int map_groups__clone(struct map_groups *self,
239                              struct map_groups *parent, enum map_type type)
240 {
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;
247                 map_groups__insert(self, new);
248         }
249         return 0;
250 }
251
252 int thread__fork(struct thread *self, struct thread *parent)
253 {
254         int i;
255
256         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         }
264
265         for (i = 0; i < MAP__NR_TYPES; ++i)
266                 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
267                         return -ENOMEM;
268         return 0;
269 }
270
271 size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
272 {
273         size_t ret = 0;
274         struct rb_node *nd;
275
276         for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
277                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
278
279                 ret += thread__fprintf(pos, fp);
280         }
281
282         return ret;
283 }
284
285 struct symbol *map_groups__find_symbol(struct map_groups *self,
286                                        enum map_type type, u64 addr,
287                                        symbol_filter_t filter)
288 {
289         struct map *map = map_groups__find(self, type, addr);
290
291         if (map != NULL)
292                 return map__find_symbol(map, map->map_ip(map, addr), filter);
293
294         return NULL;
295 }