benchtest1.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * benchtest1.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  8. * Copyright(C) 2008 Jason Schmidlapp
  9. *
  10. * Contact Email: [email protected]
  11. *
  12. *
  13. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  14. * Copyright(C) 1998 John E. Bossom
  15. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  16. *
  17. * Contact Email: [email protected]
  18. *
  19. * The original list of contributors to the Pthreads-win32 project
  20. * is contained in the file CONTRIBUTORS.ptw32 included with the
  21. * source code distribution. The list can also be seen at the
  22. * following World Wide Web location:
  23. * http://sources.redhat.com/pthreads-win32/contributors.html
  24. *
  25. * This library is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU Lesser General Public
  27. * License as published by the Free Software Foundation; either
  28. * version 2 of the License, or (at your option) any later version.
  29. *
  30. * This library is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. * Lesser General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Lesser General Public
  36. * License along with this library in the file COPYING.LIB;
  37. * if not, write to the Free Software Foundation, Inc.,
  38. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  39. *
  40. * --------------------------------------------------------------------------
  41. *
  42. * Measure time taken to complete an elementary operation.
  43. *
  44. * - Mutex
  45. * Single thread iteration over lock/unlock for each mutex type.
  46. */
  47. #include "test.h"
  48. #include "implement.h"
  49. #ifdef __GNUC__
  50. #include <stdlib.h>
  51. #endif
  52. #include "benchtest.h"
  53. #define PTW32_MUTEX_TYPES
  54. #define ITERATIONS 100000L
  55. static pthread_mutex_t mx;
  56. static pthread_mutexattr_t ma;
  57. static struct _timeb currSysTimeStart;
  58. static struct _timeb currSysTimeStop;
  59. static long durationMilliSecs;
  60. static long overHeadMilliSecs = 0;
  61. static int one = 1;
  62. static int zero = 0;
  63. #define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
  64. - (_TStart.time*1000+_TStart.millitm))
  65. /*
  66. * Dummy use of j, otherwise the loop may be removed by the optimiser
  67. * when doing the overhead timing with an empty loop.
  68. */
  69. #define TESTSTART \
  70. { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
  71. #define TESTSTOP \
  72. }; _ftime(&currSysTimeStop); if (j + k == i) j++; }
  73. static void
  74. runTest (char * testNameString, int mType)
  75. {
  76. #ifdef PTW32_MUTEX_TYPES
  77. assert(pthread_mutexattr_settype(&ma, mType) == 0);
  78. #endif
  79. assert(pthread_mutex_init(&mx, &ma) == 0);
  80. TESTSTART
  81. assert(pthread_mutex_lock(&mx) == zero);
  82. assert(pthread_mutex_unlock(&mx) == zero);
  83. TESTSTOP
  84. assert(pthread_mutex_destroy(&mx) == 0);
  85. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  86. printf( "%-45s %15ld %15.3f\n",
  87. testNameString,
  88. durationMilliSecs,
  89. (float) durationMilliSecs * 1E3 / ITERATIONS);
  90. }
  91. int pthread_test_bench1()
  92. {
  93. int i = 0;
  94. pte_osMutexHandle cs;
  95. overHeadMilliSecs = 0;
  96. one = 1;
  97. zero = 0;
  98. pthread_mutexattr_init(&ma);
  99. printf( "=============================================================================\n");
  100. printf( "\nLock plus unlock on an unlocked mutex.\n%ld iterations\n\n",
  101. ITERATIONS);
  102. printf( "%-45s %15s %15s\n",
  103. "Test",
  104. "Total(msec)",
  105. "average(usec)");
  106. /*
  107. * Time the loop overhead so we can subtract it from the actual test times.
  108. */
  109. TESTSTART
  110. assert(1 == one);
  111. assert(1 == one);
  112. TESTSTOP
  113. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  114. overHeadMilliSecs = durationMilliSecs;
  115. TESTSTART
  116. assert((dummy_call(&i), 1) == one);
  117. assert((dummy_call(&i), 1) == one);
  118. TESTSTOP
  119. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  120. printf( "%-45s %15ld %15.3f\n",
  121. "Dummy call x 2",
  122. durationMilliSecs,
  123. (float) durationMilliSecs * 1E3 / ITERATIONS);
  124. TESTSTART
  125. assert((interlocked_inc_with_conditionals(&i), 1) == one);
  126. assert((interlocked_dec_with_conditionals(&i), 1) == one);
  127. TESTSTOP
  128. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  129. printf( "%-45s %15ld %15.3f\n",
  130. "Dummy call -> Interlocked with cond x 2",
  131. durationMilliSecs,
  132. (float) durationMilliSecs * 1E3 / ITERATIONS);
  133. TESTSTART
  134. assert((PTE_ATOMIC_INCREMENT(&i), 1) == one);
  135. assert((PTE_ATOMIC_INCREMENT(&i), 1) == one);
  136. TESTSTOP
  137. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  138. printf( "%-45s %15ld %15.3f\n",
  139. "InterlockedOp x 2",
  140. durationMilliSecs,
  141. (float) durationMilliSecs * 1E3 / ITERATIONS);
  142. pte_osMutexCreate(&cs);
  143. TESTSTART
  144. {
  145. pte_osMutexLock(cs);
  146. pte_osMutexUnlock(cs);
  147. }
  148. TESTSTOP
  149. pte_osMutexDelete(cs);
  150. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  151. printf( "%-45s %15ld %15.3f\n",
  152. "Simple Critical Section",
  153. durationMilliSecs,
  154. (float) durationMilliSecs * 1E3 / ITERATIONS);
  155. printf( ".............................................................................\n");
  156. /*
  157. * Now we can start the actual tests
  158. */
  159. #ifdef PTW32_MUTEX_TYPES
  160. runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT);
  161. runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL);
  162. runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK);
  163. runTest("PTHREAD_MUTEX_RECURSIVE", 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. one = i; /* Dummy assignment to avoid 'variable unused' warning */
  173. return 0;
  174. }