benchtest3.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * benchtest3.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 a trylock on a locked mutex 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. old_mutex_t ox;
  51. pthread_mutexattr_t ma;
  52. PTW32_STRUCT_TIMEB currSysTimeStart;
  53. PTW32_STRUCT_TIMEB currSysTimeStop;
  54. long durationMilliSecs;
  55. long overHeadMilliSecs = 0;
  56. #define GetDurationMilliSecs(_TStart, _TStop) ((long)((_TStop.time*1000+_TStop.millitm) \
  57. - (_TStart.time*1000+_TStart.millitm)))
  58. /*
  59. * Dummy use of j, otherwise the loop may be removed by the optimiser
  60. * when doing the overhead timing with an empty loop.
  61. */
  62. #define TESTSTART \
  63. { int i, j = 0, k = 0; PTW32_FTIME(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
  64. #define TESTSTOP \
  65. }; PTW32_FTIME(&currSysTimeStop); if (j + k == i) j++; }
  66. void *
  67. trylockThread (void * arg)
  68. {
  69. TESTSTART
  70. (void) pthread_mutex_trylock(&mx);
  71. TESTSTOP
  72. return NULL;
  73. }
  74. void *
  75. oldTrylockThread (void * arg)
  76. {
  77. TESTSTART
  78. (void) old_mutex_trylock(&ox);
  79. TESTSTOP
  80. return NULL;
  81. }
  82. void
  83. runTest (char * testNameString, int mType)
  84. {
  85. pthread_t t;
  86. #ifdef PTW32_MUTEX_TYPES
  87. (void) pthread_mutexattr_settype(&ma, mType);
  88. #endif
  89. assert(pthread_mutex_init(&mx, &ma) == 0);
  90. assert(pthread_mutex_lock(&mx) == 0);
  91. assert(pthread_create(&t, NULL, trylockThread, 0) == 0);
  92. assert(pthread_join(t, NULL) == 0);
  93. assert(pthread_mutex_unlock(&mx) == 0);
  94. assert(pthread_mutex_destroy(&mx) == 0);
  95. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  96. printf( "%-45s %15ld %15.3f\n",
  97. testNameString,
  98. durationMilliSecs,
  99. (float) durationMilliSecs * 1E3 / ITERATIONS);
  100. }
  101. int
  102. main (int argc, char *argv[])
  103. {
  104. pthread_t t;
  105. assert(pthread_mutexattr_init(&ma) == 0);
  106. printf( "=============================================================================\n");
  107. printf( "\nTrylock on a locked mutex.\n");
  108. printf( "%ld iterations.\n\n", ITERATIONS);
  109. printf( "%-45s %15s %15s\n",
  110. "Test",
  111. "Total(msec)",
  112. "average(usec)");
  113. printf( "-----------------------------------------------------------------------------\n");
  114. /*
  115. * Time the loop overhead so we can subtract it from the actual test times.
  116. */
  117. TESTSTART
  118. TESTSTOP
  119. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  120. overHeadMilliSecs = durationMilliSecs;
  121. old_mutex_use = OLD_WIN32CS;
  122. assert(old_mutex_init(&ox, NULL) == 0);
  123. assert(old_mutex_lock(&ox) == 0);
  124. assert(pthread_create(&t, NULL, oldTrylockThread, 0) == 0);
  125. assert(pthread_join(t, NULL) == 0);
  126. assert(old_mutex_unlock(&ox) == 0);
  127. assert(old_mutex_destroy(&ox) == 0);
  128. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  129. printf( "%-45s %15ld %15.3f\n",
  130. "Old PT Mutex using a Critical Section (WNT)",
  131. durationMilliSecs,
  132. (float) durationMilliSecs * 1E3 / ITERATIONS);
  133. old_mutex_use = OLD_WIN32MUTEX;
  134. assert(old_mutex_init(&ox, NULL) == 0);
  135. assert(old_mutex_lock(&ox) == 0);
  136. assert(pthread_create(&t, NULL, oldTrylockThread, 0) == 0);
  137. assert(pthread_join(t, NULL) == 0);
  138. assert(old_mutex_unlock(&ox) == 0);
  139. assert(old_mutex_destroy(&ox) == 0);
  140. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  141. printf( "%-45s %15ld %15.3f\n",
  142. "Old PT Mutex using a Win32 Mutex (W9x)",
  143. durationMilliSecs,
  144. (float) durationMilliSecs * 1E3 / ITERATIONS);
  145. printf( ".............................................................................\n");
  146. /*
  147. * Now we can start the actual tests
  148. */
  149. #ifdef PTW32_MUTEX_TYPES
  150. runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT);
  151. runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL);
  152. runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK);
  153. runTest("PTHREAD_MUTEX_RECURSIVE", PTHREAD_MUTEX_RECURSIVE);
  154. #else
  155. runTest("Non-blocking lock", 0);
  156. #endif
  157. printf( ".............................................................................\n");
  158. pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
  159. #ifdef PTW32_MUTEX_TYPES
  160. runTest("PTHREAD_MUTEX_DEFAULT (Robust)", PTHREAD_MUTEX_DEFAULT);
  161. runTest("PTHREAD_MUTEX_NORMAL (Robust)", PTHREAD_MUTEX_NORMAL);
  162. runTest("PTHREAD_MUTEX_ERRORCHECK (Robust)", PTHREAD_MUTEX_ERRORCHECK);
  163. runTest("PTHREAD_MUTEX_RECURSIVE (Robust)", PTHREAD_MUTEX_RECURSIVE);
  164. #else
  165. runTest("Non-blocking lock", 0);
  166. #endif
  167. printf( "=============================================================================\n");
  168. /*
  169. * End of tests.
  170. */
  171. pthread_mutexattr_destroy(&ma);
  172. return 0;
  173. }