STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
block_manager.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/mng/block_manager.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2007 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007, 2009 Johannes Singler <[email protected]>
8  * Copyright (C) 2008-2010 Andreas Beckmann <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_MNG_BLOCK_MANAGER_HEADER
16 #define STXXL_MNG_BLOCK_MANAGER_HEADER
17 
18 #include <stxxl/bits/config.h>
19 
20 #include <memory>
21 #include <iostream>
22 #include <iomanip>
23 #include <fstream>
24 #include <vector>
25 #include <map>
26 #include <algorithm>
27 #include <string>
28 #include <cstdlib>
29 
30 #if STXXL_MSVC
31 #include <memory.h>
32 #endif
33 
34 #include <stxxl/bits/defines.h>
35 #include <stxxl/bits/deprecated.h>
36 #include <stxxl/bits/io/request.h>
37 #include <stxxl/bits/io/file.h>
39 #include <stxxl/bits/noncopyable.h>
40 #include <stxxl/bits/singleton.h>
41 #include <stxxl/bits/mng/bid.h>
44 #include <stxxl/bits/mng/config.h>
47 
49 
50 #ifndef STXXL_MNG_COUNT_ALLOCATION
51 #define STXXL_MNG_COUNT_ALLOCATION 1
52 #endif // STXXL_MNG_COUNT_ALLOCATION
53 
54 //! \defgroup mnglayer Block Management Layer
55 //! Group of classes which help controlling external memory space,
56 //! managing disks, and allocating and deallocating blocks of external storage
57 //! \{
58 
59 //! Block manager class.
60 //!
61 //! Manages allocation and deallocation of blocks in multiple/single disk setting
62 //! \remarks is a singleton
63 class block_manager : public singleton<block_manager>
64 {
65  friend class singleton<block_manager>;
66 
69 
70  size_t ndisks;
71  block_manager();
72 
73 #if STXXL_MNG_COUNT_ALLOCATION
74  //! total requested allocation in bytes
76 
77  //! currently allocated bytes
79 
80  //! maximum number of bytes allocated during program run.
82 #endif // STXXL_MNG_COUNT_ALLOCATION
83 
84 protected:
85  template <class BIDType, class DiskAssignFunctor, class BIDIteratorClass>
86  void new_blocks_int(
87  const unsigned_type nblocks,
88  const DiskAssignFunctor& functor,
89  unsigned_type offset,
90  BIDIteratorClass out);
91 
92 public:
93  //! return total number of bytes available in all disks
94  uint64 get_total_bytes() const;
95 
96  //! Return total number of free disk allocations
97  uint64 get_free_bytes() const;
98 
99  //! Allocates new blocks.
100  //!
101  //! Allocates new blocks according to the strategy
102  //! given by \b functor and stores block identifiers
103  //! to the range [ \b bidbegin, \b bidend)
104  //! Allocation will be lined up with previous partial allocations
105  //! of \b offset blocks.
106  //! \param functor object of model of \b allocation_strategy concept
107  //! \param bidbegin bidirectional BID iterator object
108  //! \param bidend bidirectional BID iterator object
109  //! \param offset advance for \b functor to line up partial allocations
110  template <class DiskAssignFunctor, class BIDIteratorClass>
112  const DiskAssignFunctor& functor,
113  BIDIteratorClass bidbegin,
114  BIDIteratorClass bidend,
115  unsigned_type offset = 0)
116  {
117  typedef typename std::iterator_traits<BIDIteratorClass>::value_type bid_type;
118  new_blocks_int<bid_type>(std::distance(bidbegin, bidend), functor, offset, bidbegin);
119  }
120 
121  //! Allocates new blocks according to the strategy
122  //! given by \b functor and stores block identifiers
123  //! to the output iterator \b out
124  //! Allocation will be lined up with previous partial allocations
125  //! of \b offset blocks.
126  //! \param nblocks the number of blocks to allocate
127  //! \param functor object of model of \b allocation_strategy concept
128  //! \param out iterator object of OutputIterator concept
129  //! \param offset advance for \b functor to line up partial allocations
130  //!
131  //! The \c BlockType template parameter defines the type of block to allocate
132  template <class BlockType, class DiskAssignFunctor, class BIDIteratorClass>
134  const unsigned_type nblocks,
135  const DiskAssignFunctor& functor,
136  BIDIteratorClass out,
137  unsigned_type offset = 0)
138  {
139  typedef typename BlockType::bid_type bid_type;
140  new_blocks_int<bid_type>(nblocks, functor, offset, out);
141  }
142 
143  //! Allocates a new block according to the strategy
144  //! given by \b functor and stores the block identifier
145  //! to bid.
146  //! Allocation will be lined up with previous partial allocations
147  //! of \b offset blocks.
148  //! \param functor object of model of \b allocation_strategy concept
149  //! \param bid BID to store the block identifier
150  //! \param offset advance for \b functor to line up partial allocations
151  template <typename DiskAssignFunctor, unsigned BLK_SIZE>
152  void new_block(const DiskAssignFunctor& functor, BID<BLK_SIZE>& bid, unsigned_type offset = 0)
153  {
154  new_blocks_int<BID<BLK_SIZE> >(1, functor, offset, &bid);
155  }
156 
157  //! Deallocates blocks.
158  //!
159  //! Deallocates blocks in the range [ \b bidbegin, \b bidend)
160  //! \param bidbegin iterator object of \b bid_iterator concept
161  //! \param bidend iterator object of \b bid_iterator concept
162  template <class BIDIteratorClass>
163  void delete_blocks(const BIDIteratorClass& bidbegin, const BIDIteratorClass& bidend);
164 
165  //! Deallocates a block.
166  //! \param bid block identifier
167  template <unsigned BLK_SIZE>
168  void delete_block(const BID<BLK_SIZE>& bid);
169 
170  ~block_manager();
171 
172 #if STXXL_MNG_COUNT_ALLOCATION
173  //! return total requested allocation in bytes
175  { return m_total_allocation; }
176 
177  //! return currently allocated bytes
179  { return m_current_allocation; }
180 
181  //! return maximum number of bytes allocated during program run.
183  { return m_maximum_allocation; }
184 #endif // STXXL_MNG_COUNT_ALLOCATION
185 };
186 
187 
188 template <class BIDType, class DiskAssignFunctor, class OutputIterator>
189 void block_manager::new_blocks_int(
190  const unsigned_type nblocks,
191  const DiskAssignFunctor& functor,
192  unsigned_type offset,
193  OutputIterator out)
194 {
195  typedef BIDType bid_type;
196  typedef BIDArray<bid_type::t_size> bid_array_type;
197 
198  simple_vector<int_type> bl(ndisks);
199  simple_vector<bid_array_type> disk_bids(ndisks);
200  simple_vector<file*> disk_ptrs(nblocks);
201 
202  bl.memzero();
203 
204  for (unsigned_type i = 0; i < nblocks; ++i)
205  {
206  unsigned_type disk = functor(offset + i);
207  disk_ptrs[i] = disk_files[disk];
208  bl[disk]++;
209  }
210 
211  for (unsigned_type i = 0; i < ndisks; ++i)
212  {
213  if (bl[i])
214  {
215  disk_bids[i].resize(bl[i]);
216  disk_allocators[i]->new_blocks(disk_bids[i]);
217  }
218  }
219 
220  bl.memzero();
221 
222  OutputIterator it = out;
223  for (unsigned_type i = 0; i != nblocks; ++it, ++i)
224  {
225  const int disk = disk_ptrs[i]->get_allocator_id();
226  bid_type bid(disk_ptrs[i], disk_bids[disk][bl[disk]++].offset);
227  *it = bid;
228  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:new " << FMT_BID(bid));
229  }
230 
231 #if STXXL_MNG_COUNT_ALLOCATION
232  m_total_allocation += nblocks * BIDType::size;
233  m_current_allocation += nblocks * BIDType::size;
234  m_maximum_allocation = STXXL_MAX(m_maximum_allocation, m_current_allocation);
235 #endif // STXXL_MNG_COUNT_ALLOCATION
236 }
237 
238 
239 template <unsigned BLK_SIZE>
240 void block_manager::delete_block(const BID<BLK_SIZE>& bid)
241 {
242  // do not uncomment it
243  //assert(bid.storage->get_allocator_id() < config::get_instance()->disks_number());
244  if (!bid.is_managed())
245  return; // self managed disk
246  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:delete " << FMT_BID(bid));
247  assert(bid.storage->get_allocator_id() >= 0);
248  disk_allocators[bid.storage->get_allocator_id()]->delete_block(bid);
249  disk_files[bid.storage->get_allocator_id()]->discard(bid.offset, bid.size);
250 
251 #if STXXL_MNG_COUNT_ALLOCATION
252  m_current_allocation -= BLK_SIZE;
253 #endif // STXXL_MNG_COUNT_ALLOCATION
254 }
255 
256 
257 template <class BIDIteratorClass>
258 void block_manager::delete_blocks(
259  const BIDIteratorClass& bidbegin,
260  const BIDIteratorClass& bidend)
261 {
262  for (BIDIteratorClass it = bidbegin; it != bidend; it++)
263  {
264  delete_block(*it);
265  }
266 }
267 
268 // in bytes
269 #ifndef STXXL_DEFAULT_BLOCK_SIZE
270  #define STXXL_DEFAULT_BLOCK_SIZE(type) (2 * 1024 * 1024) // use traits
271 #endif
272 
273 //! \}
274 
276 
277 #endif // !STXXL_MNG_BLOCK_MANAGER_HEADER
278 // vim: et:ts=4:sw=4
uint64 get_maximum_allocation() const
return maximum number of bytes allocated during program run.
uint64 m_current_allocation
currently allocated bytes
Definition: block_manager.h:78
Block manager class.
Definition: block_manager.h:63
unsigned long long int uint64
Definition: types.h:41
uint64 get_current_allocation() const
return currently allocated bytes
void new_blocks(const unsigned_type nblocks, const DiskAssignFunctor &functor, BIDIteratorClass out, unsigned_type offset=0)
Allocates new blocks according to the strategy given by functor and stores block identifiers to the o...
#define STXXL_VERBOSE_BLOCK_LIFE_CYCLE
Definition: bid.h:28
Block size.
Definition: bid.h:46
uint64 m_total_allocation
total requested allocation in bytes
Definition: block_manager.h:75
bool is_managed() const
Definition: bid.h:79
disk_allocator ** disk_allocators
Definition: block_manager.h:67
Defines interface of file.
Definition: file.h:52
void memzero()
Zero the whole array content.
const Tp & STXXL_MAX(const Tp &a, const Tp &b)
Definition: utils.h:154
Block identifier class.
Definition: bid.h:42
file * storage
pointer to the file of the block
Definition: bid.h:50
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void new_block(const DiskAssignFunctor &functor, BID< BLK_SIZE > &bid, unsigned_type offset=0)
Allocates a new block according to the strategy given by functor and stores the block identifier to b...
void new_blocks(const DiskAssignFunctor &functor, BIDIteratorClass bidbegin, BIDIteratorClass bidend, unsigned_type offset=0)
Allocates new blocks.
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
uint64 m_maximum_allocation
maximum number of bytes allocated during program run.
Definition: block_manager.h:81
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:51
uint64 get_total_allocation() const
return total requested allocation in bytes
#define FMT_BID(_bid_)
Definition: bid.h:30
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
void resize(size_type newsize)
resize the array to contain exactly newsize items