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 | 9a92b47 | 2009-10-08 16:37:12 +0200 | [diff] [blame] | 9 | static struct thread *thread__new(pid_t pid, int set_comm) |
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 | 9a92b47 | 2009-10-08 16:37:12 +0200 | [diff] [blame] | 15 | if (set_comm) { |
| 16 | self->comm = malloc(32); |
| 17 | if (self->comm) |
| 18 | snprintf(self->comm, 32, ":%d", self->pid); |
| 19 | } |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 20 | self->maps = RB_ROOT; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 21 | INIT_LIST_HEAD(&self->removed_maps); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 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 | { |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 37 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 38 | struct map *pos; |
| 39 | size_t ret = fprintf(fp, "Thread %d %s\nCurrent maps:\n", |
| 40 | self->pid, self->comm); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 41 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 42 | 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] | 43 | pos = rb_entry(nd, struct map, rb_node); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 44 | ret += map__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 45 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 46 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 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 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 52 | return ret; |
| 53 | } |
| 54 | |
Frederic Weisbecker | 9a92b47 | 2009-10-08 16:37:12 +0200 | [diff] [blame] | 55 | static struct thread * |
| 56 | __threads__findnew(pid_t pid, struct rb_root *threads, |
| 57 | struct thread **last_match, |
| 58 | int set_comm) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 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 | |
Frederic Weisbecker | 9a92b47 | 2009-10-08 16:37:12 +0200 | [diff] [blame] | 87 | th = thread__new(pid, set_comm); |
| 88 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 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 | |
Frederic Weisbecker | 5b447a6 | 2009-08-31 06:45:18 +0200 | [diff] [blame] | 98 | struct thread * |
Frederic Weisbecker | 9a92b47 | 2009-10-08 16:37:12 +0200 | [diff] [blame] | 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 * |
Frederic Weisbecker | 5b447a6 | 2009-08-31 06:45:18 +0200 | [diff] [blame] | 112 | register_idle_thread(struct rb_root *threads, struct thread **last_match) |
| 113 | { |
| 114 | struct thread *thread = threads__findnew(0, threads, last_match); |
| 115 | |
Ingo Molnar | 80ed098 | 2009-09-16 14:12:36 +0200 | [diff] [blame] | 116 | if (!thread || thread__set_comm(thread, "swapper")) { |
Frederic Weisbecker | 5b447a6 | 2009-08-31 06:45:18 +0200 | [diff] [blame] | 117 | fprintf(stderr, "problem inserting idle task.\n"); |
| 118 | exit(-1); |
| 119 | } |
| 120 | |
| 121 | return thread; |
| 122 | } |
| 123 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 124 | static void thread__remove_overlappings(struct thread *self, struct map *map) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 125 | { |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 126 | struct rb_node *next = rb_first(&self->maps); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 127 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 128 | while (next) { |
| 129 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 130 | next = rb_next(&pos->rb_node); |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 131 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 132 | if (!map__overlap(pos, map)) |
| 133 | continue; |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 134 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 135 | if (verbose >= 2) { |
| 136 | printf("overlapping maps:\n"); |
| 137 | map__fprintf(map, stdout); |
| 138 | map__fprintf(pos, stdout); |
| 139 | } |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 140 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 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); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 148 | } |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 149 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 150 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 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); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | int thread__fork(struct thread *self, struct thread *parent) |
| 198 | { |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 199 | struct rb_node *nd; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 200 | |
| 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 Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 207 | for (nd = rb_first(&parent->maps); nd; nd = rb_next(nd)) { |
| 208 | struct map *map = rb_entry(nd, struct map, rb_node); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 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 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 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 | } |