thread-mg-share.c 2.8 KB

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