Skip to content

Commit

Permalink
create trace files about cl_cache usage
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Harasimiuk <[email protected]>
  • Loading branch information
ArturHarasimiuk authored and Compute-Runtime-Automation committed Oct 29, 2021
1 parent ad8e640 commit 53223dc
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 14 deletions.
3 changes: 2 additions & 1 deletion opencl/test/unit_test/mocks/mock_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ ClDeviceVector toClDeviceVector(ClDevice &clDevice) {
int MockProgram::initInternalOptionsCalled = 0;

std::string MockProgram::getCachedFileName() const {
CompilerCache cache(CompilerCacheConfig{});
auto hwInfo = this->context->getDevice(0)->getHardwareInfo();
auto input = ArrayRef<const char>(this->sourceCode.c_str(), this->sourceCode.size());
auto opts = ArrayRef<const char>(this->options.c_str(), this->options.size());
auto internalOptions = getInitInternalOptions();
auto internalOpts = ArrayRef<const char>(internalOptions.c_str(), internalOptions.size());
return CompilerCache::getCachedFileName(hwInfo, input, opts, internalOpts);
return cache.getCachedFileName(hwInfo, input, opts, internalOpts);
}

} // namespace NEO
1 change: 1 addition & 0 deletions opencl/test/unit_test/test_files/igdrcl.config
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,4 @@ AllowPatchingVfeStateInCommandLists = 0
PrintMemoryRegionSizes = 0
OverrideDrmRegion = -1
AllowSingleTileEngineInstancedSubDevices = 0
BinaryCacheTrace = false
44 changes: 44 additions & 0 deletions shared/source/compiler_interface/compiler_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

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

#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/casts.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/hash.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/utilities/debug_settings_reader.h"
#include "shared/source/utilities/io_functions.h"

#include "config.h"
#include "os_inc.h"
Expand Down Expand Up @@ -50,6 +52,48 @@ const std::string CompilerCache::getCachedFileName(const HardwareInfo &hwInfo, c
<< std::hex
<< res;

if (DebugManager.flags.BinaryCacheTrace.get()) {
std::string filePath = config.cacheDir + PATH_SEPARATOR + stream.str() + ".trace";
std::lock_guard<std::mutex> lock(cacheAccessMtx);
auto fp = NEO::IoFunctions::fopenPtr(filePath.c_str(), "w");
if (fp) {
NEO::IoFunctions::fprintf(fp, "---- input ----\n");
NEO::IoFunctions::fprintf(fp, "%s\n", &*input.begin());
NEO::IoFunctions::fprintf(fp, "---- options ----\n");
NEO::IoFunctions::fprintf(fp, "%s\n", &*options.begin());
NEO::IoFunctions::fprintf(fp, "---- internal options ----\n");
NEO::IoFunctions::fprintf(fp, "%s\n", &*internalOptions.begin());

NEO::IoFunctions::fprintf(fp, "---- platform ----\n");
NEO::IoFunctions::fprintf(fp, " eProductFamily=%d\n", hwInfo.platform.eProductFamily);
NEO::IoFunctions::fprintf(fp, " ePCHProductFamily=%d\n", hwInfo.platform.ePCHProductFamily);
NEO::IoFunctions::fprintf(fp, " eDisplayCoreFamily=%d\n", hwInfo.platform.eDisplayCoreFamily);
NEO::IoFunctions::fprintf(fp, " eRenderCoreFamily=%d\n", hwInfo.platform.eRenderCoreFamily);
NEO::IoFunctions::fprintf(fp, " ePlatformType=%d\n", hwInfo.platform.ePlatformType);
NEO::IoFunctions::fprintf(fp, " usDeviceID=%d\n", hwInfo.platform.usDeviceID);
NEO::IoFunctions::fprintf(fp, " usRevId=%d\n", hwInfo.platform.usRevId);
NEO::IoFunctions::fprintf(fp, " usDeviceID_PCH=%d\n", hwInfo.platform.usDeviceID_PCH);
NEO::IoFunctions::fprintf(fp, " usRevId_PCH=%d\n", hwInfo.platform.usRevId_PCH);
NEO::IoFunctions::fprintf(fp, " eGTType=%d\n", hwInfo.platform.eGTType);

NEO::IoFunctions::fprintf(fp, "---- feature table ----\n");
auto featureTable = r_pod_cast<const char *>(&hwInfo.featureTable.packed);
for (size_t idx = 0; idx < sizeof(hwInfo.featureTable.packed); idx++) {
NEO::IoFunctions::fprintf(fp, "%02x.", (uint8_t)(featureTable[idx]));
}
NEO::IoFunctions::fprintf(fp, "\n");

NEO::IoFunctions::fprintf(fp, "---- workaround table ----\n");
auto workaroundTable = reinterpret_cast<const char *>(&hwInfo.workaroundTable);
for (size_t idx = 0; idx < sizeof(hwInfo.workaroundTable); idx++) {
NEO::IoFunctions::fprintf(fp, "%02x.", (uint8_t)(workaroundTable[idx]));
}
NEO::IoFunctions::fprintf(fp, "\n");

NEO::IoFunctions::fclosePtr(fp);
}
}

return stream.str();
}

