thread.c 4.0 KB

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