libfoedus-core
FOEDUS Core Library
page_pool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_MEMORY_PAGE_POOL_HPP_
19 #define FOEDUS_MEMORY_PAGE_POOL_HPP_
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 #include <string>
24 
25 #include "foedus/assert_nd.hpp"
26 #include "foedus/cxx11.hpp"
27 #include "foedus/error_stack.hpp"
28 #include "foedus/fwd.hpp"
29 #include "foedus/initializable.hpp"
31 #include "foedus/memory/fwd.hpp"
34 #include "foedus/storage/page.hpp"
37 
38 namespace foedus {
39 namespace memory {
48  public:
49  enum Constants {
54  kMaxSize = (1 << 12) - 1,
55  };
56  PagePoolOffsetChunk() : size_(0) {}
57 
58  uint32_t capacity() const { return kMaxSize; }
59  uint32_t size() const { return size_; }
60  bool empty() const { return size_ == 0; }
61  bool full() const { return size_ == kMaxSize; }
62  void clear() { size_ = 0; }
63 
65  ASSERT_ND(!empty());
66  return chunk_[--size_];
67  }
68  void push_back(PagePoolOffset pointer) {
69  ASSERT_ND(!full());
70  chunk_[size_++] = pointer;
71  }
72  void push_back(const PagePoolOffset* begin, const PagePoolOffset* end);
73  void move_to(PagePoolOffset* destination, uint32_t count);
74 
75  private:
76  uint32_t size_;
77  PagePoolOffset chunk_[kMaxSize];
78 };
79 
82  public:
84  : size_(size), padding_(0), chunk_(chunk) {}
85 
86  uint32_t capacity() const { return size_; }
87  uint32_t size() const { return size_; }
88  bool empty() const { return size_ == 0; }
89  bool full() const { return true; }
90 
91  void move_to(PagePoolOffset* destination, uint32_t count);
92 
93  uint32_t unused_dummy_func_padding() const { return padding_; }
94 
95  private:
96  uint32_t size_;
97  const uint32_t padding_;
98  PagePoolOffset* chunk_; // of arbitrary size
99 };
100 
109  public:
110  enum Constants {
121  kMaxSize = (1 << 16) - 1,
122  };
123  struct OffsetAndEpoch {
126  };
128 
129  uint32_t capacity() const { return kMaxSize; }
130  uint32_t size() const { return size_; }
131  bool empty() const { return size_ == 0; }
132  bool full() const { return size_ == kMaxSize; }
133  void clear() { size_ = 0; }
134  bool is_sorted() const; // only for assertion
135 
140  void push_back(PagePoolOffset offset, const Epoch& safe_epoch) {
141  ASSERT_ND(!full());
142  ASSERT_ND(empty() || Epoch(chunk_[size_ - 1U].safe_epoch_) <= safe_epoch);
143  chunk_[size_].offset_ = offset;
144  chunk_[size_].safe_epoch_ = safe_epoch.value();
145  ++size_;
146  }
148  void move_to(PagePoolOffset* destination, uint32_t count);
155  uint32_t get_safe_offset_count(const Epoch& threshold) const;
156 
157  uint32_t unused_dummy_func_dummy() const { return dummy_; }
158 
159  private:
160  uint32_t size_;
161  uint32_t dummy_;
162  OffsetAndEpoch chunk_[kMaxSize];
163 };
164 
173 class PagePool CXX11_FINAL : public virtual Initializable {
174  public:
175  struct Stat {
176  uint64_t total_pages_;
178  };
179 
180  PagePool();
181  ~PagePool();
182  void attach(
183  PagePoolControlBlock* control_block,
184  void* memory,
185  uint64_t memory_size,
186  bool owns,
187  bool rigorous_page_boundary_check);
188 
189  // Disable default constructors
191  PagePool& operator=(const PagePool&) CXX11_FUNC_DELETE;
192 
193  ErrorStack initialize() CXX11_OVERRIDE;
194  bool is_initialized() const CXX11_OVERRIDE;
195  ErrorStack uninitialize() CXX11_OVERRIDE;
196 
197  storage::Page* get_base() const;
199  uint64_t get_free_pool_capacity() const;
200  uint64_t get_memory_size() const;
212  uint32_t get_recommended_pages_per_grab() const;
213  Stat get_stat() const;
214  std::string get_debug_pool_name() const;
216  void set_debug_pool_name(const std::string& name);
217 
229  ErrorCode grab(uint32_t desired_grab_count, PagePoolOffsetChunk *chunk);
233  ErrorCode grab_one(PagePoolOffset *offset);
234 
245  void release(uint32_t desired_release_count, PagePoolOffsetChunk* chunk);
246  void release(uint32_t desired_release_count, PagePoolOffsetDynamicChunk* chunk);
247 
249  void release(uint32_t desired_release_count, PagePoolOffsetAndEpochChunk* chunk);
250 
254  void release_one(PagePoolOffset offset);
255 
260  const LocalPageResolver& get_resolver() const;
261 
262  friend std::ostream& operator<<(std::ostream& o, const PagePool& v);
263 
264  private:
265  PagePoolPimpl *pimpl_;
266 };
267 
277  public:
279  explicit PageReleaseBatch(Engine* engine);
280  ~PageReleaseBatch();
281 
282  // Disable default constructors
283  PageReleaseBatch() CXX11_FUNC_DELETE;
284  PageReleaseBatch(const PageReleaseBatch&) CXX11_FUNC_DELETE;
285  PageReleaseBatch& operator=(const PageReleaseBatch&) CXX11_FUNC_DELETE;
286 
292  void release(storage::VolatilePagePointer page_id) {
293  release(page_id.get_numa_node(), page_id.get_offset());
294  }
295 
301  void release(thread::ThreadGroupId numa_node, PagePoolOffset offset);
302 
306  void release_chunk(thread::ThreadGroupId numa_node);
307 
311  void release_all();
312 
313  private:
314  Engine* const engine_;
315  const uint16_t numa_node_count_;
317  ChunkPtr chunks_[256];
318 };
319 
333  public:
334  explicit RoundRobinPageGrabBatch(Engine* engine);
336 
337  // Disable default constructors
341 
353 
358  void release_all();
359 
360  private:
361  Engine* const engine_;
362  const uint16_t numa_node_count_;
363  thread::ThreadGroupId current_node_;
364  PagePoolOffsetChunk chunk_;
365 };
366 
376  public:
377  explicit DivvyupPageGrabBatch(Engine* engine);
379 
380  // Disable default constructors
384 
391 
401  storage::VolatilePagePointer grab_evenly(uint64_t cur, uint64_t total);
402 
407  void release_all();
408 
409  private:
410  Engine* const engine_;
411  const uint16_t node_count_;
413  PagePoolOffsetChunk* chunks_;
414 };
415 
416 
418 // Size of PagePoolOffsetChunk should be power of two (<=> x & (x-1) == 0)
420 STATIC_SIZE_CHECK(sizeof(PagePoolOffsetChunk) * 32U, sizeof(PagePoolOffsetAndEpochChunk))
421 
422 } // namespace memory
423 } // namespace foedus
424 #endif // FOEDUS_MEMORY_PAGE_POOL_HPP_
Definitions of IDs in this package and a few related constant values.
The pure-virtual interface to initialize/uninitialize non-trivial resources.
void move_to(PagePoolOffset *destination, uint32_t count)
Definition: page_pool.cpp:40
Page pool for volatile read/write store (VolatilePage) and the read-only bufferpool (SnapshotPage)...
Definition: page_pool.hpp:173
PagePoolOffsetDynamicChunk(uint32_t size, PagePoolOffset *chunk)
Definition: page_pool.hpp:83
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint32_t PagePoolOffset
Offset in PagePool that compactly represents the page address (unlike 8 bytes pointer).
Definition: memory_id.hpp:44
Typedefs of ID types used in thread package.
A helper class to grab a bunch of pages from multiple nodes in arbitrary fashion. ...
Definition: page_pool.hpp:375
uint32_t EpochInteger
Unsigned integer representation of epoch.
Definition: epoch.hpp:64
PagePoolOffsetChunk * ChunkPtr
Definition: page_pool.hpp:278
Represents a pointer to a volatile page with modification count for preventing ABA.
Definition: storage_id.hpp:194
Forward declarations of classes in root package.
STL namespace.
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
Represents a time epoch.
Definition: epoch.hpp:61
Definitions of IDs in this package and a few related constant values.
Max number of pointers to pack.
Definition: page_pool.hpp:54
void push_back(PagePoolOffset offset, const Epoch &safe_epoch)
Definition: page_pool.hpp:140
A helper class to return a bunch of pages to individual nodes.
Definition: page_pool.hpp:276
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
Pimpl object of PagePool.
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
Used to point to an already existing array.
Definition: page_pool.hpp:81
Forward declarations of classes in memory package.
#define CXX11_OVERRIDE
Used in public headers in place of "override" of C++11.
Definition: cxx11.hpp:134
Shared data in PagePoolPimpl.
void push_back(PagePoolOffset pointer)
Definition: page_pool.hpp:68
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
To reduce the overhead of grabbing/releasing pages from pool, we pack this many pointers for each gra...
Definition: page_pool.hpp:47
Resolves an offset in local (same NUMA node) page pool to a pointer and vice versa.
#define STATIC_SIZE_CHECK(desired, actual)
Used to store an epoch value with each entry in PagePoolOffsetChunk.
Definition: page_pool.hpp:108
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
uint8_t ThreadGroupId
Typedef for an ID of ThreadGroup (NUMA node).
Definition: thread_id.hpp:38
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
A helper class to grab a bunch of pages from multiple nodes in round-robin fashion per chunk...
Definition: page_pool.hpp:332
EpochInteger value() const
Returns the raw integer representation.
Definition: epoch.hpp:102