benchtest1.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. * Single thread iteration over lock/unlock for each mutex type.
  40. */
  41. #include "test.h"
  42. #include <sys/timeb.h>
  43. #ifdef __GNUC__
  44. #include <stdlib.h>
  45. #endif
  46. #include "benchtest.h"
  47. #define PTW32_MUTEX_TYPES
  48. #define ITERATIONS 10000000L
  49. pthread_mutex_t mx;
  50. pthread_mutexattr_t ma;
  51. PTW32_STRUCT_TIMEB currSysTimeStart;
  52. PTW32_STRUCT_TIMEB currSysTimeStop;
  53. long durationMilliSecs;
  54. long overHeadMilliSecs = 0;
  55. int two = 2;
  56. int one = 1;
  57. int zero = 0;
  58. int iter;
  59. #define GetDurationMilliSecs(_TStart, _TStop) ((long)((_TStop.time*1000+_TStop.millitm) \
  60. - (_TStart.time*1000+_TStart.millitm)))
  61. /*
  62. * Dummy use of j, otherwise the loop may be removed by the optimiser
  63. * when doing the overhead timing with an empty loop.
  64. */
  65. #define TESTSTART \
  66. { int i, j = 0, k = 0; PTW32_FTIME(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
  67. #define TESTSTOP \
  68. }; PTW32_FTIME(&currSysTimeStop); if (j + k == i) j++; }
  69. void
  70. runTest (char * testNameString, int mType)
  71. {
  72. #ifdef PTW32_MUTEX_TYPES
  73. assert(pthread_mutexattr_settype(&ma, mType) == 0);
  74. #endif
  75. assert(pthread_mutex_init(&mx, &ma) == 0);
  76. TESTSTART
  77. assert((pthread_mutex_lock(&mx),1) == one);
  78. assert((pthread_mutex_unlock(&mx),2) == two);
  79. TESTSTOP
  80. assert(pthread_mutex_destroy(&mx) == 0);
  81. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  82. printf( "%-45s %15ld %15.3f\n",
  83. testNameString,
  84. durationMilliSecs,
  85. (float) durationMilliSecs * 1E3 / ITERATIONS);
  86. }
  87. int
  88. main (int argc, char *argv[])
  89. {
  90. int i = 0;
  91. CRITICAL_SECTION cs;
  92. old_mutex_t ox;
  93. pthread_mutexattr_init(&ma);
  94. printf( "=============================================================================\n");
  95. printf( "\nLock plus unlock on an unlocked mutex.\n%ld iterations\n\n",
  96. ITERATIONS);
  97. printf( "%-45s %15s %15s\n",
  98. "Test",
  99. "Total(msec)",
  100. "average(usec)");
  101. printf( "-----------------------------------------------------------------------------\n");
  102. /*
  103. * Time the loop overhead so we can subtract it from the actual test times.
  104. */
  105. TESTSTART
  106. assert(1 == one);
  107. assert(2 == two);
  108. TESTSTOP
  109. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  110. overHeadMilliSecs = durationMilliSecs;
  111. TESTSTART
  112. assert((dummy_call(&i), 1) == one);
  113. assert((dummy_call(&i), 2) == two);
  114. TESTSTOP
  115. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  116. printf( "%-45s %15ld %15.3f\n",
  117. "Dummy call x 2",
  118. durationMilliSecs,
  119. (float) (durationMilliSecs * 1E3 / ITERATIONS));
  120. TESTSTART
  121. assert((interlocked_inc_with_conditionals(&i), 1) == one);
  122. assert((interlocked_dec_with_conditionals(&i), 2) == two);
  123. TESTSTOP
  124. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  125. printf( "%-45s %15ld %15.3f\n",
  126. "Dummy call -> Interlocked with cond x 2",
  127. durationMilliSecs,
  128. (float) durationMilliSecs * 1E3 / ITERATIONS);
  129. TESTSTART
  130. assert((InterlockedIncrement((LPLONG)&i), 1) == (LONG)one);
  131. assert((InterlockedDecrement((LPLONG)&i), 2) == (LONG)two);
  132. TESTSTOP
  133. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  134. printf( "%-45s %15ld %15.3f\n",
  135. "InterlockedOp x 2",
  136. durationMilliSecs,
  137. (float) durationMilliSecs * 1E3 / ITERATIONS);
  138. InitializeCriticalSection(&cs);
  139. TESTSTART
  140. assert((EnterCriticalSection(&cs), 1) == one);
  141. assert((LeaveCriticalSection(&cs), 2) == two);
  142. TESTSTOP
  143. DeleteCriticalSection(&cs);
  144. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  145. printf( "%-45s %15ld %15.3f\n",
  146. "Simple Critical Section",
  147. durationMilliSecs,
  148. (float) durationMilliSecs * 1E3 / ITERATIONS);
  149. old_mutex_use = OLD_WIN32CS;
  150. assert(old_mutex_init(&ox, NULL) == 0);
  151. TESTSTART
  152. assert(old_mutex_lock(&ox) == zero);
  153. assert(old_mutex_unlock(&ox) == zero);
  154. TESTSTOP
  155. assert(old_mutex_destroy(&ox) == 0);
  156. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  157. printf( "%-45s %15ld %15.3f\n",
  158. "Old PT Mutex using a Critical Section (WNT)",
  159. durationMilliSecs,
  160. (float) durationMilliSecs * 1E3 / ITERATIONS);
  161. old_mutex_use = OLD_WIN32MUTEX;
  162. assert(old_mutex_init(&ox, NULL) == 0);
  163. TESTSTART
  164. assert(old_mutex_lock(&ox) == zero);
  165. assert(old_mutex_unlock(&ox) == zero);
  166. TESTSTOP
  167. assert(old_mutex_destroy(&ox) == 0);
  168. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  169. printf( "%-45s %15ld %15.3f\n",
  170. "Old PT Mutex using a Win32 Mutex (W9x)",
  171. durationMilliSecs,
  172. (float) durationMilliSecs * 1E3 / ITERATIONS);
  173. printf( ".............................................................................\n");
  174. /*
  175. * Now we can start the actual tests
  176. */
  177. #ifdef PTW32_MUTEX_TYPES
  178. runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT);
  179. runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL);
  180. runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK);
  181. runTest("PTHREAD_MUTEX_RECURSIVE", PTHREAD_MUTEX_RECURSIVE);
  182. #else
  183. runTest("Non-blocking lock", 0);
  184. #endif
  185. printf( ".............................................................................\n");
  186. pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
  187. #ifdef PTW32_MUTEX_TYPES
  188. runTest("PTHREAD_MUTEX_DEFAULT (Robust)", PTHREAD_MUTEX_DEFAULT);
  189. runTest("PTHREAD_MUTEX_NORMAL (Robust)", PTHREAD_MUTEX_NORMAL);
  190. runTest("PTHREAD_MUTEX_ERRORCHECK (Robust)", PTHREAD_MUTEX_ERRORCHECK);
  191. runTest("PTHREAD_MUTEX_RECURSIVE (Robust)", PTHREAD_MUTEX_RECURSIVE);
  192. #else
  193. runTest("Non-blocking lock", 0);
  194. #endif
  195. printf( "=============================================================================\n");
  196. /*
  197. * End of tests.
  198. */
  199. pthread_mutexattr_destroy(&ma);
  200. one = i; /* Dummy assignment to avoid 'variable unused' warning */
  201. return 0;
  202. }