Skip to content

Commit b56289a

Browse files
User space AUBs capable of memory re-dumps on CPU-side memory modifications.
Any CPU related updates such as clEnqueueMapBuffer or similar need to trigger a re-dump of memory prior to the next clEnqueue call. Change-Id: I7b31e559278e92ff55b6ebab8ef4190caef1ebc0
1 parent e1697d7 commit b56289a

File tree

8 files changed

+142
-13
lines changed

8 files changed

+142
-13
lines changed

runtime/command_queue/cpu_data_transfer_handler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ void *CommandQueue::cpuDataTransferHandler(TransferProperties &transferPropertie
135135
}
136136
eventCompleted = true;
137137
}
138+
if (!unmapInfo.readOnly) {
139+
auto graphicsAllocation = transferProperties.memObj->getGraphicsAllocation();
140+
graphicsAllocation->clearTypeAubNonWritable();
141+
}
138142
break;
139143
case CL_COMMAND_READ_BUFFER:
140144
memcpy_s(transferProperties.ptr, transferProperties.size[0], ptrOffset(transferProperties.memObj->getCpuAddressForMemoryTransfer(), transferProperties.offset[0]), transferProperties.size[0]);

runtime/command_stream/aub_command_stream_receiver_hw.inl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 -2018, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -392,7 +392,7 @@ void *AUBCommandStreamReceiverHw<GfxFamily>::flattenBatchBuffer(BatchBuffer &bat
392392
void *flatBatchBuffer = nullptr;
393393

394394
if (batchBuffer.chainedBatchBuffer) {
395-
batchBuffer.chainedBatchBuffer->setAllocationType(batchBuffer.chainedBatchBuffer->getAllocationType() | GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE);
395+
batchBuffer.chainedBatchBuffer->setTypeAubNonWritable();
396396
auto sizeMainBatchBuffer = batchBuffer.chainedBatchBufferStartOffset - batchBuffer.startOffset;
397397
auto flatBatchBufferSize = alignUp(sizeMainBatchBuffer + batchBuffer.chainedBatchBuffer->getUnderlyingBufferSize(), MemoryConstants::pageSize);
398398
flatBatchBuffer = this->getMemoryManager()->alignedMallocWrapper(flatBatchBufferSize, MemoryConstants::pageSize);
@@ -488,7 +488,7 @@ bool AUBCommandStreamReceiverHw<GfxFamily>::writeMemory(GraphicsAllocation &gfxA
488488
auto size = gfxAllocation.getUnderlyingBufferSize();
489489
auto allocType = gfxAllocation.getAllocationType();
490490

491-
if ((size == 0) || !!(allocType & GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE))
491+
if ((size == 0) || gfxAllocation.isTypeAubNonWritable())
492492
return false;
493493

494494
{
@@ -515,7 +515,7 @@ bool AUBCommandStreamReceiverHw<GfxFamily>::writeMemory(GraphicsAllocation &gfxA
515515

516516
if (!!(allocType & GraphicsAllocation::ALLOCATION_TYPE_BUFFER) ||
517517
!!(allocType & GraphicsAllocation::ALLOCATION_TYPE_IMAGE))
518-
gfxAllocation.setAllocationType(allocType | GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE);
518+
gfxAllocation.setTypeAubNonWritable();
519519

520520
return true;
521521
}
@@ -526,8 +526,7 @@ void AUBCommandStreamReceiverHw<GfxFamily>::processResidency(ResidencyContainer
526526

527527
for (auto &gfxAllocation : residencyAllocations) {
528528
if (!writeMemory(*gfxAllocation)) {
529-
DEBUG_BREAK_IF(!((gfxAllocation->getUnderlyingBufferSize() == 0) ||
530-
!!(gfxAllocation->getAllocationType() & GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE)));
529+
DEBUG_BREAK_IF(!((gfxAllocation->getUnderlyingBufferSize() == 0) || gfxAllocation->isTypeAubNonWritable()));
531530
}
532531
gfxAllocation->residencyTaskCount = this->taskCount + 1;
533532
}

runtime/memory_manager/graphics_allocation.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -112,6 +112,10 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
112112
void setAllocationType(uint32_t allocationType) { this->allocationType = allocationType; }
113113
uint32_t getAllocationType() const { return allocationType; }
114114

115+
void setTypeAubNonWritable() { this->allocationType |= GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE; }
116+
void clearTypeAubNonWritable() { this->allocationType &= ~GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE; }
117+
bool isTypeAubNonWritable() const { return !!(this->allocationType & GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE); }
118+
115119
uint32_t taskCount = ObjectNotUsed;
116120
OsHandleStorage fragmentsStorage;
117121
bool isL3Capable();

unit_tests/aub_tests/command_queue/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ target_sources(igdrcl_aub_tests PRIVATE
2727
${CMAKE_CURRENT_SOURCE_DIR}/enqueue_fill_buffer_aub_tests.cpp
2828
${CMAKE_CURRENT_SOURCE_DIR}/enqueue_fill_image_aub_tests.cpp
2929
${CMAKE_CURRENT_SOURCE_DIR}/enqueue_kernel_aub_tests.cpp
30+
${CMAKE_CURRENT_SOURCE_DIR}/enqueue_map_buffer_aub_tests.cpp
3031
${CMAKE_CURRENT_SOURCE_DIR}/enqueue_map_image_aub_tests.cpp
3132
${CMAKE_CURRENT_SOURCE_DIR}/enqueue_read_buffer_aub_tests.cpp
3233
${CMAKE_CURRENT_SOURCE_DIR}/enqueue_read_buffer_rect_aub_tests.cpp
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include "unit_tests/aub_tests/command_queue/command_enqueue_fixture.h"
24+
#include "unit_tests/mocks/mock_context.h"
25+
#include "runtime/mem_obj/buffer.h"
26+
#include "test.h"
27+
28+
using namespace OCLRT;
29+
30+
struct AUBMapBuffer
31+
: public CommandEnqueueAUBFixture,
32+
public ::testing::Test {
33+
34+
void SetUp() override {
35+
CommandEnqueueAUBFixture::SetUp();
36+
}
37+
38+
void TearDown() override {
39+
CommandEnqueueAUBFixture::TearDown();
40+
}
41+
};
42+
43+
HWTEST_F(AUBMapBuffer, MapUpdateUnmapVerify) {
44+
MockContext context(&this->pCmdQ->getDevice());
45+
auto retVal = CL_INVALID_VALUE;
46+
size_t bufferSize = 10;
47+
48+
std::unique_ptr<Buffer> buffer(Buffer::create(
49+
&context,
50+
CL_MEM_READ_WRITE,
51+
bufferSize,
52+
nullptr,
53+
retVal));
54+
ASSERT_NE(nullptr, buffer);
55+
56+
uint8_t pattern[] = {0xFF};
57+
size_t patternSize = sizeof(pattern);
58+
59+
retVal = pCmdQ->enqueueFillBuffer(
60+
buffer.get(),
61+
pattern,
62+
patternSize,
63+
0,
64+
bufferSize,
65+
0,
66+
nullptr,
67+
nullptr);
68+
ASSERT_EQ(CL_SUCCESS, retVal);
69+
70+
auto mappedPtr = pCmdQ->enqueueMapBuffer(buffer.get(), CL_TRUE, CL_MAP_WRITE | CL_MAP_READ,
71+
0, bufferSize, 0, nullptr, nullptr, retVal);
72+
ASSERT_EQ(CL_SUCCESS, retVal);
73+
74+
// write to mapped ptr
75+
auto mappedPtrStart = static_cast<uint8_t *>(mappedPtr);
76+
for (uint32_t i = 0; i < bufferSize; i++) {
77+
*(mappedPtrStart + i) = i;
78+
}
79+
80+
pCmdQ->enqueueUnmapMemObject(buffer.get(), mappedPtr, 0, nullptr, nullptr);
81+
82+
// verify unmap
83+
std::unique_ptr<uint8_t[]> readMemory(new uint8_t[bufferSize]);
84+
buffer->forceDisallowCPUCopy = true;
85+
86+
retVal = pCmdQ->enqueueReadBuffer(buffer.get(), CL_TRUE, 0, bufferSize, readMemory.get(), 0, nullptr, nullptr);
87+
ASSERT_EQ(CL_SUCCESS, retVal);
88+
89+
for (size_t i = 0; i < bufferSize; i++) {
90+
AUBCommandStreamFixture::expectMemory<FamilyType>(&readMemory[i], &i, sizeof(uint8_t));
91+
}
92+
}

unit_tests/command_queue/enqueue_unmap_memobject_tests.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -203,3 +203,22 @@ TEST_F(EnqueueUnmapMemObjTest, UnmapMemObjWaitEvent) {
203203
clReleaseEvent(waitEvent);
204204
clReleaseEvent(retEvent);
205205
}
206+
207+
HWTEST_F(EnqueueUnmapMemObjTest, givenEnqueueUnmapMemObjectWhenNonAubWritableBufferObjectMappedToHostPtrForWritingThenItShouldBeResetToAubWritable) {
208+
auto buffer = std::unique_ptr<Buffer>(BufferHelper<>::create());
209+
ASSERT_NE(nullptr, buffer);
210+
buffer->getGraphicsAllocation()->setTypeAubNonWritable();
211+
212+
auto mappedForWritingPtr = pCmdQ->enqueueMapBuffer(buffer.get(), CL_TRUE, CL_MAP_WRITE, 0, 8, 0, nullptr, nullptr, retVal);
213+
ASSERT_NE(nullptr, mappedForWritingPtr);
214+
215+
retVal = pCmdQ->enqueueUnmapMemObject(
216+
buffer.get(),
217+
mappedForWritingPtr,
218+
0,
219+
nullptr,
220+
nullptr);
221+
ASSERT_EQ(CL_SUCCESS, retVal);
222+
223+
EXPECT_FALSE(buffer->getGraphicsAllocation()->isTypeAubNonWritable());
224+
}

unit_tests/command_stream/aub_command_stream_receiver_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenGraphic
307307

308308
auto gfxAllocation = memoryManager->allocateGraphicsMemory(sizeof(uint32_t), sizeof(uint32_t), false, false);
309309

310-
EXPECT_FALSE(!!(gfxAllocation->getAllocationType() & GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE));
310+
EXPECT_FALSE(gfxAllocation->isTypeAubNonWritable());
311311

312312
memoryManager->freeGraphicsMemory(gfxAllocation);
313313
}
@@ -322,7 +322,7 @@ HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenProcess
322322
ResidencyContainer allocationsForResidency = {gfxDefaultAllocation};
323323
aubCsr->processResidency(&allocationsForResidency);
324324

325-
EXPECT_FALSE(!!(gfxDefaultAllocation->getAllocationType() & GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE));
325+
EXPECT_FALSE(gfxDefaultAllocation->isTypeAubNonWritable());
326326

327327
memoryManager->freeGraphicsMemory(gfxDefaultAllocation);
328328
}
@@ -341,8 +341,8 @@ HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenProcess
341341
ResidencyContainer allocationsForResidency = {gfxBufferAllocation, gfxImageAllocation};
342342
aubCsr->processResidency(&allocationsForResidency);
343343

344-
EXPECT_TRUE(!!(gfxBufferAllocation->getAllocationType() & GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE));
345-
EXPECT_TRUE(!!(gfxImageAllocation->getAllocationType() & GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE));
344+
EXPECT_TRUE(gfxBufferAllocation->isTypeAubNonWritable());
345+
EXPECT_TRUE(gfxImageAllocation->isTypeAubNonWritable());
346346

347347
memoryManager->freeGraphicsMemory(gfxBufferAllocation);
348348
memoryManager->freeGraphicsMemory(gfxImageAllocation);
@@ -367,7 +367,7 @@ HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenGraphic
367367

368368
auto gfxAllocation = memoryManager->allocateGraphicsMemory(sizeof(uint32_t), sizeof(uint32_t), false, false);
369369

370-
gfxAllocation->setAllocationType(GraphicsAllocation::ALLOCATION_TYPE_NON_AUB_WRITABLE);
370+
gfxAllocation->setTypeAubNonWritable();
371371
EXPECT_FALSE(aubCsr->writeMemory(*gfxAllocation));
372372

373373
memoryManager->freeGraphicsMemory(gfxAllocation);

unit_tests/memory_manager/memory_manager_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ TEST(GraphicsAllocationTest, GivenGraphicsAllocationWhenLockingThenIsLocked) {
133133
EXPECT_NE(0ULL, gpuAddr);
134134
}
135135

136+
TEST(GraphicsAllocationTest, givenGraphicsAllocationWhenChangingTypeAubNonWritableThenItIsSetCorrectly) {
137+
GraphicsAllocation gfxAllocation((void *)0x30000, 0x1000);
138+
139+
gfxAllocation.setTypeAubNonWritable();
140+
EXPECT_TRUE(gfxAllocation.isTypeAubNonWritable());
141+
142+
gfxAllocation.clearTypeAubNonWritable();
143+
EXPECT_FALSE(gfxAllocation.isTypeAubNonWritable());
144+
}
145+
136146
TEST_F(MemoryAllocatorTest, allocateSystem) {
137147
auto ptr = memoryManager->allocateSystemMemory(sizeof(char), 0);
138148
EXPECT_NE(nullptr, ptr);

0 commit comments

Comments
 (0)