Added option to retrieve current memory footprint, implemented for Windows (to be tested)

This commit is contained in:
Matthias Koefferlein 2020-10-11 17:47:30 +02:00
parent f21dfb4a1c
commit 8c1e0d7e0e
3 changed files with 52 additions and 13 deletions

View File

@ -153,6 +153,11 @@ static std::string timer_to_s (const tl::Timer *timer)
}
Class<tl::Timer> decl_Timer ("tl", "Timer",
gsi::method ("memory_size", &tl::Timer::memory_size,
"@brief Gets the current memory usage of the process in Bytes\n"
"\n"
"This method has been introduced in version 0.27."
) +
gsi::method ("user", &tl::Timer::sec_user,
"@brief Returns the elapsed CPU time in user mode from start to stop in seconds\n"
) +

View File

@ -186,17 +186,27 @@ Timer::take ()
m_wall_ms = wall_ms;
}
void
SelfTimer::start_report () const
{
tl::info << m_desc << ": " << tl::to_string (tr ("started"));
}
void
SelfTimer::report () const
size_t
Timer::memory_size ()
{
#ifdef _WIN32
tl::info << m_desc << ": (user) " << sec_user () << " (sys) " << sec_sys ();
size_t mem = 0;
HANDLE h_process = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId ());
if (h_process != NULL) {
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo (h_process, &pmc, sizeof (pmc))) {
mem = size_t (pmc.WorkingSetSize);
}
CloseHandle (h_process);
}
return mem;
#else
unsigned long memsize = 0;
@ -250,12 +260,31 @@ SelfTimer::report () const
}
}
return size_t (memsize);
#endif
}
void
SelfTimer::start_report () const
{
tl::info << m_desc << ": " << tl::to_string (tr ("started"));
}
void
SelfTimer::report () const
{
size_t memsize = memory_size ();
tl::info << m_desc << ": " << sec_user () << " (user) "
<< sec_sys () << " (sys) "
<< sec_wall () << " (wall) "
<< tl::sprintf ("%.2fM", double (memsize) / (1024.0 * 1024.0)) << " (mem)"
;
#endif
<< sec_wall () << " (wall)" << tl::noendl;
if (memsize > 0) {
tl::info << " " << tl::sprintf ("%.2fM", double (memsize) / (1024.0 * 1024.0)) << " (mem)";
} else {
tl::info << "";
}
}
// -------------------------------------------------------------

View File

@ -95,6 +95,11 @@ public:
return (double (m_wall_ms_res) * 0.001);
}
/**
* @brief Reports the current memory usage
*/
static size_t memory_size ();
private:
timer_t m_user_ms, m_sys_ms, m_wall_ms;
timer_t m_user_ms_res, m_sys_ms_res, m_wall_ms_res;