From 38fcca0e7833e2b3640134e15d551ca707e35371 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Thu, 9 Jul 2020 19:51:02 +0200 Subject: [PATCH] improved stats with memory usage (OSX) --- ImGuiToolkit.cpp | 23 ++++++++------------- SystemToolkit.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++---- SystemToolkit.h | 3 ++- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/ImGuiToolkit.cpp b/ImGuiToolkit.cpp index 890f285..b260228 100644 --- a/ImGuiToolkit.cpp +++ b/ImGuiToolkit.cpp @@ -208,10 +208,11 @@ bool ImGuiToolkit::ButtonIconMultistate(std::vector > icons, Sum id = std::for_each(icons.begin(), icons.end(), Sum()); ImGui::PushID( id.sum ); - int s = CLAMP(*state, 0, icons.size() - 1); + int num_button = static_cast(icons.size()) -1; + int s = CLAMP(*state, 0, num_button); if ( ButtonIcon( icons[s].first, icons[s].second ) ){ ++s; - if (s > icons.size() -1) + if (s > num_button) *state = 0; else *state = s; @@ -530,7 +531,7 @@ bool ImGuiToolkit::TimelineSliderEdit(const char* label, guint64 *time, guint64 // segments behavior float time_segment_begin = 0.f; - float time_segment_end = 0.f; +// float time_segment_end = 0.f; if (right_mouse_press) { time_segment_begin = 0.f; } @@ -797,25 +798,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 "); - ImGui::Text("Window (%.1f,%.1f)", io.DisplaySize.x, io.DisplaySize.y); + ImGui::Text("Window %.0f x %.0f", io.DisplaySize.x, io.DisplaySize.y); // 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::Text("Refresh %.1f FPS", io.Framerate); + ImGui::Text("Memory %s", SystemToolkit::byte_to_string( SystemToolkit::memory_usage()).c_str() ); ImGui::PopFont(); if (ImGui::BeginPopupContextWindow()) { - if (ImGui::MenuItem("Custom", NULL, corner == -1)) *p_corner = -1; -// if (ImGui::MenuItem("Top-left", NULL, corner == 0)) corner = 0; + if (ImGui::MenuItem("Custom", NULL, corner == -1)) *p_corner = -1; if (ImGui::MenuItem("Top", NULL, corner == 1)) *p_corner = 1; -// if (ImGui::MenuItem("Bottom-left", NULL, corner == 2)) corner = 2; if (ImGui::MenuItem("Bottom", NULL, corner == 3)) *p_corner = 3; if (p_open && ImGui::MenuItem("Close")) *p_open = false; ImGui::EndPopup(); diff --git a/SystemToolkit.cpp b/SystemToolkit.cpp index 648766d..6a5bd6e 100644 --- a/SystemToolkit.cpp +++ b/SystemToolkit.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -12,7 +13,9 @@ using namespace std; #include #define mkdir(dir, mode) _mkdir(dir) #include +#include #define PATH_SEP '\\' +#define PATH_SETTINGS "\\\AppData\\Roaming\\" #elif defined(LINUX) or defined(APPLE) #include #include @@ -22,20 +25,61 @@ using namespace std; #define PATH_SEP '/' #endif -#ifdef WIN32 -#define PATH_SETTINGS "\\\AppData\\Roaming\\" -#elif defined(APPLE) +#if defined(APPLE) #define PATH_SETTINGS "/Library/Application Support/" +#include +#include #elif defined(LINUX) +#include #define PATH_SETTINGS "/.config/" #endif #include "defines.h" #include "SystemToolkit.h" +/// The amount of memory currently being used by this process, in bytes. +/// it will try to report the resident set in RAM +long SystemToolkit::memory_usage() +{ +#if defined(LINUX) + // Ugh, getrusage doesn't work well on Linux. Try grabbing info + // directly from the /proc pseudo-filesystem. Reading from + // /proc/self/statm gives info on your own process, as one line of + // numbers that are: virtual mem program size, resident set size, + // shared pages, text/code, data/stack, library, dirty pages. The + // mem sizes should all be multiplied by the page size. + size_t size = 0; + FILE *file = fopen("/proc/self/statm", "r"); + if (file) { + unsigned long vm = 0; + fscanf (file, "%ul", &vm); // Just need the first num: vm size + fclose (file); + size = (size_t)vm * getpagesize(); + } + return size; +#elif defined(APPLE) + // Inspired from + // http://miknight.blogspot.com/2005/11/resident-set-size-in-mac-os-x.html + struct task_basic_info t_info; + mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; + task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count); + return t_info.resident_size; + +#elif defined(WIN32) + // According to MSDN... + PROCESS_MEMORY_COUNTERS counters; + if (GetProcessMemoryInfo (GetCurrentProcess(), &counters, sizeof (counters))) + return counters.PagefileUsage; + else return 0; + +#else + return 0; +#endif +} + +long SystemToolkit::memory_max_usage() { -long SystemToolkit::memory_usage() { struct rusage r_usage; getrusage(RUSAGE_SELF,&r_usage); return r_usage.ru_maxrss; diff --git a/SystemToolkit.h b/SystemToolkit.h index 4680d1e..f1e085c 100644 --- a/SystemToolkit.h +++ b/SystemToolkit.h @@ -51,8 +51,9 @@ 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) + // return memory resident set size used (in bytes) long memory_usage(); + long memory_max_usage(); // get a string to display memory size with unit KB, MB, GB, TB std::string byte_to_string(long b);