malloc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. malloc.c
  5. Abstract:
  6. This module implements the performance benchmark tests for the malloc() C
  7. library routine.
  8. Author:
  9. Chris Stevens 8-May-2015
  10. Environment:
  11. User
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <pthread.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include "perftest.h"
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. #define PT_MALLOC_TEST_SMALL_ALLOCATION 32
  27. #define PT_MALLOC_TEST_LARGE_ALLOCATION (128 * 1024)
  28. #define PT_MALLOC_TEST_ALLOCATION_LIMIT (256 * 1024)
  29. #define PT_MALLOC_TEST_ALLOCATION_COUNT 32
  30. #define PT_MALLOC_TEST_THREAD_COUNT 8
  31. //
  32. // ------------------------------------------------------ Data Type Definitions
  33. //
  34. //
  35. // ----------------------------------------------- Internal Function Prototypes
  36. //
  37. void *
  38. MallocStartRoutine (
  39. void *Parameter
  40. );
  41. //
  42. // -------------------------------------------------------------------- Globals
  43. //
  44. volatile int MallocReadyThreadCount;
  45. //
  46. // ------------------------------------------------------------------ Functions
  47. //
  48. void
  49. MallocMain (
  50. PPT_TEST_INFORMATION Test,
  51. PPT_TEST_RESULT Result
  52. )
  53. /*++
  54. Routine Description:
  55. This routine performs the malloc performance benchmark tests.
  56. Arguments:
  57. Test - Supplies a pointer to the performance test being executed.
  58. Result - Supplies a pointer to a performance test result structure that
  59. receives the tests results.
  60. Return Value:
  61. None.
  62. --*/
  63. {
  64. void **Allocations;
  65. size_t AllocationSize;
  66. int Index;
  67. unsigned long long Iterations;
  68. pthread_mutex_t Mutex;
  69. int RandomSize;
  70. unsigned int Seed;
  71. int Status;
  72. int ThreadCount;
  73. int ThreadIndex;
  74. pthread_t *Threads;
  75. Iterations = 0;
  76. RandomSize = 0;
  77. Result->Type = PtResultIterations;
  78. Result->Status = 0;
  79. ThreadIndex = 0;
  80. Threads = NULL;
  81. //
  82. // Allocate an array to hold the allocations.
  83. //
  84. AllocationSize = sizeof(void *) * PT_MALLOC_TEST_ALLOCATION_COUNT;
  85. Allocations = malloc(AllocationSize);
  86. if (Allocations == NULL) {
  87. Result->Status = ENOMEM;
  88. goto MainEnd;
  89. }
  90. memset(Allocations, 0, AllocationSize);
  91. AllocationSize = 0;
  92. //
  93. // Figure out the allocation size based on the test.
  94. //
  95. switch (Test->TestType) {
  96. case PtTestMallocSmall:
  97. AllocationSize = PT_MALLOC_TEST_SMALL_ALLOCATION;
  98. break;
  99. case PtTestMallocLarge:
  100. AllocationSize = PT_MALLOC_TEST_LARGE_ALLOCATION;
  101. break;
  102. case PtTestMallocContended:
  103. Threads = malloc(sizeof(pthread_t) * PT_MALLOC_TEST_THREAD_COUNT);
  104. if (Threads == NULL) {
  105. Result->Status = ENOMEM;
  106. goto MainEnd;
  107. }
  108. pthread_mutex_init(&Mutex, NULL);
  109. for (ThreadIndex = 0;
  110. ThreadIndex < PT_MALLOC_TEST_THREAD_COUNT;
  111. ThreadIndex += 1) {
  112. Status = pthread_create(&(Threads[ThreadIndex]),
  113. NULL,
  114. MallocStartRoutine,
  115. &Mutex);
  116. if (Status != 0) {
  117. Result->Status = Status;
  118. goto MainEnd;
  119. }
  120. }
  121. //
  122. // Wait until all threads are spun up.
  123. //
  124. while (MallocReadyThreadCount != PT_MALLOC_TEST_THREAD_COUNT) {
  125. sleep(1);
  126. }
  127. RandomSize = 1;
  128. break;
  129. case PtTestMallocRandom:
  130. RandomSize = 1;
  131. break;
  132. default:
  133. assert(0);
  134. Result->Status = EINVAL;
  135. return;
  136. }
  137. //
  138. // Get a thread-safe seed for the random number generator.
  139. //
  140. Seed = time(NULL);
  141. //
  142. // Start the test. This snaps resource usage and starts the clock ticking.
  143. //
  144. Status = PtStartTimedTest(Test->Duration);
  145. if (Status != 0) {
  146. Result->Status = errno;
  147. goto MainEnd;
  148. }
  149. //
  150. // Measure the performance of the malloc() C library routine by counting
  151. // the number of times a memory can be allocated and freed. Randomly keep
  152. // around some allocations to make this somewhat realistic.
  153. //
  154. while (PtIsTimedTestRunning() != 0) {
  155. if (RandomSize != 0) {
  156. AllocationSize = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_LIMIT;
  157. }
  158. //
  159. // Pick a random allocation slot and either make an allocation if it is
  160. // empty or free the existing allocation.
  161. //
  162. Index = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_COUNT;
  163. if (Allocations[Index] == NULL) {
  164. Allocations[Index] = malloc(AllocationSize);
  165. if (Allocations[Index] == NULL) {
  166. Result->Status = ENOMEM;
  167. break;
  168. }
  169. } else {
  170. free(Allocations[Index]);
  171. Allocations[Index] = NULL;
  172. }
  173. Iterations += 1;
  174. }
  175. Status = PtFinishTimedTest(Result);
  176. if ((Status != 0) && (Result->Status == 0)) {
  177. Result->Status = errno;
  178. }
  179. MainEnd:
  180. switch (Test->TestType) {
  181. case PtTestMallocContended:
  182. if (Threads != NULL) {
  183. ThreadCount = ThreadIndex;
  184. for (ThreadIndex = 0; ThreadIndex < ThreadCount; ThreadIndex += 1) {
  185. pthread_cancel(Threads[ThreadIndex]);
  186. pthread_join(Threads[ThreadIndex], (void **)&Status);
  187. if ((Status != 0) && (Result->Status == 0)) {
  188. Result->Status = Status;
  189. }
  190. }
  191. free(Threads);
  192. }
  193. break;
  194. case PtTestMallocSmall:
  195. case PtTestMallocLarge:
  196. case PtTestMallocRandom:
  197. default:
  198. break;
  199. }
  200. if (Allocations != NULL) {
  201. for (Index = 0; Index < PT_MALLOC_TEST_ALLOCATION_COUNT; Index += 1) {
  202. if (Allocations[Index] != NULL) {
  203. free(Allocations[Index]);
  204. }
  205. }
  206. free(Allocations);
  207. }
  208. Result->Data.Iterations = Iterations;
  209. return;
  210. }
  211. //
  212. // --------------------------------------------------------- Internal Functions
  213. //
  214. void *
  215. MallocStartRoutine (
  216. void *Parameter
  217. )
  218. /*++
  219. Routine Description:
  220. This routine implements the start routine for a new test thread. It will
  221. wait in a loop for the test to start and then loop allocating and freeing
  222. memory regions of random size.
  223. Arguments:
  224. Parameter - Supplies a pointer to the mutex that protexts the ready thread
  225. count.
  226. Return Value:
  227. 0 on success or an errno value otherwise.
  228. --*/
  229. {
  230. void **Allocations;
  231. int AllocationSize;
  232. int Index;
  233. pthread_mutex_t *Mutex;
  234. unsigned int Seed;
  235. AllocationSize = sizeof(void *) * PT_MALLOC_TEST_ALLOCATION_COUNT;
  236. Allocations = malloc(AllocationSize);
  237. if (Allocations == NULL) {
  238. return (void *)ENOMEM;
  239. }
  240. memset(Allocations, 0, AllocationSize);
  241. Seed = time(NULL);
  242. //
  243. // Announce that this thread is ready.
  244. //
  245. Mutex = (pthread_mutex_t *)Parameter;
  246. pthread_mutex_lock(Mutex);
  247. MallocReadyThreadCount += 1;
  248. pthread_mutex_unlock(Mutex);
  249. //
  250. // Busy spin waiting for the test to start.
  251. //
  252. while (PtIsTimedTestRunning() == 0) {
  253. pthread_testcancel();
  254. }
  255. //
  256. // As long as the test is running, allocation and free blocks of memory.
  257. //
  258. while (PtIsTimedTestRunning() != 0) {
  259. AllocationSize = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_LIMIT;
  260. //
  261. // Pick a random allocation slot and either make an allocation if it is
  262. // empty or free the existing allocation.
  263. //
  264. Index = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_COUNT;
  265. if (Allocations[Index] == NULL) {
  266. Allocations[Index] = malloc(AllocationSize);
  267. if (Allocations[Index] == NULL) {
  268. break;
  269. }
  270. } else {
  271. free(Allocations[Index]);
  272. Allocations[Index] = NULL;
  273. }
  274. }
  275. for (Index = 0; Index < PT_MALLOC_TEST_ALLOCATION_COUNT; Index += 1) {
  276. if (Allocations[Index] != NULL) {
  277. free(Allocations[Index]);
  278. }
  279. }
  280. free(Allocations);
  281. return (void *)0;
  282. }