Make memUsage an atomic.

This commit is contained in:
Bartosz Taudul 2024-05-04 14:28:55 +02:00
parent 6199b2f883
commit 377e41fe61
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
6 changed files with 19 additions and 17 deletions

View File

@ -234,7 +234,7 @@ int main( int argc, char** argv )
AnsiPrintf( ANSI_YELLOW, "Tx: "); AnsiPrintf( ANSI_YELLOW, "Tx: ");
AnsiPrintf( ANSI_GREEN, "%s", tracy::MemSizeToString( netTotal ) ); AnsiPrintf( ANSI_GREEN, "%s", tracy::MemSizeToString( netTotal ) );
printf( " | "); printf( " | ");
AnsiPrintf( ANSI_RED ANSI_BOLD, "%s", tracy::MemSizeToString( tracy::memUsage ) ); AnsiPrintf( ANSI_RED ANSI_BOLD, "%s", tracy::MemSizeToString( tracy::memUsage.load( std::memory_order_relaxed ) ) );
printf( " | "); printf( " | ");
AnsiPrintf( ANSI_RED, "%s", tracy::TimeToString( worker.GetLastTime() - firstTime ) ); AnsiPrintf( ANSI_RED, "%s", tracy::TimeToString( worker.GetLastTime() - firstTime ) );
fflush( stdout ); fflush( stdout );

View File

@ -1012,12 +1012,13 @@ bool View::DrawImpl()
targetLabelSize = ImGui::CalcTextSize( ICON_FA_MEMORY " 1234.56 MB (123.45 %%)" ).x; targetLabelSize = ImGui::CalcTextSize( ICON_FA_MEMORY " 1234.56 MB (123.45 %%)" ).x;
cx = ImGui::GetCursorPosX(); cx = ImGui::GetCursorPosX();
ImGui::Text( ICON_FA_MEMORY " %s", MemSizeToString( memUsage ) ); const auto mem = memUsage.load( std::memory_order_relaxed );
ImGui::Text( ICON_FA_MEMORY " %s", MemSizeToString( mem ) );
TooltipIfHovered( "Profiler memory usage" ); TooltipIfHovered( "Profiler memory usage" );
if( m_totalMemory != 0 ) if( m_totalMemory != 0 )
{ {
ImGui::SameLine(); ImGui::SameLine();
const auto memUse = float( memUsage ) / m_totalMemory * 100; const auto memUse = float( mem ) / m_totalMemory * 100;
if( memUse < 80 ) if( memUse < 80 )
{ {
ImGui::TextDisabled( "(%.2f%%)", memUse ); ImGui::TextDisabled( "(%.2f%%)", memUse );

View File

@ -3,6 +3,6 @@
namespace tracy namespace tracy
{ {
size_t memUsage = 0; std::atomic<int64_t> memUsage( 0 );
} }

View File

@ -1,12 +1,13 @@
#ifndef __TRACYMEMORY_HPP__ #ifndef __TRACYMEMORY_HPP__
#define __TRACYMEMORY_HPP__ #define __TRACYMEMORY_HPP__
#include <stdlib.h> #include <atomic>
#include <stdint.h>
namespace tracy namespace tracy
{ {
extern size_t memUsage; extern std::atomic<int64_t> memUsage;
} }

View File

@ -21,12 +21,12 @@ public:
, m_buffer( { m_ptr } ) , m_buffer( { m_ptr } )
, m_usage( BlockSize ) , m_usage( BlockSize )
{ {
memUsage += BlockSize; memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
} }
~Slab() ~Slab()
{ {
memUsage -= m_usage; memUsage.fetch_sub( m_usage, std::memory_order_relaxed );
for( auto& v : m_buffer ) for( auto& v : m_buffer )
{ {
delete[] v; delete[] v;
@ -105,7 +105,7 @@ public:
} }
else else
{ {
memUsage += size; memUsage.fetch_add( size, std::memory_order_relaxed );
m_usage += size; m_usage += size;
auto ret = new char[size]; auto ret = new char[size];
m_buffer.emplace_back( ret ); m_buffer.emplace_back( ret );
@ -117,7 +117,7 @@ public:
{ {
if( m_buffer.size() > 1 ) if( m_buffer.size() > 1 )
{ {
memUsage -= m_usage - BlockSize; memUsage.fetch_sub( m_usage - BlockSize, std::memory_order_relaxed );
m_usage = BlockSize; m_usage = BlockSize;
for( int i=1; i<m_buffer.size(); i++ ) for( int i=1; i<m_buffer.size(); i++ )
{ {
@ -143,7 +143,7 @@ private:
m_ptr = ptr; m_ptr = ptr;
m_offset = willUseBytes; m_offset = willUseBytes;
m_buffer.emplace_back( m_ptr ); m_buffer.emplace_back( m_ptr );
memUsage += BlockSize; memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
m_usage += BlockSize; m_usage += BlockSize;
return ptr; return ptr;
} }

View File

@ -47,7 +47,7 @@ public:
, m_capacity( 0 ) , m_capacity( 0 )
, m_magic( 0 ) , m_magic( 0 )
{ {
memUsage += sizeof( T ); memUsage.fetch_add( sizeof( T ), std::memory_order_relaxed );
new(m_ptr) T( value ); new(m_ptr) T( value );
} }
@ -55,7 +55,7 @@ public:
{ {
if( m_capacity != MaxCapacity() && m_ptr ) if( m_capacity != MaxCapacity() && m_ptr )
{ {
memUsage -= Capacity() * sizeof( T ); memUsage.fetch_sub( Capacity() * sizeof( T ), std::memory_order_relaxed );
free( m_ptr ); free( m_ptr );
} }
} }
@ -65,7 +65,7 @@ public:
{ {
if( m_capacity != MaxCapacity() && m_ptr ) if( m_capacity != MaxCapacity() && m_ptr )
{ {
memUsage -= Capacity() * sizeof( T ); memUsage.fetch_sub( Capacity() * sizeof( T ), std::memory_order_relaxed );
free( m_ptr ); free( m_ptr );
} }
memcpy( (char*)this, &src, sizeof( Vector<T> ) ); memcpy( (char*)this, &src, sizeof( Vector<T> ) );
@ -254,7 +254,7 @@ public:
cap |= cap >> 8; cap |= cap >> 8;
cap |= cap >> 16; cap |= cap >> 16;
cap = TracyCountBits( cap ); cap = TracyCountBits( cap );
memUsage += ( ( 1 << cap ) - Capacity() ) * sizeof( T ); memUsage.fetch_add( ( ( 1 << cap ) - Capacity() ) * sizeof( T ), std::memory_order_relaxed );
m_capacity = cap; m_capacity = cap;
Realloc(); Realloc();
} }
@ -291,13 +291,13 @@ private:
if( m_ptr == nullptr ) if( m_ptr == nullptr )
{ {
memUsage += sizeof( T ); memUsage.fetch_add( sizeof( T ), std::memory_order_relaxed );
m_ptr = (T*)malloc( sizeof( T ) ); m_ptr = (T*)malloc( sizeof( T ) );
m_capacity = 0; m_capacity = 0;
} }
else else
{ {
memUsage += Capacity() * sizeof( T ); memUsage.fetch_add( Capacity() * sizeof( T ), std::memory_order_relaxed );
m_capacity++; m_capacity++;
Realloc(); Realloc();
} }