thread.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /* Skip "." and ".." directories */
  10. static int filter(const struct dirent *dir)
  11. {
  12. if (dir->d_name[0] == '.')
  13. return 0;
  14. else
  15. return 1;
  16. }
  17. struct thread_map *thread_map__new_by_pid(pid_t pid)
  18. {
  19. struct thread_map *threads;
  20. char name[256];
  21. int items;
  22. struct dirent **namelist = NULL;
  23. int i;
  24. sprintf(name, "/proc/%d/task", pid);
  25. items = scandir(name, &namelist, filter, NULL);
  26. if (items <= 0)
  27. return NULL;
  28. threads = malloc(sizeof(*threads) + sizeof(pid_t) * items);
  29. if (threads != NULL) {
  30. for (i = 0; i < items; i++)
  31. threads->map[i] = atoi(namelist[i]->d_name);
  32. threads->nr = items;
  33. }
  34. for (i=0; i<items; i++)
  35. free(namelist[i]);
  36. free(namelist);
  37. return threads;
  38. }
  39. struct thread_map *thread_map__new_by_tid(pid_t tid)
  40. {
  41. struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t));
  42. if (threads != NULL) {
  43. threads->map[0] = tid;
  44. threads->nr = 1;
  45. }
  46. return threads;
  47. }
  48. struct thread_map *thread_map__new(pid_t pid, pid_t tid)
  49. {
  50. if (pid != -1)
  51. return thread_map__new_by_pid(pid);
  52. return thread_map__new_by_tid(tid);
  53. }
  54. static struct thread *thread__new(pid_t pid)
  55. {
  56. struct thread *self = zalloc(sizeof(*self));
  57. if (self != NULL) {
  58. map_groups__init(&self->mg);
  59. self->pid = pid;
  60. self->comm = malloc(32);
  61. if (self->comm)
  62. snprintf(self->comm, 32, ":%d", self->pid);
  63. }
  64. return self;
  65. }
  66. void thread__delete(struct thread *self)
  67. {
  68. map_groups__exit(&self->mg);
  69. free(self->comm);
  70. free(self);
  71. }
  72. int thread__set_comm(struct thread *self, const char *comm)
  73. {
  74. int err;
  75. if (self->comm)
  76. free(self->comm);
  77. self->comm = strdup(comm);
  78. err = self->comm == NULL ? -ENOMEM : 0;
  79. if (!err) {
  80. self->comm_set = true;
  81. map_groups__flush(&self->mg);
  82. }
  83. return err;
  84. }
  85. int thread__comm_len(struct thread *self)
  86. {
  87. if (!self->comm_len) {
  88. if (!self->comm)
  89. return 0;
  90. self->comm_len = strlen(self->comm);
  91. }
  92. return self->comm_len;
  93. }
  94. static size_t thread__fprintf(struct thread *self, FILE *fp)
  95. {
  96. return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
  97. map_groups__fprintf(&self->mg, verbose, fp);
  98. }
  99. struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
  100. {
  101. struct rb_node **p = &self->threads.rb_node;
  102. struct rb_node *parent = NULL;
  103. struct thread *th;
  104. /*
  105. * Font-end cache - PID lookups come in blocks,
  106. * so most of the time we dont have to look up
  107. * the full rbtree:
  108. */
  109. if (self->last_match && self->last_match->pid == pid)
  110. return self->last_match;
  111. while (*p != NULL) {
  112. parent = *p;
  113. th = rb_entry(parent, struct thread, rb_node);
  114. if (th->pid == pid) {
  115. self->last_match = th;
  116. return th;
  117. }
  118. if (pid < th->pid)
  119. p = &(*p)->rb_left;
  120. else
  121. p = &(*p)->rb_right;
  122. }
  123. th = thread__new(pid);
  124. if (th != NULL) {
  125. rb_link_node(&th->rb_node, parent, p);
  126. rb_insert_color(&th->rb_node, &self->threads);
  127. self->last_match = th;
  128. }
  129. return th;
  130. }
  131. void thread__insert_map(struct thread *self, struct map *map)
  132. {
  133. map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
  134. map_groups__insert(&self->mg, map);
  135. }
  136. int thread__fork(struct thread *self, struct thread *parent)
  137. {
  138. int i;
  139. if (parent->comm_set) {
  140. if (self->comm)
  141. free(self->comm);
  142. self->comm = strdup(parent->comm);
  143. if (!self->comm)
  144. return -ENOMEM;
  145. self->comm_set = true;
  146. }
  147. for (i = 0; i < MAP__NR_TYPES; ++i)
  148. if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
  149. return -ENOMEM;
  150. return 0;
  151. }
  152. size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
  153. {
  154. size_t ret = 0;
  155. struct rb_node *nd;
  156. for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
  157. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  158. ret += thread__fprintf(pos, fp);
  159. }
  160. return ret;
  161. }