thread-maps-share.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "tests.h"
  3. #include "machine.h"
  4. #include "thread.h"
  5. #include "debug.h"
  6. static int test__thread_maps_share(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  7. {
  8. struct machines machines;
  9. struct machine *machine;
  10. /* thread group */
  11. struct thread *leader;
  12. struct thread *t1, *t2, *t3;
  13. struct maps *maps;
  14. /* other process */
  15. struct thread *other, *other_leader;
  16. struct maps *other_maps;
  17. /*
  18. * This test create 2 processes abstractions (struct thread)
  19. * with several threads and checks they properly share and
  20. * maintain maps info (struct maps).
  21. *
  22. * thread group (pid: 0, tids: 0, 1, 2, 3)
  23. * other group (pid: 4, tids: 4, 5)
  24. */
  25. machines__init(&machines);
  26. machine = &machines.host;
  27. /* create process with 4 threads */
  28. leader = machine__findnew_thread(machine, 0, 0);
  29. t1 = machine__findnew_thread(machine, 0, 1);
  30. t2 = machine__findnew_thread(machine, 0, 2);
  31. t3 = machine__findnew_thread(machine, 0, 3);
  32. /* and create 1 separated process, without thread leader */
  33. other = machine__findnew_thread(machine, 4, 5);
  34. TEST_ASSERT_VAL("failed to create threads",
  35. leader && t1 && t2 && t3 && other);
  36. maps = thread__maps(leader);
  37. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 4);
  38. /* test the maps pointer is shared */
  39. TEST_ASSERT_VAL("maps don't match", maps__equal(maps, thread__maps(t1)));
  40. TEST_ASSERT_VAL("maps don't match", maps__equal(maps, thread__maps(t2)));
  41. TEST_ASSERT_VAL("maps don't match", maps__equal(maps, thread__maps(t3)));
  42. /*
  43. * Verify the other leader was created by previous call.
  44. * It should have shared maps with no change in
  45. * refcnt.
  46. */
  47. other_leader = machine__find_thread(machine, 4, 4);
  48. TEST_ASSERT_VAL("failed to find other leader", other_leader);
  49. /*
  50. * Ok, now that all the rbtree related operations were done,
  51. * lets remove all of them from there so that we can do the
  52. * refcounting tests.
  53. */
  54. machine__remove_thread(machine, leader);
  55. machine__remove_thread(machine, t1);
  56. machine__remove_thread(machine, t2);
  57. machine__remove_thread(machine, t3);
  58. machine__remove_thread(machine, other);
  59. machine__remove_thread(machine, other_leader);
  60. other_maps = thread__maps(other);
  61. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(other_maps)), 2);
  62. TEST_ASSERT_VAL("maps don't match", maps__equal(other_maps, thread__maps(other_leader)));
  63. /* release thread group */
  64. thread__put(t3);
  65. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 3);
  66. thread__put(t2);
  67. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 2);
  68. thread__put(t1);
  69. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 1);
  70. thread__put(leader);
  71. /* release other group */
  72. thread__put(other_leader);
  73. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(other_maps)), 1);
  74. thread__put(other);
  75. machines__exit(&machines);
  76. return 0;
  77. }
  78. DEFINE_SUITE("Share thread maps", thread_maps_share);