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