Skip to content

Commit 53223dc

Browse files
create trace files about cl_cache usage
Signed-off-by: Artur Harasimiuk <[email protected]>
1 parent ad8e640 commit 53223dc

File tree

7 files changed

+108
-14
lines changed

7 files changed

+108
-14
lines changed

opencl/test/unit_test/mocks/mock_program.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ ClDeviceVector toClDeviceVector(ClDevice &clDevice) {
3131
int MockProgram::initInternalOptionsCalled = 0;
3232

3333
std::string MockProgram::getCachedFileName() const {
34+
CompilerCache cache(CompilerCacheConfig{});
3435
auto hwInfo = this->context->getDevice(0)->getHardwareInfo();
3536
auto input = ArrayRef<const char>(this->sourceCode.c_str(), this->sourceCode.size());
3637
auto opts = ArrayRef<const char>(this->options.c_str(), this->options.size());
3738
auto internalOptions = getInitInternalOptions();
3839
auto internalOpts = ArrayRef<const char>(internalOptions.c_str(), internalOptions.size());
39-
return CompilerCache::getCachedFileName(hwInfo, input, opts, internalOpts);
40+
return cache.getCachedFileName(hwInfo, input, opts, internalOpts);
4041
}
4142

4243
} // namespace NEO

opencl/test/unit_test/test_files/igdrcl.config

+1
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,4 @@ AllowPatchingVfeStateInCommandLists = 0
334334
PrintMemoryRegionSizes = 0
335335
OverrideDrmRegion = -1
336336
AllowSingleTileEngineInstancedSubDevices = 0
337+
BinaryCacheTrace = false

shared/source/compiler_interface/compiler_cache.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
#include "shared/source/compiler_interface/compiler_cache.h"
99

10+
#include "shared/source/debug_settings/debug_settings_manager.h"
1011
#include "shared/source/helpers/aligned_memory.h"
1112
#include "shared/source/helpers/casts.h"
1213
#include "shared/source/helpers/file_io.h"
1314
#include "shared/source/helpers/hash.h"
1415
#include "shared/source/helpers/hw_info.h"
1516
#include "shared/source/utilities/debug_settings_reader.h"
17+
#include "shared/source/utilities/io_functions.h"
1618

1719
#include "config.h"
1820
#include "os_inc.h"
@@ -50,6 +52,48 @@ const std::string CompilerCache::getCachedFileName(const HardwareInfo &hwInfo, c
5052
<< std::hex
5153
<< res;
5254

55+
if (DebugManager.flags.BinaryCacheTrace.get()) {
56+
std::string filePath = config.cacheDir + PATH_SEPARATOR + stream.str() + ".trace";
57+
std::lock_guard<std::mutex> lock(cacheAccessMtx);
58+
auto fp = NEO::IoFunctions::fopenPtr(filePath.c_str(), "w");
59+
if (fp) {
60+
NEO::IoFunctions::fprintf(fp, "---- input ----\n");
61+
NEO::IoFunctions::fprintf(fp, "%s\n", &*input.begin());
62+
NEO::IoFunctions::fprintf(fp, "---- options ----\n");
63+
NEO::IoFunctions::fprintf(fp, "%s\n", &*options.begin());
64+
NEO::IoFunctions::fprintf(fp, "---- internal options ----\n");
65+
NEO::IoFunctions::fprintf(fp, "%s\n", &*internalOptions.begin());
66+
67+
NEO::IoFunctions::fprintf(fp, "---- platform ----\n");
68+
NEO::IoFunctions::fprintf(fp, " eProductFamily=%d\n", hwInfo.platform.eProductFamily);
69+
NEO::IoFunctions::fprintf(fp, " ePCHProductFamily=%d\n", hwInfo.platform.ePCHProductFamily);
70+
NEO::IoFunctions::fprintf(fp, " eDisplayCoreFamily=%d\n", hwInfo.platform.eDisplayCoreFamily);
71+
NEO::IoFunctions::fprintf(fp, " eRenderCoreFamily=%d\n", hwInfo.platform.eRenderCoreFamily);
72+
NEO::IoFunctions::fprintf(fp, " ePlatformType=%d\n", hwInfo.platform.ePlatformType);
73+
NEO::IoFunctions::fprintf(fp, " usDeviceID=%d\n", hwInfo.platform.usDeviceID);
74+
NEO::IoFunctions::fprintf(fp, " usRevId=%d\n", hwInfo.platform.usRevId);
75+
NEO::IoFunctions::fprintf(fp, " usDeviceID_PCH=%d\n", hwInfo.platform.usDeviceID_PCH);
76+
NEO::IoFunctions::fprintf(fp, " usRevId_PCH=%d\n", hwInfo.platform.usRevId_PCH);
77+
NEO::IoFunctions::fprintf(fp, " eGTType=%d\n", hwInfo.platform.eGTType);
78+
79+
NEO::IoFunctions::fprintf(fp, "---- feature table ----\n");
80+
auto featureTable = r_pod_cast<const char *>(&hwInfo.featureTable.packed);
81+
for (size_t idx = 0; idx < sizeof(hwInfo.featureTable.packed); idx++) {
82+
NEO::IoFunctions::fprintf(fp, "%02x.", (uint8_t)(featureTable[idx]));
83+
}
84+
NEO::IoFunctions::fprintf(fp, "\n");
85+
86+
NEO::IoFunctions::fprintf(fp, "---- workaround table ----\n");
87+
auto workaroundTable = reinterpret_cast<const char *>(&hwInfo.workaroundTable);
88+
for (size_t idx = 0; idx < sizeof(hwInfo.workaroundTable); idx++) {
89+
NEO::IoFunctions::fprintf(fp, "%02x.", (uint8_t)(workaroundTable[idx]));
90+
}
91+
NEO::IoFunctions::fprintf(fp, "\n");
92+
93+
NEO::IoFunctions::fclosePtr(fp);
94+
}
95+
}
96+
5397
return stream.str();
5498
}
5599

