thread.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. struct thread *thread__new(pid_t pid, pid_t tid)
  10. {
  11. struct thread *self = zalloc(sizeof(*self));
  12. if (self != NULL) {
  13. map_groups__init(&self->mg);
  14. self->pid_ = pid;
  15. self->tid = tid;
  16. self->ppid = -1;
  17. self->comm = malloc(32);
  18. if (self->comm)
  19. snprintf(self->comm, 32, ":%d", self->tid);
  20. }
  21. return self;
  22. }
  23. void thread__delete(struct thread *self)
  24. {
  25. map_groups__exit(&self->mg);
  26. free(self->comm);
  27. free(self);
  28. }
  29. int thread__set_comm(struct thread *self, const char *comm)
  30. {
  31. int err;
  32. if (self->comm)
  33. free(self->comm);
  34. self->comm = strdup(comm);
  35. err = self->comm == NULL ? -ENOMEM : 0;
  36. if (!err) {
  37. self->comm_set = true;
  38. }
  39. return err;
  40. }
  41. int thread__comm_len(struct thread *self)
  42. {
  43. if (!self->comm_len) {
  44. if (!self->comm)
  45. return 0;
  46. self->comm_len = strlen(self->comm);
  47. }
  48. return self->comm_len;
  49. }
  50. size_t thread__fprintf(struct thread *thread, FILE *fp)
  51. {
  52. return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) +
  53. map_groups__fprintf(&thread->mg, verbose, fp);
  54. }
  55. void thread__insert_map(struct thread *self, struct map *map)
  56. {
  57. map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
  58. map_groups__insert(&self->mg, map);
  59. }
  60. int thread__fork(struct thread *self, struct thread *parent)
  61. {
  62. int i;
  63. if (parent->comm_set) {
  64. if (self->comm)
  65. free(self->comm);
  66. self->comm = strdup(parent->comm);
  67. if (!self->comm)
  68. return -ENOMEM;
  69. self->comm_set = true;
  70. }
  71. for (i = 0; i < MAP__NR_TYPES; ++i)
  72. if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
  73. return -ENOMEM;
  74. self->ppid = parent->tid;
  75. return 0;
  76. }