Reduce creating files when running ocloc tests

Add a new listener to track created files

Related-To: NEO-7126

Co-authored-by: Artur Harasimiuk <artur.harasimiuk@intel.com>
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski 2022-11-10 12:58:43 +00:00 committed by Compute-Runtime-Automation
parent 5206fd1b9a
commit 10c12bb3a7
10 changed files with 182 additions and 63 deletions

View File

@ -84,17 +84,19 @@ set(IGDRCL_SRCS_offline_compiler_tests
${CMAKE_CURRENT_SOURCE_DIR}/offline_linker_tests.h
${CMAKE_CURRENT_SOURCE_DIR}/stdout_capturer.h
${NEO_SHARED_DIRECTORY}/helpers/abort.cpp
${NEO_SHARED_DIRECTORY}/helpers/file_io.cpp
${NEO_SHARED_DIRECTORY}/helpers/file_io_load.cpp
${NEO_SHARED_DIRECTORY}/memory_manager/deferred_deleter.cpp
${NEO_SHARED_DIRECTORY}/memory_manager/deferred_deleter.h
${NEO_SHARED_TEST_DIRECTORY}/common/mocks/mock_compilers.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/mocks/mock_compilers.h
${NEO_SHARED_TEST_DIRECTORY}/common/mocks/mock_modules_zebin.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/mocks/mock_modules_zebin.h
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/file_io.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/test_files.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/libult/signal_utils.h
${NEO_SHARED_TEST_DIRECTORY}/common/libult/create_directory.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/test_macros/test_excludes.cpp
${NEO_SHARED_TEST_DIRECTORY}/common/helpers/virtual_file_system_listener.cpp
${IGDRCL_SRCS_cloc}
${IGDRCL_SRCS_offline_compiler_mock}
${NEO_CORE_tests_compiler_mocks}

View File

@ -8,6 +8,7 @@
#include "shared/source/os_interface/os_library.h"
#include "shared/test/common/helpers/custom_event_listener.h"
#include "shared/test/common/helpers/test_files.h"
#include "shared/test/common/helpers/virtual_file_system_listener.h"
#include "shared/test/common/libult/signal_utils.h"
#include "shared/test/common/test_stats.h"
@ -138,15 +139,15 @@ int main(int argc, char **argv) {
}
#endif
auto &listeners = ::testing::UnitTest::GetInstance()->listeners();
if (useDefaultListener == false) {
::testing::TestEventListeners &listeners = ::testing::UnitTest::GetInstance()->listeners();
::testing::TestEventListener *defaultListener = listeners.default_result_printer();
auto defaultListener = listeners.default_result_printer();
auto customEventListener = new CCustomEventListener(defaultListener);
listeners.Release(listeners.default_result_printer());
listeners.Release(defaultListener);
listeners.Append(customEventListener);
}
listeners.Append(new NEO::VirtualFileSystemListener);
gEnvironment = reinterpret_cast<Environment *>(::testing::AddGlobalTestEnvironment(new Environment(devicePrefix, familyNameWithType)));

View File

@ -34,26 +34,19 @@ class MockOclocArgHelper : public OclocArgHelper {
bool interceptOutput{false};
bool shouldLoadDataFromFileReturnZeroSize{false};
FilesMap interceptedFiles;
std::vector<std::string> createdFiles{};
bool callBaseFileExists = false;
bool callBaseReadBinaryFile = false;
bool callBaseLoadDataFromFile = false;
bool callBaseSaveOutput = false;
bool callBaseReadFileToVectorOfStrings = false;
bool shouldReturnEmptyVectorOfStrings = false;
MockOclocArgHelper(FilesMap &filesMap) : OclocArgHelper(0, nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr),
filesMap(filesMap){};
~MockOclocArgHelper() override {
cleanUpOutput();
}
void setAllCallBase(bool value) {
callBaseFileExists = value;
callBaseReadBinaryFile = value;
callBaseLoadDataFromFile = value;
callBaseSaveOutput = value;
callBaseReadFileToVectorOfStrings = value;
}
@ -120,21 +113,7 @@ class MockOclocArgHelper : public OclocArgHelper {
memcpy_s(fileContent.data(), fileContent.size(), pData, dataSize);
} else {
if (callBaseSaveOutput) {
createdFiles.push_back(filename.c_str());
}
OclocArgHelper::saveOutput(filename, pData, dataSize);
}
}
void cleanUpOutput() {
for (const auto &fileName : createdFiles) {
int retVal = remove(fileName.c_str());
EXPECT_EQ(0, retVal);
if (retVal != 0) {
auto errMsg = "Error deleting file: " + fileName;
perror(errMsg.c_str());
}
}
}
};

