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