mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-29 07:19:28 -05:00
Add the ability to query RAM and VRAM usage (#3427)
This commit is contained in:
@@ -92,6 +92,8 @@
|
||||
|
||||
#ifdef WIN32
|
||||
#include <Windows.h>
|
||||
#include <Pdh.h>
|
||||
#include "Psapi.h"
|
||||
#endif // WIN32
|
||||
|
||||
#ifdef __APPLE__
|
||||
@@ -107,6 +109,12 @@ namespace {
|
||||
|
||||
constexpr std::string_view _loggerCat = "OpenSpaceEngine";
|
||||
|
||||
#ifdef WIN32
|
||||
// This counter is used to measure the VRAM usage of OpenSpace
|
||||
PDH_HQUERY vramQuery;
|
||||
PDH_HCOUNTER vramCounter;
|
||||
#endif // WIN32
|
||||
|
||||
constexpr std::string_view stringify(openspace::OpenSpaceEngine::Mode m) {
|
||||
using Mode = openspace::OpenSpaceEngine::Mode;
|
||||
switch (m) {
|
||||
@@ -222,6 +230,22 @@ OpenSpaceEngine::OpenSpaceEngine()
|
||||
|
||||
addProperty(_fadeOnEnableDuration);
|
||||
addProperty(_disableAllMouseInputs);
|
||||
|
||||
#ifdef WIN32
|
||||
PDH_STATUS status = PdhOpenQueryA(nullptr, 0, &vramQuery);
|
||||
if (status != ERROR_SUCCESS) {
|
||||
LWARNING("Error opening Performance Query for VRAM usage");
|
||||
}
|
||||
|
||||
const std::string queryStr = std::format(
|
||||
"\\GPU Process Memory(pid_{}*)\\Dedicated Usage",
|
||||
GetCurrentProcessId()
|
||||
);
|
||||
status = PdhAddEnglishCounterA(vramQuery, queryStr.c_str(), 0, &vramCounter);
|
||||
if (status != ERROR_SUCCESS) {
|
||||
LWARNING("Error add Performance Query for VRAM usage");
|
||||
}
|
||||
#endif // WIN32
|
||||
}
|
||||
|
||||
OpenSpaceEngine::~OpenSpaceEngine() {}
|
||||
@@ -914,6 +938,53 @@ void OpenSpaceEngine::createUserDirectoriesIfNecessary() {
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t OpenSpaceEngine::ramInUse() const {
|
||||
#ifdef WIN32
|
||||
PROCESS_MEMORY_COUNTERS_EX pmc;
|
||||
BOOL success = GetProcessMemoryInfo(
|
||||
GetCurrentProcess(),
|
||||
reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc),
|
||||
sizeof(PROCESS_MEMORY_COUNTERS_EX)
|
||||
);
|
||||
if (!success) {
|
||||
LERROR("Error retrieving RAM usage");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return pmc.PrivateUsage;
|
||||
#else // ^^^^ WIN32 // !WIN32 vvvv
|
||||
LWARNING("Unsupported operating");
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t OpenSpaceEngine::vramInUse() const {
|
||||
#ifdef WIN32
|
||||
PDH_STATUS status = PdhCollectQueryData(vramQuery);
|
||||
if (status != ERROR_SUCCESS) {
|
||||
LERROR("Error collecting VRAM query data");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PDH_FMT_COUNTERVALUE value;
|
||||
status = PdhGetFormattedCounterValue(
|
||||
vramCounter,
|
||||
PDH_FMT_LARGE | PDH_FMT_NOSCALE,
|
||||
nullptr,
|
||||
&value
|
||||
);
|
||||
if (status != ERROR_SUCCESS) {
|
||||
LERROR("Error formatting VRAM query data");
|
||||
return 0;
|
||||
}
|
||||
LONGLONG v = value.largeValue;
|
||||
return v;
|
||||
#else // ^^^^ WIN32 // !WIN32 vvvv
|
||||
LWARNING("Unsupported operating");
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void OpenSpaceEngine::runGlobalCustomizationScripts() {
|
||||
ZoneScoped;
|
||||
|
||||
@@ -974,6 +1045,10 @@ void OpenSpaceEngine::loadFonts() {
|
||||
void OpenSpaceEngine::preSynchronization() {
|
||||
ZoneScoped;
|
||||
TracyGpuZone("preSynchronization");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
LTRACE("OpenSpaceEngine::preSynchronization(begin)");
|
||||
|
||||
@@ -1040,6 +1115,10 @@ void OpenSpaceEngine::preSynchronization() {
|
||||
|
||||
for (const std::function<void()>& func : *global::callback::preSync) {
|
||||
ZoneScopedN("[Module] preSync");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
func();
|
||||
}
|
||||
@@ -1066,6 +1145,10 @@ void OpenSpaceEngine::postSynchronizationPreDraw() {
|
||||
ZoneScoped;
|
||||
TracyGpuZone("postSynchronizationPreDraw");
|
||||
LTRACE("OpenSpaceEngine::postSynchronizationPreDraw(begin)");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
const bool master = global::windowDelegate->isMaster();
|
||||
global::syncEngine->postSynchronization(SyncEngine::IsMaster(master));
|
||||
@@ -1095,6 +1178,10 @@ void OpenSpaceEngine::postSynchronizationPreDraw() {
|
||||
|
||||
for (const std::function<void()>& func : *global::callback::postSyncPreDraw) {
|
||||
ZoneScopedN("[Module] postSyncPreDraw");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
func();
|
||||
}
|
||||
@@ -1128,12 +1215,21 @@ void OpenSpaceEngine::render(const glm::mat4& sceneMatrix, const glm::mat4& view
|
||||
TracyGpuZone("Render");
|
||||
LTRACE("OpenSpaceEngine::render(begin)");
|
||||
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
viewportChanged();
|
||||
|
||||
global::renderEngine->render(sceneMatrix, viewMatrix, projectionMatrix);
|
||||
|
||||
for (const std::function<void()>& func : *global::callback::render) {
|
||||
ZoneScopedN("[Module] render");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
func();
|
||||
}
|
||||
@@ -1145,6 +1241,10 @@ void OpenSpaceEngine::drawOverlays() {
|
||||
ZoneScoped;
|
||||
TracyGpuZone("Draw2D");
|
||||
LTRACE("OpenSpaceEngine::drawOverlays(begin)");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
viewportChanged();
|
||||
|
||||
@@ -1161,6 +1261,11 @@ void OpenSpaceEngine::drawOverlays() {
|
||||
|
||||
for (const std::function<void()>& func : *global::callback::draw2D) {
|
||||
ZoneScopedN("[Module] draw2D");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
func();
|
||||
}
|
||||
|
||||
@@ -1170,12 +1275,20 @@ void OpenSpaceEngine::drawOverlays() {
|
||||
void OpenSpaceEngine::postDraw() {
|
||||
ZoneScoped;
|
||||
TracyGpuZone("postDraw");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
LTRACE("OpenSpaceEngine::postDraw(begin)");
|
||||
|
||||
global::renderEngine->postDraw();
|
||||
|
||||
for (const std::function<void()>& func : *global::callback::postDraw) {
|
||||
ZoneScopedN("[Module] postDraw");
|
||||
#ifdef TRACY_ENABLE
|
||||
TracyPlot("RAM", static_cast<int64_t>(ramInUse()));
|
||||
TracyPlot("VRAM", static_cast<int64_t>(vramInUse()));
|
||||
#endif // TRACY_ENABLE
|
||||
|
||||
func();
|
||||
}
|
||||
@@ -1551,6 +1664,8 @@ scripting::LuaLibrary OpenSpaceEngine::luaLibrary() {
|
||||
codegen::lua::LayerServer,
|
||||
codegen::lua::LoadJson,
|
||||
codegen::lua::ResolveShortcut,
|
||||
codegen::lua::VramInUse,
|
||||
codegen::lua::RamInUse
|
||||
},
|
||||
{
|
||||
absPath("${SCRIPTS}/core_scripts.lua")
|
||||
|
||||
Reference in New Issue
Block a user