thread-map.c 2.4 KB

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