test_sref_noref.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2014-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This test module checks that the no-reference function of a scalable
  19. * reference counter is actually called when the number of references drops
  20. * to 0. An initial master thread creates a bunch of slave threads, more
  21. * than the number of processors to enforce migrations. These slaves wait
  22. * for the master to allocate a page for a test object with a scalable
  23. * reference counter. Once they receive the page, they manipulate the
  24. * counter until the master thread tells them to stop. The master thread
  25. * also manipulates the counter for a fixed number of iterations before
  26. * stopping the slaves. The master thread then joins all slaves to make
  27. * sure all of them have released their reference on the test object.
  28. * Finally, it releases the initial reference, at which point, the
  29. * no-reference function should be called.
  30. *
  31. * Notes: the number of loops must be large enough to allow many epochs
  32. * to occur.
  33. */
  34. #include <errno.h>
  35. #include <stddef.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <kern/condition.h>
  39. #include <kern/init.h>
  40. #include <kern/kmem.h>
  41. #include <kern/log.h>
  42. #include <kern/macros.h>
  43. #include <kern/mutex.h>
  44. #include <kern/panic.h>
  45. #include <kern/sref.h>
  46. #include <kern/syscnt.h>
  47. #include <kern/thread.h>
  48. #include <test/test.h>
  49. #include <vm/kmem.h>
  50. #define TEST_NR_LOOPS (10UL * 1000 * 1000)
  51. struct test_obj
  52. {
  53. struct sref_counter ref_counter;
  54. };
  55. static struct condition test_condition;
  56. static struct mutex test_lock;
  57. static struct test_obj *test_obj;
  58. static volatile int test_stop;
  59. static void
  60. test_manipulate_counter (struct test_obj *obj)
  61. {
  62. sref_counter_inc (&obj->ref_counter);
  63. thread_yield ();
  64. sref_counter_dec (&obj->ref_counter);
  65. thread_yield ();
  66. }
  67. static void
  68. test_ref (void *arg __unused)
  69. {
  70. mutex_lock (&test_lock);
  71. printf ("waiting for page\n");
  72. while (! test_obj)
  73. condition_wait (&test_condition, &test_lock);
  74. struct test_obj *obj = test_obj;
  75. mutex_unlock (&test_lock);
  76. printf ("page received, manipulate reference counter\n");
  77. while (! test_stop)
  78. test_manipulate_counter (obj);
  79. printf ("thread exiting\n");
  80. }
  81. static void
  82. test_obj_noref (struct sref_counter *counter)
  83. {
  84. struct test_obj *obj = structof (counter, struct test_obj, ref_counter);
  85. vm_kmem_free (obj, sizeof (*obj));
  86. printf ("0 references, page released\n");
  87. syscnt_info ("sref_epoch", log_stream_info ());
  88. syscnt_info ("sref_dirty_zero", log_stream_info ());
  89. syscnt_info ("sref_true_zero", log_stream_info ());
  90. }
  91. TEST_DEFERRED (sref_noref)
  92. {
  93. int error;
  94. uint32_t nr_threads = cpu_count () + 1;
  95. struct thread **threads = kmem_alloc (sizeof (*threads) * nr_threads);
  96. if (! threads)
  97. panic ("kmem_alloc: %s", strerror (ENOMEM));
  98. for (uint32_t i = 0; i < nr_threads; i++)
  99. {
  100. char name[THREAD_NAME_SIZE];
  101. snprintf (name, sizeof (name), THREAD_KERNEL_PREFIX "test_sref_ref/%u", i);
  102. struct thread_attr attr;
  103. thread_attr_init (&attr, name);
  104. error = thread_create (&threads[i], &attr, test_ref, NULL);
  105. test_assert_zero (error);
  106. }
  107. printf ("allocating page\n");
  108. struct test_obj *obj = vm_kmem_alloc (sizeof (*obj));
  109. if (! obj)
  110. panic ("vm_kmem_alloc: %s", strerror (ENOMEM));
  111. sref_counter_init (&obj->ref_counter, 1, NULL, test_obj_noref);
  112. printf ("page allocated, 1 reference, publishing\n");
  113. mutex_lock (&test_lock);
  114. test_obj = obj;
  115. condition_broadcast (&test_condition);
  116. mutex_unlock (&test_lock);
  117. for (volatile unsigned long loop = 0; loop < TEST_NR_LOOPS; loop++)
  118. test_manipulate_counter (obj);
  119. printf ("stopping test, wait for threads\n");
  120. test_stop = 1;
  121. for (uint32_t i = 0; i < nr_threads; i++)
  122. thread_join (threads[i]);
  123. printf ("releasing initial reference\n");
  124. sref_counter_dec (&obj->ref_counter);
  125. kmem_free (threads, sizeof (*threads) * nr_threads);
  126. return (TEST_OK);
  127. }