test_sref_noref.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/error.h>
  41. #include <kern/kmem.h>
  42. #include <kern/log.h>
  43. #include <kern/macros.h>
  44. #include <kern/mutex.h>
  45. #include <kern/panic.h>
  46. #include <kern/sref.h>
  47. #include <kern/syscnt.h>
  48. #include <kern/thread.h>
  49. #include <test/test.h>
  50. #include <vm/kmem.h>
  51. #define TEST_NR_LOOPS (10UL * 1000 * 1000)
  52. struct test_obj
  53. {
  54. struct sref_counter ref_counter;
  55. };
  56. static struct condition test_condition;
  57. static struct mutex test_lock;
  58. static struct test_obj *test_obj;
  59. static volatile int test_stop;
  60. static void
  61. test_manipulate_counter (struct test_obj *obj)
  62. {
  63. sref_counter_inc (&obj->ref_counter);
  64. thread_yield ();
  65. sref_counter_dec (&obj->ref_counter);
  66. thread_yield ();
  67. }
  68. static void
  69. test_ref (void *arg __unused)
  70. {
  71. mutex_lock (&test_lock);
  72. printf ("waiting for page\n");
  73. while (! test_obj)
  74. condition_wait (&test_condition, &test_lock);
  75. struct test_obj *obj = test_obj;
  76. mutex_unlock (&test_lock);
  77. printf ("page received, manipulate reference counter\n");
  78. while (! test_stop)
  79. test_manipulate_counter (obj);
  80. printf ("thread exiting\n");
  81. }
  82. static void
  83. test_obj_noref (struct sref_counter *counter)
  84. {
  85. struct test_obj *obj = structof (counter, struct test_obj, ref_counter);
  86. vm_kmem_free (obj, sizeof (*obj));
  87. printf ("0 references, page released\n");
  88. syscnt_info ("sref_epoch", log_stream_info ());
  89. syscnt_info ("sref_dirty_zero", log_stream_info ());
  90. syscnt_info ("sref_true_zero", log_stream_info ());
  91. }
  92. TEST_DEFERRED (sref_noref)
  93. {
  94. int error;
  95. uint32_t nr_threads = cpu_count () + 1;
  96. struct thread **threads = kmem_alloc (sizeof (*threads) * nr_threads);
  97. if (! threads)
  98. panic ("kmem_alloc: %s", strerror (ENOMEM));
  99. for (uint32_t i = 0; i < nr_threads; i++)
  100. {
  101. char name[THREAD_NAME_SIZE];
  102. snprintf (name, sizeof (name), THREAD_KERNEL_PREFIX "test_sref_ref/%u", i);
  103. struct thread_attr attr;
  104. thread_attr_init (&attr, name);
  105. error = thread_create (&threads[i], &attr, test_ref, NULL);
  106. error_check (error, "thread_create");
  107. }
  108. printf ("allocating page\n");
  109. struct test_obj *obj = vm_kmem_alloc (sizeof (*obj));
  110. if (! obj)
  111. panic ("vm_kmem_alloc: %s", strerror (ENOMEM));
  112. sref_counter_init (&obj->ref_counter, 1, NULL, test_obj_noref);
  113. printf ("page allocated, 1 reference, publishing\n");
  114. mutex_lock (&test_lock);
  115. test_obj = obj;
  116. condition_broadcast (&test_condition);
  117. mutex_unlock (&test_lock);
  118. for (volatile unsigned long loop = 0; loop < TEST_NR_LOOPS; loop++)
  119. test_manipulate_counter (obj);
  120. printf ("stopping test, wait for threads\n");
  121. test_stop = 1;
  122. for (uint32_t i = 0; i < nr_threads; i++)
  123. thread_join (threads[i]);
  124. printf ("releasing initial reference\n");
  125. sref_counter_dec (&obj->ref_counter);
  126. kmem_free (threads, sizeof (*threads) * nr_threads);
  127. return (TEST_OK);
  128. }