rambrain
managedMemory.h
Go to the documentation of this file.
1 /* rambrain - a dynamical physical memory extender
2  * Copyright (C) 2015 M. Imgrund, A. Arth
3  * mimgrund (at) mpifr-bonn.mpg.de
4  * arth (at) usm.uni-muenchen.de
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef MANAGEDMEMORY_H
21 #define MANAGEDMEMORY_H
22 
23 #include <stdlib.h>
24 #include <map>
25 #include <pthread.h>
26 
27 #ifdef SWAPSTATS
28 #include <signal.h>
29 #ifdef LOGSTATS
30 #include <chrono>
31 #include "timer.h"
32 #endif
33 #endif
34 
35 #include "managedMemoryChunk.h"
36 #include "exceptions.h"
37 
38 
39 //Test classes
40 #ifdef BUILD_TESTS
41 class managedFileSwap_Unit_ManualSwapping_Test;
42 class managedFileSwap_Unit_ManualSwappingDelete_Test;
43 class managedFileSwap_Unit_ManualMultiSwapping_Test;
44 class managedFileSwap_Unit_CheckSwapStats_Test;
45 class cyclicManagedMemory_Integration_ArrayAccess_Test;
46 #endif
47 
48 namespace rambrain
49 {
50 class managedFileSwap;
51 class managedDummySwap;
52 class managedSwap;
53 template<class T, int dim>
54 class managedPtr;
69 {
70 public:
77  virtual ~managedMemory();
78 
83  void closeSwap();
84 
85  //Memory Management options
91  bool setMemoryLimit ( global_bytesize size );
93  inline global_bytesize getMemoryLimit () const {
94  return memory_max;
95  }
104 
105 
109  bool setOutOfSwapIsFatal ( bool fatal = true );
110 
111 
112  //Chunk Management
114  bool prepareUse ( rambrain::managedMemoryChunk &chunk, bool acquireLock = true );
116  bool setUse ( memoryID id );
118  bool unsetUse ( memoryID id );
124  bool setUse ( managedMemoryChunk &chunk, bool writeAccess );
129  bool unsetUse ( managedMemoryChunk &chunk , unsigned int no_unsets = 1 );
130 
131 
132 #ifdef PARENTAL_CONTROL
133  //Tree Management
135  unsigned int getNumberOfChildren ( const memoryID &id );
137  void printTree ( managedMemoryChunk *current = NULL, unsigned int nspaces = 0 );
138 
139  static const memoryID root;
140  static memoryID parent;
141  //We need the following two to correctly initialize class hierarchies:
142  static bool threadSynced;
143  static pthread_mutex_t parentalMutex;
144  static pthread_cond_t parentalCond;
145  static pthread_t creatingThread;
147  void recursiveMfree ( memoryID id );
148 #else
149  void linearMfree();
151 #endif
153  static const memoryID invalid;
158  static void signalSwappingCond();
159 protected:
163  bool mrealloc ( memoryID id, global_bytesize sizereq );
165  void mfree ( rambrain::memoryID id, bool inCleanup = false );
168 
169 
180  };
188  virtual swapErrorCode swapOut ( global_bytesize min_size ) = 0;
190  virtual bool swapIn ( memoryID id );
197  virtual bool swapIn ( managedMemoryChunk &chunk ) = 0;
199  virtual bool touch ( managedMemoryChunk &chunk ) = 0;
201  virtual void untouch ( managedMemoryChunk &chunk ) = 0;
203  virtual void schedulerRegister ( managedMemoryChunk &chunk ) = 0;
205  virtual void schedulerDelete ( managedMemoryChunk &chunk ) = 0;
206 
211  bool ensureEnoughSpace ( global_bytesize sizereq, managedMemoryChunk *orIsSwappedin = NULL );
212 
213  //Swap Storage manager iface:
214  managedSwap *swap = 0;
215 
220  bool outOfSwapIsFatal = true;
221 
222  std::map<memoryID, managedMemoryChunk *> memChunks;
223 
226 
227 
230  static bool Throw ( memoryException e );
231 
232  static pthread_mutex_t stateChangeMutex;
233  //Signalled after every swapin. Synchronization is happening via stateChangeMutex
234  static pthread_cond_t swappingCond;
235  /*static pthread_cond_t topologicalCond;*/
236 
237  template<class T, int dim>
238  friend class managedPtr;
239  friend class managedSwap;
240  friend class managedFileSwap;
241  friend class managedDummySwap;
242  //Test classes
243 #ifdef BUILD_TESTS
244  friend class ::managedFileSwap_Unit_ManualSwapping_Test;
245  friend class ::managedFileSwap_Unit_ManualMultiSwapping_Test;
246  friend class ::managedFileSwap_Unit_ManualSwappingDelete_Test;
247  friend class ::cyclicManagedMemory_Integration_ArrayAccess_Test;
248  friend class ::managedFileSwap_Unit_CheckSwapStats_Test;
250 #endif
251 
252 #ifdef SWAPSTATS
253 protected:
256 
257 
262 
265 
268 #endif
269 
273  bool waitForSwapin ( managedMemoryChunk &chunk, bool keepSwapLock = false );
278  bool waitForSwapout ( managedMemoryChunk &chunk, bool keepSwapLock = false );
284  void claimUsageof ( global_bytesize bytes, bool rambytes, bool used );
292  void claimTobefreed ( global_bytesize bytes, bool tobefreed );
295  void waitForAIO();
296 
297  size_t memoryAlignment = 1;
298 #ifdef LOGSTATS
299  static FILE *logFile;
301  static bool firstLog;
302 #endif
303 
304 public:
305 #ifdef SWAPSTATS
306  void printSwapstats() const;
309  void resetSwapstats();
311  double getHitsOverMisses();
312 
315  return swap_out_bytes;
316  };
319  return swap_in_bytes;
320  };
321 
325  static void sigswapstats ( int sig );
326 #endif
327  static void versionInfo();
329 };
330 
331 }
332 
333 #endif
334 
335 
virtual bool swapIn(memoryID id)
Convenience function for swapIn ( managedMemoryChunk &chunk )
Exception for errors with the memory.
Definition: exceptions.h:87
global_bytesize swap_misses
virtual swapErrorCode swapOut(global_bytesize min_size)=0
swaps out at least min_size bytes
bool setOutOfSwapIsFatal(bool fatal=true)
set policy what to do when out of memory in both ram and swap
global_bytesize getSwappedMemory() const
returns current swap usage
Swapping action was successful.
bool waitForSwapout(managedMemoryChunk &chunk, bool keepSwapLock=false)
Waits until a certain chunk is swapped out.
static void sigswapstats(int sig)
static binding that will print out some stats. Compile with cmake -DSWAPSTATS=on and send process SIG...
static void signalSwappingCond()
signals that a swapping action has completed and memory limits have changed
void recursiveMfree(memoryID id)
recursively deletes the objects in memory, first children, then parents.
bool setUse(memoryID id)
Convenience interface for setUse( managedMemoryChunk &chunk, bool writeAccess )
global_bytesize getTotalSwapMemory() const
return current swap capacity
static bool Throw(memoryException e)
Custom throw function, as we need to prevent throwing exceptions in construtors.
bool mrealloc(memoryID id, global_bytesize sizereq)
this function is a stub. In the future it should be capable of resizing an existing allocation ...
virtual void untouch(managedMemoryChunk &chunk)=0
marks chunk as recently not needed any more
global_bytesize getUsedMemory() const
returns current ram usage
uint64_t memoryID
static void versionInfo()
prints out a GIT version info and a diff on this version at compile time
static pthread_cond_t swappingCond
Class that serves as a backend to managedMemory to actual write/read managedMemoryChunks to/from hard...
Definition: managedSwap.h:35
bool prepareUse(rambrain::managedMemoryChunk &chunk, bool acquireLock=true)
Triggers swapin of chunk.
Main class to allocate memory that is managed by the rambrain memory defaultManager.
Definition: managedMemory.h:54
global_bytesize swap_out_scheduled_bytes
global_bytesize memory_tobefreed
global_bytesize n_swap_out
static managedMemory * defaultManager
static memoryID parent
global_bytesize swap_in_bytes_last
An implementation of managedSwap that is capable of kernel asynchronousIO.
void claimTobefreed(global_bytesize bytes, bool tobefreed)
account for future availability of bytes
uint64_t global_bytesize
Definition: common.h:65
void resetSwapstats()
reset statistic about the number, size and efficiency of swapping actions
virtual void schedulerRegister(managedMemoryChunk &chunk)=0
gives scheduler code the opportunity to register its own datastructures associated with a chunk ...
static const memoryID root
const global_bytesize gig
Definition: common.h:69
global_bytesize n_swap_in
global_bytesize swap_in_scheduled_bytes
void mfree(rambrain::memoryID id, bool inCleanup=false)
this function unregisters and deallocates a chunk
double getTotalSwappedInBytes()
simple Getter
global_bytesize swap_hits
managedMemory * previousManager
static pthread_mutex_t stateChangeMutex
global_bytesize getFreeSwapMemory() const
return current swap free capacity
friend managedSwap * configTestGetSwap(managedMemory *man)
Definition: configTest.cpp:31
void linearMfree()
linearly deletes all objects in memory
bool ensureEnoughSpace(global_bytesize sizereq, managedMemoryChunk *orIsSwappedin=NULL)
This function ensures that there is sizereq space left in ram.
manages all managed Chunks of raw memory
void closeSwap()
powers down the swap class, ergo a cleanup
managedMemoryChunk * mmalloc(global_bytesize sizereq)
allocates and registers a new raw memory chunk of size sizereq to be filled in by managedPtr ...
global_bytesize memory_used
virtual bool touch(managedMemoryChunk &chunk)=0
marks chunk as recently active as a hint for scheduling
void waitForAIO()
wait for some asynchronous action to occur
std::map< memoryID, managedMemoryChunk * > memChunks
The element/size requested does not fit in RAM as a whole.
void printSwapstats() const
print statistic about the number, size and efficiency of swapping actions
void claimUsageof(global_bytesize bytes, bool rambytes, bool used)
account for memory usage change
We lack reasonable candidates for swapout (too much elements in use?)
uint64_t memoryAtime
global_bytesize swap_in_bytes
bool waitForSwapin(managedMemoryChunk &chunk, bool keepSwapLock=false)
Waits until a certain chunk is present.
global_bytesize getMemoryLimit() const
returns current memory limit
Definition: managedMemory.h:93
virtual void schedulerDelete(managedMemoryChunk &chunk)=0
signals deletion of chunk to scheduler code
double getTotalSwappedOutBytes()
simple Getter
unsigned int getNumberOfChildren(const memoryID &id)
conveniently returns number of children of the memoryChunk with id id
global_bytesize memory_max
bool setMemoryLimit(global_bytesize size)
dynamically adjusts allowed ram usage
global_bytesize memory_swapped
static pthread_t creatingThread
static const memoryID invalid
swapErrorCode
Error codes for swapOut requests.
A dummy swap that just copies swapped out chunks to a different location in ram.
static pthread_cond_t parentalCond
static pthread_mutex_t parentalMutex
global_bytesize swap_out_bytes
managedMemoryChunk & resolveMemChunk(const memoryID &id)
returns a reference to the memoryChunk indexed by id id
double getHitsOverMisses()
returns current hits over misses rate for accessing elements.
void printTree(managedMemoryChunk *current=NULL, unsigned int nspaces=0)
prints the tree of managed objects for further inspection
global_bytesize swap_out_bytes_last
Backend class to handle raw memory and interaction/storage with managedSwap.
Definition: managedMemory.h:68
bool unsetUse(memoryID id)
Convenience interface for unsetUse ( managedMemoryChunk &chunk, bool writeAccess ) ...
managedMemory(managedSwap *swap, global_bytesize size=gig)
Standard constructor.