thread.c 4.2 KB

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