thread-map.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/prctl.h>
  5. #include "tests.h"
  6. #include "thread_map.h"
  7. #include "debug.h"
  8. #define NAME (const char *) "perf"
  9. #define NAMEUL (unsigned long) NAME
  10. int test__thread_map(int subtest __maybe_unused)
  11. {
  12. struct thread_map *map;
  13. TEST_ASSERT_VAL("failed to set process name",
  14. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  15. /* test map on current pid */
  16. map = thread_map__new_by_pid(getpid());
  17. TEST_ASSERT_VAL("failed to alloc map", map);
  18. thread_map__read_comms(map);
  19. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  20. TEST_ASSERT_VAL("wrong pid",
  21. thread_map__pid(map, 0) == getpid());
  22. TEST_ASSERT_VAL("wrong comm",
  23. thread_map__comm(map, 0) &&
  24. !strcmp(thread_map__comm(map, 0), NAME));
  25. TEST_ASSERT_VAL("wrong refcnt",
  26. atomic_read(&map->refcnt) == 1);
  27. thread_map__put(map);
  28. /* test dummy pid */
  29. map = thread_map__new_dummy();
  30. TEST_ASSERT_VAL("failed to alloc map", map);
  31. thread_map__read_comms(map);
  32. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  33. TEST_ASSERT_VAL("wrong pid", thread_map__pid(map, 0) == -1);
  34. TEST_ASSERT_VAL("wrong comm",
  35. thread_map__comm(map, 0) &&
  36. !strcmp(thread_map__comm(map, 0), "dummy"));
  37. TEST_ASSERT_VAL("wrong refcnt",
  38. atomic_read(&map->refcnt) == 1);
  39. thread_map__put(map);
  40. return 0;
  41. }
  42. static int process_event(struct perf_tool *tool __maybe_unused,
  43. union perf_event *event,
  44. struct perf_sample *sample __maybe_unused,
  45. struct machine *machine __maybe_unused)
  46. {
  47. struct thread_map_event *map = &event->thread_map;
  48. struct thread_map *threads;
  49. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  50. TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid());
  51. TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME));
  52. threads = thread_map__new_event(&event->thread_map);
  53. TEST_ASSERT_VAL("failed to alloc map", threads);
  54. TEST_ASSERT_VAL("wrong nr", threads->nr == 1);
  55. TEST_ASSERT_VAL("wrong pid",
  56. thread_map__pid(threads, 0) == getpid());
  57. TEST_ASSERT_VAL("wrong comm",
  58. thread_map__comm(threads, 0) &&
  59. !strcmp(thread_map__comm(threads, 0), NAME));
  60. TEST_ASSERT_VAL("wrong refcnt",
  61. atomic_read(&threads->refcnt) == 1);
  62. thread_map__put(threads);
  63. return 0;
  64. }
  65. int test__thread_map_synthesize(int subtest __maybe_unused)
  66. {
  67. struct thread_map *threads;
  68. TEST_ASSERT_VAL("failed to set process name",
  69. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  70. /* test map on current pid */
  71. threads = thread_map__new_by_pid(getpid());
  72. TEST_ASSERT_VAL("failed to alloc map", threads);
  73. thread_map__read_comms(threads);
  74. TEST_ASSERT_VAL("failed to synthesize map",
  75. !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
  76. return 0;
  77. }
  78. int test__thread_map_remove(int subtest __maybe_unused)
  79. {
  80. struct thread_map *threads;
  81. char *str;
  82. int i;
  83. TEST_ASSERT_VAL("failed to allocate map string",
  84. asprintf(&str, "%d,%d", getpid(), getppid()) >= 0);
  85. threads = thread_map__new_str(str, NULL, 0);
  86. TEST_ASSERT_VAL("failed to allocate thread_map",
  87. threads);
  88. if (verbose)
  89. thread_map__fprintf(threads, stderr);
  90. TEST_ASSERT_VAL("failed to remove thread",
  91. !thread_map__remove(threads, 0));
  92. TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1);
  93. if (verbose)
  94. thread_map__fprintf(threads, stderr);
  95. TEST_ASSERT_VAL("failed to remove thread",
  96. !thread_map__remove(threads, 0));
  97. TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0);
  98. if (verbose)
  99. thread_map__fprintf(threads, stderr);
  100. TEST_ASSERT_VAL("failed to not remove thread",
  101. thread_map__remove(threads, 0));
  102. for (i = 0; i < threads->nr; i++)
  103. free(threads->map[i].comm);
  104. free(threads);
  105. return 0;
  106. }