libfoedus-core
FOEDUS Core Library
storage_id.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_STORAGE_STORAGE_ID_HPP_
19 #define FOEDUS_STORAGE_STORAGE_ID_HPP_
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 
24 #include "foedus/assert_nd.hpp"
30 
36 namespace foedus {
37 namespace storage {
38 
45 const uint16_t kPageSize = 1 << 12;
46 
55 typedef uint32_t StorageId;
56 
59 STATIC_SIZE_CHECK(sizeof(StorageName), 64)
60 
61 
65 typedef thread::ThreadGroupId PartitionId;
66 
79 typedef uint64_t SnapshotPagePointer;
80 
89 typedef uint64_t SnapshotLocalPageId;
90 
92  SnapshotPagePointer pointer) {
93  return pointer & 0x000000FFFFFFFFFFULL;
94 }
96  return static_cast<uint8_t>(pointer >> 40);
97 }
99  return static_cast<uint16_t>(pointer >> 48);
100 }
101 
103  ASSERT_ND(page_id < (1ULL << 40));
104 }
105 
107  uint16_t snapshot_id,
108  uint8_t node,
109  SnapshotLocalPageId local_page_id) {
111  return static_cast<uint64_t>(snapshot_id) << 48
112  | static_cast<uint64_t>(node) << 40
113  | local_page_id;
114 }
115 
116 void describe_snapshot_pointer(std::ostream* o, SnapshotPagePointer pointer);
117 
133 };
134 
139 inline const char* to_storage_type_name(StorageType type) {
140  switch (type) {
141  case kInvalidStorage: return "Invalid";
142  case kArrayStorage: return "Array";
143  case kHashStorage: return "Hash";
144  case kMasstreeStorage: return "Masstree";
145  case kSequentialStorage: return "Sequential";
146  default: return "Unknown";
147  }
148 }
149 
165 };
166 
174 typedef uint32_t Checksum;
175 
176 const uint8_t kVolatilePointerFlagSwappable = 0x02;
177 
195  uint64_t word;
196 
197  VolatilePagePointer() : word(0) {}
198  explicit VolatilePagePointer(uint64_t the_word) : word(the_word) {}
199  VolatilePagePointer(const VolatilePagePointer& other) : word(other.word) {}
200 
201  uint8_t get_numa_node() const { return static_cast<uint8_t>(word >> 32); }
202  memory::PagePoolOffset get_offset() const { return static_cast<uint32_t>(word & 0xFFFFFFFFULL); }
208  set(get_numa_node(), offset);
209  }
210  bool is_null() const { return get_offset() == 0; }
211  void clear() { word = 0; }
212  void set(uint8_t numa_node, memory::PagePoolOffset offset) {
213  word = (static_cast<uint64_t>(numa_node) << 32) | offset;
214  }
215  bool is_equivalent(const VolatilePagePointer& other) const {
216  // Now that we got rid of unused flags/mod_count, this is simply a word comparison.
217  return word == other.word;
218  }
219  bool operator==(const VolatilePagePointer& other) const {
220  return word == other.word;
221  }
222  bool operator!=(const VolatilePagePointer& other) const {
223  return word != other.word;
224  }
225 
226  friend std::ostream& operator<<(std::ostream& o, const VolatilePagePointer& v);
227 };
228 void describe_volatile_pointer(std::ostream* o, VolatilePagePointer pointer);
229 
231  VolatilePagePointer pointer;
232  pointer.word = word;
233  return pointer;
234 }
236  uint8_t numa_node,
237  memory::PagePoolOffset offset) {
239  ret.set(numa_node, offset);
240  return ret;
241 }
242 
274  }
275 
276  bool operator==(const DualPagePointer& other) const {
277  return snapshot_pointer_ == other.snapshot_pointer_
279  }
280  bool operator!=(const DualPagePointer& other) const {
281  return !operator==(other);
282  }
283 
284  friend std::ostream& operator<<(std::ostream& o, const DualPagePointer& v);
285 
288  const DualPagePointer &expected, const DualPagePointer &desired) {
290  reinterpret_cast<uint64_t*>(this),
291  reinterpret_cast<const uint64_t*>(&expected),
292  reinterpret_cast<const uint64_t*>(&desired));
293  }
296  const DualPagePointer &expected, const DualPagePointer &desired) {
298  reinterpret_cast<uint64_t*>(this),
299  reinterpret_cast<const uint64_t*>(&expected),
300  reinterpret_cast<const uint64_t*>(&desired));
301  }
302 
303  bool is_both_null() const {
305  }
306 
309 };
310 STATIC_SIZE_CHECK(sizeof(DualPagePointer), sizeof(uint64_t) * 2)
311 
312 } // namespace storage
313 } // namespace foedus
314 #endif // FOEDUS_STORAGE_STORAGE_ID_HPP_
bool atomic_compare_exchange_weak(const DualPagePointer &expected, const DualPagePointer &desired)
128-bit atomic CAS (weak version) for the dual pointer.
Definition: storage_id.hpp:295
thread::ThreadGroupId PartitionId
As partition=NUMA node, this is just a synonym of foedus::thread::ThreadGroupId.
Definition: storage_id.hpp:65
uint64_t SnapshotLocalPageId
Represents a local page ID in each one snapshot file in some NUMA node.
Definition: storage_id.hpp:89
void describe_volatile_pointer(std::ostream *o, VolatilePagePointer pointer)
Definition: storage_id.cpp:44
Represents a pointer to another page (usually a child page).
Definition: storage_id.hpp:271
The storage has been marked for drop and can't be used.
Definition: storage_id.hpp:164
SnapshotLocalPageId extract_local_page_id_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:91
uint32_t StorageId
Unique ID for storage.
Definition: storage_id.hpp:55
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
bool operator!=(const DualPagePointer &other) const
Definition: storage_id.hpp:280
Typedefs of ID types used in thread package.
SnapshotPagePointer to_snapshot_page_pointer(uint16_t snapshot_id, uint8_t node, SnapshotLocalPageId local_page_id)
Definition: storage_id.hpp:106
Represents a pointer to a volatile page with modification count for preventing ABA.
Definition: storage_id.hpp:194
friend std::ostream & operator<<(std::ostream &o, const VolatilePagePointer &v)
Definition: storage_id.cpp:40
The storage has been created and ready for use.
Definition: storage_id.hpp:158
Definitions of IDs in this package and a few related constant values.
bool is_equivalent(const VolatilePagePointer &other) const
Definition: storage_id.hpp:215
0 indicates invalid type.
Definition: storage_id.hpp:124
VolatilePagePointer(const VolatilePagePointer &other)
Definition: storage_id.hpp:199
VolatilePagePointer combine_volatile_page_pointer(uint8_t numa_node, memory::PagePoolOffset offset)
Definition: storage_id.hpp:235
void set_offset_unsafe(memory::PagePoolOffset offset)
This is used only in special places (snapshot composer).
Definition: storage_id.hpp:207
VolatilePagePointer volatile_pointer_
Definition: storage_id.hpp:308
memory::PagePoolOffset get_offset() const
Definition: storage_id.hpp:202
uint64_t SnapshotPagePointer
Page ID of a snapshot page.
Definition: storage_id.hpp:79
VolatilePagePointer(uint64_t the_word)
Definition: storage_id.hpp:198
assorted::FixedString< 60 > StorageName
Represents a unique name of a storage.
Definition: storage_id.hpp:58
SnapshotPagePointer snapshot_pointer_
Definition: storage_id.hpp:307
uint16_t extract_snapshot_id_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:98
const uint8_t kVolatilePointerFlagSwappable
Definition: storage_id.hpp:176
bool atomic_compare_exchange_strong(const DualPagePointer &expected, const DualPagePointer &desired)
128-bit atomic CAS (strong version) for the dual pointer.
Definition: storage_id.hpp:287
void set(uint8_t numa_node, memory::PagePoolOffset offset)
Definition: storage_id.hpp:212
const char * to_storage_type_name(StorageType type)
Gives a string representation of StorageType.
Definition: storage_id.hpp:139
uint8_t extract_numa_node_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:95
StorageType
Type of the storage, such as hash.
Definition: storage_id.hpp:122
void describe_snapshot_pointer(std::ostream *o, SnapshotPagePointer pointer)
Definition: storage_id.cpp:27
VolatilePagePointer construct_volatile_page_pointer(uint64_t word)
Definition: storage_id.hpp:230
Initial state, which means the storage has not been created yet.
Definition: storage_id.hpp:156
bool operator==(const DualPagePointer &other) const
Definition: storage_id.hpp:276
bool operator!=(const VolatilePagePointer &other) const
Definition: storage_id.hpp:222
#define STATIC_SIZE_CHECK(desired, actual)
bool operator==(const VolatilePagePointer &other) const
Definition: storage_id.hpp:219
#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
bool raw_atomic_compare_exchange_strong_uint128(uint64_t *ptr, const uint64_t *old_value, const uint64_t *new_value)
Atomic 128-bit CAS, which is not in the standard yet.
Definition: raw_atomics.cpp:25
Raw atomic operations that work for both C++11 and non-C++11 code.
uint8_t ThreadGroupId
Typedef for an ID of ThreadGroup (NUMA node).
Definition: thread_id.hpp:38
friend std::ostream & operator<<(std::ostream &o, const DualPagePointer &v)
Definition: storage_id.cpp:56
const uint16_t kPageSize
A constant defining the page size (in bytes) of both snapshot pages and volatile pages.
Definition: storage_id.hpp:45
uint32_t Checksum
Checksum of a snapshot page.
Definition: storage_id.hpp:174
bool raw_atomic_compare_exchange_weak_uint128(uint64_t *ptr, const uint64_t *old_value, const uint64_t *new_value)
Weak version of raw_atomic_compare_exchange_strong_uint128().
void assert_valid_snapshot_local_page_id(SnapshotLocalPageId page_id)
Definition: storage_id.hpp:102
StorageStatus
Status of a storage.
Definition: storage_id.hpp:154