thread.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "../perf.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "thread.h"
  6. #include "util.h"
  7. #include "debug.h"
  8. static struct rb_root threads;
  9. static struct thread *last_match;
  10. void map_groups__init(struct map_groups *self)
  11. {
  12. int i;
  13. for (i = 0; i < MAP__NR_TYPES; ++i) {
  14. self->maps[i] = RB_ROOT;
  15. INIT_LIST_HEAD(&self->removed_maps[i]);
  16. }
  17. }
  18. static struct thread *thread__new(pid_t pid)
  19. {
  20. struct thread *self = zalloc(sizeof(*self));
  21. if (self != NULL) {
  22. map_groups__init(&self->mg);
  23. self->pid = pid;
  24. self->comm = malloc(32);
  25. if (self->comm)
  26. snprintf(self->comm, 32, ":%d", self->pid);
  27. }
  28. return self;
  29. }
  30. int thread__set_comm(struct thread *self, const char *comm)
  31. {
  32. if (self->comm)
  33. free(self->comm);
  34. self->comm = strdup(comm);
  35. return self->comm ? 0 : -ENOMEM;
  36. }
  37. int thread__comm_len(struct thread *self)
  38. {
  39. if (!self->comm_len) {
  40. if (!self->comm)
  41. return 0;
  42. self->comm_len = strlen(self->comm);
  43. }
  44. return self->comm_len;
  45. }
  46. static const char *map_type__name[MAP__NR_TYPES] = {
  47. [MAP__FUNCTION] = "Functions",
  48. };
  49. static size_t __map_groups__fprintf_maps(struct map_groups *self,
  50. enum map_type type, FILE *fp)
  51. {
  52. size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
  53. struct rb_node *nd;
  54. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  55. struct map *pos = rb_entry(nd, struct map, rb_node);
  56. printed += fprintf(fp, "Map:");
  57. printed += map__fprintf(pos, fp);
  58. if (verbose > 1) {
  59. printed += dso__fprintf(pos->dso, type, fp);
  60. printed += fprintf(fp, "--\n");
  61. }
  62. }
  63. return printed;
  64. }
  65. size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
  66. {
  67. size_t printed = 0, i;
  68. for (i = 0; i < MAP__NR_TYPES; ++i)
  69. printed += __map_groups__fprintf_maps(self, i, fp);
  70. return printed;
  71. }
  72. static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
  73. enum map_type type, FILE *fp)
  74. {
  75. struct map *pos;
  76. size_t printed = 0;
  77. list_for_each_entry(pos, &self->removed_maps[type], node) {
  78. printed += fprintf(fp, "Map:");
  79. printed += map__fprintf(pos, fp);
  80. if (verbose > 1) {
  81. printed += dso__fprintf(pos->dso, type, fp);
  82. printed += fprintf(fp, "--\n");
  83. }
  84. }
  85. return printed;
  86. }
  87. static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
  88. {
  89. size_t printed = 0, i;
  90. for (i = 0; i < MAP__NR_TYPES; ++i)
  91. printed += __map_groups__fprintf_removed_maps(self, i, fp);
  92. return printed;
  93. }
  94. static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
  95. {
  96. size_t printed = map_groups__fprintf_maps(self, fp);
  97. printed += fprintf(fp, "Removed maps:\n");
  98. return printed + map_groups__fprintf_removed_maps(self, fp);
  99. }
  100. static size_t thread__fprintf(struct thread *self, FILE *fp)
  101. {
  102. return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
  103. map_groups__fprintf(&self->mg, fp);
  104. }
  105. struct thread *threads__findnew(pid_t pid)
  106. {
  107. struct rb_node **p = &threads.rb_node;
  108. struct rb_node *parent = NULL;
  109. struct thread *th;
  110. /*
  111. * Font-end cache - PID lookups come in blocks,
  112. * so most of the time we dont have to look up
  113. * the full rbtree:
  114. */
  115. if (last_match && last_match->pid == pid)
  116. return last_match;
  117. while (*p != NULL) {
  118. parent = *p;
  119. th = rb_entry(parent, struct thread, rb_node);
  120. if (th->pid == pid) {
  121. last_match = th;
  122. return th;
  123. }
  124. if (pid < th->pid)
  125. p = &(*p)->rb_left;
  126. else
  127. p = &(*p)->rb_right;
  128. }
  129. th = thread__new(pid);
  130. if (th != NULL) {
  131. rb_link_node(&th->rb_node, parent, p);
  132. rb_insert_color(&th->rb_node, &threads);
  133. last_match = th;
  134. }
  135. return th;
  136. }
  137. struct thread *register_idle_thread(void)
  138. {
  139. struct thread *thread = threads__findnew(0);
  140. if (!thread || thread__set_comm(thread, "swapper")) {
  141. fprintf(stderr, "problem inserting idle task.\n");
  142. exit(-1);
  143. }
  144. return thread;
  145. }
  146. static void map_groups__remove_overlappings(struct map_groups *self,
  147. struct map *map)
  148. {
  149. struct rb_root *root = &self->maps[map->type];
  150. struct rb_node *next = rb_first(root);
  151. while (next) {
  152. struct map *pos = rb_entry(next, struct map, rb_node);
  153. next = rb_next(&pos->rb_node);
  154. if (!map__overlap(pos, map))
  155. continue;
  156. if (verbose >= 2) {
  157. fputs("overlapping maps:\n", stderr);
  158. map__fprintf(map, stderr);
  159. map__fprintf(pos, stderr);
  160. }
  161. rb_erase(&pos->rb_node, root);
  162. /*
  163. * We may have references to this map, for instance in some
  164. * hist_entry instances, so just move them to a separate
  165. * list.
  166. */
  167. list_add_tail(&pos->node, &self->removed_maps[map->type]);
  168. }
  169. }
  170. void maps__insert(struct rb_root *maps, struct map *map)
  171. {
  172. struct rb_node **p = &maps->rb_node;
  173. struct rb_node *parent = NULL;
  174. const u64 ip = map->start;
  175. struct map *m;
  176. while (*p != NULL) {
  177. parent = *p;
  178. m = rb_entry(parent, struct map, rb_node);
  179. if (ip < m->start)
  180. p = &(*p)->rb_left;
  181. else
  182. p = &(*p)->rb_right;
  183. }
  184. rb_link_node(&map->rb_node, parent, p);
  185. rb_insert_color(&map->rb_node, maps);
  186. }
  187. struct map *maps__find(struct rb_root *maps, u64 ip)
  188. {
  189. struct rb_node **p = &maps->rb_node;
  190. struct rb_node *parent = NULL;
  191. struct map *m;
  192. while (*p != NULL) {
  193. parent = *p;
  194. m = rb_entry(parent, struct map, rb_node);
  195. if (ip < m->start)
  196. p = &(*p)->rb_left;
  197. else if (ip > m->end)
  198. p = &(*p)->rb_right;
  199. else
  200. return m;
  201. }
  202. return NULL;
  203. }
  204. void thread__insert_map(struct thread *self, struct map *map)
  205. {
  206. map_groups__remove_overlappings(&self->mg, map);
  207. map_groups__insert(&self->mg, map);
  208. }
  209. /*
  210. * XXX This should not really _copy_ te maps, but refcount them.
  211. */
  212. static int map_groups__clone(struct map_groups *self,
  213. struct map_groups *parent, enum map_type type)
  214. {
  215. struct rb_node *nd;
  216. for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
  217. struct map *map = rb_entry(nd, struct map, rb_node);
  218. struct map *new = map__clone(map);
  219. if (new == NULL)
  220. return -ENOMEM;
  221. map_groups__insert(self, new);
  222. }
  223. return 0;
  224. }
  225. int thread__fork(struct thread *self, struct thread *parent)
  226. {
  227. int i;
  228. if (self->comm)
  229. free(self->comm);
  230. self->comm = strdup(parent->comm);
  231. if (!self->comm)
  232. return -ENOMEM;
  233. for (i = 0; i < MAP__NR_TYPES; ++i)
  234. if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
  235. return -ENOMEM;
  236. return 0;
  237. }
  238. size_t threads__fprintf(FILE *fp)
  239. {
  240. size_t ret = 0;
  241. struct rb_node *nd;
  242. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  243. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  244. ret += thread__fprintf(pos, fp);
  245. }
  246. return ret;
  247. }
  248. struct symbol *map_groups__find_symbol(struct map_groups *self,
  249. enum map_type type, u64 addr,
  250. symbol_filter_t filter)
  251. {
  252. struct map *map = map_groups__find(self, type, addr);
  253. if (map != NULL)
  254. return map__find_symbol(map, map->map_ip(map, addr), filter);
  255. return NULL;
  256. }