24 #include <sys/signal.h>
30 #include <mm_malloc.h>
34 namespace rambrainglobals
49 #ifdef PARENTAL_CONTROL
71 #ifdef PARENTAL_CONTROL
97 signal ( SIGUSR1, SIG_IGN );
110 #ifdef PARENTAL_CONTROL
187 bool cacheCleaned =
false;
198 if ( cacheCleaned ) {
203 Throw (
memoryException (
"Could not swap memory: Object size requested is bigger than actual RAM limits" ) );
244 #ifdef PARENTAL_CONTROL
250 chunk->
size = sizereq;
252 #ifdef PARENTAL_CONTROL
255 if ( chunk->
id ==
root ) {
264 #ifdef PARENTAL_CONTROL
279 if ( sizereq != 0 ) {
315 if ( !
swapIn ( chunk ) ) {
316 errmsgf (
"Could not swap in chunk %lu", chunk.
id );
350 errmsgf (
"Waited for swapin of chunk %lu and could not make it.", chunk.
id );
384 if ( !
setUse ( chunk ) ) {
388 void *realloced = realloc ( chunk.
locPtr, sizereq );
391 chunk.
size = sizereq;
422 if ( no_unsets == 0 ) {
427 if ( chunk.
useCnt < no_unsets ) {
432 chunk.
useCnt -= no_unsets;
446 #ifdef PARENTAL_CONTROL
472 #ifdef PARENTAL_CONTROL
478 if ( chunk->
id !=
root ) {
483 _mm_free ( chunk->
locPtr );
490 if ( pchunk->
child == chunk->
id ) {
495 if ( pchunk->
next == chunk->
id ) {
512 _mm_free ( chunk->
locPtr );
525 #ifdef PARENTAL_CONTROL
553 for (
unsigned int n = 0; n < nspaces; n++ ) {
556 printf (
"(%d : size %lu Bytes, %s, ", current->
id, current->
size, current->
preemptiveLoaded ?
"P" :
"" );
557 switch ( current->
status ) {
559 printf (
"Root Element" );
562 printf (
"Swapping in" );
565 printf (
"Swapping out" );
568 printf (
"Swapped out" );
571 printf (
"Allocated" );
574 printf (
"Allocated&inUse" );
577 printf (
"Allocated&inUse (writable)" );
580 printf (
"Allocated&inUse (readonly)" );
628 mfree ( old ,
true );
641 infomsgf (
"A total of %lu swapouts occured, writing out %lu bytes (%.3e Bytes/avg)\
642 \n\tA total of %lu swapins occured, reading in %lu bytes (%.3e Bytes/avg)\
643 \n\twe used already loaded elements %lu times, %lu had to be fetched\
644 \n\tthus, the hits over misses rate was %.5f\
645 \n\tfraction of swapped out ram (currently) %.2e\n\t %lu scheduled out bytes and %lu scheduled in bytes saved by caching.",
n_swap_out,
swap_out_bytes, \
656 #define SAFESWAP(func) (defaultManager->swap != NULL ? defaultManager->swap->func : 0lu)
669 fprintf ( managedMemory::logFile,
"#Time [ms]\tPrep for swap out [B] \tSwapped out [B]\tSwapped out last [B]\tPrep for swap in [B] \tSwapped in [B]\tSwapped in last [B]\tHits / Miss\tMemory Used [B]\t\
670 Memory Used\tSwap Used [B]\tSwap Used\n" );
673 int64_t now = std::chrono::duration_cast<std::chrono::milliseconds> ( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
674 fprintf (
logFile,
"%ld\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%e\t%lu\t%e\t%lu\t%e\n",
686 (
double ) usedSwap / totalSwap );
689 printf (
"%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%e\t%lu\t%e\t%lu\t%e\n",
700 (
double ) usedSwap / totalSwap );
720 const char *swapstats =
"with swapstats";
722 const char *swapstats =
"without swapstats";
725 const char *logstats =
"with logstats";
727 const char *logstats =
"Without logstats";
729 #ifdef PARENTAL_CONTROL
730 const char *parentalcontrol =
"with parental control";
732 const char *parentalcontrol =
"without parental_control";
735 infomsgf (
"compiled from %s\n\ton %s at %s\n\
737 \n \t git diff\n%s\n",
gitCommit, __DATE__, __TIME__, swapstats, logstats, parentalcontrol,
gitDiff );
760 if ( !keepSwapLock ) {
768 if ( !keepSwapLock ) {
782 if ( !keepSwapLock ) {
791 if ( !keepSwapLock ) {
816 memory_tobefreed += tobefreed ? bytes : -bytes;
virtual bool swapIn(memoryID id)
Convenience function for swapIn ( managedMemoryChunk &chunk )
virtual bool checkForAIO()
Exception for errors with the memory.
global_bytesize swap_misses
virtual swapErrorCode swapOut(global_bytesize min_size)=0
swaps out at least min_size bytes
static bool isRunning()
Simple getter.
bool setOutOfSwapIsFatal(bool fatal=true)
set policy what to do when out of memory in both ram and swap
An exception class for when code is used which is not fully finished developping. ...
global_bytesize getSwappedMemory() const
returns current swap usage
Swapping action was successful.
memoryID id
an ID to identify the object in scheduler or elsewhere
bool waitForSwapout(managedMemoryChunk &chunk, bool keepSwapLock=false)
Waits until a certain chunk is swapped out.
static void stopTimer()
Stop the timer.
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.
Exception class for cases of an incomplete setup.
void * locPtr
pointer to the actual data in RAM
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 ...
size_t getMemoryAlignment() const
Returns possible memory alignment restrictions.
virtual void untouch(managedMemoryChunk &chunk)=0
marks chunk as recently not needed any more
global_bytesize getUsedMemory() const
returns current ram usage
static void versionInfo()
prints out a GIT version info and a diff on this version at compile time
void * swapBuf
a place to store additional swapping information
static pthread_cond_t swappingCond
Class that serves as a backend to managedMemory to actual write/read managedMemoryChunks to/from hard...
bool prepareUse(rambrain::managedMemoryChunk &chunk, bool acquireLock=true)
Triggers swapin of chunk.
global_bytesize swap_out_scheduled_bytes
void * schedBuf
a place to store additional scheduling information
virtual global_bytesize getSwapSize() const
Simple getter.
global_bytesize memory_tobefreed
global_bytesize n_swap_out
static managedMemory * defaultManager
global_bytesize swap_in_bytes_last
memoryID child
first child element if creating a class hierarchy
void claimTobefreed(global_bytesize bytes, bool tobefreed)
account for future availability of bytes
virtual void close()=0
Close the swap if not already closed.
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
memoryID next
next element
virtual global_bytesize getFreeSwap() const
Simple getter.
const unsigned char gitCommit[]
const unsigned char gitDiff[]
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
global_bytesize swap_hits
Main class for handling configuration throughout the library and for the user.
managedMemory * previousManager
static pthread_mutex_t stateChangeMutex
global_bytesize getFreeSwapMemory() const
return current swap free capacity
void linearMfree()
linearly deletes all objects in memory
rambrainConfig config
You will find the object in managedMemory.cpp as we have to define it in some 'used' file in the link...
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
void waitForCleanExit()
Function waits for all asynchronous IO to complete. The wait is implemented non-performant as a norma...
std::map< memoryID, managedMemoryChunk * > memChunks
virtual bool cleanupCachedElements(rambrain::global_bytesize minimum_size=0)
throws out cached elements still in ram but also resident on disk. This makes space in situations of ...
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?)
global_bytesize swap_in_bytes
static void startTimer(long seconds, long nanoseconds)
Start the timer.
memoryID parent
parent element if created in class hierarchy
bool waitForSwapin(managedMemoryChunk &chunk, bool keepSwapLock=false)
Waits until a certain chunk is present.
virtual void schedulerDelete(managedMemoryChunk &chunk)=0
signals deletion of chunk to scheduler code
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
virtual void invalidateCacheFor(managedMemoryChunk &chunk)
tells managedFileSwap that the chunk under consideration might have been changed by user and needs to...
global_bytesize memory_swapped
global_bytesize size
Size of actual object in bytes.
static pthread_t creatingThread
static const memoryID invalid
unsigned short useCnt
Number of using adhereTos or a possible location for locking the object to changes.
swapErrorCode
Error codes for swapOut requests.
virtual void swapDelete(managedMemoryChunk *chunk)=0
Mark chunk as deleted.
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.
bool unsetUse(memoryID id)
Convenience interface for unsetUse ( managedMemoryChunk &chunk, bool writeAccess ) ...
managedMemory(managedSwap *swap, global_bytesize size=gig)
Standard constructor.
static pthread_mutex_t mutex