thread.c 2.7 KB

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