thread.c 4.5 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 "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. if (exec)
  99. unwind__flush_access(thread);
  100. }
  101. thread->comm_set = true;
  102. return 0;
  103. }
  104. const char *thread__comm_str(const struct thread *thread)
  105. {
  106. const struct comm *comm = thread__comm(thread);
  107. if (!comm)
  108. return NULL;
  109. return comm__str(comm);
  110. }
  111. /* CHECKME: it should probably better return the max comm len from its comm list */
  112. int thread__comm_len(struct thread *thread)
  113. {
  114. if (!thread->comm_len) {
  115. const char *comm = thread__comm_str(thread);
  116. if (!comm)
  117. return 0;
  118. thread->comm_len = strlen(comm);
  119. }
  120. return thread->comm_len;
  121. }
  122. size_t thread__fprintf(struct thread *thread, FILE *fp)
  123. {
  124. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  125. map_groups__fprintf(thread->mg, fp);
  126. }
  127. void thread__insert_map(struct thread *thread, struct map *map)
  128. {
  129. map_groups__fixup_overlappings(thread->mg, map, stderr);
  130. map_groups__insert(thread->mg, map);
  131. }
  132. static int thread__clone_map_groups(struct thread *thread,
  133. struct thread *parent)
  134. {
  135. int i;
  136. /* This is new thread, we share map groups for process. */
  137. if (thread->pid_ == parent->pid_)
  138. return 0;
  139. /* But this one is new process, copy maps. */
  140. for (i = 0; i < MAP__NR_TYPES; ++i)
  141. if (map_groups__clone(thread->mg, parent->mg, i) < 0)
  142. return -ENOMEM;
  143. return 0;
  144. }
  145. int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
  146. {
  147. int err;
  148. if (parent->comm_set) {
  149. const char *comm = thread__comm_str(parent);
  150. if (!comm)
  151. return -ENOMEM;
  152. err = thread__set_comm(thread, comm, timestamp);
  153. if (err)
  154. return err;
  155. thread->comm_set = true;
  156. }
  157. thread->ppid = parent->tid;
  158. return thread__clone_map_groups(thread, parent);
  159. }
  160. void thread__find_cpumode_addr_location(struct thread *thread,
  161. struct machine *machine,
  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, machine, cpumodes[i], type,
  174. addr, al);
  175. if (al->map)
  176. break;
  177. }
  178. }