test_mutex.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2017-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 is a stress test, expected to never terminate, of the
  19. * timed lock functionality provided by the mutex implementations. The
  20. * two conditions for success are :
  21. * - no assertion triggered
  22. * - all debugging system counters of the selected mutex implementation
  23. * must be non-zero after some time.
  24. *
  25. * The system counters are meant to perform simple code coverage, asserting
  26. * all the tricky code paths are taken at least once.
  27. */
  28. #include <assert.h>
  29. #include <stddef.h>
  30. #include <stdio.h>
  31. #include <kern/atomic.h>
  32. #include <kern/clock.h>
  33. #include <kern/error.h>
  34. #include <kern/init.h>
  35. #include <kern/kmem.h>
  36. #include <kern/log.h>
  37. #include <kern/mutex.h>
  38. #include <kern/panic.h>
  39. #include <kern/syscnt.h>
  40. #include <kern/thread.h>
  41. #include <kern/timer.h>
  42. #include <test/test.h>
  43. #define TEST_MIN_CPUS 3
  44. #define TEST_REPORT_INTERVAL 10000
  45. struct test {
  46. struct mutex mutex;
  47. unsigned int counter;
  48. };
  49. static struct timer test_timer;
  50. static void
  51. test_run(void *arg)
  52. {
  53. unsigned int prev, counter;
  54. struct test *test;
  55. int error;
  56. test = arg;
  57. for (counter = 1; /* no condition */; counter++) {
  58. if ((counter % 1024) == 0) {
  59. printf("%s ", thread_self()->name);
  60. }
  61. error = mutex_timedlock(&test->mutex, clock_get_time() + 1);
  62. if (error) {
  63. thread_delay(1, false);
  64. continue;
  65. }
  66. prev = atomic_fetch_add(&test->counter, 1, ATOMIC_SEQ_CST);
  67. if (prev != 0) {
  68. break;
  69. }
  70. if ((counter % 2) == 0) {
  71. cpu_delay(clock_ticks_to_ms(1) * 1000);
  72. } else {
  73. thread_delay(1, false);
  74. }
  75. prev = atomic_fetch_sub(&test->counter, 1, ATOMIC_SEQ_CST);
  76. if (prev != 1) {
  77. break;
  78. }
  79. mutex_unlock(&test->mutex);
  80. if ((counter % 2) == 0) {
  81. thread_delay(1, false);
  82. }
  83. }
  84. panic("test: invalid counter value (%u)", test->counter);
  85. }
  86. static struct test *
  87. test_create(unsigned int nr_threads)
  88. {
  89. char name[THREAD_NAME_SIZE];
  90. struct thread_attr attr;
  91. struct thread *thread;
  92. struct cpumap *cpumap;
  93. struct test *test;
  94. int error;
  95. assert(nr_threads);
  96. test = kmem_alloc(sizeof(*test));
  97. if (!test) {
  98. panic("test: unable to allocate memory");
  99. }
  100. mutex_init(&test->mutex);
  101. test->counter = 0;
  102. error = cpumap_create(&cpumap);
  103. error_check(error, "cpumap_create");
  104. for (size_t i = 0; i < nr_threads; i++) {
  105. cpumap_zero(cpumap);
  106. cpumap_set(cpumap, i % 3);
  107. snprintf(name, sizeof(name), THREAD_KERNEL_PREFIX "test_run:%u/%zu",
  108. nr_threads, i);
  109. thread_attr_init(&attr, name);
  110. thread_attr_set_detached(&attr);
  111. thread_attr_set_cpumap(&attr, cpumap);
  112. if (i < 2) {
  113. thread_attr_set_policy(&attr, THREAD_SCHED_POLICY_RR);
  114. thread_attr_set_priority(&attr, THREAD_SCHED_RT_PRIO_MIN + i);
  115. }
  116. error = thread_create(&thread, &attr, test_run, test);
  117. error_check(error, "thread_create");
  118. }
  119. return test;
  120. }
  121. static void
  122. test_report_syscnt(struct timer *timer)
  123. {
  124. uint64_t time;
  125. #ifdef CONFIG_MUTEX_PI
  126. syscnt_info("rtmutex", log_info);
  127. #else /* CONFIG_MUTEX_PI */
  128. syscnt_info("mutex", log_info);
  129. #endif /* CONFIG_MUTEX_PI */
  130. time = timer_get_time(timer) + clock_ticks_from_ms(TEST_REPORT_INTERVAL);
  131. timer_schedule(timer, time);
  132. }
  133. void __init
  134. test_setup(void)
  135. {
  136. uint64_t time;
  137. if (cpu_count() < TEST_MIN_CPUS) {
  138. panic("test: at least %u processors are required", TEST_MIN_CPUS);
  139. }
  140. test_create(1);
  141. test_create(2);
  142. test_create(3);
  143. test_create(10);
  144. timer_init(&test_timer, test_report_syscnt, TIMER_DETACHED);
  145. time = clock_get_time() + clock_ticks_from_ms(TEST_REPORT_INTERVAL);
  146. timer_schedule(&test_timer, time);
  147. }