Add memory discard message.

This can be used to erase all allocations made within the named memory
pool. The usual use case would be for arena allocators, which allocate
by advancing a pointer and never have to free the memory. There is no
tracking of individual allocations and everything is freed frequently,
by reseting the pointer, for example once per frame.

Since this is used in special-purpose allocators, there is no support
for discarding the memory of the default memory pool.
This commit is contained in:
Bartosz Taudul 2024-10-21 17:32:54 +02:00
parent 3dc68bcb76
commit f4df9013bb
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
5 changed files with 80 additions and 1 deletions

View File

@ -2862,6 +2862,15 @@ Profiler::DequeueStatus Profiler::DequeueSerial()
MemWrite( &item->memFree.time, dt ); MemWrite( &item->memFree.time, dt );
break; break;
} }
case QueueType::MemDiscard:
case QueueType::MemDiscardCallstack:
{
int64_t t = MemRead<int64_t>( &item->memDiscard.time );
int64_t dt = t - refSerial;
refSerial = t;
MemWrite( &item->memDiscard.time, dt );
break;
}
case QueueType::GpuZoneBeginSerial: case QueueType::GpuZoneBeginSerial:
case QueueType::GpuZoneBeginCallstackSerial: case QueueType::GpuZoneBeginCallstackSerial:
{ {

View File

@ -633,6 +633,40 @@ public:
#endif #endif
} }
static tracy_force_inline void MemDiscard( const char* name, bool secure )
{
if( secure && !ProfilerAvailable() ) return;
#ifdef TRACY_ON_DEMAND
if( !GetProfiler().IsConnected() ) return;
#endif
const auto thread = GetThreadHandle();
GetProfiler().m_serialLock.lock();
SendMemDiscard( QueueType::MemDiscard, thread, name );
GetProfiler().m_serialLock.unlock();
}
static tracy_force_inline void MemDiscardCallstack( const char* name, bool secure, int depth )
{
if( secure && !ProfilerAvailable() ) return;
#ifdef TRACY_HAS_CALLSTACK
# ifdef TRACY_ON_DEMAND
if( !GetProfiler().IsConnected() ) return;
# endif
const auto thread = GetThreadHandle();
auto callstack = Callstack( depth );
GetProfiler().m_serialLock.lock();
SendCallstackSerial( callstack );
SendMemDiscard( QueueType::MemDiscard, thread, name );
GetProfiler().m_serialLock.unlock();
#else
static_cast<void>(depth); // unused
MemDiscard( name, secure );
#endif
}
static tracy_force_inline void SendCallstack( int depth ) static tracy_force_inline void SendCallstack( int depth )
{ {
#ifdef TRACY_HAS_CALLSTACK #ifdef TRACY_HAS_CALLSTACK
@ -922,6 +956,18 @@ private:
GetProfiler().m_serialQueue.commit_next(); GetProfiler().m_serialQueue.commit_next();
} }
static tracy_force_inline void SendMemDiscard( QueueType type, const uint32_t thread, const char* name )
{
assert( type == QueueType::MemDiscard || type == QueueType::MemDiscardCallstack );
auto item = GetProfiler().m_serialQueue.prepare_next();
MemWrite( &item->hdr.type, type );
MemWrite( &item->memDiscard.time, GetTime() );
MemWrite( &item->memDiscard.thread, thread );
MemWrite( &item->memDiscard.name, (uint64_t)name );
GetProfiler().m_serialQueue.commit_next();
}
static tracy_force_inline void SendMemName( const char* name ) static tracy_force_inline void SendMemName( const char* name )
{ {
assert( name ); assert( name );

View File

@ -9,7 +9,7 @@ namespace tracy
constexpr unsigned Lz4CompressBound( unsigned isize ) { return isize + ( isize / 255 ) + 16; } constexpr unsigned Lz4CompressBound( unsigned isize ) { return isize + ( isize / 255 ) + 16; }
enum : uint32_t { ProtocolVersion = 71 }; enum : uint32_t { ProtocolVersion = 72 };
enum : uint16_t { BroadcastVersion = 3 }; enum : uint16_t { BroadcastVersion = 3 };
using lz4sz_t = uint32_t; using lz4sz_t = uint32_t;

View File

@ -42,6 +42,8 @@ enum class QueueType : uint8_t
MemAllocCallstackNamed, MemAllocCallstackNamed,
MemFreeCallstack, MemFreeCallstack,
MemFreeCallstackNamed, MemFreeCallstackNamed,
MemDiscard,
MemDiscardCallstack,
GpuZoneBegin, GpuZoneBegin,
GpuZoneBeginCallstack, GpuZoneBeginCallstack,
GpuZoneBeginAllocSrcLoc, GpuZoneBeginAllocSrcLoc,
@ -502,6 +504,13 @@ struct QueueMemFree
uint64_t ptr; uint64_t ptr;
}; };
struct QueueMemDiscard
{
int64_t time;
uint32_t thread;
uint64_t name;
};
struct QueueCallstackFat struct QueueCallstackFat
{ {
uint64_t ptr; uint64_t ptr;
@ -742,6 +751,7 @@ struct QueueItem
QueueGpuContextNameFat gpuContextNameFat; QueueGpuContextNameFat gpuContextNameFat;
QueueMemAlloc memAlloc; QueueMemAlloc memAlloc;
QueueMemFree memFree; QueueMemFree memFree;
QueueMemDiscard memDiscard;
QueueMemNamePayload memName; QueueMemNamePayload memName;
QueueThreadGroupHint threadGroupHint; QueueThreadGroupHint threadGroupHint;
QueueCallstackFat callstackFat; QueueCallstackFat callstackFat;
@ -813,6 +823,8 @@ static constexpr size_t QueueDataSize[] = {
sizeof( QueueHeader ) + sizeof( QueueMemAlloc ), // callstack, named sizeof( QueueHeader ) + sizeof( QueueMemAlloc ), // callstack, named
sizeof( QueueHeader ) + sizeof( QueueMemFree ), // callstack sizeof( QueueHeader ) + sizeof( QueueMemFree ), // callstack
sizeof( QueueHeader ) + sizeof( QueueMemFree ), // callstack, named sizeof( QueueHeader ) + sizeof( QueueMemFree ), // callstack, named
sizeof( QueueHeader ) + sizeof( QueueMemDiscard ),
sizeof( QueueHeader ) + sizeof( QueueMemDiscard ), // callstack
sizeof( QueueHeader ) + sizeof( QueueGpuZoneBegin ), sizeof( QueueHeader ) + sizeof( QueueGpuZoneBegin ),
sizeof( QueueHeader ) + sizeof( QueueGpuZoneBegin ), // callstack sizeof( QueueHeader ) + sizeof( QueueGpuZoneBegin ), // callstack
sizeof( QueueHeader ) + sizeof( QueueGpuZoneBeginLean ),// allocated source location sizeof( QueueHeader ) + sizeof( QueueGpuZoneBeginLean ),// allocated source location

View File

@ -75,8 +75,10 @@
#define TracyAlloc(x,y) #define TracyAlloc(x,y)
#define TracyFree(x) #define TracyFree(x)
#define TracyMemoryDiscard(x)
#define TracySecureAlloc(x,y) #define TracySecureAlloc(x,y)
#define TracySecureFree(x) #define TracySecureFree(x)
#define TracySecureMemoryDiscard(x)
#define TracyAllocN(x,y,z) #define TracyAllocN(x,y,z)
#define TracyFreeN(x,y) #define TracyFreeN(x,y)
@ -98,8 +100,10 @@
#define TracyAllocS(x,y,z) #define TracyAllocS(x,y,z)
#define TracyFreeS(x,y) #define TracyFreeS(x,y)
#define TracyMemoryDiscardS(x,y)
#define TracySecureAllocS(x,y,z) #define TracySecureAllocS(x,y,z)
#define TracySecureFreeS(x,y) #define TracySecureFreeS(x,y)
#define TracySecureMemoryDiscardS(x,y)
#define TracyAllocNS(x,y,z,w) #define TracyAllocNS(x,y,z,w)
#define TracyFreeNS(x,y,z) #define TracyFreeNS(x,y,z)
@ -206,8 +210,10 @@
# define TracyAllocN( ptr, size, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, TRACY_CALLSTACK, false, name ) # define TracyAllocN( ptr, size, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, TRACY_CALLSTACK, false, name )
# define TracyFreeN( ptr, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, TRACY_CALLSTACK, false, name ) # define TracyFreeN( ptr, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, TRACY_CALLSTACK, false, name )
# define TracyMemoryDiscard( name ) tracy::Profiler::MemDiscardCallstack( name, false, TRACY_CALLSTACK )
# define TracySecureAllocN( ptr, size, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, TRACY_CALLSTACK, true, name ) # define TracySecureAllocN( ptr, size, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, TRACY_CALLSTACK, true, name )
# define TracySecureFreeN( ptr, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, TRACY_CALLSTACK, true, name ) # define TracySecureFreeN( ptr, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, TRACY_CALLSTACK, true, name )
# define TracySecureMemoryDiscard( name ) tracy::Profiler::MemDiscardCallstack( name, true, TRACY_CALLSTACK )
#else #else
# define TracyMessage( txt, size ) tracy::Profiler::Message( txt, size, 0 ) # define TracyMessage( txt, size ) tracy::Profiler::Message( txt, size, 0 )
# define TracyMessageL( txt ) tracy::Profiler::Message( txt, 0 ) # define TracyMessageL( txt ) tracy::Profiler::Message( txt, 0 )
@ -221,8 +227,10 @@
# define TracyAllocN( ptr, size, name ) tracy::Profiler::MemAllocNamed( ptr, size, false, name ) # define TracyAllocN( ptr, size, name ) tracy::Profiler::MemAllocNamed( ptr, size, false, name )
# define TracyFreeN( ptr, name ) tracy::Profiler::MemFreeNamed( ptr, false, name ) # define TracyFreeN( ptr, name ) tracy::Profiler::MemFreeNamed( ptr, false, name )
# define TracyMemoryDiscard( name ) tracy::Profiler::MemDiscard( name, false )
# define TracySecureAllocN( ptr, size, name ) tracy::Profiler::MemAllocNamed( ptr, size, true, name ) # define TracySecureAllocN( ptr, size, name ) tracy::Profiler::MemAllocNamed( ptr, size, true, name )
# define TracySecureFreeN( ptr, name ) tracy::Profiler::MemFreeNamed( ptr, true, name ) # define TracySecureFreeN( ptr, name ) tracy::Profiler::MemFreeNamed( ptr, true, name )
# define TracySecureMemoryDiscard( name ) tracy::Profiler::MemDiscard( name, true )
#endif #endif
#ifdef TRACY_HAS_CALLSTACK #ifdef TRACY_HAS_CALLSTACK
@ -246,8 +254,10 @@
# define TracyAllocNS( ptr, size, depth, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, depth, false, name ) # define TracyAllocNS( ptr, size, depth, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, depth, false, name )
# define TracyFreeNS( ptr, depth, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, depth, false, name ) # define TracyFreeNS( ptr, depth, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, depth, false, name )
# define TracyMemoryDiscardS( name, depth ) tracy::Profiler::MemDiscardCallstack( name, false, depth )
# define TracySecureAllocNS( ptr, size, depth, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, depth, true, name ) # define TracySecureAllocNS( ptr, size, depth, name ) tracy::Profiler::MemAllocCallstackNamed( ptr, size, depth, true, name )
# define TracySecureFreeNS( ptr, depth, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, depth, true, name ) # define TracySecureFreeNS( ptr, depth, name ) tracy::Profiler::MemFreeCallstackNamed( ptr, depth, true, name )
# define TracySecureMemoryDiscardS( name, depth ) tracy::Profiler::MemDiscardCallstack( name, true, depth )
# define TracyMessageS( txt, size, depth ) tracy::Profiler::Message( txt, size, depth ) # define TracyMessageS( txt, size, depth ) tracy::Profiler::Message( txt, size, depth )
# define TracyMessageLS( txt, depth ) tracy::Profiler::Message( txt, depth ) # define TracyMessageLS( txt, depth ) tracy::Profiler::Message( txt, depth )
@ -274,8 +284,10 @@
# define TracyAllocNS( ptr, size, depth, name ) TracyAllocN( ptr, size, name ) # define TracyAllocNS( ptr, size, depth, name ) TracyAllocN( ptr, size, name )
# define TracyFreeNS( ptr, depth, name ) TracyFreeN( ptr, name ) # define TracyFreeNS( ptr, depth, name ) TracyFreeN( ptr, name )
# define TracyMemoryDiscardS( name, depth ) tracy::Profiler::MemDiscard( name, false )
# define TracySecureAllocNS( ptr, size, depth, name ) TracySecureAllocN( ptr, size, name ) # define TracySecureAllocNS( ptr, size, depth, name ) TracySecureAllocN( ptr, size, name )
# define TracySecureFreeNS( ptr, depth, name ) TracySecureFreeN( ptr, name ) # define TracySecureFreeNS( ptr, depth, name ) TracySecureFreeN( ptr, name )
# define TracySecureMemoryDiscardS( name, depth ) tracy::Profiler::MemDiscard( name, true )
# define TracyMessageS( txt, size, depth ) TracyMessage( txt, size ) # define TracyMessageS( txt, size, depth ) TracyMessage( txt, size )
# define TracyMessageLS( txt, depth ) TracyMessageL( txt ) # define TracyMessageLS( txt, depth ) TracyMessageL( txt )