summaryrefslogtreecommitdiffstats
path: root/source/Root.cpp
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2013-10-08 20:12:34 +0200
committermadmaxoft <github@xoft.cz>2013-10-08 20:12:34 +0200
commita120507be027ba18d5443e76061b47e0c624f229 (patch)
tree6a4379b153bd4785b430cdc044c9947f2ac6c302 /source/Root.cpp
parentCleaned up cEntity's enums. (diff)
downloadcuberite-a120507be027ba18d5443e76061b47e0c624f229.tar
cuberite-a120507be027ba18d5443e76061b47e0c624f229.tar.gz
cuberite-a120507be027ba18d5443e76061b47e0c624f229.tar.bz2
cuberite-a120507be027ba18d5443e76061b47e0c624f229.tar.lz
cuberite-a120507be027ba18d5443e76061b47e0c624f229.tar.xz
cuberite-a120507be027ba18d5443e76061b47e0c624f229.tar.zst
cuberite-a120507be027ba18d5443e76061b47e0c624f229.zip
Diffstat (limited to '')
-rw-r--r--source/Root.cpp114
1 files changed, 112 insertions, 2 deletions
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 <iostream>
+#ifdef _WIN32
+ #include <psapi.h>
+#elif defined(__linux__)
+ #include <fstream>
+#elif defined(__APPLE__)
+ #include <mach/mach.h>
+#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;