rambrain
rambrain::managedFileSwap Class Reference

An implementation of managedSwap that is capable of kernel asynchronousIO. More...

#include <managedFileSwap.h>

Inheritance diagram for rambrain::managedFileSwap:
rambrain::managedSwap

Public Member Functions

 managedFileSwap (global_bytesize size, const char *filemask, global_bytesize oneFile=0, bool enableDMA=false)
 
virtual ~managedFileSwap ()
 
virtual void swapDelete (managedMemoryChunk *chunk)
 Mark chunk as deleted. More...
 
virtual global_bytesize swapIn (managedMemoryChunk **chunklist, unsigned int nchunks)
 Trigger swap in of the chunks pointed to by chunklist. More...
 
virtual global_bytesize swapIn (managedMemoryChunk *chunk)
 Trigger swap in of the chunk pointed to by chunk. More...
 
virtual global_bytesize swapOut (managedMemoryChunk **chunklist, unsigned int nchunks)
 Trigger swap out of the chunks pointed to by chunklist. More...
 
virtual global_bytesize swapOut (managedMemoryChunk *chunk)
 Trigger swap out of the chunk pointed to by chunk. More...
 
virtual bool extendSwap (global_bytesize size)
 extend swap by size number of bytes More...
 
virtual bool extendSwapByPolicy (global_bytesize min_size)
 extend swap by policy More...
 
void setDMA (bool arg1)
 
virtual void close ()
 Close the swap if not already closed. More...
 
- Public Member Functions inherited from rambrain::managedSwap
 managedSwap (global_bytesize size)
 
virtual ~managedSwap ()
 
virtual global_bytesize getSwapSize () const
 Simple getter. More...
 
virtual global_bytesize getUsedSwap () const
 Simple getter. More...
 
virtual global_bytesize getFreeSwap () const
 Simple getter. More...
 
virtual swapPolicy getSwapPolicy () const
 
virtual swapPolicy setSwapPolicy (swapPolicy newPolicy)
 
size_t getMemoryAlignment () const
 Returns possible memory alignment restrictions. More...
 
void claimUsageof (global_bytesize bytes, bool rambytes, bool used)
 account for memory usage change More...
 
void waitForCleanExit ()
 Function waits for all asynchronous IO to complete. The wait is implemented non-performant as a normal user does not have to wait for this. Implementing this with a _cond just destroys performance in the respective swapIn/out procedures without increasing any user space functionality. More...
 

Public Attributes

const unsigned int pageSize
 

Protected Member Functions

void asyncIoArrived (rambrain::pageFileLocation *ref, struct io_event *aio)
 deals with a single asynchronous IO event completion More...
 
void completeTransactionOn (rambrain::pageFileLocation *ref, bool lock=true)
 called to finish a transaction when all pending aio on a managedMemoryChunk has completed More...
 
virtual bool checkForAIO ()
 gives this class the chance to treat incoming aio events More...
 
void my_io_submit (struct iocb *aio)
 
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 low swap memory More...
 
virtual void invalidateCacheFor (managedMemoryChunk &chunk)
 tells managedFileSwap that the chunk under consideration might have been changed by user and needs to be copied out freshly More...
 
global_bytesize getFreeDiskSpace ()
 returns free disk space at file system location specified by filemask More...
 

Static Protected Member Functions

static void sigStat (int signum)
 returns some statistics. Typically, we will be sensitive to SIGUSR2 if compiled with -DSWAPSTATS=on More...
 
static void * io_submit_worker (void *ptr)
 
static void * io_arrrive_worker (void *ptr)
 

Protected Attributes

bool deleteFilesOnExit = true
 
struct iocb aio_template
 
io_context_t aio_context = 0
 
unsigned int aio_max_transactions = 10240
 
struct io_event * aio_eventarr
 
pthread_mutex_t aioWaiterLock = PTHREAD_MUTEX_INITIALIZER
 
std::unordered_map< struct iocb *, pageFileLocation * > pendingAios
 
