bes  Updated for version 3.20.5
BESMemoryGlobalArea.cc
1 // BESMemoryGlobalArea.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #include <iostream>
36 #include <cstdlib>
37 #include <cstring>
38 #include <cerrno>
39 
40 #include <sys/resource.h>
41 
42 using std::cerr;
43 using std::endl;
44 
45 #include "BESMemoryGlobalArea.h"
46 #include "BESInternalFatalError.h"
47 #include "BESDebug.h"
48 #include "BESLog.h"
49 #include "TheBESKeys.h"
50 
51 int BESMemoryGlobalArea::_counter = 0;
52 unsigned long BESMemoryGlobalArea::_size = 0;
53 void* BESMemoryGlobalArea::_buffer = 0;
54 
55 BESMemoryGlobalArea::BESMemoryGlobalArea()
56 {
57  limit.rlim_cur = 0;
58  limit.rlim_max = 0;
59 
60  if (_counter++ == 0) {
61  try {
62  bool fnd = false;
63  string key = "BES.Memory.GlobalArea.";
64 
65  string eps;
66  TheBESKeys::TheKeys()->get_value(key + "EmergencyPoolSize", eps, fnd);
67 
68  string mhs;
69  TheBESKeys::TheKeys()->get_value(key + "MaximumHeapSize", mhs, fnd);
70 
71  string verbose;
72  TheBESKeys::TheKeys()->get_value(key + "Verbose", verbose, fnd);
73 
74  string control_heap;
75  TheBESKeys::TheKeys()->get_value(key + "ControlHeap", control_heap, fnd);
76 
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.";
82  throw BESInternalFatalError(line, __FILE__, __LINE__);
83  }
84  else {
85  if (verbose == "no") BESLog::TheLog()->suspend();
86 
87  unsigned int emergency = atol(eps.c_str());
88 
89  if (control_heap == "yes") {
90  unsigned int max = atol(mhs.c_str());
91 
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";
96  LOG(s);
97  throw BESInternalFatalError(s, __FILE__, __LINE__);
98  }
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)
104  + "\n";
105  if ( errno == EPERM) {
106  s = s + "Attempting to increase the soft/hard " + "limit above the current hard limit, "
107  + "must be superuser\n";
108  }
109  LOG(s);
110  throw BESInternalFatalError(s, __FILE__, __LINE__);
111  }
112  log_limits("after setting limits: ");
113  _buffer = 0;
114  _buffer = malloc(megabytes(max));
115  if (!_buffer) {
116  string s = string("BES: ") + "cannot get heap of size " + mhs + " to start running";
117  LOG(s);
118  throw BESInternalFatalError(s, __FILE__, __LINE__);
119  }
120  free(_buffer);
121  }
122  else {
123  if (emergency > 10) {
124  string s = "Emergency pool is larger than 10 Megabytes";
125  throw BESInternalFatalError(s, __FILE__, __LINE__);
126  }
127  }
128 
129  _size = megabytes(emergency);
130  _buffer = 0;
131  _buffer = malloc(_size);
132  if (!_buffer) {
133  string s = (string) "BES: cannot expand heap to " + eps + " to start running";
134  LOG(s << endl);
135  throw BESInternalFatalError(s, __FILE__, __LINE__);
136  }
137  }
138  }
139  catch (BESError &ex) {
140  cerr << "BES: unable to start properly because " << ex.get_message() << endl;
141  exit(1);
142  }
143  catch (...) {
144  cerr << "BES: unable to start: undefined exception happened\n";
145  exit(1);
146  }
147  }
148 
149  BESLog::TheLog()->resume();
150 }
151 
152 BESMemoryGlobalArea::~BESMemoryGlobalArea()
153 {
154  if (--_counter == 0) {
155  if (_buffer) free(_buffer);
156  _buffer = 0;
157  }
158 }
159 
160 inline void BESMemoryGlobalArea::log_limits(const string &msg)
161 {
162  if (getrlimit( RLIMIT_DATA, &limit) < 0) {
163  LOG(msg << "Could not get limits because " << strerror( errno) << endl);
164  _counter--;
165  throw BESInternalFatalError(strerror( errno), __FILE__, __LINE__);
166  }
167  if (limit.rlim_cur == RLIM_INFINITY)
168  LOG(msg << "BES heap size soft limit is infinite" << endl);
169  else
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);
174  else
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);
177 }
178 
179 void BESMemoryGlobalArea::release_memory()
180 {
181  if (_buffer) {
182  free(_buffer);
183  _buffer = 0;
184  }
185 }
186 
187 bool BESMemoryGlobalArea::reclaim_memory()
188 {
189  if (!_buffer) _buffer = malloc(_size);
190  if (_buffer)
191  return true;
192  else
193  return false;
194 }
195 
203 void BESMemoryGlobalArea::dump(ostream &strm) const
204 {
205  strm << BESIndent::LMarg << "BESMemoryGlobalArea::dump - (" << (void *) this << ")" << endl;
206  BESIndent::Indent();
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();
213 }
214 
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
Definition: BESError.h:99
void resume()
Resumes logging after being suspended.
Definition: BESLog.h:144
virtual void dump(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.
Definition: TheBESKeys.cc:420
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:61
void suspend()
Suspend logging of any information until resumed.
Definition: BESLog.h:134