blob: 8bd5ca2d2f287e64a07e930ee15241ebb2e8664e [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
Frederic Weisbecker9a92b472009-10-08 16:37:12 +02009static struct thread *thread__new(pid_t pid, int set_comm)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020010{
Ingo Molnar0ec04e12009-09-16 17:40:48 +020011 struct thread *self = calloc(1, sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020012
13 if (self != NULL) {
14 self->pid = pid;
Frederic Weisbecker9a92b472009-10-08 16:37:12 +020015 if (set_comm) {
16 self->comm = malloc(32);
17 if (self->comm)
18 snprintf(self->comm, 32, ":%d", self->pid);
19 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030020 self->maps = RB_ROOT;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030021 INIT_LIST_HEAD(&self->removed_maps);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020022 }
23
24 return self;
25}
26
27int 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
35static size_t thread__fprintf(struct thread *self, FILE *fp)
36{
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030037 struct rb_node *nd;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030038 struct map *pos;
39 size_t ret = fprintf(fp, "Thread %d %s\nCurrent maps:\n",
40 self->pid, self->comm);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020041
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030042 for (nd = rb_first(&self->maps); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030043 pos = rb_entry(nd, struct map, rb_node);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020044 ret += map__fprintf(pos, fp);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030045 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020046
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030047 ret = fprintf(fp, "Removed maps:\n");
48
49 list_for_each_entry(pos, &self->removed_maps, node)
50 ret += map__fprintf(pos, fp);
51
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020052 return ret;
53}
54
Frederic Weisbecker9a92b472009-10-08 16:37:12 +020055static struct thread *
56__threads__findnew(pid_t pid, struct rb_root *threads,
57 struct thread **last_match,
58 int set_comm)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020059{
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
Frederic Weisbecker9a92b472009-10-08 16:37:12 +020087 th = thread__new(pid, set_comm);
88
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020089 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
Frederic Weisbecker5b447a62009-08-31 06:45:18 +020098struct thread *
Frederic Weisbecker9a92b472009-10-08 16:37:12 +020099threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match)
100{
101 return __threads__findnew(pid, threads, last_match, 1);
102}
103
104struct thread *
105threads__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
111struct thread *
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200112register_idle_thread(struct rb_root *threads, struct thread **last_match)
113{
114 struct thread *thread = threads__findnew(0, threads, last_match);
115
Ingo Molnar80ed0982009-09-16 14:12:36 +0200116 if (!thread || thread__set_comm(thread, "swapper")) {
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200117 fprintf(stderr, "problem inserting idle task.\n");
118 exit(-1);
119 }
120
121 return thread;
122}
123
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300124static void thread__remove_overlappings(struct thread *self, struct map *map)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200125{
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300126 struct rb_node *next = rb_first(&self->maps);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200127
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300128 while (next) {
129 struct map *pos = rb_entry(next, struct map, rb_node);
130 next = rb_next(&pos->rb_node);
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200131
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300132 if (!map__overlap(pos, map))
133 continue;
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200134
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300135 if (verbose >= 2) {
136 printf("overlapping maps:\n");
137 map__fprintf(map, stdout);
138 map__fprintf(pos, stdout);
139 }
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200140
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300141 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);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200148 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300149}
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200150
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300151void 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
171struct 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
191void thread__insert_map(struct thread *self, struct map *map)
192{
193 thread__remove_overlappings(self, map);
194 maps__insert(&self->maps, map);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200195}
196
197int thread__fork(struct thread *self, struct thread *parent)
198{
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300199 struct rb_node *nd;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200200
201 if (self->comm)
202 free(self->comm);
203 self->comm = strdup(parent->comm);
204 if (!self->comm)
205 return -ENOMEM;
206
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300207 for (nd = rb_first(&parent->maps); nd; nd = rb_next(nd)) {
208 struct map *map = rb_entry(nd, struct map, rb_node);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200209 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
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200218size_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}