|
3 | 3 | #include "platform/global_var.h" |
4 | 4 | #include "platform/memory_util.h" |
5 | 5 | #include "platform/assert.h" |
| 6 | +#include "platform/concurrency.h" |
6 | 7 |
|
7 | 8 | #include "network_messages/entity.h" |
8 | 9 | #include "network_messages/assets.h" |
| 10 | +#include "contract_core/pre_qpi_def.h" |
| 11 | +#include "contracts/math_lib.h" |
9 | 12 |
|
10 | 13 |
|
11 | 14 | constexpr unsigned long long spectrumSizeInBytes = SPECTRUM_CAPACITY * sizeof(EntityRecord); |
12 | 15 | constexpr unsigned long long universeSizeInBytes = ASSETS_CAPACITY * sizeof(AssetRecord); |
13 | | -// TODO: check that max contract state size does not exceed size of spectrum or universe |
14 | | -constexpr unsigned long long reorgBufferSize = (spectrumSizeInBytes >= universeSizeInBytes) ? spectrumSizeInBytes : universeSizeInBytes; |
| 16 | +constexpr unsigned long long defaultCommonBuffersSize = math_lib::max(MAX_CONTRACT_STATE_SIZE, math_lib::max(spectrumSizeInBytes, universeSizeInBytes)); |
15 | 17 |
|
16 | | -// Buffer used for reorganizing spectrum and universe hash maps, currently also used as scratchpad buffer for contracts |
| 18 | +// Buffer(s) used for: |
| 19 | +// - reorganizing spectrum and universe hash maps (tick processor) |
| 20 | +// - scratchpad buffer used internally in QPI::Collection, QPI::HashMap, QPI::HashSet, |
| 21 | +// QPI::ProposalAndVotingByShareholders |
| 22 | +// (often used in contract processor which does not run concurrently with tick processor, but now also used outside |
| 23 | +// of contracts, e.g. pendingTxsPool.add() running in request processor may trigger Collection::_rebuild() which |
| 24 | +// uses scratchpad) |
| 25 | +// - building oracle transactions in processTick() in tick processor |
| 26 | +// - calculateStableComputorIndex() in tick processor |
| 27 | +// - saving and loading of logging state |
| 28 | +// - DustBurnLogger used in increaseEnergy() in tick / contract processor |
17 | 29 | // Must be large enough to fit any contract, full spectrum, and full universe! |
18 | | -GLOBAL_VAR_DECL void* reorgBuffer GLOBAL_VAR_INIT(nullptr); |
| 30 | +class CommonBuffers |
| 31 | +{ |
| 32 | +public: |
| 33 | + // Allocate common buffers. With count > 1, multiple buffers may be used concurrently. The maximum buffer size |
| 34 | + // that can be acquired is given by size. |
| 35 | + bool init(unsigned int count, unsigned long long size = defaultCommonBuffersSize) |
| 36 | + { |
| 37 | + if (!count || !size) |
| 38 | + return false; |
| 39 | + |
| 40 | + // soft limit, just to detect mistakes in usage like init(sizeof(Object)) |
| 41 | + ASSERT(count < 16); |
| 42 | + |
| 43 | + // memory layout of buffer: sub buffer pointers | sub buffer locks | sub buffer 1 | sub buffer 2 | ... |
| 44 | + unsigned char* buffer = nullptr; |
| 45 | + const unsigned long long ptrSize = count * sizeof(unsigned char*); |
| 46 | + const unsigned long long lockSize = (count + 7) / 8; |
| 47 | + const unsigned long long bufSize = count * size; |
| 48 | + |
| 49 | + if (!allocPoolWithErrorLog(L"commonBuffers", ptrSize + lockSize + bufSize, (void**)&buffer, __LINE__)) |
| 50 | + { |
| 51 | + return false; |
| 52 | + } |
19 | 53 |
|
20 | | -static bool initCommonBuffers() |
21 | | -{ |
22 | | - if (!allocPoolWithErrorLog(L"reorgBuffer", reorgBufferSize, (void**)&reorgBuffer, __LINE__)) |
| 54 | + bufferCount = count; |
| 55 | + subBufferSize = size; |
| 56 | + subBufferPtr = (unsigned char**)buffer; |
| 57 | + subBufferLock = (volatile char*)(buffer + ptrSize); |
| 58 | + unsigned char* subBuf = buffer + ptrSize + lockSize; |
| 59 | + for (unsigned int i = 0; i < count; ++i) |
| 60 | + { |
| 61 | + subBufferPtr[i] = subBuf; |
| 62 | + subBuf += size; |
| 63 | + } |
| 64 | + |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + // Free common buffers. |
| 69 | + void deinit() |
23 | 70 | { |
24 | | - return false; |
| 71 | + if (subBufferPtr) |
| 72 | + { |
| 73 | + freePool(subBufferPtr); |
| 74 | + subBufferPtr = nullptr; |
| 75 | + subBufferLock = nullptr; |
| 76 | + subBufferSize = 0; |
| 77 | + bufferCount = 0; |
| 78 | + waitingCount = 0; |
| 79 | + maxWaitingCount = 0; |
| 80 | + invalidReleaseCount = 0; |
| 81 | + } |
25 | 82 | } |
26 | 83 |
|
27 | | - return true; |
28 | | -} |
29 | | - |
30 | | -static void deinitCommonBuffers() |
31 | | -{ |
32 | | - if (reorgBuffer) |
| 84 | + // Get buffer of given size. |
| 85 | + // Returns nullptr if size is too big. Otherwise may block until buffer is available. |
| 86 | + // Does not init buffer! Buffer needs to be released with releaseBuffer() after use. |
| 87 | + void* acquireBuffer(unsigned long long size) |
| 88 | + { |
| 89 | + ASSERT(subBufferLock && subBufferPtr); |
| 90 | +#if !defined(NO_UEFI) |
| 91 | + ASSERT(size <= subBufferSize); |
| 92 | +#endif |
| 93 | + if (size > subBufferSize) |
| 94 | + return nullptr; |
| 95 | + |
| 96 | + // shortcut for default case |
| 97 | + if (TRY_ACQUIRE(subBufferLock[0])) |
| 98 | + { |
| 99 | + return subBufferPtr[0]; |
| 100 | + } |
| 101 | + |
| 102 | + long cnt = _InterlockedIncrement(&waitingCount); |
| 103 | + if (maxWaitingCount < cnt) |
| 104 | + maxWaitingCount = cnt; |
| 105 | + |
| 106 | + unsigned int i = 0; |
| 107 | + BEGIN_WAIT_WHILE(TRY_ACQUIRE(subBufferLock[i]) == false) |
| 108 | + { |
| 109 | + ++i; |
| 110 | + if (i >= bufferCount) |
| 111 | + i = 0; |
| 112 | + } |
| 113 | + END_WAIT_WHILE(); |
| 114 | + |
| 115 | + _InterlockedDecrement(&waitingCount); |
| 116 | + |
| 117 | + return subBufferPtr[i]; |
| 118 | + } |
| 119 | + |
| 120 | + // Release buffer that was acquired with acquireBuffer() before. |
| 121 | + void releaseBuffer(void* buffer) |
| 122 | + { |
| 123 | + ASSERT(subBufferLock && subBufferPtr && buffer); |
| 124 | + |
| 125 | + // shortcut for default case |
| 126 | + if (subBufferPtr[0] == buffer) |
| 127 | + { |
| 128 | + if (subBufferLock[0]) |
| 129 | + RELEASE(subBufferLock[0]); |
| 130 | + else |
| 131 | + ++invalidReleaseCount; |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + // find buffer |
| 136 | + unsigned int bufferIdx = 1; |
| 137 | + while (bufferIdx < bufferCount && subBufferPtr[bufferIdx] != buffer) |
| 138 | + ++bufferIdx; |
| 139 | + |
| 140 | + // invalid pointer passed? |
| 141 | +#if !defined(NO_UEFI) |
| 142 | + ASSERT(bufferIdx < bufferCount); |
| 143 | + ASSERT(subBufferLock[bufferIdx]); |
| 144 | +#endif |
| 145 | + if (bufferIdx >= bufferCount || !subBufferLock[bufferIdx]) |
| 146 | + { |
| 147 | + ++invalidReleaseCount; |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + // release buffer |
| 152 | + RELEASE(subBufferLock[bufferIdx]); |
| 153 | + } |
| 154 | + |
| 155 | + // Heuristics how many processors were waiting for a buffer in parallel (for deciding the count of buffers) |
| 156 | + long getMaxWaitingProcessorCount() const |
| 157 | + { |
| 158 | + return maxWaitingCount; |
| 159 | + } |
| 160 | + |
| 161 | + // Counter of invalid release calls as an indicator if debugging is needed |
| 162 | + long getInvalidReleaseCount() const |
33 | 163 | { |
34 | | - freePool(reorgBuffer); |
35 | | - reorgBuffer = nullptr; |
| 164 | + return invalidReleaseCount; |
36 | 165 | } |
| 166 | + |
| 167 | + // Returns number of buffers currently acquired |
| 168 | + unsigned int acquiredBuffers() const |
| 169 | + { |
| 170 | + unsigned int count = 0; |
| 171 | + for (unsigned int i = 0; i < bufferCount; ++i) |
| 172 | + if (subBufferLock[i]) |
| 173 | + ++count; |
| 174 | + return count; |
| 175 | + } |
| 176 | + |
| 177 | +protected: |
| 178 | + unsigned char** subBufferPtr = nullptr; |
| 179 | + volatile char* subBufferLock = nullptr; |
| 180 | + unsigned long long subBufferSize = 0; |
| 181 | + unsigned int bufferCount = 0; |
| 182 | + volatile long waitingCount = 0; |
| 183 | + long maxWaitingCount = 0; |
| 184 | + long invalidReleaseCount = 0; |
| 185 | +}; |
| 186 | + |
| 187 | + |
| 188 | +GLOBAL_VAR_DECL CommonBuffers commonBuffers; |
| 189 | + |
| 190 | + |
| 191 | +static void* __acquireScratchpad(unsigned long long size, bool initZero = true) |
| 192 | +{ |
| 193 | + void* ptr = commonBuffers.acquireBuffer(size); |
| 194 | + if (ptr && initZero) |
| 195 | + setMem(ptr, size, 0); |
| 196 | + return ptr; |
37 | 197 | } |
38 | 198 |
|
39 | | -static void* __scratchpad(unsigned long long sizeToMemsetZero) |
| 199 | +static void __releaseScratchpad(void* ptr) |
40 | 200 | { |
41 | | - ASSERT(sizeToMemsetZero <= reorgBufferSize); |
42 | | - if (sizeToMemsetZero) |
43 | | - setMem(reorgBuffer, sizeToMemsetZero, 0); |
44 | | - return reorgBuffer; |
| 201 | + commonBuffers.releaseBuffer(ptr); |
45 | 202 | } |
0 commit comments