pthread_mutex_t io_submit_lock = PTHREAD_MUTEX_INITIALIZER
 
pthread_cond_t io_submit_cond = PTHREAD_COND_INITIALIZER
 
unsigned int io_submit_num_threads = 1
 
pthread_t * io_submit_threads
 
pthread_t io_waiter_thread
 
pthread_t io_arrive_thread
 
std::queue< struct iocb * > io_submit_requests
 
bool io_arrive_work = true
 
- Protected Attributes inherited from rambrain::managedSwap
global_bytesize swapSize
 
global_bytesize swapUsed
 
global_bytesize swapFree
 
unsigned int totalSwapActionsQueued = 0
 
size_t memoryAlignment = 1
 
swapPolicy policy = swapPolicy::fixed
 
bool closed = false
 

Static Protected Attributes

static managedFileSwapinstance = NULL
 

Private Member Functions

pageFileLocation determinePFLoc (global_offset g_offset, global_bytesize length) const
 generate a pageFileLocation object given a global offset and a length of the data. This maps our "virtual" adress space to physical locations in a certain file More...
 
global_offset determineGlobalOffset (const pageFileLocation &ref) const
 maps from physical location to "virtual" adress More...
 
bool openSwapFiles ()
 opens swap files according to settings More...
 
bool openSwapFileRange (unsigned int start, unsigned int stop)
 opens certain range of swap files according to settings More...
 
void closeSwapFiles ()
 closes swap files More...
 
void scheduleCopy (rambrain::pageFileLocation &ref, void *ramBuf, int *tracker, bool reverse=false)
 Schedules an elementary pageFileLocation chunk for copying (in or out) More...
 
void copyMem (rambrain::pageFileLocation &ref, void *ramBuf, bool reverse=false)
 Schedules copying on level of whole managedMemoryChunks and calls scheduleCopy on the assigned parts. More...
 
void copyMem (void *ramBuf, rambrain::pageFileLocation &ref)
 Convenience function for reverse copying. More...
 
void scheduleCopy (void *ramBuf, pageFileLocation &ref, int *parttracker)
 Convenience function for reverse scheduling a copy. More...
 
size_t getMemoryAlignment () const
 If we have any restrictions regarding memory alignment of RAM buffers(DMA), this function tells us about it. More...
 
pageFileLocationpfmalloc (rambrain::global_bytesize size, rambrain::managedMemoryChunk *chunk)
 Tries to find space in the swapFiles to write out an object of size size and returns first pageFileLocation to it. More...
 
void pffree (pageFileLocation *pagePtr)
 
pageFileLocationallocInFree (pageFileLocation *freeChunk, global_bytesize size)
 Helper function for pfmalloc. More...
 

Private Attributes

const char * filemask
 
unsigned int pageFileNumber
 
global_bytesize pageFileSize
 
float swapFileResizeFrac = .1
 
struct swapFileDescswapFiles = NULL
 
std::map< global_offset, pageFileLocation * > free_space
 
std::map< global_offset, pageFileLocation * > all_space
 
bool enableDMA = false
 

Friends

class ::managedFileSwap_Unit_SwapAllocation_Test
 
class ::managedFileSwap_Integration_RandomAccess_Test
 
class ::managedFileSwap_Integration_RandomAccessVariousSize_Test
 
class ::managedFileSwap_Unit_SwapPolicy_Test
 

Detailed Description

An implementation of managedSwap that is capable of kernel asynchronousIO.

Note
we also support DMA, however this is not recommended as kernel caching&buffering will be circumvent. For our use case this turns out to slow down things more and we do not make best use of system resources.
all public functions of managedFileSwap need to be called holding stateChangeMutex

Definition at line 107 of file managedFileSwap.h.

Constructor & Destructor Documentation

rambrain::managedFileSwap::managedFileSwap ( global_bytesize  size,
const char *  filemask,
global_bytesize  oneFile = 0,
bool  enableDMA = false 
)

Definition at line 44 of file managedFileSwap.cpp.

