thread.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "util.h"
  8. #include "debug.h"
  9. #include "comm.h"
  10. #include "unwind.h"
  11. int thread__init_map_groups(struct thread *thread, struct machine *machine)
  12. {
  13. struct thread *leader;
  14. pid_t pid = thread->pid_;
  15. if (pid == thread->tid || pid == -1) {
  16. thread->mg = map_groups__new();
  17. } else {
  18. leader = machine__findnew_thread(machine, pid, pid);
  19. if (leader)
  20. thread->mg = map_groups__get(leader->mg);
  21. }
  22. return thread->mg ? 0 : -1;
  23. }
  24. struct thread *thread__new(pid_t pid, pid_t tid)
  25. {
  26. char *comm_str;
  27. struct comm *comm;
  28. struct thread *thread = zalloc(sizeof(*thread));
  29. if (thread != NULL) {
  30. thread->pid_ = pid;
  31. thread->tid = tid;
  32. thread->ppid = -1;
  33. thread->cpu = -1;
  34. INIT_LIST_HEAD(&thread->comm_list);
  35. if (unwind__prepare_access(thread) < 0)
  36. goto err_thread;
  37. comm_str = malloc(32);
  38. if (!comm_str)
  39. goto err_thread;
  40. snprintf(comm_str, 32, ":%d", tid);
  41. comm = comm__new(comm_str, 0, false);
  42. free(comm_str);
  43. if (!comm)
  44. goto err_thread;
  45. list_add(&comm->list, &thread->comm_list);
  46. }
  47. return thread;
  48. err_thread:
  49. free(thread);
  50. return NULL;
  51. }
  52. void thread__delete(struct thread *thread)
  53. {
  54. struct comm *comm, *tmp;
  55. if (thread->mg) {
  56. map_groups__put(thread->mg);
  57. thread->mg = NULL;
  58. }
  59. list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
  60. list_del(&comm->list);
  61. comm__free(comm);
  62. }
  63. unwind__finish_access(thread);
  64. free(thread);
  65. }
  66. struct comm *thread__comm(const struct thread *thread)
  67. {
  68. if (list_empty(&thread->comm_list))
  69. return NULL;
  70. return list_first_entry(&thread->comm_list, struct comm, list);
  71. }
  72. struct comm *thread__exec_comm(const struct thread *thread)
  73. {
  74. struct comm *comm, *last = NULL;
  75. list_for_each_entry(comm, &thread->comm_list, list) {
  76. if (comm->exec)
  77. return comm;
  78. last = comm;
  79. }
  80. return last;
  81. }
  82. /* CHECKME: time should always be 0 if event aren't ordered */
  83. int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
  84. bool exec)
  85. {
  86. struct comm *new, *curr = thread__comm(thread);
  87. int err;
  88. /* Override latest entry if it had no specific time coverage */
  89. if (!curr->start && !curr->exec) {
  90. err = comm__override(curr, str, timestamp, exec);
  91. if (err)
  92. return err;
  93. } else {
  94. new = comm__new(str, timestamp, exec);
  95. if (!new)
  96. return -ENOMEM;
  97. list_add(&new->list, &thread->comm_list);
  98. }
  99. thread->comm_set = true;
  100. return 0;
  101. }
  102. const char *thread__comm_str(const struct thread *thread)
  103. {
  104. const struct comm *comm = thread__comm(thread);
  105. if (!comm)
  106. return NULL;
  107. return comm__str(comm);
  108. }
  109. /* CHECKME: it should probably better return the max comm len from its comm list */
  110. int thread__comm_len(struct thread *thread)
  111. {
  112. if (!thread->comm_len) {
  113. const char *comm = thread__comm_str(thread);
  114. if (!comm)
  115. return 0;
  116. thread->comm_len = strlen(comm);
  117. }
  118. return thread->comm_len;
  119. }
  120. size_t thread__fprintf(struct thread *thread, FILE *fp)
  121. {
  122. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  123. map_groups__fprintf(thread->mg, fp);
  124. }
  125. void thread__insert_map(struct thread *thread, struct map *map)
  126. {
  127. map_groups__fixup_overlappings(thread->mg, map, stderr);
  128. map_groups__insert(thread->mg, map);
  129. }
  130. static int thread__clone_map_groups(struct thread *thread,
  131. struct thread *parent)
  132. {
  133. int i;
  134. /* This is new thread, we share map groups for process. */
  135. if (thread->pid_ == parent->pid_)
  136. return 0;
  137. /* But this one is new process, copy maps. */
  138. for (i = 0; i < MAP__NR_TYPES; ++i)
  139. if (map_groups__clone(thread->mg, parent->mg, i) < 0)
  140. return -ENOMEM;
  141. return 0;
  142. }
  143. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
  144. {
  145. int err;
  146. if (parent->comm_set) {
  147. const char *comm = thread__comm_str(parent);
  148. if (!comm)
  149. return -ENOMEM;
  150. err = thread__set_comm(thread, comm, timestamp);
  151. if (err)
  152. return err;
  153. thread->comm_set = true;
  154. }
  155. thread->ppid = parent->tid;
  156. return thread__clone_map_groups(thread, parent);
  157. }
  158. void thread__find_cpumode_addr_location(struct thread *thread,
  159. struct machine *machine,
  160. enum map_type type, u64 addr,
  161. struct addr_location *al)
  162. {
  163. size_t i;
  164. const u8 const cpumodes[] = {
  165. PERF_RECORD_MISC_USER,
  166. PERF_RECORD_MISC_KERNEL,
  167. PERF_RECORD_MISC_GUEST_USER,
  168. PERF_RECORD_MISC_GUEST_KERNEL
  169. };
  170. for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
  171. thread__find_addr_location(thread, machine, cpumodes[i], type,
  172. addr, al);
  173. if (al->map)
  174. break;
  175. }
  176. }