41#include <sys/resource.h>
48#include "BESMemoryGlobalArea.h"
49#include "BESInternalFatalError.h"
52#include "TheBESKeys.h"
70#define prolog std::string("BESMemoryGlobalArea::").append(__func__).append("() - ")
72int BESMemoryGlobalArea::_counter = 0;
73unsigned long BESMemoryGlobalArea::_size = 0;
74void* BESMemoryGlobalArea::_buffer = 0;
76BESMemoryGlobalArea::BESMemoryGlobalArea()
81 if (_counter++ == 0) {
84 string key =
"BES.Memory.GlobalArea.";
98 if ((eps ==
"") || (mhs ==
"") || (verbose ==
"") || (control_heap ==
"")) {
99 string line =
"cannot determine memory keys.";
100 line += (string)
"Please set values for" +
" BES.Memory.GlobalArea.EmergencyPoolSize,"
101 +
" BES.Memory.GlobalArea.MaxiumumHeapSize," +
" BES.Memory.GlobalArea.Verbose, and"
102 +
" BES.Memory.GlobalArea.ControlHeap" +
" in the BES configuration file.";
106 if (verbose ==
"no") BESLog::TheLog()->
suspend();
108 unsigned int emergency = atol(eps.c_str());
110 if (control_heap ==
"yes") {
111 unsigned int max = atol(mhs.c_str());
113 INFO_LOG(prolog <<
"Initialize emergency heap size to " << (
unsigned long)emergency <<
" and heap size to " << (
unsigned long)(max + 1) <<
" MB" << endl);
114 if (emergency > max) {
115 string s = prolog +
"Unable to start since the emergency "
116 +
"pool is larger than the maximum size of " +
"the heap.\n";
120 log_limits(
"before setting limits: ");
121 limit.rlim_cur = megabytes(max + 1);
122 limit.rlim_max = megabytes(max + 1);
123 if (setrlimit( RLIMIT_DATA, &limit) < 0) {
124 string s = prolog +
"Could not set limit for the heap " +
"because " + strerror(errno)
126 if ( errno == EPERM) {
127 s = s +
"Attempting to increase the soft/hard " +
"limit above the current hard limit, "
128 +
"must be superuser\n";
133 log_limits(
"after setting limits: ");
135 _buffer = malloc(megabytes(max));
137 string s = prolog +
"Cannot get heap of size " + mhs +
" to start running";
144 if (emergency > 10) {
145 string s = prolog +
"Emergency pool is larger than 10 Megabytes";
150 _size = megabytes(emergency);
152 _buffer = malloc(_size);
154 string s = prolog +
"Cannot expand heap to " + eps +
" to start running";
155 ERROR_LOG(s << endl);
161 cerr <<
"BES: unable to start properly because " << ex.
get_message() << endl;
165 cerr <<
"BES: unable to start: undefined exception happened\n";
170 BESLog::TheLog()->
resume();
173BESMemoryGlobalArea::~BESMemoryGlobalArea()
175 if (--_counter == 0) {
176 if (_buffer) free(_buffer);
181inline void BESMemoryGlobalArea::log_limits(
const string &msg)
183 if (getrlimit( RLIMIT_DATA, &limit) < 0) {
184 std::stringstream moo;
185 moo << prolog << msg <<
"Could not get limits because " << strerror(errno) << endl;
186 ERROR_LOG(moo.str());
190 if (limit.rlim_cur == RLIM_INFINITY)
191 INFO_LOG(prolog << msg <<
"BES heap size soft limit is infinite" << endl);
193 INFO_LOG(prolog << msg <<
"BES heap size soft limit is " << (
long int) limit.rlim_cur <<
" bytes ("
194 << (
long int) (limit.rlim_cur) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
195 if (limit.rlim_max == RLIM_INFINITY)
196 INFO_LOG(prolog << msg <<
"BES heap size hard limit is infinite" << endl);
198 INFO_LOG(prolog <<
"BES heap size hard limit is " << (
long int) limit.rlim_max <<
" bytes ("
199 << (
long int) (limit.rlim_max) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
202void BESMemoryGlobalArea::release_memory()
210bool BESMemoryGlobalArea::reclaim_memory()
212 if (!_buffer) _buffer = malloc(_size);
228 strm << BESIndent::LMarg <<
"BESMemoryGlobalArea::dump - (" << (
void *)
this <<
")" << endl;
230 strm << BESIndent::LMarg <<
"area set? " << _counter << endl;
231 strm << BESIndent::LMarg <<
"emergency buffer: " << (
void *) _buffer << endl;
232 strm << BESIndent::LMarg <<
"buffer size: " << _size << endl;
233 strm << BESIndent::LMarg <<
"rlimit current: " << limit.rlim_cur << endl;
234 strm << BESIndent::LMarg <<
"rlimit max: " << limit.rlim_max << endl;
235 BESIndent::UnIndent();
Base exception class for the BES with basic string message.
std::string get_message() const
get the error message for this exception
exception thrown if an internal error is found and is fatal to the BES
void resume()
Resumes logging after being suspended.
void suspend()
Suspend logging of any information until resumed.
virtual void dump(std::ostream &strm) const
dumps information about this object
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()