| 1 | // Copyright 2009-2021 Intel Corporation |
|---|---|
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #include "alloc.h" |
| 5 | #include "../../common/sys/thread.h" |
| 6 | #if defined(APPLE) && defined(__aarch64__) |
| 7 | #include "../../common/sys/barrier.h" |
| 8 | #endif |
| 9 | |
| 10 | namespace embree |
| 11 | { |
| 12 | __thread FastAllocator::ThreadLocal2* FastAllocator::thread_local_allocator2 = nullptr; |
| 13 | SpinLock FastAllocator::s_thread_local_allocators_lock; |
| 14 | std::vector<std::unique_ptr<FastAllocator::ThreadLocal2>> FastAllocator::s_thread_local_allocators; |
| 15 | |
| 16 | struct fast_allocator_regression_test : public RegressionTest |
| 17 | { |
| 18 | BarrierSys barrier; |
| 19 | std::atomic<size_t> numFailed; |
| 20 | std::unique_ptr<FastAllocator> alloc; |
| 21 | |
| 22 | fast_allocator_regression_test() |
| 23 | : RegressionTest("fast_allocator_regression_test"), numFailed(0) |
| 24 | { |
| 25 | registerRegressionTest(this); |
| 26 | } |
| 27 | |
| 28 | static void thread_alloc(fast_allocator_regression_test* This) |
| 29 | { |
| 30 | FastAllocator::CachedAllocator threadalloc = This->alloc->getCachedAllocator(); |
| 31 | |
| 32 | size_t* ptrs[1000]; |
| 33 | for (size_t j=0; j<1000; j++) |
| 34 | { |
| 35 | This->barrier.wait(); |
| 36 | for (size_t i=0; i<1000; i++) { |
| 37 | ptrs[i] = (size_t*) threadalloc.malloc0(sizeof(size_t)+(i%32)); |
| 38 | *ptrs[i] = size_t(threadalloc.talloc0) + i; |
| 39 | } |
| 40 | for (size_t i=0; i<1000; i++) { |
| 41 | if (*ptrs[i] != size_t(threadalloc.talloc0) + i) |
| 42 | This->numFailed++; |
| 43 | } |
| 44 | This->barrier.wait(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | bool run () |
| 49 | { |
| 50 | alloc = make_unique(new FastAllocator(nullptr,false)); |
| 51 | numFailed.store(0); |
| 52 | |
| 53 | size_t numThreads = getNumberOfLogicalThreads(); |
| 54 | barrier.init(numThreads+1); |
| 55 | |
| 56 | /* create threads */ |
| 57 | std::vector<thread_t> threads; |
| 58 | for (size_t i=0; i<numThreads; i++) |
| 59 | threads.push_back(createThread((thread_func)thread_alloc,this)); |
| 60 | |
| 61 | /* run test */ |
| 62 | for (size_t i=0; i<1000; i++) |
| 63 | { |
| 64 | alloc->reset(); |
| 65 | barrier.wait(); |
| 66 | barrier.wait(); |
| 67 | } |
| 68 | |
| 69 | /* destroy threads */ |
| 70 | for (size_t i=0; i<numThreads; i++) |
| 71 | join(threads[i]); |
| 72 | |
| 73 | alloc = nullptr; |
| 74 | |
| 75 | return numFailed == 0; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | fast_allocator_regression_test fast_allocator_regression; |
| 80 | } |
| 81 | |
| 82 | |
| 83 |