refactor: capability to print mmap and munmap calls

Signed-off-by: Bartosz Dunajski <bartosz.dunajski@intel.com>
This commit is contained in:
Bartosz Dunajski
2024-03-06 13:17:37 +00:00
committed by Compute-Runtime-Automation
parent 4b5d5f235a
commit fcd57f94cf
5 changed files with 55 additions and 0 deletions

View File

@@ -275,6 +275,7 @@ DECLARE_DEBUG_VARIABLE(int32_t, SetProcessPowerThrottlingState, -1, "-1: default
DECLARE_DEBUG_VARIABLE(int32_t, SetThreadPriority, -1, "-1: default, 0: Disabled, 1: Enabled. If set, will set thread priority to above normal on os context init. Windows only.")
DECLARE_DEBUG_VARIABLE(int32_t, OverrideCpuCaching, -1, "-1: default, 1: DRM_XE_GEM_CPU_CACHING_WB, 2: DRM_XE_GEM_CPU_CACHING_WC")
DECLARE_DEBUG_VARIABLE(int32_t, FlushTlbBeforeCopy, -1, "-1: default, 0: Dont flush, 1: flush TLB as part of MI_FLUSH_DW/PIPE_CONTROL command before copy operation")
DECLARE_DEBUG_VARIABLE(int32_t, PrintMmapAndMunMapCalls, -1, "-1: default, If set, print all system mmap and munmap calls")
/*LOGGING FLAGS*/
DECLARE_DEBUG_VARIABLE(int32_t, PrintDriverDiagnostics, -1, "prints driver diagnostics messages to standard output, value corresponds to hint level")

View File

@@ -53,6 +53,22 @@ namespace NEO {
using AllocationStatus = MemoryManager::AllocationStatus;
int debugMunmap(void *ptr, size_t size) noexcept {
int returnVal = munmap(ptr, size);
printf("\n%s: munmap(%p, %zu) = %d", __FUNCTION__, ptr, size, returnVal);
return returnVal;
}
void *debugMmap(void *ptr, size_t size, int prot, int flags, int fd, off_t offset) noexcept {
void *returnVal = mmap(ptr, size, prot, flags, fd, offset);
printf("\n%s: mmap(%p, %zu, %d, %d, %d, %ld) = %p", __FUNCTION__, ptr, size, prot, flags, fd, offset, returnVal);
return returnVal;
}
DrmMemoryManager::DrmMemoryManager(GemCloseWorkerMode mode,
bool forcePinAllowed,
bool validateHostPtrMemory,
@@ -60,6 +76,11 @@ DrmMemoryManager::DrmMemoryManager(GemCloseWorkerMode mode,
forcePinEnabled(forcePinAllowed),
validateHostPtrMemory(validateHostPtrMemory) {
if (debugManager.flags.PrintMmapAndMunMapCalls.get() == 1) {
this->munmapFunction = debugMunmap;
this->mmapFunction = debugMmap;
}
alignmentSelector.addCandidateAlignment(MemoryConstants::pageSize64k, true, AlignmentSelector::anyWastage, HeapIndex::heapStandard64KB);
if (debugManager.flags.AlignLocalMemoryVaTo2MB.get() != 0) {
alignmentSelector.addCandidateAlignment(MemoryConstants::pageSize2M, false, AlignmentSelector::anyWastage, HeapIndex::heapStandard2MB);

View File

@@ -187,6 +187,8 @@ struct MockDrmGemCloseWorker : DrmGemCloseWorker {
struct MockDrmMemoryManager : DrmMemoryManager {
using DrmMemoryManager::DrmMemoryManager;
using DrmMemoryManager::gemCloseWorker;
using DrmMemoryManager::mmapFunction;
using DrmMemoryManager::munmapFunction;
};
} // namespace NEO

View File

@@ -588,4 +588,5 @@ OverridePatIndexForUncachedTypes = -1
OverridePatIndexForCachedTypes = -1
FlushTlbBeforeCopy = -1
UseGemCreateExtInAllocateMemoryByKMD = -1
PrintMmapAndMunMapCalls = -1
# Please don't edit below this line

View File

@@ -205,6 +205,36 @@ TEST_F(DrmMemoryManagerTest, givenEnableDirectSubmissionWhenCreateDrmMemoryManag
EXPECT_EQ(memoryManager.peekGemCloseWorker(), nullptr);
}
TEST_F(DrmMemoryManagerTest, givenDebugFlagSetWhenUsingMmapFunctionsThenPrintContent) {
DebugManagerStateRestore dbgState;
debugManager.flags.PrintMmapAndMunMapCalls.set(1);
MockDrmMemoryManager memoryManager(GemCloseWorkerMode::gemCloseWorkerInactive, false, false, *executionEnvironment);
testing::internal::CaptureStdout();
size_t len = 2;
off_t offset = 6;
void *ptr = reinterpret_cast<void *>(0x1234);
void *retPtr = memoryManager.mmapFunction(ptr, len, 3, 4, 5, offset);
std::string output = testing::internal::GetCapturedStdout();
char expected1[256] = {};
sprintf(expected1, "mmap(%p, %zu, %d, %d, %d, %ld) = %p", ptr, len, 3, 4, 5, offset, retPtr);
EXPECT_NE(std::string::npos, output.find(expected1));
testing::internal::CaptureStdout();
int retVal = memoryManager.munmapFunction(ptr, len);
output = testing::internal::GetCapturedStdout();
char expected2[256] = {};
sprintf(expected2, "munmap(%p, %zu) = %d", ptr, len, retVal);
EXPECT_NE(std::string::npos, output.find(expected2));
}
TEST_F(DrmMemoryManagerTest, givenDebugVariableWhenCreatingDrmMemoryManagerThenSetSupportForMultiStorageResources) {
DebugManagerStateRestore dbgState;