thread.c 4.4 KB

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