Skip to content

Commit e8e94fc

Browse files
authored
Show RAM usage (max rss) in UI for Linux systems (#18)
Compiler warning will display for other platforms and default to showing 0.
1 parent 726eca7 commit e8e94fc

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

MP-APS/Core/GUISystem.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ void GUISystem::Render(const int framebufferWidth,
4949
nk_glfw3_new_frame();
5050

5151
const auto frameStatFlags = NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_NO_INPUT;
52-
if (nk_begin(m_nuklearContext, "Frame Stats", nk_rect(0, framebufferHeight - 40, 500, 40), frameStatFlags)) {
52+
if (nk_begin(m_nuklearContext, "Frame Stats", nk_rect(0, framebufferHeight - 40, 720, 40), frameStatFlags)) {
5353
nk_layout_row_begin(m_nuklearContext, NK_STATIC, 0, 1);
5454
{
55-
nk_layout_row_push(m_nuklearContext, 500);
55+
nk_layout_row_push(m_nuklearContext, 720);
5656
nk_label(
5757
m_nuklearContext,
58-
fmt::format("Frame Time: {:.2f} ms ({:.0f} fps) | GPU VRAM Usage: {} MB",
58+
fmt::format("Frame Time: {:.2f} ms ({:.0f} fps) | GPU VRAM Usage: {} MB | RAM Usage: {} MB",
5959
frameStats.frameTimeMilliseconds,
6060
1.0 / (frameStats.frameTimeMilliseconds / 1000.0),
61-
frameStats.videoMemoryUsageKB / 1000
61+
frameStats.videoMemoryUsageKB / 1000,
62+
frameStats.ramUsageKB / 1000
6263
).c_str(),
6364
NK_TEXT_LEFT
6465
);

MP-APS/Engine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "ResourceManager.h"
77
#include "SceneBase.h"
88
#include "FrameStats.h"
9+
#include "Platform/Platform.h"
910

1011
#include <GLFW/glfw3.h>
1112
#include <pugixml.hpp>
@@ -120,6 +121,7 @@ void Engine::Execute() {
120121
const auto frameTime{ frameTimeMilliseconds(numFramesRendered) };
121122
frameStats.frameTimeMilliseconds = frameTime;
122123
frameStats.videoMemoryUsageKB = m_renderer.GetVideoMemUsageKB();
124+
frameStats.ramUsageKB = Platform::maxRSSKb();
123125

124126
numFramesRendered = 0;
125127
hasOneSecondPassed = false;

MP-APS/FrameStats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
struct FrameStats {
44
double frameTimeMilliseconds{ 0.0 };
55
int videoMemoryUsageKB{ 0 };
6+
long ramUsageKB{ 0 };
67
};

MP-APS/Platform/Platform.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#ifdef __linux__
4+
#include <sys/resource.h>
5+
#endif
6+
7+
namespace Platform {
8+
long maxRSSKb() noexcept {
9+
#ifdef __linux__
10+
// https://man7.org/linux/man-pages/man2/getrusage.2.html
11+
rusage ru;
12+
const auto result{ getrusage(RUSAGE_SELF, &ru) };
13+
14+
if (result == -1) {
15+
return 0;
16+
}
17+
18+
return ru.ru_maxrss;
19+
#else
20+
#warning "Platform::maxRSSKb not implemented for this platform."
21+
return 0;
22+
#endif
23+
}
24+
};

0 commit comments

Comments
 (0)