Resource Tracking for FlightGear: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
Line 275: Line 275:


Here's a first stab at a simple subsystem to monitor FlightGear memory usage on Linux at 5 second intervals, consider it a "proof of concept" prototype now, as this would need to be cleaned up and implemented for Mac/Windows respectively - on Linux it simply works such that it merely fopen()s /proc/pid/smaps and copies two metrics to the property tree:
Here's a first stab at a simple subsystem to monitor FlightGear memory usage on Linux at 5 second intervals, consider it a "proof of concept" prototype now, as this would need to be cleaned up and implemented for Mac/Windows respectively - on Linux it simply works such that it merely fopen()s /proc/pid/smaps and copies two metrics to the property tree:
<syntaxhighlight lang="diff">
<syntaxhighlight lang="diff">diff --git a/src/Main/CMakeLists.txt b/src/Main/CMakeLists.txt
diff --git a/src/Main/CMakeLists.txt b/src/Main/CMakeLists.txt
index 4b6926e..31d3a8f 100644
index 4b6926e..31d3a8f 100644
--- a/src/Main/CMakeLists.txt
--- a/src/Main/CMakeLists.txt
Line 355: Line 354:
diff --git a/src/Main/ram_usage.cxx b/src/Main/ram_usage.cxx
diff --git a/src/Main/ram_usage.cxx b/src/Main/ram_usage.cxx
new file mode 100644
new file mode 100644
index 0000000..9c809c4
index 0000000..92d397a
--- /dev/null
--- /dev/null
+++ b/src/Main/ram_usage.cxx
+++ b/src/Main/ram_usage.cxx
@@ -0,0 +1,25 @@
@@ -0,0 +1,27 @@
+#include "ram_usage_linux.hxx"
+#include "ram_usage_linux.hxx"
+
+
Line 371: Line 370:
+void
+void
+MemoryUsageStats::update(double dt) {
+MemoryUsageStats::update(double dt) {
+  _mem->update();
+  _mem->update(); // will update ram/swap utilization
+  _mem->updateVRAMStats(); // will update vram utilization
+#if 0
+  int vram = _mem->getVRAMUsageInKB(); // vram tracking stuff
+  int vram = _mem->getVRAMUsageInKB(); // vram tracking stuff
+  int swap = _mem->getSwapSize();
+  int swap = _mem->getSwapSize();
Line 379: Line 380:
+  fgSetInt("/memory-usage/total-usage-kb", total );
+  fgSetInt("/memory-usage/total-usage-kb", total );
+  fgSetInt("/memory-usage/vram-available-kb", vram );
+  fgSetInt("/memory-usage/vram-available-kb", vram );
+  
+#endif
+}
+}
+
+
Line 386: Line 387:
diff --git a/src/Main/ram_usage.hxx b/src/Main/ram_usage.hxx
diff --git a/src/Main/ram_usage.hxx b/src/Main/ram_usage.hxx
new file mode 100644
new file mode 100644
index 0000000..1f492e2
index 0000000..63db270
--- /dev/null
--- /dev/null
+++ b/src/Main/ram_usage.hxx
+++ b/src/Main/ram_usage.hxx
@@ -0,0 +1,178 @@
@@ -0,0 +1,177 @@
+#ifndef __RAM_USAGE
+#ifndef __RAM_USAGE
+#define __RAM_USAGE
+#define __RAM_USAGE
Line 441: Line 442:
+    public:
+    public:
+    GPUInfo(): total_mem_kb(-1), cur_avail_mem_kb(-1) {
+    GPUInfo(): total_mem_kb(-1), cur_avail_mem_kb(-1) {
+ total_vram_kb = new simgear::PropertyObject<int>("/stas/vram/total-size-kb");
+ total_vram_kb = new simgear::PropertyObject<int>("/stats/vram/total-size-kb");
+ available_vram_kb =  new simgear::PropertyObject<int>("/stats/vram/available-kb");
+ available_vram_kb =  new simgear::PropertyObject<int>("/stats/vram/available-kb");
+ used_vram_kb =  new simgear::PropertyObject<int>("/stats/vram/used-kb");
+ used_vram_kb =  new simgear::PropertyObject<int>("/stats/vram/used-kb");
Line 450: Line 451:
+ delete used_vram_kb;
+ delete used_vram_kb;
+ };
+ };
+    virtual int getVRAMUsageInKB() = 0;
+    virtual void updateVRAMStats() {};
+    };
+    };
+  
+  
Line 463: Line 464:
+    *total_vram_kb = total_mem_kb;  
+    *total_vram_kb = total_mem_kb;  
+    }
+    }
+    virtual int getVRAMUsageInKB() {
+    virtual void updateVRAMStats() {
+    glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX, &cur_avail_mem_kb);
+    glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX, &cur_avail_mem_kb);
+
+
Line 469: Line 470:
+    *available_vram_kb = cur_avail_mem_kb;
+    *available_vram_kb = cur_avail_mem_kb;
+    *used_vram_kb = *total_vram_kb - *available_vram_kb;
+    *used_vram_kb = *total_vram_kb - *available_vram_kb;
+    return cur_avail_mem_kb;
+    }
+    }
+  
+  
Line 478: Line 478:
+    // ATI_GPU()  
+    // ATI_GPU()  
+    // https://github.com/OpenGLInsights/OpenGLInsightsCode/blob/master/Chapter%2038%20Monitoring%20Graphics%20Memory%20Usage/Ch38AMD/Ch38AMD/Ch38AMDDlg.cpp#L280
+    // https://github.com/OpenGLInsights/OpenGLInsightsCode/blob/master/Chapter%2038%20Monitoring%20Graphics%20Memory%20Usage/Ch38AMD/Ch38AMD/Ch38AMDDlg.cpp#L280
+    virtual int getVRAMUsageInKB() {
+    virtual void updateVRAMStats() {
+    SG_LOG(SG_GENERAL, SG_ALERT,"Sorry:ATI VRAM tracking function not yet implemented (nvidia only)!");
+    SG_LOG(SG_GENERAL, SG_ALERT,"Sorry:ATI VRAM tracking function not yet implemented (nvidia only)!");
+     return -1;
+   *used_vram_kb = *total_vram_kb = *available_vram_kb = -1;
+    }
+    }
+  
+  
Line 487: Line 487:
+    class INTEL_GPU : public GPUInfo {
+    class INTEL_GPU : public GPUInfo {
+    public:
+    public:
+    virtual int getVRAMUsageInKB() {
+    virtual void updateVRAMStat() {
+    SG_LOG(SG_GENERAL, SG_ALERT,"Sorry:Intel VRAM tracking function not yet implemented (nvidia only)!");
+    SG_LOG(SG_GENERAL, SG_ALERT,"Sorry:Intel VRAM tracking function not yet implemented (nvidia only)!");
+     return -1;
+   *used_vram_kb = *total_vram_kb = *available_vram_kb = -1;
+    }
+    }
+  
+  
Line 502: Line 502:
+ delete _gpu;
+ delete _gpu;
+ };
+ };
+ MemoryInterface() {
+ MemoryInterface() : _total_size("/stats/ram/memory-used-kb"), _swap_size("/stats/ram/swap-used-kb") {
+    // see if we have a GPU that supports vram tracking:
+
+    std::string fallback = "NVIDIA"; //default value
+    std::string fallback = "NVIDIA"; //default value
+  
+  
Line 533: Line 535:
+ virtual void update() = 0;
+ virtual void update() = 0;
+
+
+ int getVRAMUsageInKB() const {
+ void updateVRAMStats() const {
+if (_gpu)
+if (_gpu)
+return _gpu->getVRAMUsageInKB();
+_gpu->updateVRAMStats();
+
+return -1; // VRAM tracking not supported, so return -1
+}
+}
+
+
Line 551: Line 551:
+ GPUInfo* _gpu;
+ GPUInfo* _gpu;
+
+
+ int _total_size;
+ simgear::PropertyObject<int> _total_size;
+ int _swap_size;
+ simgear::PropertyObject<int> _swap_size;
+};
+};
+
+
Line 570: Line 570:
diff --git a/src/Main/ram_usage_linux.cxx b/src/Main/ram_usage_linux.cxx
diff --git a/src/Main/ram_usage_linux.cxx b/src/Main/ram_usage_linux.cxx
new file mode 100644
new file mode 100644
index 0000000..ffc48ce
index 0000000..3a6498a
--- /dev/null
--- /dev/null
+++ b/src/Main/ram_usage_linux.cxx
+++ b/src/Main/ram_usage_linux.cxx
Line 600: Line 600:
+
+
+LinuxMemoryInterface::~LinuxMemoryInterface() {
+LinuxMemoryInterface::~LinuxMemoryInterface() {
+ if (file) fclose(file);
+ //if (file) fclose(file);
+}
+}
+
+
Line 625: Line 625:
diff --git a/src/Main/ram_usage_linux.hxx b/src/Main/ram_usage_linux.hxx
diff --git a/src/Main/ram_usage_linux.hxx b/src/Main/ram_usage_linux.hxx
new file mode 100644
new file mode 100644
index 0000000..a6d2033
index 0000000..abce41b
--- /dev/null
--- /dev/null
+++ b/src/Main/ram_usage_linux.hxx
+++ b/src/Main/ram_usage_linux.hxx
Line 647: Line 647:
+ void OpenProcFile();
+ void OpenProcFile();
+ const char* filename;
+ const char* filename;
+ FILE *file;
+ FILE* file;
+};
+};
+
+
+
+
+#endif
+#endif
</syntaxhighlight>
</syntaxhighlight>


[[Category:Core development projects]]
[[Category:Core development projects]]

Navigation menu