Skip to content

v0.2.54..v0.2.55 changeset SystemInfo.cpp

Garret Voltz edited this page Aug 14, 2020 · 1 revision
diff --git a/tgs/src/main/cpp/tgs/System/SystemInfo.cpp b/tgs/src/main/cpp/tgs/System/SystemInfo.cpp
index a33c611..463e41f 100644
--- a/tgs/src/main/cpp/tgs/System/SystemInfo.cpp
+++ b/tgs/src/main/cpp/tgs/System/SystemInfo.cpp
@@ -22,7 +22,7 @@
  * This will properly maintain the copyright information. DigitalGlobe
  * copyrights will be updated automatically.
  *
- * @copyright Copyright (C) 2015, 2017, 2018, 2019 DigitalGlobe (http://www.digitalglobe.com/)
+ * @copyright Copyright (C) 2015, 2017, 2018, 2019, 2020 DigitalGlobe (http://www.digitalglobe.com/)
  */
 
 #include "SystemInfo.h"
@@ -42,7 +42,10 @@
 #include <fstream>
 #include <string>
 #include <sstream>
+#include "sys/types.h"
+#include "sys/sysinfo.h"
 
+// tgs
 #include <tgs/TgsException.h>
 
 namespace Tgs
@@ -64,10 +67,9 @@ namespace Tgs
   }
 
 # ifdef _WIN32
-#   error "getMemoryUsage not defined for WIN32"
+#   error "Memory usage stats not defined for WIN32"
 # endif
-  // taken from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
-  void SystemInfo::getMemoryUsage(long& vmUsage, long& residentSet)
+  void SystemInfo::_getCurrentProcessMemoryUsage(long& vmUsage, long& residentSet)
   {
     using std::ios_base;
     using std::ifstream;
@@ -77,18 +79,15 @@ namespace Tgs
     residentSet = 0.0;
 
     // 'file' stat seems to give the most reliable results
-    //
     ifstream stat_stream("/proc/self/stat", ios_base::in);
 
     // dummy vars for leading entries in stat that we don't care about
-    //
     string pid, comm, state, ppid, pgrp, session, tty_nr;
     string tpgid, flags, minflt, cminflt, majflt, cmajflt;
     string utime, stime, cutime, cstime, priority, nice;
     string O, itrealvalue, starttime;
 
     // the two fields we want
-    //
     unsigned long vsize;
     long rss;
 
@@ -109,7 +108,7 @@ namespace Tgs
     }
   }
 
-  std::string SystemInfo::humanReadable(long bytes)
+  std::string SystemInfo::humanReadableStorageSize(long bytes)
   {
     std::stringstream ss;
 
@@ -138,15 +137,140 @@ namespace Tgs
     return ss.str();
   }
 
-  std::string SystemInfo::getMemoryUsageString()
+  std::string SystemInfo::getCurrentProcessMemoryUsageString()
   {
     std::stringstream ss;
     long vm;
     long rss;
-    getMemoryUsage(vm, rss);
+    _getCurrentProcessMemoryUsage(vm, rss);
 
-    ss << "Memory usage, vm:\t" << humanReadable(vm) << "\trss:\t" << humanReadable(rss);
+    ss << "Process memory usage: virtual:\t" << humanReadableStorageSize(vm) << "\tphysical:\t" <<
+          humanReadableStorageSize(rss);
 
     return ss.str();
   }
