thread.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. INIT_LIST_HEAD(&self->maps);
  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 map *pos;
  30. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  31. list_for_each_entry(pos, &self->maps, node)
  32. ret += map__fprintf(pos, fp);
  33. return ret;
  34. }
  35. struct thread *
  36. threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match)
  37. {
  38. struct rb_node **p = &threads->rb_node;
  39. struct rb_node *parent = NULL;
  40. struct thread *th;
  41. /*
  42. * Font-end cache - PID lookups come in blocks,
  43. * so most of the time we dont have to look up
  44. * the full rbtree:
  45. */
  46. if (*last_match && (*last_match)->pid == pid)
  47. return *last_match;
  48. while (*p != NULL) {
  49. parent = *p;
  50. th = rb_entry(parent, struct thread, rb_node);
  51. if (th->pid == pid) {
  52. *last_match = th;
  53. return th;
  54. }
  55. if (pid < th->pid)
  56. p = &(*p)->rb_left;
  57. else
  58. p = &(*p)->rb_right;
  59. }
  60. th = thread__new(pid);
  61. if (th != NULL) {
  62. rb_link_node(&th->rb_node, parent, p);
  63. rb_insert_color(&th->rb_node, threads);
  64. *last_match = th;
  65. }
  66. return th;
  67. }
  68. struct thread *
  69. register_idle_thread(struct rb_root *threads, struct thread **last_match)
  70. {
  71. struct thread *thread = threads__findnew(0, threads, last_match);
  72. if (!thread || thread__set_comm(thread, "swapper")) {
  73. fprintf(stderr, "problem inserting idle task.\n");
  74. exit(-1);
  75. }
  76. return thread;
  77. }
  78. void thread__insert_map(struct thread *self, struct map *map)
  79. {
  80. struct map *pos, *tmp;
  81. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  82. if (map__overlap(pos, map)) {
  83. if (verbose >= 2) {
  84. printf("overlapping maps:\n");
  85. map__fprintf(map, stdout);
  86. map__fprintf(pos, stdout);
  87. }
  88. if (map->start <= pos->start && map->end > pos->start)
  89. pos->start = map->end;
  90. if (map->end >= pos->end && map->start < pos->end)
  91. pos->end = map->start;
  92. if (verbose >= 2) {
  93. printf("after collision:\n");
  94. map__fprintf(pos, stdout);
  95. }
  96. if (pos->start >= pos->end) {
  97. list_del_init(&pos->node);
  98. free(pos);
  99. }
  100. }
  101. }
  102. list_add_tail(&map->node, &self->maps);
  103. }
  104. int thread__fork(struct thread *self, struct thread *parent)
  105. {
  106. struct map *map;
  107. if (self->comm)
  108. free(self->comm);
  109. self->comm = strdup(parent->comm);
  110. if (!self->comm)
  111. return -ENOMEM;
  112. list_for_each_entry(map, &parent->maps, node) {
  113. struct map *new = map__clone(map);
  114. if (!new)
  115. return -ENOMEM;
  116. thread__insert_map(self, new);
  117. }
  118. return 0;
  119. }
  120. struct map *thread__find_map(struct thread *self, u64 ip)
  121. {
  122. struct map *pos;
  123. if (self == NULL)
  124. return NULL;
  125. list_for_each_entry(pos, &self->maps, node)
  126. if (ip >= pos->start && ip <= pos->end)
  127. return pos;
  128. return NULL;
  129. }
  130. size_t threads__fprintf(FILE *fp, struct rb_root *threads)
  131. {
  132. size_t ret = 0;
  133. struct rb_node *nd;
  134. for (nd = rb_first(threads); nd; nd = rb_next(nd)) {
  135. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  136. ret += thread__fprintf(pos, fp);
  137. }
  138. return ret;
  139. }