1
0

Implemented the two memory-statistics functions in cRoot.

This fixes #185. cWebAdmin::GetMemoryUsage() is accessible but deprecated (with a warning output to the screen).
This commit is contained in:
madmaxoft
2013-10-08 20:12:34 +02:00
parent a4cbe9fbb1
commit a120507be0
5 changed files with 185 additions and 57 deletions

View File

@@ -17,14 +17,6 @@
#include "HTTPServer/HTTPMessage.h"
#include "HTTPServer/HTTPConnection.h"
#ifdef _WIN32
#include <psapi.h>
#elif defined(__linux__)
#include <fstream>
#elif defined(__APPLE__)
#include <mach/mach.h>
#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();
}