sync_stress_consumer.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * sync stress test: producer/consumer
  3. * Copyright 2015-2016 Collabora Ltd.
  4. *
  5. * Based on the implementation from the Android Open Source Project,
  6. *
  7. * Copyright 2012 Google, Inc
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <pthread.h>
  28. #include "sync.h"
  29. #include "sw_sync.h"
  30. #include "synctest.h"
  31. /* IMPORTANT NOTE: if you see this test failing on your system, it may be
  32. * due to a shortage of file descriptors. Please ensure your system has
  33. * a sensible limit for this test to finish correctly.
  34. */
  35. /* Returns 1 on error, 0 on success */
  36. static int busy_wait_on_fence(int fence)
  37. {
  38. int error, active;
  39. do {
  40. error = sync_fence_count_with_status(fence, FENCE_STATUS_ERROR);
  41. ASSERT(error == 0, "Error occurred on fence\n");
  42. active = sync_fence_count_with_status(fence,
  43. FENCE_STATUS_ACTIVE);
  44. } while (active);
  45. return 0;
  46. }
  47. static struct {
  48. int iterations;
  49. int threads;
  50. int counter;
  51. int consumer_timeline;
  52. int *producer_timelines;
  53. pthread_mutex_t lock;
  54. } test_data_mpsc;
  55. static int mpsc_producer_thread(void *d)
  56. {
  57. int id = (long)d;
  58. int fence, valid, i;
  59. int *producer_timelines = test_data_mpsc.producer_timelines;
  60. int consumer_timeline = test_data_mpsc.consumer_timeline;
  61. int iterations = test_data_mpsc.iterations;
  62. for (i = 0; i < iterations; i++) {
  63. fence = sw_sync_fence_create(consumer_timeline, "fence", i);
  64. valid = sw_sync_fence_is_valid(fence);
  65. ASSERT(valid, "Failure creating fence\n");
  66. /*
  67. * Wait for the consumer to finish. Use alternate
  68. * means of waiting on the fence
  69. */
  70. if ((iterations + id) % 8 != 0) {
  71. ASSERT(sync_wait(fence, -1) > 0,
  72. "Failure waiting on fence\n");
  73. } else {
  74. ASSERT(busy_wait_on_fence(fence) == 0,
  75. "Failure waiting on fence\n");
  76. }
  77. /*
  78. * Every producer increments the counter, the consumer
  79. * checks and erases it
  80. */
  81. pthread_mutex_lock(&test_data_mpsc.lock);
  82. test_data_mpsc.counter++;
  83. pthread_mutex_unlock(&test_data_mpsc.lock);
  84. ASSERT(sw_sync_timeline_inc(producer_timelines[id], 1) == 0,
  85. "Error advancing producer timeline\n");
  86. sw_sync_fence_destroy(fence);
  87. }
  88. return 0;
  89. }
  90. static int mpcs_consumer_thread(void)
  91. {
  92. int fence, merged, tmp, valid, it, i;
  93. int *producer_timelines = test_data_mpsc.producer_timelines;
  94. int consumer_timeline = test_data_mpsc.consumer_timeline;
  95. int iterations = test_data_mpsc.iterations;
  96. int n = test_data_mpsc.threads;
  97. for (it = 1; it <= iterations; it++) {
  98. fence = sw_sync_fence_create(producer_timelines[0], "name", it);
  99. for (i = 1; i < n; i++) {
  100. tmp = sw_sync_fence_create(producer_timelines[i],
  101. "name", it);
  102. merged = sync_merge("name", tmp, fence);
  103. sw_sync_fence_destroy(tmp);
  104. sw_sync_fence_destroy(fence);
  105. fence = merged;
  106. }
  107. valid = sw_sync_fence_is_valid(fence);
  108. ASSERT(valid, "Failure merging fences\n");
  109. /*
  110. * Make sure we see an increment from every producer thread.
  111. * Vary the means by which we wait.
  112. */
  113. if (iterations % 8 != 0) {
  114. ASSERT(sync_wait(fence, -1) > 0,
  115. "Producers did not increment as expected\n");
  116. } else {
  117. ASSERT(busy_wait_on_fence(fence) == 0,
  118. "Producers did not increment as expected\n");
  119. }
  120. ASSERT(test_data_mpsc.counter == n * it,
  121. "Counter value mismatch!\n");
  122. /* Release the producer threads */
  123. ASSERT(sw_sync_timeline_inc(consumer_timeline, 1) == 0,
  124. "Failure releasing producer threads\n");
  125. sw_sync_fence_destroy(fence);
  126. }
  127. return 0;
  128. }
  129. int test_consumer_stress_multi_producer_single_consumer(void)
  130. {
  131. int iterations = 1 << 12;
  132. int n = 5;
  133. long i, ret;
  134. int producer_timelines[n];
  135. int consumer_timeline;
  136. pthread_t threads[n];
  137. consumer_timeline = sw_sync_timeline_create();
  138. for (i = 0; i < n; i++)
  139. producer_timelines[i] = sw_sync_timeline_create();
  140. test_data_mpsc.producer_timelines = producer_timelines;
  141. test_data_mpsc.consumer_timeline = consumer_timeline;
  142. test_data_mpsc.iterations = iterations;
  143. test_data_mpsc.threads = n;
  144. test_data_mpsc.counter = 0;
  145. pthread_mutex_init(&test_data_mpsc.lock, NULL);
  146. for (i = 0; i < n; i++) {
  147. pthread_create(&threads[i], NULL, (void * (*)(void *))
  148. mpsc_producer_thread, (void *)i);
  149. }
  150. /* Consumer thread runs here */
  151. ret = mpcs_consumer_thread();
  152. for (i = 0; i < n; i++)
  153. pthread_join(threads[i], NULL);
  154. return ret;
  155. }