thread.c 4.2 KB

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