Skip to content

Commit f2eb7f3

Browse files
Fix DirectSubmission residency handling
- allocations should be resident within OsContext Signed-off-by: Mateusz Hoppe <[email protected]>
1 parent e9f56e7 commit f2eb7f3

File tree

11 files changed

+101
-10
lines changed

11 files changed

+101
-10
lines changed

opencl/test/unit_test/os_interface/linux/drm_buffer_object_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ TEST_F(DrmBufferObjectTest, givenResidentBOWhenPrintExecutionBufferIsSetToTrueTh
191191

192192
std::string output = testing::internal::GetCapturedStdout();
193193
auto idx = output.find("drm_i915_gem_execbuffer2 {");
194-
size_t expectedValue = 0;
194+
size_t expectedValue = 29;
195195
EXPECT_EQ(expectedValue, idx);
196196

197197
idx = output.find("Buffer Object = { handle: BO-");
@@ -228,7 +228,7 @@ TEST_F(DrmBufferObjectTest, whenPrintExecutionBufferIsSetToTrueThenMessageFoundI
228228

229229
std::string output = testing::internal::GetCapturedStdout();
230230
auto idx = output.find("drm_i915_gem_execbuffer2 {");
231-
size_t expectedValue = 0;
231+
size_t expectedValue = 29;
232232
EXPECT_EQ(expectedValue, idx);
233233
}
234234

shared/source/direct_submission/direct_submission_hw.inl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ bool DirectSubmissionHw<GfxFamily, Dispatcher>::allocateResources() {
106106
template <typename GfxFamily, typename Dispatcher>
107107
bool DirectSubmissionHw<GfxFamily, Dispatcher>::makeResourcesResident(DirectSubmissionAllocations &allocations) {
108108
auto memoryInterface = this->device.getRootDeviceEnvironment().memoryOperationsInterface.get();
109-
auto ret = memoryInterface->makeResident(&device, ArrayRef<GraphicsAllocation *>(allocations)) == MemoryOperationsStatus::SUCCESS;
109+
110+
auto ret = memoryInterface->makeResidentWithinOsContext(&this->osContext, ArrayRef<GraphicsAllocation *>(allocations), false) == MemoryOperationsStatus::SUCCESS;
110111

111112
return ret;
112113
}

shared/source/memory_manager/memory_operations_handler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 Intel Corporation
2+
* Copyright (C) 2019-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -14,6 +14,7 @@
1414
namespace NEO {
1515
class Device;
1616
class GraphicsAllocation;
17+
class OsContext;
1718

1819
class MemoryOperationsHandler {
1920
public:
@@ -23,5 +24,8 @@ class MemoryOperationsHandler {
2324
virtual MemoryOperationsStatus makeResident(Device *device, ArrayRef<GraphicsAllocation *> gfxAllocations) = 0;
2425
virtual MemoryOperationsStatus evict(Device *device, GraphicsAllocation &gfxAllocation) = 0;
2526
virtual MemoryOperationsStatus isResident(Device *device, GraphicsAllocation &gfxAllocation) = 0;
27+
28+
virtual MemoryOperationsStatus makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) = 0;
29+
virtual MemoryOperationsStatus evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) = 0;
2630
};
2731
} // namespace NEO

shared/source/os_interface/aub_memory_operations_handler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ MemoryOperationsStatus AubMemoryOperationsHandler::evict(Device *device, Graphic
5656
}
5757
}
5858

59+
MemoryOperationsStatus AubMemoryOperationsHandler::makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) {
60+
return makeResident(nullptr, gfxAllocations);
61+
}
62+
63+
MemoryOperationsStatus AubMemoryOperationsHandler::evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) {
64+
return evict(nullptr, gfxAllocation);
65+
}
66+
5967
MemoryOperationsStatus AubMemoryOperationsHandler::isResident(Device *device, GraphicsAllocation &gfxAllocation) {
6068
auto lock = acquireLock(resourcesLock);
6169
auto itor = std::find(residentAllocations.begin(), residentAllocations.end(), &gfxAllocation);

shared/source/os_interface/aub_memory_operations_handler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 Intel Corporation
2+
* Copyright (C) 2019-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -24,6 +24,10 @@ class AubMemoryOperationsHandler : public MemoryOperationsHandler {
2424
MemoryOperationsStatus makeResident(Device *device, ArrayRef<GraphicsAllocation *> gfxAllocations) override;
2525
MemoryOperationsStatus evict(Device *device, GraphicsAllocation &gfxAllocation) override;
2626
MemoryOperationsStatus isResident(Device *device, GraphicsAllocation &gfxAllocation) override;
27+
28+
MemoryOperationsStatus makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) override;
29+
MemoryOperationsStatus evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) override;
30+
2731
void setAubManager(aub_stream::AubManager *aubManager);
2832

