Commit a3805817835e45ec232e73fc0b156f09e4ce716e
1 parent
e75fe5a4
Add get_current_rss() method to determine the physical memory size of the proces…
…s. This method is useful for debugging the memory usage of ctools.
Showing
2 changed files
with
54 additions
and
0 deletions
src/support/ctool.cpp
| @@ -31,6 +31,21 @@ | @@ -31,6 +31,21 @@ | ||
| 31 | #include "ctool.hpp" | 31 | #include "ctool.hpp" |
| 32 | #include "GTools.hpp" | 32 | #include "GTools.hpp" |
| 33 | 33 | ||
| 34 | +/* __ Includes for memory usage determination ____________________________ */ | ||
| 35 | +#if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) | ||
| 36 | +#include <unistd.h> | ||
| 37 | +#include <sys/resource.h> | ||
| 38 | +#if defined(__APPLE__) && defined(__MACH__) | ||
| 39 | +#include <mach/mach.h> | ||
| 40 | +#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__))) | ||
| 41 | +#include <fcntl.h> | ||
| 42 | +#include <procfs.h> | ||
| 43 | +#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) | ||
| 44 | +#include <stdio.h> | ||
| 45 | +#endif | ||
| 46 | +#endif | ||
| 47 | + | ||
| 48 | + | ||
| 34 | /* __ Method name definitions ____________________________________________ */ | 49 | /* __ Method name definitions ____________________________________________ */ |
| 35 | #define G_GET_EBOUNDS "ctool::get_ebounds()" | 50 | #define G_GET_EBOUNDS "ctool::get_ebounds()" |
| 36 | 51 | ||
| @@ -391,3 +406,41 @@ void ctool::set_obs_response(GCTAObservation* obs) | @@ -391,3 +406,41 @@ void ctool::set_obs_response(GCTAObservation* obs) | ||
| 391 | // Return | 406 | // Return |
| 392 | return; | 407 | return; |
| 393 | } | 408 | } |
| 409 | + | ||
| 410 | + | ||
| 411 | +/***********************************************************************//** | ||
| 412 | + * @brief Get current resident set size (physical memory use) in Bytes | ||
| 413 | + * | ||
| 414 | + * @return Physical memory use in Bytes. | ||
| 415 | + ***************************************************************************/ | ||
| 416 | +size_t ctool::get_current_rss(void) | ||
| 417 | +{ | ||
| 418 | + // Initialize resident set size | ||
| 419 | + size_t rss = 0; | ||
| 420 | + | ||
| 421 | + // Determine resident set size (architecture dependent) | ||
| 422 | + // OSX | ||
| 423 | + #if defined(__APPLE__) && defined(__MACH__) | ||
| 424 | + struct mach_task_basic_info info; | ||
| 425 | + mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT; | ||
| 426 | + if (task_info(mach_task_self( ), MACH_TASK_BASIC_INFO, | ||
| 427 | + (task_info_t)&info, &infoCount) == KERN_SUCCESS) { | ||
| 428 | + rss = (size_t)info.resident_size; | ||
| 429 | + } | ||
| 430 | + // Linux | ||
| 431 | + #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) | ||
| 432 | + FILE* fp = NULL; | ||
| 433 | + if ((fp = fopen( "/proc/self/statm", "r" )) != NULL) { | ||
| 434 | + if (fscanf( fp, "%*s%ld", &rss ) == 1) { | ||
| 435 | + rss *= (size_t)sysconf(_SC_PAGESIZE); | ||
| 436 | + } | ||
| 437 | + fclose(fp); | ||
| 438 | + } | ||
| 439 | + // AIX, BSD, Solaris, and Unknown OS | ||
| 440 | + #else | ||
| 441 | + rss = 0; | ||
| 442 | + #endif | ||
| 443 | + | ||
| 444 | + // Return resident set size | ||
| 445 | + return rss; | ||
| 446 | +} |
src/support/ctool.hpp
| @@ -69,6 +69,7 @@ protected: | @@ -69,6 +69,7 @@ protected: | ||
| 69 | GEbounds get_ebounds(void); | 69 | GEbounds get_ebounds(void); |
| 70 | void set_response(GObservations& obs); | 70 | void set_response(GObservations& obs); |
| 71 | void set_obs_response(GCTAObservation* obs); | 71 | void set_obs_response(GCTAObservation* obs); |
| 72 | + size_t get_current_rss(void); | ||
| 72 | 73 | ||
| 73 | // Protected members | 74 | // Protected members |
| 74 | bool m_read_ahead; //!< Read ahead parameters | 75 | bool m_read_ahead; //!< Read ahead parameters |