Skip to content

Commit

Permalink
Add process safety to cl_cache on Linux
Browse files Browse the repository at this point in the history
Current flow will be to have one synchronization point
config.file. Read remains unblocking, only write(caching)
operation will be blocking (lock on config.file)

Related-To: NEO-4262

Signed-off-by: Diedrich, Kamil <kamil.diedrich@intel.com>
  • Loading branch information
kamdiedrich authored and Compute-Runtime-Automation committed Apr 25, 2023
1 parent c9fdeb2 commit 26ca64b
Show file tree
Hide file tree
Showing 15 changed files with 1,091 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2022 Intel Corporation
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand All @@ -9,6 +9,7 @@

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

#include "shared/source/helpers/constants.h"
#include "shared/source/utilities/debug_settings_reader.h"

#include "level_zero/core/source/compiler_interface/l0_reg_path.h"
Expand All @@ -24,6 +25,7 @@ CompilerCacheConfig getDefaultCompilerCacheConfig() {
std::unique_ptr<NEO::SettingsReader> settingsReader(NEO::SettingsReader::createOsReader(false, keyName));
ret.cacheDir = settingsReader->getSetting(settingsReader->appSpecificLocation(keyName), static_cast<std::string>(L0_CACHE_LOCATION));

ret.cacheSize = MemoryConstants::gigaByte;
ret.cacheFileExtension = ".l0_cache";

return ret;
Expand Down
4 changes: 3 additions & 1 deletion opencl/source/compiler_interface/default_cache_config.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* Copyright (C) 2020-2022 Intel Corporation
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

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

#include "shared/source/helpers/constants.h"
#include "shared/source/utilities/debug_settings_reader.h"

#include "opencl/source/os_interface/ocl_reg_path.h"
Expand All @@ -25,6 +26,7 @@ CompilerCacheConfig getDefaultCompilerCacheConfig() {
std::unique_ptr<SettingsReader> settingsReader(SettingsReader::createOsReader(false, keyName));
ret.cacheDir = settingsReader->getSetting(settingsReader->appSpecificLocation(keyName), static_cast<std::string>(CL_CACHE_LOCATION));

ret.cacheSize = MemoryConstants::gigaByte;
ret.cacheFileExtension = ".cl_cache";

return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,3 @@ TEST(CompilerCache, GivenDefaultClCacheConfigThenValuesAreProperlyPopulated) {
EXPECT_STREQ(".cl_cache", cacheConfig.cacheFileExtension.c_str());
EXPECT_TRUE(cacheConfig.enabled);
}

TEST(CompilerCacheTests, GivenExistingConfigWhenLoadingFromCacheThenBinaryIsLoaded) {
NEO::CompilerCache cache(NEO::getDefaultCompilerCacheConfig());
static const char *hash = "SOME_HASH";
std::unique_ptr<char[]> data(new char[32]);
for (size_t i = 0; i < 32; i++)
data.get()[i] = static_cast<char>(i);

bool ret = cache.cacheBinary(hash, static_cast<const char *>(data.get()), 32);
EXPECT_TRUE(ret);

size_t size;
auto loadedBin = cache.loadCachedBinary(hash, size);
EXPECT_NE(nullptr, loadedBin);
EXPECT_NE(0U, size);
}
2 changes: 2 additions & 0 deletions shared/offline_compiler/source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ endif()

if(WIN32)
list(APPEND CLOC_LIB_SRCS_LIB
${NEO_SHARED_DIRECTORY}/compiler_interface/windows/compiler_cache_windows.cpp
${NEO_SHARED_DIRECTORY}/dll/windows/options_windows.cpp
${NEO_SHARED_DIRECTORY}/os_interface/windows/os_inc.h
${NEO_SHARED_DIRECTORY}/os_interface/windows/os_library_win.cpp
Expand All @@ -132,6 +133,7 @@ if(WIN32)
)
else()
list(APPEND CLOC_LIB_SRCS_LIB
${NEO_SHARED_DIRECTORY}/compiler_interface/linux/compiler_cache_linux.cpp
${NEO_SHARED_DIRECTORY}/dll/linux/options_linux.cpp
${NEO_SHARED_DIRECTORY}/os_interface/linux/os_inc.h
${NEO_SHARED_DIRECTORY}/os_interface/linux/os_library_linux.cpp
Expand Down
11 changes: 11 additions & 0 deletions shared/source/compiler_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ set(NEO_CORE_COMPILER_INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/tokenized_string.h
)

if(WIN32)
list(APPEND NEO_CORE_COMPILER_INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/windows/compiler_cache_windows.cpp
)
else()
list(APPEND NEO_CORE_COMPILER_INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/linux/compiler_cache_linux.cpp
)
endif()

set_property(GLOBAL PROPERTY NEO_CORE_COMPILER_INTERFACE ${NEO_CORE_COMPILER_INTERFACE})
add_subdirectories()
19 changes: 2 additions & 17 deletions shared/source/compiler_interface/compiler_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2022 Intel Corporation
* Copyright (C) 2019-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -27,6 +27,7 @@

namespace NEO {
std::mutex CompilerCache::cacheAccessMtx;

const std::string CompilerCache::getCachedFileName(const HardwareInfo &hwInfo, const ArrayRef<const char> input,
const ArrayRef<const char> options, const ArrayRef<const char> internalOptions) {
Hash hash;
Expand Down Expand Up @@ -109,20 +110,4 @@ const std::string CompilerCache::getCachedFileName(const HardwareInfo &hwInfo, c
CompilerCache::CompilerCache(const CompilerCacheConfig &cacheConfig)
: config(cacheConfig){};

bool CompilerCache::cacheBinary(const std::string kernelFileHash, const char *pBinary, uint32_t binarySize) {
if (pBinary == nullptr || binarySize == 0) {
return false;
}
std::string filePath = config.cacheDir + PATH_SEPARATOR + kernelFileHash + config.cacheFileExtension;
std::lock_guard<std::mutex> lock(cacheAccessMtx);
return 0 != writeDataToFile(filePath.c_str(), pBinary, binarySize);
}

std::unique_ptr<char[]> CompilerCache::loadCachedBinary(const std::string kernelFileHash, size_t &cachedBinarySize) {
std::string filePath = config.cacheDir + PATH_SEPARATOR + kernelFileHash + config.cacheFileExtension;

std::lock_guard<std::mutex> lock(cacheAccessMtx);
return loadDataFromFile(filePath.c_str(), cachedBinarySize);
}

} // namespace NEO
11 changes: 9 additions & 2 deletions shared/source/compiler_interface/compiler_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>

namespace NEO {
struct HardwareInfo;
Expand All @@ -21,6 +22,7 @@ struct CompilerCacheConfig {
bool enabled = true;
std::string cacheFileExtension;
std::string cacheDir;
size_t cacheSize = 0;
};

class CompilerCache {
Expand All @@ -36,10 +38,15 @@ class CompilerCache {
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);
MOCKABLE_VIRTUAL bool cacheBinary(const std::string &kernelFileHash, const char *pBinary, size_t binarySize);
MOCKABLE_VIRTUAL std::unique_ptr<char[]> loadCachedBinary(const std::string &kernelFileHash, size_t &cachedBinarySize);

protected:
MOCKABLE_VIRTUAL bool evictCache();
MOCKABLE_VIRTUAL bool renameTempFileBinaryToProperName(const std::string &oldName, const std::string &kernelFileHash);
MOCKABLE_VIRTUAL bool createUniqueTempFileAndWriteData(char *tmpFilePathTemplate, const char *pBinary, size_t binarySize);
MOCKABLE_VIRTUAL void lockConfigFileAndReadSize(const std::string &configFilePath, int &fd, size_t &directorySize);

static std::mutex cacheAccessMtx;
CompilerCacheConfig config;
};
Expand Down

0 comments on commit 26ca64b

Please sign in to comment.