diff --git a/CMakeLists.txt b/CMakeLists.txt index b56c3e2c..e6026004 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,15 +173,21 @@ install(FILES $ # ----------------------------------------------------------------------------- # API surface testing # ----------------------------------------------------------------------------- -add_executable(mimalloc-test test/test-api.c) -target_compile_definitions(mimalloc-test PRIVATE ${mi_defines}) -target_compile_options(mimalloc-test PRIVATE ${mi_cflags}) -target_include_directories(mimalloc-test PRIVATE include) -target_link_libraries(mimalloc-test PRIVATE mimalloc-static) +add_executable(mimalloc-test-api test/test-api.c) +target_compile_definitions(mimalloc-test-api PRIVATE ${mi_defines}) +target_compile_options(mimalloc-test-api PRIVATE ${mi_cflags}) +target_include_directories(mimalloc-test-api PRIVATE include) +target_link_libraries(mimalloc-test-api PRIVATE mimalloc-static) + +add_executable(mimalloc-test-stress test/test-stress.c) +target_compile_definitions(mimalloc-test-stress PRIVATE ${mi_defines}) +target_compile_options(mimalloc-test-stress PRIVATE ${mi_cflags}) +target_include_directories(mimalloc-test-stress PRIVATE include) +target_link_libraries(mimalloc-test-stress PRIVATE mimalloc-static) enable_testing() -add_test(test_api, mimalloc-test) - +add_test(test_api, mimalloc-test-api) +add_test(test_stress, mimalloc-test-stress) # ----------------------------------------------------------------------------- # Set override properties @@ -192,6 +198,5 @@ if (MI_OVERRIDE MATCHES "ON") # It is only possible to override malloc on Windows when building as a DLL. (src/alloc-override.c) target_compile_definitions(mimalloc-static PRIVATE MI_MALLOC_OVERRIDE) target_compile_definitions(mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE) - target_compile_definitions(mimalloc-test PRIVATE MI_MALLOC_OVERRIDE) endif() endif() diff --git a/azure-pipelines.yml b/azure-pipelines.yml index a73ac1a0..79228c41 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -23,19 +23,50 @@ jobs: solution: build/libmimalloc.sln - upload: $(Build.SourcesDirectory)/build artifact: windows + - job: displayName: Linux pool: vmImage: ubuntu-16.04 + strategy: + matrix: + Debug: + CC: gcc + CXX: g++ + BuildType: debug + cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Debug -DMI_CHECK_FULL=ON + Release: + CC: gcc + CXX: g++ + BuildType: release + cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release + Debug Clang: + CC: clang + CXX: clang++ + BuildType: debug-clang + cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Debug -DMI_CHECK_FULL=ON + Release Clang: + CC: clang + CXX: clang++ + BuildType: release-clang + cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release + steps: - task: CMake@1 inputs: - workingDirectory: 'build' - cmakeArgs: .. - - script: make -j$(nproc) -C build - - upload: $(Build.SourcesDirectory)/build - artifact: ubuntu + workingDirectory: $(BuildType) + cmakeArgs: .. $(cmakeExtraArgs) + + - script: make -j$(nproc) -C $(BuildType) + displayName: Make + + - script: make test -C $(BuildType) + displayName: Ctest + + - upload: $(Build.SourcesDirectory)/$(BuildType) + artifact: ubuntu-$(BuildType) + - job: displayName: macOS pool: diff --git a/ide/vs2017/mimalloc-test-stress.vcxproj b/ide/vs2017/mimalloc-test-stress.vcxproj new file mode 100644 index 00000000..5ef92d86 --- /dev/null +++ b/ide/vs2017/mimalloc-test-stress.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {FEF7958F-750E-4C21-A04D-22707CC66878} + mimalloc-test-stress + 10.0.17134.0 + mimalloc-test-stress + + + + Application + true + v141 + + + Application + false + v141 + true + + + Application + true + v141 + + + Application + false + v141 + true + + + + + + + + + + + + + + + + + + + + + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + true + true + ..\..\include + + + Console + + + + + Level3 + Disabled + true + true + ..\..\include + + + Console + + + + + Level3 + MaxSpeed + true + true + true + true + ..\..\include + %(PreprocessorDefinitions);NDEBUG + + + true + true + Console + + + + + Level3 + MaxSpeed + true + true + true + true + ..\..\include + %(PreprocessorDefinitions);NDEBUG + + + true + true + Console + + + + + + + + + {abb5eae7-b3e6-432e-b636-333449892ea6} + + + + + + diff --git a/ide/vs2017/mimalloc-test-stress.vcxproj.filters b/ide/vs2017/mimalloc-test-stress.vcxproj.filters new file mode 100644 index 00000000..b857ea52 --- /dev/null +++ b/ide/vs2017/mimalloc-test-stress.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + diff --git a/ide/vs2017/mimalloc.sln b/ide/vs2017/mimalloc.sln index f4860d93..aeab6b88 100644 --- a/ide/vs2017/mimalloc.sln +++ b/ide/vs2017/mimalloc.sln @@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mimalloc-override", "mimall EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mimalloc-override-test", "mimalloc-override-test.vcxproj", "{FEF7868F-750E-4C21-A04D-22707CC66879}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mimalloc-test-stress", "mimalloc-test-stress.vcxproj", "{FEF7958F-750E-4C21-A04D-22707CC66878}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -51,6 +53,14 @@ Global {FEF7868F-750E-4C21-A04D-22707CC66879}.Release|x64.Build.0 = Release|x64 {FEF7868F-750E-4C21-A04D-22707CC66879}.Release|x86.ActiveCfg = Release|Win32 {FEF7868F-750E-4C21-A04D-22707CC66879}.Release|x86.Build.0 = Release|Win32 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Debug|x64.ActiveCfg = Debug|x64 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Debug|x64.Build.0 = Debug|x64 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Debug|x86.ActiveCfg = Debug|Win32 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Debug|x86.Build.0 = Debug|Win32 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Release|x64.ActiveCfg = Release|x64 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Release|x64.Build.0 = Release|x64 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Release|x86.ActiveCfg = Release|Win32 + {FEF7958F-750E-4C21-A04D-22707CC66878}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/os.c b/src/os.c index 8d3e37e5..0fc6213c 100644 --- a/src/os.c +++ b/src/os.c @@ -93,7 +93,7 @@ void _mi_os_init(void) { if (si.dwAllocationGranularity > 0) os_alloc_granularity = si.dwAllocationGranularity; // get the VirtualAlloc2 function HINSTANCE hDll; - hDll = LoadLibrary("kernelbase.dll"); + hDll = LoadLibrary(TEXT("kernelbase.dll")); if (hDll != NULL) { // use VirtualAlloc2FromApp as it is available to Windows store apps pVirtualAlloc2 = (VirtualAlloc2Ptr)GetProcAddress(hDll, "VirtualAlloc2FromApp"); @@ -110,7 +110,7 @@ void _mi_os_init(void) { ok = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token); if (ok) { TOKEN_PRIVILEGES tp; - ok = LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &tp.Privileges[0].Luid); + ok = LookupPrivilegeValue(NULL, TEXT("SeLockMemoryPrivilege"), &tp.Privileges[0].Luid); if (ok) { tp.PrivilegeCount = 1; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; diff --git a/src/segment.c b/src/segment.c index 01d0d04f..aa33faf2 100644 --- a/src/segment.c +++ b/src/segment.c @@ -92,6 +92,10 @@ static void mi_segment_enqueue(mi_segment_queue_t* queue, mi_segment_t* segment) } } +/* ----------------------------------------------------------- + Invariant checking +----------------------------------------------------------- */ + #if (MI_DEBUG > 1) static size_t mi_segment_pagesize(mi_segment_t* segment) { return ((size_t)1 << segment->page_shift); diff --git a/test/test-stress.c b/test/test-stress.c new file mode 100644 index 00000000..6afde306 --- /dev/null +++ b/test/test-stress.c @@ -0,0 +1,166 @@ +/* ---------------------------------------------------------------------------- +Copyright (c) 2018,2019 Microsoft Research, Daan Leijen +This is free software; you can redistribute it and/or modify it under the +terms of the MIT license. A copy of the license can be found in the file +"LICENSE" at the root of this distribution. +-----------------------------------------------------------------------------*/ + +/* This is a stress test for the allocator, using multiple threads and + transferring objects between threads. This is not a typical workload + but uses a random size distribution. Do not use this test as a benchmark! + Note: pthreads uses mimalloc to allocate stacks and thus not all + memory is freed at the end. (usually the 320 byte chunks). +*/ + +#include +#include +#include +#include "mimalloc.h" +#include "mimalloc-internal.h" +#include "mimalloc-atomic.h" + +#define N (10) // scaling factor +#define THREADS (32) +#define TRANSFERS (1000) + +static volatile void* transfer[TRANSFERS]; + +#if (MI_INTPTR_SIZE==8) +const uintptr_t cookie = 0xbf58476d1ce4e5b9UL; +#else +const uintptr_t cookie = 0x1ce4e5b9UL; +#endif + + +static void* alloc_items(size_t items) { + if ((rand()%100) == 0) items *= 100; // 1% huge objects; + if (items==40) items++; // pthreads uses that size for stack increases + uintptr_t* p = mi_mallocn_tp(uintptr_t,items); + for (uintptr_t i = 0; i < items; i++) p[i] = (items - i) ^ cookie; + return p; +} + +static void free_items(void* p) { + if (p != NULL) { + uintptr_t* q = (uintptr_t*)p; + uintptr_t items = (q[0] ^ cookie); + for (uintptr_t i = 0; i < items; i++) { + if ((q[i]^cookie) != items - i) { + fprintf(stderr,"memory corruption at block %p at %zu\n", p, i); + abort(); + } + } + } + mi_free(p); +} + + +static void stress(intptr_t tid) { + const size_t max_item = 128; // in words + const size_t max_item_retained = 10*max_item; + size_t allocs = 80*N*(tid%8 + 1); // some threads do more + size_t retain = allocs/2; + void** data = NULL; + size_t data_size = 0; + size_t data_top = 0; + void** retained = mi_mallocn_tp(void*,retain); + size_t retain_top = 0; + + while (allocs>0 || retain>0) { + if (retain == 0 || ((rand()%4 == 0) && allocs > 0)) { + // 75% alloc + allocs--; + if (data_top >= data_size) { + data_size += 100000; + data = mi_reallocn_tp(data, void*, data_size); + } + data[data_top++] = alloc_items((rand() % max_item) + 1); + } + else { + // 25% retain + retained[retain_top++] = alloc_items( 10*((rand() % max_item_retained) + 1) ); + retain--; + } + if ((rand()%3)!=0 && data_top > 0) { + // 66% free previous alloc + size_t idx = rand() % data_top; + free_items(data[idx]); + data[idx]=NULL; + } + if ((tid%2)==0 && (rand()%4)==0 && data_top > 0) { + // 25% transfer-swap of half the threads + size_t data_idx = rand() % data_top; + size_t transfer_idx = rand() % TRANSFERS; + void* p = data[data_idx]; + void* q = mi_atomic_exchange_ptr(&transfer[transfer_idx],p); + data[data_idx] = q; + } + } + // free everything that is left + for (size_t i = 0; i < retain_top; i++) { + free_items(retained[i]); + } + for (size_t i = 0; i < data_top; i++) { + free_items(data[i]); + } + mi_free(retained); + mi_free(data); +} + +static void run_os_threads(); + +int main() { + srand(42); + memset((void*)transfer,0,TRANSFERS*sizeof(void*)); + run_os_threads(); + for (int i = 0; i < TRANSFERS; i++) { + free_items((void*)transfer[i]); + } + mi_collect(false); + mi_stats_print(NULL); + return 0; +} + + +#ifdef _WIN32 + +#include + +static DWORD WINAPI thread_entry(LPVOID param) { + stress((intptr_t)param); + return 0; +} + +static void run_os_threads() { + DWORD tids[THREADS]; + HANDLE thandles[THREADS]; + for(intptr_t i = 0; i < THREADS; i++) { + thandles[i] = CreateThread(0,4096,&thread_entry,(void*)(i),0,&tids[i]); + } + for (int i = 0; i < THREADS; i++) { + WaitForSingleObject(thandles[i], INFINITE); + } +} + +#else + +#include + +static void* thread_entry( void* param ) { + stress((uintptr_t)param); + return NULL; +} + +static void run_os_threads() { + pthread_t threads[THREADS]; + memset(threads,0,sizeof(pthread_t)*THREADS); + //pthread_setconcurrency(THREADS); + for(uintptr_t i = 0; i < THREADS; i++) { + pthread_create(&threads[i], NULL, &thread_entry, (void*)i); + } + for (size_t i = 0; i < THREADS; i++) { + pthread_join(threads[i], NULL); + } +} + +#endif