benchtest4.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * benchtest4.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 trylock/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. 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. oldRunTest (char * testNameString, int mType)
  68. {
  69. }
  70. void
  71. runTest (char * testNameString, int mType)
  72. {
  73. #ifdef PTW32_MUTEX_TYPES
  74. pthread_mutexattr_settype(&ma, mType);
  75. #endif
  76. pthread_mutex_init(&mx, &ma);
  77. TESTSTART
  78. (void) pthread_mutex_trylock(&mx);
  79. (void) pthread_mutex_unlock(&mx);
  80. TESTSTOP
  81. pthread_mutex_destroy(&mx);
  82. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  83. printf( "%-45s %15ld %15.3f\n",
  84. testNameString,
  85. durationMilliSecs,
  86. (float) durationMilliSecs * 1E3 / ITERATIONS);
  87. }
  88. int
  89. main (int argc, char *argv[])
  90. {
  91. pthread_mutexattr_init(&ma);
  92. printf( "=============================================================================\n");
  93. printf( "Trylock plus unlock on an unlocked mutex.\n");
  94. printf( "%ld iterations.\n\n", ITERATIONS);
  95. printf( "%-45s %15s %15s\n",
  96. "Test",
  97. "Total(msec)",
  98. "average(usec)");
  99. printf( "-----------------------------------------------------------------------------\n");
  100. /*
  101. * Time the loop overhead so we can subtract it from the actual test times.
  102. */
  103. TESTSTART
  104. TESTSTOP
  105. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  106. overHeadMilliSecs = durationMilliSecs;
  107. old_mutex_use = OLD_WIN32CS;
  108. assert(old_mutex_init(&ox, NULL) == 0);
  109. TESTSTART
  110. (void) old_mutex_trylock(&ox);
  111. (void) old_mutex_unlock(&ox);
  112. TESTSTOP
  113. assert(old_mutex_destroy(&ox) == 0);
  114. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  115. printf( "%-45s %15ld %15.3f\n",
  116. "Old PT Mutex using a Critical Section (WNT)",
  117. durationMilliSecs,
  118. (float) durationMilliSecs * 1E3 / ITERATIONS);
  119. old_mutex_use = OLD_WIN32MUTEX;
  120. assert(old_mutex_init(&ox, NULL) == 0);
  121. TESTSTART
  122. (void) old_mutex_trylock(&ox);
  123. (void) old_mutex_unlock(&ox);
  124. TESTSTOP
  125. assert(old_mutex_destroy(&ox) == 0);
  126. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  127. printf( "%-45s %15ld %15.3f\n",
  128. "Old PT Mutex using a Win32 Mutex (W9x)",
  129. durationMilliSecs,
  130. (float) durationMilliSecs * 1E3 / ITERATIONS);
  131. printf( ".............................................................................\n");
  132. /*
  133. * Now we can start the actual tests
  134. */
  135. #ifdef PTW32_MUTEX_TYPES
  136. runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT);
  137. runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL);
  138. runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK);
  139. runTest("PTHREAD_MUTEX_RECURSIVE", PTHREAD_MUTEX_RECURSIVE);
  140. #else
  141. runTest("Non-blocking lock", 0);
  142. #endif
  143. printf( ".............................................................................\n");
  144. pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
  145. #ifdef PTW32_MUTEX_TYPES
  146. runTest("PTHREAD_MUTEX_DEFAULT (Robust)", PTHREAD_MUTEX_DEFAULT);
  147. runTest("PTHREAD_MUTEX_NORMAL (Robust)", PTHREAD_MUTEX_NORMAL);
  148. runTest("PTHREAD_MUTEX_ERRORCHECK (Robust)", PTHREAD_MUTEX_ERRORCHECK);
  149. runTest("PTHREAD_MUTEX_RECURSIVE (Robust)", PTHREAD_MUTEX_RECURSIVE);
  150. #else
  151. runTest("Non-blocking lock", 0);
  152. #endif
  153. printf( "=============================================================================\n");
  154. /*
  155. * End of tests.
  156. */
  157. pthread_mutexattr_destroy(&ma);
  158. return 0;
  159. }