mutex.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. mutex.c
  5. Abstract:
  6. This module implements the mutex performance benchmark tests.
  7. Author:
  8. Chris Stevens 8-May-2015
  9. Environment:
  10. User
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <pthread.h>
  19. #include <unistd.h>
  20. #include "perftest.h"
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. #define PT_MUTEXT_TEST_THREAD_COUNT 8
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. //
  29. // ----------------------------------------------- Internal Function Prototypes
  30. //
  31. void *
  32. MutexStartRoutine (
  33. void *Parameter
  34. );
  35. //
  36. // -------------------------------------------------------------------- Globals
  37. //
  38. volatile int MutexReadyThreadCount;
  39. //
  40. // ------------------------------------------------------------------ Functions
  41. //
  42. void
  43. MutexMain (
  44. PPT_TEST_INFORMATION Test,
  45. PPT_TEST_RESULT Result
  46. )
  47. /*++
  48. Routine Description:
  49. This routine performs the mutex performance benchmark tests.
  50. Arguments:
  51. Test - Supplies a pointer to the performance test being executed.
  52. Result - Supplies a pointer to a performance test result structure that
  53. receives the tests results.
  54. Return Value:
  55. None.
  56. --*/
  57. {
  58. unsigned long long Iterations;
  59. pthread_mutex_t Mutex;
  60. int MutexInitialized;
  61. int Status;
  62. int ThreadCount;
  63. int ThreadIndex;
  64. pthread_t *Threads;
  65. Iterations = 0;
  66. MutexInitialized = 0;
  67. Threads = NULL;
  68. Result->Type = PtResultIterations;
  69. Result->Status = 0;
  70. ThreadIndex = 0;
  71. //
  72. // Initialize a mutex for use by the main thread and any additional threads.
  73. //
  74. Status = pthread_mutex_init(&Mutex, NULL);
  75. if (Status != 0) {
  76. Result->Status = Status;
  77. goto MainEnd;
  78. }
  79. MutexInitialized = 1;
  80. //
  81. // Initialize the given test state.
  82. //
  83. switch (Test->TestType) {
  84. case PtTestMutex:
  85. break;
  86. case PtTestMutexContended:
  87. Threads = malloc(sizeof(pthread_t) * PT_MUTEXT_TEST_THREAD_COUNT);
  88. if (Threads == NULL) {
  89. Result->Status = ENOMEM;
  90. goto MainEnd;
  91. }
  92. for (ThreadIndex = 0;
  93. ThreadIndex < PT_MUTEXT_TEST_THREAD_COUNT;
  94. ThreadIndex += 1) {
  95. Status = pthread_create(&(Threads[ThreadIndex]),
  96. NULL,
  97. MutexStartRoutine,
  98. &Mutex);
  99. if (Status != 0) {
  100. Result->Status = Status;
  101. goto MainEnd;
  102. }
  103. }
  104. //
  105. // Wait until all threads are spun up.
  106. //
  107. while (MutexReadyThreadCount != PT_MUTEXT_TEST_THREAD_COUNT) {
  108. sleep(1);
  109. }
  110. break;
  111. default:
  112. assert(0);
  113. Result->Status = EINVAL;
  114. return;
  115. }
  116. //
  117. // Start the test. This snaps resource usage and starts the clock ticking.
  118. //
  119. Status = PtStartTimedTest(Test->Duration);
  120. if (Status != 0) {
  121. Result->Status = errno;
  122. goto MainEnd;
  123. }
  124. //
  125. // Measure the performance of the mutex lock and unlock by seeing how many
  126. // times it can be acquired and released.
  127. //
  128. while (PtIsTimedTestRunning() != 0) {
  129. pthread_mutex_lock(&Mutex);
  130. pthread_mutex_unlock(&Mutex);
  131. Iterations += 1;
  132. }
  133. Status = PtFinishTimedTest(Result);
  134. if ((Status != 0) && (Result->Status == 0)) {
  135. Result->Status = errno;
  136. }
  137. MainEnd:
  138. //
  139. // Tear down the test state.
  140. //
  141. switch (Test->TestType) {
  142. case PtTestMutexContended:
  143. if (Threads != NULL) {
  144. ThreadCount = ThreadIndex;
  145. for (ThreadIndex = 0; ThreadIndex < ThreadCount; ThreadIndex += 1) {
  146. pthread_cancel(Threads[ThreadIndex]);
  147. pthread_join(Threads[ThreadIndex], NULL);
  148. }
  149. free(Threads);
  150. }
  151. break;
  152. case PtTestMutex:
  153. default:
  154. break;
  155. }
  156. if (MutexInitialized != 0) {
  157. pthread_mutex_destroy(&Mutex);
  158. }
  159. Result->Data.Iterations = Iterations;
  160. return;
  161. }
  162. //
  163. // --------------------------------------------------------- Internal Functions
  164. //
  165. void *
  166. MutexStartRoutine (
  167. void *Parameter
  168. )
  169. /*++
  170. Routine Description:
  171. This routine implements the start routine for a new test thread. It will
  172. wait in a loop for the test to start and then loop acquiring and releasing
  173. the given mutex.
  174. Arguments:
  175. Parameter - Supplies a pointer to the mutex to use.
  176. Return Value:
  177. Returns the NULL pointer.
  178. --*/
  179. {
  180. pthread_mutex_t *Mutex;
  181. Mutex = (pthread_mutex_t *)Parameter;
  182. //
  183. // Announce that the thread is ready.
  184. //
  185. pthread_mutex_lock(Mutex);
  186. MutexReadyThreadCount += 1;
  187. pthread_mutex_unlock(Mutex);
  188. //
  189. // Busy spin waiting for the test to start.
  190. //
  191. while (PtIsTimedTestRunning() == 0) {
  192. pthread_testcancel();
  193. }
  194. //
  195. // Loop running the test.
  196. //
  197. while (PtIsTimedTestRunning() != 0) {
  198. pthread_mutex_lock(Mutex);
  199. pthread_mutex_unlock(Mutex);
  200. }
  201. return NULL;
  202. }