STXXL  1.4-dev
 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 //! \addtogroup mnglayer
55 //! \{
56 
57 //! Block manager class.
58 //!
59 //! Manages allocation and deallocation of blocks in multiple/single disk setting
60 //! \remarks is a singleton
61 class block_manager : public singleton<block_manager>
62 {
63  friend class singleton<block_manager>;
64 
67 
68  size_t ndisks;
69  block_manager();
70 
71 #if STXXL_MNG_COUNT_ALLOCATION
72  //! total requested allocation in bytes
74 
75  //! currently allocated bytes
77 
78  //! maximum number of bytes allocated during program run.
80 #endif // STXXL_MNG_COUNT_ALLOCATION
81 
82 protected:
83  template <class BIDType, class DiskAssignFunctor, class BIDIteratorClass>
84  void new_blocks_int(
85  const unsigned_type nblocks,
86  const DiskAssignFunctor& functor,
87  unsigned_type offset,
88  BIDIteratorClass out);
89 
90 public:
91  //! return total number of bytes available in all disks
92  uint64 get_total_bytes() const;
93 
94  //! Return total number of free disk allocations
95  uint64 get_free_bytes() const;
96 
97  //! Allocates new blocks.
98  //!
99  //! Allocates new blocks according to the strategy
100  //! given by \b functor and stores block identifiers
101  //! to the range [ \b bidbegin, \b bidend)
102  //! Allocation will be lined up with previous partial allocations
103  //! of \b offset blocks.
104  //! \param functor object of model of \b allocation_strategy concept
105  //! \param bidbegin bidirectional BID iterator object
106  //! \param bidend bidirectional BID iterator object
107  //! \param offset advance for \b functor to line up partial allocations
108  template <class DiskAssignFunctor, class BIDIteratorClass>
110  const DiskAssignFunctor& functor,
111  BIDIteratorClass bidbegin,
112  BIDIteratorClass bidend,
113  unsigned_type offset = 0)
114  {
115  typedef typename std::iterator_traits<BIDIteratorClass>::value_type bid_type;
116  new_blocks_int<bid_type>(std::distance(bidbegin, bidend), functor, offset, bidbegin);
117  }
118 
119  //! Allocates new blocks according to the strategy
120  //! given by \b functor and stores block identifiers
121  //! to the output iterator \b out
122  //! Allocation will be lined up with previous partial allocations
123  //! of \b offset blocks.
124  //! \param nblocks the number of blocks to allocate
125  //! \param functor object of model of \b allocation_strategy concept
126  //! \param out iterator object of OutputIterator concept
127  //! \param offset advance for \b functor to line up partial allocations
128  //!
129  //! The \c BlockType template parameter defines the type of block to allocate
130  template <class BlockType, class DiskAssignFunctor, class BIDIteratorClass>
132  const unsigned_type nblocks,
133  const DiskAssignFunctor& functor,
134  BIDIteratorClass out,
135  unsigned_type offset = 0)
136  {
137  typedef typename BlockType::bid_type bid_type;
138  new_blocks_int<bid_type>(nblocks, functor, offset, out);
139  }
140 
141  //! Allocates a new block according to the strategy
142  //! given by \b functor and stores the block identifier
143  //! to bid.
144  //! Allocation will be lined up with previous partial allocations
145  //! of \b offset blocks.
146  //! \param functor object of model of \b allocation_strategy concept
147  //! \param bid BID to store the block identifier
148  //! \param offset advance for \b functor to line up partial allocations
149  template <typename DiskAssignFunctor, unsigned BLK_SIZE>
150  void new_block(const DiskAssignFunctor& functor, BID<BLK_SIZE>& bid, unsigned_type offset = 0)
151  {
152  new_blocks_int<BID<BLK_SIZE> >(1, functor, offset, &bid);
153  }
154 
155  //! Deallocates blocks.
156  //!
157  //! Deallocates blocks in the range [ \b bidbegin, \b bidend)
158  //! \param bidbegin iterator object of \b bid_iterator concept
159  //! \param bidend iterator object of \b bid_iterator concept
160  template <class BIDIteratorClass>
161  void delete_blocks(const BIDIteratorClass& bidbegin, const BIDIteratorClass& bidend);
162 
163  //! Deallocates a block.
164  //! \param bid block identifier
165  template <unsigned BLK_SIZE>
166  void delete_block(const BID<BLK_SIZE>& bid);
167 
168  ~block_manager();
169 
170 #if STXXL_MNG_COUNT_ALLOCATION
171  //! return total requested allocation in bytes
173  { return m_total_allocation; }
174 
175  //! return currently allocated bytes
177  { return m_current_allocation; }
178 
179  //! return maximum number of bytes allocated during program run.
181  { return m_maximum_allocation; }
182 #endif // STXXL_MNG_COUNT_ALLOCATION
183 };
184 
185 template <class BIDType, class DiskAssignFunctor, class OutputIterator>
186 void block_manager::new_blocks_int(
187  const unsigned_type nblocks,
188  const DiskAssignFunctor& functor,
189  unsigned_type offset,
190  OutputIterator out)
191 {
192  typedef BIDType bid_type;
193  typedef BIDArray<bid_type::t_size> bid_array_type;
194 
195  simple_vector<int_type> bl(ndisks);
196  simple_vector<bid_array_type> disk_bids(ndisks);
197  simple_vector<file*> disk_ptrs(nblocks);
198 
199  // choose disks by calling DiskAssignFunctor
200 
201  bl.memzero();
202  for (unsigned_type i = 0; i < nblocks; ++i)
203  {
204  unsigned_type disk = functor(offset + i);
205  disk_ptrs[i] = disk_files[disk];
206  bl[disk]++;
207  }
208 
209  // allocate blocks on disks
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 template <unsigned BlockSize>
239 void block_manager::delete_block(const BID<BlockSize>& bid)
240 {
241  if (!bid.valid()) {
242  //STXXL_MSG("Warning: invalid block to be deleted.");
243  return;
244  }
245  if (!bid.is_managed())
246  return; // self managed disk
247  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:delete " << FMT_BID(bid));
248  assert(bid.storage->get_allocator_id() >= 0);
249  disk_allocators[bid.storage->get_allocator_id()]->delete_block(bid);
250  disk_files[bid.storage->get_allocator_id()]->discard(bid.offset, bid.size);
251 
252 #if STXXL_MNG_COUNT_ALLOCATION
253  m_current_allocation -= BlockSize;
254 #endif // STXXL_MNG_COUNT_ALLOCATION
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:76
Block manager class.
Definition: block_manager.h:61
unsigned long long int uint64
Definition: types.h:39
Block size.
Definition: bid.h:45
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
uint64 m_total_allocation
total requested allocation in bytes
Definition: block_manager.h:73
disk_allocator ** disk_allocators
Definition: block_manager.h:65
Defines interface of file.
Definition: file.h:56
void memzero()
Zero the whole array content.
bool is_managed() const
Definition: bid.h:79
Block identifier class.
Definition: bid.h:41
const Type & STXXL_MAX(const Type &a, const Type &b)
Definition: utils.h:153
#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...
bool valid() const
Definition: bid.h:55
void new_blocks(const DiskAssignFunctor &functor, BIDIteratorClass bidbegin, BIDIteratorClass bidend, unsigned_type offset=0)
Allocates new blocks.
virtual int get_allocator_id() const =0
Returns the file&#39;s disk allocator number.
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
file * storage
pointer to the file of the block
Definition: bid.h:49
uint64 m_maximum_allocation
maximum number of bytes allocated during program run.
Definition: block_manager.h:79
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:50
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