thread.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. }
  23. return thread->mg ? 0 : -1;
  24. }
  25. struct thread *thread__new(pid_t pid, pid_t tid)
  26. {
  27. char *comm_str;
  28. struct comm *comm;
  29. struct thread *thread = zalloc(sizeof(*thread));
  30. if (thread != NULL) {
  31. thread->pid_ = pid;
  32. thread->tid = tid;
  33. thread->ppid = -1;
  34. thread->cpu = -1;
  35. INIT_LIST_HEAD(&thread->comm_list);
  36. if (unwind__prepare_access(thread) < 0)
  37. goto err_thread;
  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, 0);
  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. list_del_init(&thread->node);
  81. thread__delete(thread);
  82. }
  83. }
  84. struct comm *thread__comm(const struct thread *thread)
  85. {
  86. if (list_empty(&thread->comm_list))
  87. return NULL;
  88. return list_first_entry(&thread->comm_list, struct comm, list);
  89. }
  90. struct comm *thread__exec_comm(const struct thread *thread)
  91. {
  92. struct comm *comm, *last = NULL;
  93. list_for_each_entry(comm, &thread->comm_list, list) {
  94. if (comm->exec)
  95. return comm;
  96. last = comm;
  97. }
  98. return last;
  99. }
  100. int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
  101. bool exec)
  102. {
  103. struct comm *new, *curr = thread__comm(thread);
  104. int err;
  105. /* Override the default :tid entry */
  106. if (!thread->comm_set) {
  107. err = comm__override(curr, str, timestamp, exec);
  108. if (err)
  109. return err;
  110. } else {
  111. new = comm__new(str, timestamp, exec);
  112. if (!new)
  113. return -ENOMEM;
  114. list_add(&new->list, &thread->comm_list);
  115. if (exec)
  116. unwind__flush_access(thread);
  117. }
  118. thread->comm_set = true;
  119. return 0;
  120. }
  121. const char *thread__comm_str(const struct thread *thread)
  122. {
  123. const struct comm *comm = thread__comm(thread);
  124. if (!comm)
  125. return NULL;
  126. return comm__str(comm);
  127. }
  128. /* CHECKME: it should probably better return the max comm len from its comm list */
  129. int thread__comm_len(struct thread *thread)
  130. {
  131. if (!thread->comm_len) {
  132. const char *comm = thread__comm_str(thread);
  133. if (!comm)
  134. return 0;
  135. thread->comm_len = strlen(comm);
  136. }
  137. return thread->comm_len;
  138. }
  139. size_t thread__fprintf(struct thread *thread, FILE *fp)
  140. {
  141. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  142. map_groups__fprintf(thread->mg, fp);
  143. }
  144. void thread__insert_map(struct thread *thread, struct map *map)
  145. {
  146. map_groups__fixup_overlappings(thread->mg, map, stderr);
  147. map_groups__insert(thread->mg, map);
  148. }
  149. static int thread__clone_map_groups(struct thread *thread,
  150. struct thread *parent)
  151. {
  152. int i;
  153. /* This is new thread, we share map groups for process. */
  154. if (thread->pid_ == parent->pid_)
  155. return 0;
  156. if (thread->mg == parent->mg) {
  157. pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
  158. thread->pid_, thread->tid, parent->pid_, parent->tid);
  159. return 0;
  160. }
  161. /* But this one is new process, copy maps. */
  162. for (i = 0; i < MAP__NR_TYPES; ++i)
  163. if (map_groups__clone(thread->mg, parent->mg, i) < 0)
  164. return -ENOMEM;
  165. return 0;
  166. }
  167. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
  168. {
  169. int err;
  170. if (parent->comm_set) {
  171. const char *comm = thread__comm_str(parent);
  172. if (!comm)
  173. return -ENOMEM;
  174. err = thread__set_comm(thread, comm, timestamp);
  175. if (err)
  176. return err;
  177. }
  178. thread->ppid = parent->tid;
  179. return thread__clone_map_groups(thread, parent);
  180. }
  181. void thread__find_cpumode_addr_location(struct thread *thread,
  182. enum map_type type, u64 addr,
  183. struct addr_location *al)
  184. {
  185. size_t i;
  186. const u8 cpumodes[] = {
  187. PERF_RECORD_MISC_USER,
  188. PERF_RECORD_MISC_KERNEL,
  189. PERF_RECORD_MISC_GUEST_USER,
  190. PERF_RECORD_MISC_GUEST_KERNEL
  191. };
  192. for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
  193. thread__find_addr_location(thread, cpumodes[i], type, addr, al);
  194. if (al->map)
  195. break;
  196. }
  197. }