41#include <sys/resource.h>
48#include "BESMemoryGlobalArea.h"
49#include "BESInternalFatalError.h"
52#include "TheBESKeys.h"
54#define prolog std::string("BESMemoryGlobalArea::").append(__func__).append("() - ")
56int BESMemoryGlobalArea::_counter = 0;
57unsigned long BESMemoryGlobalArea::_size = 0;
58void* BESMemoryGlobalArea::_buffer = 0;
60BESMemoryGlobalArea::BESMemoryGlobalArea()
65 if (_counter++ == 0) {
68 string key =
"BES.Memory.GlobalArea.";
82 if ((eps ==
"") || (mhs ==
"") || (verbose ==
"") || (control_heap ==
"")) {
83 string line =
"cannot determine memory keys.";
84 line += (string)
"Please set values for" +
" BES.Memory.GlobalArea.EmergencyPoolSize,"
85 +
" BES.Memory.GlobalArea.MaxiumumHeapSize," +
" BES.Memory.GlobalArea.Verbose, and"
86 +
" BES.Memory.GlobalArea.ControlHeap" +
" in the BES configuration file.";
90 if (verbose ==
"no") BESLog::TheLog()->
suspend();
92 unsigned int emergency = atol(eps.c_str());
94 if (control_heap ==
"yes") {
95 unsigned int max = atol(mhs.c_str());
97 INFO_LOG(prolog <<
"Initialize emergency heap size to " << (
unsigned long)emergency <<
" and heap size to " << (
unsigned long)(max + 1) <<
" MB" << endl);
98 if (emergency > max) {
99 string s = prolog +
"Unable to start since the emergency "
100 +
"pool is larger than the maximum size of " +
"the heap.\n";
104 log_limits(
"before setting limits: ");
105 limit.rlim_cur = megabytes(max + 1);
106 limit.rlim_max = megabytes(max + 1);
107 if (setrlimit( RLIMIT_DATA, &limit) < 0) {
108 string s = prolog +
"Could not set limit for the heap " +
"because " + strerror(errno)
110 if ( errno == EPERM) {
111 s = s +
"Attempting to increase the soft/hard " +
"limit above the current hard limit, "
112 +
"must be superuser\n";
117 log_limits(
"after setting limits: ");
119 _buffer = malloc(megabytes(max));
121 string s = prolog +
"Cannot get heap of size " + mhs +
" to start running";
128 if (emergency > 10) {
129 string s = prolog +
"Emergency pool is larger than 10 Megabytes";
134 _size = megabytes(emergency);
136 _buffer = malloc(_size);
138 string s = prolog +
"Cannot expand heap to " + eps +
" to start running";
139 ERROR_LOG(s << endl);
145 cerr <<
"BES: unable to start properly because " << ex.
get_message() << endl;
149 cerr <<
"BES: unable to start: undefined exception happened\n";
154 BESLog::TheLog()->
resume();
157BESMemoryGlobalArea::~BESMemoryGlobalArea()
159 if (--_counter == 0) {
160 if (_buffer) free(_buffer);
165inline void BESMemoryGlobalArea::log_limits(
const string &msg)
167 if (getrlimit( RLIMIT_DATA, &limit) < 0) {
168 std::stringstream moo;
169 moo << prolog << msg <<
"Could not get limits because " << strerror(errno) << endl;
170 ERROR_LOG(moo.str());
174 if (limit.rlim_cur == RLIM_INFINITY)
175 INFO_LOG(prolog << msg <<
"BES heap size soft limit is infinite" << endl);
177 INFO_LOG(prolog << msg <<
"BES heap size soft limit is " << (
long int) limit.rlim_cur <<
" bytes ("
178 << (
long int) (limit.rlim_cur) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
179 if (limit.rlim_max == RLIM_INFINITY)
180 INFO_LOG(prolog << msg <<
"BES heap size hard limit is infinite" << endl);
182 INFO_LOG(prolog <<
"BES heap size hard limit is " << (
long int) limit.rlim_max <<
" bytes ("
183 << (
long int) (limit.rlim_max) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
186void BESMemoryGlobalArea::release_memory()
194bool BESMemoryGlobalArea::reclaim_memory()
196 if (!_buffer) _buffer = malloc(_size);
212 strm << BESIndent::LMarg <<
"BESMemoryGlobalArea::dump - (" << (
void *)
this <<
")" << endl;
214 strm << BESIndent::LMarg <<
"area set? " << _counter << endl;
215 strm << BESIndent::LMarg <<
"emergency buffer: " << (
void *) _buffer << endl;
216 strm << BESIndent::LMarg <<
"buffer size: " << _size << endl;
217 strm << BESIndent::LMarg <<
"rlimit current: " << limit.rlim_cur << endl;
218 strm << BESIndent::LMarg <<
"rlimit max: " << limit.rlim_max << endl;
219 BESIndent::UnIndent();
Abstract exception class for the BES with basic string message.
virtual std::string get_message()
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()