rambrain::managedFileSwap::~managedFileSwap ( )
virtual

Definition at line 113 of file managedFileSwap.cpp.

Member Function Documentation

pageFileLocation * rambrain::managedFileSwap::allocInFree ( pageFileLocation freeChunk,
global_bytesize  size 
)
private

Helper function for pfmalloc.

Definition at line 399 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::asyncIoArrived ( rambrain::pageFileLocation ref,
struct io_event *  aio 
)
protected

deals with a single asynchronous IO event completion

Definition at line 828 of file managedFileSwap.cpp.

bool rambrain::managedFileSwap::checkForAIO ( )
protectedvirtual

gives this class the chance to treat incoming aio events

Returns
true if there is pending IO to wait for, false otherwise
Warning
if false is returned, immediate waiting for pending aio results in deadlock

Reimplemented from rambrain::managedSwap.

Definition at line 767 of file managedFileSwap.cpp.

bool rambrain::managedFileSwap::cleanupCachedElements ( rambrain::global_bytesize  minimum_size = 0)
protectedvirtual

throws out cached elements still in ram but also resident on disk. This makes space in situations of low swap memory

Reimplemented from rambrain::managedSwap.

Definition at line 924 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::close ( )
virtual

Close the swap if not already closed.

Note
Not automatically done by destructor
Warning
Swap can not be used anymore afterwards

Implements rambrain::managedSwap.

Definition at line 143 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::closeSwapFiles ( )
private

closes swap files

Definition at line 119 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::completeTransactionOn ( rambrain::pageFileLocation ref,
bool  lock = true 
)
protected

called to finish a transaction when all pending aio on a managedMemoryChunk has completed

Definition at line 708 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::copyMem ( rambrain::pageFileLocation ref,
void *  ramBuf,
bool  reverse = false 
)
private

Schedules copying on level of whole managedMemoryChunks and calls scheduleCopy on the assigned parts.

Parameters
refthe pageFileLocation the chunks data should be copied to
ramBufpointer to the managedMemoryChunk data buffer
reversecopy in the other direction

Definition at line 865 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::copyMem ( void *  ramBuf,
rambrain::pageFileLocation ref 
)
inlineprivate

Convenience function for reverse copying.

Definition at line 161 of file managedFileSwap.h.

global_offset rambrain::managedFileSwap::determineGlobalOffset ( const pageFileLocation ref) const
inlineprivate

maps from physical location to "virtual" adress

Definition at line 604 of file managedFileSwap.cpp.

pageFileLocation rambrain::managedFileSwap::determinePFLoc ( global_offset  g_offset,
global_bytesize  length 
) const
private

generate a pageFileLocation object given a global offset and a length of the data. This maps our "virtual" adress space to physical locations in a certain file

Definition at line 608 of file managedFileSwap.cpp.

bool rambrain::managedFileSwap::extendSwap ( global_bytesize  size)
virtual

extend swap by size number of bytes

Returns
success

Reimplemented from rambrain::managedSwap.

Definition at line 262 of file managedFileSwap.cpp.

bool rambrain::managedFileSwap::extendSwapByPolicy ( global_bytesize  min_size)
virtual

extend swap by policy

Returns
success
Parameters
min_sizeMinimum size the swap has to be extended before success is returned

Reimplemented from rambrain::managedSwap.

Definition at line 207 of file managedFileSwap.cpp.

global_bytesize rambrain::managedFileSwap::getFreeDiskSpace ( )
protected

returns free disk space at file system location specified by filemask

Definition at line 289 of file managedFileSwap.cpp.

size_t rambrain::managedFileSwap::getMemoryAlignment ( ) const
inlineprivate

If we have any restrictions regarding memory alignment of RAM buffers(DMA), this function tells us about it.

Definition at line 170 of file managedFileSwap.h.

void rambrain::managedFileSwap::invalidateCacheFor ( managedMemoryChunk chunk)
protectedvirtual

tells managedFileSwap that the chunk under consideration might have been changed by user and needs to be copied out freshly

