Skip to content

Commit 8f93f4f

Browse files
Recycle old command buffers of immediate command lists
Related-To: NEO-6242 Signed-off-by: Zbigniew Zdanowicz <[email protected]>
1 parent dd01cff commit 8f93f4f

File tree

8 files changed

+96
-28
lines changed

8 files changed

+96
-28
lines changed

shared/source/command_container/cmdcontainer.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,16 @@ IndirectHeap *CommandContainer::getHeapWithRequiredSizeAndAlignment(HeapType hea
229229

230230
void CommandContainer::handleCmdBufferAllocations(size_t startIndex) {
231231
for (size_t i = startIndex; i < cmdBufferAllocations.size(); i++) {
232-
if (this->reusableAllocationList) {
233-
this->device->getMemoryManager()->handleFenceCompletion(cmdBufferAllocations[i]);
234-
reusableAllocationList->pushFrontOne(*cmdBufferAllocations[i]);
235-
} else {
236-
this->device->getMemoryManager()->freeGraphicsMemory(cmdBufferAllocations[i]);
237-
}
232+
handleCmdBufferAllocation(cmdBufferAllocations[i]);
233+
}
234+
}
235+
236+
void CommandContainer::handleCmdBufferAllocation(GraphicsAllocation *allocation) {
237+
if (this->reusableAllocationList) {
238+
this->device->getMemoryManager()->handleFenceCompletion(allocation);
239+
reusableAllocationList->pushFrontOne(*allocation);
240+
} else {
241+
this->device->getMemoryManager()->freeGraphicsMemory(allocation);
238242
}
239243
}
240244

@@ -264,6 +268,11 @@ void CommandContainer::allocateNextCommandBuffer() {
264268
auto cmdBufferAllocation = this->obtainNextCommandBufferAllocation();
265269
UNRECOVERABLE_IF(!cmdBufferAllocation);
266270

271+
if (getFlushTaskUsedForImmediate()) {
272+
NEO::GraphicsAllocation *oldCmdBuffer = cmdBufferAllocations.back();
273+
handleCmdBufferAllocation(oldCmdBuffer);
274+
cmdBufferAllocations.pop_back();
275+
}
267276
cmdBufferAllocations.push_back(cmdBufferAllocation);
268277

269278
size_t alignedSize = alignUp<size_t>(totalCmdBufferSize, MemoryConstants::pageSize64k);

shared/source/command_container/cmdcontainer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class CommandContainer : public NonCopyableOrMovableClass {
8888
void closeAndAllocateNextCommandBuffer();
8989

9090
void handleCmdBufferAllocations(size_t startIndex);
91+
void handleCmdBufferAllocation(GraphicsAllocation *allocation);
9192
GraphicsAllocation *obtainNextCommandBufferAllocation();
9293

9394
void reset();

shared/test/common/fixtures/command_container_fixture.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,24 @@
99
#include "shared/source/command_container/command_encoder.h"
1010
#include "shared/source/kernel/kernel_descriptor.h"
1111
#include "shared/test/common/fixtures/device_fixture.h"
12+
#include "shared/test/common/mocks/mock_command_container.h"
1213
#include "shared/test/common/test_macros/test.h"
1314

1415
namespace NEO {
1516

1617
class CommandEncodeStatesFixture : public DeviceFixture {
1718
public:
18-
class MyMockCommandContainer : public CommandContainer {
19-
public:
20-
using CommandContainer::dirtyHeaps;
21-
};
22-
2319
void SetUp() {
2420
DeviceFixture::SetUp();
25-
cmdContainer = std::make_unique<MyMockCommandContainer>();
21+
cmdContainer = std::make_unique<MockCommandContainer>();
2622
cmdContainer->initialize(pDevice, nullptr);
2723
cmdContainer->setDirtyStateForAllHeaps(false);
2824
}
2925
void TearDown() {
3026
cmdContainer.reset();
3127
DeviceFixture::TearDown();
3228
}
33-
std::unique_ptr<MyMockCommandContainer> cmdContainer;
29+
std::unique_ptr<MockCommandContainer> cmdContainer;
3430
KernelDescriptor descriptor;
3531

3632
EncodeDispatchKernelArgs createDefaultDispatchKernelArgs(Device *device,

shared/test/common/mocks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(NEO_CORE_tests_mocks
2727
${CMAKE_CURRENT_SOURCE_DIR}/mock_builtinslib.h
2828
${CMAKE_CURRENT_SOURCE_DIR}/mock_cif.cpp
2929
${CMAKE_CURRENT_SOURCE_DIR}/mock_cif.h
30+
${CMAKE_CURRENT_SOURCE_DIR}/mock_command_container.h
3031
${CMAKE_CURRENT_SOURCE_DIR}/mock_command_stream_receiver.cpp
3132
${CMAKE_CURRENT_SOURCE_DIR}/mock_command_stream_receiver.h
3233
${CMAKE_CURRENT_SOURCE_DIR}/mock_compiler_interface_spirv.cpp
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (C) 2022 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
#include "shared/source/command_container/cmdcontainer.h"
10+
11+
struct MockCommandContainer : public NEO::CommandContainer {
12+
using CommandContainer::cmdBufferAllocations;
13+
using CommandContainer::dirtyHeaps;
14+
using CommandContainer::isFlushTaskUsedForImmediate;
15+
using CommandContainer::reusableAllocationList;
16+
};

shared/test/unit_test/command_container/command_container_tests.cpp

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "shared/source/memory_manager/allocations_list.h"
1111
#include "shared/test/common/fixtures/device_fixture.h"
1212
#include "shared/test/common/helpers/debug_manager_state_restore.h"
13+
#include "shared/test/common/mocks/mock_command_container.h"
1314
#include "shared/test/common/mocks/mock_graphics_allocation.h"
1415
#include "shared/test/common/mocks/mock_memory_manager.h"
1516
#include "shared/test/common/test_macros/test.h"
@@ -27,18 +28,17 @@ class CommandContainerTest : public DeviceFixture,
2728
DeviceFixture::SetUp();
2829
}
2930
void TearDown() override {
31+
allocList.freeAllGraphicsAllocations(pDevice);
32+
3033
DeviceFixture::TearDown();
3134
::testing::Test::TearDown();
3235
}
36+
37+
AllocationsList allocList;
3338
};
3439

3540
struct CommandContainerHeapStateTests : public ::testing::Test {
36-
class MyMockCommandContainer : public CommandContainer {
37-
public:
38-
using CommandContainer::dirtyHeaps;
39-
};
40-
41-
MyMockCommandContainer myCommandContainer;
41+
MockCommandContainer myCommandContainer;
4242
};
4343

4444
TEST_F(CommandContainerHeapStateTests, givenDirtyHeapsWhenSettingStateForAllThenValuesAreCorrect) {
@@ -173,7 +173,6 @@ TEST_F(CommandContainerTest, givenCommandContainerDuringInitWhenAllocateGfxMemor
173173
}
174174

175175
TEST_F(CommandContainerTest, givenCmdContainerWithAllocsListWhenAllocateAndResetThenCmdBufferAllocIsReused) {
176-
AllocationsList allocList;
177176
auto cmdContainer = std::make_unique<CommandContainer>();
178177
cmdContainer->initialize(pDevice, &allocList);
179178
auto &cmdBufferAllocs = cmdContainer->getCmdBufferAllocations();
@@ -203,7 +202,6 @@ TEST_F(CommandContainerTest, givenCmdContainerWithAllocsListWhenAllocateAndReset
203202
cmdContainer.reset();
204203
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 3u);
205204
EXPECT_FALSE(allocList.peekIsEmpty());
206-
allocList.freeAllGraphicsAllocations(pDevice);
207205
}
208206

209207
TEST_F(CommandContainerTest, givenCommandContainerDuringInitWhenAllocateHeapMemoryFailsThenErrorIsReturned) {
@@ -728,4 +726,49 @@ TEST_F(CommandContainerTest, givenCmdContainerWhenCloseAndAllocateNextCommandBuf
728726
EXPECT_EQ(cmdContainer.getCmdBufferAllocations().size(), 1u);
729727
cmdContainer.closeAndAllocateNextCommandBuffer();
730728
EXPECT_EQ(cmdContainer.getCmdBufferAllocations().size(), 2u);
731-
}
729+
}
730+
731+
TEST_F(CommandContainerTest, givenFlushTaskCmdContainerWithAllocationListWhenNewCmdBufferAllocatedThenOldCmdBufferIsStored) {
732+
auto cmdContainer = std::make_unique<MockCommandContainer>();
733+
cmdContainer->isFlushTaskUsedForImmediate = true;
734+
cmdContainer->initialize(pDevice, &allocList);
735+
736+
auto &cmdBufferAllocs = cmdContainer->cmdBufferAllocations;
737+
auto memoryManager = static_cast<MockMemoryManager *>(pDevice->getMemoryManager());
738+
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
739+
EXPECT_EQ(1u, cmdBufferAllocs.size());
740+
EXPECT_TRUE(allocList.peekIsEmpty());
741+
GraphicsAllocation *oldAllocation = cmdContainer->getCommandStream()->getGraphicsAllocation();
742+
743+
cmdContainer->allocateNextCommandBuffer();
744+
EXPECT_EQ(1u, cmdBufferAllocs.size());
745+
746+
auto cmdBuffer0 = cmdBufferAllocs[0];
747+
EXPECT_EQ(cmdBuffer0, cmdContainer->getCommandStream()->getGraphicsAllocation());
748+
EXPECT_NE(cmdBuffer0, oldAllocation);
749+
750+
EXPECT_EQ(0u, memoryManager->freeGraphicsMemoryCalled);
751+
EXPECT_EQ(1u, memoryManager->handleFenceCompletionCalled);
752+
EXPECT_FALSE(allocList.peekIsEmpty());
753+
}
754+
755+
TEST_F(CommandContainerTest, givenFlushTaskCmdContainerWithoutAllocationListWhenNewCmdBufferAllocatedThenOldCmdBufferIsFreed) {
756+
auto cmdContainer = std::make_unique<MockCommandContainer>();
757+
cmdContainer->isFlushTaskUsedForImmediate = true;
758+
cmdContainer->initialize(pDevice, nullptr);
759+
760+
auto &cmdBufferAllocs = cmdContainer->cmdBufferAllocations;
761+
auto memoryManager = static_cast<MockMemoryManager *>(pDevice->getMemoryManager());
762+
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
763+
EXPECT_EQ(1u, cmdBufferAllocs.size());
764+
GraphicsAllocation *oldAllocation = cmdContainer->getCommandStream()->getGraphicsAllocation();
765+
766+
cmdContainer->allocateNextCommandBuffer();
767+
EXPECT_EQ(1u, cmdBufferAllocs.size());
768+
769+
auto cmdBuffer0 = cmdBufferAllocs[0];
770+
EXPECT_EQ(cmdBuffer0, cmdContainer->getCommandStream()->getGraphicsAllocation());
771+
EXPECT_NE(cmdBuffer0, oldAllocation);
772+
773+
EXPECT_EQ(1u, memoryManager->freeGraphicsMemoryCalled);
774+
}

shared/test/unit_test/encoders/test_encode_dispatch_kernel.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "shared/test/common/fixtures/command_container_fixture.h"
1616
#include "shared/test/common/fixtures/front_window_fixture.h"
1717
#include "shared/test/common/helpers/debug_manager_state_restore.h"
18+
#include "shared/test/common/mocks/mock_command_container.h"
1819
#include "shared/test/common/mocks/mock_device.h"
1920
#include "shared/test/common/mocks/mock_dispatch_kernel_encoder_interface.h"
2021
#include "shared/test/common/test_macros/matchers.h"
@@ -461,7 +462,7 @@ HWCMDTEST_F(IGFX_GEN8_CORE, CommandEncodeStatesTest, givenForceBtpPrefetchModeDe
461462

462463
{
463464
DebugManager.flags.ForceBtpPrefetchMode.set(-1);
464-
cmdContainer.reset(new MyMockCommandContainer());
465+
cmdContainer.reset(new MockCommandContainer());
465466
cmdContainer->initialize(pDevice, nullptr);
466467
bool requiresUncachedMocs = false;
467468
EncodeDispatchKernelArgs dispatchArgs = createDefaultDispatchKernelArgs(pDevice, dispatchInterface.get(), dims, requiresUncachedMocs);
@@ -492,7 +493,7 @@ HWCMDTEST_F(IGFX_GEN8_CORE, CommandEncodeStatesTest, givenForceBtpPrefetchModeDe
492493

493494
{
494495
DebugManager.flags.ForceBtpPrefetchMode.set(0);
495-
cmdContainer.reset(new MyMockCommandContainer());
496+
cmdContainer.reset(new MockCommandContainer());
496497
cmdContainer->initialize(pDevice, nullptr);
497498

498499
bool requiresUncachedMocs = false;
@@ -519,7 +520,7 @@ HWCMDTEST_F(IGFX_GEN8_CORE, CommandEncodeStatesTest, givenForceBtpPrefetchModeDe
519520

520521
{
521522
DebugManager.flags.ForceBtpPrefetchMode.set(1);
522-
cmdContainer.reset(new MyMockCommandContainer());
523+
cmdContainer.reset(new MockCommandContainer());
523524
cmdContainer->initialize(pDevice, nullptr);
524525

525526
bool requiresUncachedMocs = false;

shared/test/unit_test/encoders/test_encode_dispatch_kernel_xehp_and_later.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "shared/test/common/fixtures/command_container_fixture.h"
1919
#include "shared/test/common/helpers/debug_manager_state_restore.h"
2020
#include "shared/test/common/helpers/variable_backup.h"
21+
#include "shared/test/common/mocks/mock_command_container.h"
2122
#include "shared/test/common/mocks/mock_device.h"
2223
#include "shared/test/common/mocks/mock_dispatch_kernel_encoder_interface.h"
2324
#include "shared/test/common/test_macros/test.h"
@@ -363,7 +364,7 @@ HWCMDTEST_F(IGFX_XE_HP_CORE, CommandEncodeStatesTest, givenForceBtpPrefetchModeD
363364

364365
{
365366
DebugManager.flags.ForceBtpPrefetchMode.set(-1);
366-
cmdContainer.reset(new MyMockCommandContainer());
367+
cmdContainer.reset(new MockCommandContainer());
367368
cmdContainer->initialize(pDevice, nullptr);
368369

369370
bool requiresUncachedMocs = false;
@@ -394,7 +395,7 @@ HWCMDTEST_F(IGFX_XE_HP_CORE, CommandEncodeStatesTest, givenForceBtpPrefetchModeD
394395

395396
{
396397
DebugManager.flags.ForceBtpPrefetchMode.set(0);
397-
cmdContainer.reset(new MyMockCommandContainer());
398+
cmdContainer.reset(new MockCommandContainer());
398399
cmdContainer->initialize(pDevice, nullptr);
399400

400401
bool requiresUncachedMocs = false;
@@ -416,7 +417,7 @@ HWCMDTEST_F(IGFX_XE_HP_CORE, CommandEncodeStatesTest, givenForceBtpPrefetchModeD
416417

417418
{
418419
DebugManager.flags.ForceBtpPrefetchMode.set(1);
419-
cmdContainer.reset(new MyMockCommandContainer());
420+
cmdContainer.reset(new MockCommandContainer());
420421
cmdContainer->initialize(pDevice, nullptr);
421422

422423
bool requiresUncachedMocs = false;

0 commit comments

Comments
 (0)