Added Windows OS support to MemCheckTests (#6684)

* Added Windows OS support to MemCheckTests

* Move system headers to cpp file

* Moved includes for windows to cpp file. Was rewrited function to get memory info

* Was rewrite functions for getting information about memory

* Moved implementation run_in_process to cpp file

* Fixed implementation of run_in_process
This commit is contained in:
Ilya Sharikov 2021-07-28 13:06:16 +03:00 committed by GitHub
parent c0eb700024
commit 6ee9285fe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 34 deletions

View File

@ -94,7 +94,7 @@ void runTest(const std::function<void(std::string, std::string, int)> &tests_pip
#if DEBUG_MODE #if DEBUG_MODE
tests_pipeline(params.model, params.device, params.numiters); tests_pipeline(params.model, params.device, params.numiters);
#else #else
int status = run_in_processes(params.numprocesses, _runTest, tests_pipeline, params); int status = run_in_processes(params.numprocesses, [&](){ _runTest(tests_pipeline, params); });
ASSERT_EQ(status, 0) << "Test failed with exitcode " << std::to_string(status); ASSERT_EQ(status, 0) << "Test failed with exitcode " << std::to_string(status);
#endif #endif
} }

View File

@ -11,8 +11,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <unistd.h>
#include <sys/wait.h>
enum TestStatus enum TestStatus
{ {

View File

@ -7,6 +7,16 @@
#include <string> #include <string>
#include <string.h> #include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#include <tlhelp32.h>
#else
#include <sys/unistd.h>
#include <sys/wait.h>
#endif
std::string OS_PATH_JOIN(std::initializer_list<std::string> list) { std::string OS_PATH_JOIN(std::initializer_list<std::string> list) {
if (!list.size()) if (!list.size())
return ""; return "";
@ -37,13 +47,52 @@ static int parseLine(std::string line) {
} }
#ifdef _WIN32 #ifdef _WIN32
static PROCESS_MEMORY_COUNTERS getMemoryInfo() {
static PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(),&pmc, pmc.cb);
return pmc;
}
size_t getVmSizeInKB() { size_t getVmSizeInKB() {
// TODO rewrite for Virtual Memory return getMemoryInfo().PagefileUsage / 1024;
PROCESS_MEMORY_COUNTERS pmc; }
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(GetCurrentProcess(),&pmc, pmc.cb); size_t getVmPeakInKB() {
return pmc.WorkingSetSize; return getMemoryInfo().PeakPagefileUsage / 1024;
} }
size_t getVmRSSInKB() {
return getMemoryInfo().WorkingSetSize / 1024;
}
size_t getVmHWMInKB() {
return getMemoryInfo().PeakWorkingSetSize / 1024;
}
size_t getThreadsNum() {
// first determine the id of the current process
DWORD const id = GetCurrentProcessId();
// then get a process list snapshot.
HANDLE const snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPALL, 0 );
// initialize the process entry structure.
PROCESSENTRY32 entry = { 0 };
entry.dwSize = sizeof( entry );
// get the first process info.
BOOL ret = true;
ret = Process32First( snapshot, &entry );
while( ret && entry.th32ProcessID != id ) {
ret = Process32Next( snapshot, &entry );
}
CloseHandle( snapshot );
return ret
? entry.cntThreads
: -1;
}
#else #else
size_t getSystemDataByName(char *name){ size_t getSystemDataByName(char *name){
FILE* file = fopen("/proc/self/status", "r"); FILE* file = fopen("/proc/self/status", "r");
@ -70,6 +119,35 @@ size_t getThreadsNum() {return getSystemDataByName((char*) "Threads:");}
#endif #endif
int run_in_processes(const int &numprocesses, const std::function<void()> &function) {
#ifdef _WIN32
// TODO: implement run in separate process by using WinAPI
function;
return 0;
#else
std::vector<pid_t> child_pids(numprocesses);
for (int i = 0; i < numprocesses; i++) {
child_pids[i] = fork();
if (child_pids[i] == 0) {
function;
exit(EXIT_SUCCESS);
}
}
int status = 0;
for (int i = 0; i < numprocesses; i++) {
int _status = 0;
waitpid(child_pids[i], &_status, WSTOPPED);
if (_status) {
log_err("Process run # " << i << " failed with exitcode " << _status);
status = _status;
}
}
return status;
#endif
}
void auto_expand_env_vars(std::string &input) { void auto_expand_env_vars(std::string &input) {
const static std::string pattern1 = "${", pattern2 = "}"; const static std::string pattern1 = "${", pattern2 = "}";
size_t pattern1_pos, pattern2_pos, envvar_start_pos, envvar_finish_pos; size_t pattern1_pos, pattern2_pos, envvar_start_pos, envvar_finish_pos;

View File

@ -9,8 +9,6 @@
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <sys/unistd.h>
#include <sys/wait.h>
#ifdef _WIN32 #ifdef _WIN32
#define OS_SEP std::string("\\") #define OS_SEP std::string("\\")
@ -39,29 +37,7 @@ size_t getVmRSSInKB();
size_t getVmHWMInKB(); size_t getVmHWMInKB();
size_t getThreadsNum(); size_t getThreadsNum();
template<typename Function, typename ... Args> int run_in_processes(const int &numprocesses, const std::function<void()> &function);
int run_in_processes(const int &numprocesses, Function const &function, Args ... args) {
std::vector<pid_t> child_pids(numprocesses);
for (int i = 0; i < numprocesses; i++) {
child_pids[i] = fork();
if (child_pids[i] == 0) {
function(args...);
exit(EXIT_SUCCESS);
}
}
int status = 0;
for (int i = 0; i < numprocesses; i++) {
int _status = 0;
waitpid(child_pids[i], &_status, WSTOPPED);
if (_status) {
log_err("Process run # " << i << " failed with exitcode " << _status);
status = _status;
}
}
return status;
}
template<typename Function, typename ... Args> template<typename Function, typename ... Args>
inline void run_in_threads(const int &numthreads, Function const &function, Args ... args) { inline void run_in_threads(const int &numthreads, Function const &function, Args ... args) {

View File

@ -6,6 +6,7 @@
#include "../common/tests_utils.h" #include "../common/tests_utils.h"
#include <array>
#include <pugixml.hpp> #include <pugixml.hpp>
// Measure values // Measure values