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 | |
| 9 | static struct thread *thread__new(pid_t pid) |
| 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; |
| 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; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | return self; |
| 22 | } |
| 23 | |
| 24 | int thread__set_comm(struct thread *self, const char *comm) |
| 25 | { |
| 26 | if (self->comm) |
| 27 | free(self->comm); |
| 28 | self->comm = strdup(comm); |
| 29 | return self->comm ? 0 : -ENOMEM; |
| 30 | } |
| 31 | |
| 32 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 33 | { |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 34 | struct rb_node *nd; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 35 | size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm); |
| 36 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 37 | for (nd = rb_first(&self->maps); nd; nd = rb_next(nd)) { |
| 38 | struct map *pos = rb_entry(nd, struct map, rb_node); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 39 | ret += map__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 40 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 41 | |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | struct thread * |
| 46 | threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match) |
| 47 | { |
| 48 | struct rb_node **p = &threads->rb_node; |
| 49 | struct rb_node *parent = NULL; |
| 50 | struct thread *th; |
| 51 | |
| 52 | /* |
| 53 | * Font-end cache - PID lookups come in blocks, |
| 54 | * so most of the time we dont have to look up |
| 55 | * the full rbtree: |
| 56 | */ |
| 57 | if (*last_match && (*last_match)->pid == pid) |
| 58 | return *last_match; |
| 59 | |
| 60 | while (*p != NULL) { |
| 61 | parent = *p; |
| 62 | th = rb_entry(parent, struct thread, rb_node); |
| 63 | |
| 64 | if (th->pid == pid) { |
| 65 | *last_match = th; |
| 66 | return th; |
| 67 | } |
| 68 | |
| 69 | if (pid < th->pid) |
| 70 | p = &(*p)->rb_left; |
| 71 | else |
| 72 | p = &(*p)->rb_right; |
| 73 | } |
| 74 | |
| 75 | th = thread__new(pid); |
| 76 | if (th != NULL) { |
| 77 | rb_link_node(&th->rb_node, parent, p); |
| 78 | rb_insert_color(&th->rb_node, threads); |
| 79 | *last_match = th; |
| 80 | } |
| 81 | |
| 82 | return th; |
| 83 | } |
| 84 | |
Frederic Weisbecker | 5b447a6 | 2009-08-31 06:45:18 +0200 | [diff] [blame] | 85 | struct thread * |
| 86 | register_idle_thread(struct rb_root *threads, struct thread **last_match) |
| 87 | { |
| 88 | struct thread *thread = threads__findnew(0, threads, last_match); |
| 89 | |
Ingo Molnar | 80ed098 | 2009-09-16 14:12:36 +0200 | [diff] [blame] | 90 | if (!thread || thread__set_comm(thread, "swapper")) { |
Frederic Weisbecker | 5b447a6 | 2009-08-31 06:45:18 +0200 | [diff] [blame] | 91 | fprintf(stderr, "problem inserting idle task.\n"); |
| 92 | exit(-1); |
| 93 | } |
| 94 | |
| 95 | return thread; |
| 96 | } |
| 97 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 98 | static void thread__remove_overlappings(struct thread *self, struct map *map) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 99 | { |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 100 | struct rb_node *next = rb_first(&self->maps); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 101 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 102 | while (next) { |
| 103 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 104 | next = rb_next(&pos->rb_node); |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 105 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 106 | if (!map__overlap(pos, map)) |
| 107 | continue; |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 108 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 109 | if (verbose >= 2) { |
| 110 | printf("overlapping maps:\n"); |
| 111 | map__fprintf(map, stdout); |
| 112 | map__fprintf(pos, stdout); |
| 113 | } |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 114 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 115 | if (map->start <= pos->start && map->end > pos->start) |
| 116 | pos->start = map->end; |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 117 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame^] | 118 | if (map->end >= pos->end && map->start < pos->end) |
| 119 | pos->end = map->start; |
| 120 | |
| 121 | if (verbose >= 2) { |
| 122 | printf("after collision:\n"); |
| 123 | map__fprintf(pos, stdout); |
| 124 | } |
| 125 | |
| 126 | if (pos->start >= pos->end) { |
| 127 | rb_erase(&pos->rb_node, &self->maps); |
| 128 | free(pos); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 129 | } |
| 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 | } |