thread.c 2.7 KB

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