thread.c 6.5 KB

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