Skip to content

Commit 6165806

Browse files
Reuse command buffers in L0 command list
Signed-off-by: Lukasz Jobczyk <[email protected]>
1 parent 64aec8d commit 6165806

19 files changed

+151
-103
lines changed

level_zero/core/source/cmdlist/cmdlist_hw.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ ze_result_t CommandListCoreFamily<gfxCoreFamily>::initialize(Device *device, NEO
134134

135135
commandContainer.setReservedSshSize(getReserveSshSize());
136136
DeviceImp *deviceImp = static_cast<DeviceImp *>(device);
137-
auto returnValue = commandContainer.initialize(deviceImp->getActiveDevice());
137+
auto returnValue = commandContainer.initialize(deviceImp->getActiveDevice(), deviceImp->allocationsForReuse.get());
138138
ze_result_t returnType = parseErrorCode(returnValue);
139139
if (returnType == ZE_RESULT_SUCCESS) {
140140
if (!isCopyOnly()) {

level_zero/core/test/unit_tests/sources/debugger/test_l0_debugger.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ HWTEST_F(L0DebuggerSimpleTest, givenNotChangedSurfaceStateWhenCapturingSBAThenNo
13851385
debugger->sbaTrackingGpuVa.address = 0x45670000;
13861386

13871387
NEO::CommandContainer container;
1388-
container.initialize(neoDevice);
1388+
container.initialize(neoDevice, nullptr);
13891389

13901390
NEO::Debugger::SbaAddresses sba = {};
13911391
sba.SurfaceStateBaseAddress = 0x123456000;
@@ -1408,7 +1408,7 @@ HWTEST_F(L0DebuggerSimpleTest, givenChangedBaseAddressesWhenCapturingSBAThenNoTr
14081408
debugger->sbaTrackingGpuVa.address = 0x45670000;
14091409
{
14101410
NEO::CommandContainer container;
1411-
container.initialize(neoDevice);
1411+
container.initialize(neoDevice, nullptr);
14121412

14131413
NEO::Debugger::SbaAddresses sba = {};
14141414
sba.SurfaceStateBaseAddress = 0x123456000;
@@ -1421,7 +1421,7 @@ HWTEST_F(L0DebuggerSimpleTest, givenChangedBaseAddressesWhenCapturingSBAThenNoTr
14211421

14221422
{
14231423
NEO::CommandContainer container;
1424-
container.initialize(neoDevice);
1424+
container.initialize(neoDevice, nullptr);
14251425

14261426
NEO::Debugger::SbaAddresses sba = {};
14271427
sba.GeneralStateBaseAddress = 0x123456000;
@@ -1434,7 +1434,7 @@ HWTEST_F(L0DebuggerSimpleTest, givenChangedBaseAddressesWhenCapturingSBAThenNoTr
14341434

14351435
{
14361436
NEO::CommandContainer container;
1437-
container.initialize(neoDevice);
1437+
container.initialize(neoDevice, nullptr);
14381438

14391439
NEO::Debugger::SbaAddresses sba = {};
14401440
sba.BindlessSurfaceStateBaseAddress = 0x123456000;

opencl/test/unit_test/source_level_debugger/source_level_debugger_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ TEST(SourceLevelDebugger, whenCaptureSBACalledThenNoCommandsAreAddedToStream) {
684684
MockSourceLevelDebugger debugger;
685685

686686
CommandContainer container;
687-
container.initialize(device.get());
687+
container.initialize(device.get(), nullptr);
688688

689689
NEO::Debugger::SbaAddresses sbaAddresses = {};
690690
debugger.captureStateBaseAddress(container, sbaAddresses);

shared/source/command_container/cmdcontainer.cpp

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "shared/source/helpers/heap_helper.h"
1717
#include "shared/source/helpers/hw_helper.h"
1818
#include "shared/source/indirect_heap/indirect_heap.h"
19+
#include "shared/source/memory_manager/allocations_list.h"
1920
#include "shared/source/memory_manager/memory_manager.h"
2021

2122
namespace NEO {
@@ -26,11 +27,7 @@ CommandContainer::~CommandContainer() {
2627
return;
2728
}
2829

29-
auto memoryManager = device->getMemoryManager();
30-
31-
for (auto *alloc : cmdBufferAllocations) {
32-
memoryManager->freeGraphicsMemory(alloc);
33-
}
30+
this->handleCmdBufferAllocations(0u);
3431

3532
for (auto allocationIndirectHeap : allocationIndirectHeaps) {
3633
if (heapHelper) {
@@ -44,19 +41,14 @@ CommandContainer::~CommandContainer() {
4441
}
4542
}
4643

47-
ErrorCode CommandContainer::initialize(Device *device) {
44+
ErrorCode CommandContainer::initialize(Device *device, AllocationsList *reusableAllocationList) {
4845
this->device = device;
46+
this->reusableAllocationList = reusableAllocationList;
4947

5048
size_t alignedSize = alignUp<size_t>(totalCmdBufferSize, MemoryConstants::pageSize64k);
51-
AllocationProperties properties{device->getRootDeviceIndex(),
52-
true /* allocateMemory*/,
53-
alignedSize,
54-
GraphicsAllocation::AllocationType::COMMAND_BUFFER,
55-
(device->getNumGenericSubDevices() > 1u) /* multiOsContextCapable */,
56-
false,
57-
device->getDeviceBitfield()};
58-
59-
auto cmdBufferAllocation = device->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties);
49+
50+
auto cmdBufferAllocation = this->obtainNextCommandBufferAllocation();
51+
6052
if (!cmdBufferAllocation) {
6153
return ErrorCode::OUT_OF_DEVICE_MEMORY;
6254
}
@@ -124,9 +116,7 @@ void CommandContainer::reset() {
124116
getDeallocationContainer().clear();
125117
sshAllocations.clear();
126118

127-
for (size_t i = 1; i < cmdBufferAllocations.size(); i++) {
128-
device->getMemoryManager()->freeGraphicsMemory(cmdBufferAllocations[i]);
129-
}
119+
this->handleCmdBufferAllocations(1u);
130120
cmdBufferAllocations.erase(cmdBufferAllocations.begin() + 1, cmdBufferAllocations.end());
131121

132122
commandStream->replaceBuffer(cmdBufferAllocations[0]->getUnderlyingBuffer(),
@@ -220,17 +210,40 @@ IndirectHeap *CommandContainer::getHeapWithRequiredSizeAndAlignment(HeapType hea
220210
return indirectHeap;
221211
}
222212

223-
void CommandContainer::allocateNextCommandBuffer() {
213+
void CommandContainer::handleCmdBufferAllocations(size_t startIndex) {
214+
for (size_t i = startIndex; i < cmdBufferAllocations.size(); i++) {
215+
if (this->reusableAllocationList) {
216+
reusableAllocationList->pushFrontOne(*cmdBufferAllocations[i]);
217+
} else {
218+
this->device->getMemoryManager()->freeGraphicsMemory(cmdBufferAllocations[i]);
219+
}
220+
}
221+
}
222+
223+
GraphicsAllocation *CommandContainer::obtainNextCommandBufferAllocation() {
224224
size_t alignedSize = alignUp<size_t>(totalCmdBufferSize, MemoryConstants::pageSize64k);
225-
AllocationProperties properties{device->getRootDeviceIndex(),
226-
true /* allocateMemory*/,
227-
alignedSize,
228-
GraphicsAllocation::AllocationType::COMMAND_BUFFER,
229-
(device->getNumGenericSubDevices() > 1u) /* multiOsContextCapable */,
230-
false,
231-
device->getDeviceBitfield()};
232-
233-
auto cmdBufferAllocation = device->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties);
225+
226+
GraphicsAllocation *cmdBufferAllocation = nullptr;
227+
if (this->reusableAllocationList) {
228+
cmdBufferAllocation = this->reusableAllocationList->detachAllocation(alignedSize, nullptr, nullptr, GraphicsAllocation::AllocationType::COMMAND_BUFFER).release();
229+
}
230+
if (!cmdBufferAllocation) {
231+
AllocationProperties properties{device->getRootDeviceIndex(),
232+
true /* allocateMemory*/,
233+
alignedSize,
234+
GraphicsAllocation::AllocationType::COMMAND_BUFFER,
235+
(device->getNumGenericSubDevices() > 1u) /* multiOsContextCapable */,
236+
false,
237+
device->getDeviceBitfield()};
238+
239+
cmdBufferAllocation = device->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties);
240+
}
241+
242+
return cmdBufferAllocation;
243+
}
244+
245+
void CommandContainer::allocateNextCommandBuffer() {
246+
auto cmdBufferAllocation = this->obtainNextCommandBufferAllocation();
234247
UNRECOVERABLE_IF(!cmdBufferAllocation);
235248

236249
cmdBufferAllocations.push_back(cmdBufferAllocation);

shared/source/command_container/cmdcontainer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace NEO {
2020
class Device;
2121
class GraphicsAllocation;
2222
class LinearStream;
23+
class AllocationsList;
2324

2425
using ResidencyContainer = std::vector<GraphicsAllocation *>;
2526
using CmdBufferContainer = std::vector<GraphicsAllocation *>;
@@ -77,7 +78,7 @@ class CommandContainer : public NonCopyableOrMovableClass {
7778

7879
void *getHeapSpaceAllowGrow(HeapType heapType, size_t size);
7980

80-
ErrorCode initialize(Device *device);
81+
ErrorCode initialize(Device *device, AllocationsList *reusableAllocationList);
8182

8283
void prepareBindfulSsh();
8384

@@ -94,6 +95,9 @@ class CommandContainer : public NonCopyableOrMovableClass {
9495
IndirectHeap *getHeapWithRequiredSizeAndAlignment(HeapType heapType, size_t sizeRequired, size_t alignment);
9596
void allocateNextCommandBuffer();
9697

98+
void handleCmdBufferAllocations(size_t startIndex);
99+
GraphicsAllocation *obtainNextCommandBufferAllocation();
100+
97101
void reset();
98102

99103
bool isHeapDirty(HeapType heapType) const { return (dirtyHeaps & (1u << heapType)); }
@@ -114,6 +118,7 @@ class CommandContainer : public NonCopyableOrMovableClass {
114118
protected:
115119
void *iddBlock = nullptr;
116120
Device *device = nullptr;
121+
AllocationsList *reusableAllocationList = nullptr;
117122
std::unique_ptr<HeapHelper> heapHelper;
118123

119124
CmdBufferContainer cmdBufferAllocations;

shared/test/common/fixtures/command_container_fixture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CommandEncodeStatesFixture : public DeviceFixture {
2424
void SetUp() {
2525
DeviceFixture::SetUp();
2626
cmdContainer = std::make_unique<MyMockCommandContainer>();
27-
cmdContainer->initialize(pDevice);
27+
cmdContainer->initialize(pDevice, nullptr);
2828
cmdContainer->setDirtyStateForAllHeaps(false);
2929
}
3030
void TearDown() {

shared/test/common/gen11/test_encode_math_gen11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GEN11TEST_F(CommandEncoderMathTestGen11, WhenAppendsAGreaterThanThenPredicateCor
2424
using MI_MATH_ALU_INST_INLINE = typename FamilyType::MI_MATH_ALU_INST_INLINE;
2525

2626
CommandContainer cmdContainer;
27-
cmdContainer.initialize(pDevice);
27+
cmdContainer.initialize(pDevice, nullptr);
2828

2929
EncodeMathMMIO<FamilyType>::encodeGreaterThanPredicate(cmdContainer, 0xDEADBEEFCAF0u, 17u);
3030

shared/test/common/gen12lp/test_command_encoder_gen12lp.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ GEN12LPTEST_F(CommandEncoderTest, WhenAdjustComputeModeIsCalledThenStateComputeM
2626

2727
CommandContainer cmdContainer;
2828

29-
auto ret = cmdContainer.initialize(pDevice);
29+
auto ret = cmdContainer.initialize(pDevice, nullptr);
3030
ASSERT_EQ(ErrorCode::SUCCESS, ret);
3131

3232
auto usedSpaceBefore = cmdContainer.getCommandStream()->getUsed();
@@ -54,7 +54,7 @@ GEN12LPTEST_F(CommandEncoderTest, WhenAdjustComputeModeIsCalledThenStateComputeM
5454

5555
GEN12LPTEST_F(CommandEncoderTest, givenCommandContainerWhenEncodeL3StateThenDoNotDispatchMMIOCommand) {
5656
CommandContainer cmdContainer;
57-
cmdContainer.initialize(pDevice);
57+
cmdContainer.initialize(pDevice, nullptr);
5858
EncodeL3State<FamilyType>::encode(cmdContainer, false);
5959

6060
GenCmdList commands;
@@ -76,7 +76,7 @@ GEN12LPTEST_F(CommandEncoderTest, givenVariousEngineTypesWhenEncodeSBAThenAdditi
7676

7777
CommandContainer cmdContainer;
7878

79-
auto ret = cmdContainer.initialize(pDevice);
79+
auto ret = cmdContainer.initialize(pDevice, nullptr);
8080
ASSERT_EQ(ErrorCode::SUCCESS, ret);
8181

8282
{
@@ -124,7 +124,7 @@ GEN12LPTEST_F(CommandEncoderTest, GivenGen12LpWhenProgrammingL3StateOnThenExpect
124124
using MI_LOAD_REGISTER_IMM = typename FamilyType::MI_LOAD_REGISTER_IMM;
125125

126126
CommandContainer cmdContainer;
127-
cmdContainer.initialize(pDevice);
127+
cmdContainer.initialize(pDevice, nullptr);
128128

129129
EncodeL3State<FamilyType>::encode(cmdContainer, true);
130130

@@ -138,7 +138,7 @@ GEN12LPTEST_F(CommandEncoderTest, GivenGen12LpWhenProgrammingL3StateOnThenExpect
138138
GEN12LPTEST_F(CommandEncoderTest, GivenGen12LpWhenProgrammingL3StateOffThenExpectNoCommandsDispatched) {
139139
using MI_LOAD_REGISTER_IMM = typename FamilyType::MI_LOAD_REGISTER_IMM;
140140
CommandContainer cmdContainer;
141-
cmdContainer.initialize(pDevice);
141+
cmdContainer.initialize(pDevice, nullptr);
142142

143143
EncodeL3State<FamilyType>::encode(cmdContainer, false);
144144

shared/test/common/gen12lp/test_encode_math_gen12lp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GEN12LPTEST_F(CommandEncoderMathTestGen12Lp, WhenAppendsAGreaterThanThenPredicat
2424
using MI_MATH_ALU_INST_INLINE = typename FamilyType::MI_MATH_ALU_INST_INLINE;
2525

2626
CommandContainer cmdContainer;
27-
cmdContainer.initialize(pDevice);
27+
cmdContainer.initialize(pDevice, nullptr);
2828

2929
EncodeMathMMIO<FamilyType>::encodeGreaterThanPredicate(cmdContainer, 0xDEADBEEFCAF0u, 17u);
3030

shared/test/common/gen8/test_encode_math_gen8.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GEN8TEST_F(CommandEncoderMathTestGen8, WhenAppendsAGreaterThanThenPredicateCorre
2424
using MI_MATH_ALU_INST_INLINE = typename FamilyType::MI_MATH_ALU_INST_INLINE;
2525

2626
CommandContainer cmdContainer;
27-
cmdContainer.initialize(pDevice);
27+
cmdContainer.initialize(pDevice, nullptr);
2828

2929
EncodeMathMMIO<FamilyType>::encodeGreaterThanPredicate(cmdContainer, 0xDEADBEEFCAF0u, 17u);
3030

0 commit comments

Comments
 (0)