From 3e800d55e6f22f1cd9f313c1e8e869c3b2b5ba64 Mon Sep 17 00:00:00 2001 From: Katarzyna Cencelewska Date: Wed, 10 Oct 2018 16:02:19 +0200 Subject: [PATCH] Add support for dumping cl_cache to specified directory Change-Id: I782ff17d0d4b17d3d26db543eb7ae222f75ff8a1 --- runtime/compiler_interface/binary_cache.cpp | 23 +++++++------- runtime/compiler_interface/binary_cache.h | 10 +++---- .../os_interface/debug_settings_manager.cpp | 2 +- .../os_interface/linux/debug_env_reader.cpp | 7 +++++ .../windows/debug_registry_reader.cpp | 25 ++++++++++++---- .../os_interface/windows/registry_reader.h | 11 +++++-- .../os_interface/windows/windows_wrapper.h | 16 ++++++++++ runtime/utilities/debug_file_reader.cpp | 4 +++ runtime/utilities/debug_file_reader.h | 3 +- runtime/utilities/debug_settings_reader.h | 3 +- runtime/utilities/linux/debug_env_reader.h | 1 + .../helpers/debug_manager_state_restore.h | 4 +++ .../os_interface/linux/debug_env_reader.cpp | 12 ++++++++ .../os_interface/windows/CMakeLists.txt | 1 + .../windows/driver_info_tests.cpp | 2 +- .../windows/mock_registry_reader.cpp | 27 +++++++++++++++++ .../windows/registry_reader_tests.cpp | 30 +++++++++++++++---- .../utilities/debug_file_reader_tests.inl | 6 ++++ .../utilities/debug_settings_reader_tests.cpp | 6 ++++ 19 files changed, 158 insertions(+), 35 deletions(-) create mode 100644 unit_tests/os_interface/windows/mock_registry_reader.cpp diff --git a/runtime/compiler_interface/binary_cache.cpp b/runtime/compiler_interface/binary_cache.cpp index 496c3053ff..4645a48c16 100644 --- a/runtime/compiler_interface/binary_cache.cpp +++ b/runtime/compiler_interface/binary_cache.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include "os_inc.h" #include #include @@ -23,7 +25,6 @@ namespace OCLRT { std::mutex BinaryCache::cacheAccessMtx; - const std::string BinaryCache::getCachedFileName(const HardwareInfo &hwInfo, const ArrayRef input, const ArrayRef options, const ArrayRef internalOptions) { Hash hash; @@ -51,18 +52,22 @@ const std::string BinaryCache::getCachedFileName(const HardwareInfo &hwInfo, con return stream.str(); } +BinaryCache::BinaryCache() { + std::unique_ptr settingsReader(SettingsReader::createOsReader(CL_CACHE_LOCATION)); + clCacheLocation = settingsReader->getSetting(settingsReader->appSpecificLocation("cl_cache_dir"), static_cast(CL_CACHE_LOCATION)); +}; + +BinaryCache::~BinaryCache(){}; + bool BinaryCache::cacheBinary(const std::string kernelFileHash, const char *pBinary, uint32_t binarySize) { if (pBinary == nullptr || binarySize == 0) { return false; } - - std::string hashFilePath = CL_CACHE_LOCATION; - hashFilePath.append(Os::fileSeparator); - hashFilePath.append(kernelFileHash + ".cl_cache"); + std::string filePath = clCacheLocation + PATH_SEPARATOR + kernelFileHash + ".cl_cache"; std::lock_guard lock(cacheAccessMtx); if (writeDataToFile( - hashFilePath.c_str(), + filePath.c_str(), pBinary, binarySize) == 0) { return false; @@ -75,13 +80,11 @@ bool BinaryCache::loadCachedBinary(const std::string kernelFileHash, Program &pr void *pBinary = nullptr; size_t binarySize = 0; - std::string hashFilePath = CL_CACHE_LOCATION; - hashFilePath.append(Os::fileSeparator); - hashFilePath.append(kernelFileHash + ".cl_cache"); + std::string filePath = clCacheLocation + PATH_SEPARATOR + kernelFileHash + ".cl_cache"; { std::lock_guard lock(cacheAccessMtx); - binarySize = loadDataFromFile(hashFilePath.c_str(), pBinary); + binarySize = loadDataFromFile(filePath.c_str(), pBinary); } if ((pBinary == nullptr) || (binarySize == 0)) { diff --git a/runtime/compiler_interface/binary_cache.h b/runtime/compiler_interface/binary_cache.h index b9f157633d..09a800eaf9 100644 --- a/runtime/compiler_interface/binary_cache.h +++ b/runtime/compiler_interface/binary_cache.h @@ -11,25 +11,23 @@ #include #include #include - #include "runtime/utilities/arrayref.h" namespace OCLRT { - struct HardwareInfo; class Program; + class BinaryCache { public: static const std::string getCachedFileName(const HardwareInfo &hwInfo, ArrayRef input, ArrayRef options, ArrayRef internalOptions); - - virtual ~BinaryCache(){}; - + BinaryCache(); + virtual ~BinaryCache(); virtual bool cacheBinary(const std::string kernelFileHash, const char *pBinary, uint32_t binarySize); virtual bool loadCachedBinary(const std::string kernelFileHash, Program &program); protected: static std::mutex cacheAccessMtx; + std::string clCacheLocation; }; - } // namespace OCLRT diff --git a/runtime/os_interface/debug_settings_manager.cpp b/runtime/os_interface/debug_settings_manager.cpp index f386e19710..cd6b95c0f5 100644 --- a/runtime/os_interface/debug_settings_manager.cpp +++ b/runtime/os_interface/debug_settings_manager.cpp @@ -41,7 +41,7 @@ DebugSettingsManager::DebugSettingsManager() { } std::remove(logFileName.c_str()); -} +} // namespace OCLRT template void DebugSettingsManager::writeToFile(std::string filename, const char *str, size_t length, std::ios_base::openmode mode) { diff --git a/runtime/os_interface/linux/debug_env_reader.cpp b/runtime/os_interface/linux/debug_env_reader.cpp index b45eb6bb23..1816756e6f 100644 --- a/runtime/os_interface/linux/debug_env_reader.cpp +++ b/runtime/os_interface/linux/debug_env_reader.cpp @@ -13,6 +13,13 @@ SettingsReader *SettingsReader::createOsReader(bool userScope) { return new EnvironmentVariableReader; } +SettingsReader *SettingsReader::createOsReader(const std::string ®Key) { + return new EnvironmentVariableReader; +} +const char *EnvironmentVariableReader::appSpecificLocation(const std::string &name) { + return name.c_str(); +} + bool EnvironmentVariableReader::getSetting(const char *settingName, bool defaultValue) { return getSetting(settingName, static_cast(defaultValue)) ? true : false; } diff --git a/runtime/os_interface/windows/debug_registry_reader.cpp b/runtime/os_interface/windows/debug_registry_reader.cpp index f594398b3f..70c4974441 100644 --- a/runtime/os_interface/windows/debug_registry_reader.cpp +++ b/runtime/os_interface/windows/debug_registry_reader.cpp @@ -16,6 +16,22 @@ namespace OCLRT { SettingsReader *SettingsReader::createOsReader(bool userScope) { return new RegistryReader(userScope); } +SettingsReader *SettingsReader::createOsReader(const std::string ®Key) { + return new RegistryReader(regKey); +} +void RegistryReader::setUpProcessName() { + char buff[MAX_PATH]; + GetModuleFileNameA(nullptr, buff, MAX_PATH); + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + processName = ""; + } + processName.assign(buff); +} +const char *RegistryReader::appSpecificLocation(const std::string &name) { + if (processName.length() > 0) + return processName.c_str(); + return name.c_str(); +} bool RegistryReader::getSetting(const char *settingName, bool defaultValue) { return getSetting(settingName, static_cast(defaultValue)) ? true : false; @@ -27,7 +43,7 @@ int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue DWORD success = ERROR_SUCCESS; success = RegOpenKeyExA(igdrclHkeyType, - igdrclRegKey.c_str(), + registryReadRootKey.c_str(), 0, KEY_READ, &Key); @@ -52,11 +68,10 @@ int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue std::string RegistryReader::getSetting(const char *settingName, const std::string &value) { HKEY Key; DWORD success = ERROR_SUCCESS; - bool retFlag = false; std::string keyValue = value; success = RegOpenKeyExA(igdrclHkeyType, - igdrclRegKey.c_str(), + registryReadRootKey.c_str(), 0, KEY_READ, &Key); @@ -80,10 +95,8 @@ std::string RegistryReader::getSetting(const char *settingName, const std::strin ®Type, (LPBYTE)regData, ®Size); - keyValue.assign(regData); delete[] regData; - retFlag = true; } else if (success == ERROR_SUCCESS && regType == REG_BINARY) { std::unique_ptr regData(new wchar_t[regSize]); success = RegQueryValueExA(Key, @@ -99,11 +112,11 @@ std::string RegistryReader::getSetting(const char *settingName, const std::strin wcstombs_s(&charsConverted, convertedData.get(), regSize, regData.get(), regSize); keyValue.assign(convertedData.get()); - retFlag = true; } RegCloseKey(Key); } return keyValue; } + }; // namespace OCLRT diff --git a/runtime/os_interface/windows/registry_reader.h b/runtime/os_interface/windows/registry_reader.h index 3330c11c3d..40f297e4db 100644 --- a/runtime/os_interface/windows/registry_reader.h +++ b/runtime/os_interface/windows/registry_reader.h @@ -8,7 +8,7 @@ #pragma once #include "runtime/utilities/debug_settings_reader.h" - +#include "os_inc.h" #include #include #include @@ -21,13 +21,18 @@ class RegistryReader : public SettingsReader { std::string getSetting(const char *settingName, const std::string &value) override; RegistryReader(bool userScope) { igdrclHkeyType = userScope ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; + setUpProcessName(); } RegistryReader(std::string regKey) { - igdrclRegKey = regKey; + registryReadRootKey.append(std::string(1, PATH_SEPARATOR)).append(regKey); + setUpProcessName(); } + const char *appSpecificLocation(const std::string &name) override; protected: HKEY igdrclHkeyType = HKEY_LOCAL_MACHINE; - std::string igdrclRegKey = "Software\\Intel\\IGFX\\OCL"; + std::string registryReadRootKey = "Software\\Intel\\IGFX\\OCL"; + void setUpProcessName(); + std::string processName; }; } // namespace OCLRT diff --git a/runtime/os_interface/windows/windows_wrapper.h b/runtime/os_interface/windows/windows_wrapper.h index 77e61be4bb..19abcedfe2 100644 --- a/runtime/os_interface/windows/windows_wrapper.h +++ b/runtime/os_interface/windows/windows_wrapper.h @@ -14,3 +14,19 @@ // There is a conflict with max/min defined as macro in windows headers with std::max/std::min #undef min #undef max +#undef RegOpenKeyExA +#undef RegQueryValueExA +#pragma warning(disable : 4273) +LSTATUS APIENTRY RegOpenKeyExA( + HKEY hKey, + LPCSTR lpSubKey, + DWORD ulOptions, + REGSAM samDesired, + PHKEY phkResult); +LSTATUS APIENTRY RegQueryValueExA( + HKEY hKey, + LPCSTR lpValueName, + LPDWORD lpReserved, + LPDWORD lpType, + LPBYTE lpData, + LPDWORD lpcbData); diff --git a/runtime/utilities/debug_file_reader.cpp b/runtime/utilities/debug_file_reader.cpp index 70b91c7940..d3d1e477e9 100644 --- a/runtime/utilities/debug_file_reader.cpp +++ b/runtime/utilities/debug_file_reader.cpp @@ -82,4 +82,8 @@ std::string SettingsFileReader::getSetting(const char *settingName, const std::s return returnValue; } + +const char *SettingsFileReader::appSpecificLocation(const std::string &name) { + return name.c_str(); +} }; // namespace OCLRT diff --git a/runtime/utilities/debug_file_reader.h b/runtime/utilities/debug_file_reader.h index 0d465871d6..4a2b20f167 100644 --- a/runtime/utilities/debug_file_reader.h +++ b/runtime/utilities/debug_file_reader.h @@ -24,9 +24,10 @@ class SettingsFileReader : public SettingsReader { int32_t getSetting(const char *settingName, int32_t defaultValue) override; bool getSetting(const char *settingName, bool defaultValue) override; std::string getSetting(const char *settingName, const std::string &value) override; + const char *appSpecificLocation(const std::string &name) override; protected: std::map settingValueMap; std::map settingStringMap; }; -}; // namespace OCLRT \ No newline at end of file +}; // namespace OCLRT diff --git a/runtime/utilities/debug_settings_reader.h b/runtime/utilities/debug_settings_reader.h index 447e3523e8..a50f5a83f2 100644 --- a/runtime/utilities/debug_settings_reader.h +++ b/runtime/utilities/debug_settings_reader.h @@ -23,11 +23,12 @@ class SettingsReader { return createOsReader(false); } static SettingsReader *createOsReader(bool userScope); + static SettingsReader *createOsReader(const std::string ®Key); static SettingsReader *createFileReader(); virtual int32_t getSetting(const char *settingName, int32_t defaultValue) = 0; virtual bool getSetting(const char *settingName, bool defaultValue) = 0; virtual std::string getSetting(const char *settingName, const std::string &value) = 0; - + virtual const char *appSpecificLocation(const std::string &name) = 0; static const char *settingsFileName; }; }; // namespace OCLRT diff --git a/runtime/utilities/linux/debug_env_reader.h b/runtime/utilities/linux/debug_env_reader.h index f3dc284c8d..89af606e04 100644 --- a/runtime/utilities/linux/debug_env_reader.h +++ b/runtime/utilities/linux/debug_env_reader.h @@ -17,5 +17,6 @@ class EnvironmentVariableReader : public SettingsReader { int32_t getSetting(const char *settingName, int32_t defaultValue) override; bool getSetting(const char *settingName, bool defaultValue) override; std::string getSetting(const char *settingName, const std::string &value) override; + const char *appSpecificLocation(const std::string &name) override; }; } // namespace OCLRT diff --git a/unit_tests/helpers/debug_manager_state_restore.h b/unit_tests/helpers/debug_manager_state_restore.h index 53fb206617..4a0158f0be 100644 --- a/unit_tests/helpers/debug_manager_state_restore.h +++ b/unit_tests/helpers/debug_manager_state_restore.h @@ -49,7 +49,11 @@ class RegistryReaderMock : public SettingsReader { bool getSetting(const char *settingName, bool defaultValue) override { return true; } + std::string getSetting(const char *settingName, const std::string &value) override { return ""; } + const char *appSpecificLocation(const std::string &name) override { + return name.c_str(); + } }; diff --git a/unit_tests/os_interface/linux/debug_env_reader.cpp b/unit_tests/os_interface/linux/debug_env_reader.cpp index 695d22b71f..6e68b92713 100644 --- a/unit_tests/os_interface/linux/debug_env_reader.cpp +++ b/unit_tests/os_interface/linux/debug_env_reader.cpp @@ -6,6 +6,7 @@ */ #include "test.h" +#include #include "runtime/utilities/linux/debug_env_reader.h" namespace OCLRT { @@ -68,4 +69,15 @@ TEST_F(DebugEnvReaderTests, CheckBoolEnvVariable) { ret = evr->getSetting("TestingVariable", defaultValue); EXPECT_EQ(defaultValue, ret); } + +TEST_F(DebugEnvReaderTests, appSpecificLacationReturnClCacheLocation) { + std::string appSpecific; + appSpecific = "cl_cache_dir"; + EXPECT_EQ(appSpecific, evr->appSpecificLocation(appSpecific)); +} + +TEST_F(DebugEnvReaderTests, givenEnvironmentVariableReaderWhenCreateOsReaderWithStringThenNotNullPointer) { + std::unique_ptr evr(SettingsReader::createOsReader("")); + EXPECT_NE(nullptr, evr); +} } // namespace OCLRT diff --git a/unit_tests/os_interface/windows/CMakeLists.txt b/unit_tests/os_interface/windows/CMakeLists.txt index d3c0b36f12..9bbca4a77c 100644 --- a/unit_tests/os_interface/windows/CMakeLists.txt +++ b/unit_tests/os_interface/windows/CMakeLists.txt @@ -38,6 +38,7 @@ set(IGDRCL_SRCS_tests_os_interface_windows ${CMAKE_CURRENT_SOURCE_DIR}/wddm_memory_manager_allocate_in_device_pool_tests.inl ${CMAKE_CURRENT_SOURCE_DIR}/wddm_preemption_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/wddm_residency_controller_tests.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_registry_reader.cpp ) if(WIN32) file(GLOB IGDRCL_SRC_tests_wddm_interface "${CMAKE_CURRENT_SOURCE_DIR}/wddm2[0-9]_tests\.cpp") diff --git a/unit_tests/os_interface/windows/driver_info_tests.cpp b/unit_tests/os_interface/windows/driver_info_tests.cpp index 9372c178c0..b95cef234d 100644 --- a/unit_tests/os_interface/windows/driver_info_tests.cpp +++ b/unit_tests/os_interface/windows/driver_info_tests.cpp @@ -69,7 +69,6 @@ class RegistryReaderMock : public SettingsReader { public: std::string nameString; std::string versionString; - std::string getSetting(const char *settingName, const std::string &value) { std::string key(settingName); if (key == "HardwareInformation.AdapterString") { @@ -82,6 +81,7 @@ class RegistryReaderMock : public SettingsReader { bool getSetting(const char *settingName, bool defaultValue) { return defaultValue; }; int32_t getSetting(const char *settingName, int32_t defaultValue) { return defaultValue; }; + const char *appSpecificLocation(const std::string &name) { return name.c_str(); }; bool properNameKey = false; bool properVersionKey = false; diff --git a/unit_tests/os_interface/windows/mock_registry_reader.cpp b/unit_tests/os_interface/windows/mock_registry_reader.cpp new file mode 100644 index 0000000000..3017ef57c0 --- /dev/null +++ b/unit_tests/os_interface/windows/mock_registry_reader.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017-2018 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "windows_wrapper.h" + +LSTATUS APIENTRY RegOpenKeyExA( + HKEY hKey, + LPCSTR lpSubKey, + DWORD ulOptions, + REGSAM samDesired, + PHKEY phkResult) { + return ERROR_FILE_NOT_FOUND; +}; + +LSTATUS APIENTRY RegQueryValueExA( + HKEY hKey, + LPCSTR lpValueName, + LPDWORD lpReserved, + LPDWORD lpType, + LPBYTE lpData, + LPDWORD lpcbData) { + return ERROR_FILE_NOT_FOUND; +}; diff --git a/unit_tests/os_interface/windows/registry_reader_tests.cpp b/unit_tests/os_interface/windows/registry_reader_tests.cpp index bbefd312e1..7849909274 100644 --- a/unit_tests/os_interface/windows/registry_reader_tests.cpp +++ b/unit_tests/os_interface/windows/registry_reader_tests.cpp @@ -14,12 +14,12 @@ class TestedRegistryReader : public RegistryReader { public: TestedRegistryReader(bool userScope) : RegistryReader(userScope){}; TestedRegistryReader(std::string regKey) : RegistryReader(regKey){}; - HKEY getHkeyType() const { return igdrclHkeyType; } + using RegistryReader::getSetting; const char *getRegKey() const { - return igdrclRegKey.c_str(); + return registryReadRootKey.c_str(); } }; @@ -37,8 +37,26 @@ TEST_F(RegistryReaderTest, givenRegistryReaderWhenItIsCreatedWithUserScopeSetToT EXPECT_EQ(HKEY_CURRENT_USER, registryReader.getHkeyType()); } -TEST_F(RegistryReaderTest, givenRegistryReaderWhenItIsCreatedWithRegKeySpecifiedThenItsRegKeyIsInitializedAccordingly) { - std::string regKey = "Software\\Intel\\OpenCL"; - TestedRegistryReader registryReader(regKey); - EXPECT_STREQ("Software\\Intel\\OpenCL", registryReader.getRegKey()); +TEST_F(RegistryReaderTest, givenRegistryReaderWhenCallAppSpecificLocationThenReturnCurrentProcessName) { + char buff[MAX_PATH]; + GetModuleFileNameA(nullptr, buff, MAX_PATH); + + TestedRegistryReader registryReader(false); + const char *ret = registryReader.appSpecificLocation("cl_cache_dir"); + EXPECT_STREQ(buff, ret); +} + +TEST_F(RegistryReaderTest, givenRegistryReaderWhenRegKeyNotExistThenReturnDefaultValue) { + std::string regKey = "notExistPath"; + std::string value = "defaultValue"; + TestedRegistryReader registryReader(regKey); + + EXPECT_EQ(value, registryReader.getSetting("", value)); +} + +TEST_F(RegistryReaderTest, givenRegistryReaderWhenItIsCreatedWithRegKeySpecifiedThenRegKeyIsInitializedAccordingly) { + std::string regKey = "regKey"; + std::string defaultKey = "Software\\Intel\\IGFX\\OCL"; + TestedRegistryReader registryReader(regKey); + EXPECT_STREQ((defaultKey + "\\" + regKey).c_str(), registryReader.getRegKey()); } diff --git a/unit_tests/utilities/debug_file_reader_tests.inl b/unit_tests/utilities/debug_file_reader_tests.inl index e4efc1653e..9c8f7255ba 100644 --- a/unit_tests/utilities/debug_file_reader_tests.inl +++ b/unit_tests/utilities/debug_file_reader_tests.inl @@ -116,3 +116,9 @@ TEST(SettingsFileReader, GetSettingWhenNotInFile) { EXPECT_EQ(defaultStringValue, returnedStringValue); } + +TEST(SettingsFileReader, appSpecificLocation) { + std::unique_ptr reader(new TestSettingsFileReader(TestSettingsFileReader::testPath)); + std::string appSpecific = "cl_cache_dir"; + EXPECT_EQ(appSpecific, reader->appSpecificLocation(appSpecific)); +} diff --git a/unit_tests/utilities/debug_settings_reader_tests.cpp b/unit_tests/utilities/debug_settings_reader_tests.cpp index ac4a2fc6f5..14aedb074e 100644 --- a/unit_tests/utilities/debug_settings_reader_tests.cpp +++ b/unit_tests/utilities/debug_settings_reader_tests.cpp @@ -43,6 +43,12 @@ TEST(SettingsReader, CreateOsReader) { delete reader; } +TEST(SettingsReader, CreateOsReaderWithRegKey) { + std::string regKey = "Software\\Intel\\OpenCL"; + unique_ptr reader(SettingsReader::createOsReader(regKey)); + EXPECT_NE(nullptr, reader); +} + TEST(SettingsReader, givenPrintDebugStringWhenCalledWithTrueItPrintsToOutput) { int i = 4; testing::internal::CaptureStdout();