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());
88 LOG(
"Initialize emergency heap size to " << (
unsigned long)emergency <<
" and heap size to " << (
unsigned long)(max + 1) <<
" MB" << endl);
89 if (emergency > max) {
90 string s = string(
"BES: ") +
"unable to start since the emergency " 91 +
"pool is larger than the maximum size of " +
"the heap.\n";
95 log_limits(
"before setting limits: ");
96 limit.rlim_cur = megabytes(max + 1);
97 limit.rlim_max = megabytes(max + 1);
98 if (setrlimit( RLIMIT_DATA, &limit) < 0) {
99 string s = string(
"BES: ") +
"Could not set limit for the heap " +
"because " + strerror(errno)
101 if ( errno == EPERM) {
102 s = s +
"Attempting to increase the soft/hard " +
"limit above the current hard limit, " 103 +
"must be superuser\n";
108 log_limits(
"after setting limits: ");
110 _buffer = malloc(megabytes(max));
112 string s = string(
"BES: ") +
"cannot get heap of size " + mhs +
" to start running";
119 if (emergency > 10) {
120 string s =
"Emergency pool is larger than 10 Megabytes";
125 _size = megabytes(emergency);
127 _buffer = malloc(_size);
129 string s = (string)
"BES: cannot expand heap to " + eps +
" to start running";
136 cerr <<
"BES: unable to start properly because " << ex.
get_message() << endl;
140 cerr <<
"BES: unable to start: undefined exception happened\n";
145 BESLog::TheLog()->
resume();
148 BESMemoryGlobalArea::~BESMemoryGlobalArea()
150 if (--_counter == 0) {
151 if (_buffer) free(_buffer);
156 inline void BESMemoryGlobalArea::log_limits(
const string &msg)
158 if (getrlimit( RLIMIT_DATA, &limit) < 0) {
159 LOG(msg <<
"Could not get limits because " << strerror( errno) << endl);
163 if (limit.rlim_cur == RLIM_INFINITY)
164 LOG(msg <<
"BES heap size soft limit is infinite" << endl);
166 LOG(msg <<
"BES heap size soft limit is " << (
long int) limit.rlim_cur <<
" bytes (" 167 << (
long int) (limit.rlim_cur) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
168 if (limit.rlim_max == RLIM_INFINITY)
169 LOG(msg <<
"BES heap size hard limit is infinite" << endl);
171 LOG(
"BES heap size hard limit is " << (
long int) limit.rlim_max <<
" bytes (" 172 << (
long int) (limit.rlim_max) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
175 void BESMemoryGlobalArea::release_memory()
183 bool BESMemoryGlobalArea::reclaim_memory()
185 if (!_buffer) _buffer = malloc(_size);
201 strm << BESIndent::LMarg <<
"BESMemoryGlobalArea::dump - (" << (
void *)
this <<
")" << endl;
203 strm << BESIndent::LMarg <<
"area set? " << _counter << endl;
204 strm << BESIndent::LMarg <<
"emergency buffer: " << (
void *) _buffer << endl;
205 strm << BESIndent::LMarg <<
"buffer size: " << _size << endl;
206 strm << BESIndent::LMarg <<
"rlimit current: " << limit.rlim_cur << endl;
207 strm << BESIndent::LMarg <<
"rlimit max: " << limit.rlim_max << endl;
208 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
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Abstract exception class for the BES with basic string message.
static TheBESKeys * TheKeys()
void suspend()
Suspend logging of any information until resumed.