Hash :
684ff60b
Author :
Date :
2022-06-21T10:52:31
Vulkan: Add shared ring buffer cmd alloc feature
* Added RingBufferAllocator.cpp with implementation.
* Main classes:
* RingBufferAllocator (fast allocation with bulk deallocation)
* SharedRingBufferAllocator (wrapper to help with shared use and
multiple threads)
* Implemented "angle_enable_vulkan_shared_ring_buffer_cmd_alloc"
feature. (Disabled by default)
* Details (from the original CL)
* The angle::PoolAllocator replaced with
angle::RingBufferAllocator.
* Before, there was separate angle::PoolAllocator per each
CommandBufferHelper. Now, a single angle::RingBufferAllocator
is shared between multiple CommandBufferHelper objects.
* Commands data from multiple CommandBufferHelpers is
tightly packed without fragmentation.
* Significantly less memory overhead, observed with enabled
async queue.
* Moved the parts of the code related to the allocators into the
classes in the new AllocatorHelperPool and AllocatorHelperRing files
for better management. The allocator can be switched by changing the
following BUILD flag:
`angle_enable_vulkan_shared_ring_buffer_cmd_alloc`
* It is connected to the following macro:
ANGLE_ENABLE_VULKAN_SHARED_RING_BUFFER_CMD_ALLOC
* The two main allocator classes in each file are aliased as:
* SecondaryCommandBlockAllocator (in CommandBufferHelper objects)
* SecondaryCommandBlockPool (in SecondaryCommandBuffer)
* Also added placeholder functions for VulkanSecondaryCommandBuffer.
* Added descriptions regarding the two allocators.
* renderer/vulkan/doc/Allocators.md
Credit: Original CL authored by Igor Nazarov <i.nazarov@samsung.com>
Bug: angleproject:6401
Bug: b/256666069
Change-Id: I0f24793eef6334bf4ff8e327b9665338807dad37
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3715968
Reviewed-by: Charlie Lao <cclao@google.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// RingBufferAllocator.cpp:
// Implements classes used for ring buffer allocators.
//
#include "common/RingBufferAllocator.h"
namespace angle
{
// RingBufferAllocator implementation.
RingBufferAllocator::RingBufferAllocator(RingBufferAllocator &&other)
{
*this = std::move(other);
}
RingBufferAllocator &RingBufferAllocator::operator=(RingBufferAllocator &&other)
{
mOldBuffers = std::move(other.mOldBuffers);
mBuffer = std::move(other.mBuffer);
mDataBegin = other.mDataBegin;
mDataEnd = other.mDataEnd;
mFragmentEnd = other.mFragmentEnd;
mFragmentEndR = other.mFragmentEndR;
mFragmentReserve = other.mFragmentReserve;
mMinCapacity = other.mMinCapacity;
mCurrentCapacity = other.mCurrentCapacity;
mAllocationMargin = other.mAllocationMargin;
mDecaySpeedFactor = other.mDecaySpeedFactor;
ASSERT(other.mOldBuffers.size() == 0);
ASSERT(other.mBuffer.getStorageSize() == 0);
other.mBuffer.resetId();
other.mDataBegin = nullptr;
other.mDataEnd = nullptr;
other.mFragmentEnd = nullptr;
other.mFragmentEndR = nullptr;
other.mFragmentReserve = 0;
other.mMinCapacity = 0;
other.mCurrentCapacity = 0;
other.mAllocationMargin = 0;
other.mDecaySpeedFactor = 0;
return *this;
}
void RingBufferAllocator::reset()
{
mListener = nullptr;
mFragmentReserve = 0;
mDecaySpeedFactor = kDefaultDecaySpeedFactor;
mMinCapacity = kMinRingBufferAllocatiorCapacity;
resize(mMinCapacity);
mOldBuffers.clear();
}
void RingBufferAllocator::setListener(RingBufferAllocateListener *listener)
{
ASSERT(!mListener || !listener);
mListener = listener;
}
void RingBufferAllocator::setDecaySpeedFactor(uint32_t decaySpeedFactor)
{
ASSERT(valid());
mDecaySpeedFactor = std::max(decaySpeedFactor, 1u);
}
RingBufferAllocatorCheckPoint RingBufferAllocator::getReleaseCheckPoint() const
{
ASSERT(valid());
RingBufferAllocatorCheckPoint result;
result.mBufferId = mBuffer.getId();
result.mReleasePtr = mDataEnd;
return result;
}
void RingBufferAllocator::release(const RingBufferAllocatorCheckPoint &checkPoint)
{
ASSERT(valid());
ASSERT(checkPoint.valid());
if (mOldBuffers.size() > 0)
{
// mOldBuffers are sorted by id
int removeCount = 0;
for (uint32_t i = 0;
(i < mOldBuffers.size()) && (mOldBuffers[i].getId() < checkPoint.mBufferId); ++i)
{
++removeCount;
}
mOldBuffers.erase(mOldBuffers.begin(), mOldBuffers.begin() + removeCount);
}
if (checkPoint.mBufferId == mBuffer.getId())
{
const uint32_t allocatedBefore = getNumAllocatedInBuffer();
release(checkPoint.mReleasePtr);
if (allocatedBefore >= mAllocationMargin)
{
if ((mCurrentCapacity > mMinCapacity) && (allocatedBefore * 6 <= mCurrentCapacity))
{
resize(std::max(allocatedBefore * 3, mMinCapacity));
}
else
{
mAllocationMargin = mCurrentCapacity;
}
}
else
{
const uint64_t numReleased = (allocatedBefore - getNumAllocatedInBuffer());
const uint64_t distanceToMargin = (mAllocationMargin - allocatedBefore);
mAllocationMargin -=
std::max(static_cast<uint32_t>(numReleased * distanceToMargin / mAllocationMargin /
mDecaySpeedFactor),
1u);
}
}
}
void RingBufferAllocator::setFragmentReserve(uint32_t reserve)
{
ASSERT(valid());
mFragmentReserve = reserve;
mFragmentEndR = mBuffer.decClamped(mFragmentEnd, mFragmentReserve);
}
uint8_t *RingBufferAllocator::allocateInNewFragment(uint32_t size)
{
if (mListener)
{
mListener->onRingBufferFragmentEnd();
}
uint8_t *newFragmentBegin = nullptr;
if (mFragmentEnd != mDataBegin)
{
newFragmentBegin = mBuffer.data();
uint8_t *const newFragmentEnd = mDataBegin;
uint8_t *const newFragmentEndR = mBuffer.decClamped(newFragmentEnd, mFragmentReserve);
// It should wrap around only if it can allocate.
if (newFragmentEndR - newFragmentBegin >= static_cast<ptrdiff_t>(size))
{
mDataEnd = newFragmentBegin;
mFragmentEnd = newFragmentEnd;
mFragmentEndR = newFragmentEndR;
if (mListener)
{
mListener->onRingBufferNewFragment();
}
mDataEnd = newFragmentBegin + size;
return newFragmentBegin;
}
}
resize(std::max(mCurrentCapacity + mCurrentCapacity / 2, size + mFragmentReserve));
if (mListener)
{
mListener->onRingBufferNewFragment();
}
ASSERT(mFragmentEndR - mDataEnd >= static_cast<ptrdiff_t>(size));
newFragmentBegin = mDataEnd;
mDataEnd = newFragmentBegin + size;
return newFragmentBegin;
}
void RingBufferAllocator::resize(uint32_t newCapacity)
{
ASSERT(newCapacity >= mMinCapacity);
if (mBuffer.getId() != 0)
{
mOldBuffers.emplace_back(std::move(mBuffer));
}
mCurrentCapacity = newCapacity;
mBuffer.incrementId();
mBuffer.resize(mCurrentCapacity);
resetPointers();
mAllocationMargin = mCurrentCapacity;
}
void RingBufferAllocator::release(uint8_t *releasePtr)
{
if (releasePtr == mDataEnd)
{
// Ensures "mDataEnd == mBuffer.data()" with 0 allocations.
resetPointers();
return;
}
if (mDataBegin == mFragmentEnd)
{
ASSERT((releasePtr >= mBuffer.data() && releasePtr < mDataEnd) ||
(releasePtr >= mDataBegin && releasePtr <= mBuffer.data() + mCurrentCapacity));
if (releasePtr < mDataBegin)
{
mFragmentEnd = mBuffer.data() + mCurrentCapacity;
}
else
{
mFragmentEnd = releasePtr;
}
mFragmentEndR = mBuffer.decClamped(mFragmentEnd, mFragmentReserve);
}
else
{
ASSERT(releasePtr >= mDataBegin && releasePtr < mDataEnd);
}
mDataBegin = releasePtr;
}
uint32_t RingBufferAllocator::getNumAllocatedInBuffer() const
{
// 2 free fragments: [mBuffer.begin, mDataBegin) [mDataEnd, mBuffer.end);
// 1 used fragment: [DataBegin, DataEnd)
if (mFragmentEnd != mDataBegin)
{
ASSERT(mDataEnd >= mDataBegin);
return static_cast<uint32_t>(mDataEnd - mDataBegin);
}
// 1 free fragment: [mDataEnd, mDataBegin)
// 2 used fragments: [mBuffer.begin, mDataEnd) [mDataBegin, mBuffer.end)
ASSERT(mDataBegin >= mDataEnd);
return (mCurrentCapacity - static_cast<uint32_t>(mDataBegin - mDataEnd));
}
void RingBufferAllocator::resetPointers()
{
mDataBegin = mBuffer.data();
mDataEnd = mDataBegin;
mFragmentEnd = mDataEnd + mCurrentCapacity;
mFragmentEndR = mBuffer.decClamped(mFragmentEnd, mFragmentReserve);
}
// SharedRingBufferAllocator implementation.
SharedRingBufferAllocator::SharedRingBufferAllocator()
{
mAllocator.reset();
}
SharedRingBufferAllocator::~SharedRingBufferAllocator()
{
#if defined(ANGLE_ENABLE_ASSERTS)
ASSERT(!mSharedCP || mSharedCP->mRefCount == 1);
#endif
SafeDelete(mSharedCP);
}
SharedRingBufferAllocatorCheckPoint *SharedRingBufferAllocator::acquireSharedCP()
{
if (!mSharedCP)
{
mSharedCP = new SharedRingBufferAllocatorCheckPoint();
}
#if defined(ANGLE_ENABLE_ASSERTS)
ASSERT(++mSharedCP->mRefCount > 1);
// Must always be 1 ref before
#endif
return mSharedCP;
}
void SharedRingBufferAllocator::releaseToSharedCP()
{
ASSERT(mSharedCP);
const RingBufferAllocatorCheckPoint releaseCP = mSharedCP->pop();
if (releaseCP.valid())
{
mAllocator.release(releaseCP);
}
}
void SharedRingBufferAllocatorCheckPoint::releaseAndUpdate(RingBufferAllocatorCheckPoint *newValue)
{
ASSERT(newValue && newValue->valid());
#if defined(ANGLE_ENABLE_ASSERTS)
ASSERT(--mRefCount >= 1);
// Must always remain 1 ref
#endif
{
std::lock_guard<std::mutex> lock(mMutex);
mValue = *newValue;
}
newValue->reset();
}
RingBufferAllocatorCheckPoint SharedRingBufferAllocatorCheckPoint::pop()
{
std::lock_guard<std::mutex> lock(mMutex);
RingBufferAllocatorCheckPoint value = mValue;
mValue.reset();
return value;
}
} // namespace angle