thread-map.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <sys/prctl.h>
  7. #include "tests.h"
  8. #include "thread_map.h"
  9. #include "debug.h"
  10. #include "event.h"
  11. #include "util/synthetic-events.h"
  12. #include <linux/zalloc.h>
  13. #include <perf/event.h>
  14. #include <internal/threadmap.h>
  15. struct perf_sample;
  16. struct perf_tool;
  17. struct machine;
  18. #define NAME (const char *) "perf"
  19. #define NAMEUL (unsigned long) NAME
  20. static int test__thread_map(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  21. {
  22. struct perf_thread_map *map;
  23. TEST_ASSERT_VAL("failed to set process name",
  24. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  25. /* test map on current pid */
  26. map = thread_map__new_by_pid(getpid());
  27. TEST_ASSERT_VAL("failed to alloc map", map);
  28. thread_map__read_comms(map);
  29. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  30. TEST_ASSERT_VAL("wrong pid",
  31. perf_thread_map__pid(map, 0) == getpid());
  32. TEST_ASSERT_VAL("wrong comm",
  33. perf_thread_map__comm(map, 0) &&
  34. !strcmp(perf_thread_map__comm(map, 0), NAME));
  35. TEST_ASSERT_VAL("wrong refcnt",
  36. refcount_read(&map->refcnt) == 1);
  37. perf_thread_map__put(map);
  38. /* test dummy pid */
  39. map = perf_thread_map__new_dummy();
  40. TEST_ASSERT_VAL("failed to alloc map", map);
  41. thread_map__read_comms(map);
  42. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  43. TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
  44. TEST_ASSERT_VAL("wrong comm",
  45. perf_thread_map__comm(map, 0) &&
  46. !strcmp(perf_thread_map__comm(map, 0), "dummy"));
  47. TEST_ASSERT_VAL("wrong refcnt",
  48. refcount_read(&map->refcnt) == 1);
  49. perf_thread_map__put(map);
  50. return 0;
  51. }
  52. static int process_event(const struct perf_tool *tool __maybe_unused,
  53. union perf_event *event,
  54. struct perf_sample *sample __maybe_unused,
  55. struct machine *machine __maybe_unused)
  56. {
  57. struct perf_record_thread_map *map = &event->thread_map;
  58. struct perf_thread_map *threads;
  59. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  60. TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid());
  61. TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME));
  62. threads = thread_map__new_event(&event->thread_map);
  63. TEST_ASSERT_VAL("failed to alloc map", threads);
  64. TEST_ASSERT_VAL("wrong nr", threads->nr == 1);
  65. TEST_ASSERT_VAL("wrong pid",
  66. perf_thread_map__pid(threads, 0) == getpid());
  67. TEST_ASSERT_VAL("wrong comm",
  68. perf_thread_map__comm(threads, 0) &&
  69. !strcmp(perf_thread_map__comm(threads, 0), NAME));
  70. TEST_ASSERT_VAL("wrong refcnt",
  71. refcount_read(&threads->refcnt) == 1);
  72. perf_thread_map__put(threads);
  73. return 0;
  74. }
  75. static int test__thread_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  76. {
  77. struct perf_thread_map *threads;
  78. TEST_ASSERT_VAL("failed to set process name",
  79. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  80. /* test map on current pid */
  81. threads = thread_map__new_by_pid(getpid());
  82. TEST_ASSERT_VAL("failed to alloc map", threads);
  83. thread_map__read_comms(threads);
  84. TEST_ASSERT_VAL("failed to synthesize map",
  85. !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
  86. perf_thread_map__put(threads);
  87. return 0;
  88. }
  89. static int test__thread_map_remove(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  90. {
  91. struct perf_thread_map *threads;
  92. char *str;
  93. TEST_ASSERT_VAL("failed to allocate map string",
  94. asprintf(&str, "%d,%d", getpid(), getppid()) >= 0);
  95. threads = thread_map__new_str(str, /*tid=*/NULL, /*all_threads=*/false);
  96. free(str);
  97. TEST_ASSERT_VAL("failed to allocate thread_map",
  98. threads);
  99. if (verbose > 0)
  100. thread_map__fprintf(threads, stderr);
  101. TEST_ASSERT_VAL("failed to remove thread",
  102. !thread_map__remove(threads, 0));
  103. TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1);
  104. if (verbose > 0)
  105. thread_map__fprintf(threads, stderr);
  106. TEST_ASSERT_VAL("failed to remove thread",
  107. !thread_map__remove(threads, 0));
  108. TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0);
  109. if (verbose > 0)
  110. thread_map__fprintf(threads, stderr);
  111. TEST_ASSERT_VAL("failed to not remove thread",
  112. thread_map__remove(threads, 0));
  113. perf_thread_map__put(threads);
  114. return 0;
  115. }
  116. DEFINE_SUITE("Thread map", thread_map);
  117. DEFINE_SUITE("Synthesize thread map", thread_map_synthesize);
  118. DEFINE_SUITE("Remove thread map", thread_map_remove);