From 69cabd385f37f0aac5f273c08aa6b66211b1491e Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Thu, 9 Jul 2020 12:22:30 +0200 Subject: [PATCH] Implementation of memory usage monitor. --- ImGuiToolkit.cpp | 14 ++++++++------ SystemToolkit.cpp | 27 +++++++++++++++++++++++++++ SystemToolkit.h | 6 ++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/ImGuiToolkit.cpp b/ImGuiToolkit.cpp index b25a003..890f285 100644 --- a/ImGuiToolkit.cpp +++ b/ImGuiToolkit.cpp @@ -669,7 +669,7 @@ void ImGuiToolkit::Bar(float value, float in, float out, float min, float max, c ImVec2 pos0 = ImLerp(bb.Min, bb.Max, 0.1); - ImVec2 pos1 = ImLerp(bb.Min, bb.Max, 0.2); +// ImVec2 pos1 = ImLerp(bb.Min, bb.Max, 0.2); float step = (bb.Max.x - bb.Min.x) / 100.f; int i = 0; for (float tic = bb.Min.x; tic < bb.Max.x; tic += step, ++i) { @@ -797,15 +797,17 @@ void ImGuiToolkit::ShowStats(bool *p_open, int* p_corner) { ImGuiToolkit::PushFont(ImGuiToolkit::FONT_MONO); - if (ImGui::IsMousePosValid()) - ImGui::Text("Mouse (%.1f,%.1f)", io.MousePos.x, io.MousePos.y); - else - ImGui::Text("Mouse "); +// if (ImGui::IsMousePosValid()) +// ImGui::Text("Mouse (%.1f,%.1f)", io.MousePos.x, io.MousePos.y); +// else +// ImGui::Text("Mouse "); ImGui::Text("Window (%.1f,%.1f)", io.DisplaySize.x, io.DisplaySize.y); - ImGui::Text("HiDPI (retina) %s", io.DisplayFramebufferScale.x > 1.f ? "on" : "off"); +// ImGui::Text("HiDPI (retina) %s", io.DisplayFramebufferScale.x > 1.f ? "on" : "off"); // ImGui::Text("DPI Scale (%.1f,%.1f)", io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); ImGui::Text("Rendering %.1f FPS", io.Framerate); + ImGui::Text("Memory %s", SystemToolkit::byte_to_string( SystemToolkit::memory_usage()).c_str() ); +// ImGui::Text("Memory %.ld MB", SystemToolkit::memory_usage()); ImGui::PopFont(); if (ImGui::BeginPopupContextWindow()) diff --git a/SystemToolkit.cpp b/SystemToolkit.cpp index 7ebbb9f..648766d 100644 --- a/SystemToolkit.cpp +++ b/SystemToolkit.cpp @@ -14,6 +14,7 @@ using namespace std; #include #define PATH_SEP '\\' #elif defined(LINUX) or defined(APPLE) +#include #include #include #include @@ -33,6 +34,32 @@ using namespace std; #include "SystemToolkit.h" + +long SystemToolkit::memory_usage() { + struct rusage r_usage; + getrusage(RUSAGE_SELF,&r_usage); + return r_usage.ru_maxrss; +// return r_usage.ru_isrss; +} + +string SystemToolkit::byte_to_string(long b) +{ + double numbytes = static_cast(b); + ostringstream oss; + + std::list list = {" Bytes", " KB", " MB", " GB", " TB"}; + std::list::iterator i = list.begin(); + + while(numbytes >= 1024.0 && i != list.end()) + { + i++; + numbytes /= 1024.0; + } + oss << std::fixed << std::setprecision(2) << numbytes << *i; + return oss.str(); +} + + string SystemToolkit::date_time_string() { chrono::system_clock::time_point now = chrono::system_clock::now(); diff --git a/SystemToolkit.h b/SystemToolkit.h index 30d3303..4680d1e 100644 --- a/SystemToolkit.h +++ b/SystemToolkit.h @@ -50,6 +50,12 @@ namespace SystemToolkit // try to open the file with system void open(const std::string& path); + + // (tries to) return maximum resident set size used (in kilobytes) + long memory_usage(); + + // get a string to display memory size with unit KB, MB, GB, TB + std::string byte_to_string(long b); } #endif // SYSTEMTOOLKIT_H