bes  Updated for version 3.20.13
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 <sstream>
37 #include <cstdlib>
38 #include <cstring>
39 #include <cerrno>
40 
41 #include <sys/resource.h>
42 
43 using std::cerr;
44 using std::endl;
45 using std::string;
46 using std::ostream;
47 
48 #include "BESMemoryGlobalArea.h"
49 #include "BESInternalFatalError.h"
50 #include "BESDebug.h"
51 #include "BESLog.h"
52 #include "TheBESKeys.h"
53 
54 // TODO Add documentation for this class here and in the header. It seems that
55 // this is what the class does: allocates a buffer of 'emergency' MB. If
56 // ControlHeap is 'yes', the it will set the max data size of this process to
57 // the value of MaximumHeapSize MB (using setrlimit()) and then _test_ that
58 // out by malloc()'ing and then free()'ing a buffer of that size. If Verbose
59 // is 'no' log out put is suspended.
60 //
61 // I think this should not use the simple counter logic but use share_ptr.
62 // The strings passed to ERROR_LOG(), etc should be cleaned up - use \n or endl
63 // or not, but not a mixture.
64 //
65 // But wait... is this ever used? I think it was used by the BESApacheInterface.cc
66 // file but that's all.
67 //
68 // Write unit tests for this if we're going to keep it.
69 
70 #define prolog std::string("BESMemoryGlobalArea::").append(__func__).append("() - ")
71 
72 int BESMemoryGlobalArea::_counter = 0;
73 unsigned long BESMemoryGlobalArea::_size = 0;
74 void* BESMemoryGlobalArea::_buffer = 0;
75 
76 BESMemoryGlobalArea::BESMemoryGlobalArea()
77 {
78  limit.rlim_cur = 0;
79  limit.rlim_max = 0;
80 
81  if (_counter++ == 0) {
82  try {
83  bool fnd = false;
84  string key = "BES.Memory.GlobalArea.";
85 
86  string eps;
87  TheBESKeys::TheKeys()->get_value(key + "EmergencyPoolSize", eps, fnd);
88 
89  string mhs;
90  TheBESKeys::TheKeys()->get_value(key + "MaximumHeapSize", mhs, fnd);
91 
92  string verbose;
93  TheBESKeys::TheKeys()->get_value(key + "Verbose", verbose, fnd);
94 
95  string control_heap;
96  TheBESKeys::TheKeys()->get_value(key + "ControlHeap", control_heap, fnd);
97 
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.";
103  throw BESInternalFatalError(line, __FILE__, __LINE__);
104  }
105  else {
106  if (verbose == "no") BESLog::TheLog()->suspend();
107 
108  unsigned int emergency = atol(eps.c_str());
109 
110  if (control_heap == "yes") {
111  unsigned int max = atol(mhs.c_str());
112 
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";
117  ERROR_LOG(s);
118  throw BESInternalFatalError(s, __FILE__, __LINE__);
119  }
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)
125  + "\n";
126  if ( errno == EPERM) {
127  s = s + "Attempting to increase the soft/hard " + "limit above the current hard limit, "
128  + "must be superuser\n";
129  }
130  ERROR_LOG(s);
131  throw BESInternalFatalError(s, __FILE__, __LINE__);
132  }
133  log_limits("after setting limits: ");
134  _buffer = 0;
135  _buffer = malloc(megabytes(max));
136  if (!_buffer) {
137  string s = prolog + "Cannot get heap of size " + mhs + " to start running";
138  ERROR_LOG(s);
139  throw BESInternalFatalError(s, __FILE__, __LINE__);
140  }
141  free(_buffer);
142  }
143  else {
144  if (emergency > 10) {
145  string s = prolog + "Emergency pool is larger than 10 Megabytes";
146  throw BESInternalFatalError(s, __FILE__, __LINE__);
147  }
148  }
149 
150  _size = megabytes(emergency);
151  _buffer = 0;
152  _buffer = malloc(_size);
153  if (!_buffer) {
154  string s = prolog + "Cannot expand heap to " + eps + " to start running";
155  ERROR_LOG(s << endl);
156  throw BESInternalFatalError(s, __FILE__, __LINE__);
157  }
158  }
159  }
160  catch (BESError &ex) {
161  cerr << "BES: unable to start properly because " << ex.get_message() << endl;
162  exit(1);
163  }
164  catch (...) {
165  cerr << "BES: unable to start: undefined exception happened\n";
166  exit(1);
167  }
168  }
169 
170  BESLog::TheLog()->resume();
171 }
172 
173 BESMemoryGlobalArea::~BESMemoryGlobalArea()
174 {
175  if (--_counter == 0) {
176  if (_buffer) free(_buffer);
177  _buffer = 0;
178  }
179 }
180 
181 inline void BESMemoryGlobalArea::log_limits(const string &msg)
182 {
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());
187  _counter--;
188  throw BESInternalFatalError(moo.str(), __FILE__, __LINE__);
189  }
190  if (limit.rlim_cur == RLIM_INFINITY)
191  INFO_LOG(prolog << msg << "BES heap size soft limit is infinite" << endl);
192  else
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);
197  else
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);
200 }
201 
202 void BESMemoryGlobalArea::release_memory()
203 {
204  if (_buffer) {
205  free(_buffer);
206  _buffer = 0;
207  }
208 }
209 
210 bool BESMemoryGlobalArea::reclaim_memory()
211 {
212  if (!_buffer) _buffer = malloc(_size);
213  if (_buffer)
214  return true;
215  else
216  return false;
217 }
218 
226 void BESMemoryGlobalArea::dump(ostream &strm) const
227 {
228  strm << BESIndent::LMarg << "BESMemoryGlobalArea::dump - (" << (void *) this << ")" << endl;
229  BESIndent::Indent();
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();
236 }
237 
Base exception class for the BES with basic string message.
Definition: BESError.h:59
std::string get_message() const
get the error message for this exception
Definition: BESError.h:111
exception thrown if an internal error is found and is fatal to the BES
void resume()
Resumes logging after being suspended.
Definition: BESLog.h:173
void suspend()
Suspend logging of any information until resumed.
Definition: BESLog.h:163
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.
Definition: TheBESKeys.cc:340
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:71