thread.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. int find_all_tid(int pid, pid_t ** all_tid)
  10. {
  11. char name[256];
  12. int items;
  13. struct dirent **namelist = NULL;
  14. int ret = 0;
  15. int i;
  16. sprintf(name, "/proc/%d/task", pid);
  17. items = scandir(name, &namelist, NULL, NULL);
  18. if (items <= 0)
  19. return -ENOENT;
  20. *all_tid = malloc(sizeof(pid_t) * items);
  21. if (!*all_tid) {
  22. ret = -ENOMEM;
  23. goto failure;
  24. }
  25. for (i = 0; i < items; i++)
  26. (*all_tid)[i] = atoi(namelist[i]->d_name);
  27. ret = items;
  28. failure:
  29. for (i=0; i<items; i++)
  30. free(namelist[i]);
  31. free(namelist);
  32. return ret;
  33. }
  34. void map_groups__init(struct map_groups *self)
  35. {
  36. int i;
  37. for (i = 0; i < MAP__NR_TYPES; ++i) {
  38. self->maps[i] = RB_ROOT;
  39. INIT_LIST_HEAD(&self->removed_maps[i]);
  40. }
  41. }
  42. static struct thread *thread__new(pid_t pid)
  43. {
  44. struct thread *self = zalloc(sizeof(*self));
  45. if (self != NULL) {
  46. map_groups__init(&self->mg);
  47. self->pid = pid;
  48. self->comm = malloc(32);
  49. if (self->comm)
  50. snprintf(self->comm, 32, ":%d", self->pid);
  51. }
  52. return self;
  53. }
  54. static void map_groups__flush(struct map_groups *self)
  55. {
  56. int type;
  57. for (type = 0; type < MAP__NR_TYPES; type++) {
  58. struct rb_root *root = &self->maps[type];
  59. struct rb_node *next = rb_first(root);
  60. while (next) {
  61. struct map *pos = rb_entry(next, struct map, rb_node);
  62. next = rb_next(&pos->rb_node);
  63. rb_erase(&pos->rb_node, root);
  64. /*
  65. * We may have references to this map, for
  66. * instance in some hist_entry instances, so
  67. * just move them to a separate list.
  68. */
  69. list_add_tail(&pos->node, &self->removed_maps[pos->type]);
  70. }
  71. }
  72. }
  73. int thread__set_comm(struct thread *self, const char *comm)
  74. {
  75. int err;
  76. if (self->comm)
  77. free(self->comm);
  78. self->comm = strdup(comm);
  79. err = self->comm == NULL ? -ENOMEM : 0;
  80. if (!err) {
  81. self->comm_set = true;
  82. map_groups__flush(&self->mg);
  83. }
  84. return err;
  85. }
  86. int thread__comm_len(struct thread *self)
  87. {
  88. if (!self->comm_len) {
  89. if (!self->comm)
  90. return 0;
  91. self->comm_len = strlen(self->comm);
  92. }
  93. return self->comm_len;
  94. }
  95. size_t __map_groups__fprintf_maps(struct map_groups *self,
  96. enum map_type type, FILE *fp)
  97. {
  98. size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
  99. struct rb_node *nd;
  100. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  101. struct map *pos = rb_entry(nd, struct map, rb_node);
  102. printed += fprintf(fp, "Map:");
  103. printed += map__fprintf(pos, fp);
  104. if (verbose > 2) {
  105. printed += dso__fprintf(pos->dso, type, fp);
  106. printed += fprintf(fp, "--\n");
  107. }
  108. }
  109. return printed;
  110. }
  111. size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
  112. {
  113. size_t printed = 0, i;
  114. for (i = 0; i < MAP__NR_TYPES; ++i)
  115. printed += __map_groups__fprintf_maps(self, i, fp);
  116. return printed;
  117. }
  118. static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
  119. enum map_type type, FILE *fp)
  120. {
  121. struct map *pos;
  122. size_t printed = 0;
  123. list_for_each_entry(pos, &self->removed_maps[type], node) {
  124. printed += fprintf(fp, "Map:");
  125. printed += map__fprintf(pos, fp);
  126. if (verbose > 1) {
  127. printed += dso__fprintf(pos->dso, type, fp);
  128. printed += fprintf(fp, "--\n");
  129. }
  130. }
  131. return printed;
  132. }
  133. static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
  134. {
  135. size_t printed = 0, i;
  136. for (i = 0; i < MAP__NR_TYPES; ++i)
  137. printed += __map_groups__fprintf_removed_maps(self, i, fp);
  138. return printed;
  139. }
  140. static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
  141. {
  142. size_t printed = map_groups__fprintf_maps(self, fp);
  143. printed += fprintf(fp, "Removed maps:\n");
  144. return printed + map_groups__fprintf_removed_maps(self, fp);
  145. }
  146. static size_t thread__fprintf(struct thread *self, FILE *fp)
  147. {
  148. return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
  149. map_groups__fprintf(&self->mg, fp);
  150. }
  151. struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
  152. {
  153. struct rb_node **p = &self->threads.rb_node;
  154. struct rb_node *parent = NULL;
  155. struct thread *th;
  156. /*
  157. * Font-end cache - PID lookups come in blocks,
  158. * so most of the time we dont have to look up
  159. * the full rbtree:
  160. */
  161. if (self->last_match && self->last_match->pid == pid)
  162. return self->last_match;
  163. while (*p != NULL) {
  164. parent = *p;
  165. th = rb_entry(parent, struct thread, rb_node);
  166. if (th->pid == pid) {
  167. self->last_match = th;
  168. return th;
  169. }
  170. if (pid < th->pid)
  171. p = &(*p)->rb_left;
  172. else
  173. p = &(*p)->rb_right;
  174. }
  175. th = thread__new(pid);
  176. if (th != NULL) {
  177. rb_link_node(&th->rb_node, parent, p);
  178. rb_insert_color(&th->rb_node, &self->threads);
  179. self->last_match = th;
  180. }
  181. return th;
  182. }
  183. static int map_groups__fixup_overlappings(struct map_groups *self,
  184. struct map *map)
  185. {
  186. struct rb_root *root = &self->maps[map->type];
  187. struct rb_node *next = rb_first(root);
  188. while (next) {
  189. struct map *pos = rb_entry(next, struct map, rb_node);
  190. next = rb_next(&pos->rb_node);
  191. if (!map__overlap(pos, map))
  192. continue;
  193. if (verbose >= 2) {
  194. fputs("overlapping maps:\n", stderr);
  195. map__fprintf(map, stderr);
  196. map__fprintf(pos, stderr);
  197. }
  198. rb_erase(&pos->rb_node, root);
  199. /*
  200. * We may have references to this map, for instance in some
  201. * hist_entry instances, so just move them to a separate
  202. * list.
  203. */
  204. list_add_tail(&pos->node, &self->removed_maps[map->type]);
  205. /*
  206. * Now check if we need to create new maps for areas not
  207. * overlapped by the new map:
  208. */
  209. if (map->start > pos->start) {
  210. struct map *before = map__clone(pos);
  211. if (before == NULL)
  212. return -ENOMEM;
  213. before->end = map->start - 1;
  214. map_groups__insert(self, before);
  215. if (verbose >= 2)
  216. map__fprintf(before, stderr);
  217. }
  218. if (map->end < pos->end) {
  219. struct map *after = map__clone(pos);
  220. if (after == NULL)
  221. return -ENOMEM;
  222. after->start = map->end + 1;
  223. map_groups__insert(self, after);
  224. if (verbose >= 2)
  225. map__fprintf(after, stderr);
  226. }
  227. }
  228. return 0;
  229. }
  230. void maps__insert(struct rb_root *maps, struct map *map)
  231. {
  232. struct rb_node **p = &maps->rb_node;
  233. struct rb_node *parent = NULL;
  234. const u64 ip = map->start;
  235. struct map *m;
  236. while (*p != NULL) {
  237. parent = *p;
  238. m = rb_entry(parent, struct map, rb_node);
  239. if (ip < m->start)
  240. p = &(*p)->rb_left;
  241. else
  242. p = &(*p)->rb_right;
  243. }
  244. rb_link_node(&map->rb_node, parent, p);
  245. rb_insert_color(&map->rb_node, maps);
  246. }
  247. struct map *maps__find(struct rb_root *maps, u64 ip)
  248. {
  249. struct rb_node **p = &maps->rb_node;
  250. struct rb_node *parent = NULL;
  251. struct map *m;
  252. while (*p != NULL) {
  253. parent = *p;
  254. m = rb_entry(parent, struct map, rb_node);
  255. if (ip < m->start)
  256. p = &(*p)->rb_left;
  257. else if (ip > m->end)
  258. p = &(*p)->rb_right;
  259. else
  260. return m;
  261. }
  262. return NULL;
  263. }
  264. void thread__insert_map(struct thread *self, struct map *map)
  265. {
  266. map_groups__fixup_overlappings(&self->mg, map);
  267. map_groups__insert(&self->mg, map);
  268. }
  269. /*
  270. * XXX This should not really _copy_ te maps, but refcount them.
  271. */
  272. static int map_groups__clone(struct map_groups *self,
  273. struct map_groups *parent, enum map_type type)
  274. {
  275. struct rb_node *nd;
  276. for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
  277. struct map *map = rb_entry(nd, struct map, rb_node);
  278. struct map *new = map__clone(map);
  279. if (new == NULL)
  280. return -ENOMEM;
  281. map_groups__insert(self, new);
  282. }
  283. return 0;
  284. }
  285. int thread__fork(struct thread *self, struct thread *parent)
  286. {
  287. int i;
  288. if (parent->comm_set) {
  289. if (self->comm)
  290. free(self->comm);
  291. self->comm = strdup(parent->comm);
  292. if (!self->comm)
  293. return -ENOMEM;
  294. self->comm_set = true;
  295. }
  296. for (i = 0; i < MAP__NR_TYPES; ++i)
  297. if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
  298. return -ENOMEM;
  299. return 0;
  300. }
  301. size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
  302. {
  303. size_t ret = 0;
  304. struct rb_node *nd;
  305. for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
  306. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  307. ret += thread__fprintf(pos, fp);
  308. }
  309. return ret;
  310. }
  311. struct symbol *map_groups__find_symbol(struct map_groups *self,
  312. enum map_type type, u64 addr,
  313. symbol_filter_t filter)
  314. {
  315. struct map *map = map_groups__find(self, type, addr);
  316. if (map != NULL)
  317. return map__find_symbol(map, map->map_ip(map, addr), filter);
  318. return NULL;
  319. }