123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- /*++
- Copyright (c) 2015 Minoca Corp. All Rights Reserved
- Module Name:
- malloc.c
- Abstract:
- This module implements the performance benchmark tests for the malloc() C
- library routine.
- Author:
- Chris Stevens 8-May-2015
- Environment:
- User
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <assert.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <string.h>
- #include "perftest.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define PT_MALLOC_TEST_SMALL_ALLOCATION 32
- #define PT_MALLOC_TEST_LARGE_ALLOCATION (128 * 1024)
- #define PT_MALLOC_TEST_ALLOCATION_LIMIT (256 * 1024)
- #define PT_MALLOC_TEST_ALLOCATION_COUNT 32
- #define PT_MALLOC_TEST_THREAD_COUNT 8
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- void *
- MallocStartRoutine (
- void *Parameter
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- volatile int MallocReadyThreadCount;
- //
- // ------------------------------------------------------------------ Functions
- //
- void
- MallocMain (
- PPT_TEST_INFORMATION Test,
- PPT_TEST_RESULT Result
- )
- /*++
- Routine Description:
- This routine performs the malloc performance benchmark tests.
- Arguments:
- Test - Supplies a pointer to the performance test being executed.
- Result - Supplies a pointer to a performance test result structure that
- receives the tests results.
- Return Value:
- None.
- --*/
- {
- void **Allocations;
- size_t AllocationSize;
- int Index;
- unsigned long long Iterations;
- pthread_mutex_t Mutex;
- int RandomSize;
- unsigned int Seed;
- int Status;
- int ThreadCount;
- int ThreadIndex;
- pthread_t *Threads;
- Iterations = 0;
- RandomSize = 0;
- Result->Type = PtResultIterations;
- Result->Status = 0;
- ThreadIndex = 0;
- Threads = NULL;
- //
- // Allocate an array to hold the allocations.
- //
- AllocationSize = sizeof(void *) * PT_MALLOC_TEST_ALLOCATION_COUNT;
- Allocations = malloc(AllocationSize);
- if (Allocations == NULL) {
- Result->Status = ENOMEM;
- goto MainEnd;
- }
- memset(Allocations, 0, AllocationSize);
- AllocationSize = 0;
- //
- // Figure out the allocation size based on the test.
- //
- switch (Test->TestType) {
- case PtTestMallocSmall:
- AllocationSize = PT_MALLOC_TEST_SMALL_ALLOCATION;
- break;
- case PtTestMallocLarge:
- AllocationSize = PT_MALLOC_TEST_LARGE_ALLOCATION;
- break;
- case PtTestMallocContended:
- Threads = malloc(sizeof(pthread_t) * PT_MALLOC_TEST_THREAD_COUNT);
- if (Threads == NULL) {
- Result->Status = ENOMEM;
- goto MainEnd;
- }
- pthread_mutex_init(&Mutex, NULL);
- for (ThreadIndex = 0;
- ThreadIndex < PT_MALLOC_TEST_THREAD_COUNT;
- ThreadIndex += 1) {
- Status = pthread_create(&(Threads[ThreadIndex]),
- NULL,
- MallocStartRoutine,
- &Mutex);
- if (Status != 0) {
- Result->Status = Status;
- goto MainEnd;
- }
- }
- //
- // Wait until all threads are spun up.
- //
- while (MallocReadyThreadCount != PT_MALLOC_TEST_THREAD_COUNT) {
- sleep(1);
- }
- RandomSize = 1;
- break;
- case PtTestMallocRandom:
- RandomSize = 1;
- break;
- default:
- assert(0);
- Result->Status = EINVAL;
- return;
- }
- //
- // Get a thread-safe seed for the random number generator.
- //
- Seed = time(NULL);
- //
- // Start the test. This snaps resource usage and starts the clock ticking.
- //
- Status = PtStartTimedTest(Test->Duration);
- if (Status != 0) {
- Result->Status = errno;
- goto MainEnd;
- }
- //
- // Measure the performance of the malloc() C library routine by counting
- // the number of times a memory can be allocated and freed. Randomly keep
- // around some allocations to make this somewhat realistic.
- //
- while (PtIsTimedTestRunning() != 0) {
- if (RandomSize != 0) {
- AllocationSize = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_LIMIT;
- }
- //
- // Pick a random allocation slot and either make an allocation if it is
- // empty or free the existing allocation.
- //
- Index = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_COUNT;
- if (Allocations[Index] == NULL) {
- Allocations[Index] = malloc(AllocationSize);
- if (Allocations[Index] == NULL) {
- Result->Status = ENOMEM;
- break;
- }
- } else {
- free(Allocations[Index]);
- Allocations[Index] = NULL;
- }
- Iterations += 1;
- }
- Status = PtFinishTimedTest(Result);
- if ((Status != 0) && (Result->Status == 0)) {
- Result->Status = errno;
- }
- MainEnd:
- switch (Test->TestType) {
- case PtTestMallocContended:
- if (Threads != NULL) {
- ThreadCount = ThreadIndex;
- for (ThreadIndex = 0; ThreadIndex < ThreadCount; ThreadIndex += 1) {
- pthread_cancel(Threads[ThreadIndex]);
- pthread_join(Threads[ThreadIndex], (void **)&Status);
- if ((Status != 0) && (Result->Status == 0)) {
- Result->Status = Status;
- }
- }
- free(Threads);
- }
- break;
- case PtTestMallocSmall:
- case PtTestMallocLarge:
- case PtTestMallocRandom:
- default:
- break;
- }
- if (Allocations != NULL) {
- for (Index = 0; Index < PT_MALLOC_TEST_ALLOCATION_COUNT; Index += 1) {
- if (Allocations[Index] != NULL) {
- free(Allocations[Index]);
- }
- }
- free(Allocations);
- }
- Result->Data.Iterations = Iterations;
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- void *
- MallocStartRoutine (
- void *Parameter
- )
- /*++
- Routine Description:
- This routine implements the start routine for a new test thread. It will
- wait in a loop for the test to start and then loop allocating and freeing
- memory regions of random size.
- Arguments:
- Parameter - Supplies a pointer to the mutex that protexts the ready thread
- count.
- Return Value:
- 0 on success or an errno value otherwise.
- --*/
- {
- void **Allocations;
- int AllocationSize;
- int Index;
- pthread_mutex_t *Mutex;
- unsigned int Seed;
- AllocationSize = sizeof(void *) * PT_MALLOC_TEST_ALLOCATION_COUNT;
- Allocations = malloc(AllocationSize);
- if (Allocations == NULL) {
- return (void *)ENOMEM;
- }
- memset(Allocations, 0, AllocationSize);
- Seed = time(NULL);
- //
- // Announce that this thread is ready.
- //
- Mutex = (pthread_mutex_t *)Parameter;
- pthread_mutex_lock(Mutex);
- MallocReadyThreadCount += 1;
- pthread_mutex_unlock(Mutex);
- //
- // Busy spin waiting for the test to start.
- //
- while (PtIsTimedTestRunning() == 0) {
- pthread_testcancel();
- }
- //
- // As long as the test is running, allocation and free blocks of memory.
- //
- while (PtIsTimedTestRunning() != 0) {
- AllocationSize = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_LIMIT;
- //
- // Pick a random allocation slot and either make an allocation if it is
- // empty or free the existing allocation.
- //
- Index = rand_r(&Seed) % PT_MALLOC_TEST_ALLOCATION_COUNT;
- if (Allocations[Index] == NULL) {
- Allocations[Index] = malloc(AllocationSize);
- if (Allocations[Index] == NULL) {
- break;
- }
- } else {
- free(Allocations[Index]);
- Allocations[Index] = NULL;
- }
- }
- for (Index = 0; Index < PT_MALLOC_TEST_ALLOCATION_COUNT; Index += 1) {
- if (Allocations[Index] != NULL) {
- free(Allocations[Index]);
- }
- }
- free(Allocations);
- return (void *)0;
- }
|