2933
protected:

shared/source/os_interface/linux/drm_buffer_object.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "shared/source/os_interface/linux/drm_memory_manager.h"
1313
#include "shared/source/os_interface/linux/drm_memory_operations_handler.h"
1414
#include "shared/source/os_interface/linux/drm_neo.h"
15+
#include "shared/source/os_interface/linux/os_context_linux.h"
1516
#include "shared/source/os_interface/linux/os_time_linux.h"
1617
#include "shared/source/os_interface/os_context.h"
1718
#include "shared/source/utilities/stackvec.h"
@@ -141,6 +142,9 @@ int BufferObject::exec(uint32_t used, size_t startOffset, unsigned int flags, bo
141142
execbuf.rsvd1 = drmContextId;
142143

143144
if (DebugManager.flags.PrintExecutionBuffer.get()) {
145+
PRINT_DEBUG_STRING(DebugManager.flags.PrintExecutionBuffer.get(), stdout, "Exec called with drmVmId = %u\n",
146+
static_cast<const OsContextLinux *>(osContext)->getDrmVmIds().size() ? static_cast<const OsContextLinux *>(osContext)->getDrmVmIds()[vmHandleId] : 0);
147+
144148
printExecutionBuffer(execbuf, residencyCount, execObjectsStorage, residency);
145149
}
146150

@@ -171,7 +175,10 @@ int BufferObject::bind(OsContext *osContext, uint32_t vmHandleId) {
171175
if (!this->bindInfo[contextId][vmHandleId]) {
172176
retVal = this->drm->bindBufferObject(osContext, vmHandleId, this);
173177
auto err = this->drm->getErrno();
174-
PRINT_DEBUG_STRING(DebugManager.flags.PrintBOBindingResult.get(), stderr, "bind BO-%d to VM %u, range: %llx - %llx, size: %lld, result: %d, errno: %d(%s)\n", this->handle, vmHandleId, this->gpuAddress, ptrOffset(this->gpuAddress, this->size), this->size, retVal, err, strerror(err));
178+
179+
PRINT_DEBUG_STRING(DebugManager.flags.PrintBOBindingResult.get(), stderr, "bind BO-%d to VM %u, drmVmId = %u, range: %llx - %llx, size: %lld, result: %d, errno: %d(%s)\n",
180+
this->handle, vmHandleId, static_cast<const OsContextLinux *>(osContext)->getDrmVmIds().size() ? static_cast<const OsContextLinux *>(osContext)->getDrmVmIds()[vmHandleId] : 0, this->gpuAddress, ptrOffset(this->gpuAddress, this->size), this->size, retVal, err, strerror(err));
181+
175182
if (!retVal) {
176183
this->bindInfo[contextId][vmHandleId] = true;
177184
}
@@ -185,7 +192,10 @@ int BufferObject::unbind(OsContext *osContext, uint32_t vmHandleId) {
185192
if (this->bindInfo[contextId][vmHandleId]) {
186193
retVal = this->drm->unbindBufferObject(osContext, vmHandleId, this);
187194
auto err = this->drm->getErrno();
188-
PRINT_DEBUG_STRING(DebugManager.flags.PrintBOBindingResult.get(), stderr, "unbind BO-%d from VM %u, range: %llx - %llx, size: %lld, result: %d, errno: %d(%s)\n", this->handle, vmHandleId, this->gpuAddress, ptrOffset(this->gpuAddress, this->size), this->size, retVal, err, strerror(err));
195+
196+
PRINT_DEBUG_STRING(DebugManager.flags.PrintBOBindingResult.get(), stderr, "unbind BO-%d from VM %u, drmVmId = %u, range: %llx - %llx, size: %lld, result: %d, errno: %d(%s)\n",
197+
this->handle, vmHandleId, static_cast<const OsContextLinux *>(osContext)->getDrmVmIds().size() ? static_cast<const OsContextLinux *>(osContext)->getDrmVmIds()[vmHandleId] : 0, this->gpuAddress, ptrOffset(this->gpuAddress, this->size), this->size, retVal, err, strerror(err));
198+
189199
if (!retVal) {
190200
this->bindInfo[contextId][vmHandleId] = false;
191201
}

shared/source/os_interface/linux/drm_memory_operations_handler.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class DrmMemoryOperationsHandler : public MemoryOperationsHandler {
2020
DrmMemoryOperationsHandler() = default;
2121
~DrmMemoryOperationsHandler() override = default;
2222

23-
virtual MemoryOperationsStatus makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) = 0;
24-
virtual MemoryOperationsStatus evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) = 0;
2523
virtual void mergeWithResidencyContainer(OsContext *osContext, ResidencyContainer &residencyContainer) = 0;
2624
virtual std::unique_lock<std::mutex> lockHandlerIfUsed() = 0;
2725

shared/source/os_interface/windows/wddm_memory_operations_handler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class WddmMemoryOperationsHandler : public MemoryOperationsHandler {
2424
MemoryOperationsStatus evict(Device *device, GraphicsAllocation &gfxAllocation) override;
2525
MemoryOperationsStatus isResident(Device *device, GraphicsAllocation &gfxAllocation) override;
2626

27+
MemoryOperationsStatus makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) override {
28+
return makeResident(nullptr, gfxAllocations);
29+
}
30+
MemoryOperationsStatus evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) override {
31+
return evict(nullptr, gfxAllocation);
32+
}
33+
2734
protected:
2835
Wddm *wddm;
2936
std::unique_ptr<WddmResidentAllocationsContainer> residentAllocations;

shared/test/common/mocks/mock_direct_submission_hw.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "shared/source/direct_submission/direct_submission_hw.h"
1010
#include "shared/source/direct_submission/direct_submission_hw_diagnostic_mode.h"
1111
#include "shared/source/memory_manager/graphics_allocation.h"
12-
12+
#include "shared/test/common/mocks/mock_memory_operations_handler.h"
1313
namespace NEO {
1414

1515
template <typename GfxFamily, typename Dispatcher>
@@ -73,6 +73,9 @@ struct MockDirectSubmissionHw : public DirectSubmissionHw<GfxFamily, Dispatcher>
7373
}
7474

7575
bool makeResourcesResident(DirectSubmissionAllocations &allocations) override {
76+
if (callBaseResident) {
77+
return BaseClass::makeResourcesResident(allocations);
78+
}
7679
return true;
7780
}
7881

@@ -122,5 +125,6 @@ struct MockDirectSubmissionHw : public DirectSubmissionHw<GfxFamily, Dispatcher>
122125
bool allocateOsResourcesReturn = true;
123126
bool submitReturn = true;
124127
bool handleResidencyReturn = true;
128+
bool callBaseResident = false;
125129
};
126130
} // namespace NEO

shared/test/common/mocks/mock_memory_operations_handler.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99
#include "shared/source/memory_manager/memory_operations_handler.h"
10+
#include "shared/source/os_interface/os_context.h"
1011

1112
#include "gmock/gmock.h"
1213

@@ -20,6 +21,8 @@ class MockMemoryOperationsHandler : public MemoryOperationsHandler {
2021
MemoryOperationsStatus makeResident(Device *device, ArrayRef<GraphicsAllocation *> gfxAllocations) override { return MemoryOperationsStatus::UNSUPPORTED; }
2122
MemoryOperationsStatus evict(Device *device, GraphicsAllocation &gfxAllocation) override { return MemoryOperationsStatus::UNSUPPORTED; }
2223
MemoryOperationsStatus isResident(Device *device, GraphicsAllocation &gfxAllocation) override { return MemoryOperationsStatus::UNSUPPORTED; }
24+
MemoryOperationsStatus makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) override { return MemoryOperationsStatus::UNSUPPORTED; }
25+
MemoryOperationsStatus evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) override { return MemoryOperationsStatus::UNSUPPORTED; }
2326
};
2427

