Advertisement
hamzaalloush

Hooray's ram_usage.patch

Aug 20th, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.07 KB | None | 0 0
  1. diff -urN a/src/Main/CMakeLists.txt b/src/Main/CMakeLists.txt
  2. --- a/src/Main/CMakeLists.txt   2015-08-20 23:03:15.070835000 +0300
  3. +++ b/src/Main/CMakeLists.txt   2015-08-20 23:26:58.706798388 +0300
  4. @@ -17,12 +17,17 @@
  5.     main.cxx
  6.     options.cxx
  7.     util.cxx
  8. +   ram_usage.cxx
  9.      positioninit.cxx
  10.      subsystemFactory.cxx
  11.      screensaver_control.cxx
  12.     ${RESOURCE_FILE}
  13.     )
  14.  
  15. +IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  16. +        list(APPEND SOURCES ram_usage_linux.cxx)
  17. +ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  18. +
  19.  set(HEADERS
  20.     fg_commands.hxx
  21.     fg_init.hxx
  22. @@ -35,12 +40,19 @@
  23.     main.hxx
  24.     options.hxx
  25.     util.hxx
  26. +   ram_usage.hxx
  27.      positioninit.hxx
  28.      subsystemFactory.hxx
  29.      AircraftDirVisitorBase.hxx
  30.      screensaver_control.hxx
  31.     )
  32.  
  33. +IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  34. +        list(APPEND HEADERS ram_usage_linux.hxx)
  35. +ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  36. +
  37. +
  38. +
  39.  get_property(FG_SOURCES GLOBAL PROPERTY FG_SOURCES)
  40.  get_property(FG_HEADERS GLOBAL PROPERTY FG_HEADERS)
  41.  
  42. diff -urN a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx
  43. --- a/src/Main/fg_init.cxx  2015-08-20 23:22:52.166804000 +0300
  44. +++ b/src/Main/fg_init.cxx  2015-08-20 23:29:15.074794813 +0300
  45. @@ -141,6 +141,7 @@
  46.  #include "globals.hxx"
  47.  #include "logger.hxx"
  48.  #include "main.hxx"
  49. +#include "ram_usage.hxx"
  50.  #include "positioninit.hxx"
  51.  #include "util.hxx"
  52.  #include "AircraftDirVisitorBase.hxx"
  53. @@ -715,6 +716,10 @@
  54.      ////////////////////////////////////////////////////////////////////
  55.      globals->add_subsystem("properties", new FGProperties);
  56.  
  57. +    ////////////////////////////////////////////////////////////////////
  58. +    // Add the ram usage statistics system
  59. +    ////////////////////////////////////////////////////////////////////
  60. +    globals->add_subsystem("memory-stats", new MemoryUsageStats, SGSubsystemMgr::INIT, 5.00);
  61.  
  62.      ////////////////////////////////////////////////////////////////////
  63.      // Add the performance monitoring system.
  64. diff -urN a/src/Main/ram_usage.cxx b/src/Main/ram_usage.cxx
  65. --- a/src/Main/ram_usage.cxx    1970-01-01 03:00:00.000000000 +0300
  66. +++ b/src/Main/ram_usage.cxx    2015-08-20 23:29:53.950793794 +0300
  67. @@ -0,0 +1,22 @@
  68. +#include "ram_usage_linux.hxx"
  69. +
  70. +MemoryUsageStats::MemoryUsageStats() {
  71. + _mem = new LinuxMemoryInterface(); //FIXME: should be implemented for Win/Mac & Linux
  72. +}
  73. +
  74. +MemoryUsageStats::~MemoryUsageStats() {
  75. + delete _mem;
  76. +}
  77. +
  78. +void
  79. +MemoryUsageStats::update(double dt) {
  80. +   _mem->update();
  81. +   double swap = _mem->getSwapSize();
  82. +   double total = _mem->getTotalSize();
  83. +   SG_LOG(SG_GENERAL, SG_DEBUG, "Updating Memory Stats:" << total << " kb");
  84. +   fgSetInt("/memory-usage/swap-usage-kb",  swap );
  85. +   fgSetInt("/memory-usage/total-usage-kb", total );
  86. +}
  87. +
  88. +
  89. +
  90. diff -urN a/src/Main/ram_usage.hxx b/src/Main/ram_usage.hxx
  91. --- a/src/Main/ram_usage.hxx    1970-01-01 03:00:00.000000000 +0300
  92. +++ b/src/Main/ram_usage.hxx    2015-08-20 23:29:53.950793794 +0300
  93. @@ -0,0 +1,51 @@
  94. +#ifndef __RAM_USAGE
  95. +#define __RAM_USAGE
  96. +
  97. +#include <simgear/timing/timestamp.hxx>
  98. +#include <simgear/structure/subsystem_mgr.hxx>
  99. +
  100. +#include <Main/globals.hxx>
  101. +#include <Main/fg_props.hxx>
  102. +
  103. +#include <string>
  104. +#include <map>
  105. +
  106. +using std::map;
  107. +
  108. +// Linux: /proc/pid/smaps
  109. +// Windows: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v=vs.85).aspx
  110. +
  111. +class MemoryInterface {
  112. +public:
  113. + MemoryInterface() {}
  114. + typedef map<const char*, double> RamMap;
  115. +//protected:
  116. + virtual void update() = 0;
  117. +
  118. + double getTotalSize() const {return _total_size;}
  119. + //virtual void   setTotalSize(double t) {_total_size=t;}
  120. +
  121. + double getSwapSize() const {return _swap_size;}
  122. + //virtual void   setSwapSize(double s) {_swap_size=s;}
  123. +protected:
  124. + RamMap _size;
  125. + std::string _path;
  126. + std::stringstream _pid;
  127. +
  128. + double _total_size;
  129. + double _swap_size;
  130. +};
  131. +
  132. +class MemoryUsageStats : public SGSubsystem
  133. +{
  134. +public:
  135. +  MemoryUsageStats();
  136. + ~MemoryUsageStats();
  137. +  virtual void update(double);
  138. +protected:
  139. +private:
  140. +  MemoryInterface* _mem;
  141. +};
  142. +
  143. +#endif
  144. +
  145. diff -urN a/src/Main/ram_usage_linux.cxx b/src/Main/ram_usage_linux.cxx
  146. --- a/src/Main/ram_usage_linux.cxx  1970-01-01 03:00:00.000000000 +0300
  147. +++ b/src/Main/ram_usage_linux.cxx  2015-08-20 23:29:53.950793794 +0300
  148. @@ -0,0 +1,49 @@
  149. +// https://gist.github.com/896026/c346c7c8e4a9ab18577b4e6abfca37e358de83c1
  150. +
  151. +#include "ram_usage_linux.hxx"
  152. +
  153. +#include <cstring>
  154. +#include <string>
  155. +
  156. +#include "Main/globals.hxx"
  157. +
  158. +using std::string;
  159. +
  160. +LinuxMemoryInterface::LinuxMemoryInterface() {
  161. + _pid << getpid();
  162. + _path = "/proc/"+ _pid.str() +"/smaps";
  163. +}
  164. +
  165. +void
  166. +LinuxMemoryInterface::OpenProcFile() {
  167. + file = fopen(_path.c_str(),"r" );
  168. + if (!file) {
  169. +        throw("MemoryTracker:Cannot open /proc/pid/smaps");
  170. +    }
  171. + SG_LOG(SG_GENERAL, SG_DEBUG, "Opened:"<< _path.c_str() );
  172. +}
  173. +
  174. +LinuxMemoryInterface::~LinuxMemoryInterface() {
  175. + if (file) fclose(file);
  176. +}
  177. +
  178. +void LinuxMemoryInterface::update() {
  179. + OpenProcFile();
  180. + if (!file) throw("MemoryTracker: ProcFile not open");
  181. +
  182. +  _total_size = 0;
  183. +  _swap_size = 0;
  184. +
  185. +  char line[1024];
  186. +  while (fgets(line, sizeof line, file))
  187. +    {
  188. +        char substr[32];
  189. +        int n;
  190. +        if (sscanf(line, "%31[^:]: %d", substr, &n) == 2) {
  191. +          if (strcmp(substr, "Size") == 0)           { _total_size += n; }
  192. +               else    if (strcmp(substr, "Swap") == 0)           { _swap_size += n; }
  193. +    }
  194. +   }
  195. +  fclose(file);
  196. +}
  197. +
  198. diff -urN a/src/Main/ram_usage_linux.hxx b/src/Main/ram_usage_linux.hxx
  199. --- a/src/Main/ram_usage_linux.hxx  1970-01-01 03:00:00.000000000 +0300
  200. +++ b/src/Main/ram_usage_linux.hxx  2015-08-20 23:29:53.950793794 +0300
  201. @@ -0,0 +1,22 @@
  202. +#ifndef __RAM_USAGE_LINUX
  203. +#define __RAM_USAGE_LINUX
  204. +
  205. + #include <sys/types.h>
  206. + #include <unistd.h>
  207. + #include <stdio.h>
  208. +
  209. + #include "ram_usage.hxx"
  210. +
  211. +class LinuxMemoryInterface : public MemoryInterface {
  212. +public:
  213. + LinuxMemoryInterface();
  214. +~LinuxMemoryInterface();
  215. + virtual void update();
  216. +private:
  217. + void OpenProcFile();
  218. + const char* filename;
  219. + FILE *file;
  220. +};
  221. +
  222. +
  223. +#endif
  224. +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement