41 #include "BESMemoryGlobalArea.h" 42 #include "BESInternalFatalError.h" 45 #include "TheBESKeys.h" 47 int BESMemoryGlobalArea::_counter = 0;
48 unsigned long BESMemoryGlobalArea::_size = 0;
49 void* BESMemoryGlobalArea::_buffer = 0;
51 BESMemoryGlobalArea::BESMemoryGlobalArea()
56 if (_counter++ == 0) {
59 string key =
"BES.Memory.GlobalArea.";
73 if ((eps ==
"") || (mhs ==
"") || (verbose ==
"") || (control_heap ==
"")) {
74 string line =
"cannot determine memory keys.";
75 line += (string)
"Please set values for" +
" BES.Memory.GlobalArea.EmergencyPoolSize," 76 +
" BES.Memory.GlobalArea.MaxiumumHeapSize," +
" BES.Memory.GlobalArea.Verbose, and" 77 +
" BES.Memory.GlobalArea.ControlHeap" +
" in the BES configuration file.";
81 if (verbose ==
"no") BESLog::TheLog()->
suspend();
83 unsigned int emergency = atol(eps.c_str());
85 if (control_heap ==
"yes") {
86 unsigned int max = atol(mhs.c_str());
87 BESDEBUG(
"bes",
"Initializing emergency heap to " 88 << (
long int)emergency <<
" MB" << endl );
89 BESDEBUG(
"bes",
"Initializing max heap size to " 90 << (
long int)(max+1) <<
" MB" << endl );
91 (*BESLog::TheLog()) <<
"Initialize emergency heap size to " << (
long int) emergency
92 <<
" and heap size to ";
93 (*BESLog::TheLog()) << (
long int) (max + 1) <<
" megabytes" << endl;
94 if (emergency > max) {
95 string s = string(
"BES: ") +
"unable to start since the emergency " 96 +
"pool is larger than the maximum size of " +
"the heap.\n";
97 (*BESLog::TheLog()) << s;
100 log_limits(
"before setting limits: ");
101 limit.rlim_cur = megabytes(max + 1);
102 limit.rlim_max = megabytes(max + 1);
103 if (setrlimit( RLIMIT_DATA, &limit) < 0) {
104 string s = string(
"BES: ") +
"Could not set limit for the heap " +
"because " + strerror(errno)
106 if ( errno == EPERM) {
107 s = s +
"Attempting to increase the soft/hard " +
"limit above the current hard limit, " 108 +
"must be superuser\n";
110 (*BESLog::TheLog()) << s;
113 log_limits(
"after setting limits: ");
115 _buffer = malloc(megabytes(max));
117 string s = string(
"BES: ") +
"cannot get heap of size " + mhs +
" to start running";
118 (*BESLog::TheLog()) << s;
124 if (emergency > 10) {
125 string s =
"Emergency pool is larger than 10 Megabytes";
130 _size = megabytes(emergency);
132 _buffer = malloc(_size);
134 string s = (string)
"BES: cannot expand heap to " + eps +
" to start running";
135 (*BESLog::TheLog()) << s << endl;
141 cerr <<
"BES: unable to start properly because " << ex.
get_message() << endl;
145 cerr <<
"BES: unable to start: undefined exception happened\n";
150 BESLog::TheLog()->
resume();
153 BESMemoryGlobalArea::~BESMemoryGlobalArea()
155 if (--_counter == 0) {
156 if (_buffer) free(_buffer);
161 inline void BESMemoryGlobalArea::log_limits(
const string &msg)
163 if (getrlimit( RLIMIT_DATA, &limit) < 0) {
164 (*BESLog::TheLog()) << msg <<
"Could not get limits because " << strerror( errno) << endl;
168 if (limit.rlim_cur == RLIM_INFINITY)
169 (*BESLog::TheLog()) << msg <<
"heap size soft limit is infinte" << endl;
171 (*BESLog::TheLog()) << msg <<
"heap size soft limit is " << (
long int) limit.rlim_cur <<
" bytes (" 172 << (
long int) (limit.rlim_cur) / (MEGABYTE) <<
" MB - may be rounded up)" << endl;
173 if (limit.rlim_max == RLIM_INFINITY)
174 (*BESLog::TheLog()) << msg <<
"heap size hard limit is infinite" << endl;
176 (*BESLog::TheLog()) << msg <<
"heap size hard limit is " << (
long int) limit.rlim_max <<
" bytes (" 177 << (
long int) (limit.rlim_max) / (MEGABYTE) <<
" MB - may be rounded up)" << endl;
180 void BESMemoryGlobalArea::release_memory()
188 bool BESMemoryGlobalArea::reclaim_memory()
190 if (!_buffer) _buffer = malloc(_size);
206 strm << BESIndent::LMarg <<
"BESMemoryGlobalArea::dump - (" << (
void *)
this <<
")" << endl;
208 strm << BESIndent::LMarg <<
"area set? " << _counter << endl;
209 strm << BESIndent::LMarg <<
"emergency buffer: " << (
void *) _buffer << endl;
210 strm << BESIndent::LMarg <<
"buffer size: " << _size << endl;
211 strm << BESIndent::LMarg <<
"rlimit current: " << limit.rlim_cur << endl;
212 strm << BESIndent::LMarg <<
"rlimit max: " << limit.rlim_max << endl;
213 BESIndent::UnIndent();
exception thrown if an internal error is found and is fatal to the BES
virtual std::string get_message()
get the error message for this exception
void resume()
Resumes logging after being suspended.
virtual void dump(ostream &strm) const
dumps information about this object
Abstract exception class for the BES with basic string message.
void suspend()
Suspend logging of any information until resumed.
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
static BESKeys * TheKeys()