thread.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include "../perf.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "session.h"
  6. #include "thread.h"
  7. #include "util.h"
  8. #include "debug.h"
  9. void map_groups__init(struct map_groups *self)
  10. {
  11. int i;
  12. for (i = 0; i < MAP__NR_TYPES; ++i) {
  13. self->maps[i] = RB_ROOT;
  14. INIT_LIST_HEAD(&self->removed_maps[i]);
  15. }
  16. }
  17. static struct thread *thread__new(pid_t pid)
  18. {
  19. struct thread *self = zalloc(sizeof(*self));
  20. if (self != NULL) {
  21. map_groups__init(&self->mg);
  22. self->pid = pid;
  23. self->comm = malloc(32);
  24. if (self->comm)
  25. snprintf(self->comm, 32, ":%d", self->pid);
  26. }
  27. return self;
  28. }
  29. int thread__set_comm(struct thread *self, const char *comm)
  30. {
  31. if (self->comm)
  32. free(self->comm);
  33. self->comm = strdup(comm);
  34. if (self->comm == NULL)
  35. return -ENOMEM;
  36. self->comm_set = true;
  37. return 0;
  38. }
  39. int thread__comm_len(struct thread *self)
  40. {
  41. if (!self->comm_len) {
  42. if (!self->comm)
  43. return 0;
  44. self->comm_len = strlen(self->comm);
  45. }
  46. return self->comm_len;
  47. }
  48. static const char *map_type__name[MAP__NR_TYPES] = {
  49. [MAP__FUNCTION] = "Functions",
  50. [MAP__VARIABLE] = "Variables",
  51. };
  52. static size_t __map_groups__fprintf_maps(struct map_groups *self,
  53. enum map_type type, FILE *fp)
  54. {
  55. size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
  56. struct rb_node *nd;
  57. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  58. struct map *pos = rb_entry(nd, struct map, rb_node);
  59. printed += fprintf(fp, "Map:");
  60. printed += map__fprintf(pos, fp);
  61. if (verbose > 1) {
  62. printed += dso__fprintf(pos->dso, type, fp);
  63. printed += fprintf(fp, "--\n");
  64. }
  65. }
  66. return printed;
  67. }
  68. size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
  69. {
  70. size_t printed = 0, i;
  71. for (i = 0; i < MAP__NR_TYPES; ++i)
  72. printed += __map_groups__fprintf_maps(self, i, fp);
  73. return printed;
  74. }
  75. static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
  76. enum map_type type, FILE *fp)
  77. {
  78. struct map *pos;
  79. size_t printed = 0;
  80. list_for_each_entry(pos, &self->removed_maps[type], node) {
  81. printed += fprintf(fp, "Map:");
  82. printed += map__fprintf(pos, fp);
  83. if (verbose > 1) {
  84. printed += dso__fprintf(pos->dso, type, fp);
  85. printed += fprintf(fp, "--\n");
  86. }
  87. }
  88. return printed;
  89. }
  90. static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
  91. {
  92. size_t printed = 0, i;
  93. for (i = 0; i < MAP__NR_TYPES; ++i)
  94. printed += __map_groups__fprintf_removed_maps(self, i, fp);
  95. return printed;
  96. }
  97. static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
  98. {
  99. size_t printed = map_groups__fprintf_maps(self, fp);
  100. printed += fprintf(fp, "Removed maps:\n");
  101. return printed + map_groups__fprintf_removed_maps(self, fp);
  102. }
  103. static size_t thread__fprintf(struct thread *self, FILE *fp)
  104. {
  105. return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
  106. map_groups__fprintf(&self->mg, fp);
  107. }
  108. struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
  109. {
  110. struct rb_node **p = &self->threads.rb_node;
  111. struct rb_node *parent = NULL;
  112. struct thread *th;
  113. /*
  114. * Font-end cache - PID lookups come in blocks,
  115. * so most of the time we dont have to look up
  116. * the full rbtree:
  117. */
  118. if (self->last_match && self->last_match->pid == pid)
  119. return self->last_match;
  120. while (*p != NULL) {
  121. parent = *p;
  122. th = rb_entry(parent, struct thread, rb_node);
  123. if (th->pid == pid) {
  124. self->last_match = th;
  125. return th;
  126. }
  127. if (pid < th->pid)
  128. p = &(*p)->rb_left;
  129. else
  130. p = &(*p)->rb_right;
  131. }
  132. th = thread__new(pid);
  133. if (th != NULL) {
  134. rb_link_node(&th->rb_node, parent, p);
  135. rb_insert_color(&th->rb_node, &self->threads);
  136. self->last_match = th;
  137. }
  138. return th;
  139. }
  140. static void map_groups__remove_overlappings(struct map_groups *self,
  141. struct map *map)
  142. {
  143. struct rb_root *root = &self->maps[map->type];
  144. struct rb_node *next = rb_first(root);
  145. while (next) {
  146. struct map *pos = rb_entry(next, struct map, rb_node);
  147. next = rb_next(&pos->rb_node);
  148. if (!map__overlap(pos, map))
  149. continue;
  150. if (verbose >= 2) {
  151. fputs("overlapping maps:\n", stderr);
  152. map__fprintf(map, stderr);
  153. map__fprintf(pos, stderr);
  154. }
  155. rb_erase(&pos->rb_node, root);
  156. /*
  157. * We may have references to this map, for instance in some
  158. * hist_entry instances, so just move them to a separate
  159. * list.
  160. */
  161. list_add_tail(&pos->node, &self->removed_maps[map->type]);
  162. }
  163. }
  164. void maps__insert(struct rb_root *maps, struct map *map)
  165. {
  166. struct rb_node **p = &maps->rb_node;
  167. struct rb_node *parent = NULL;
  168. const u64 ip = map->start;
  169. struct map *m;
  170. while (*p != NULL) {
  171. parent = *p;
  172. m = rb_entry(parent, struct map, rb_node);
  173. if (ip < m->start)
  174. p = &(*p)->rb_left;
  175. else
  176. p = &(*p)->rb_right;
  177. }
  178. rb_link_node(&map->rb_node, parent, p);
  179. rb_insert_color(&map->rb_node, maps);
  180. }
  181. struct map *maps__find(struct rb_root *maps, u64 ip)
  182. {
  183. struct rb_node **p = &maps->rb_node;
  184. struct rb_node *parent = NULL;
  185. struct map *m;
  186. while (*p != NULL) {
  187. parent = *p;
  188. m = rb_entry(parent, struct map, rb_node);
  189. if (ip < m->start)
  190. p = &(*p)->rb_left;
  191. else if (ip > m->end)
  192. p = &(*p)->rb_right;
  193. else
  194. return m;
  195. }
  196. return NULL;
  197. }
  198. void thread__insert_map(struct thread *self, struct map *map)
  199. {
  200. map_groups__remove_overlappings(&self->mg, map);
  201. map_groups__insert(&self->mg, map);
  202. }
  203. /*
  204. * XXX This should not really _copy_ te maps, but refcount them.
  205. */
  206. static int map_groups__clone(struct map_groups *self,
  207. struct map_groups *parent, enum map_type type)
  208. {
  209. struct rb_node *nd;
  210. for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
  211. struct map *map = rb_entry(nd, struct map, rb_node);
  212. struct map *new = map__clone(map);
  213. if (new == NULL)
  214. return -ENOMEM;
  215. map_groups__insert(self, new);
  216. }
  217. return 0;
  218. }
  219. int thread__fork(struct thread *self, struct thread *parent)
  220. {
  221. int i;
  222. if (parent->comm_set) {
  223. if (self->comm)
  224. free(self->comm);
  225. self->comm = strdup(parent->comm);
  226. if (!self->comm)
  227. return -ENOMEM;
  228. self->comm_set = true;
  229. }
  230. for (i = 0; i < MAP__NR_TYPES; ++i)
  231. if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
  232. return -ENOMEM;
  233. return 0;
  234. }
  235. size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
  236. {
  237. size_t ret = 0;
  238. struct rb_node *nd;
  239. for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
  240. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  241. ret += thread__fprintf(pos, fp);
  242. }
  243. return ret;
  244. }
  245. struct symbol *map_groups__find_symbol(struct map_groups *self,
  246. enum map_type type, u64 addr,
  247. symbol_filter_t filter)
  248. {
  249. struct map *map = map_groups__find(self, type, addr);
  250. if (map != NULL)
  251. return map__find_symbol(map, map->map_ip(map, addr), filter);
  252. return NULL;
  253. }