bes  Updated for version 3.17.0
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  BESDEBUG( "bes", "Initializing emergency heap to "
88  << (long int)emergency << " MB" << endl );
89  BESDEBUG( "bes", "Initializing max heap size to "
90  << (long int)(max+1) << " MB" << endl );
91  (*BESLog::TheLog()) << "Initialize emergency heap size to " << (long int) emergency
92  << " and heap size to ";
93  (*BESLog::TheLog()) << (long int) (max + 1) << " megabytes" << endl;
94  if (emergency > max) {
95  string s = string("BES: ") + "unable to start since the emergency "
96  + "pool is larger than the maximum size of " + "the heap.\n";
97  (*BESLog::TheLog()) << s;
98  throw BESInternalFatalError(s, __FILE__, __LINE__);
99  }
100  log_limits("before setting limits: ");
101  limit.rlim_cur = megabytes(max + 1);
102  limit.rlim_max = megabytes(max + 1);
103  if (setrlimit( RLIMIT_DATA, &limit) < 0) {
104  string s = string("BES: ") + "Could not set limit for the heap " + "because " + strerror(errno)
105  + "\n";
106  if ( errno == EPERM) {
107  s = s + "Attempting to increase the soft/hard " + "limit above the current hard limit, "
108  + "must be superuser\n";
109  }
110  (*BESLog::TheLog()) << s;
111  throw BESInternalFatalError(s, __FILE__, __LINE__);
112  }
113  log_limits("after setting limits: ");
114  _buffer = 0;
115  _buffer = malloc(megabytes(max));
116  if (!_buffer) {
117  string s = string("BES: ") + "cannot get heap of size " + mhs + " to start running";
118  (*BESLog::TheLog()) << s;
119  throw BESInternalFatalError(s, __FILE__, __LINE__);
120  }
121  free(_buffer);
122  }
123  else {
124  if (emergency > 10) {
125  string s = "Emergency pool is larger than 10 Megabytes";
126  throw BESInternalFatalError(s, __FILE__, __LINE__);
127  }
128  }
129 
130  _size = megabytes(emergency);
131  _buffer = 0;
132  _buffer = malloc(_size);
133  if (!_buffer) {
134  string s = (string) "BES: cannot expand heap to " + eps + " to start running";
135  (*BESLog::TheLog()) << s << endl;
136  throw BESInternalFatalError(s, __FILE__, __LINE__);
137  }
138  }
139  }
140  catch (BESError &ex) {
141  cerr << "BES: unable to start properly because " << ex.get_message() << endl;
142  exit(1);
143  }
144  catch (...) {
145  cerr << "BES: unable to start: undefined exception happened\n";
146  exit(1);
147  }
148  }
149 
150  BESLog::TheLog()->resume();
151 }
152 
153 BESMemoryGlobalArea::~BESMemoryGlobalArea()
154 {
155  if (--_counter == 0) {
156  if (_buffer) free(_buffer);
157  _buffer = 0;
158  }
159 }
160 
161 inline void BESMemoryGlobalArea::log_limits(const string &msg)
162 {
163  if (getrlimit( RLIMIT_DATA, &limit) < 0) {
164  (*BESLog::TheLog()) << msg << "Could not get limits because " << strerror( errno) << endl;
165  _counter--;
166  throw BESInternalFatalError(strerror( errno), __FILE__, __LINE__);
167  }
168  if (limit.rlim_cur == RLIM_INFINITY)
169  (*BESLog::TheLog()) << msg << "heap size soft limit is infinte" << endl;
170  else
171  (*BESLog::TheLog()) << msg << "heap size soft limit is " << (long int) limit.rlim_cur << " bytes ("
172  << (long int) (limit.rlim_cur) / (MEGABYTE) << " MB - may be rounded up)" << endl;
173  if (limit.rlim_max == RLIM_INFINITY)
174  (*BESLog::TheLog()) << msg << "heap size hard limit is infinite" << endl;
175  else
176  (*BESLog::TheLog()) << msg << "heap size hard limit is " << (long int) limit.rlim_max << " bytes ("
177  << (long int) (limit.rlim_max) / (MEGABYTE) << " MB - may be rounded up)" << endl;
178 }
179 
180 void BESMemoryGlobalArea::release_memory()
181 {
182  if (_buffer) {
183  free(_buffer);
184  _buffer = 0;
185  }
186 }
187 
188 bool BESMemoryGlobalArea::reclaim_memory()
189 {
190  if (!_buffer) _buffer = malloc(_size);
191  if (_buffer)
192  return true;
193  else
194  return false;
195 }
196 
204 void BESMemoryGlobalArea::dump(ostream &strm) const
205 {
206  strm << BESIndent::LMarg << "BESMemoryGlobalArea::dump - (" << (void *) this << ")" << endl;
207  BESIndent::Indent();
208  strm << BESIndent::LMarg << "area set? " << _counter << endl;
209  strm << BESIndent::LMarg << "emergency buffer: " << (void *) _buffer << endl;
210  strm << BESIndent::LMarg << "buffer size: " << _size << endl;
211  strm << BESIndent::LMarg << "rlimit current: " << limit.rlim_cur << endl;
212  strm << BESIndent::LMarg << "rlimit max: " << limit.rlim_max << endl;
213  BESIndent::UnIndent();
214 }
215 
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:120
virtual void dump(ostream &strm) const
dumps information about this object
Abstract exception class for the BES with basic string message.
Definition: BESError.h:56
void suspend()
Suspend logging of any information until resumed.
Definition: BESLog.h:110
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:481
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:43