#include "GLTB/exception.h" #include "GLTB/processinfo.h" #include "GLTB/stringconvert.h" #ifdef WIN32 #include #include #else #include #include #include #include #endif namespace gltb { #ifdef WIN32 std::wstring executableFullName() { TCHAR path[MAX_PATH]; GetModuleFileName(0,path,MAX_PATH); return path; } unsigned long long totalAllocatedMemory() { PROCESS_MEMORY_COUNTERS_EX processMemoryCounters; processMemoryCounters.cb=sizeof(processMemoryCounters); GetProcessMemoryInfo(GetCurrentProcess(),(PROCESS_MEMORY_COUNTERS*)&processMemoryCounters,sizeof(processMemoryCounters)); return processMemoryCounters.WorkingSetSize; } #elif defined LINUX || defined ANDROID // the /proc filesystem is the same on Linux and Android std::wstring executableFullName() { std::ostringstream exeLink; exeLink << "/proc/" << getpid() << "/exe"; char binaryPath[4096]; memset(binaryPath,0,4096); ssize_t result=readlink(exeLink.str().c_str(),binaryPath,4095); if(result==-1 || result>=4095) { throw Error("failed to read link to binary from " + exeLink.str(),"gltb::executableFullName()"); } return utf8ToUtf16WString(binaryPath); } unsigned long long totalAllocatedMemory() { /* * Parse /proc/[pid]/statm here because it's a simple, unformatted file. * The values in the file are (given in numbers of pages): * * total program size | resident set size | shared pages | text (code) | data/stack | library | dirty pages * * So here we need the total program size, that is, the first value */ std::ostringstream statmFilename; statmFilename << "/proc/" << getpid() << "/statm"; std::ifstream statmFile(statmFilename.str().c_str()); if(!statmFile) { throw Error("unable to read memory statistics from " + statmFilename.str(),"gltb::totalAllocatedMemory()"); } unsigned long long numAllocatedPages; statmFile >> numAllocatedPages; return numAllocatedPages * (unsigned long long)sysconf(_SC_PAGESIZE); } #else #error not implemented for this platform #endif }