thread.c 4.6 KB

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