blob: f53fad7c0a8d4a1fde9cceff0a8ac750b8d849b2 [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
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020012static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020013{
Ingo Molnar0ec04e12009-09-16 17:40:48 +020014 struct thread *self = calloc(1, sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020015
16 if (self != NULL) {
17 self->pid = pid;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020018 self->comm = malloc(32);
19 if (self->comm)
20 snprintf(self->comm, 32, ":%d", self->pid);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030021 self->maps = RB_ROOT;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030022 INIT_LIST_HEAD(&self->removed_maps);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020023 }
24
25 return self;
26}
27
28int thread__set_comm(struct thread *self, const char *comm)
29{
30 if (self->comm)
31 free(self->comm);
32 self->comm = strdup(comm);
33 return self->comm ? 0 : -ENOMEM;
34}
35
36static size_t thread__fprintf(struct thread *self, FILE *fp)
37{
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030038 struct rb_node *nd;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030039 struct map *pos;
40 size_t ret = fprintf(fp, "Thread %d %s\nCurrent maps:\n",
41 self->pid, self->comm);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020042
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030043 for (nd = rb_first(&self->maps); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030044 pos = rb_entry(nd, struct map, rb_node);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020045 ret += map__fprintf(pos, fp);
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030046 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020047
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030048 ret = fprintf(fp, "Removed maps:\n");
49
50 list_for_each_entry(pos, &self->removed_maps, node)
51 ret += map__fprintf(pos, fp);
52
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020053 return ret;
54}
55
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030056struct thread *threads__findnew(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020057{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030058 struct rb_node **p = &threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020059 struct rb_node *parent = NULL;
60 struct thread *th;
61
62 /*
63 * Font-end cache - PID lookups come in blocks,
64 * so most of the time we dont have to look up
65 * the full rbtree:
66 */
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030067 if (last_match && last_match->pid == pid)
68 return last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020069
70 while (*p != NULL) {
71 parent = *p;
72 th = rb_entry(parent, struct thread, rb_node);
73
74 if (th->pid == pid) {
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030075 last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020076 return th;
77 }
78
79 if (pid < th->pid)
80 p = &(*p)->rb_left;
81 else
82 p = &(*p)->rb_right;
83 }
84
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020085 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020086 if (th != NULL) {
87 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030088 rb_insert_color(&th->rb_node, &threads);
89 last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020090 }
91
92 return th;
93}
94
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030095struct thread *register_idle_thread(void)
Frederic Weisbecker5b447a62009-08-31 06:45:18 +020096{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030097 struct thread *thread = threads__findnew(0);
Frederic Weisbecker5b447a62009-08-31 06:45:18 +020098
Ingo Molnar80ed0982009-09-16 14:12:36 +020099 if (!thread || thread__set_comm(thread, "swapper")) {
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200100 fprintf(stderr, "problem inserting idle task.\n");
101 exit(-1);
102 }
103
104 return thread;
105}
106
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300107static void thread__remove_overlappings(struct thread *self, struct map *map)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200108{
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300109 struct rb_node *next = rb_first(&self->maps);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200110
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300111 while (next) {
112 struct map *pos = rb_entry(next, struct map, rb_node);
113 next = rb_next(&pos->rb_node);
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200114
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300115 if (!map__overlap(pos, map))
116 continue;
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200117
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300118 if (verbose >= 2) {
119 printf("overlapping maps:\n");
120 map__fprintf(map, stdout);
121 map__fprintf(pos, stdout);
122 }
Frederic Weisbecker6e086432009-08-18 17:04:03 +0200123
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300124 rb_erase(&pos->rb_node, &self->maps);
125 /*
126 * We may have references to this map, for instance in some
127 * hist_entry instances, so just move them to a separate
128 * list.
129 */
130 list_add_tail(&pos->node, &self->removed_maps);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200131 }
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300132}
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200133
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300134void maps__insert(struct rb_root *maps, struct map *map)
135{
136 struct rb_node **p = &maps->rb_node;
137 struct rb_node *parent = NULL;
138 const u64 ip = map->start;
139 struct map *m;
140
141 while (*p != NULL) {
142 parent = *p;
143 m = rb_entry(parent, struct map, rb_node);
144 if (ip < m->start)
145 p = &(*p)->rb_left;
146 else
147 p = &(*p)->rb_right;
148 }
149
150 rb_link_node(&map->rb_node, parent, p);
151 rb_insert_color(&map->rb_node, maps);
152}
153
154struct map *maps__find(struct rb_root *maps, u64 ip)
155{
156 struct rb_node **p = &maps->rb_node;
157 struct rb_node *parent = NULL;
158 struct map *m;
159
160 while (*p != NULL) {
161 parent = *p;
162 m = rb_entry(parent, struct map, rb_node);
163 if (ip < m->start)
164 p = &(*p)->rb_left;
165 else if (ip > m->end)
166 p = &(*p)->rb_right;
167 else
168 return m;
169 }
170
171 return NULL;
172}
173
174void thread__insert_map(struct thread *self, struct map *map)
175{
176 thread__remove_overlappings(self, map);
177 maps__insert(&self->maps, map);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200178}
179
180int thread__fork(struct thread *self, struct thread *parent)
181{
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300182 struct rb_node *nd;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200183
184 if (self->comm)
185 free(self->comm);
186 self->comm = strdup(parent->comm);
187 if (!self->comm)
188 return -ENOMEM;
189
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300190 for (nd = rb_first(&parent->maps); nd; nd = rb_next(nd)) {
191 struct map *map = rb_entry(nd, struct map, rb_node);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200192 struct map *new = map__clone(map);
193 if (!new)
194 return -ENOMEM;
195 thread__insert_map(self, new);
196 }
197
198 return 0;
199}
200
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300201size_t threads__fprintf(FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200202{
203 size_t ret = 0;
204 struct rb_node *nd;
205
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300206 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200207 struct thread *pos = rb_entry(nd, struct thread, rb_node);
208
209 ret += thread__fprintf(pos, fp);
210 }
211
212 return ret;
213}