benchtest2.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * benchtest1.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Measure time taken to complete an elementary operation.
  37. *
  38. * - Mutex
  39. * Two threads iterate over lock/unlock for each mutex type.
  40. * The two threads are forced into lock-step using two mutexes,
  41. * forcing the threads to block on each lock operation. The
  42. * time measured is therefore the worst case senario.
  43. */
  44. #include "test.h"
  45. #include <sys/timeb.h>
  46. #ifdef __GNUC__
  47. #include <stdlib.h>
  48. #endif
  49. #include "benchtest.h"
  50. #define PTW32_MUTEX_TYPES
  51. #define ITERATIONS 100000L
  52. pthread_mutex_t gate1, gate2;
  53. old_mutex_t ox1, ox2;
  54. CRITICAL_SECTION cs1, cs2;
  55. pthread_mutexattr_t ma;
  56. long durationMilliSecs;
  57. long overHeadMilliSecs = 0;
  58. PTW32_STRUCT_TIMEB currSysTimeStart;
  59. PTW32_STRUCT_TIMEB currSysTimeStop;
  60. pthread_t worker;
  61. int running = 0;
  62. #define GetDurationMilliSecs(_TStart, _TStop) ((long)((_TStop.time*1000+_TStop.millitm) \
  63. - (_TStart.time*1000+_TStart.millitm)))
  64. /*
  65. * Dummy use of j, otherwise the loop may be removed by the optimiser
  66. * when doing the overhead timing with an empty loop.
  67. */
  68. #define TESTSTART \
  69. { int i, j = 0, k = 0; PTW32_FTIME(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
  70. #define TESTSTOP \
  71. }; PTW32_FTIME(&currSysTimeStop); if (j + k == i) j++; }
  72. void *
  73. overheadThread(void * arg)
  74. {
  75. do
  76. {
  77. sched_yield();
  78. }
  79. while (running);
  80. return NULL;
  81. }
  82. void *
  83. oldThread(void * arg)
  84. {
  85. do
  86. {
  87. (void) old_mutex_lock(&ox1);
  88. (void) old_mutex_lock(&ox2);
  89. (void) old_mutex_unlock(&ox1);
  90. sched_yield();
  91. (void) old_mutex_unlock(&ox2);
  92. }
  93. while (running);
  94. return NULL;
  95. }
  96. void *
  97. workerThread(void * arg)
  98. {
  99. do
  100. {
  101. (void) pthread_mutex_lock(&gate1);
  102. (void) pthread_mutex_lock(&gate2);
  103. (void) pthread_mutex_unlock(&gate1);
  104. sched_yield();
  105. (void) pthread_mutex_unlock(&gate2);
  106. }
  107. while (running);
  108. return NULL;
  109. }
  110. void *
  111. CSThread(void * arg)
  112. {
  113. do
  114. {
  115. EnterCriticalSection(&cs1);
  116. EnterCriticalSection(&cs2);
  117. LeaveCriticalSection(&cs1);
  118. sched_yield();
  119. LeaveCriticalSection(&cs2);
  120. }
  121. while (running);
  122. return NULL;
  123. }
  124. void
  125. runTest (char * testNameString, int mType)
  126. {
  127. #ifdef PTW32_MUTEX_TYPES
  128. assert(pthread_mutexattr_settype(&ma, mType) == 0);
  129. #endif
  130. assert(pthread_mutex_init(&gate1, &ma) == 0);
  131. assert(pthread_mutex_init(&gate2, &ma) == 0);
  132. assert(pthread_mutex_lock(&gate1) == 0);
  133. assert(pthread_mutex_lock(&gate2) == 0);
  134. running = 1;
  135. assert(pthread_create(&worker, NULL, workerThread, NULL) == 0);
  136. TESTSTART
  137. (void) pthread_mutex_unlock(&gate1);
  138. sched_yield();
  139. (void) pthread_mutex_unlock(&gate2);
  140. (void) pthread_mutex_lock(&gate1);
  141. (void) pthread_mutex_lock(&gate2);
  142. TESTSTOP
  143. running = 0;
  144. assert(pthread_mutex_unlock(&gate2) == 0);
  145. assert(pthread_mutex_unlock(&gate1) == 0);
  146. assert(pthread_join(worker, NULL) == 0);
  147. assert(pthread_mutex_destroy(&gate2) == 0);
  148. assert(pthread_mutex_destroy(&gate1) == 0);
  149. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  150. printf( "%-45s %15ld %15.3f\n",
  151. testNameString,
  152. durationMilliSecs,
  153. (float) durationMilliSecs * 1E3 / ITERATIONS / 4 /* Four locks/unlocks per iteration */);
  154. }
  155. int
  156. main (int argc, char *argv[])
  157. {
  158. assert(pthread_mutexattr_init(&ma) == 0);
  159. printf( "=============================================================================\n");
  160. printf( "\nLock plus unlock on a locked mutex.\n");
  161. printf("%ld iterations, four locks/unlocks per iteration.\n\n", ITERATIONS);
  162. printf( "%-45s %15s %15s\n",
  163. "Test",
  164. "Total(msec)",
  165. "average(usec)");
  166. printf( "-----------------------------------------------------------------------------\n");
  167. /*
  168. * Time the loop overhead so we can subtract it from the actual test times.
  169. */
  170. running = 1;
  171. assert(pthread_create(&worker, NULL, overheadThread, NULL) == 0);
  172. TESTSTART
  173. sched_yield();
  174. sched_yield();
  175. TESTSTOP
  176. running = 0;
  177. assert(pthread_join(worker, NULL) == 0);
  178. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  179. overHeadMilliSecs = durationMilliSecs;
  180. InitializeCriticalSection(&cs1);
  181. InitializeCriticalSection(&cs2);
  182. EnterCriticalSection(&cs1);
  183. EnterCriticalSection(&cs2);
  184. running = 1;
  185. assert(pthread_create(&worker, NULL, CSThread, NULL) == 0);
  186. TESTSTART
  187. LeaveCriticalSection(&cs1);
  188. sched_yield();
  189. LeaveCriticalSection(&cs2);
  190. EnterCriticalSection(&cs1);
  191. EnterCriticalSection(&cs2);
  192. TESTSTOP
  193. running = 0;
  194. LeaveCriticalSection(&cs2);
  195. LeaveCriticalSection(&cs1);
  196. assert(pthread_join(worker, NULL) == 0);
  197. DeleteCriticalSection(&cs2);
  198. DeleteCriticalSection(&cs1);
  199. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  200. printf( "%-45s %15ld %15.3f\n",
  201. "Simple Critical Section",
  202. durationMilliSecs,
  203. (float) durationMilliSecs * 1E3 / ITERATIONS / 4 );
  204. old_mutex_use = OLD_WIN32CS;
  205. assert(old_mutex_init(&ox1, NULL) == 0);
  206. assert(old_mutex_init(&ox2, NULL) == 0);
  207. assert(old_mutex_lock(&ox1) == 0);
  208. assert(old_mutex_lock(&ox2) == 0);
  209. running = 1;
  210. assert(pthread_create(&worker, NULL, oldThread, NULL) == 0);
  211. TESTSTART
  212. (void) old_mutex_unlock(&ox1);
  213. sched_yield();
  214. (void) old_mutex_unlock(&ox2);
  215. (void) old_mutex_lock(&ox1);
  216. (void) old_mutex_lock(&ox2);
  217. TESTSTOP
  218. running = 0;
  219. assert(old_mutex_unlock(&ox1) == 0);
  220. assert(old_mutex_unlock(&ox2) == 0);
  221. assert(pthread_join(worker, NULL) == 0);
  222. assert(old_mutex_destroy(&ox2) == 0);
  223. assert(old_mutex_destroy(&ox1) == 0);
  224. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  225. printf( "%-45s %15ld %15.3f\n",
  226. "Old PT Mutex using a Critical Section (WNT)",
  227. durationMilliSecs,
  228. (float) durationMilliSecs * 1E3 / ITERATIONS / 4);
  229. old_mutex_use = OLD_WIN32MUTEX;
  230. assert(old_mutex_init(&ox1, NULL) == 0);
  231. assert(old_mutex_init(&ox2, NULL) == 0);
  232. assert(old_mutex_lock(&ox1) == 0);
  233. assert(old_mutex_lock(&ox2) == 0);
  234. running = 1;
  235. assert(pthread_create(&worker, NULL, oldThread, NULL) == 0);
  236. TESTSTART
  237. (void) old_mutex_unlock(&ox1);
  238. sched_yield();
  239. (void) old_mutex_unlock(&ox2);
  240. (void) old_mutex_lock(&ox1);
  241. (void) old_mutex_lock(&ox2);
  242. TESTSTOP
  243. running = 0;
  244. assert(old_mutex_unlock(&ox1) == 0);
  245. assert(old_mutex_unlock(&ox2) == 0);
  246. assert(pthread_join(worker, NULL) == 0);
  247. assert(old_mutex_destroy(&ox2) == 0);
  248. assert(old_mutex_destroy(&ox1) == 0);
  249. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  250. printf( "%-45s %15ld %15.3f\n",
  251. "Old PT Mutex using a Win32 Mutex (W9x)",
  252. durationMilliSecs,
  253. (float) durationMilliSecs * 1E3 / ITERATIONS / 4);
  254. printf( ".............................................................................\n");
  255. /*
  256. * Now we can start the actual tests
  257. */
  258. #ifdef PTW32_MUTEX_TYPES
  259. runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT);
  260. runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL);
  261. runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK);
  262. runTest("PTHREAD_MUTEX_RECURSIVE", PTHREAD_MUTEX_RECURSIVE);
  263. #else
  264. runTest("Non-blocking lock", 0);
  265. #endif
  266. printf( ".............................................................................\n");
  267. pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
  268. #ifdef PTW32_MUTEX_TYPES
  269. runTest("PTHREAD_MUTEX_DEFAULT (Robust)", PTHREAD_MUTEX_DEFAULT);
  270. runTest("PTHREAD_MUTEX_NORMAL (Robust)", PTHREAD_MUTEX_NORMAL);
  271. runTest("PTHREAD_MUTEX_ERRORCHECK (Robust)", PTHREAD_MUTEX_ERRORCHECK);
  272. runTest("PTHREAD_MUTEX_RECURSIVE (Robust)", PTHREAD_MUTEX_RECURSIVE);
  273. #else
  274. runTest("Non-blocking lock", 0);
  275. #endif
  276. printf( "=============================================================================\n");
  277. /*
  278. * End of tests.
  279. */
  280. pthread_mutexattr_destroy(&ma);
  281. return 0;
  282. }