From a120507be027ba18d5443e76061b47e0c624f229 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 8 Oct 2013 20:12:34 +0200 Subject: [PATCH] Implemented the two memory-statistics functions in cRoot. This fixes #185. cWebAdmin::GetMemoryUsage() is accessible but deprecated (with a warning output to the screen). --- source/Bindings.cpp | 60 ++++++++++++++++++++++- source/Bindings.h | 2 +- source/Root.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++- source/Root.h | 12 ++++- source/WebAdmin.cpp | 54 +-------------------- 5 files changed, 185 insertions(+), 57 deletions(-) diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 34772e792..48d8f3f83 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 10/08/13 19:32:59. +** Generated automatically by tolua++-1.0.92 on 10/08/13 20:07:51. */ #ifndef __cplusplus @@ -19546,6 +19546,62 @@ static int tolua_AllToLua_cRoot_GetProtocolVersionTextFromInt00(lua_State* tolua } #endif //#ifndef TOLUA_DISABLE +/* method: GetVirtualRAMUsage of class cRoot */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cRoot_GetVirtualRAMUsage00 +static int tolua_AllToLua_cRoot_GetVirtualRAMUsage00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"cRoot",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + { + int tolua_ret = (int) cRoot::GetVirtualRAMUsage(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'GetVirtualRAMUsage'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* method: GetPhysicalRAMUsage of class cRoot */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cRoot_GetPhysicalRAMUsage00 +static int tolua_AllToLua_cRoot_GetPhysicalRAMUsage00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"cRoot",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + { + int tolua_ret = (int) cRoot::GetPhysicalRAMUsage(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'GetPhysicalRAMUsage'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* method: new of class Vector3f */ #ifndef TOLUA_DISABLE_tolua_AllToLua_Vector3f_new00 static int tolua_AllToLua_Vector3f_new00(lua_State* tolua_S) @@ -30345,6 +30401,8 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"SaveAllChunks",tolua_AllToLua_cRoot_SaveAllChunks00); tolua_function(tolua_S,"BroadcastChat",tolua_AllToLua_cRoot_BroadcastChat00); tolua_function(tolua_S,"GetProtocolVersionTextFromInt",tolua_AllToLua_cRoot_GetProtocolVersionTextFromInt00); + tolua_function(tolua_S,"GetVirtualRAMUsage",tolua_AllToLua_cRoot_GetVirtualRAMUsage00); + tolua_function(tolua_S,"GetPhysicalRAMUsage",tolua_AllToLua_cRoot_GetPhysicalRAMUsage00); tolua_endmodule(tolua_S); #ifdef __cplusplus tolua_cclass(tolua_S,"Vector3f","Vector3f","",tolua_collect_Vector3f); diff --git a/source/Bindings.h b/source/Bindings.h index d5ff7a886..a1daea398 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 10/08/13 19:32:59. +** Generated automatically by tolua++-1.0.92 on 10/08/13 20:07:52. */ /* Exported function */ diff --git a/source/Root.cpp b/source/Root.cpp index 26e6d347d..290a5269a 100644 --- a/source/Root.cpp +++ b/source/Root.cpp @@ -21,13 +21,19 @@ #include "../iniFile/iniFile.h" -#include +#ifdef _WIN32 + #include +#elif defined(__linux__) + #include +#elif defined(__APPLE__) + #include +#endif -cRoot* cRoot::s_Root = 0; +cRoot* cRoot::s_Root = NULL; @@ -571,6 +577,110 @@ AString cRoot::GetProtocolVersionTextFromInt(int a_ProtocolVersion) +int cRoot::GetVirtualRAMUsage(void) +{ + #ifdef _WIN32 + PROCESS_MEMORY_COUNTERS_EX pmc; + if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmc, sizeof(pmc))) + { + return (int)(pmc.PrivateUsage / 1024); + } + return -1; + #elif defined(__linux__) + // Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process + std::ifstream StatFile("/proc/self/status"); + if (!StatFile.good()) + { + return -1; + } + while (StatFile.good()) + { + AString Line; + std::getline(StatFile, Line); + if (strncmp(Line.c_str(), "VmSize:", 7) == 0) + { + int res = atoi(Line.c_str() + 8); + return (res == 0) ? -1 : res; // If parsing failed, return -1 + } + } + return -1; + #elif defined (__APPLE__) + // Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process + struct task_basic_info t_info; + mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; + + if (KERN_SUCCESS == task_info( + mach_task_self(), + TASK_BASIC_INFO, + (task_info_t)&t_info, + &t_info_count + )) + { + return (int)(t_info.virtual_size / 1024); + } + return -1; + #else + LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__); + return -1; + #endif +} + + + + + +int cRoot::GetPhysicalRAMUsage(void) +{ + #ifdef _WIN32 + PROCESS_MEMORY_COUNTERS pmc; + if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) + { + return (int)(pmc.WorkingSetSize / 1024); + } + return -1; + #elif defined(__linux__) + // Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process + std::ifstream StatFile("/proc/self/status"); + if (!StatFile.good()) + { + return -1; + } + while (StatFile.good()) + { + AString Line; + std::getline(StatFile, Line); + if (strncmp(Line.c_str(), "VmRSS:", 7) == 0) + { + int res = atoi(Line.c_str() + 8); + return (res == 0) ? -1 : res; // If parsing failed, return -1 + } + } + return -1; + #elif defined (__APPLE__) + // Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process + struct task_basic_info t_info; + mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; + + if (KERN_SUCCESS == task_info( + mach_task_self(), + TASK_BASIC_INFO, + (task_info_t)&t_info, + &t_info_count + )) + { + return (int)(t_info.resident_size / 1024); + } + return -1; + #else + LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__); + return -1; + #endif +} + + + + + void cRoot::LogChunkStats(cCommandOutputCallback & a_Output) { int SumNumValid = 0; diff --git a/source/Root.h b/source/Root.h index e5197ce2b..2b15d3461 100644 --- a/source/Root.h +++ b/source/Root.h @@ -105,8 +105,18 @@ public: /// Finds a player from a partial or complete player name and calls the callback - case-insensitive bool FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback); // >> EXPORTED IN MANUALBINDINGS << + // tolua_begin + /// Returns the textual description of the protocol version: 49 -> "1.4.4". Provided specifically for Lua API - static AString GetProtocolVersionTextFromInt(int a_ProtocolVersionNum); // tolua_export + static AString GetProtocolVersionTextFromInt(int a_ProtocolVersionNum); + + /// Returns the amount of virtual RAM used, in KiB. Returns a negative number on error + static int GetVirtualRAMUsage(void); + + /// Returns the amount of virtual RAM used, in KiB. Returns a negative number on error + static int GetPhysicalRAMUsage(void); + + // tolua_end private: class cCommand diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp index c917ec658..08817139a 100644 --- a/source/WebAdmin.cpp +++ b/source/WebAdmin.cpp @@ -17,14 +17,6 @@ #include "HTTPServer/HTTPMessage.h" #include "HTTPServer/HTTPConnection.h" -#ifdef _WIN32 - #include -#elif defined(__linux__) - #include -#elif defined(__APPLE__) - #include -#endif - @@ -392,50 +384,8 @@ AString cWebAdmin::GetBaseURL( const AStringVector& a_URLSplit ) int cWebAdmin::GetMemoryUsage(void) { - #ifdef _WIN32 - PROCESS_MEMORY_COUNTERS pmc; - if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) - { - return (int)(pmc.WorkingSetSize / 1024); - } - return -1; - #elif defined(__linux__) - // Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process - std::ifstream StatFile("/proc/self/status"); - if (!StatFile.good()) - { - return -1; - } - while (StatFile.good()) - { - AString Line; - std::getline(StatFile, Line); - if (strncmp(Line.c_str(), "VmSize:", 7) == 0) - { - int res = atoi(Line.c_str() + 8); - return (res == 0) ? -1 : res; // If parsing failed, return -1 - } - } - return -1; - #elif defined (__APPLE__) - // Code adapted from http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process - struct task_basic_info t_info; - mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; - - if (KERN_SUCCESS == task_info( - mach_task_self(), - TASK_BASIC_INFO, - (task_info_t)&t_info, - &t_info_count - )) - { - return (int)(t_info.resident_size / 1024); - } - return -1; - #else - LOGINFO("%s: Unknown platform, cannot query memory usage", __FUNCTION__); - return -1; - #endif + LOGWARNING("%s: This function is obsolete, use cRoot::GetPhysicalRAMUsage() or cRoot::GetVirtualRAMUsage() instead", __FUNCTION__); + return cRoot::GetPhysicalRAMUsage(); }