thread-mg-share.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "tests.h"
  2. #include "machine.h"
  3. #include "thread.h"
  4. #include "map.h"
  5. #include "debug.h"
  6. int test__thread_mg_share(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 map_groups *mg;
  14. /* other process */
  15. struct thread *other, *other_leader;
  16. struct map_groups *other_mg;
  17. /*
  18. * This test create 2 processes abstractions (struct thread)
  19. * with several threads and checks they properly share and
  20. * maintain map groups info (struct map_groups).
  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. mg = leader->mg;
  37. TEST_ASSERT_EQUAL("wrong refcnt", atomic_read(&mg->refcnt), 4);
  38. /* test the map groups pointer is shared */
  39. TEST_ASSERT_VAL("map groups don't match", mg == t1->mg);
  40. TEST_ASSERT_VAL("map groups don't match", mg == t2->mg);
  41. TEST_ASSERT_VAL("map groups don't match", mg == t3->mg);
  42. /*
  43. * Verify the other leader was created by previous call.
  44. * It should have shared map groups 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_mg = other->mg;
  61. TEST_ASSERT_EQUAL("wrong refcnt", atomic_read(&other_mg->refcnt), 2);
  62. TEST_ASSERT_VAL("map groups don't match", other_mg == other_leader->mg);
  63. /* release thread group */
  64. thread__put(leader);
  65. TEST_ASSERT_EQUAL("wrong refcnt", atomic_read(&mg->refcnt), 3);
  66. thread__put(t1);
  67. TEST_ASSERT_EQUAL("wrong refcnt", atomic_read(&mg->refcnt), 2);
  68. thread__put(t2);
  69. TEST_ASSERT_EQUAL("wrong refcnt", atomic_read(&mg->refcnt), 1);
  70. thread__put(t3);
  71. /* release other group */
  72. thread__put(other_leader);
  73. TEST_ASSERT_EQUAL("wrong refcnt", atomic_read(&other_mg->refcnt), 1);
  74. thread__put(other);
  75. machines__exit(&machines);
  76. return 0;
  77. }