Permalink
Browse files

updated flyweight memory report (used for memory saving of strings)

enable via ENABLE_FLYWEIGHT_REPORT cmake option
  • Loading branch information...
turleypol committed Dec 2, 2018
1 parent c598481 commit dafb1d7b9edb5dff3fe6d67694bf267dcfc1bb3a
@@ -25,6 +25,7 @@ set(POL_VERSION_NAME "Never Gonna Give You Up")
option(NO_PCH "Disable pre-compiled headers" OFF)
option(USE_CCACHE "Use ccache if found" OFF)
option(ENABLE_BENCHMARK "Enable benchmark support" OFF)
option(ENABLE_FLYWEIGHT_REPORT "Enables flyweight memory report in memoryusage.log" OFF)
option(BUILD_ALL "Build everything" ON)
option(ONLY_ECOMPILE "Build only ecompile" OFF)
@@ -2,7 +2,7 @@
#define POL_GLOBAL_CONFIG_H
#cmakedefine USE_BOOST_REGEX
#cmakedefine DEBUG_FLYWEIGHT
#cmakedefine ENABLE_FLYWEIGHT_REPORT
#cmakedefine MEMORYLEAK
#cmakedefine PERGON
#cmakedefine ESCRIPT_PROFILE
@@ -27,6 +27,12 @@
#define POL_BUILD_TARGET_TAG_PROFILE ""
#endif
#ifdef ENABLE_FLYWEIGHT_REPORT
#define POL_BUILD_TARGET_TAG_FLYWEIGHT " - FlyweightReport"
#else
#define POL_BUILD_TARGET_TAG_FLYWEIGHT ""
#endif
#if defined( LINUX )
#define POL_ARCH "Linux"
#else
@@ -35,6 +41,6 @@
#define POL_BUILD_TARGET \
( POL_ARCH " " POL_BUILD_ARCH_BITS_STR "bit" POL_BUILD_TARGET_TAG_PERGON \
"" POL_BUILD_TARGET_TAG_MEMLEAK "" POL_BUILD_TARGET_TAG_PROFILE "" POL_REVISION_TAG )
"" POL_BUILD_TARGET_TAG_MEMLEAK "" POL_BUILD_TARGET_TAG_PROFILE "" POL_BUILD_TARGET_TAG_FLYWEIGHT "" POL_REVISION_TAG )
#endif /* POL_REVISION_CONFIG_H */
@@ -4,8 +4,39 @@ namespace Pol
{
namespace boost_utils
{
#ifdef DEBUG_FLYWEIGHT
std::vector<bucket_query*> debug_flyweight_queries;
flyweight_initializers::~flyweight_initializers()
{
#ifdef ENABLE_FLYWEIGHT_REPORT
if ( Query::debug_flyweight_queries != nullptr )
delete Query::debug_flyweight_queries;
Query::debug_flyweight_queries = nullptr;
#endif
}
#ifdef ENABLE_FLYWEIGHT_REPORT
std::vector<bucket_query*>* Query::debug_flyweight_queries = nullptr;
bucket_query::bucket_query()
{
Query::add( this );
}
void Query::add( bucket_query* b )
{
if ( debug_flyweight_queries == nullptr )
debug_flyweight_queries = new std::vector<bucket_query*>;
debug_flyweight_queries->push_back( b );
}
std::vector<std::pair<size_t, size_t>> Query::getCountAndSize()
{
std::vector<std::pair<size_t, size_t>> res;
for ( const auto& factory : *debug_flyweight_queries )
{
res.emplace_back( factory->bucket_count(), factory->estimateSize() );
}
return res;
}
#endif
} // namespace boost_utils
} // namespace Pol
@@ -12,41 +12,56 @@
#pragma warning( disable : 4503 ) // decorated name length exceeded
#endif
#ifdef DEBUG_FLYWEIGHT
#ifdef ENABLE_FLYWEIGHT_REPORT
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <boost/flyweight/factory_tag.hpp>
#include <boost/flyweight/hashed_factory_fwd.hpp>
#include <boost/foreach.hpp>
#include <boost/mpl/aux_/lambda_support.hpp>
#include <boost/mpl/if.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index_container.hpp>
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
#include <utility>
#endif
#include <string>
#include <vector>
#endif
#include <boost/flyweight.hpp>
namespace Pol
{
namespace boost_utils
{
#ifdef DEBUG_FLYWEIGHT
#ifdef ENABLE_FLYWEIGHT_REPORT
template <typename Entry, typename Key, typename Hash = boost::mpl::na,
typename Pred = boost::mpl::na, typename Allocator = boost::mpl::na>
class accessible_hashed_factory_class;
template <typename Hash = boost::mpl::na, typename Pred = boost::mpl::na,
typename Allocator = boost::mpl::na BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION>
struct accessible_hashed_factory;
class bucket_query
{
public:
bucket_query(); // register instance in Query
typedef std::size_t size_type;
virtual ~bucket_query(){};
virtual ~bucket_query() = default;
virtual size_type bucket_count() const = 0;
virtual size_type max_bucket_count() const = 0;
virtual size_type bucket_size( size_type n ) const = 0;
virtual size_type estimateSize() const = 0;
};
extern std::vector<bucket_query*> debug_flyweight_queries;
struct Query
{
static std::vector<std::pair<size_t, size_t>> getCountAndSize();
static void add( bucket_query* b );
static std::vector<bucket_query*>* debug_flyweight_queries;
};
template <typename Entry, typename Key, typename Hash = boost::mpl::na,
typename Pred = boost::mpl::na, typename Allocator = boost::mpl::na>
template <typename Entry, typename Key, typename Hash, typename Pred, typename Allocator>
class accessible_hashed_factory_class : public boost::flyweights::factory_marker,
public bucket_query
{
@@ -65,39 +80,45 @@ class accessible_hashed_factory_class : public boost::flyweights::factory_marker
container_type;
public:
accessible_hashed_factory_class() : boost::flyweights::factory_marker(), bucket_query() {}
typedef const Entry* handle_type;
accessible_hashed_factory_class() { debug_flyweight_queries.push_back( this ); }
handle_type insert( const Entry& x ) { return &*cont.insert( x ).first; }
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
handle_type insert( Entry&& x ) { return &*cont.insert( std::move( x ) ).first; }
#endif
void erase( handle_type h ) { cont.erase( cont.iterator_to( *h ) ); }
static const Entry& entry( handle_type h ) { return *h; }
typedef std::size_t size_type;
private:
container_type cont;
public:
typedef accessible_hashed_factory_class type;
BOOST_MPL_AUX_LAMBDA_SUPPORT( 5, accessible_hashed_factory_class,
( Entry, Key, Hash, Pred, Allocator ) )
virtual size_type bucket_count() const override { return cont.bucket_count(); }
virtual size_type max_bucket_count() const override { return cont.max_bucket_count(); }
virtual size_type bucket_size( size_type n ) const override { return cont.bucket_size( n ); }
virtual size_type estimateSize() const override
{
size_type size = 0;
BOOST_FOREACH ( const Entry& e, cont )
for ( const Entry& e : cont )
{
size += (int)( 4.5 * sizeof( long ) ); // ~overhead
size += ( (std::string)e ).capacity();
}
return size;
}
private:
container_type cont;
public:
typedef accessible_hashed_factory_class type;
BOOST_MPL_AUX_LAMBDA_SUPPORT( 5, accessible_hashed_factory_class,
( Entry, Key, Hash, Pred, Allocator ) )
};
/* hashed_factory_class specifier */
template <typename Hash = boost::mpl::na, typename Pred = boost::mpl::na,
typename Allocator = boost::mpl::na BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION>
template <typename Hash, typename Pred,
typename Allocator BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF>
struct accessible_hashed_factory : boost::flyweights::factory_marker
{
template <typename Entry, typename Key>
@@ -172,8 +193,10 @@ struct flyweight_initializers
script_name_flystring::initializer fwInit_script_name;
npctemplate_name_flystring::initializer fwInit_npctemplate_name;
function_name_flystring::initializer fwInit_func_name;
~flyweight_initializers();
};
}
}
} // namespace boost_utils
} // namespace Pol
#endif
@@ -1,6 +1,6 @@
#include "memoryusage.h"
#include <format/format.h>
#include "../../clib/boostutils.h"
#include "../../clib/fileutil.h"
#include "../../clib/logfacility.h"
#include "../../plib/systemstate.h"
@@ -12,6 +12,7 @@
#include "state.h"
#include "ucfg.h"
#include "uvars.h"
#include <format/format.h>
#include "pol_global_config.h"
@@ -76,16 +77,15 @@ void MemoryUsage::log()
logs.push_back( std::make_pair( "ObjArmorSize", object_sizes.obj_armor_size ) );
logs.push_back( std::make_pair( "ObjMultiCount", object_sizes.obj_multi_count ) );
logs.push_back( std::make_pair( "ObjMultiSize", object_sizes.obj_multi_size ) );
#ifdef DEBUG_FLYWEIGHT
for ( size_t i = 0; i < boost_utils::debug_flyweight_queries.size(); ++i )
#ifdef ENABLE_FLYWEIGHT_REPORT
auto flydata = boost_utils::Query::getCountAndSize();
int i = 0;
for ( const auto& data : flydata )
{
auto ptr = boost_utils::debug_flyweight_queries[i];
if ( ptr == nullptr )
continue;
auto str = std::to_string( i );
logs.push_back( std::make_pair( "FlyWeightBucket" + str + "Count", ptr->bucket_count() ) );
logs.push_back( std::make_pair( "FlyWeightBucket" + str + "Size", ptr->estimateSize() ) );
logs.push_back( std::make_pair( "FlyWeightBucket" + str + "Count", data.first ) );
logs.push_back( std::make_pair( "FlyWeightBucket" + str + "Size", data.second ) );
++i;
}
#endif
bool needs_header = !Clib::FileExists( "log/memoryusage.log" );
@@ -108,5 +108,5 @@ void MemoryUsage::log()
CLOSE_FLEXLOG( log );
}
}
}
} // namespace Core
} // namespace Pol

0 comments on commit dafb1d7

Please sign in to comment.