thread.c 4.1 KB

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