thread.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. static struct thread *thread__new(pid_t pid)
  11. {
  12. struct thread *self = calloc(1, sizeof(*self));
  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);
  18. self->maps = RB_ROOT;
  19. INIT_LIST_HEAD(&self->removed_maps);
  20. }
  21. return self;
  22. }
  23. int thread__set_comm(struct thread *self, const char *comm)
  24. {
  25. if (self->comm)
  26. free(self->comm);
  27. self->comm = strdup(comm);
  28. return self->comm ? 0 : -ENOMEM;
  29. }
  30. int thread__comm_len(struct thread *self)
  31. {
  32. if (!self->comm_len) {
  33. if (!self->comm)
  34. return 0;
  35. self->comm_len = strlen(self->comm);
  36. }
  37. return self->comm_len;
  38. }
  39. static size_t thread__fprintf(struct thread *self, FILE *fp)
  40. {
  41. struct rb_node *nd;
  42. struct map *pos;
  43. size_t ret = fprintf(fp, "Thread %d %s\nCurrent maps:\n",
  44. self->pid, self->comm);
  45. for (nd = rb_first(&self->maps); nd; nd = rb_next(nd)) {
  46. pos = rb_entry(nd, struct map, rb_node);
  47. ret += map__fprintf(pos, fp);
  48. }
  49. ret = fprintf(fp, "Removed maps:\n");
  50. list_for_each_entry(pos, &self->removed_maps, node)
  51. ret += map__fprintf(pos, fp);
  52. return ret;
  53. }
  54. struct thread *threads__findnew(pid_t pid)
  55. {
  56. struct rb_node **p = &threads.rb_node;
  57. struct rb_node *parent = NULL;
  58. struct thread *th;
  59. /*
  60. * Font-end cache - PID lookups come in blocks,
  61. * so most of the time we dont have to look up
  62. * the full rbtree:
  63. */
  64. if (last_match && last_match->pid == pid)
  65. return last_match;
  66. while (*p != NULL) {
  67. parent = *p;
  68. th = rb_entry(parent, struct thread, rb_node);
  69. if (th->pid == pid) {
  70. last_match = th;
  71. return th;
  72. }
  73. if (pid < th->pid)
  74. p = &(*p)->rb_left;
  75. else
  76. p = &(*p)->rb_right;
  77. }
  78. th = thread__new(pid);
  79. if (th != NULL) {
  80. rb_link_node(&th->rb_node, parent, p);
  81. rb_insert_color(&th->rb_node, &threads);
  82. last_match = th;
  83. }
  84. return th;
  85. }
  86. struct thread *register_idle_thread(void)
  87. {
  88. struct thread *thread = threads__findnew(0);
  89. if (!thread || thread__set_comm(thread, "swapper")) {
  90. fprintf(stderr, "problem inserting idle task.\n");
  91. exit(-1);
  92. }
  93. return thread;
  94. }
  95. static void thread__remove_overlappings(struct thread *self, struct map *map)
  96. {
  97. struct rb_node *next = rb_first(&self->maps);
  98. while (next) {
  99. struct map *pos = rb_entry(next, struct map, rb_node);
  100. next = rb_next(&pos->rb_node);
  101. if (!map__overlap(pos, map))
  102. continue;
  103. if (verbose >= 2) {
  104. printf("overlapping maps:\n");
  105. map__fprintf(map, stdout);
  106. map__fprintf(pos, stdout);
  107. }
  108. rb_erase(&pos->rb_node, &self->maps);
  109. /*
  110. * We may have references to this map, for instance in some
  111. * hist_entry instances, so just move them to a separate
  112. * list.
  113. */
  114. list_add_tail(&pos->node, &self->removed_maps);
  115. }
  116. }
  117. void maps__insert(struct rb_root *maps, struct map *map)
  118. {
  119. struct rb_node **p = &maps->rb_node;
  120. struct rb_node *parent = NULL;
  121. const u64 ip = map->start;
  122. struct map *m;
  123. while (*p != NULL) {
  124. parent = *p;
  125. m = rb_entry(parent, struct map, rb_node);
  126. if (ip < m->start)
  127. p = &(*p)->rb_left;
  128. else
  129. p = &(*p)->rb_right;
  130. }
  131. rb_link_node(&map->rb_node, parent, p);
  132. rb_insert_color(&map->rb_node, maps);
  133. }
  134. struct map *maps__find(struct rb_root *maps, u64 ip)
  135. {
  136. struct rb_node **p = &maps->rb_node;
  137. struct rb_node *parent = NULL;
  138. struct map *m;
  139. while (*p != NULL) {
  140. parent = *p;
  141. m = rb_entry(parent, struct map, rb_node);
  142. if (ip < m->start)
  143. p = &(*p)->rb_left;
  144. else if (ip > m->end)
  145. p = &(*p)->rb_right;
  146. else
  147. return m;
  148. }
  149. return NULL;
  150. }
  151. void thread__insert_map(struct thread *self, struct map *map)
  152. {
  153. thread__remove_overlappings(self, map);
  154. maps__insert(&self->maps, map);
  155. }
  156. int thread__fork(struct thread *self, struct thread *parent)
  157. {
  158. struct rb_node *nd;
  159. if (self->comm)
  160. free(self->comm);
  161. self->comm = strdup(parent->comm);
  162. if (!self->comm)
  163. return -ENOMEM;
  164. for (nd = rb_first(&parent->maps); nd; nd = rb_next(nd)) {
  165. struct map *map = rb_entry(nd, struct map, rb_node);
  166. struct map *new = map__clone(map);
  167. if (!new)
  168. return -ENOMEM;
  169. thread__insert_map(self, new);
  170. }
  171. return 0;
  172. }
  173. size_t threads__fprintf(FILE *fp)
  174. {
  175. size_t ret = 0;
  176. struct rb_node *nd;
  177. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  178. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  179. ret += thread__fprintf(pos, fp);
  180. }
  181. return ret;
  182. }