diff --git a/rocclr/device/pal/paldevice.cpp b/rocclr/device/pal/paldevice.cpp index 5f29fa4460..6f65ad422b 100644 --- a/rocclr/device/pal/paldevice.cpp +++ b/rocclr/device/pal/paldevice.cpp @@ -1961,15 +1961,15 @@ bool Device::unbindExternalDevice(uint flags, void* const pDevice[], void* pCont } bool Device::globalFreeMemory(size_t* freeMemory) const { - const uint TotalFreeMemory = 0; - const uint LargestFreeBlock = 1; + constexpr uint32_t TotalFreeMemory = 0; + constexpr uint32_t LargestFreeBlock = 1; // Initialization of heap and other resources because getMemInfo needs it. if (!(const_cast(this)->initializeHeapResources())) { return false; } - - Pal::gpusize local = allocedMem[Pal::GpuHeapLocal]; + // Don't report cached memory in runtime as allocated, since allocedMem tracked at PAL calls + Pal::gpusize local = allocedMem[Pal::GpuHeapLocal] - resourceCache().persistentCacheSize(); Pal::gpusize invisible = allocedMem[Pal::GpuHeapInvisible] - resourceCache().lclCacheSize(); Pal::gpusize total_alloced = local + invisible; @@ -1987,7 +1987,9 @@ bool Device::globalFreeMemory(size_t* freeMemory) const { HIP_HIDDEN_FREE_MEM * Ki : 0; if (settings().apuSystem_) { - Pal::gpusize sysMem = allocedMem[Pal::GpuHeapGartCacheable] + allocedMem[Pal::GpuHeapGartUswc] - + // Allocated system memory without cached allocations. Don't count persistent and local + Pal::gpusize sysMem = allocedMem[Pal::GpuHeapGartCacheable] + allocedMem[Pal::GpuHeapGartUswc] + + resourceCache().persistentCacheSize() - resourceCache().cacheSize() + resourceCache().lclCacheSize(); sysMem /= Ki; if (sysMem >= freeMemory[TotalFreeMemory]) { diff --git a/rocclr/device/pal/palresource.cpp b/rocclr/device/pal/palresource.cpp index 37b5338921..d4b1141475 100644 --- a/rocclr/device/pal/palresource.cpp +++ b/rocclr/device/pal/palresource.cpp @@ -2257,6 +2257,8 @@ bool ResourceCache::addGpuMemory(Resource::Descriptor* desc, GpuMemoryReference* cacheSize_ += size; if (desc->type_ == Resource::Local) { lclCacheSize_ += size; + } else if (desc->type_ == Resource::Persistent) { + persistentCacheSize_ += size; } result = true; } @@ -2311,6 +2313,8 @@ GpuMemoryReference* ResourceCache::findGpuMemory(Resource::Descriptor* desc, Pal cacheSize_ -= sizeRes; if (entry->type_ == Resource::Local) { lclCacheSize_ -= sizeRes; + } else if (entry->type_ == Resource::Persistent) { + persistentCacheSize_ -= sizeRes; } delete it.first; // Remove the found etry from the cache @@ -2345,9 +2349,12 @@ void ResourceCache::removeLast() { if (resCache_.size() > 0) { entry = resCache_.back(); resCache_.pop_back(); - cacheSize_ -= entry.second->iMem()->Desc().size; + auto mem_size = entry.second->iMem()->Desc().size; + cacheSize_ -= mem_size; if (entry.first->type_ == Resource::Local) { - lclCacheSize_ -= entry.second->iMem()->Desc().size; + lclCacheSize_ -= mem_size; + } else if (entry.first->type_ == Resource::Persistent) { + persistentCacheSize_ -= mem_size; } // Delete Descriptor delete entry.first; diff --git a/rocclr/device/pal/palresource.hpp b/rocclr/device/pal/palresource.hpp index 0baa761863..ede3b3b0c8 100644 --- a/rocclr/device/pal/palresource.hpp +++ b/rocclr/device/pal/palresource.hpp @@ -582,6 +582,7 @@ class ResourceCache : public amd::HeapObject { : lockCacheOps_("PAL resource cache", true), cacheSize_(0), lclCacheSize_(0), + persistentCacheSize_(0), cacheSizeLimit_(cacheSizeLimit), mem_sub_alloc_local_(device), mem_sub_alloc_coarse_(device), @@ -614,6 +615,9 @@ class ResourceCache : public amd::HeapObject { //! Returns the size of local memory, stored in the cache size_t lclCacheSize() const { return lclCacheSize_; } + //! Returns the size of persistent memory, stored in the cache + size_t persistentCacheSize() const { return persistentCacheSize_; } + private: //! Disable copy constructor ResourceCache(const ResourceCache&); @@ -628,6 +632,7 @@ class ResourceCache : public amd::HeapObject { size_t cacheSize_; //!< Current cache size in bytes size_t lclCacheSize_; //!< Local memory stored in the cache + size_t persistentCacheSize_; //!< Persistent memory stored in the cache const size_t cacheSizeLimit_; //!< Cache size limit in bytes //! PAL resource cache