bes  Updated for version 3.19.1
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 <iostream>
34 #include <cstdlib>
35 #include <cstring>
36 #include <cerrno>
37 
38 using std::cerr;
39 using std::endl;
40 
41 #include "BESMemoryGlobalArea.h"
42 #include "BESInternalFatalError.h"
43 #include "BESDebug.h"
44 #include "BESLog.h"
45 #include "TheBESKeys.h"
46 
47 int BESMemoryGlobalArea::_counter = 0;
48 unsigned long BESMemoryGlobalArea::_size = 0;
49 void* BESMemoryGlobalArea::_buffer = 0;
50 
51 BESMemoryGlobalArea::BESMemoryGlobalArea()
52 {
53  limit.rlim_cur = 0;
54  limit.rlim_max = 0;
55 
56  if (_counter++ == 0) {
57  try {
58  bool fnd = false;
59  string key = "BES.Memory.GlobalArea.";
60 
61  string eps;
62  TheBESKeys::TheKeys()->get_value(key + "EmergencyPoolSize", eps, fnd);
63 
64  string mhs;
65  TheBESKeys::TheKeys()->get_value(key + "MaximumHeapSize", mhs, fnd);
66 
67  string verbose;
68  TheBESKeys::TheKeys()->get_value(key + "Verbose", verbose, fnd);
69 
70  string control_heap;
71  TheBESKeys::TheKeys()->get_value(key + "ControlHeap", control_heap, fnd);
72 
73  if ((eps == "") || (mhs == "") || (verbose == "") || (control_heap == "")) {
74  string line = "cannot determine memory keys.";
75  line += (string) "Please set values for" + " BES.Memory.GlobalArea.EmergencyPoolSize,"
76  + " BES.Memory.GlobalArea.MaxiumumHeapSize," + " BES.Memory.GlobalArea.Verbose, and"
77  + " BES.Memory.GlobalArea.ControlHeap" + " in the BES configuration file.";
78  throw BESInternalFatalError(line, __FILE__, __LINE__);
79  }
80  else {
81  if (verbose == "no") BESLog::TheLog()->suspend();
82 
83  unsigned int emergency = atol(eps.c_str());
84 
85  if (control_heap == "yes") {
86  unsigned int max = atol(mhs.c_str());
87 
88  LOG("Initialize emergency heap size to " << (unsigned long)emergency << " and heap size to " << (unsigned long)(max + 1) << " MB" << endl);
89  if (emergency > max) {
90  string s = string("BES: ") + "unable to start since the emergency "
91  + "pool is larger than the maximum size of " + "the heap.\n";
92  LOG(s);
93  throw BESInternalFatalError(s, __FILE__, __LINE__);
94  }
95  log_limits("before setting limits: ");
96  limit.rlim_cur = megabytes(max + 1);
97  limit.rlim_max = megabytes(max + 1);
98  if (setrlimit( RLIMIT_DATA, &limit) < 0) {
99  string s = string("BES: ") + "Could not set limit for the heap " + "because " + strerror(errno)
100  + "\n";
101  if ( errno == EPERM) {
102  s = s + "Attempting to increase the soft/hard " + "limit above the current hard limit, "
103  + "must be superuser\n";
104  }
105  LOG(s);
106  throw BESInternalFatalError(s, __FILE__, __LINE__);
107  }
108  log_limits("after setting limits: ");
109  _buffer = 0;
110  _buffer = malloc(megabytes(max));
111  if (!_buffer) {
112  string s = string("BES: ") + "cannot get heap of size " + mhs + " to start running";
113  LOG(s);
114  throw BESInternalFatalError(s, __FILE__, __LINE__);
115  }
116  free(_buffer);
117  }
118  else {
119  if (emergency > 10) {
120  string s = "Emergency pool is larger than 10 Megabytes";
121  throw BESInternalFatalError(s, __FILE__, __LINE__);
122  }
123  }
124 
125  _size = megabytes(emergency);
126  _buffer = 0;
127  _buffer = malloc(_size);
128  if (!_buffer) {
129  string s = (string) "BES: cannot expand heap to " + eps + " to start running";
130  LOG(s << endl);
131  throw BESInternalFatalError(s, __FILE__, __LINE__);
132  }
133  }
134  }
135  catch (BESError &ex) {
136  cerr << "BES: unable to start properly because " << ex.get_message() << endl;
137  exit(1);
138  }
139  catch (...) {
140  cerr << "BES: unable to start: undefined exception happened\n";
141  exit(1);
142  }
143  }
144 
145  BESLog::TheLog()->resume();
146 }
147 
148 BESMemoryGlobalArea::~BESMemoryGlobalArea()
149 {
150  if (--_counter == 0) {
151  if (_buffer) free(_buffer);
152  _buffer = 0;
153  }
154 }
155 
156 inline void BESMemoryGlobalArea::log_limits(const string &msg)
157 {
158  if (getrlimit( RLIMIT_DATA, &limit) < 0) {
159  LOG(msg << "Could not get limits because " << strerror( errno) << endl);
160  _counter--;
161  throw BESInternalFatalError(strerror( errno), __FILE__, __LINE__);
162  }
163  if (limit.rlim_cur == RLIM_INFINITY)
164  LOG(msg << "BES heap size soft limit is infinite" << endl);
165  else
166  LOG(msg << "BES heap size soft limit is " << (long int) limit.rlim_cur << " bytes ("
167  << (long int) (limit.rlim_cur) / (MEGABYTE) << " MB - may be rounded up)" << endl);
168  if (limit.rlim_max == RLIM_INFINITY)
169  LOG(msg << "BES heap size hard limit is infinite" << endl);
170  else
171  LOG("BES heap size hard limit is " << (long int) limit.rlim_max << " bytes ("
172  << (long int) (limit.rlim_max) / (MEGABYTE) << " MB - may be rounded up)" << endl);
173 }
174 
175 void BESMemoryGlobalArea::release_memory()
176 {
177  if (_buffer) {
178  free(_buffer);
179  _buffer = 0;
180  }
181 }
182 
183 bool BESMemoryGlobalArea::reclaim_memory()
184 {
185  if (!_buffer) _buffer = malloc(_size);
186  if (_buffer)
187  return true;
188  else
189  return false;
190 }
191 
199 void BESMemoryGlobalArea::dump(ostream &strm) const
200 {
201  strm << BESIndent::LMarg << "BESMemoryGlobalArea::dump - (" << (void *) this << ")" << endl;
202  BESIndent::Indent();
203  strm << BESIndent::LMarg << "area set? " << _counter << endl;
204  strm << BESIndent::LMarg << "emergency buffer: " << (void *) _buffer << endl;
205  strm << BESIndent::LMarg << "buffer size: " << _size << endl;
206  strm << BESIndent::LMarg << "rlimit current: " << limit.rlim_cur << endl;
207  strm << BESIndent::LMarg << "rlimit max: " << limit.rlim_max << endl;
208  BESIndent::UnIndent();
209 }
210 
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:97
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 string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:422
Abstract exception class for the BES with basic string message.
Definition: BESError.h:56
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
void suspend()
Suspend logging of any information until resumed.
Definition: BESLog.h:134