mem tracing - TraceMemorySwap helper function

This commit is contained in:
Matthias Hochsteger 2020-11-19 17:35:29 +01:00
parent 6f98123e98
commit f0152baacf
2 changed files with 25 additions and 4 deletions

View File

@ -701,7 +701,7 @@ namespace ngcore
NETGEN_INLINE Array (Array && a2)
{
TraceMemoryChange(mem_tracing_id, sizeof(T)*(a2.allocsize-allocsize));
TraceMemorySwap(mem_tracing_id, sizeof(T)*allocsize, a2.mem_tracing_id, sizeof(T)*a2.allocsize);
size = a2.size;
data = a2.data;
@ -969,7 +969,7 @@ namespace ngcore
/// steal array
NETGEN_INLINE Array & operator= (Array && a2)
{
TraceMemoryChange(mem_tracing_id, sizeof(T)*(a2.allocsize-allocsize));
TraceMemorySwap(mem_tracing_id, sizeof(T)*allocsize, a2.mem_tracing_id, sizeof(T)*a2.allocsize);
ngcore::Swap (size, a2.size);
ngcore::Swap (data, a2.data);
@ -1044,13 +1044,12 @@ namespace ngcore
NETGEN_INLINE void Swap (Array & b)
{
TraceMemoryChange(mem_tracing_id, sizeof(T)*(b.allocsize-allocsize));
TraceMemorySwap(mem_tracing_id, sizeof(T)*allocsize, b.mem_tracing_id, sizeof(T)*b.allocsize);
ngcore::Swap (size, b.size);
ngcore::Swap (data, b.data);
ngcore::Swap (allocsize, b.allocsize);
ngcore::Swap (mem_to_delete, b.mem_to_delete);
ngcore::Swap (mem_tracing_id, b.mem_tracing_id);
}
NETGEN_INLINE void SetMemoryTracing (int mem_id)

View File

@ -433,6 +433,28 @@ namespace ngcore
trace->ChangeMemory(mem_id, size);
}
NETGEN_INLINE void TraceMemorySwap( int mem_id, size_t size, int mem_id2, size_t size2 )
{
if(!trace || (mem_id==0 && mem_id2==0))
return;
if(mem_id == 0)
return trace->ChangeMemory(mem_id2, size-size2);
if(mem_id2 == 0)
return trace->ChangeMemory(mem_id, size2-size);
// first decrease memory, otherwise have artificial/wrong high peak memory usage
if(size<size2)
{
trace->ChangeMemory(mem_id2, size-size2);
trace->ChangeMemory(mem_id, size2-size);
}
else
{
trace->ChangeMemory(mem_id, size2-size);
trace->ChangeMemory(mem_id2, size-size2);
}
}
} // namespace ngcore
// Helper macro to easily add multiple timers in a function for profiling