thread-maps-share.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "tests.h"
  3. #include "machine.h"
  4. #include "thread.h"
  5. #include "debug.h"
  6. int test__thread_maps_share(struct test *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 = leader->maps;
  37. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 4);
  38. /* test the maps pointer is shared */
  39. TEST_ASSERT_VAL("maps don't match", maps == t1->maps);
  40. TEST_ASSERT_VAL("maps don't match", maps == t2->maps);
  41. TEST_ASSERT_VAL("maps don't match", maps == t3->maps);
  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 = other->maps;
  61. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_maps->refcnt), 2);
  62. TEST_ASSERT_VAL("maps don't match", other_maps == other_leader->maps);
  63. /* release thread group */
  64. thread__put(leader);
  65. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 3);
  66. thread__put(t1);
  67. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 2);
  68. thread__put(t2);
  69. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 1);
  70. thread__put(t3);
  71. /* release other group */
  72. thread__put(other_leader);
  73. TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_maps->refcnt), 1);
  74. thread__put(other);
  75. machines__exit(&machines);
  76. return 0;
  77. }