Expand Down
6 changes: 3 additions & 3 deletions shared/source/compiler_interface/compiler_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ struct CompilerCacheConfig {

class CompilerCache {
public:
static const std::string getCachedFileName(const HardwareInfo &hwInfo, ArrayRef<const char> input,
ArrayRef<const char> options, ArrayRef<const char> internalOptions);

CompilerCache(const CompilerCacheConfig &config);
virtual ~CompilerCache() = default;

Expand All @@ -37,6 +34,9 @@ class CompilerCache {
CompilerCache &operator=(const CompilerCache &) = delete;
CompilerCache &operator=(CompilerCache &&) = delete;

const std::string getCachedFileName(const HardwareInfo &hwInfo, ArrayRef<const char> input,
ArrayRef<const char> options, ArrayRef<const char> internalOptions);

MOCKABLE_VIRTUAL bool cacheBinary(const std::string kernelFileHash, const char *pBinary, uint32_t binarySize);
MOCKABLE_VIRTUAL std::unique_ptr<char[]> loadCachedBinary(const std::string kernelFileHash, size_t &cachedBinarySize);

Expand Down
14 changes: 7 additions & 7 deletions shared/source/compiler_interface/compiler_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ TranslationOutput::ErrorCode CompilerInterface::build(

std::string kernelFileHash;
if (cachingMode == CachingMode::Direct) {
kernelFileHash = CompilerCache::getCachedFileName(device.getHardwareInfo(),
input.src,
input.apiOptions,
input.internalOptions);
kernelFileHash = cache->getCachedFileName(device.getHardwareInfo(),
input.src,
input.apiOptions,
input.internalOptions);
output.deviceBinary.mem = cache->loadCachedBinary(kernelFileHash, output.deviceBinary.size);
if (output.deviceBinary.mem) {
return TranslationOutput::ErrorCode::Success;
Expand Down Expand Up @@ -120,9 +120,9 @@ TranslationOutput::ErrorCode CompilerInterface::build(
}

if (cachingMode == CachingMode::PreProcess) {
kernelFileHash = CompilerCache::getCachedFileName(device.getHardwareInfo(), ArrayRef<const char>(intermediateRepresentation->GetMemory<char>(), intermediateRepresentation->GetSize<char>()),
input.apiOptions,
input.internalOptions);
kernelFileHash = cache->getCachedFileName(device.getHardwareInfo(), ArrayRef<const char>(intermediateRepresentation->GetMemory<char>(), intermediateRepresentation->GetSize<char>()),
input.apiOptions,
input.internalOptions);
output.deviceBinary.mem = cache->loadCachedBinary(kernelFileHash, output.deviceBinary.size);
if (output.deviceBinary.mem) {
return TranslationOutput::ErrorCode::Success;
Expand Down
3 changes: 3 additions & 0 deletions shared/source/debug_settings/debug_variables_base.inl
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,6 @@ DECLARE_DEBUG_VARIABLE(bool, SkipFlushingEventsOnGetStatusCalls, false, "When se
DECLARE_DEBUG_VARIABLE(bool, AllowUnrestrictedSize, false, "Allow allocating memory with greater size than MAX_MEM_ALLOC_SIZE")
DECLARE_DEBUG_VARIABLE(int32_t, ProgramPipeControlPriorToNonPipelinedStateCommand, -1, "-1: default, 0: disable, 1: enable, Program additional PIPE CONTROL command before non pipelined state command")
DECLARE_DEBUG_VARIABLE(int32_t, OverrideDrmRegion, -1, "-1: disable, 0+: override to given memory region for all allocations")

/* Binary Cache */
DECLARE_DEBUG_VARIABLE(bool, BinaryCacheTrace, false, "enable cl_cache to produce .trace files with information about hash computation")
51 changes: 48 additions & 3 deletions shared/test/unit_test/compiler_interface/compiler_cache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "shared/source/helpers/hash.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/string.h"
#include "shared/source/utilities/io_functions.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/default_hw_info.h"
#include "shared/test/common/libult/global_environment.h"
#include "shared/test/common/mocks/mock_device.h"
Expand Down Expand Up @@ -180,6 +182,8 @@ TEST(CompilerCacheHashTests, GivenCompilingOptionsWhenGettingCacheThenCorrectCac
ArrayRef<char> apiOptions;
ArrayRef<char> internalOptions;

CompilerCache cache(CompilerCacheConfig{});

for (auto platform : platforms) {
hwInfo.platform = *platform;

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

std::string hash = CompilerCache::getCachedFileName(hwInfo, src, apiOptions, internalOptions);
std::string hash = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);

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

std::string hash = CompilerCache::getCachedFileName(hwInfo, src, apiOptions, internalOptions);
std::string hash2 = CompilerCache::getCachedFileName(hwInfo, src, apiOptions, internalOptions);
std::string hash = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);
std::string hash2 = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);
EXPECT_STREQ(hash.c_str(), hash2.c_str());
}

TEST(CompilerCacheTests, GivenBinaryCacheWhenDebugFlagIsSetThenTraceFileIsCreated) {
DebugManagerStateRestore restorer;
DebugManager.flags.BinaryCacheTrace.set(true);

static struct VerifyData {
bool matched;
const char *pattern;
} verifyData[] = {
{false, "---- input ----"},
{false, "---- options ----"},
{false, "---- internal options ----"},
{false, "---- platform ----"},
{false, "---- feature table ----"},
{false, "---- workaround table ----"}};

// reset global array state
for (size_t idx = 0; idx < sizeof(verifyData) / sizeof(verifyData[0]); idx++) {
verifyData[idx].matched = false;
}

VariableBackup<NEO::IoFunctions::vfprintfFuncPtr> mockVFprintf(&NEO::IoFunctions::vfprintfPtr, [](FILE *fp, const char *formatStr, va_list) -> int {
for (size_t idx = 0; idx < sizeof(verifyData) / sizeof(verifyData[0]); idx++) {
if (strncmp(formatStr, verifyData[idx].pattern, strlen(verifyData[idx].pattern))) {
verifyData[idx].matched = true;
}
}
return 0;
});

HardwareInfo hwInfo = *defaultHwInfo;
ArrayRef<char> src;
ArrayRef<char> apiOptions;
ArrayRef<char> internalOptions;
CompilerCache cache(CompilerCacheConfig{});
std::string hash = cache.getCachedFileName(hwInfo, src, apiOptions, internalOptions);

for (size_t idx = 0; idx < sizeof(verifyData) / sizeof(verifyData[0]); idx++) {
EXPECT_TRUE(verifyData[idx].matched);
}
}

TEST(CompilerCacheTests, GivenEmptyBinaryWhenCachingThenBinaryIsNotCached) {
CompilerCache cache(CompilerCacheConfig{});
bool ret = cache.cacheBinary("some_hash", nullptr, 12u);
Expand Down

0 comments on commit 53223dc

Please sign in to comment.