Track server memory usage.

This commit is contained in:
Bartosz Taudul 2017-09-17 01:22:46 +02:00
parent ab033188d8
commit 447f042c9a
5 changed files with 37 additions and 1 deletions

8
server/TracyMemory.cpp Executable file
View File

@ -0,0 +1,8 @@
#include "TracyMemory.hpp"
namespace tracy
{
std::atomic<size_t> memUsage = 0;
}

14
server/TracyMemory.hpp Executable file
View File

@ -0,0 +1,14 @@
#ifndef __TRACYMEMORY_HPP__
#define __TRACYMEMORY_HPP__
#include <atomic>
#include <stdlib.h>
namespace tracy
{
extern std::atomic<size_t> memUsage;
}
#endif

View File

@ -4,6 +4,8 @@
#include <assert.h> #include <assert.h>
#include <vector> #include <vector>
#include "TracyMemory.hpp"
namespace tracy namespace tracy
{ {
@ -15,10 +17,13 @@ public:
: m_ptr( new char[BlockSize] ) : m_ptr( new char[BlockSize] )
, m_buffer( { m_ptr } ) , m_buffer( { m_ptr } )
, m_offset( 0 ) , m_offset( 0 )
{} {
memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
}
~Slab() ~Slab()
{ {
memUsage.fetch_sub( BlockSize * m_buffer.size(), std::memory_order_relaxed );
for( auto& v : m_buffer ) for( auto& v : m_buffer )
{ {
delete[] v; delete[] v;
@ -33,6 +38,7 @@ public:
m_ptr = new char[BlockSize]; m_ptr = new char[BlockSize];
m_offset = 0; m_offset = 0;
m_buffer.emplace_back( m_ptr ); m_buffer.emplace_back( m_ptr );
memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
} }
void* ret = m_ptr + m_offset; void* ret = m_ptr + m_offset;
m_offset += size; m_offset += size;
@ -55,6 +61,7 @@ public:
{ {
if( m_buffer.size() > 1 ) if( m_buffer.size() > 1 )
{ {
memUsage.fetch_sub( BlockSize * ( m_buffer.size() - 1 ), std::memory_order_relaxed );
for( int i=1; i<m_buffer.size(); i++ ) for( int i=1; i<m_buffer.size(); i++ )
{ {
delete[] m_buffer[i]; delete[] m_buffer[i];

View File

@ -4,6 +4,8 @@
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include "TracyMemory.hpp"
namespace tracy namespace tracy
{ {
@ -24,6 +26,7 @@ public:
~Vector() ~Vector()
{ {
memUsage.fetch_sub( m_capacity * sizeof( T ), std::memory_order_relaxed );
delete[] m_ptr; delete[] m_ptr;
} }
@ -77,9 +80,11 @@ private:
if( m_capacity == 0 ) if( m_capacity == 0 )
{ {
m_capacity = 64; m_capacity = 64;
memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed );
} }
else else
{ {
memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed );
m_capacity *= 2; m_capacity *= 2;
} }
T* ptr = new T[m_capacity]; T* ptr = new T[m_capacity];

View File

@ -309,6 +309,8 @@ void View::DrawImpl()
ImGui::PlotLines( buf, m_mbps.data(), m_mbps.size(), 0, nullptr, 0 ); 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<std::mutex> lock( m_lock ); std::lock_guard<std::mutex> lock( m_lock );
{ {
const auto sz = m_frames.size(); const auto sz = m_frames.size();