From 447f042c9ad8ab28c0e7df77b3f25d497a208670 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sun, 17 Sep 2017 01:22:46 +0200 Subject: [PATCH] Track server memory usage. --- server/TracyMemory.cpp | 8 ++++++++ server/TracyMemory.hpp | 14 ++++++++++++++ server/TracySlab.hpp | 9 ++++++++- server/TracyVector.hpp | 5 +++++ server/TracyView.cpp | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100755 server/TracyMemory.cpp create mode 100755 server/TracyMemory.hpp diff --git a/server/TracyMemory.cpp b/server/TracyMemory.cpp new file mode 100755 index 00000000..a88ce153 --- /dev/null +++ b/server/TracyMemory.cpp @@ -0,0 +1,8 @@ +#include "TracyMemory.hpp" + +namespace tracy +{ + +std::atomic memUsage = 0; + +} diff --git a/server/TracyMemory.hpp b/server/TracyMemory.hpp new file mode 100755 index 00000000..183999a6 --- /dev/null +++ b/server/TracyMemory.hpp @@ -0,0 +1,14 @@ +#ifndef __TRACYMEMORY_HPP__ +#define __TRACYMEMORY_HPP__ + +#include +#include + +namespace tracy +{ + +extern std::atomic memUsage; + +} + +#endif diff --git a/server/TracySlab.hpp b/server/TracySlab.hpp index df756710..84ba1f4b 100755 --- a/server/TracySlab.hpp +++ b/server/TracySlab.hpp @@ -4,6 +4,8 @@ #include #include +#include "TracyMemory.hpp" + namespace tracy { @@ -15,10 +17,13 @@ public: : m_ptr( new char[BlockSize] ) , m_buffer( { m_ptr } ) , m_offset( 0 ) - {} + { + memUsage.fetch_add( BlockSize, std::memory_order_relaxed ); + } ~Slab() { + memUsage.fetch_sub( BlockSize * m_buffer.size(), std::memory_order_relaxed ); for( auto& v : m_buffer ) { delete[] v; @@ -33,6 +38,7 @@ public: m_ptr = new char[BlockSize]; m_offset = 0; m_buffer.emplace_back( m_ptr ); + memUsage.fetch_add( BlockSize, std::memory_order_relaxed ); } void* ret = m_ptr + m_offset; m_offset += size; @@ -55,6 +61,7 @@ public: { if( m_buffer.size() > 1 ) { + memUsage.fetch_sub( BlockSize * ( m_buffer.size() - 1 ), std::memory_order_relaxed ); for( int i=1; i #include +#include "TracyMemory.hpp" + namespace tracy { @@ -24,6 +26,7 @@ public: ~Vector() { + memUsage.fetch_sub( m_capacity * sizeof( T ), std::memory_order_relaxed ); delete[] m_ptr; } @@ -77,9 +80,11 @@ private: if( m_capacity == 0 ) { m_capacity = 64; + memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed ); } else { + memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed ); m_capacity *= 2; } T* ptr = new T[m_capacity]; diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 1f969214..817725fc 100755 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -309,6 +309,8 @@ void View::DrawImpl() ImGui::PlotLines( buf, m_mbps.data(), m_mbps.size(), 0, nullptr, 0 ); } + ImGui::Text( "Memory usage: %.2f MB", memUsage.load( std::memory_order_relaxed ) / ( 1024.f * 1024.f ) ); + std::lock_guard lock( m_lock ); { const auto sz = m_frames.size();