From 35f6cd00eebf8ae9533d691342635610067f1b2d Mon Sep 17 00:00:00 2001 From: Lukasz Jobczyk Date: Wed, 22 Dec 2021 08:43:41 +0000 Subject: [PATCH] Add option to log allocations to std out Signed-off-by: Lukasz Jobczyk --- .../linux/file_logger_linux_tests.cpp | 53 +++++++++++++++++++ .../test/unit_test/test_files/igdrcl.config | 1 + .../debug_settings/debug_variables_base.inl | 1 + shared/source/utilities/logger.cpp | 19 ++++--- shared/source/utilities/logger.h | 1 + 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/opencl/test/unit_test/os_interface/linux/file_logger_linux_tests.cpp b/opencl/test/unit_test/os_interface/linux/file_logger_linux_tests.cpp index ca4b9c3417..5164646376 100644 --- a/opencl/test/unit_test/os_interface/linux/file_logger_linux_tests.cpp +++ b/opencl/test/unit_test/os_interface/linux/file_logger_linux_tests.cpp @@ -63,6 +63,59 @@ TEST(FileLogger, GivenLogAllocationMemoryPoolFlagThenLogsCorrectInfo) { } } +TEST(FileLogger, givenLogAllocationStdoutWhenLogAllocationThenLogToStdoutInsteadOfFileAndDoNotCreateFile) { + std::string testFile = "testfile"; + DebugVariables flags; + flags.LogAllocationMemoryPool.set(true); + flags.LogAllocationStdout.set(true); + FullyEnabledFileLogger fileLogger(testFile, flags); + + // Log file not created + bool logFileCreated = fileExists(fileLogger.getLogFileName()); + EXPECT_FALSE(logFileCreated); + auto executionEnvironment = std::make_unique(); + executionEnvironment->prepareRootDeviceEnvironments(1); + DrmMock drm(*executionEnvironment->rootDeviceEnvironments[0]); + + MockDrmAllocation allocation(GraphicsAllocation::AllocationType::BUFFER, MemoryPool::System64KBPages); + + allocation.setCpuPtrAndGpuAddress(&allocation, 0x12345); + + MockBufferObject bo(&drm); + bo.handle = 4; + + allocation.bufferObjects[0] = &bo; + + testing::internal::CaptureStdout(); + fileLogger.logAllocation(&allocation); + std::string output = testing::internal::GetCapturedStdout(); + + std::thread::id thisThread = std::this_thread::get_id(); + + std::stringstream threadIDCheck; + threadIDCheck << " ThreadID: " << thisThread; + + std::stringstream memoryPoolCheck; + memoryPoolCheck << " MemoryPool: " << allocation.getMemoryPool(); + + std::stringstream gpuAddressCheck; + gpuAddressCheck << " GPU address: 0x" << std::hex << allocation.getGpuAddress(); + + std::stringstream rootDeviceIndexCheck; + rootDeviceIndexCheck << " Root device index: " << allocation.getRootDeviceIndex(); + + EXPECT_TRUE(output.find(threadIDCheck.str()) != std::string::npos); + EXPECT_TRUE(output.find(memoryPoolCheck.str()) != std::string::npos); + EXPECT_TRUE(output.find(gpuAddressCheck.str()) != std::string::npos); + EXPECT_TRUE(output.find(rootDeviceIndexCheck.str()) != std::string::npos); + EXPECT_TRUE(output.find("AllocationType: BUFFER") != std::string::npos); + EXPECT_TRUE(output.find("Handle: 4") != std::string::npos); + EXPECT_TRUE(output.find("\n") != std::string::npos); + + logFileCreated = fileExists(fileLogger.getLogFileName()); + EXPECT_FALSE(logFileCreated); +} + TEST(FileLogger, GivenDrmAllocationWithoutBOThenNoHandleLogged) { std::string testFile = "testfile"; DebugVariables flags; diff --git a/opencl/test/unit_test/test_files/igdrcl.config b/opencl/test/unit_test/test_files/igdrcl.config index 80ca7bbd24..5adbe48654 100644 --- a/opencl/test/unit_test/test_files/igdrcl.config +++ b/opencl/test/unit_test/test_files/igdrcl.config @@ -329,6 +329,7 @@ AllowUnrestrictedSize = 0 DoNotFreeResources = 0 OverrideGmmResourceUsageField = -1 LogAllocationType = 0 +LogAllocationStdout = 0 ProgramPipeControlPriorToNonPipelinedStateCommand = -1 ProgramWalkerPartitionSelfCleanup = -1 WparidRegisterProgramming = -1 diff --git a/shared/source/debug_settings/debug_variables_base.inl b/shared/source/debug_settings/debug_variables_base.inl index 47ac94ae48..599c773ad8 100644 --- a/shared/source/debug_settings/debug_variables_base.inl +++ b/shared/source/debug_settings/debug_variables_base.inl @@ -196,6 +196,7 @@ DECLARE_DEBUG_VARIABLE(bool, LogTaskCounts, false, "Enables logging taskCounts a DECLARE_DEBUG_VARIABLE(bool, LogAlignedAllocations, false, "Logs alignedMalloc and alignedFree allocations") DECLARE_DEBUG_VARIABLE(bool, LogAllocationMemoryPool, false, "Logs memory pool for allocations") DECLARE_DEBUG_VARIABLE(bool, LogAllocationType, false, "Logs allocation type to sdout") +DECLARE_DEBUG_VARIABLE(bool, LogAllocationStdout, false, "Log allocations to std out instead of file") DECLARE_DEBUG_VARIABLE(bool, LogMemoryObject, false, "Logs memory object ptrs, sizes and operations") DECLARE_DEBUG_VARIABLE(bool, LogWaitingForCompletion, false, "Logs waiting for completion") DECLARE_DEBUG_VARIABLE(bool, ResidencyDebugEnable, false, "enables debug messages and checks for Residency Model") diff --git a/shared/source/utilities/logger.cpp b/shared/source/utilities/logger.cpp index 8dd9be34b5..d10688c74d 100644 --- a/shared/source/utilities/logger.cpp +++ b/shared/source/utilities/logger.cpp @@ -29,6 +29,7 @@ FileLogger::FileLogger(std::string filename, const DebugVariables &f logApiCalls = flags.LogApiCalls.get(); logAllocationMemoryPool = flags.LogAllocationMemoryPool.get(); logAllocationType = flags.LogAllocationType.get(); + logAllocationStdout = flags.LogAllocationStdout.get(); } template @@ -84,14 +85,10 @@ void FileLogger::logAllocation(GraphicsAllocation const *graphicsAll printDebugString(true, stdout, "Created Graphics Allocation of type %s\n", getAllocationTypeString(graphicsAllocation)); } - if (false == enabled()) { - return; - } - + std::stringstream ss; if (logAllocationMemoryPool || logAllocationType) { std::thread::id thisThread = std::this_thread::get_id(); - std::stringstream ss; ss << " ThreadID: " << thisThread; ss << " AllocationType: " << getAllocationTypeString(graphicsAllocation); ss << " MemoryPool: " << graphicsAllocation->getMemoryPool(); @@ -100,9 +97,19 @@ void FileLogger::logAllocation(GraphicsAllocation const *graphicsAll ss << graphicsAllocation->getAllocationInfoString(); ss << std::endl; + } + auto str = ss.str(); - auto str = ss.str(); + if (logAllocationStdout) { + printf("%s", str.c_str()); + return; + } + if (false == enabled()) { + return; + } + + if (logAllocationMemoryPool || logAllocationType) { writeToFile(logFileName, str.c_str(), str.size(), std::ios::app); } } diff --git a/shared/source/utilities/logger.h b/shared/source/utilities/logger.h index 83ddf1309e..f8571afe11 100644 --- a/shared/source/utilities/logger.h +++ b/shared/source/utilities/logger.h @@ -142,6 +142,7 @@ class FileLogger { bool logApiCalls = false; bool logAllocationMemoryPool = false; bool logAllocationType = false; + bool logAllocationStdout = false; // Required for variadic template with 0 args passed void printInputs(std::stringstream &ss) {}