diff --git a/runtime/hsa-runtime/core/inc/runtime.h b/runtime/hsa-runtime/core/inc/runtime.h index b76c35e97f..ea22340ec9 100644 --- a/runtime/hsa-runtime/core/inc/runtime.h +++ b/runtime/hsa-runtime/core/inc/runtime.h @@ -118,6 +118,9 @@ class Runtime { // @brief Callback handler for VM fault access. static bool VMFaultHandler(hsa_signal_value_t val, void* arg); + // @brief Print known allocations near ptr. + static void PrintMemoryMapNear(void* ptr); + /// @brief Singleton object of the runtime. static Runtime* runtime_singleton_; diff --git a/runtime/hsa-runtime/core/runtime/runtime.cpp b/runtime/hsa-runtime/core/runtime/runtime.cpp index 74f13ba6ec..69b2e5ed48 100644 --- a/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -1207,57 +1207,8 @@ bool Runtime::VMFaultHandler(hsa_signal_value_t val, void* arg) { (fault.Failure.Imprecise == 1) ? "(may not be exact address)" : "", reason.c_str()); #ifndef NDEBUG - runtime_singleton_->memory_lock_.Acquire(); - auto it = runtime_singleton_->allocation_map_.upper_bound( - reinterpret_cast(fault.VirtualAddress)); - for (int i = 0; i < 2; i++) { - if (it != runtime_singleton_->allocation_map_.begin()) it--; - } - fprintf(stderr, "Nearby memory map:\n"); - auto start = it; - for (int i = 0; i < 3; i++) { - if (it == runtime_singleton_->allocation_map_.end()) break; - std::string kind = "Non-HSA"; - if (it->second.region != nullptr) { - const amd::MemoryRegion* region = - static_cast(it->second.region); - if (region->IsSystem()) - kind = "System"; - else if (region->IsLocalMemory()) - kind = "VRAM"; - else if (region->IsScratch()) - kind = "Scratch"; - else if (region->IsLDS()) - kind = "LDS"; - } - fprintf(stderr, "%p, 0x%lx, %s\n", it->first, it->second.size, kind.c_str()); - it++; - } - fprintf(stderr, "\n"); - it = start; - runtime_singleton_->memory_lock_.Release(); - hsa_amd_pointer_info_t info; - PtrInfoBlockData block; - uint32_t count; - hsa_agent_t* canAccess; - info.size = sizeof(info); - for (int i = 0; i < 3; i++) { - if (it == runtime_singleton_->allocation_map_.end()) break; - runtime_singleton_->PtrInfo(const_cast(it->first), &info, malloc, &count, &canAccess, - &block); - fprintf(stderr, - "PtrInfo:\n\tAddress: %p-%p/%p-%p\n\tSize: 0x%lx\n\tType: %u\n\tOwner: %p\n", - info.agentBaseAddress, (char*)info.agentBaseAddress + info.sizeInBytes, - info.hostBaseAddress, (char*)info.hostBaseAddress + info.sizeInBytes, - info.sizeInBytes, info.type, reinterpret_cast(info.agentOwner.handle)); - fprintf(stderr, "\tCanAccess: %u\n", count); - for (int t = 0; t < count; t++) - fprintf(stderr, "\t\t%p\n", reinterpret_cast(canAccess[t].handle)); - fprintf(stderr, "\tIn block: %p, 0x%lx\n", block.base, block.length); - free(canAccess); - it++; - } -#endif //! NDEBUG + PrintMemoryMapNear(reinterpret_cast(fault.VirtualAddress)); +#endif } assert(false && "GPU memory access fault."); std::abort(); @@ -1266,6 +1217,56 @@ bool Runtime::VMFaultHandler(hsa_signal_value_t val, void* arg) { return false; } +void Runtime::PrintMemoryMapNear(void* ptr) { + runtime_singleton_->memory_lock_.Acquire(); + auto it = runtime_singleton_->allocation_map_.upper_bound(ptr); + for (int i = 0; i < 2; i++) { + if (it != runtime_singleton_->allocation_map_.begin()) it--; + } + fprintf(stderr, "Nearby memory map:\n"); + auto start = it; + for (int i = 0; i < 3; i++) { + if (it == runtime_singleton_->allocation_map_.end()) break; + std::string kind = "Non-HSA"; + if (it->second.region != nullptr) { + const amd::MemoryRegion* region = static_cast(it->second.region); + if (region->IsSystem()) + kind = "System"; + else if (region->IsLocalMemory()) + kind = "VRAM"; + else if (region->IsScratch()) + kind = "Scratch"; + else if (region->IsLDS()) + kind = "LDS"; + } + fprintf(stderr, "%p, 0x%lx, %s\n", it->first, it->second.size, kind.c_str()); + it++; + } + fprintf(stderr, "\n"); + it = start; + runtime_singleton_->memory_lock_.Release(); + hsa_amd_pointer_info_t info; + PtrInfoBlockData block; + uint32_t count; + hsa_agent_t* canAccess; + info.size = sizeof(info); + for (int i = 0; i < 3; i++) { + if (it == runtime_singleton_->allocation_map_.end()) break; + runtime_singleton_->PtrInfo(const_cast(it->first), &info, malloc, &count, &canAccess, + &block); + fprintf(stderr, "PtrInfo:\n\tAddress: %p-%p/%p-%p\n\tSize: 0x%lx\n\tType: %u\n\tOwner: %p\n", + info.agentBaseAddress, (char*)info.agentBaseAddress + info.sizeInBytes, + info.hostBaseAddress, (char*)info.hostBaseAddress + info.sizeInBytes, info.sizeInBytes, + info.type, reinterpret_cast(info.agentOwner.handle)); + fprintf(stderr, "\tCanAccess: %u\n", count); + for (int t = 0; t < count; t++) + fprintf(stderr, "\t\t%p\n", reinterpret_cast(canAccess[t].handle)); + fprintf(stderr, "\tIn block: %p, 0x%lx\n", block.base, block.length); + free(canAccess); + it++; + } +} + Runtime::Runtime() : region_gpu_(nullptr), sys_clock_freq_(0),