thread.c 5.1 KB

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