thread.c 1.9 KB

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