thread-map.c 3.7 KB

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