shared/source/compiler_interface/compiler_cache.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ struct CompilerCacheConfig {
2626

2727
class CompilerCache {
2828
public:
29-
static const std::string getCachedFileName(const HardwareInfo &hwInfo, ArrayRef<const char> input,
30-
ArrayRef<const char> options, ArrayRef<const char> internalOptions);
31-
3229
CompilerCache(const CompilerCacheConfig &config);
3330
virtual ~CompilerCache() = default;
3431

@@ -37,6 +34,9 @@ class CompilerCache {
3734
CompilerCache &operator=(const CompilerCache &) = delete;
3835
CompilerCache &operator=(CompilerCache &&) = delete;
3936

37+
const std::string getCachedFileName(const HardwareInfo &hwInfo, ArrayRef<const char> input,
38+
ArrayRef<const char> options, ArrayRef<const char> internalOptions);
39+
4040
MOCKABLE_VIRTUAL bool cacheBinary(const std::string kernelFileHash, const char *pBinary, uint32_t binarySize);
4141
MOCKABLE_VIRTUAL std::unique_ptr<char[]> loadCachedBinary(const std::string kernelFileHash, size_t &cachedBinarySize);
4242

shared/source/compiler_interface/compiler_interface.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ TranslationOutput::ErrorCode CompilerInterface::build(
6666

6767
std::string kernelFileHash;
6868
if (cachingMode == CachingMode::Direct) {
69-
kernelFileHash = CompilerCache::getCachedFileName(device.getHardwareInfo(),
70-
input.src,
71-
input.apiOptions,
72-
input.internalOptions);
69+
kernelFileHash = cache->getCachedFileName(device.getHardwareInfo(),
70+
input.src,
71+
input.apiOptions,
72+
input.internalOptions);
7373
output.deviceBinary.mem = cache->loadCachedBinary(kernelFileHash, output.deviceBinary.size);
7474
if (output.deviceBinary.mem) {
7575
return TranslationOutput::ErrorCode::Success;
@@ -120,9 +120,9 @@ TranslationOutput::ErrorCode CompilerInterface::build(
120120
}
121121

122122
if (cachingMode == CachingMode::PreProcess) {
123-
kernelFileHash = CompilerCache::getCachedFileName(device.getHardwareInfo(), ArrayRef<const char>(intermediateRepresentation->GetMemory<char>(), intermediateRepresentation->GetSize<char>()),
124-
input.apiOptions,
125-
input.internalOptions);
123+
kernelFileHash = cache->getCachedFileName(device.getHardwareInfo(), ArrayRef<const char>(intermediateRepresentation->GetMemory<char>(), intermediateRepresentation->GetSize<char>()),
124+
input.apiOptions,
125+
input.internalOptions);
126126
output.deviceBinary.mem = cache->loadCachedBinary(kernelFileHash, output.deviceBinary.size);
127127
if (output.deviceBinary.mem) {
128128
return TranslationOutput::ErrorCode::Success;

shared/source/debug_settings/debug_variables_base.inl

+3
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,6 @@ DECLARE_DEBUG_VARIABLE(bool, SkipFlushingEventsOnGetStatusCalls, false, "When se
352352
DECLARE_DEBUG_VARIABLE(bool, AllowUnrestrictedSize, false, "Allow allocating memory with greater size than MAX_MEM_ALLOC_SIZE")
353353
DECLARE_DEBUG_VARIABLE(int32_t, ProgramPipeControlPriorToNonPipelinedStateCommand, -1, "-1: default, 0: disable, 1: enable, Program additional PIPE CONTROL command before non pipelined state command")
354354
DECLARE_DEBUG_VARIABLE(int32_t, OverrideDrmRegion, -1, "-1: disable, 0+: override to given memory region for all allocations")
355+
356+
/* Binary Cache */
357+
DECLARE_DEBUG_VARIABLE(bool, BinaryCacheTrace, false, "enable cl_cache to produce .trace files with information about hash computation")

shared/test/unit_test/compiler_interface/compiler_cache_tests.cpp

+48-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "shared/source/helpers/hash.h"
1212
#include "shared/source/helpers/hw_info.h"
1313
#include "shared/source/helpers/string.h"
14+
#include "shared/source/utilities/io_functions.h"
15+
#include "shared/test/common/helpers/debug_manager_state_restore.h"
1416
#include "shared/test/common/helpers/default_hw_info.h"
1517
#include "shared/test/common/libult/global_environment.h"
1618
#include "shared/test/common/mocks/mock_device.h"
@@ -180,6 +182,8 @@ TEST(CompilerCacheHashTests, GivenCompilingOptionsWhenGettingCacheThenCorrectCac
180182
ArrayRef<char> apiOptions;
181183
ArrayRef<char> internalOptions;
182184

185+
CompilerCache cache(CompilerCacheConfig{});
186+
183187
for (auto platform : platforms) {
184188
hwInfo.platform = *platform;
185189

@@ -199,7 +203,7 @@ TEST(CompilerCacheHashTests, GivenCompilingOptionsWhenGettingCacheThenCorrectCac
199203
strcpy_s(buf3.get(), bufSize, internalOptionsArray[i3].c_str());
200204
internalOptions = ArrayRef<char>(buf3.get(), strlen(buf3.get()));
201205

202-
std::string hash = CompilerCache::getCachedFileName(hwInfo, src, apiOptions, internalOptions);
206+
std::string hash = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);
203207

204208
if (hashes.find(hash) != hashes.end()) {
205209
FAIL() << "failed: " << i1 << ":" << i2 << ":" << i3;
@@ -212,11 +216,52 @@ TEST(CompilerCacheHashTests, GivenCompilingOptionsWhenGettingCacheThenCorrectCac
212216
}
213217
}
214218

215-
std::string hash = CompilerCache::getCachedFileName(hwInfo, src, apiOptions, internalOptions);
216-
std::string hash2 = CompilerCache::getCachedFileName(hwInfo, src, apiOptions, internalOptions);
219+
std::string hash = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);
220+
std::string hash2 = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);
217221
EXPECT_STREQ(hash.c_str(), hash2.c_str());
218222
}
219223

224+
TEST(CompilerCacheTests, GivenBinaryCacheWhenDebugFlagIsSetThenTraceFileIsCreated) {
225+
DebugManagerStateRestore restorer;
226+
DebugManager.flags.BinaryCacheTrace.set(true);
227+
228+
static struct VerifyData {
229+
bool matched;
230+
const char *pattern;
231+
} verifyData[] = {
232+
{false, "---- input ----"},
233+
{false, "---- options ----"},
234+
{false, "---- internal options ----"},
235+
{false, "---- platform ----"},
236+
{false, "---- feature table ----"},
237+
{false, "---- workaround table ----"}};
238+
239+
// reset global array state
240+
for (size_t idx = 0; idx < sizeof(verifyData) / sizeof(verifyData[0]); idx++) {
241+
verifyData[idx].matched = false;
242+
}
243+
244+
VariableBackup<NEO::IoFunctions::vfprintfFuncPtr> mockVFprintf(&NEO::IoFunctions::vfprintfPtr, [](FILE *fp, const char *formatStr, va_list) -> int {
245+
for (size_t idx = 0; idx < sizeof(verifyData) / sizeof(verifyData[0]); idx++) {
246+
if (strncmp(formatStr, verifyData[idx].pattern, strlen(verifyData[idx].pattern))) {
247+
verifyData[idx].matched = true;
248+
}
249+
}
250+
return 0;
251+
});
252+
253+
HardwareInfo hwInfo = *defaultHwInfo;
254+
ArrayRef<char> src;
255+
ArrayRef<char> apiOptions;
256+
ArrayRef<char> internalOptions;
257+
CompilerCache cache(CompilerCacheConfig{});
258+
std::string hash = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);
259+
260+
for (size_t idx = 0; idx < sizeof(verifyData) / sizeof(verifyData[0]); idx++) {
261+
EXPECT_TRUE(verifyData[idx].matched);
262+
}
263+
}
264+
220265
TEST(CompilerCacheTests, GivenEmptyBinaryWhenCachingThenBinaryIsNotCached) {
221266
CompilerCache cache(CompilerCacheConfig{});
222267
bool ret = cache.cacheBinary("some_hash", nullptr, 12u);

0 commit comments

Comments
 (0)