thread-map.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. struct perf_sample;
  15. struct perf_tool;
  16. struct machine;
  17. #define NAME (const char *) "perf"
  18. #define NAMEUL (unsigned long) NAME
  19. int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unused)
  20. {
  21. struct perf_thread_map *map;
  22. TEST_ASSERT_VAL("failed to set process name",
  23. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  24. /* test map on current pid */
  25. map = thread_map__new_by_pid(getpid());
  26. TEST_ASSERT_VAL("failed to alloc map", map);
  27. thread_map__read_comms(map);
  28. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  29. TEST_ASSERT_VAL("wrong pid",
  30. perf_thread_map__pid(map, 0) == getpid());
  31. TEST_ASSERT_VAL("wrong comm",
  32. perf_thread_map__comm(map, 0) &&
  33. !strcmp(perf_thread_map__comm(map, 0), NAME));
  34. TEST_ASSERT_VAL("wrong refcnt",
  35. refcount_read(&map->refcnt) == 1);
  36. perf_thread_map__put(map);
  37. /* test dummy pid */
  38. map = perf_thread_map__new_dummy();
  39. TEST_ASSERT_VAL("failed to alloc map", map);
  40. thread_map__read_comms(map);
  41. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  42. TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
  43. TEST_ASSERT_VAL("wrong comm",
  44. perf_thread_map__comm(map, 0) &&
  45. !strcmp(perf_thread_map__comm(map, 0), "dummy"));
  46. TEST_ASSERT_VAL("wrong refcnt",
  47. refcount_read(&map->refcnt) == 1);
  48. perf_thread_map__put(map);
  49. return 0;
  50. }
  51. static int process_event(struct perf_tool *tool __maybe_unused,
  52. union perf_event *event,
  53. struct perf_sample *sample __maybe_unused,
  54. struct machine *machine __maybe_unused)
  55. {
  56. struct perf_record_thread_map *map = &event->thread_map;
  57. struct perf_thread_map *threads;
  58. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  59. TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid());
  60. TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME));
  61. threads = thread_map__new_event(&event->thread_map);
  62. TEST_ASSERT_VAL("failed to alloc map", threads);
  63. TEST_ASSERT_VAL("wrong nr", threads->nr == 1);
  64. TEST_ASSERT_VAL("wrong pid",
  65. perf_thread_map__pid(threads, 0) == getpid());
  66. TEST_ASSERT_VAL("wrong comm",
  67. perf_thread_map__comm(threads, 0) &&
  68. !strcmp(perf_thread_map__comm(threads, 0), NAME));
  69. TEST_ASSERT_VAL("wrong refcnt",
  70. refcount_read(&threads->refcnt) == 1);
  71. perf_thread_map__put(threads);
  72. return 0;
  73. }
  74. int test__thread_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
  75. {
  76. struct perf_thread_map *threads;
  77. TEST_ASSERT_VAL("failed to set process name",
  78. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  79. /* test map on current pid */
  80. threads = thread_map__new_by_pid(getpid());
  81. TEST_ASSERT_VAL("failed to alloc map", threads);
  82. thread_map__read_comms(threads);
  83. TEST_ASSERT_VAL("failed to synthesize map",
  84. !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
  85. return 0;
  86. }
  87. int test__thread_map_remove(struct test *test __maybe_unused, int subtest __maybe_unused)
  88. {
  89. struct perf_thread_map *threads;
  90. char *str;
  91. int i;
  92. TEST_ASSERT_VAL("failed to allocate map string",
  93. asprintf(&str, "%d,%d", getpid(), getppid()) >= 0);
  94. threads = thread_map__new_str(str, NULL, 0, false);
  95. TEST_ASSERT_VAL("failed to allocate thread_map",
  96. threads);
  97. if (verbose > 0)
  98. thread_map__fprintf(threads, stderr);
  99. TEST_ASSERT_VAL("failed to remove thread",
  100. !thread_map__remove(threads, 0));
  101. TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1);
  102. if (verbose > 0)
  103. thread_map__fprintf(threads, stderr);
  104. TEST_ASSERT_VAL("failed to remove thread",
  105. !thread_map__remove(threads, 0));
  106. TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0);
  107. if (verbose > 0)
  108. thread_map__fprintf(threads, stderr);
  109. TEST_ASSERT_VAL("failed to not remove thread",
  110. thread_map__remove(threads, 0));
  111. for (i = 0; i < threads->nr; i++)
  112. zfree(&threads->map[i].comm);
  113. free(threads);
  114. return 0;
  115. }