benchtest2.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * Two threads iterate over lock/unlock for each mutex type.
  46. * The two threads are forced into lock-step using two mutexes,
  47. * forcing the threads to block on each lock operation. The
  48. * time measured is therefore the worst case senario.
  49. */
  50. #include "test.h"
  51. #ifdef __GNUC__
  52. #include <stdlib.h>
  53. #endif
  54. #include "benchtest.h"
  55. #define PTW32_MUTEX_TYPES
  56. #define ITERATIONS 10000L
  57. static pthread_mutex_t gate1, gate2;
  58. static pthread_mutexattr_t ma;
  59. static long durationMilliSecs;
  60. static long overHeadMilliSecs = 0;
  61. static struct _timeb currSysTimeStart;
  62. static struct _timeb currSysTimeStop;
  63. static pthread_t worker;
  64. static int running = 0;
  65. #define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
  66. - (_TStart.time*1000+_TStart.millitm))
  67. /*
  68. * Dummy use of j, otherwise the loop may be removed by the optimiser
  69. * when doing the overhead timing with an empty loop.
  70. */
  71. #define TESTSTART \
  72. { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
  73. #define TESTSTOP \
  74. }; _ftime(&currSysTimeStop); if (j + k == i) j++; }
  75. static void *
  76. overheadThread(void * arg)
  77. {
  78. do
  79. {
  80. sched_yield();
  81. }
  82. while (running);
  83. return NULL;
  84. }
  85. static void *
  86. workerThread(void * arg)
  87. {
  88. do
  89. {
  90. (void) pthread_mutex_lock(&gate1);
  91. (void) pthread_mutex_lock(&gate2);
  92. (void) pthread_mutex_unlock(&gate1);
  93. sched_yield();
  94. (void) pthread_mutex_unlock(&gate2);
  95. }
  96. while (running);
  97. return NULL;
  98. }
  99. static void
  100. runTest (char * testNameString, int mType)
  101. {
  102. #ifdef PTW32_MUTEX_TYPES
  103. assert(pthread_mutexattr_settype(&ma, mType) == 0);
  104. #endif
  105. assert(pthread_mutex_init(&gate1, &ma) == 0);
  106. assert(pthread_mutex_init(&gate2, &ma) == 0);
  107. assert(pthread_mutex_lock(&gate1) == 0);
  108. assert(pthread_mutex_lock(&gate2) == 0);
  109. running = 1;
  110. assert(pthread_create(&worker, NULL, workerThread, NULL) == 0);
  111. TESTSTART
  112. (void) pthread_mutex_unlock(&gate1);
  113. sched_yield();
  114. (void) pthread_mutex_unlock(&gate2);
  115. (void) pthread_mutex_lock(&gate1);
  116. (void) pthread_mutex_lock(&gate2);
  117. TESTSTOP
  118. running = 0;
  119. assert(pthread_mutex_unlock(&gate2) == 0);
  120. assert(pthread_mutex_unlock(&gate1) == 0);
  121. assert(pthread_join(worker, NULL) == 0);
  122. assert(pthread_mutex_destroy(&gate2) == 0);
  123. assert(pthread_mutex_destroy(&gate1) == 0);
  124. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  125. printf( "%-45s %15ld %15.3f\n",
  126. testNameString,
  127. durationMilliSecs,
  128. (float) durationMilliSecs * 1E3 / ITERATIONS / 4 /* Four locks/unlocks per iteration */);
  129. }
  130. int pthread_test_bench2()
  131. {
  132. assert(pthread_mutexattr_init(&ma) == 0);
  133. printf( "=============================================================================\n");
  134. printf( "\nLock plus unlock on a locked mutex.\n");
  135. printf("%ld iterations, four locks/unlocks per iteration.\n\n", ITERATIONS);
  136. printf( "%-45s %15s %15s\n",
  137. "Test",
  138. "Total(msec)",
  139. "average(usec)");
  140. /*
  141. * Time the loop overhead so we can subtract it from the actual test times.
  142. */
  143. running = 1;
  144. assert(pthread_create(&worker, NULL, overheadThread, NULL) == 0);
  145. TESTSTART
  146. sched_yield();
  147. sched_yield();
  148. TESTSTOP
  149. running = 0;
  150. assert(pthread_join(worker, NULL) == 0);
  151. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  152. overHeadMilliSecs = durationMilliSecs;
  153. /*
  154. * Now we can start the actual tests
  155. */
  156. #ifdef PTW32_MUTEX_TYPES
  157. runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT);
  158. runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL);
  159. runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK);
  160. runTest("PTHREAD_MUTEX_RECURSIVE", PTHREAD_MUTEX_RECURSIVE);
  161. #else
  162. runTest("Blocking locks", 0);
  163. #endif
  164. printf( "=============================================================================\n");
  165. /*
  166. * End of tests.
  167. */
  168. pthread_mutexattr_destroy(&ma);
  169. return 0;
  170. }