Mantid
Loading...
Searching...
No Matches
DiskBuffer.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2011 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
10#include "MantidKernel/System.h"
11#ifndef Q_MOC_RUN
12#include <boost/multi_index/hashed_index.hpp>
13#include <boost/multi_index/mem_fun.hpp>
14#include <boost/multi_index/ordered_index.hpp>
15#include <boost/multi_index/sequenced_index.hpp>
16#include <boost/multi_index_container.hpp>
17#endif
18#include <cstdint>
19#include <limits>
20#include <list>
21#include <mutex>
22#include <string>
23#include <vector>
24
25namespace Mantid {
26namespace Kernel {
27
28// Forward declare
29class ISaveable;
30
42class MANTID_KERNEL_DLL DiskBuffer {
43public:
48 using freeSpace_t = boost::multi_index::multi_index_container<
49 FreeBlock, boost::multi_index::indexed_by<
50 boost::multi_index::ordered_non_unique<
51 ::boost::multi_index::const_mem_fun<FreeBlock, uint64_t, &FreeBlock::getFilePosition>>,
52 boost::multi_index::ordered_non_unique<
53 ::boost::multi_index::const_mem_fun<FreeBlock, uint64_t, &FreeBlock::getSize>>>>;
54
56 using freeSpace_bySize_t = freeSpace_t::nth_index<1>::type;
57
58 DiskBuffer();
59 DiskBuffer(uint64_t m_writeBufferSize);
60 DiskBuffer(const DiskBuffer &) = delete;
61 DiskBuffer &operator=(const DiskBuffer &) = delete;
62 virtual ~DiskBuffer() = default;
63
64 void toWrite(ISaveable *item);
65 void flushCache();
66 void objectDeleted(ISaveable *item);
67
68 // Free space map methods
69 void freeBlock(uint64_t const pos, uint64_t const size);
70 void defragFreeBlocks();
71
72 // Allocating
73 uint64_t allocate(uint64_t const newSize);
74 uint64_t relocate(uint64_t const oldPos, uint64_t const oldSize, const uint64_t newSize);
75
76 // For reporting and saving
77 void getFreeSpaceVector(std::vector<uint64_t> &free) const;
78 void setFreeSpaceVector(std::vector<uint64_t> &free);
79 std::string getMemoryStr() const;
80
81 //-------------------------------------------------------------------------------------------
85 void setWriteBufferSize(uint64_t buffer) {
86 if (buffer > std::numeric_limits<size_t>::max() / 2)
87 throw std::runtime_error(" Can not aloocate memory for that many events "
88 "on given architecture ");
89
90 m_writeBufferSize = static_cast<size_t>(buffer);
91 }
92
94 uint64_t getWriteBufferSize() const { return m_writeBufferSize; }
95
97 uint64_t getWriteBufferUsed() const { return m_writeBufferUsed; }
98
99 //-------------------------------------------------------------------------------------------
101 freeSpace_t &getFreeSpaceMap() { return m_free; }
102
103 //-------------------------------------------------------------------------------------------
106 uint64_t getFileLength() const { return m_fileLength; }
107
111 void setFileLength(const uint64_t length) const { m_fileLength = length; }
112
113 //-------------------------------------------------------------------------------------------
114
115protected:
116 inline void writeOldObjects();
117
118 // ----------------------- To-write buffer
119 // --------------------------------------
121 // bool m_useWriteBuffer;
122
125
131 std::list<ISaveable *> m_toWriteBuffer;
132
134 std::mutex m_mutex;
135
136 // ----------------------- Free space map
137 // --------------------------------------
140
143
145 std::mutex m_freeMutex;
146
147 // ----------------------- File object --------------------------------------
149 mutable uint64_t m_fileLength;
150
151private:
152};
153
154} // namespace Kernel
155} // namespace Mantid
Buffer objects that need to be written out to disk so as to optimize writing operations.
Definition: DiskBuffer.h:42
DiskBuffer(const DiskBuffer &)=delete
std::mutex m_freeMutex
Mutex for modifying the free space list.
Definition: DiskBuffer.h:145
uint64_t getWriteBufferSize() const
Definition: DiskBuffer.h:94
freeSpace_t & getFreeSpaceMap()
Definition: DiskBuffer.h:101
uint64_t getFileLength() const
Definition: DiskBuffer.h:106
std::list< ISaveable * > m_toWriteBuffer
A forward list for the buffer of "toWrite" objects.
Definition: DiskBuffer.h:131
uint64_t m_fileLength
Length of the file. This is where new blocks that don't fit get placed.
Definition: DiskBuffer.h:149
size_t m_writeBufferUsed
Total amount of memory in the "toWrite" buffer.
Definition: DiskBuffer.h:127
boost::multi_index::multi_index_container< FreeBlock, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< ::boost::multi_index::const_mem_fun< FreeBlock, uint64_t, &FreeBlock::getFilePosition > >, boost::multi_index::ordered_non_unique< ::boost::multi_index::const_mem_fun< FreeBlock, uint64_t, &FreeBlock::getSize > > > > freeSpace_t
A map for the list of free space blocks in the file.
Definition: DiskBuffer.h:53
size_t m_nObjectsToWrite
number of objects stored in to write buffer list
Definition: DiskBuffer.h:129
void setFileLength(const uint64_t length) const
Set the length of the file that this MRU writes to.
Definition: DiskBuffer.h:111
uint64_t getWriteBufferUsed() const
Definition: DiskBuffer.h:97
freeSpace_bySize_t & m_free_bySize
Index into m_free, but indexed by block size.
Definition: DiskBuffer.h:142
size_t m_writeBufferSize
Do we use the write buffer? Always now.
Definition: DiskBuffer.h:124
void setWriteBufferSize(uint64_t buffer)
Set the size of the to-write buffer, in number of events.
Definition: DiskBuffer.h:85
freeSpace_t::nth_index< 1 >::type freeSpace_bySize_t
A way to index the free space by their size.
Definition: DiskBuffer.h:56
std::mutex m_mutex
Mutex for modifying the the toWrite buffer.
Definition: DiskBuffer.h:134
DiskBuffer & operator=(const DiskBuffer &)=delete
virtual ~DiskBuffer()=default
freeSpace_t m_free
Map of the free blocks in the file.
Definition: DiskBuffer.h:139
FreeBlock: a simple class that holds the position and size of block of free space in a file.
Definition: FreeBlock.h:22
An interface for objects that can be cached or saved to disk.
Definition: ISaveable.h:28
Helper class which provides the Collimation Length for SANS instruments.