thread.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 thread__init(struct thread *self, pid_t pid)
  11. {
  12. int i;
  13. self->pid = pid;
  14. self->comm = NULL;
  15. for (i = 0; i < MAP__NR_TYPES; ++i) {
  16. self->maps[i] = RB_ROOT;
  17. INIT_LIST_HEAD(&self->removed_maps[i]);
  18. }
  19. }
  20. static struct thread *thread__new(pid_t pid)
  21. {
  22. struct thread *self = zalloc(sizeof(*self));
  23. if (self != NULL) {
  24. thread__init(self, pid);
  25. self->comm = malloc(32);
  26. if (self->comm)
  27. snprintf(self->comm, 32, ":%d", self->pid);
  28. }
  29. return self;
  30. }
  31. int thread__set_comm(struct thread *self, const char *comm)
  32. {
  33. if (self->comm)
  34. free(self->comm);
  35. self->comm = strdup(comm);
  36. return self->comm ? 0 : -ENOMEM;
  37. }
  38. int thread__comm_len(struct thread *self)
  39. {
  40. if (!self->comm_len) {
  41. if (!self->comm)
  42. return 0;
  43. self->comm_len = strlen(self->comm);
  44. }
  45. return self->comm_len;
  46. }
  47. static const char *map_type__name[MAP__NR_TYPES] = {
  48. [MAP__FUNCTION] = "Functions",
  49. };
  50. static size_t __thread__fprintf_maps(struct thread *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 thread__fprintf_maps(struct thread *self, FILE *fp)
  67. {
  68. size_t printed = 0, i;
  69. for (i = 0; i < MAP__NR_TYPES; ++i)
  70. printed += __thread__fprintf_maps(self, i, fp);
  71. return printed;
  72. }
  73. static size_t __thread__fprintf_removed_maps(struct thread *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 thread__fprintf_removed_maps(struct thread *self, FILE *fp)
  89. {
  90. size_t printed = 0, i;
  91. for (i = 0; i < MAP__NR_TYPES; ++i)
  92. printed += __thread__fprintf_removed_maps(self, i, fp);
  93. return printed;
  94. }
  95. static size_t thread__fprintf(struct thread *self, FILE *fp)
  96. {
  97. size_t printed = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  98. printed += thread__fprintf_removed_maps(self, fp);
  99. printed += fprintf(fp, "Removed maps:\n");
  100. return printed + thread__fprintf_removed_maps(self, fp);
  101. }
  102. struct thread *threads__findnew(pid_t pid)
  103. {
  104. struct rb_node **p = &threads.rb_node;
  105. struct rb_node *parent = NULL;
  106. struct thread *th;
  107. /*
  108. * Font-end cache - PID lookups come in blocks,
  109. * so most of the time we dont have to look up
  110. * the full rbtree:
  111. */
  112. if (last_match && last_match->pid == pid)
  113. return last_match;
  114. while (*p != NULL) {
  115. parent = *p;
  116. th = rb_entry(parent, struct thread, rb_node);
  117. if (th->pid == pid) {
  118. last_match = th;
  119. return th;
  120. }
  121. if (pid < th->pid)
  122. p = &(*p)->rb_left;
  123. else
  124. p = &(*p)->rb_right;
  125. }
  126. th = thread__new(pid);
  127. if (th != NULL) {
  128. rb_link_node(&th->rb_node, parent, p);
  129. rb_insert_color(&th->rb_node, &threads);
  130. last_match = th;
  131. }
  132. return th;
  133. }
  134. struct thread *register_idle_thread(void)
  135. {
  136. struct thread *thread = threads__findnew(0);
  137. if (!thread || thread__set_comm(thread, "swapper")) {
  138. fprintf(stderr, "problem inserting idle task.\n");
  139. exit(-1);
  140. }
  141. return thread;
  142. }
  143. static void thread__remove_overlappings(struct thread *self, struct map *map)
  144. {
  145. struct rb_root *root = &self->maps[map->type];
  146. struct rb_node *next = rb_first(root);
  147. while (next) {
  148. struct map *pos = rb_entry(next, struct map, rb_node);
  149. next = rb_next(&pos->rb_node);
  150. if (!map__overlap(pos, map))
  151. continue;
  152. if (verbose >= 2) {
  153. fputs("overlapping maps:\n", stderr);
  154. map__fprintf(map, stderr);
  155. map__fprintf(pos, stderr);
  156. }
  157. rb_erase(&pos->rb_node, root);
  158. /*
  159. * We may have references to this map, for instance in some
  160. * hist_entry instances, so just move them to a separate
  161. * list.
  162. */
  163. list_add_tail(&pos->node, &self->removed_maps[map->type]);
  164. }
  165. }
  166. void maps__insert(struct rb_root *maps, struct map *map)
  167. {
  168. struct rb_node **p = &maps->rb_node;
  169. struct rb_node *parent = NULL;
  170. const u64 ip = map->start;
  171. struct map *m;
  172. while (*p != NULL) {
  173. parent = *p;
  174. m = rb_entry(parent, struct map, rb_node);
  175. if (ip < m->start)
  176. p = &(*p)->rb_left;
  177. else
  178. p = &(*p)->rb_right;
  179. }
  180. rb_link_node(&map->rb_node, parent, p);
  181. rb_insert_color(&map->rb_node, maps);
  182. }
  183. struct map *maps__find(struct rb_root *maps, u64 ip)
  184. {
  185. struct rb_node **p = &maps->rb_node;
  186. struct rb_node *parent = NULL;
  187. struct map *m;
  188. while (*p != NULL) {
  189. parent = *p;
  190. m = rb_entry(parent, struct map, rb_node);
  191. if (ip < m->start)
  192. p = &(*p)->rb_left;
  193. else if (ip > m->end)
  194. p = &(*p)->rb_right;
  195. else
  196. return m;
  197. }
  198. return NULL;
  199. }
  200. void thread__insert_map(struct thread *self, struct map *map)
  201. {
  202. thread__remove_overlappings(self, map);
  203. maps__insert(&self->maps[map->type], map);
  204. }
  205. static int thread__clone_maps(struct thread *self, struct thread *parent,
  206. enum map_type type)
  207. {
  208. struct rb_node *nd;
  209. for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
  210. struct map *map = rb_entry(nd, struct map, rb_node);
  211. struct map *new = map__clone(map);
  212. if (new == NULL)
  213. return -ENOMEM;
  214. thread__insert_map(self, new);
  215. }
  216. return 0;
  217. }
  218. int thread__fork(struct thread *self, struct thread *parent)
  219. {
  220. int i;
  221. if (self->comm)
  222. free(self->comm);
  223. self->comm = strdup(parent->comm);
  224. if (!self->comm)
  225. return -ENOMEM;
  226. for (i = 0; i < MAP__NR_TYPES; ++i)
  227. if (thread__clone_maps(self, parent, i) < 0)
  228. return -ENOMEM;
  229. return 0;
  230. }
  231. size_t threads__fprintf(FILE *fp)
  232. {
  233. size_t ret = 0;
  234. struct rb_node *nd;
  235. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  236. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  237. ret += thread__fprintf(pos, fp);
  238. }
  239. return ret;
  240. }