00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <sys/types.h>
00036 #include <inttypes.h>
00037
00038 #include <boost/lexical_cast.hpp>
00039
00040 #include "VMRSSLimit.h"
00041 #include "Log.h"
00042 #include "massert.h"
00043 #include "params.h"
00044
00045
00046 using namespace std;
00047
00048 VMRSSLimit* VMRSSLimit::instance = 0;
00049
00050 void VMRSSLimit::runVMRSSLimit( double maxRSS_MB, void (*callbackFnc)(double) ) {
00051
00052 if (instance == 0) {
00053 instance = new VMRSSLimit( maxRSS_MB, callbackFnc );
00054 }
00055 instance->cancel();
00056 instance->schedule(instance->frequency);
00057 }
00058
00059 void VMRSSLimit::stopVMRSSLimit() {
00060 instance->halt = true;
00061 }
00062
00063 VMRSSLimit::VMRSSLimit( double m, void (*f)(double) ) :
00064 halt(false), frequency(DEFAULT_FREQUENCY), maxRSS(m), callbackFnc(f)
00065 {
00066 ADD_SELECTORS("VMRSSLimit");
00067 statfile.open("/proc/self/statm");
00068 if (!statfile.good()) {
00069 perror( "REPLAY error opening /proc/self/statm" );
00070 return;
00071 }
00072
00073 if( params::containsKey(params::MACE_VMRSSLIMIT_FREQUENCY_MS))
00074 {
00075 frequency = (int)(params::get<int>(params::MACE_VMRSSLIMIT_FREQUENCY_MS) * 1000);
00076 }
00077
00078 maceLog("REPLAY VM RSS monitoring frequency is %" PRIu64
00079 " usecs.", frequency );
00080 }
00081
00082 void VMRSSLimit::expire() {
00083 ADD_SELECTORS("VMRSSLimit");
00084 static string tmp;
00085 int rss_pages;
00086
00087 statfile >> tmp >> rss_pages;
00088 statfile.seekg(0, ios::beg);
00089
00090 double currentRSS = ((double) rss_pages) * 4096.0 / (1024.0 * 1024.0);
00091
00092 maceLog("REPLAY VM RSS: %d pages %.1f MB",
00093 rss_pages, currentRSS );
00094
00095 if( maxRSS > 0 && currentRSS > maxRSS )
00096 {
00097 fprintf( stderr, "VMRSSLimit:: REPLAY VM RSS: ERROR maximum RSS exceeded. %.1f current %.1f max\n",
00098 currentRSS, maxRSS );
00099 maceError("REPLAY VM RSS: ERROR maximum RSS exceeded. %.1f current %.1f max",
00100 currentRSS, maxRSS );
00101
00102 if( callbackFnc != NULL )
00103 {
00104 maceLog("REPLAY VM RSS: entering callback function" );
00105 (*callbackFnc)(currentRSS);
00106 }
00107
00108 fflush(NULL);
00109 ABORT("VMRSSLimit Killing Program");
00110
00111 }
00112
00113 if (!halt) {
00114 schedule(frequency);
00115 }
00116 }
00117