Skip to content

Commit

Permalink
refs #6449 Enabled DiskBuffer test
Browse files Browse the repository at this point in the history
for everything that is outside of ISaveable test
  • Loading branch information
abuts committed Apr 16, 2013
1 parent a207b86 commit c03e563
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 294 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Kernel/inc/MantidKernel/DiskBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace Kernel
freeSpace_bySize_t & m_free_bySize;

/// Mutex for modifying the free space list
Kernel::RecursiveMutex m_freeMutex;
Kernel::Mutex m_freeMutex;

// ----------------------- File object --------------------------------------
/// Length of the file. This is where new blocks that don't fit get placed.
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Kernel/inc/MantidKernel/ISaveable.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace Kernel
/// Start point in the NXS file where the events are located
uint64_t m_fileIndexStart;
/// Number of events saved in the file, after the start index location
uint64_t m_fileNumEvents;
uint64_t m_fileNumEvents;
private:
// the iterator which describes the position of this object in the DiskBuffer. Undefined if not placed to buffer
boost::optional< std::list<ISaveable * const >::iterator> m_BufPosition;
Expand Down
110 changes: 110 additions & 0 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/Saveable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#ifndef MANTID_KERNEL_SAVEABLE_H_
#define MANTID_KERNEL_SAVEABLE_H_

#include "MantidKernel/ISaveable.h"
#include <vector>
#include <algorithm>

namespace Mantid
{
namespace Kernel
{

/** An interface for objects that can be cached or saved to disk.
This is implemented by MDBox and is used in the in-memory
cache of file-backed MDEventWorkspaces.
Copyright &copy; 2011 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport Saveable : public ISaveable
{
public:
Saveable();
Saveable(const size_t id);
Saveable(const Saveable & other);
virtual ~Saveable();


/// @return true if it the data of the object is busy and so cannot be cleared; false if the data was released and can be cleared/written.
bool isBusy() const
{
return m_Busy;
}
/// @ set the data busy to prevent from removing them from memory. The process which does that should clean the data when finished with them
void setBusy(bool On=true)
{
m_Busy=On;
}
/** Returns the state of the parameter, which tells disk buffer to force writing data
* to disk despite the size of the object have not changed (so one have probably done something with object contents. */
bool isDataChanged()const{return m_dataChanged;}

/** Call this method from the method which changes the object but keeps the object size the same to tell DiskBuffer to write it back
the dataChanged ID is reset after save from the DataBuffer is emptied */
void setDataChanged()
{
if(this->wasSaved())m_dataChanged=true;
}
/** this method has to be called if the object has been discarded from memory and is not changed any more.
It expected to be called from clearDataFromMemory. */
void resetDataChanges()
{
m_dataChanged=false;
}


/** Sets the location of the object on HDD */
void setFilePosition(uint64_t newPos,uint64_t newSize,bool wasSaved=true);

/** function returns true if the object have ever been saved on HDD and knows it place there*/
bool wasSaved()const
{ // for speed it returns this boolean, but for relaibility this should be m_wasSaved&&(m_fileIndexStart!=max())
return m_wasSaved;
}


bool isLoaded()const
{ return m_isLoaded;}

void setLoaded(bool Yes=true)
{ m_isLoaded = Yes;}
protected:
//--------------
/// a user needs to set this variable to true preventing from deleting data from buffer
bool m_Busy;
/** a user needs to set this variable to true to allow DiskBuffer saving the object to HDD
when it decides it suitable, if the size of iSavable object in cache is unchanged from the previous
save/load operation */
bool m_dataChanged;
/// this
bool m_wasSaved;
bool m_isLoaded;

/// the function saveAt has to be availible to DiskBuffer and nobody else. To highlight this we make it private
friend class DiskBuffer;

};


} // namespace Kernel
} // namespace Mantid

#endif /* MANTID_KERNEL_ISAVEABLE_H_ */
28 changes: 15 additions & 13 deletions Code/Mantid/Framework/Kernel/src/DiskBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace Kernel
m_nObjectsToWrite++;
m_mutex.unlock();
}


// Should we now write out the old data?
if (m_writeBufferUsed > m_writeBufferSize)
Expand Down Expand Up @@ -117,8 +117,8 @@ namespace Kernel
//std::cout << "DiskBuffer deleting ID " << item->getId() << "; new size " << m_writeBuffer.size() << std::endl;

// Mark the amount of space used on disk as free
//this->freeBlock(item->getFilePosition(), sizeOnFile);
this->freeBlock(item->getFilePosition(), item->getFileSize());
if(item->wasSaved())
this->freeBlock(item->getFilePosition(), item->getFileSize());
}


