Pol  Revision:4b29d2b
memoryusage.cpp
Go to the documentation of this file.
1 #include "memoryusage.h"
2 
3 #include <format/format.h>
4 #include "../../clib/fileutil.h"
5 #include "../../clib/logfacility.h"
6 #include "../../plib/systemstate.h"
7 #include "multidefs.h"
8 #include "network.h"
9 #include "object_storage.h"
10 #include "script_internals.h"
11 #include "settings.h"
12 #include "state.h"
13 #include "ucfg.h"
14 #include "uvars.h"
15 
16 #include "pol_global_config.h"
17 
18 namespace Pol
19 {
20 namespace Core
21 {
23 {
24  // std::string footprint is ~ string.capacity()
25  // std::vector footprint is ~ 3 * sizeof(T*) + vector.capacity() * sizeof( T );
26  // std::set footprint is ~ 3 * sizeof( void* ) + set.size() * ( sizeof(T)+3 * sizeof( void* ) );
27  // std::map footprint is ~ ( sizeof(K)+sizeof( V ) + ( sizeof(void*) * 3 + 1 ) / 2 ) * map.size();
28 
29  size_t systemstate_size = Plib::systemstate.estimatedSize();
30  size_t multibuffer_size = Multi::multidef_buffer.estimateSize();
31  auto network_size = networkManager.estimateSize();
32  auto object_sizes = objStorageManager.estimateSize();
33  auto script_sizes = scriptScheduler.estimateSize();
34  size_t settings_size = settingsManager.estimateSize();
35  size_t state_size = stateManager.estimateSize();
36  auto config_sizes = configurationbuffer.estimateSize();
37  auto gamestate_size = gamestate.estimateSize();
38  auto cprop_profiler_size = CPropProfiler::instance().estimateSize();
39 
40  std::vector<std::pair<std::string, size_t>> logs;
41  logs.push_back( std::make_pair( "ProcessSize", Clib::getCurrentMemoryUsage() ) );
42  logs.push_back( std::make_pair( "CPProfilerSize", cprop_profiler_size ) );
43  logs.push_back( std::make_pair( "GameStateSize", gamestate_size.misc ) );
44  logs.push_back( std::make_pair( "RealmSize", gamestate_size.realm_size ) );
45  logs.push_back( std::make_pair( "SystemStateSize", systemstate_size ) );
46  logs.push_back( std::make_pair( "MultiBufferSize", multibuffer_size ) );
47  logs.push_back( std::make_pair( "SettingsSize", settings_size ) );
48  logs.push_back( std::make_pair( "StateSize", state_size ) );
49  logs.push_back( std::make_pair( "ScriptCount", script_sizes.script_count ) );
50  logs.push_back( std::make_pair( "ScriptSize", script_sizes.script_size ) );
51  logs.push_back( std::make_pair( "ScriptStoreCount", script_sizes.scriptstorage_count ) );
52  logs.push_back( std::make_pair( "ScriptStoreSize", script_sizes.scriptstorage_size ) );
53  logs.push_back( std::make_pair( "ConfigCount", config_sizes.cfg_count ) );
54  logs.push_back( std::make_pair( "ConfigSize", config_sizes.cfg_size ) );
55  logs.push_back( std::make_pair( "DataStoreCount", config_sizes.datastore_count ) );
56  logs.push_back( std::make_pair( "DataStoreSize", config_sizes.datastore_size ) );
57  logs.push_back( std::make_pair( "ConfigBufferSize", config_sizes.misc ) );
58  logs.push_back( std::make_pair( "AccountCount", gamestate_size.account_count ) );
59  logs.push_back( std::make_pair( "AccountSize", gamestate_size.account_size ) );
60  logs.push_back( std::make_pair( "ClientCount", network_size.client_count ) );
61  logs.push_back( std::make_pair( "ClientSize", network_size.client_size ) );
62  logs.push_back( std::make_pair( "NetworkSize", network_size.misc ) );
63  logs.push_back( std::make_pair( "ObjectStorage", object_sizes.misc ) );
64 
65  logs.push_back( std::make_pair( "ObjItemCount", object_sizes.obj_item_count ) );
66  logs.push_back( std::make_pair( "ObjItemSize", object_sizes.obj_item_size ) );
67  logs.push_back( std::make_pair( "ObjContCount", object_sizes.obj_cont_count ) );
68  logs.push_back( std::make_pair( "ObjContSize", object_sizes.obj_cont_size ) );
69  logs.push_back( std::make_pair( "ObjCharCount", object_sizes.obj_char_count ) );
70  logs.push_back( std::make_pair( "ObjCharSize", object_sizes.obj_char_size ) );
71  logs.push_back( std::make_pair( "ObjNpcCount", object_sizes.obj_npc_count ) );
72  logs.push_back( std::make_pair( "ObjNpcSize", object_sizes.obj_npc_size ) );
73  logs.push_back( std::make_pair( "ObjWeaponCount", object_sizes.obj_weapon_count ) );
74  logs.push_back( std::make_pair( "ObjWeaponSize", object_sizes.obj_weapon_size ) );
75  logs.push_back( std::make_pair( "ObjArmorCount", object_sizes.obj_armor_count ) );
76  logs.push_back( std::make_pair( "ObjArmorSize", object_sizes.obj_armor_size ) );
77  logs.push_back( std::make_pair( "ObjMultiCount", object_sizes.obj_multi_count ) );
78  logs.push_back( std::make_pair( "ObjMultiSize", object_sizes.obj_multi_size ) );
79 
80 #ifdef DEBUG_FLYWEIGHT
81  for ( size_t i = 0; i < boost_utils::debug_flyweight_queries.size(); ++i )
82  {
83  auto ptr = boost_utils::debug_flyweight_queries[i];
84  if ( ptr == nullptr )
85  continue;
86  auto str = std::to_string( i );
87  logs.push_back( std::make_pair( "FlyWeightBucket" + str + "Count", ptr->bucket_count() ) );
88  logs.push_back( std::make_pair( "FlyWeightBucket" + str + "Size", ptr->estimateSize() ) );
89  }
90 #endif
91  bool needs_header = !Clib::FileExists( "log/memoryusage.log" );
92  auto log = OPEN_FLEXLOG( "log/memoryusage.log", false );
93  if ( needs_header )
94  {
95  fmt::Writer header;
96  header << "Time";
97  for ( const auto& entry : logs )
98  header << " ;" << entry.first;
99  FLEXLOG( log ) << header.str() << "\n";
100  }
101 
102 
103  fmt::Writer line;
104  line << GET_LOG_FILESTAMP;
105  for ( const auto& entry : logs )
106  line << " ;" << entry.second;
107  FLEXLOG( log ) << line.str() << "\n";
108 
109  CLOSE_FLEXLOG( log );
110 }
111 }
112 }
size_t getCurrentMemoryUsage()
returns the current process size in bytes
Definition: xmain.cpp:26
size_t estimateSize() const
Definition: settings.cpp:36
SystemState systemstate
Definition: systemstate.cpp:12
static CPropProfiler & instance()
Definition: proplist.cpp:71
Memory estimateSize() const
Definition: ucfg.cpp:37
MemoryUsage estimateSize() const
size_t estimatedSize() const
Definition: systemstate.cpp:35
Memory estimateSize() const
Definition: uvars.cpp:410
#define FLEXLOG(id)
Definition: logfacility.h:245
Memory estimateSize() const
Definition: network.cpp:131
size_t estimateSize() const
Definition: multidefs.cpp:36
NetworkManager networkManager
Definition: network.cpp:28
#define GET_LOG_FILESTAMP
Definition: logfacility.h:261
ConfigurationBuffer configurationbuffer
Definition: ucfg.cpp:13
#define OPEN_FLEXLOG(filename, open_timestamp)
Definition: logfacility.h:248
size_t estimateSize() const
Definition: state.cpp:35
GameState gamestate
Definition: uvars.cpp:74
size_t estimateSize() const
Definition: proplist.cpp:303
SettingsManager settingsManager
Definition: settings.cpp:14
StateManager stateManager
Definition: state.cpp:8
ScriptScheduler scriptScheduler
ObjectStorageManager objStorageManager
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
Definition: berror.cpp:12
MultiDefBuffer multidef_buffer
Definition: multidefs.cpp:19
#define CLOSE_FLEXLOG(id)
Definition: logfacility.h:251