STXXL  1.4.1
 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  bl.memzero();
200 
201  for (unsigned_type i = 0; i < nblocks; ++i)
202  {
203  unsigned_type disk = functor(offset + i);
204  disk_ptrs[i] = disk_files[disk];
205  bl[disk]++;
206  }
207 
208  for (unsigned_type i = 0; i < ndisks; ++i)
209  {
210  if (bl[i])
211  {
212  disk_bids[i].resize(bl[i]);
213  disk_allocators[i]->new_blocks(disk_bids[i]);
214  }
215  }
216 
217  bl.memzero();
218 
219  OutputIterator it = out;
220  for (unsigned_type i = 0; i != nblocks; ++it, ++i)
221  {
222  const int disk = disk_ptrs[i]->get_allocator_id();
223  bid_type bid(disk_ptrs[i], disk_bids[disk][bl[disk]++].offset);
224  *it = bid;
225  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:new " << FMT_BID(bid));
226  }
227 
228 #if STXXL_MNG_COUNT_ALLOCATION
229  m_total_allocation += nblocks * BIDType::size;
230  m_current_allocation += nblocks * BIDType::size;
231  m_maximum_allocation = STXXL_MAX(m_maximum_allocation, m_current_allocation);
232 #endif // STXXL_MNG_COUNT_ALLOCATION
233 }
234 
235 template <unsigned BlockSize>
236 void block_manager::delete_block(const BID<BlockSize>& bid)
237 {
238  // do not uncomment it
239  //assert(bid.storage->get_allocator_id() < config::get_instance()->disks_number());
240  if (!bid.is_managed())
241  return; // self managed disk
242  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:delete " << FMT_BID(bid));
243  assert(bid.storage->get_allocator_id() >= 0);
244  disk_allocators[bid.storage->get_allocator_id()]->delete_block(bid);
245  disk_files[bid.storage->get_allocator_id()]->discard(bid.offset, bid.size);
246 
247 #if STXXL_MNG_COUNT_ALLOCATION
248  m_current_allocation -= BlockSize;
249 #endif // STXXL_MNG_COUNT_ALLOCATION
250 }
251 
252 template <class BIDIteratorClass>
253 void block_manager::delete_blocks(
254  const BIDIteratorClass& bidbegin,
255  const BIDIteratorClass& bidend)
256 {
257  for (BIDIteratorClass it = bidbegin; it != bidend; it++)
258  {
259  delete_block(*it);
260  }
261 }
262 
263 // in bytes
264 #ifndef STXXL_DEFAULT_BLOCK_SIZE
265  #define STXXL_DEFAULT_BLOCK_SIZE(type) (2 * 1024 * 1024) // use traits
266 #endif
267 
268 //! \}
269 
271 
272 #endif // !STXXL_MNG_BLOCK_MANAGER_HEADER
273 // 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...
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: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