Commit 5cc877f9 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'sys_resources' into 'master'

System resources

See merge request !68
parents b0c1efd4 cfe874b1
Pipeline #40463 passed with stages
in 15 minutes and 30 seconds
......@@ -16,7 +16,13 @@ TRIBITS_ADD_LIBRARY(radixcore
SOURCES ${SOURCES}
NOINSTALLHEADERS ${HEADERS}
)
#
# Resolve MINGW linking of psapi library
IF(MINGW)
FIND_LIBRARY(PSAPI psapi)
MESSAGE(STATUS "Checking required PSAPI library")
TARGET_LINK_LIBRARIES(radixcore "${PSAPI}")
ENDIF()
#
# Add testing directory
TRIBITS_ADD_TEST_DIRECTORIES(tests)
......
......@@ -20,13 +20,20 @@
#include "system.hh"
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#include <io.h> // _access msvc2013
#include <psapi.h>
#include <sys/stat.h>
#define getcwd _getcwd // MSFT "deprecation" warning
#define chdir _chdir // change directory
#define popen _popen
#define pclose _pclose
#elif defined(__APPLE__)
#include <mach/mach.h>
#include <sys/stat.h>
#include <unistd.h>
#else
#include <sys/stat.h>
#include <unistd.h>
......@@ -416,7 +423,81 @@ std::string system_copy(const std::string &source,
return output;
}
#ifdef _WIN32
//
// WINDOWS version of get_process_memory
long get_process_memory()
{
long result = -1L;
DWORD pid = GetCurrentProcessId();
PROCESS_MEMORY_COUNTERS pmc;
HANDLE handle =
OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (handle != NULL && GetProcessMemoryInfo(handle, &pmc, sizeof(pmc)))
result = long(pmc.WorkingSetSize);
return result;
} // WINDOWS version of get_process_memory
#elif defined(__APPLE__)
//
// MAC version of get_process_memory
long get_process_memory()
{
long result = -1L;
// remove_file
vm_size_t page_size;
int status = host_page_size(mach_host_self(), &page_size);
if (status == KERN_SUCCESS)
{
task_basic_info tbi;
mach_msg_type_number_t tbi_count = TASK_BASIC_INFO_COUNT;
status = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&tbi,
&tbi_count);
if (status == KERN_SUCCESS) result = (long)(tbi.resident_size * page_size);
// tbi.resident_size, tbi.virtual_size
} // if page size returned
return result;
} // MAC version of get_process_memory
#else
//
// LINUX version of get_process_memory
long get_process_memory()
{
long result = -1L;
std::string path = "/proc/";
auto pid = getpid();
path = path + std::to_string(pid) + "/status";
std::ifstream input(path);
if (input.is_open())
{
std::string line;
while (getline(input, line))
{
std::transform(line.begin(), line.end(), line.begin(), ::tolower);
if (line.find("vmrss:") == 0)
{
char buffer[128];
strncpy(buffer, line.substr(6).c_str(),
std::min(sizeof(buffer), line.size() - 6));
caddr_t context = nullptr, token = strtok_r(buffer, " \t", &context);
if (token != nullptr) result = long(atoi(token)) * 1024L;
break;
}
} // while reading
input.close();
}
return result;
} // LINUX version of get_process_memory
#endif
} // namespace radix
......@@ -107,6 +107,14 @@ RADIX_PUBLIC bool get_paths(const std::string &spec,
std::vector<std::string> &paths,
bool append = false);
/**
* @brief get_process_memory
* Retrieve the number of bytes this proces is taking of RAM (memory)
* The majority share of implementation is courtesy of Ron Lee.
* @return <b>long</b> - number of bytes
*/
RADIX_PUBLIC long get_process_memory();
/**
* @brief repeatedly call getPaths on each element
* @param path <b>std::vector<std::string></b> resultant paths
......
......@@ -233,4 +233,9 @@ TEST(System, copy)
EXPECT_TRUE(radix::file_exists(radix::basename(this_file)));
radix::remove_file(radix::basename(this_file));
EXPECT_FALSE(radix::file_exists(basename(this_file)));
}
TEST(System, get_process_memory)
{
auto process_memory = radix::get_process_memory();
EXPECT_TRUE(process_memory > 0);
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment