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