thread.c 5.4 KB

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