thread.c 6.4 KB

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