thread.c 3.3 KB

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