Skip to content

Commit f2a8fc7

Browse files
Print_formatter refactor
Change-Id: Icb03697281b009c853d91a63d5d21ffcde545a8f Signed-off-by: Maciej Plewka <[email protected]>
1 parent 7392856 commit f2a8fc7

File tree

11 files changed

+55
-73
lines changed

11 files changed

+55
-73
lines changed

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!groovy
22
dependenciesRevision='e299ec97c8d0d14d604a94606ce11c7498e30a76-1244'
33
strategy='EQUAL'
4-
allowedCD=276
4+
allowedCD=275
55
allowedF=4

runtime/program/kernel_info.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,6 @@ void WorkSizeInfo::checkRatio(const size_t workItems[3]) {
199199
KernelInfo::~KernelInfo() {
200200
kernelArgInfo.clear();
201201

202-
for (auto &stringData : patchInfo.stringDataMap) {
203-
delete[] stringData.second.pStringData;
204-
}
205202
patchInfo.stringDataMap.clear();
206203
delete[] crossThreadData;
207204
}
@@ -376,14 +373,9 @@ void KernelInfo::storePatchToken(const SPatchAllocateStatelessDefaultDeviceQueue
376373

377374
void KernelInfo::storePatchToken(const SPatchString *pStringArg) {
378375
uint32_t stringIndex = pStringArg->Index;
379-
PrintfStringInfo printfStringInfo;
380-
printfStringInfo.SizeInBytes = pStringArg->StringSize;
381-
if (printfStringInfo.SizeInBytes) {
382-
printfStringInfo.pStringData = new char[printfStringInfo.SizeInBytes];
383-
if (printfStringInfo.pStringData != nullptr) {
384-
memcpy_s(printfStringInfo.pStringData, printfStringInfo.SizeInBytes, (cl_char *)pStringArg + sizeof(SPatchString), printfStringInfo.SizeInBytes);
385-
patchInfo.stringDataMap.insert(std::pair<uint32_t, PrintfStringInfo>(stringIndex, printfStringInfo));
386-
}
376+
if (pStringArg->StringSize > 0) {
377+
const char *stringData = reinterpret_cast<const char *>(pStringArg + 1);
378+
patchInfo.stringDataMap.emplace(stringIndex, std::string(stringData, stringData + pStringArg->StringSize));
387379
}
388380
}
389381

@@ -404,11 +396,6 @@ void KernelInfo::storePatchToken(const SPatchAllocateSystemThreadSurface *pSyste
404396
patchInfo.pAllocateSystemThreadSurface = pSystemThreadSurface;
405397
}
406398

407-
const char *KernelInfo::queryPrintfString(uint32_t index) const {
408-
auto printfInfo = patchInfo.stringDataMap.find(index);
409-
return printfInfo == patchInfo.stringDataMap.end() ? nullptr : printfInfo->second.pStringData;
410-
}
411-
412399
cl_int KernelInfo::resolveKernelInfo() {
413400
cl_int retVal = CL_SUCCESS;
414401
std::unordered_map<std::string, uint32_t>::iterator iterUint;

runtime/program/kernel_info.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ struct KernelInfo {
163163

164164
void storeKernelArgPatchInfo(uint32_t argNum, uint32_t dataSize, uint32_t crossthreadOffset, uint32_t sourceOffset, uint32_t offsetSSH);
165165

166-
const char *queryPrintfString(uint32_t index) const;
167-
168166
size_t getSamplerStateArrayCount() const;
169167
size_t getSamplerStateArraySize(const HardwareInfo &hwInfo) const;
170168
size_t getBorderColorStateSize() const;

runtime/program/patch_info.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "patch_list.h"
1111

1212
#include <map>
13+
#include <string>
14+
#include <unordered_map>
1315
#include <vector>
1416

1517
namespace NEO {
@@ -44,11 +46,6 @@ using iOpenCL::SPatchString;
4446
using iOpenCL::SPatchThreadPayload;
4547
using iOpenCL::SProgramBinaryHeader;
4648

47-
typedef struct TagPrintfStringInfo {
48-
size_t SizeInBytes;
49-
char *pStringData;
50-
} PrintfStringInfo, *PPrintfStringInfo;
51-
5249
struct PatchInfo {
5350
const SPatchMediaInterfaceDescriptorLoad *interfaceDescriptorDataLoad = nullptr;
5451
const SPatchAllocateLocalSurface *localsurface = nullptr;
@@ -74,7 +71,7 @@ struct PatchInfo {
7471
const SPatchAllocateStatelessEventPoolSurface *pAllocateStatelessEventPoolSurface = nullptr;
7572
const SPatchAllocateStatelessDefaultDeviceQueueSurface *pAllocateStatelessDefaultDeviceQueueSurface = nullptr;
7673
const SPatchAllocateSystemThreadSurface *pAllocateSystemThreadSurface = nullptr;
77-
::std::map<uint32_t, PrintfStringInfo> stringDataMap;
74+
::std::unordered_map<uint32_t, std::string> stringDataMap;
7875
::std::vector<const SPatchKernelArgumentInfo *> kernelArgumentInfo;
7976

8077
PatchInfo() {

runtime/program/print_formatter.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,24 @@
1414

1515
namespace NEO {
1616

17-
PrintFormatter::PrintFormatter(Kernel &kernelArg, GraphicsAllocation &dataArg) : kernel(kernelArg),
18-
data(dataArg),
19-
buffer(nullptr),
20-
bufferSize(0),
21-
offset(0) {
17+
PrintFormatter::PrintFormatter(const uint8_t *printfOutputBuffer, uint32_t printfOutputBufferMaxSize,
18+
bool using32BitPointers, const StringMap &stringLiteralMap)
19+
: printfOutputBuffer(printfOutputBuffer),
20+
printfOutputBufferSize(printfOutputBufferMaxSize),
21+
stringLiteralMap(stringLiteralMap),
22+
using32BitPointers(using32BitPointers) {
2223
}
2324

2425
void PrintFormatter::printKernelOutput(const std::function<void(char *)> &print) {
25-
offset = 0;
26-
buffer = reinterpret_cast<uint8_t *>(data.getUnderlyingBuffer());
26+
currentOffset = 0;
2727

28-
// first 4 bytes of the buffer store it's own size
29-
// before reading it size needs to be set to 4 because read() checks bounds and would fail if bufferSize was 0
30-
bufferSize = 4;
31-
read(&bufferSize);
28+
// first 4 bytes of the buffer store the actual size of data that was written by printf from within EUs
29+
read(&printfOutputBufferSize);
3230

3331
uint32_t stringIndex = 0;
34-
35-
while (offset + 4 <= bufferSize) {
32+
while (currentOffset + 4 <= printfOutputBufferSize) {
3633
read(&stringIndex);
37-
const char *formatString = kernel.getKernelInfo().queryPrintfString(stringIndex);
34+
const char *formatString = queryPrintfString(stringIndex);
3835
if (formatString != nullptr) {
3936
printString(formatString, print);
4037
}
@@ -146,7 +143,7 @@ size_t PrintFormatter::printStringToken(char *output, size_t size, const char *f
146143
read(&type);
147144
read(&index);
148145
if (type == static_cast<int>(PRINTF_DATA_TYPE::STRING)) {
149-
return simple_sprintf(output, size, formatString, kernel.getKernelInfo().queryPrintfString(index));
146+
return simple_sprintf(output, size, formatString, queryPrintfString(index));
150147
} else {
151148
return simple_sprintf(output, size, formatString, 0);
152149
}
@@ -156,7 +153,7 @@ size_t PrintFormatter::printPointerToken(char *output, size_t size, const char *
156153
uint64_t value = {0};
157154
read(&value);
158155

159-
if (kernel.is32Bit()) {
156+
if (using32BitPointers) {
160157
value &= 0x00000000FFFFFFFF;
161158
}
162159

@@ -196,4 +193,10 @@ bool PrintFormatter::isConversionSpecifier(char c) {
196193
return false;
197194
}
198195
}
196+
197+
const char *PrintFormatter::queryPrintfString(uint32_t index) const {
198+
auto stringEntry = stringLiteralMap.find(index);
199+
return stringEntry == stringLiteralMap.end() ? nullptr : stringEntry->second.c_str();
200+
}
201+
199202
} // namespace NEO

runtime/program/print_formatter.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
#include <cctype>
1616
#include <cstdint>
1717
#include <functional>
18+
#include <string>
19+
#include <unordered_map>
1820

1921
extern int memcpy_s(void *dst, size_t destSize, const void *src, size_t count);
2022

2123
namespace NEO {
2224

25+
using StringMap = std::unordered_map<uint32_t, std::string>;
26+
2327
enum class PRINTF_DATA_TYPE : int {
2428
INVALID,
2529
BYTE,
@@ -40,12 +44,14 @@ enum class PRINTF_DATA_TYPE : int {
4044

4145
class PrintFormatter {
4246
public:
43-
PrintFormatter(Kernel &kernelArg, GraphicsAllocation &dataArg);
47+
PrintFormatter(const uint8_t *printfOutputBuffer, uint32_t printfOutputBufferMaxSize,
48+
bool using32BitPointers, const StringMap &stringLiteralMap);
4449
void printKernelOutput(const std::function<void(char *)> &print = [](char *str) { printToSTDOUT(str); });
4550

4651
static const size_t maxPrintfOutputLength = 1024;
4752

4853
protected:
54+
const char *queryPrintfString(uint32_t index) const;
4955
void printString(const char *formatString, const std::function<void(char *)> &print);
5056
size_t printToken(char *output, size_t size, const char *formatString);
5157
size_t printStringToken(char *output, size_t size, const char *formatString);
@@ -58,15 +64,15 @@ class PrintFormatter {
5864

5965
template <class T>
6066
bool read(T *value) {
61-
if (offset + sizeof(T) <= bufferSize) {
62-
auto srcPtr = reinterpret_cast<T *>(buffer + offset);
67+
if (currentOffset + sizeof(T) <= printfOutputBufferSize) {
68+
auto srcPtr = reinterpret_cast<const T *>(printfOutputBuffer + currentOffset);
6369

6470
if (isAligned(srcPtr)) {
6571
*value = *srcPtr;
6672
} else {
67-
memcpy_s(value, bufferSize - offset, srcPtr, sizeof(T));
73+
memcpy_s(value, printfOutputBufferSize - currentOffset, srcPtr, sizeof(T));
6874
}
69-
offset += sizeof(T);
75+
currentOffset += sizeof(T);
7076
return true;
7177
} else {
7278
return false;
@@ -101,17 +107,18 @@ class PrintFormatter {
101107
}
102108

103109
if (sizeof(T) < 4) {
104-
offset += (4 - sizeof(T)) * valueCount;
110+
currentOffset += (4 - sizeof(T)) * valueCount;
105111
}
106112

107113
return charactersPrinted;
108114
}
109115

110-
Kernel &kernel;
111-
GraphicsAllocation &data;
116+
const uint8_t *printfOutputBuffer = nullptr; // buffer extracted from the kernel, contains values to be printed
117+
uint32_t printfOutputBufferSize = 0; // size of the data contained in the buffer
118+
119+
const StringMap &stringLiteralMap;
120+
bool using32BitPointers = false;
112121

113-
uint8_t *buffer; // buffer extracted from the kernel, contains values to be printed
114-
uint32_t bufferSize; // size of the data contained in the buffer
115-
uint32_t offset; // current position in currently parsed buffer
122+
uint32_t currentOffset = 0; // current position in currently parsed buffer
116123
};
117124
}; // namespace NEO

runtime/program/printf_handler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ void PrintfHandler::makeResident(CommandStreamReceiver &commandStreamReceiver) {
5858
}
5959

6060
void PrintfHandler::printEnqueueOutput() {
61-
PrintFormatter printFormatter(*kernel, *printfSurface);
61+
PrintFormatter printFormatter(reinterpret_cast<const uint8_t *>(printfSurface->getUnderlyingBuffer()), static_cast<uint32_t>(printfSurface->getUnderlyingBufferSize()),
62+
kernel->is32Bit(), kernel->getKernelInfo().patchInfo.stringDataMap);
6263
printFormatter.printKernelOutput();
6364
}
6465
} // namespace NEO

unit_tests/command_queue/enqueue_kernel_2_tests.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,9 @@ HWTEST_P(EnqueueKernelPrintfTest, GivenKernelWithPrintfBlockedByEventWhenEventUn
610610

611611
auto crossThreadData = reinterpret_cast<uint64_t *>(mockKernel.mockKernel->getCrossThreadData());
612612

613-
char *testString = new char[sizeof("test")];
614-
strcpy_s(testString, sizeof("test"), "test");
613+
std::string testString = "test";
615614

616-
PrintfStringInfo printfStringInfo;
617-
printfStringInfo.SizeInBytes = sizeof("test");
618-
printfStringInfo.pStringData = testString;
619-
620-
mockKernel.kernelInfo.patchInfo.stringDataMap.insert(std::make_pair(0, printfStringInfo));
615+
mockKernel.kernelInfo.patchInfo.stringDataMap.insert(std::make_pair(0, testString));
621616

622617
cl_uint workDim = 1;
623618
size_t globalWorkOffset[3] = {0, 0, 0};

unit_tests/event/event_tests.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,13 @@ TEST_F(InternalsEventTest, givenBlockedKernelWithPrintfWhenSubmittedThenPrintOut
548548
pPrintfSurface->DataParamOffset = 0;
549549
pPrintfSurface->DataParamSize = 8;
550550

551-
char *testString = new char[sizeof("test")];
552-
553-
strcpy_s(testString, sizeof("test"), "test");
554-
555-
PrintfStringInfo printfStringInfo;
556-
printfStringInfo.SizeInBytes = sizeof("test");
557-
printfStringInfo.pStringData = testString;
551+
std::string testString = "test";
558552

559553
MockKernelWithInternals mockKernelWithInternals(*pDevice);
560554
auto pKernel = mockKernelWithInternals.mockKernel;
561555
KernelInfo *kernelInfo = const_cast<KernelInfo *>(&pKernel->getKernelInfo());
562556
kernelInfo->patchInfo.pAllocateStatelessPrintfSurface = pPrintfSurface;
563-
kernelInfo->patchInfo.stringDataMap.insert(std::make_pair(0, printfStringInfo));
557+
kernelInfo->patchInfo.stringDataMap.insert(std::make_pair(0, testString));
564558
uint64_t crossThread[10];
565559
pKernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
566560

unit_tests/program/kernel_data.cpp

Lines changed: 2 additions & 2 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-2019 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -123,7 +123,7 @@ TEST_F(KernelDataTest, PrintfString) {
123123

124124
buildAndDecode();
125125

126-
EXPECT_EQ_VAL(0, strcmp(stringValue, pKernelInfo->queryPrintfString(0)));
126+
EXPECT_EQ_VAL(0, strcmp(stringValue, pKernelInfo->patchInfo.stringDataMap.find(0)->second.c_str()));
127127
delete[] pPrintfString;
128128
}
129129

0 commit comments

Comments
 (0)