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