]> git.karo-electronics.de Git - mv-sheeva.git/blob - tools/perf/util/thread.c
perf session: Register the idle thread in perf_session__process_events
[mv-sheeva.git] / tools / perf / util / thread.c
1 #include "../perf.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "thread.h"
6 #include "util.h"
7 #include "debug.h"
8
9 static struct rb_root threads;
10 static struct thread *last_match;
11
12 void map_groups__init(struct map_groups *self)
13 {
14         int i;
15         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
21 static struct thread *thread__new(pid_t pid)
22 {
23         struct thread *self = zalloc(sizeof(*self));
24
25         if (self != NULL) {
26                 map_groups__init(&self->mg);
27                 self->pid = pid;
28                 self->comm = malloc(32);
29                 if (self->comm)
30                         snprintf(self->comm, 32, ":%d", self->pid);
31         }
32
33         return self;
34 }
35
36 int 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
44 int 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
55 static const char *map_type__name[MAP__NR_TYPES] = {
56         [MAP__FUNCTION] = "Functions",
57         [MAP__VARIABLE] = "Variables",
58 };
59
60 static size_t __map_groups__fprintf_maps(struct map_groups *self,
61                                          enum map_type type, FILE *fp)
62 {
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                 }
74         }
75
76         return printed;
77 }
78
79 size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
80 {
81         size_t printed = 0, i;
82         for (i = 0; i < MAP__NR_TYPES; ++i)
83                 printed += __map_groups__fprintf_maps(self, i, fp);
84         return printed;
85 }
86
87 static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
88                                                  enum map_type type, FILE *fp)
89 {
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
104 static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
105 {
106         size_t printed = 0, i;
107         for (i = 0; i < MAP__NR_TYPES; ++i)
108                 printed += __map_groups__fprintf_removed_maps(self, i, fp);
109         return printed;
110 }
111
112 static 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
119 static size_t thread__fprintf(struct thread *self, FILE *fp)
120 {
121         return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
122                map_groups__fprintf(&self->mg, fp);
123 }
124
125 struct thread *threads__findnew(pid_t pid)
126 {
127         struct rb_node **p = &threads.rb_node;
128         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          */
136         if (last_match && last_match->pid == pid)
137                 return last_match;
138
139         while (*p != NULL) {
140                 parent = *p;
141                 th = rb_entry(parent, struct thread, rb_node);
142
143                 if (th->pid == pid) {
144                         last_match = th;
145                         return th;
146                 }
147
148                 if (pid < th->pid)
149                         p = &(*p)->rb_left;
150                 else
151                         p = &(*p)->rb_right;
152         }
153
154         th = thread__new(pid);
155         if (th != NULL) {
156                 rb_link_node(&th->rb_node, parent, p);
157                 rb_insert_color(&th->rb_node, &threads);
158                 last_match = th;
159         }
160
161         return th;
162 }
163
164 static void map_groups__remove_overlappings(struct map_groups *self,
165                                             struct map *map)
166 {
167         struct rb_root *root = &self->maps[map->type];
168         struct rb_node *next = rb_first(root);
169
170         while (next) {
171                 struct map *pos = rb_entry(next, struct map, rb_node);
172                 next = rb_next(&pos->rb_node);
173
174                 if (!map__overlap(pos, map))
175                         continue;
176
177                 if (verbose >= 2) {
178                         fputs("overlapping maps:\n", stderr);
179                         map__fprintf(map, stderr);
180                         map__fprintf(pos, stderr);
181                 }
182
183                 rb_erase(&pos->rb_node, root);
184                 /*
185                  * We may have references to this map, for instance in some
186                  * hist_entry instances, so just move them to a separate
187                  * list.
188                  */
189                 list_add_tail(&pos->node, &self->removed_maps[map->type]);
190         }
191 }
192
193 void maps__insert(struct rb_root *maps, struct map *map)
194 {
195         struct rb_node **p = &maps->rb_node;
196         struct rb_node *parent = NULL;
197         const u64 ip = map->start;
198         struct map *m;
199
200         while (*p != NULL) {
201                 parent = *p;
202                 m = rb_entry(parent, struct map, rb_node);
203                 if (ip < m->start)
204                         p = &(*p)->rb_left;
205                 else
206                         p = &(*p)->rb_right;
207         }
208
209         rb_link_node(&map->rb_node, parent, p);
210         rb_insert_color(&map->rb_node, maps);
211 }
212
213 struct map *maps__find(struct rb_root *maps, u64 ip)
214 {
215         struct rb_node **p = &maps->rb_node;
216         struct rb_node *parent = NULL;
217         struct map *m;
218
219         while (*p != NULL) {
220                 parent = *p;
221                 m = rb_entry(parent, struct map, rb_node);
222                 if (ip < m->start)
223                         p = &(*p)->rb_left;
224                 else if (ip > m->end)
225                         p = &(*p)->rb_right;
226                 else
227                         return m;
228         }
229
230         return NULL;
231 }
232
233 void thread__insert_map(struct thread *self, struct map *map)
234 {
235         map_groups__remove_overlappings(&self->mg, map);
236         map_groups__insert(&self->mg, map);
237 }
238
239 /*
240  * XXX This should not really _copy_ te maps, but refcount them.
241  */
242 static int map_groups__clone(struct map_groups *self,
243                              struct map_groups *parent, enum map_type type)
244 {
245         struct rb_node *nd;
246         for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
247                 struct map *map = rb_entry(nd, struct map, rb_node);
248                 struct map *new = map__clone(map);
249                 if (new == NULL)
250                         return -ENOMEM;
251                 map_groups__insert(self, new);
252         }
253         return 0;
254 }
255
256 int thread__fork(struct thread *self, struct thread *parent)
257 {
258         int i;
259
260         if (self->comm)
261                 free(self->comm);
262         self->comm = strdup(parent->comm);
263         if (!self->comm)
264                 return -ENOMEM;
265
266         for (i = 0; i < MAP__NR_TYPES; ++i)
267                 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
268                         return -ENOMEM;
269         return 0;
270 }
271
272 size_t threads__fprintf(FILE *fp)
273 {
274         size_t ret = 0;
275         struct rb_node *nd;
276
277         for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
278                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
279
280                 ret += thread__fprintf(pos, fp);
281         }
282
283         return ret;
284 }
285
286 struct symbol *map_groups__find_symbol(struct map_groups *self,
287                                        enum map_type type, u64 addr,
288                                        symbol_filter_t filter)
289 {
290         struct map *map = map_groups__find(self, type, addr);
291
292         if (map != NULL)
293                 return map__find_symbol(map, map->map_ip(map, addr), filter);
294
295         return NULL;
296 }