thread.c 3.1 KB

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