+
+  std::string SystemInfo::getMemoryDetailString()
+  {
+    std::stringstream ss;
+
+    ss << "Total system virtual memory: " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getTotalSystemVirtualMemory()) <<
+          "\n";
+    ss << "System virtual memory used: " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getSystemVirtualMemoryUsed()) <<
+          "\n";
+    ss << "Current process virtual memory usage (kb): " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getCurrentProcessVirtualMemoryUsage()) <<
+          "\n";
+    ss << "Virtual memory available: " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getVirtualMemoryAvailable()) << "\n";
+    ss << "Percentage of virtual memory used: " <<
+          SystemInfo::getPercentageOfVirtualMemoryUsed() << "%" << "\n";
+
+    ss << "Total system physical memory: " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getTotalSystemPhysicalMemory()) <<
+          "\n";
+    ss << "System physical memory used: " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getSystemPhysicalMemoryUsed()) <<
+          "\n";
+    ss << "Current process physical memory usage: " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getCurrentProcessPhysicalMemoryUsage()) <<
+          "\n";
+    ss << "Physical memory available: " <<
+          SystemInfo::humanReadableStorageSize(SystemInfo::getPhysicalMemoryAvailable()) <<
+          "\n";
+    ss << "Percentage of physical memory used: " <<
+          SystemInfo::getPercentageOfPhysicalMemoryUsed() << "%" << "\n";
+
+    return ss.str();
+  }
+
+  long SystemInfo::getTotalSystemVirtualMemory()
+  {
+    struct sysinfo memInfo;
+    sysinfo (&memInfo);
+    long long totalVirtualMem = memInfo.totalram;
+    //Add other values in next statement to avoid int overflow on right hand side...
+    totalVirtualMem += memInfo.totalswap;
+    totalVirtualMem *= memInfo.mem_unit;
+    return (long)totalVirtualMem;
+  }
+
+  long SystemInfo::getSystemVirtualMemoryUsed()
+  {
+    struct sysinfo memInfo;
+    sysinfo (&memInfo);
+    long long virtualMemUsed = memInfo.totalram - memInfo.freeram;
+    //Add other values in next statement to avoid int overflow on right hand side...
+    virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
+    virtualMemUsed *= memInfo.mem_unit;
+    return (long)virtualMemUsed;
+  }
+
+  long SystemInfo::getCurrentProcessVirtualMemoryUsage()
+  {
+    long v;
+    long rss;
+    _getCurrentProcessMemoryUsage(v, rss);
+    return v;
+  }
+
+  long SystemInfo::getVirtualMemoryAvailable()
+  {
+    return getTotalSystemVirtualMemory() - getSystemVirtualMemoryUsed();
+  }
+
+  double SystemInfo::getPercentageOfVirtualMemoryUsed()
+  {
+    return (double)getSystemVirtualMemoryUsed() /  (double)getTotalSystemVirtualMemory();
+  }
+
+  double SystemInfo::getPercentageOfVirtualMemoryUsedByCurrentProcess()
+  {
+    return (double)getCurrentProcessVirtualMemoryUsage() /  (double)getTotalSystemVirtualMemory();
+  }
+
+  long SystemInfo::getTotalSystemPhysicalMemory()
+  {
+    struct sysinfo memInfo;
+    sysinfo(&memInfo);
+    long long totalPhysMem = memInfo.totalram;
+    // Multiply in next statement to avoid int overflow on right hand side...
+    totalPhysMem *= memInfo.mem_unit;
+    return (long)totalPhysMem;
+  }
+
+  long SystemInfo::getSystemPhysicalMemoryUsed()
+  {
+    struct sysinfo memInfo;
+    sysinfo(&memInfo);
+    long long physMemUsed = memInfo.totalram - memInfo.freeram;
+    // Multiply in next statement to avoid int overflow on right hand side...
+    physMemUsed *= memInfo.mem_unit;
+    return (long)physMemUsed;
+  }
+
+  long SystemInfo::getCurrentProcessPhysicalMemoryUsage()
+  {
+    long v;
+    long rss;
+    _getCurrentProcessMemoryUsage(v, rss);
+    return rss;
+  }
+
+  long SystemInfo::getPhysicalMemoryAvailable()
+  {
+    return getTotalSystemPhysicalMemory() - getSystemPhysicalMemoryUsed();
+  }
+
+  double SystemInfo::getPercentageOfPhysicalMemoryUsed()
+  {
+    return (double)getSystemPhysicalMemoryUsed() /  (double)getTotalSystemPhysicalMemory();
+  }
+
+  double SystemInfo::getPercentageOfPhysicalMemoryUsedByCurrentProcess()
+  {
+    return (double)getCurrentProcessPhysicalMemoryUsage() /  (double)getTotalSystemPhysicalMemory();
+  }
 }
Clone this wiki locally