thread-map.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <sys/prctl.h>
  4. #include "tests.h"
  5. #include "thread_map.h"
  6. #include "debug.h"
  7. #define NAME (const char *) "perf"
  8. #define NAMEUL (unsigned long) NAME
  9. int test__thread_map(int subtest __maybe_unused)
  10. {
  11. struct thread_map *map;
  12. TEST_ASSERT_VAL("failed to set process name",
  13. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  14. /* test map on current pid */
  15. map = thread_map__new_by_pid(getpid());
  16. TEST_ASSERT_VAL("failed to alloc map", map);
  17. thread_map__read_comms(map);
  18. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  19. TEST_ASSERT_VAL("wrong pid",
  20. thread_map__pid(map, 0) == getpid());
  21. TEST_ASSERT_VAL("wrong comm",
  22. thread_map__comm(map, 0) &&
  23. !strcmp(thread_map__comm(map, 0), NAME));
  24. TEST_ASSERT_VAL("wrong refcnt",
  25. atomic_read(&map->refcnt) == 1);
  26. thread_map__put(map);
  27. /* test dummy pid */
  28. map = thread_map__new_dummy();
  29. TEST_ASSERT_VAL("failed to alloc map", map);
  30. thread_map__read_comms(map);
  31. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  32. TEST_ASSERT_VAL("wrong pid", thread_map__pid(map, 0) == -1);
  33. TEST_ASSERT_VAL("wrong comm",
  34. thread_map__comm(map, 0) &&
  35. !strcmp(thread_map__comm(map, 0), "dummy"));
  36. TEST_ASSERT_VAL("wrong refcnt",
  37. atomic_read(&map->refcnt) == 1);
  38. thread_map__put(map);
  39. return 0;
  40. }
  41. static int process_event(struct perf_tool *tool __maybe_unused,
  42. union perf_event *event,
  43. struct perf_sample *sample __maybe_unused,
  44. struct machine *machine __maybe_unused)
  45. {
  46. struct thread_map_event *map = &event->thread_map;
  47. struct thread_map *threads;
  48. TEST_ASSERT_VAL("wrong nr", map->nr == 1);
  49. TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid());
  50. TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME));
  51. threads = thread_map__new_event(&event->thread_map);
  52. TEST_ASSERT_VAL("failed to alloc map", threads);
  53. TEST_ASSERT_VAL("wrong nr", threads->nr == 1);
  54. TEST_ASSERT_VAL("wrong pid",
  55. thread_map__pid(threads, 0) == getpid());
  56. TEST_ASSERT_VAL("wrong comm",
  57. thread_map__comm(threads, 0) &&
  58. !strcmp(thread_map__comm(threads, 0), NAME));
  59. TEST_ASSERT_VAL("wrong refcnt",
  60. atomic_read(&threads->refcnt) == 1);
  61. thread_map__put(threads);
  62. return 0;
  63. }
  64. int test__thread_map_synthesize(int subtest __maybe_unused)
  65. {
  66. struct thread_map *threads;
  67. TEST_ASSERT_VAL("failed to set process name",
  68. !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
  69. /* test map on current pid */
  70. threads = thread_map__new_by_pid(getpid());
  71. TEST_ASSERT_VAL("failed to alloc map", threads);
  72. thread_map__read_comms(threads);
  73. TEST_ASSERT_VAL("failed to synthesize map",
  74. !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
  75. return 0;
  76. }