40 #include <sys/resource.h>
45 #include "BESMemoryGlobalArea.h"
46 #include "BESInternalFatalError.h"
49 #include "TheBESKeys.h"
51 int BESMemoryGlobalArea::_counter = 0;
52 unsigned long BESMemoryGlobalArea::_size = 0;
53 void* BESMemoryGlobalArea::_buffer = 0;
55 BESMemoryGlobalArea::BESMemoryGlobalArea()
60 if (_counter++ == 0) {
63 string key =
"BES.Memory.GlobalArea.";
77 if ((eps ==
"") || (mhs ==
"") || (verbose ==
"") || (control_heap ==
"")) {
78 string line =
"cannot determine memory keys.";
79 line += (string)
"Please set values for" +
" BES.Memory.GlobalArea.EmergencyPoolSize,"
80 +
" BES.Memory.GlobalArea.MaxiumumHeapSize," +
" BES.Memory.GlobalArea.Verbose, and"
81 +
" BES.Memory.GlobalArea.ControlHeap" +
" in the BES configuration file.";
85 if (verbose ==
"no") BESLog::TheLog()->
suspend();
87 unsigned int emergency = atol(eps.c_str());
89 if (control_heap ==
"yes") {
90 unsigned int max = atol(mhs.c_str());
92 LOG(
"Initialize emergency heap size to " << (
unsigned long)emergency <<
" and heap size to " << (
unsigned long)(max + 1) <<
" MB" << endl);
93 if (emergency > max) {
94 string s = string(
"BES: ") +
"unable to start since the emergency "
95 +
"pool is larger than the maximum size of " +
"the heap.\n";
99 log_limits(
"before setting limits: ");
100 limit.rlim_cur = megabytes(max + 1);
101 limit.rlim_max = megabytes(max + 1);
102 if (setrlimit( RLIMIT_DATA, &limit) < 0) {
103 string s = string(
"BES: ") +
"Could not set limit for the heap " +
"because " + strerror(errno)
105 if ( errno == EPERM) {
106 s = s +
"Attempting to increase the soft/hard " +
"limit above the current hard limit, "
107 +
"must be superuser\n";
112 log_limits(
"after setting limits: ");
114 _buffer = malloc(megabytes(max));
116 string s = string(
"BES: ") +
"cannot get heap of size " + mhs +
" to start running";
123 if (emergency > 10) {
124 string s =
"Emergency pool is larger than 10 Megabytes";
129 _size = megabytes(emergency);
131 _buffer = malloc(_size);
133 string s = (string)
"BES: cannot expand heap to " + eps +
" to start running";
140 cerr <<
"BES: unable to start properly because " << ex.
get_message() << endl;
144 cerr <<
"BES: unable to start: undefined exception happened\n";
149 BESLog::TheLog()->
resume();
152 BESMemoryGlobalArea::~BESMemoryGlobalArea()
154 if (--_counter == 0) {
155 if (_buffer) free(_buffer);
160 inline void BESMemoryGlobalArea::log_limits(
const string &msg)
162 if (getrlimit( RLIMIT_DATA, &limit) < 0) {
163 LOG(msg <<
"Could not get limits because " << strerror( errno) << endl);
167 if (limit.rlim_cur == RLIM_INFINITY)
168 LOG(msg <<
"BES heap size soft limit is infinite" << endl);
170 LOG(msg <<
"BES heap size soft limit is " << (
long int) limit.rlim_cur <<
" bytes ("
171 << (
long int) (limit.rlim_cur) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
172 if (limit.rlim_max == RLIM_INFINITY)
173 LOG(msg <<
"BES heap size hard limit is infinite" << endl);
175 LOG(
"BES heap size hard limit is " << (
long int) limit.rlim_max <<
" bytes ("
176 << (
long int) (limit.rlim_max) / (MEGABYTE) <<
" MB - may be rounded up)" << endl);
179 void BESMemoryGlobalArea::release_memory()
187 bool BESMemoryGlobalArea::reclaim_memory()
189 if (!_buffer) _buffer = malloc(_size);
205 strm << BESIndent::LMarg <<
"BESMemoryGlobalArea::dump - (" << (
void *)
this <<
")" << endl;
207 strm << BESIndent::LMarg <<
"area set? " << _counter << endl;
208 strm << BESIndent::LMarg <<
"emergency buffer: " << (
void *) _buffer << endl;
209 strm << BESIndent::LMarg <<
"buffer size: " << _size << endl;
210 strm << BESIndent::LMarg <<
"rlimit current: " << limit.rlim_cur << endl;
211 strm << BESIndent::LMarg <<
"rlimit max: " << limit.rlim_max << endl;
212 BESIndent::UnIndent();