Expand Down Expand Up @@ -149,44 +149,46 @@ namespace Kernel
obj = *it;
if (!obj->isBusy())
{
uint64_t NumAllEvents = obj->getTotalDataSize();
uint64_t NumObjEvents = obj->getTotalDataSize();
uint64_t fileIndexStart;
if (!obj->wasSaved())
{
fileIndexStart=this->allocate(NumAllEvents);
fileIndexStart=this->allocate(NumObjEvents);
// Write to the disk; this will call the object specific save function;
// Prevent simultaneous file access (e.g. write while loading)
m_fileMutex.lock();
obj->saveAt(fileIndexStart,NumAllEvents);
obj->saveAt(fileIndexStart,NumObjEvents);
m_fileMutex.unlock();
}
else
{
uint64_t NumFileEvents= obj->getFileSize();
if (NumAllEvents != NumFileEvents)
if (NumObjEvents != NumFileEvents)
{
// Event list changed size. The MRU can tell us where it best fits now.
fileIndexStart= this->relocate(obj->getFilePosition(), NumFileEvents, NumAllEvents);
fileIndexStart= this->relocate(obj->getFilePosition(), NumFileEvents, NumObjEvents);
m_fileMutex.lock();
// Write to the disk; this will call the object specific save function;
obj->saveAt(fileIndexStart,NumAllEvents);
obj->saveAt(fileIndexStart,NumObjEvents);
m_fileMutex.unlock();
}
else // despite object size have not been changed, it can be modified other way. In this case, the method which changed the data should set dataChanged ID
{
if(obj->isDataChanged())
{
fileIndexStart = obj->getFilePosition();
m_fileMutex.lock();
m_fileMutex.lock();
// Write to the disk; this will call the object specific save function;
obj->saveAt(fileIndexStart,NumAllEvents);
m_fileMutex.unlock();
obj->saveAt(fileIndexStart,NumObjEvents);
m_fileMutex.unlock();
// this is questionable operation, which adjust file size in case when the file postions were allocated externaly
if(fileIndexStart+NumObjEvents>m_fileLength)m_fileLength=fileIndexStart+NumObjEvents;
}
else // just clean the object up -- it just occupies memory
obj->clearDataFromMemory();
}
}
// mark the object removed from the buffer
// tell the object that it has been removed from the buffer
obj->clearBufferState();
}
else // object busy
Expand Down
1 change: 0 additions & 1 deletion Code/Mantid/Framework/Kernel/src/ISaveable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ namespace Kernel
this->save();
this->clearDataFromMemory();
}

/// clears the state of the object, and indicate that it is not stored in buffer any more
void ISaveable::clearBufferState()
{
Expand Down
61 changes: 61 additions & 0 deletions Code/Mantid/Framework/Kernel/src/Saveable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "MantidKernel/Saveable.h"
#include "MantidKernel/System.h"
#include <limits>

namespace Mantid
{
namespace Kernel
{


//----------------------------------------------------------------------------------------------
/** Constructor
*/
Saveable::Saveable()
:ISaveable(),
m_Busy(false),m_dataChanged(false),m_wasSaved(false),m_isLoaded(false)
{
}

//----------------------------------------------------------------------------------------------
/** Copy constructor --> big qusetions about the validity of such implementation
*/
Saveable::Saveable(const Saveable & other)
:ISaveable(other),
m_Busy(other.m_Busy),m_dataChanged(other.m_dataChanged),m_wasSaved(other.m_wasSaved),m_isLoaded(false)
{
}

Saveable::Saveable(const size_t id)
:ISaveable(id),
m_Busy(false),m_dataChanged(false),m_wasSaved(false),m_isLoaded(false)
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
Saveable::~Saveable()
{
}



/** Set the start/end point in the file where the events are located
* @param newPos :: start point,
* @param newSize :: number of events in the file
* @param wasSaved :: flag to mark if the info was saved, by default it does
*/
void Saveable::setFilePosition(uint64_t newPos, uint64_t newSize, bool wasSaved)
{
m_fileIndexStart=newPos;
m_fileNumEvents =newSize;
m_wasSaved = wasSaved;
}




} // namespace Mantid
} // namespace Kernel

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ std::string ISaveableTester::fakeFile = "";
Kernel::Mutex ISaveableTester::streamMutex;

//====================================================================================
class DiskBufferTest : public CxxTest::TestSuite
class DiskBufferISaveableTest : public CxxTest::TestSuite
{
public:

Expand Down

0 comments on commit c03e563

Please sign in to comment.