View File

@ -51,7 +51,6 @@ set(CLOC_LIB_SRCS_LIB
${NEO_SHARED_DIRECTORY}/helpers/cache_policy_bdw_and_later.inl
${NEO_SHARED_DIRECTORY}/helpers/cache_policy_dg2_and_later.inl
${NEO_SHARED_DIRECTORY}/helpers/debug_helpers.cpp
${NEO_SHARED_DIRECTORY}/helpers/file_io.cpp
${NEO_SHARED_DIRECTORY}/helpers/hw_info.cpp
${NEO_SHARED_DIRECTORY}/helpers/hw_info.h
${NEO_SHARED_DIRECTORY}/helpers/product_config_helper.cpp
@ -193,6 +192,8 @@ apply_macro_for_each_core_type("SUPPORTED")
set(CLOC_LIB_SRCS
${CLOC_LIB_SRCS_LIB}
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${NEO_SHARED_DIRECTORY}/helpers/file_io.cpp
${NEO_SHARED_DIRECTORY}/helpers/file_io_load.cpp
)
add_library(${OCLOC_NAME}_lib SHARED ${CLOC_LIB_SRCS})

View File

@ -55,6 +55,7 @@ set(NEO_CORE_HELPERS
${CMAKE_CURRENT_SOURCE_DIR}/engine_node_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/extendable_enum.h
${CMAKE_CURRENT_SOURCE_DIR}/file_io.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file_io_load.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file_io.h
${CMAKE_CURRENT_SOURCE_DIR}/flat_batch_buffer_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/flat_batch_buffer_helper.cpp

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
* Copyright (C) 2018-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@ -13,41 +13,6 @@
#include <cstring>
#include <new>
std::unique_ptr<char[]> loadDataFromFile(
const char *filename,
size_t &retSize) {
FILE *fp = nullptr;
size_t nsize = 0;
std::unique_ptr<char[]> ret;
DEBUG_BREAK_IF(nullptr == filename);
// Open the file
fopen_s(&fp, filename, "rb");
if (fp) {
// Allocate a buffer for the file contents
fseek(fp, 0, SEEK_END);
nsize = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET);
ret.reset(new (std::nothrow) char[nsize + 1]);
if (ret) {
// we initialize to all zeroes before reading in data
memset(ret.get(), 0x00, nsize + 1);
[[maybe_unused]] auto read = fread(ret.get(), sizeof(unsigned char), nsize, fp);
DEBUG_BREAK_IF(read != nsize);
} else {
nsize = 0;
}
fclose(fp);
}
retSize = nsize;
return ret;
}
size_t writeDataToFile(
const char *filename,
const void *pData,

View File

@ -0,0 +1,49 @@
/*
* Copyright (C) 2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/helpers/stdio.h"
#include "file_io.h"
#include <cstring>
#include <new>
std::unique_ptr<char[]> loadDataFromFile(
const char *filename,
size_t &retSize) {
FILE *fp = nullptr;
size_t nsize = 0;
std::unique_ptr<char[]> ret;
DEBUG_BREAK_IF(nullptr == filename);
// Open the file
fopen_s(&fp, filename, "rb");
if (fp) {
// Allocate a buffer for the file contents
fseek(fp, 0, SEEK_END);
nsize = static_cast<size_t>(ftell(fp));
fseek(fp, 0, SEEK_SET);
ret.reset(new (std::nothrow) char[nsize + 1]);
if (ret) {
// we initialize to all zeroes before reading in data
memset(ret.get(), 0x00, nsize + 1);
[[maybe_unused]] auto read = fread(ret.get(), sizeof(unsigned char), nsize, fp);
DEBUG_BREAK_IF(read != nsize);
} else {
nsize = 0;
}
fclose(fp);
}
retSize = nsize;
return ret;
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/helpers/stdio.h"
#include <cstring>
#include <new>
#include <set>
namespace NEO {
extern std::set<std::string> virtualFileList;
}
size_t writeDataToFile(
const char *filename,
const void *pData,
size_t dataSize) {
DEBUG_BREAK_IF(nullptr == pData);
DEBUG_BREAK_IF(nullptr == filename);
NEO::virtualFileList.insert(filename);
return dataSize;
}
bool fileExists(const std::string &fileName) {
FILE *pFile = nullptr;
DEBUG_BREAK_IF(fileName.empty());
DEBUG_BREAK_IF(fileName == "");
if (NEO::virtualFileList.count(fileName) > 0) {
return true;
}
fopen_s(&pFile, fileName.c_str(), "rb");
if (pFile) {
fclose(pFile);
}
return pFile != nullptr;
}
bool fileExistsHasSize(const std::string &fileName) {
FILE *pFile = nullptr;
size_t nsize = 0;
DEBUG_BREAK_IF(fileName.empty());
DEBUG_BREAK_IF(fileName == "");
fopen_s(&pFile, fileName.c_str(), "rb");
if (pFile) {
fseek(pFile, 0, SEEK_END);
nsize = (size_t)ftell(pFile);
fclose(pFile);
}
return pFile != nullptr && nsize > 0;
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/test/common/helpers/virtual_file_system_listener.h"
namespace NEO {
std::set<std::string> virtualFileList;
void VirtualFileSystemListener::OnTestStart(const ::testing::TestInfo &testInfo) {
virtualFileList.clear();
}
void VirtualFileSystemListener::OnTestEnd(const ::testing::TestInfo &) {}
void VirtualFileSystemListener::OnTestProgramStart(const testing::UnitTest &) {}
void VirtualFileSystemListener::OnTestIterationStart(const testing::UnitTest &, int) {}
void VirtualFileSystemListener::OnEnvironmentsSetUpStart(const testing::UnitTest &) {}
void VirtualFileSystemListener::OnEnvironmentsSetUpEnd(const testing::UnitTest &) {}
void VirtualFileSystemListener::OnTestPartResult(const testing::TestPartResult &) {}
void VirtualFileSystemListener::OnEnvironmentsTearDownStart(const testing::UnitTest &) {}
void VirtualFileSystemListener::OnEnvironmentsTearDownEnd(const testing::UnitTest &) {}
void VirtualFileSystemListener::OnTestIterationEnd(const testing::UnitTest &, int) {}
void VirtualFileSystemListener::OnTestProgramEnd(const testing::UnitTest &) {}
} // namespace NEO

View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "gtest/gtest.h"
namespace NEO {
class VirtualFileSystemListener : public ::testing::TestEventListener {
private:
void OnTestStart(const testing::TestInfo &) override;
void OnTestEnd(const testing::TestInfo &) override;
void OnTestProgramStart(const testing::UnitTest &) override;
void OnTestIterationStart(const testing::UnitTest &, int) override;
void OnEnvironmentsSetUpStart(const testing::UnitTest &) override;
void OnEnvironmentsSetUpEnd(const testing::UnitTest &) override;
void OnTestPartResult(const testing::TestPartResult &) override;
void OnEnvironmentsTearDownStart(const testing::UnitTest &) override;
void OnEnvironmentsTearDownEnd(const testing::UnitTest &) override;
void OnTestIterationEnd(const testing::UnitTest &, int) override;
void OnTestProgramEnd(const testing::UnitTest &) override;
};
} // namespace NEO