2
0

testq.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2018 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "libks/ks.h"
  23. #include "tap.h"
  24. #define MAX 200
  25. static void *test1_thread(ks_thread_t *thread, void *data)
  26. {
  27. ks_q_t *q = (ks_q_t *) data;
  28. void *pop;
  29. while(ks_q_pop(q, &pop) == KS_STATUS_SUCCESS) {
  30. //int *i = (int *)pop;
  31. //printf("POP %d\n", *i);
  32. ks_pool_free(&pop);
  33. }
  34. return NULL;
  35. }
  36. static void do_flush(ks_q_t *q, void *ptr, void *flush_data)
  37. {
  38. //ks_pool_t *pool = (ks_pool_t *)flush_data;
  39. ks_pool_free(&ptr);
  40. }
  41. int qtest1(int loops)
  42. {
  43. ks_thread_t *thread;
  44. ks_q_t *q;
  45. ks_pool_t *pool;
  46. int i;
  47. int r = 1;
  48. void *pop;
  49. ks_pool_open(&pool);
  50. ks_q_create(&q, pool, loops);
  51. ks_thread_create(&thread, test1_thread, q, pool);
  52. if (ks_q_pop_timeout(q, &pop, 500) != KS_STATUS_TIMEOUT) {
  53. r = 0;
  54. goto end;
  55. }
  56. for (i = 0; i < 10000; i++) {
  57. int *val = (int *)ks_pool_alloc(pool, sizeof(int));
  58. *val = i;
  59. ks_q_push(q, val);
  60. }
  61. ks_q_wait(q);
  62. ks_q_term(q);
  63. ks_thread_join(thread);
  64. ks_q_destroy(&q);
  65. ks_q_create(&q, pool, loops);
  66. ks_q_set_flush_fn(q, do_flush, pool);
  67. for (i = 0; i < loops; i++) {
  68. int *val = (int *)ks_pool_alloc(pool, sizeof(int));
  69. *val = i;
  70. ks_q_push(q, val);
  71. }
  72. end:
  73. ks_q_destroy(&q);
  74. ks_pool_close(&pool);
  75. return r;
  76. }
  77. struct test2_data {
  78. ks_q_t *q;
  79. int try;
  80. int ready;
  81. int running;
  82. };
  83. static void *test2_thread(ks_thread_t *thread, void *data)
  84. {
  85. struct test2_data *t2 = (struct test2_data *) data;
  86. void *pop;
  87. ks_status_t status;
  88. int popped = 0;
  89. while (t2->running && (t2->try && !t2->ready)) {
  90. ks_sleep(10000);
  91. continue;
  92. }
  93. while (t2->running || ks_q_size(t2->q)) {
  94. if (t2->try) {
  95. status = ks_q_trypop(t2->q, &pop);
  96. } else {
  97. status = ks_q_pop(t2->q, &pop);
  98. }
  99. if (status == KS_STATUS_SUCCESS) {
  100. //int *i = (int *)pop;
  101. //printf("%p POP %d\n", (void *)pthread_self(), *i);
  102. popped++;
  103. ks_pool_free(&pop);
  104. } else if (status == KS_STATUS_INACTIVE) {
  105. break;
  106. } else if (t2->try && ks_q_size(t2->q)) {
  107. int s = rand() % 100;
  108. ks_sleep(s * 1000);
  109. }
  110. }
  111. return (void *) (intptr_t)popped;
  112. }
  113. ks_size_t qtest2(int ttl, int try, int loops)
  114. {
  115. ks_thread_t *threads[MAX];
  116. ks_q_t *q;
  117. ks_pool_t *pool;
  118. int i;
  119. struct test2_data t2 = { 0 };
  120. ks_size_t r;
  121. int dropped = 0;
  122. int qlen = loops / 2;
  123. int total_popped = 0;
  124. ks_pool_open(&pool);
  125. ks_q_create(&q, pool, qlen);
  126. t2.q = q;
  127. t2.try = try;
  128. t2.running = 1;
  129. for (i = 0; i < ttl; i++) {
  130. ks_thread_create(&threads[i], test2_thread, &t2, pool);
  131. }
  132. //ks_sleep(loops00);
  133. for (i = 0; i < loops; i++) {
  134. int *val = (int *)ks_pool_alloc(pool, sizeof(int));
  135. *val = i;
  136. if (try > 1) {
  137. if (ks_q_trypush(q, val) != KS_STATUS_SUCCESS) {
  138. dropped++;
  139. }
  140. } else {
  141. ks_q_push(q, val);
  142. }
  143. if (i > qlen / 2) {
  144. t2.ready = 1;
  145. }
  146. }
  147. t2.running = 0;
  148. if (!try) {
  149. ks_q_wait(q);
  150. ks_q_term(q);
  151. }
  152. for (i = 0; i < ttl; i++) {
  153. int popped;
  154. ks_thread_join(threads[i]);
  155. popped = (int)(intptr_t)ks_thread_get_return_data(threads[i]);
  156. if (popped) {
  157. printf("%d/%d POPPED %d\n", i, ttl, popped);
  158. }
  159. total_popped += popped;
  160. }
  161. r = ks_q_size(q);
  162. ks_assert(r == 0);
  163. ks_q_destroy(&q);
  164. printf("TOTAL POPPED: %d DROPPED %d SUM: %d\n", total_popped, dropped, total_popped + dropped);fflush(stdout);
  165. if (try < 2) {
  166. ks_assert(total_popped == loops);
  167. } else {
  168. ks_assert(total_popped + dropped == loops);
  169. }
  170. ks_pool_close(&pool);
  171. return r;
  172. }
  173. int main(int argc, char **argv)
  174. {
  175. int ttl;
  176. int size = 100000;
  177. int runs = 1;
  178. int i;
  179. ks_init();
  180. plan(4 * runs);
  181. ttl = ks_env_cpu_count() * 5;
  182. //ttl = 5;
  183. for(i = 0; i < runs; i++) {
  184. ok(qtest1(size));
  185. ok(qtest2(ttl, 0, size) == 0);
  186. ok(qtest2(ttl, 1, size) == 0);
  187. ok(qtest2(ttl, 2, size) == 0);
  188. }
  189. printf("TTL %d RUNS %d\n", ttl, runs);
  190. ks_shutdown();
  191. done_testing();
  192. }