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