|
@@ -15,7 +15,7 @@ static struct thread *thread__new(pid_t pid)
|
|
self->comm = malloc(32);
|
|
self->comm = malloc(32);
|
|
if (self->comm)
|
|
if (self->comm)
|
|
snprintf(self->comm, 32, ":%d", self->pid);
|
|
snprintf(self->comm, 32, ":%d", self->pid);
|
|
- INIT_LIST_HEAD(&self->maps);
|
|
|
|
|
|
+ self->maps = RB_ROOT;
|
|
}
|
|
}
|
|
|
|
|
|
return self;
|
|
return self;
|
|
@@ -31,11 +31,13 @@ int thread__set_comm(struct thread *self, const char *comm)
|
|
|
|
|
|
static size_t thread__fprintf(struct thread *self, FILE *fp)
|
|
static size_t thread__fprintf(struct thread *self, FILE *fp)
|
|
{
|
|
{
|
|
- struct map *pos;
|
|
|
|
|
|
+ struct rb_node *nd;
|
|
size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
|
|
size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
|
|
|
|
|
|
- list_for_each_entry(pos, &self->maps, node)
|
|
|
|
|
|
+ for (nd = rb_first(&self->maps); nd; nd = rb_next(nd)) {
|
|
|
|
+ struct map *pos = rb_entry(nd, struct map, rb_node);
|
|
ret += map__fprintf(pos, fp);
|
|
ret += map__fprintf(pos, fp);
|
|
|
|
+ }
|
|
|
|
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
@@ -93,42 +95,90 @@ register_idle_thread(struct rb_root *threads, struct thread **last_match)
|
|
return thread;
|
|
return thread;
|
|
}
|
|
}
|
|
|
|
|
|
-void thread__insert_map(struct thread *self, struct map *map)
|
|
|
|
|
|
+static void thread__remove_overlappings(struct thread *self, struct map *map)
|
|
{
|
|
{
|
|
- struct map *pos, *tmp;
|
|
|
|
-
|
|
|
|
- list_for_each_entry_safe(pos, tmp, &self->maps, node) {
|
|
|
|
- if (map__overlap(pos, map)) {
|
|
|
|
- if (verbose >= 2) {
|
|
|
|
- printf("overlapping maps:\n");
|
|
|
|
- map__fprintf(map, stdout);
|
|
|
|
- map__fprintf(pos, stdout);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (map->start <= pos->start && map->end > pos->start)
|
|
|
|
- pos->start = map->end;
|
|
|
|
-
|
|
|
|
- if (map->end >= pos->end && map->start < pos->end)
|
|
|
|
- pos->end = map->start;
|
|
|
|
-
|
|
|
|
- if (verbose >= 2) {
|
|
|
|
- printf("after collision:\n");
|
|
|
|
- map__fprintf(pos, stdout);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (pos->start >= pos->end) {
|
|
|
|
- list_del_init(&pos->node);
|
|
|
|
- free(pos);
|
|
|
|
- }
|
|
|
|
|
|
+ struct rb_node *next = rb_first(&self->maps);
|
|
|
|
+
|
|
|
|
+ while (next) {
|
|
|
|
+ struct map *pos = rb_entry(next, struct map, rb_node);
|
|
|
|
+ next = rb_next(&pos->rb_node);
|
|
|
|
+
|
|
|
|
+ if (!map__overlap(pos, map))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ if (verbose >= 2) {
|
|
|
|
+ printf("overlapping maps:\n");
|
|
|
|
+ map__fprintf(map, stdout);
|
|
|
|
+ map__fprintf(pos, stdout);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (map->start <= pos->start && map->end > pos->start)
|
|
|
|
+ pos->start = map->end;
|
|
|
|
+
|
|
|
|
+ if (map->end >= pos->end && map->start < pos->end)
|
|
|
|
+ pos->end = map->start;
|
|
|
|
+
|
|
|
|
+ if (verbose >= 2) {
|
|
|
|
+ printf("after collision:\n");
|
|
|
|
+ map__fprintf(pos, stdout);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (pos->start >= pos->end) {
|
|
|
|
+ rb_erase(&pos->rb_node, &self->maps);
|
|
|
|
+ free(pos);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void maps__insert(struct rb_root *maps, struct map *map)
|
|
|
|
+{
|
|
|
|
+ struct rb_node **p = &maps->rb_node;
|
|
|
|
+ struct rb_node *parent = NULL;
|
|
|
|
+ const u64 ip = map->start;
|
|
|
|
+ struct map *m;
|
|
|
|
+
|
|
|
|
+ while (*p != NULL) {
|
|
|
|
+ parent = *p;
|
|
|
|
+ m = rb_entry(parent, struct map, rb_node);
|
|
|
|
+ if (ip < m->start)
|
|
|
|
+ p = &(*p)->rb_left;
|
|
|
|
+ else
|
|
|
|
+ p = &(*p)->rb_right;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ rb_link_node(&map->rb_node, parent, p);
|
|
|
|
+ rb_insert_color(&map->rb_node, maps);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+struct map *maps__find(struct rb_root *maps, u64 ip)
|
|
|
|
+{
|
|
|
|
+ struct rb_node **p = &maps->rb_node;
|
|
|
|
+ struct rb_node *parent = NULL;
|
|
|
|
+ struct map *m;
|
|
|
|
+
|
|
|
|
+ while (*p != NULL) {
|
|
|
|
+ parent = *p;
|
|
|
|
+ m = rb_entry(parent, struct map, rb_node);
|
|
|
|
+ if (ip < m->start)
|
|
|
|
+ p = &(*p)->rb_left;
|
|
|
|
+ else if (ip > m->end)
|
|
|
|
+ p = &(*p)->rb_right;
|
|
|
|
+ else
|
|
|
|
+ return m;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return NULL;
|
|
|
|
+}
|
|
|
|
|
|
- list_add_tail(&map->node, &self->maps);
|
|
|
|
|
|
+void thread__insert_map(struct thread *self, struct map *map)
|
|
|
|
+{
|
|
|
|
+ thread__remove_overlappings(self, map);
|
|
|
|
+ maps__insert(&self->maps, map);
|
|
}
|
|
}
|
|
|
|
|
|
int thread__fork(struct thread *self, struct thread *parent)
|
|
int thread__fork(struct thread *self, struct thread *parent)
|
|
{
|
|
{
|
|
- struct map *map;
|
|
|
|
|
|
+ struct rb_node *nd;
|
|
|
|
|
|
if (self->comm)
|
|
if (self->comm)
|
|
free(self->comm);
|
|
free(self->comm);
|
|
@@ -136,7 +186,8 @@ int thread__fork(struct thread *self, struct thread *parent)
|
|
if (!self->comm)
|
|
if (!self->comm)
|
|
return -ENOMEM;
|
|
return -ENOMEM;
|
|
|
|
|
|
- list_for_each_entry(map, &parent->maps, node) {
|
|
|
|
|
|
+ for (nd = rb_first(&parent->maps); nd; nd = rb_next(nd)) {
|
|
|
|
+ struct map *map = rb_entry(nd, struct map, rb_node);
|
|
struct map *new = map__clone(map);
|
|
struct map *new = map__clone(map);
|
|
if (!new)
|
|
if (!new)
|
|
return -ENOMEM;
|
|
return -ENOMEM;
|
|
@@ -146,20 +197,6 @@ int thread__fork(struct thread *self, struct thread *parent)
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-struct map *thread__find_map(struct thread *self, u64 ip)
|
|
|
|
-{
|
|
|
|
- struct map *pos;
|
|
|
|
-
|
|
|
|
- if (self == NULL)
|
|
|
|
- return NULL;
|
|
|
|
-
|
|
|
|
- list_for_each_entry(pos, &self->maps, node)
|
|
|
|
- if (ip >= pos->start && ip <= pos->end)
|
|
|
|
- return pos;
|
|
|
|
-
|
|
|
|
- return NULL;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
size_t threads__fprintf(FILE *fp, struct rb_root *threads)
|
|
size_t threads__fprintf(FILE *fp, struct rb_root *threads)
|
|
{
|
|
{
|
|
size_t ret = 0;
|
|
size_t ret = 0;
|