bes  Updated for version 3.20.10
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 #define prolog std::string("BESMemoryGlobalArea::").append(__func__).append("() - ")
55 
56 int BESMemoryGlobalArea::_counter = 0;
57 unsigned long BESMemoryGlobalArea::_size = 0;
58 void* BESMemoryGlobalArea::_buffer = 0;
59 
60 BESMemoryGlobalArea::BESMemoryGlobalArea()
61 {
62  limit.rlim_cur = 0;
63  limit.rlim_max = 0;
64 
65  if (_counter++ == 0) {
66  try {
67  bool fnd = false;
68  string key = "BES.Memory.GlobalArea.";
69 
70  string eps;
71  TheBESKeys::TheKeys()->get_value(key + "EmergencyPoolSize", eps, fnd);
72 
73  string mhs;
74  TheBESKeys::TheKeys()->get_value(key + "MaximumHeapSize", mhs, fnd);
75 
76  string verbose;
77  TheBESKeys::TheKeys()->get_value(key + "Verbose", verbose, fnd);
78 
79  string control_heap;
80  TheBESKeys::TheKeys()->get_value(key + "ControlHeap", control_heap, fnd);
81 
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.";
87  throw BESInternalFatalError(line, __FILE__, __LINE__);
88  }
89  else {
90  if (verbose == "no") BESLog::TheLog()->suspend();
91 
92  unsigned int emergency = atol(eps.c_str());
93 
94  if (control_heap == "yes") {
95  unsigned int max = atol(mhs.c_str());
96 
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";
101  ERROR_LOG(s);
102  throw BESInternalFatalError(s, __FILE__, __LINE__);
103  }
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)
109  + "\n";
110  if ( errno == EPERM) {
111  s = s + "Attempting to increase the soft/hard " + "limit above the current hard limit, "
112  + "must be superuser\n";
113  }
114  ERROR_LOG(s);
115  throw BESInternalFatalError(s, __FILE__, __LINE__);
116  }
117  log_limits("after setting limits: ");
118  _buffer = 0;
119  _buffer = malloc(megabytes(max));
120  if (!_buffer) {
121  string s = prolog + "Cannot get heap of size " + mhs + " to start running";
122  ERROR_LOG(s);
123  throw BESInternalFatalError(s, __FILE__, __LINE__);
124  }
125  free(_buffer);
126  }
127  else {
128  if (emergency > 10) {
129  string s = prolog + "Emergency pool is larger than 10 Megabytes";
130  throw BESInternalFatalError(s, __FILE__, __LINE__);
131  }
132  }
133 
134  _size = megabytes(emergency);
135  _buffer = 0;
136  _buffer = malloc(_size);
137  if (!_buffer) {
138  string s = prolog + "Cannot expand heap to " + eps + " to start running";
139  ERROR_LOG(s << endl);
140  throw BESInternalFatalError(s, __FILE__, __LINE__);
141  }
142  }
143  }
144  catch (BESError &ex) {
145  cerr << "BES: unable to start properly because " << ex.get_message() << endl;
146  exit(1);
147  }
148  catch (...) {
149  cerr << "BES: unable to start: undefined exception happened\n";
150  exit(1);
151  }
152  }
153 
154  BESLog::TheLog()->resume();
155 }
156 
157 BESMemoryGlobalArea::~BESMemoryGlobalArea()
158 {
159  if (--_counter == 0) {
160  if (_buffer) free(_buffer);
161  _buffer = 0;
162  }
163 }
164 
165 inline void BESMemoryGlobalArea::log_limits(const string &msg)
166 {
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());
171  _counter--;
172  throw BESInternalFatalError(moo.str(), __FILE__, __LINE__);
173  }
174  if (limit.rlim_cur == RLIM_INFINITY)
175  INFO_LOG(prolog << msg << "BES heap size soft limit is infinite" << endl);
176  else
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);
181  else
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);
184 }
185 
186 void BESMemoryGlobalArea::release_memory()
187 {
188  if (_buffer) {
189  free(_buffer);
190  _buffer = 0;
191  }
192 }
193 
194 bool BESMemoryGlobalArea::reclaim_memory()
195 {
196  if (!_buffer) _buffer = malloc(_size);
197  if (_buffer)
198  return true;
199  else
200  return false;
201 }
202 
210 void BESMemoryGlobalArea::dump(ostream &strm) const
211 {
212  strm << BESIndent::LMarg << "BESMemoryGlobalArea::dump - (" << (void *) this << ")" << endl;
213  BESIndent::Indent();
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();
220 }
221 
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58
virtual std::string get_message()
get the error message for this exception
Definition: BESError.h:99
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