From 8c1e0d7e0eac13d6b99688e9185ade1f227cc1f2 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sun, 11 Oct 2020 17:47:30 +0200 Subject: [PATCH] Added option to retrieve current memory footprint, implemented for Windows (to be tested) --- src/gsi/gsi/gsiDeclTl.cc | 5 ++++ src/tl/tl/tlTimer.cc | 55 ++++++++++++++++++++++++++++++---------- src/tl/tl/tlTimer.h | 5 ++++ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/gsi/gsi/gsiDeclTl.cc b/src/gsi/gsi/gsiDeclTl.cc index 0d189d6bc..c4aa03114 100644 --- a/src/gsi/gsi/gsiDeclTl.cc +++ b/src/gsi/gsi/gsiDeclTl.cc @@ -153,6 +153,11 @@ static std::string timer_to_s (const tl::Timer *timer) } Class 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" ) + diff --git a/src/tl/tl/tlTimer.cc b/src/tl/tl/tlTimer.cc index 76ec42301..3e07f5136 100644 --- a/src/tl/tl/tlTimer.cc +++ b/src/tl/tl/tlTimer.cc @@ -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 << ""; + } } // ------------------------------------------------------------- diff --git a/src/tl/tl/tlTimer.h b/src/tl/tl/tlTimer.h index a9cc6ccbd..a8eddef00 100644 --- a/src/tl/tl/tlTimer.h +++ b/src/tl/tl/tlTimer.h @@ -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;