thread.c 6.3 KB

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