diff --git a/Grid/allocator/MemoryManager.cc b/Grid/allocator/MemoryManager.cc index 81ad058ee..c8f4b27d6 100644 --- a/Grid/allocator/MemoryManager.cc +++ b/Grid/allocator/MemoryManager.cc @@ -84,6 +84,31 @@ MemoryManager::AllocationCacheEntry MemoryManager::Entries[MemoryManager::Nalloc int MemoryManager::Victim[MemoryManager::NallocType]; int MemoryManager::Ncache[MemoryManager::NallocType] = { 2, 0, 8, 8, 0, 16, 8, 0, 16 }; uint64_t MemoryManager::CacheBytes[MemoryManager::NallocType]; +uint64_t MemoryManager::DeviceAllocCalls; +uint64_t MemoryManager::DeviceFreeCalls; +uint64_t MemoryManager::DeviceAllocBytes; +uint64_t MemoryManager::DeviceFreeBytes; +uint64_t MemoryManager::DeviceCacheHits; + +void MemoryManager::PrintAllocCounts(void) +{ + std::cout << GridLogMemory << "MemoryManager: device allocator calls: acceleratorAllocDevice " + << DeviceAllocCalls <<" ("<< DeviceAllocBytes <<" bytes), acceleratorFreeDevice " + << DeviceFreeCalls <<" ("<< DeviceFreeBytes <<" bytes), served from ring cache " + << DeviceCacheHits << std::endl; + std::cout << GridLogMemory << "MemoryManager: view traffic: HostToDevice " + << HostToDeviceXfer <<" transfers ("<< HostToDeviceBytes <<" bytes), DeviceToHost " + << DeviceToHostXfer <<" transfers ("<< DeviceToHostBytes <<" bytes), evictions " + << DeviceEvictions << std::endl; +} +void MemoryManager::Snapshot(const std::string &where) +{ + if ( !GridLogMemory.isActive() ) return; + std::cout << GridLogMemory << "---------------- memory snapshot: "<< where <<" ----------------"<= GRID_ALLOC_HUGE_LIMIT) cache = type + 1; else cache = type; - return Insert(ptr,bytes,Entries[cache],Ncache[cache],Victim[cache],CacheBytes[cache]); + return Insert(ptr,bytes,Entries[cache],Ncache[cache],Victim[cache],CacheBytes[cache],freed); #else return ptr; #endif @@ -326,7 +360,7 @@ void MemoryManager::DropCache(void) } } -void *MemoryManager::Insert(void *ptr,size_t bytes,AllocationCacheEntry *entries,int ncache,int &victim, uint64_t &cacheBytes) +void *MemoryManager::Insert(void *ptr,size_t bytes,AllocationCacheEntry *entries,int ncache,int &victim, uint64_t &cacheBytes,size_t *freed) { #ifdef GRID_OMP GRID_ASSERT(omp_in_parallel()==0); @@ -351,6 +385,7 @@ void *MemoryManager::Insert(void *ptr,size_t bytes,AllocationCacheEntry *entries if ( entries[v].valid ) { ret = entries[v].address; + if ( freed ) *freed = entries[v].bytes; // the DISPLACED block is what actually gets freed cacheBytes -= entries[v].bytes; entries[v].valid = 0; entries[v].address = NULL; diff --git a/Grid/allocator/MemoryManager.h b/Grid/allocator/MemoryManager.h index 08a62b6ea..a6434317d 100644 --- a/Grid/allocator/MemoryManager.h +++ b/Grid/allocator/MemoryManager.h @@ -108,9 +108,9 @@ private: ///////////////////////////////////////////////// // Free pool ///////////////////////////////////////////////// - static void *Insert(void *ptr,size_t bytes,int type) ; + static void *Insert(void *ptr,size_t bytes,int type,size_t *freed=nullptr) ; static void *Lookup(size_t bytes,int type) ; - static void *Insert(void *ptr,size_t bytes,AllocationCacheEntry *entries,int ncache,int &victim,uint64_t &cbytes) ; + static void *Insert(void *ptr,size_t bytes,AllocationCacheEntry *entries,int ncache,int &victim,uint64_t &cbytes,size_t *freed=nullptr) ; static void *Lookup(size_t bytes,AllocationCacheEntry *entries,int ncache,uint64_t &cbytes) ; public: @@ -137,7 +137,19 @@ public: static uint64_t DeviceToHostXfer; static uint64_t DeviceEvictions; static uint64_t DeviceDestroy; - + // Calls that actually reach the runtime, as distinct from the Evict/Clone traffic above: + // a free only reaches acceleratorFreeDevice when displaced from the allocation ring cache, + // and an allocate only reaches acceleratorAllocDevice on a ring miss. + static uint64_t DeviceAllocCalls; + static uint64_t DeviceFreeCalls; + static uint64_t DeviceAllocBytes; + static uint64_t DeviceFreeBytes; + static uint64_t DeviceCacheHits; + static void PrintAllocCounts(void); + // Labelled snapshot: allocator counts + footprint + device free/total. Silent unless + // --log Memory is on, so it can be left in hot code. + static void Snapshot(const std::string &where); + static uint64_t DeviceCacheBytes(); static uint64_t HostCacheBytes();