Reimplemented from rambrain::managedSwap.

Definition at line 947 of file managedFileSwap.cpp.

void * rambrain::managedFileSwap::io_arrrive_worker ( void *  ptr)
staticprotected

Definition at line 682 of file managedFileSwap.cpp.

void * rambrain::managedFileSwap::io_submit_worker ( void *  ptr)
staticprotected

Definition at line 655 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::my_io_submit ( struct iocb *  aio)
protected

Definition at line 699 of file managedFileSwap.cpp.

bool rambrain::managedFileSwap::openSwapFileRange ( unsigned int  start,
unsigned int  stop 
)
private

opens certain range of swap files according to settings

Definition at line 171 of file managedFileSwap.cpp.

bool rambrain::managedFileSwap::openSwapFiles ( )
private

opens swap files according to settings

Definition at line 192 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::pffree ( pageFileLocation pagePtr)
private

Definition at line 434 of file managedFileSwap.cpp.

pageFileLocation * rambrain::managedFileSwap::pfmalloc ( rambrain::global_bytesize  size,
rambrain::managedMemoryChunk chunk 
)
private

Tries to find space in the swapFiles to write out an object of size size and returns first pageFileLocation to it.

Priority: -Use first free chunk that fits completely -Distribute over free locations -look for read-in memory that can be overwritten -delete cached files and look again

Definition at line 306 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::scheduleCopy ( rambrain::pageFileLocation ref,
void *  ramBuf,
int *  tracker,
bool  reverse = false 
)
private

Schedules an elementary pageFileLocation chunk for copying (in or out)

Definition at line 617 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::scheduleCopy ( void *  ramBuf,
pageFileLocation ref,
int *  parttracker 
)
inlineprivate

Convenience function for reverse scheduling a copy.

Definition at line 165 of file managedFileSwap.h.

void rambrain::managedFileSwap::setDMA ( bool  arg1)

Definition at line 137 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::sigStat ( int  signum)
staticprotected

returns some statistics. Typically, we will be sensitive to SIGUSR2 if compiled with -DSWAPSTATS=on

Definition at line 890 of file managedFileSwap.cpp.

void rambrain::managedFileSwap::swapDelete ( managedMemoryChunk chunk)
virtual

Mark chunk as deleted.

Note
this is only called when deleting a chunk in managedMemory if chunk->swapBuf is not NULL
this function must be called having stateChangeMutex acquired.

Implements rambrain::managedSwap.

Definition at line 504 of file managedFileSwap.cpp.

global_bytesize rambrain::managedFileSwap::swapIn ( managedMemoryChunk **  chunklist,
unsigned int  nchunks 
)
virtual

Trigger swap in of the chunks pointed to by chunklist.

Note
this function must be called having stateChangeMutex acquired.

Implements rambrain::managedSwap.

Definition at line 539 of file managedFileSwap.cpp.

global_bytesize rambrain::managedFileSwap::swapIn ( managedMemoryChunk chunk)
virtual

Trigger swap in of the chunk pointed to by chunk.

Note
this function must be called having stateChangeMutex acquired.

Implements rambrain::managedSwap.

Definition at line 516 of file managedFileSwap.cpp.

global_bytesize rambrain::managedFileSwap::swapOut ( managedMemoryChunk **  chunklist,
unsigned int  nchunks 
)
virtual

Trigger swap out of the chunks pointed to by chunklist.

Note
this function must be called having stateChangeMutex acquired.

Implements rambrain::managedSwap.

Definition at line 594 of file managedFileSwap.cpp.

global_bytesize rambrain::managedFileSwap::swapOut ( managedMemoryChunk chunk)
virtual

Trigger swap out of the chunk pointed to by chunk.

Note
this function must be called having stateChangeMutex acquired.

Implements rambrain::managedSwap.

Definition at line 548 of file managedFileSwap.cpp.

Friends And Related Function Documentation

friend class ::managedFileSwap_Integration_RandomAccess_Test
friend