2528
class MockMemoryOperationsHandlerTests : public MemoryOperationsHandler {
@@ -31,6 +34,10 @@ class MockMemoryOperationsHandlerTests : public MemoryOperationsHandler {
3134
(Device * device, GraphicsAllocation &gfxAllocation), (override));
3235
MOCK_METHOD(MemoryOperationsStatus, isResident,
3336
(Device * device, GraphicsAllocation &gfxAllocation), (override));
37+
MOCK_METHOD(MemoryOperationsStatus, makeResidentWithinOsContext,
38+
(OsContext * osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable), (override));
39+
MOCK_METHOD(MemoryOperationsStatus, evictWithinOsContext,
40+
(OsContext * osContext, GraphicsAllocation &gfxAllocation), (override));
3441
};
3542

3643
class MockMemoryOperations : public MemoryOperationsHandler {
@@ -49,8 +56,21 @@ class MockMemoryOperations : public MemoryOperationsHandler {
4956
return MemoryOperationsStatus::SUCCESS;
5057
}
5158

59+
MemoryOperationsStatus makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) override {
60+
makeResidentCalledCount++;
61+
if (osContext) {
62+
makeResidentContextId = osContext->getContextId();
63+
}
64+
return MemoryOperationsStatus::SUCCESS;
65+
}
66+
MemoryOperationsStatus evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) override {
67+
evictCalledCount++;
68+
return MemoryOperationsStatus::SUCCESS;
69+
}
70+
5271
int makeResidentCalledCount = 0;
5372
int evictCalledCount = 0;
73+
uint32_t makeResidentContextId = std::numeric_limits<uint32_t>::max();
5474
};
5575

5676
} // namespace NEO

0 commit comments

Comments
 (0)