thread.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. u64 timestamp __maybe_unused)
  31. {
  32. int err;
  33. if (thread->comm)
  34. free(thread->comm);
  35. thread->comm = strdup(comm);
  36. err = thread->comm == NULL ? -ENOMEM : 0;
  37. if (!err) {
  38. thread->comm_set = true;
  39. }
  40. return err;
  41. }
  42. const char *thread__comm_str(const struct thread *thread)
  43. {
  44. return thread->comm;
  45. }
  46. int thread__comm_len(struct thread *thread)
  47. {
  48. if (!thread->comm_len) {
  49. if (!thread->comm)
  50. return 0;
  51. thread->comm_len = strlen(thread->comm);
  52. }
  53. return thread->comm_len;
  54. }
  55. size_t thread__fprintf(struct thread *thread, FILE *fp)
  56. {
  57. return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
  58. map_groups__fprintf(&thread->mg, verbose, fp);
  59. }
  60. void thread__insert_map(struct thread *thread, struct map *map)
  61. {
  62. map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr);
  63. map_groups__insert(&thread->mg, map);
  64. }
  65. int thread__fork(struct thread *thread, struct thread *parent,
  66. u64 timestamp __maybe_unused)
  67. {
  68. int i;
  69. if (parent->comm_set) {
  70. if (thread->comm)
  71. free(thread->comm);
  72. thread->comm = strdup(parent->comm);
  73. if (!thread->comm)
  74. return -ENOMEM;
  75. thread->comm_set = true;
  76. }
  77. for (i = 0; i < MAP__NR_TYPES; ++i)
  78. if (map_groups__clone(&thread->mg, &parent->mg, i) < 0)
  79. return -ENOMEM;
  80. thread->ppid = parent->tid;
  81. return 0;
  82. }