Definition at line 241 of file managedFileSwap.h.

friend class ::managedFileSwap_Integration_RandomAccessVariousSize_Test
friend

Definition at line 242 of file managedFileSwap.h.

friend class ::managedFileSwap_Unit_SwapAllocation_Test
friend

Definition at line 240 of file managedFileSwap.h.

friend class ::managedFileSwap_Unit_SwapPolicy_Test
friend

Definition at line 243 of file managedFileSwap.h.

Member Data Documentation

io_context_t rambrain::managedFileSwap::aio_context = 0
protected

Definition at line 203 of file managedFileSwap.h.

struct io_event* rambrain::managedFileSwap::aio_eventarr
protected

Definition at line 205 of file managedFileSwap.h.

unsigned int rambrain::managedFileSwap::aio_max_transactions = 10240
protected

Definition at line 204 of file managedFileSwap.h.

struct iocb rambrain::managedFileSwap::aio_template
protected

Definition at line 202 of file managedFileSwap.h.

pthread_mutex_t rambrain::managedFileSwap::aioWaiterLock = PTHREAD_MUTEX_INITIALIZER
protected

Definition at line 206 of file managedFileSwap.h.

std::map<global_offset, pageFileLocation *> rambrain::managedFileSwap::all_space
private

Definition at line 183 of file managedFileSwap.h.

bool rambrain::managedFileSwap::deleteFilesOnExit = true
protected

Definition at line 188 of file managedFileSwap.h.

bool rambrain::managedFileSwap::enableDMA = false
private

Definition at line 186 of file managedFileSwap.h.

const char* rambrain::managedFileSwap::filemask
private

Definition at line 141 of file managedFileSwap.h.

std::map<global_offset, pageFileLocation *> rambrain::managedFileSwap::free_space
private

Definition at line 182 of file managedFileSwap.h.

managedFileSwap * rambrain::managedFileSwap::instance = NULL
staticprotected

Definition at line 210 of file managedFileSwap.h.

pthread_t rambrain::managedFileSwap::io_arrive_thread
protected

Definition at line 220 of file managedFileSwap.h.

bool rambrain::managedFileSwap::io_arrive_work = true
protected

Definition at line 228 of file managedFileSwap.h.

pthread_cond_t rambrain::managedFileSwap::io_submit_cond = PTHREAD_COND_INITIALIZER
protected

Definition at line 216 of file managedFileSwap.h.

pthread_mutex_t rambrain::managedFileSwap::io_submit_lock = PTHREAD_MUTEX_INITIALIZER
protected

Definition at line 215 of file managedFileSwap.h.

unsigned int rambrain::managedFileSwap::io_submit_num_threads = 1
protected

Definition at line 217 of file managedFileSwap.h.

std::queue<struct iocb *> rambrain::managedFileSwap::io_submit_requests
protected

Definition at line 222 of file managedFileSwap.h.

pthread_t* rambrain::managedFileSwap::io_submit_threads
protected

Definition at line 218 of file managedFileSwap.h.

pthread_t rambrain::managedFileSwap::io_waiter_thread
protected

Definition at line 219 of file managedFileSwap.h.

unsigned int rambrain::managedFileSwap::pageFileNumber
private

Definition at line 143 of file managedFileSwap.h.

global_bytesize rambrain::managedFileSwap::pageFileSize
private

Definition at line 144 of file managedFileSwap.h.

const unsigned int rambrain::managedFileSwap::pageSize

Definition at line 126 of file managedFileSwap.h.

std::unordered_map<struct iocb *, pageFileLocation *> rambrain::managedFileSwap::pendingAios
protected

Definition at line 208 of file managedFileSwap.h.

float rambrain::managedFileSwap::swapFileResizeFrac = .1
private

Definition at line 147 of file managedFileSwap.h.

struct swapFileDesc* rambrain::managedFileSwap::swapFiles = NULL
private

Definition at line 149 of file managedFileSwap.h.


The documentation for this class was generated from the following files: