thread.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 "thread-stack.h"
  8. #include "util.h"
  9. #include "debug.h"
  10. #include "comm.h"
  11. #include "unwind.h"
  12. #include <api/fs/fs.h>
  13. int thread__init_map_groups(struct thread *thread, struct machine *machine)
  14. {
  15. struct thread *leader;
  16. pid_t pid = thread->pid_;
  17. if (pid == thread->tid || pid == -1) {
  18. thread->mg = map_groups__new(machine);
  19. } else {
  20. leader = __machine__findnew_thread(machine, pid, pid);
  21. if (leader) {
  22. thread->mg = map_groups__get(leader->mg);
  23. thread__put(leader);
  24. }
  25. }
  26. return thread->mg ? 0 : -1;
  27. }
  28. struct thread *thread__new(pid_t pid, pid_t tid)
  29. {
  30. char *comm_str;
  31. struct comm *comm;
  32. struct thread *thread = zalloc(sizeof(*thread));
  33. if (thread != NULL) {
  34. thread->pid_ = pid;
  35. thread->tid = tid;
  36. thread->ppid = -1;
  37. thread->cpu = -1;
  38. INIT_LIST_HEAD(&thread->comm_list);
  39. comm_str = malloc(32);
  40. if (!comm_str)
  41. goto err_thread;
  42. snprintf(comm_str, 32, ":%d", tid);
  43. comm = comm__new(comm_str, 0, false);
  44. free(comm_str);
  45. if (!comm)
  46. goto err_thread;
  47. list_add(&comm->list, &thread->comm_list);
  48. atomic_set(&thread->refcnt, 1);
  49. RB_CLEAR_NODE(&thread->rb_node);
  50. }
  51. return thread;
  52. err_thread:
  53. free(thread);
  54. return NULL;
  55. }
  56. void thread__delete(struct thread *thread)
  57. {
  58. struct comm *comm, *tmp;
  59. BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
  60. thread_stack__free(thread);
  61. if (thread->mg) {
  62. map_groups__put(thread->mg);
  63. thread->mg = NULL;
  64. }
  65. list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
  66. list_del(&comm->list);
  67. comm__free(comm);
  68. }
  69. unwind__finish_access(thread);
  70. free(thread);
  71. }
  72. struct thread *thread__get(struct thread *thread)
  73. {
  74. if (thread)
  75. atomic_inc(&thread->refcnt);
  76. return thread;
  77. }
  78. void thread__put(struct thread *thread)
  79. {
  80. if (thread && atomic_dec_and_test(&thread->refcnt)) {
  81. /*
  82. * Remove it from the dead_threads list, as last reference
  83. * is gone.
  84. */
  85. list_del_init(&thread->node);
  86. thread__delete(thread);
  87. }
  88. }
  89. struct comm *thread__comm(const struct thread *thread)
  90. {
  91. if (list_empty(&thread->comm_list))
  92. return NULL;
  93. return list_first_entry(&thread->comm_list, struct comm, list);
  94. }
  95. struct comm *thread__exec_comm(const struct thread *thread)
  96. {
  97. struct comm *comm, *last = NULL;
  98. list_for_each_entry(comm, &thread->comm_list, list) {
  99. if (comm->exec)
  100. return comm;
  101. last = comm;
  102. }
  103. return last;
  104. }
  105. int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
  106. bool exec)
  107. {
  108. struct comm *new, *curr = thread__comm(thread);
  109. int err;
  110. /* Override the default :tid entry */
  111. if (!thread->comm_set) {
  112. err = comm__override(curr, str, timestamp, exec);
  113. if (err)
  114. return err;
  115. } else {
  116. new = comm__new(str, timestamp, exec);
  117. if (!new)
  118. return -ENOMEM;
  119. list_add(&new->list, &thread->comm_list);
  120. if (exec)
  121. unwind__flush_access(thread);
  122. }
  123. thread->comm_set = true;
  124. return 0;
  125. }
  126. int thread__set_comm_from_proc(struct thread *thread)
  127. {
  128. char path[64];
  129. char *comm = NULL;
  130. size_t sz;
  131. int err = -1;
  132. if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
  133. thread->pid_, thread->tid) >= (int)sizeof(path)) &&
  134. procfs__read_str(path, &comm, &sz) == 0) {
  135. comm[sz - 1] = '\0';
  136. err = thread__set_comm(thread, comm, 0);
  137. }
  138. return err;
  139. }
  140. const char *thread__comm_str(const struct thread *thread)
  141. {
  142. const struct comm *comm = thread__comm(thread);
  143. if (!comm)
  144. return NULL;
  145. return comm__str(comm);
  146. }
  147. /* CHECKME: it should probably better return the max comm len from its comm list */
  148. int thread__comm_len(struct thread *thread)
  149. {
  150. if (!thread->comm_len) {
  151. const char *comm = thread__comm_str(thread);
  152. if (!comm)
  153. return 0;
  154. thread->comm_len = strlen(comm);
  155. }
  156. return thread->comm_len;
  157. }
  158. size_t thread__fprintf(struct thread *thread, FILE *fp)
  159. {
  160. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  161. map_groups__fprintf(thread->mg, fp);
  162. }
  163. int thread__insert_map(struct thread *thread, struct map *map)
  164. {
  165. int ret;
  166. ret = unwind__prepare_access(thread, map, NULL);
  167. if (ret)
  168. return ret;
  169. map_groups__fixup_overlappings(thread->mg, map, stderr);
  170. map_groups__insert(thread->mg, map);
  171. return 0;
  172. }
  173. static int __thread__prepare_access(struct thread *thread)
  174. {
  175. bool initialized = false;
  176. int i, err = 0;
  177. for (i = 0; i < MAP__NR_TYPES; ++i) {
  178. struct maps *maps = &thread->mg->maps[i];
  179. struct map *map;
  180. pthread_rwlock_rdlock(&maps->lock);
  181. for (map = maps__first(maps); map; map = map__next(map)) {
  182. err = unwind__prepare_access(thread, map, &initialized);
  183. if (err || initialized)
  184. break;
  185. }
  186. pthread_rwlock_unlock(&maps->lock);
  187. }
  188. return err;
  189. }
  190. static int thread__prepare_access(struct thread *thread)
  191. {
  192. int err = 0;
  193. if (symbol_conf.use_callchain)
  194. err = __thread__prepare_access(thread);
  195. return err;
  196. }
  197. static int thread__clone_map_groups(struct thread *thread,
  198. struct thread *parent)
  199. {
  200. int i;
  201. /* This is new thread, we share map groups for process. */
  202. if (thread->pid_ == parent->pid_)
  203. return thread__prepare_access(thread);
  204. if (thread->mg == parent->mg) {
  205. pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
  206. thread->pid_, thread->tid, parent->pid_, parent->tid);
  207. return 0;
  208. }
  209. /* But this one is new process, copy maps. */
  210. for (i = 0; i < MAP__NR_TYPES; ++i)
  211. if (map_groups__clone(thread, parent->mg, i) < 0)
  212. return -ENOMEM;
  213. return 0;
  214. }
  215. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
  216. {
  217. int err;
  218. if (parent->comm_set) {
  219. const char *comm = thread__comm_str(parent);
  220. if (!comm)
  221. return -ENOMEM;
  222. err = thread__set_comm(thread, comm, timestamp);
  223. if (err)
  224. return err;
  225. }
  226. thread->ppid = parent->tid;
  227. return thread__clone_map_groups(thread, parent);
  228. }
  229. void thread__find_cpumode_addr_location(struct thread *thread,
  230. enum map_type type, u64 addr,
  231. struct addr_location *al)
  232. {
  233. size_t i;
  234. const u8 cpumodes[] = {
  235. PERF_RECORD_MISC_USER,
  236. PERF_RECORD_MISC_KERNEL,
  237. PERF_RECORD_MISC_GUEST_USER,
  238. PERF_RECORD_MISC_GUEST_KERNEL
  239. };
  240. for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
  241. thread__find_addr_location(thread, cpumodes[i], type, addr, al);
  242. if (al->map)
  243. break;
  244. }
  245. }
  246. struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
  247. {
  248. if (thread->pid_ == thread->tid)
  249. return thread__get(thread);
  250. if (thread->pid_ == -1)
  251. return NULL;
  252. return machine__find_thread(machine, thread->pid_, thread->pid_);
  253. }