testlockperf.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_thread_proc.h"
  17. #include "fspr_thread_mutex.h"
  18. #include "fspr_thread_rwlock.h"
  19. #include "fspr_file_io.h"
  20. #include "fspr_errno.h"
  21. #include "fspr_general.h"
  22. #include "fspr_getopt.h"
  23. #include "errno.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include "testutil.h"
  27. #if !APR_HAS_THREADS
  28. int main(void)
  29. {
  30. printf("This program won't work on this platform because there is no "
  31. "support for threads.\n");
  32. return 0;
  33. }
  34. #else /* !APR_HAS_THREADS */
  35. #define MAX_COUNTER 1000000
  36. #define MAX_THREADS 6
  37. static long mutex_counter;
  38. static fspr_thread_mutex_t *thread_lock;
  39. void * APR_THREAD_FUNC thread_mutex_func(fspr_thread_t *thd, void *data);
  40. fspr_status_t test_thread_mutex(int num_threads); /* fspr_thread_mutex_t */
  41. static fspr_thread_rwlock_t *thread_rwlock;
  42. void * APR_THREAD_FUNC thread_rwlock_func(fspr_thread_t *thd, void *data);
  43. fspr_status_t test_thread_rwlock(int num_threads); /* fspr_thread_rwlock_t */
  44. int test_thread_mutex_nested(int num_threads);
  45. fspr_pool_t *pool;
  46. int i = 0, x = 0;
  47. void * APR_THREAD_FUNC thread_mutex_func(fspr_thread_t *thd, void *data)
  48. {
  49. int i;
  50. for (i = 0; i < MAX_COUNTER; i++) {
  51. fspr_thread_mutex_lock(thread_lock);
  52. mutex_counter++;
  53. fspr_thread_mutex_unlock(thread_lock);
  54. }
  55. return NULL;
  56. }
  57. void * APR_THREAD_FUNC thread_rwlock_func(fspr_thread_t *thd, void *data)
  58. {
  59. int i;
  60. for (i = 0; i < MAX_COUNTER; i++) {
  61. fspr_thread_rwlock_wrlock(thread_rwlock);
  62. mutex_counter++;
  63. fspr_thread_rwlock_unlock(thread_rwlock);
  64. }
  65. return NULL;
  66. }
  67. int test_thread_mutex(int num_threads)
  68. {
  69. fspr_thread_t *t[MAX_THREADS];
  70. fspr_status_t s[MAX_THREADS];
  71. fspr_time_t time_start, time_stop;
  72. int i;
  73. mutex_counter = 0;
  74. printf("fspr_thread_mutex_t Tests\n");
  75. printf("%-60s", " Initializing the fspr_thread_mutex_t (UNNESTED)");
  76. s[0] = fspr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_UNNESTED, pool);
  77. if (s[0] != APR_SUCCESS) {
  78. printf("Failed!\n");
  79. return s[0];
  80. }
  81. printf("OK\n");
  82. fspr_thread_mutex_lock(thread_lock);
  83. /* set_concurrency(4)? -aaron */
  84. printf(" Starting %d threads ", num_threads);
  85. for (i = 0; i < num_threads; ++i) {
  86. s[i] = fspr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
  87. if (s[i] != APR_SUCCESS) {
  88. printf("Failed!\n");
  89. return s[i];
  90. }
  91. }
  92. printf("OK\n");
  93. time_start = fspr_time_now();
  94. fspr_thread_mutex_unlock(thread_lock);
  95. /* printf("%-60s", " Waiting for threads to exit"); */
  96. for (i = 0; i < num_threads; ++i) {
  97. fspr_thread_join(&s[i], t[i]);
  98. }
  99. /* printf("OK\n"); */
  100. time_stop = fspr_time_now();
  101. printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  102. (time_stop - time_start));
  103. if (mutex_counter != MAX_COUNTER * num_threads)
  104. printf("error: counter = %ld\n", mutex_counter);
  105. return APR_SUCCESS;
  106. }
  107. int test_thread_mutex_nested(int num_threads)
  108. {
  109. fspr_thread_t *t[MAX_THREADS];
  110. fspr_status_t s[MAX_THREADS];
  111. fspr_time_t time_start, time_stop;
  112. int i;
  113. mutex_counter = 0;
  114. printf("fspr_thread_mutex_t Tests\n");
  115. printf("%-60s", " Initializing the fspr_thread_mutex_t (NESTED)");
  116. s[0] = fspr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_NESTED, pool);
  117. if (s[0] != APR_SUCCESS) {
  118. printf("Failed!\n");
  119. return s[0];
  120. }
  121. printf("OK\n");
  122. fspr_thread_mutex_lock(thread_lock);
  123. /* set_concurrency(4)? -aaron */
  124. printf(" Starting %d threads ", num_threads);
  125. for (i = 0; i < num_threads; ++i) {
  126. s[i] = fspr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
  127. if (s[i] != APR_SUCCESS) {
  128. printf("Failed!\n");
  129. return s[i];
  130. }
  131. }
  132. printf("OK\n");
  133. time_start = fspr_time_now();
  134. fspr_thread_mutex_unlock(thread_lock);
  135. /* printf("%-60s", " Waiting for threads to exit"); */
  136. for (i = 0; i < num_threads; ++i) {
  137. fspr_thread_join(&s[i], t[i]);
  138. }
  139. /* printf("OK\n"); */
  140. time_stop = fspr_time_now();
  141. printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  142. (time_stop - time_start));
  143. if (mutex_counter != MAX_COUNTER * num_threads)
  144. printf("error: counter = %ld\n", mutex_counter);
  145. return APR_SUCCESS;
  146. }
  147. int test_thread_rwlock(int num_threads)
  148. {
  149. fspr_thread_t *t[MAX_THREADS];
  150. fspr_status_t s[MAX_THREADS];
  151. fspr_time_t time_start, time_stop;
  152. int i;
  153. mutex_counter = 0;
  154. printf("fspr_thread_rwlock_t Tests\n");
  155. printf("%-60s", " Initializing the fspr_thread_rwlock_t");
  156. s[0] = fspr_thread_rwlock_create(&thread_rwlock, pool);
  157. if (s[0] != APR_SUCCESS) {
  158. printf("Failed!\n");
  159. return s[0];
  160. }
  161. printf("OK\n");
  162. fspr_thread_rwlock_wrlock(thread_rwlock);
  163. /* set_concurrency(4)? -aaron */
  164. printf(" Starting %d threads ", num_threads);
  165. for (i = 0; i < num_threads; ++i) {
  166. s[i] = fspr_thread_create(&t[i], NULL, thread_rwlock_func, NULL, pool);
  167. if (s[i] != APR_SUCCESS) {
  168. printf("Failed!\n");
  169. return s[i];
  170. }
  171. }
  172. printf("OK\n");
  173. time_start = fspr_time_now();
  174. fspr_thread_rwlock_unlock(thread_rwlock);
  175. /* printf("%-60s", " Waiting for threads to exit"); */
  176. for (i = 0; i < num_threads; ++i) {
  177. fspr_thread_join(&s[i], t[i]);
  178. }
  179. /* printf("OK\n"); */
  180. time_stop = fspr_time_now();
  181. printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  182. (time_stop - time_start));
  183. if (mutex_counter != MAX_COUNTER * num_threads)
  184. printf("error: counter = %ld\n", mutex_counter);
  185. return APR_SUCCESS;
  186. }
  187. int main(int argc, const char * const *argv)
  188. {
  189. fspr_status_t rv;
  190. char errmsg[200];
  191. const char *lockname = "multi.lock";
  192. fspr_getopt_t *opt;
  193. char optchar;
  194. const char *optarg;
  195. printf("APR Lock Performance Test\n==============\n\n");
  196. fspr_initialize();
  197. atexit(fspr_terminate);
  198. if (fspr_pool_create(&pool, NULL) != APR_SUCCESS)
  199. exit(-1);
  200. if ((rv = fspr_getopt_init(&opt, pool, argc, argv)) != APR_SUCCESS) {
  201. fprintf(stderr, "Could not set up to parse options: [%d] %s\n",
  202. rv, fspr_strerror(rv, errmsg, sizeof errmsg));
  203. exit(-1);
  204. }
  205. while ((rv = fspr_getopt(opt, "f:", &optchar, &optarg)) == APR_SUCCESS) {
  206. if (optchar == 'f') {
  207. lockname = optarg;
  208. }
  209. }
  210. if (rv != APR_SUCCESS && rv != APR_EOF) {
  211. fprintf(stderr, "Could not parse options: [%d] %s\n",
  212. rv, fspr_strerror(rv, errmsg, sizeof errmsg));
  213. exit(-1);
  214. }
  215. for (i = 1; i <= MAX_THREADS; ++i) {
  216. if ((rv = test_thread_mutex(i)) != APR_SUCCESS) {
  217. fprintf(stderr,"thread_mutex test failed : [%d] %s\n",
  218. rv, fspr_strerror(rv, (char*)errmsg, 200));
  219. exit(-3);
  220. }
  221. if ((rv = test_thread_mutex_nested(i)) != APR_SUCCESS) {
  222. fprintf(stderr,"thread_mutex (NESTED) test failed : [%d] %s\n",
  223. rv, fspr_strerror(rv, (char*)errmsg, 200));
  224. exit(-4);
  225. }
  226. if ((rv = test_thread_rwlock(i)) != APR_SUCCESS) {
  227. fprintf(stderr,"thread_rwlock test failed : [%d] %s\n",
  228. rv, fspr_strerror(rv, (char*)errmsg, 200));
  229. exit(-6);
  230. }
  231. }
  232. return 0;
  233. }
  234. #endif /* !APR_HAS_THREADS */