perf tools: Improve thread comm resolution in perf sched
[linux-3.10.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 thread *thread__new(pid_t pid, int set_comm)
10 {
11         struct thread *self = calloc(1, sizeof(*self));
12
13         if (self != NULL) {
14                 self->pid = pid;
15                 if (set_comm) {
16                         self->comm = malloc(32);
17                         if (self->comm)
18                                 snprintf(self->comm, 32, ":%d", self->pid);
19                 }
20                 self->maps = RB_ROOT;
21                 INIT_LIST_HEAD(&self->removed_maps);
22         }
23
24         return self;
25 }
26
27 int thread__set_comm(struct thread *self, const char *comm)
28 {
29         if (self->comm)
30                 free(self->comm);
31         self->comm = strdup(comm);
32         return self->comm ? 0 : -ENOMEM;
33 }
34
35 static size_t thread__fprintf(struct thread *self, FILE *fp)
36 {
37         struct rb_node *nd;
38         struct map *pos;
39         size_t ret = fprintf(fp, "Thread %d %s\nCurrent maps:\n",
40                              self->pid, self->comm);
41
42         for (nd = rb_first(&self->maps); nd; nd = rb_next(nd)) {
43                 pos = rb_entry(nd, struct map, rb_node);
44                 ret += map__fprintf(pos, fp);
45         }
46
47         ret = fprintf(fp, "Removed maps:\n");
48
49         list_for_each_entry(pos, &self->removed_maps, node)
50                 ret += map__fprintf(pos, fp);
51
52         return ret;
53 }
54
55 static struct thread *
56 __threads__findnew(pid_t pid, struct rb_root *threads,
57                    struct thread **last_match,
58                    int set_comm)
59 {
60         struct rb_node **p = &threads->rb_node;
61         struct rb_node *parent = NULL;
62         struct thread *th;
63
64         /*
65          * Font-end cache - PID lookups come in blocks,
66          * so most of the time we dont have to look up
67          * the full rbtree:
68          */
69         if (*last_match && (*last_match)->pid == pid)
70                 return *last_match;
71
72         while (*p != NULL) {
73                 parent = *p;
74                 th = rb_entry(parent, struct thread, rb_node);
75
76                 if (th->pid == pid) {
77                         *last_match = th;
78                         return th;
79                 }
80
81                 if (pid < th->pid)
82                         p = &(*p)->rb_left;
83                 else
84                         p = &(*p)->rb_right;
85         }
86
87         th = thread__new(pid, set_comm);
88
89         if (th != NULL) {
90                 rb_link_node(&th->rb_node, parent, p);
91                 rb_insert_color(&th->rb_node, threads);
92                 *last_match = th;
93         }
94
95         return th;
96 }
97
98 struct thread *
99 threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match)
100 {
101         return __threads__findnew(pid, threads, last_match, 1);
102 }
103
104 struct thread *
105 threads__findnew_nocomm(pid_t pid, struct rb_root *threads,
106                         struct thread **last_match)
107 {
108         return __threads__findnew(pid, threads, last_match, 0);
109 }
110
111 struct thread *
112 register_idle_thread(struct rb_root *threads, struct thread **last_match)
113 {
114         struct thread *thread = threads__findnew(0, threads, last_match);
115
116         if (!thread || thread__set_comm(thread, "swapper")) {
117                 fprintf(stderr, "problem inserting idle task.\n");
118                 exit(-1);
119         }
120
121         return thread;
122 }
123
124 static void thread__remove_overlappings(struct thread *self, struct map *map)
125 {
126         struct rb_node *next = rb_first(&self->maps);
127
128         while (next) {
129                 struct map *pos = rb_entry(next, struct map, rb_node);
130                 next = rb_next(&pos->rb_node);
131
132                 if (!map__overlap(pos, map))
133                         continue;
134
135                 if (verbose >= 2) {
136                         printf("overlapping maps:\n");
137                         map__fprintf(map, stdout);
138                         map__fprintf(pos, stdout);
139                 }
140
141                 rb_erase(&pos->rb_node, &self->maps);
142                 /*
143                  * We may have references to this map, for instance in some
144                  * hist_entry instances, so just move them to a separate
145                  * list.
146                  */
147                 list_add_tail(&pos->node, &self->removed_maps);
148         }
149 }
150
151 void maps__insert(struct rb_root *maps, struct map *map)
152 {
153         struct rb_node **p = &maps->rb_node;
154         struct rb_node *parent = NULL;
155         const u64 ip = map->start;
156         struct map *m;
157
158         while (*p != NULL) {
159                 parent = *p;
160                 m = rb_entry(parent, struct map, rb_node);
161                 if (ip < m->start)
162                         p = &(*p)->rb_left;
163                 else
164                         p = &(*p)->rb_right;
165         }
166
167         rb_link_node(&map->rb_node, parent, p);
168         rb_insert_color(&map->rb_node, maps);
169 }
170
171 struct map *maps__find(struct rb_root *maps, u64 ip)
172 {
173         struct rb_node **p = &maps->rb_node;
174         struct rb_node *parent = NULL;
175         struct map *m;
176
177         while (*p != NULL) {
178                 parent = *p;
179                 m = rb_entry(parent, struct map, rb_node);
180                 if (ip < m->start)
181                         p = &(*p)->rb_left;
182                 else if (ip > m->end)
183                         p = &(*p)->rb_right;
184                 else
185                         return m;
186         }
187
188         return NULL;
189 }
190
191 void thread__insert_map(struct thread *self, struct map *map)
192 {
193         thread__remove_overlappings(self, map);
194         maps__insert(&self->maps, map);
195 }
196
197 int thread__fork(struct thread *self, struct thread *parent)
198 {
199         struct rb_node *nd;
200
201         if (self->comm)
202                 free(self->comm);
203         self->comm = strdup(parent->comm);
204         if (!self->comm)
205                 return -ENOMEM;
206
207         for (nd = rb_first(&parent->maps); nd; nd = rb_next(nd)) {
208                 struct map *map = rb_entry(nd, struct map, rb_node);
209                 struct map *new = map__clone(map);
210                 if (!new)
211                         return -ENOMEM;
212                 thread__insert_map(self, new);
213         }
214
215         return 0;
216 }
217
218 size_t threads__fprintf(FILE *fp, struct rb_root *threads)
219 {
220         size_t ret = 0;
221         struct rb_node *nd;
222
223         for (nd = rb_first(threads); nd; nd = rb_next(nd)) {
224                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
225
226                 ret += thread__fprintf(pos, fp);
227         }
228
229         return ret;
230 }