test_sref_noref.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/vm_kmem.h>
  51. #define TEST_NR_LOOPS (10UL * 1000 * 1000)
  52. struct test_obj {
  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)
  69. {
  70. struct test_obj *obj;
  71. (void)arg;
  72. mutex_lock(&test_lock);
  73. printf("waiting for page\n");
  74. while (test_obj == NULL) {
  75. condition_wait(&test_condition, &test_lock);
  76. }
  77. obj = test_obj;
  78. mutex_unlock(&test_lock);
  79. printf("page received, manipulate reference counter\n");
  80. while (!test_stop) {
  81. test_manipulate_counter(obj);
  82. }
  83. printf("thread exiting\n");
  84. }
  85. static void
  86. test_obj_noref(struct sref_counter *counter)
  87. {
  88. struct test_obj *obj;
  89. obj = structof(counter, struct test_obj, ref_counter);
  90. vm_kmem_free(obj, sizeof(*obj));
  91. printf("0 references, page released\n");
  92. syscnt_info("sref_epoch", log_info);
  93. syscnt_info("sref_dirty_zero", log_info);
  94. syscnt_info("sref_true_zero", log_info);
  95. }
  96. static void
  97. test_run(void *arg)
  98. {
  99. char name[THREAD_NAME_SIZE];
  100. struct thread_attr attr;
  101. struct thread **threads;
  102. struct test_obj *obj;
  103. volatile unsigned long loop;
  104. unsigned int i, nr_threads;
  105. int error;
  106. (void)arg;
  107. nr_threads = cpu_count() + 1;
  108. threads = kmem_alloc(sizeof(*threads) * nr_threads);
  109. if (threads == NULL) {
  110. panic("kmem_alloc: %s", strerror(ENOMEM));
  111. }
  112. for (i = 0; i < nr_threads; i++) {
  113. snprintf(name, sizeof(name), THREAD_KERNEL_PREFIX "test_ref/%u", i);
  114. thread_attr_init(&attr, name);
  115. error = thread_create(&threads[i], &attr, test_ref, NULL);
  116. error_check(error, "thread_create");
  117. }
  118. printf("allocating page\n");
  119. obj = vm_kmem_alloc(sizeof(*obj));
  120. if (obj == NULL) {
  121. panic("vm_kmem_alloc: %s", strerror(ENOMEM));
  122. }
  123. sref_counter_init(&obj->ref_counter, 1, NULL, test_obj_noref);
  124. printf("page allocated, 1 reference, publishing\n");
  125. mutex_lock(&test_lock);
  126. test_obj = obj;
  127. condition_broadcast(&test_condition);
  128. mutex_unlock(&test_lock);
  129. for (loop = 0; loop < TEST_NR_LOOPS; loop++) {
  130. test_manipulate_counter(obj);
  131. }
  132. printf("stopping test, wait for threads\n");
  133. test_stop = 1;
  134. for (i = 0; i < nr_threads; i++) {
  135. thread_join(threads[i]);
  136. }
  137. printf("releasing initial reference\n");
  138. sref_counter_dec(&obj->ref_counter);
  139. kmem_free(threads, sizeof(*threads) * nr_threads);
  140. }
  141. void __init
  142. test_setup(void)
  143. {
  144. struct thread_attr attr;
  145. struct thread *thread;
  146. int error;
  147. condition_init(&test_condition);
  148. mutex_init(&test_lock);
  149. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run");
  150. thread_attr_set_detached(&attr);
  151. error = thread_create(&thread, &attr, test_run, NULL);
  152. error_check(error, "thread_create");
  153. }