blob: 2229f82cd630ab2d72ec26eb4d2162479381b42c [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>
5#include "thread.h"
6#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02007#include "debug.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02008
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03009static struct rb_root threads;
10static struct thread *last_match;
11
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020012void thread__init(struct thread *self, pid_t pid)
13{
14 int i;
15 self->pid = pid;
16 self->comm = NULL;
17 for (i = 0; i < MAP__NR_TYPES; ++i) {
18 self->maps[i] = RB_ROOT;
19 INIT_LIST_HEAD(&self->removed_maps[i]);
20 }
21}
22
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020023static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020024{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020025 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020026
27 if (self != NULL) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020028 thread__init(self, pid);
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020029 self->comm = malloc(32);
30 if (self->comm)
31 snprintf(self->comm, 32, ":%d", self->pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020032 }
33
34 return self;
35}
36
37int thread__set_comm(struct thread *self, const char *comm)
38{
39 if (self->comm)
40 free(self->comm);
41 self->comm = strdup(comm);
42 return self->comm ? 0 : -ENOMEM;
43}
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",
58};
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020059
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020060static size_t __thread__fprintf_maps(struct thread *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 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030074 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020075
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020076 return printed;
77}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030078
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020079size_t thread__fprintf_maps(struct thread *self, FILE *fp)
80{
81 size_t printed = 0, i;
82 for (i = 0; i < MAP__NR_TYPES; ++i)
83 printed += __thread__fprintf_maps(self, i, fp);
84 return printed;
85}
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030086
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020087static size_t __thread__fprintf_removed_maps(struct thread *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
104static size_t thread__fprintf_removed_maps(struct thread *self, FILE *fp)
105{
106 size_t printed = 0, i;
107 for (i = 0; i < MAP__NR_TYPES; ++i)
108 printed += __thread__fprintf_removed_maps(self, i, fp);
109 return printed;
110}
111
112static size_t thread__fprintf(struct thread *self, FILE *fp)
113{
114 size_t printed = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
115 printed += thread__fprintf_removed_maps(self, fp);
116 printed += fprintf(fp, "Removed maps:\n");
117 return printed + thread__fprintf_removed_maps(self, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200118}
119
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300120struct thread *threads__findnew(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200121{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300122 struct rb_node **p = &threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200123 struct rb_node *parent = NULL;
124 struct thread *th;
125
126 /*
127 * Font-end cache - PID lookups come in blocks,
128 * so most of the time we dont have to look up
129 * the full rbtree:
130 */
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300131 if (last_match && last_match->pid == pid)
132 return last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200133
134 while (*p != NULL) {
135 parent = *p;
136 th = rb_entry(parent, struct thread, rb_node);
137
138 if (th->pid == pid) {
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300139 last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200140 return th;
141 }
142
143 if (pid < th->pid)
144 p = &(*p)->rb_left;
145 else
146 p = &(*p)->rb_right;
147 }
148
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200149 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200150 if (th != NULL) {
151 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300152 rb_insert_color(&th->rb_node, &threads);
153 last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200154 }
155
156 return th;
157}
158
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300159struct thread *register_idle_thread(void)
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200160{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300161 struct thread *thread = threads__findnew(0);
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200162
Ingo Molnar80ed0982009-09-16 14:12:36 +0200163 if (!thread || thread__set_comm(thread, "swapper")) {
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200164 fprintf(stderr, "problem inserting idle task.\n");
165 exit(-1);
166 }
167
168 return thread;
169}
170
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300171static void thread__remove_overlappings(struct thread *self, struct map *map)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200172{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200173 struct rb_root *root = &self->maps[map->type];
174 struct rb_node *next = rb_first(root);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200175
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300176 while (next) {
177 struct map *pos = rb_entry(next, struct map, rb_node);
178 next = rb_next(&pos->rb_node);
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200179
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300180 if (!map__overlap(pos, map))
181 continue;
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200182
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300183 if (verbose >= 2) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200184 fputs("overlapping maps:\n", stderr);
185 map__fprintf(map, stderr);
186 map__fprintf(pos, stderr);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300187 }
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200188
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200189 rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300190 /*
191 * We may have references to this map, for instance in some
192 * hist_entry instances, so just move them to a separate
193 * list.
194 */
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200195 list_add_tail(&pos->node, &self->removed_maps[map->type]);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200196 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300197}
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200198
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300199void maps__insert(struct rb_root *maps, struct map *map)
200{
201 struct rb_node **p = &maps->rb_node;
202 struct rb_node *parent = NULL;
203 const u64 ip = map->start;
204 struct map *m;
205
206 while (*p != NULL) {
207 parent = *p;
208 m = rb_entry(parent, struct map, rb_node);
209 if (ip < m->start)
210 p = &(*p)->rb_left;
211 else
212 p = &(*p)->rb_right;
213 }
214
215 rb_link_node(&map->rb_node, parent, p);
216 rb_insert_color(&map->rb_node, maps);
217}
218
219struct map *maps__find(struct rb_root *maps, u64 ip)
220{
221 struct rb_node **p = &maps->rb_node;
222 struct rb_node *parent = NULL;
223 struct map *m;
224
225 while (*p != NULL) {
226 parent = *p;
227 m = rb_entry(parent, struct map, rb_node);
228 if (ip < m->start)
229 p = &(*p)->rb_left;
230 else if (ip > m->end)
231 p = &(*p)->rb_right;
232 else
233 return m;
234 }
235
236 return NULL;
237}
238
239void thread__insert_map(struct thread *self, struct map *map)
240{
241 thread__remove_overlappings(self, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200242 maps__insert(&self->maps[map->type], map);
243}
244
245static int thread__clone_maps(struct thread *self, struct thread *parent,
246 enum map_type type)
247{
248 struct rb_node *nd;
249 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
250 struct map *map = rb_entry(nd, struct map, rb_node);
251 struct map *new = map__clone(map);
252 if (new == NULL)
253 return -ENOMEM;
254 thread__insert_map(self, new);
255 }
256 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200257}
258
259int thread__fork(struct thread *self, struct thread *parent)
260{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200261 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200262
263 if (self->comm)
264 free(self->comm);
265 self->comm = strdup(parent->comm);
266 if (!self->comm)
267 return -ENOMEM;
268
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200269 for (i = 0; i < MAP__NR_TYPES; ++i)
270 if (thread__clone_maps(self, parent, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200271 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200272 return 0;
273}
274
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300275size_t threads__fprintf(FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200276{
277 size_t ret = 0;
278 struct rb_node *nd;
279
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300280 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200281 struct thread *pos = rb_entry(nd, struct thread, rb_node);
282
283 ret += thread__fprintf(pos, fp);
284 }
285
286 return ret;
287}