libfoedus-core
FOEDUS Core Library
shared_memory_repo.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_SOC_SHARED_MEMORY_REPO_HPP_
19 #define FOEDUS_SOC_SHARED_MEMORY_REPO_HPP_
20 
21 #include <stdint.h>
22 
23 #include <cstring>
24 #include <string>
25 
26 #include "foedus/assert_nd.hpp"
27 #include "foedus/cxx11.hpp"
28 #include "foedus/engine.hpp"
30 #include "foedus/error_stack.hpp"
31 #include "foedus/module_type.hpp"
34 #include "foedus/log/fwd.hpp"
35 #include "foedus/memory/fwd.hpp"
37 #include "foedus/proc/fwd.hpp"
38 #include "foedus/proc/proc_id.hpp"
39 #include "foedus/restart/fwd.hpp"
40 #include "foedus/savepoint/fwd.hpp"
41 #include "foedus/snapshot/fwd.hpp"
42 #include "foedus/soc/fwd.hpp"
43 #include "foedus/soc/soc_id.hpp"
44 #include "foedus/storage/fwd.hpp"
46 #include "foedus/thread/fwd.hpp"
48 #include "foedus/xct/fwd.hpp"
49 #include "foedus/xct/xct_id.hpp"
50 
51 namespace foedus {
52 namespace soc {
53 
65  enum StatusCode {
69  kInitial = 0,
80 
83 
93  };
94 
95  // only for reinterpret_cast
98  MasterEngineStatus(const MasterEngineStatus &other) CXX11_FUNC_DELETE;
99  MasterEngineStatus& operator=(const MasterEngineStatus &other) CXX11_FUNC_DELETE;
100 
102  void change_status_atomic(StatusCode new_status) {
103  ASSERT_ND(new_status == kFatalError || new_status == kWaitingForChildTerminate ||
104  static_cast<int>(new_status) == static_cast<int>(status_code_) + 1);
106  status_code_ = new_status;
108  }
112  return status_code_;
113  }
114 
116  ASSERT_ND(static_cast<int>(initialized_modules_) == static_cast<int>(value) - 1);
118  initialized_modules_ = value;
120  }
122  ASSERT_ND(static_cast<int>(uninitialized_modules_) == static_cast<int>(value) + 1);
124  uninitialized_modules_ = value;
126  }
127 
133  // so far this is the only information.
134 };
135 
147  enum StatusCode {
151  kInitial = 0,
162 
165 
170  };
171 
172  // only for reinterpret_cast
175  ChildEngineStatus(const ChildEngineStatus &other) CXX11_FUNC_DELETE;
176  ChildEngineStatus& operator=(const ChildEngineStatus &other) CXX11_FUNC_DELETE;
177 
179  void change_status_atomic(StatusCode new_status) {
180  ASSERT_ND(new_status == kFatalError ||
181  static_cast<int>(new_status) == static_cast<int>(status_code_) + 1);
183  status_code_ = new_status;
185  }
189  return status_code_;
190  }
191 
193  ASSERT_ND(static_cast<int>(initialized_modules_) == static_cast<int>(value) - 1);
195  initialized_modules_ = value;
197  }
199  ASSERT_ND(static_cast<int>(uninitialized_modules_) == static_cast<int>(value) + 1);
201  uninitialized_modules_ = value;
203  }
204 
210  // so far this is the only information.
211 };
212 
219  enum Constants {
222  kMetaLoggerSize = 1 << 13,
225  kSavepointManagerMemorySize = (3 << 19) + (1 << 12),
230  kMaxBoundaries = 1 << 7,
231  };
232 
235  void clear() { std::memset(this, 0, sizeof(*this)); }
236 
237  // No copying
240 
245 
251 
266 
277 
283 
293 
300 
307 };
308 
314  enum Constants {
318  kLoggerMemorySize = 1 << 21,
320  kMaxBoundaries = 1 << 12,
321  };
322 
325  void clear() { std::memset(this, 0, sizeof(*this)); }
326 
327  // No copying
330 
331  void allocate_arrays(const EngineOptions& options);
332  void deallocate_arrays();
333 
339 
345 
361 
368 
374 
381 
388 
393 
396 
403 };
404 
410  enum Constants {
411  kThreadMemorySize = 1 << 15,
417  };
418  ThreadMemoryAnchors() { std::memset(this, 0, sizeof(*this)); }
420 
421  // No copying
424 
430 
436 
442 
449  // These two might be integrated, but for now we allocate both.
453 };
454 
464  public:
466  soc_count_(0),
467  my_soc_id_(0),
468  node_memories_(CXX11_NULLPTR),
469  node_memory_anchors_(CXX11_NULLPTR) {}
471 
472  // Disable copy constructor
475 
477  ErrorStack allocate_shared_memories(uint64_t upid, Eid eid, const EngineOptions& options);
478 
491  uint64_t master_upid,
492  Eid master_eid,
493  SocId my_soc_id,
494  EngineOptions* options);
495 
508  void mark_for_release();
509 
512 
513  void* get_global_memory() { return global_memory_.get_block(); }
514  GlobalMemoryAnchors* get_global_memory_anchors() { return &global_memory_anchors_; }
515  void* get_global_user_memory() { return global_memory_anchors_.user_memory_; }
516 
519 
520  void* get_node_memory(SocId node) { return node_memories_[node].get_block(); }
521  NodeMemoryAnchors* get_node_memory_anchors(SocId node) { return &node_memory_anchors_[node]; }
522 
525 
527  SocId node = thread::decompose_numa_node(thread_id);
528  uint16_t thread_ordinal = thread::decompose_numa_local_ordinal(thread_id);
529  return &node_memory_anchors_[node].thread_anchors_[thread_ordinal];
530  }
531 
532  void* get_volatile_pool(SocId node) { return node_memory_anchors_[node].volatile_page_pool_; }
533 
534  static uint64_t calculate_global_memory_size(uint64_t xml_size, const EngineOptions& options);
535  static uint64_t calculate_node_memory_size(const EngineOptions& options);
536 
537  private:
538  SocId soc_count_;
539  SocId my_soc_id_;
540 
546  memory::SharedMemory global_memory_;
547  GlobalMemoryAnchors global_memory_anchors_;
548 
566  memory::SharedMemory* node_memories_;
567  NodeMemoryAnchors* node_memory_anchors_;
568 
569  void init_empty(const EngineOptions& options);
570 
571  void set_global_memory_anchors(
572  uint64_t xml_size,
573  const EngineOptions& options,
574  bool reset_boundaries);
575  void put_global_memory_boundary(
576  uint64_t* position,
577  const std::string& name,
578  bool reset_boundaries) {
579  char* base = global_memory_.get_block();
581  = reinterpret_cast<assorted::ProtectedBoundary*>(base + (*position));
582  if (reset_boundaries) {
583  boundary->reset(name);
584  }
585  uint32_t next_index = global_memory_anchors_.protected_boundaries_count_;
587  global_memory_anchors_.protected_boundaries_[next_index] = boundary;
588  ++global_memory_anchors_.protected_boundaries_count_;
589  *position += sizeof(assorted::ProtectedBoundary);
590  }
591 
592  void set_node_memory_anchors(SocId node, const EngineOptions& options, bool reset_boundaries);
593  void put_node_memory_boundary(
594  SocId node,
595  uint64_t* position,
596  const std::string& name,
597  bool reset_boundaries) {
598  char* base = node_memories_[node].get_block();
599  assorted::ProtectedBoundary* boundary
600  = reinterpret_cast<assorted::ProtectedBoundary*>(base + (*position));
601  if (reset_boundaries) {
602  boundary->reset(name);
603  }
604  uint32_t next_index = node_memory_anchors_[node].protected_boundaries_count_;
606  node_memory_anchors_[node].protected_boundaries_[next_index] = boundary;
607  ++node_memory_anchors_[node].protected_boundaries_count_;
608  *position += sizeof(assorted::ProtectedBoundary);
609  }
610 
611 
613  static void allocate_one_node(
614  uint64_t upid,
615  Eid eid,
616  uint16_t node,
617  uint64_t node_memory_size,
618  bool rigorous_memory_boundary_check,
619  bool rigorous_page_boundary_check,
620  ErrorStack* alloc_result,
621  SharedMemoryRepo* repo);
622 };
623 
625  sizeof(MasterEngineStatus) <= (1 << 12),
626  "Size of MasterEngineStatus exceeds 4kb");
627 
629  sizeof(ChildEngineStatus) <= (1 << 12),
630  "Size of ChildEngineStatus exceeds 4kb");
631 
632 } // namespace soc
633 } // namespace foedus
634 #endif // FOEDUS_SOC_SHARED_MEMORY_REPO_HPP_
xct::McsRwAsyncMapping * mcs_rw_async_mappings_memories_
uint32_t protected_boundaries_count_
To be a POD, we avoid vector and instead uses a fix-sized array.
ErrorStack allocate_shared_memories(uint64_t upid, Eid eid, const EngineOptions &options)
Master process creates shared memories by calling this method.
NodeMemoryAnchors & operator=(const NodeMemoryAnchors &other)=delete
Definitions of IDs in this package and a few related constant values.
GlobalMemoryAnchors & operator=(const GlobalMemoryAnchors &other)=delete
The master engine has normally terminated.
ModuleType initialized_modules_
The module that has been most recently initialized in this node.
Forward declarations of classes in log manager package.
A 4kb dummy data placed between separate memory regions so that we can check if/where a bogus memory ...
#define CXX11_NULLPTR
Used in public headers in place of "nullptr" of C++11.
Definition: cxx11.hpp:132
void change_master_status(MasterEngineStatus::StatusCode new_status)
void reset(const std::string &boundary_name)
Fills the block with magic words.
storage::StorageId * storage_name_sort_memory_
This memory stores the ID of storages sorted by their names.
uint32_t StorageId
Unique ID for storage.
Definition: storage_id.hpp:55
uint64_t options_xml_length_
The beginning of global memory is an XML-serialized EngineOption.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
void * partitioner_data_
Data block to place detailed information of partitioners.
xct::McsRwExtendedBlock * mcs_rw_extended_lock_memories_
The master is waiting for child engines to terminate.
proc::LocalProcId * proc_name_sort_memory_
This memory stores the ID of procedures sorted by their names.
GlobalMemoryAnchors * get_global_memory_anchors()
log::LogManagerControlBlock * log_manager_memory_
Tiny memory for log manager.
Typedefs of ID types used in thread package.
StatusCode
These statuses represent each step described in SocManager comment.
log::LoggerControlBlock ** logger_memories_
Status and synchronization mechanism for loggers on this node.
void * volatile_page_pool_
By far the largest memory for volatile page pool on this node.
Shared data of ThreadPimpl.
void deallocate_shared_memories()
Detaches and releases the shared memories.
Forward declarations of classes in transaction package.
void change_status_atomic(StatusCode new_status)
Update the value of status_code_ with fence.
char * get_block() const
Returns the memory block.
void change_status_atomic(StatusCode new_status)
Update the value of status_code_ with fence.
ThreadLocalOrdinal decompose_numa_local_ordinal(ThreadId global_id)
Extracts local ordinal from the given globally unique ID of Thread (core).
Definition: thread_id.hpp:139
Whenever child observes this, they will call _exit() asap.
Shared data in StorageManagerPimpl.
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
Reader-writer (RW) MCS lock classes.
Definition: xct_id.hpp:387
void * user_memory_
This 'user memory' can be used for arbitrary purporses by the user to communicate between SOCs...
Forward declarations of classes in snapshot manager package.
StatusCode read_status_atomic() const
Read status_code_ with fence.
Same as GlobalMemoryAnchors except this is for node_memories_.
Typedefs of ID types used in SOC package.
void mark_for_release()
Marks shared memories as being removed so that it will be reclaimed when all processes detach it...
Part of NodeMemoryAnchors for each thread.
NodeMemoryAnchors * get_node_memory_anchors(SocId node)
log::MetaLogControlBlock * meta_logger_memory_
Tiny memory for metadata logger.
Shared data in XctManagerPimpl.
void * task_input_memory_
Input buffer for an impersonated task.
ModuleType initialized_modules_
The module that has been most recently initialized in master.
Forward declarations of classes in restart package.
storage::StorageControlBlock * storage_memories_
Status of each storage instance is stored in this shared memory.
Child engine successfully attached shared memory and waiting for master's kWaitingForChildInitializat...
ModuleType
Enumerates modules in FOEDUS engine.
Definition: module_type.hpp:26
Represents memory shared between processes.
proc::ProcManagerControlBlock * proc_manager_memory_
ProcManagers's status and its synchronization mechanism on this node.
The child engine has normally terminated.
storage::StorageManagerControlBlock * storage_manager_memory_
Tiny memory for storage manager.
uint32_t protected_boundaries_count_
To be a POD, we avoid vector and instead uses a fix-sized array.
void change_uninit_atomic(ModuleType value)
Forward declarations of classes in storage package.
storage::PartitionerMetadata * partitioner_metadata_
Tiny metadata memory for partitioners.
Pre-allocated MCS block for WW-locks.
Definition: xct_id.hpp:274
Definitions of IDs in this package and a few related constant values.
Current status of master engine.
ErrorStack attach_shared_memories(uint64_t master_upid, Eid master_eid, SocId my_soc_id, EngineOptions *options)
Child processes (emulated or not) set a reference to shared memory and receive the EngnieOption value...
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
static uint64_t calculate_global_memory_size(uint64_t xml_size, const EngineOptions &options)
Pre-allocated MCS block for extended version of RW-locks.
Definition: xct_id.hpp:513
Shared data in LogManagerPimpl.
xct::McsWwBlock * mcs_ww_lock_memories_
Pre-allocated MCS block for each thread.
Done all initialization and running transactions.
ThreadMemoryAnchors & operator=(const ThreadMemoryAnchors &other)=delete
SharedMemoryRepo & operator=(const SharedMemoryRepo &other)=delete
Typedefs of ID types used in procedure package.
thread::ThreadControlBlock * thread_memory_
Status and synchronization mechanism for impersonation of this thread.
Just a marker to denote that the memory region represents a data page.
Definition: page.hpp:334
std::pair< ProcName, Proc > ProcAndName
Just a std::pair.
Definition: proc_id.hpp:119
Set of option values given to the engine at start-up.
Child engine has successfully initialized all modules and is now waiting for master's kRunning status...
snapshot::LogReducerControlBlock * log_reducer_memory_
Tiny control memory for LogReducer in this node.
xct::McsRwSimpleBlock * mcs_rw_simple_lock_memories_
ThreadGroupId decompose_numa_node(ThreadId global_id)
Extracts NUMA node ID from the given globally unique ID of Thread (core).
Definition: thread_id.hpp:131
Current status of a child SOC engine.
#define CXX11_STATIC_ASSERT(expr, message)
Used in public headers in place of "static_assert" of C++11.
Definition: cxx11.hpp:135
static uint64_t calculate_node_memory_size(const EngineOptions &options)
void * log_reducer_buffers_[2]
Actual buffers for LogReducer.
xct::XctManagerControlBlock * xct_manager_memory_
Tiny memory for xct manager.
Repository of all shared memory in one FOEDUS instance.
Shared data of Logger.
Definition: logger_impl.hpp:51
Forward declarations of classes in memory package.
Just a set of pointers within global_memory_ for ease of use.
snapshot::SnapshotManagerControlBlock * snapshot_manager_memory_
Tiny memory for snapshot manager.
storage::Page * log_reducer_root_info_pages_
This is the 'output' of the reducer in this node.
Shared data in PagePoolPimpl.
Forward declarations of classes in SOC package.
Shared data in RestartManagerPimpl.
uint16_t SocId
Represents an ID of an SOC, or NUMA node.
Definition: soc_id.hpp:41
ThreadMemoryAnchors * thread_anchors_
Anchors for each thread.
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
Forward declarations of classes in savepoint manager package.
void change_init_atomic(ModuleType value)
Tiny metadata of partitioner for every storage used while log gleaning.
uint16_t ThreadId
Typedef for a global ID of Thread (core), which is unique across NUMA nodes.
Definition: thread_id.hpp:80
void change_init_atomic(ModuleType value)
proc::ProcAndName * proc_memory_
Procedure list on this node.
MasterEngineStatus * master_status_memory_
This tiny piece of memory contains the current status of the master engine and its synchronization me...
This small control block is used to synchronize the access to the array.
void allocate_arrays(const EngineOptions &options)
Atomic fence methods and load/store with fences that work for both C++11/non-C++11 code...
void change_uninit_atomic(ModuleType value)
ChildEngineStatus * child_status_memory_
This tiny piece of memory contains the current status of the child engine on this node...
savepoint::SavepointManagerControlBlock * savepoint_manager_memory_
Tiny memory for savepoint manager.
uint32_t LocalProcId
Represents a locally-unique ID of a procedure in one SOC.
Definition: proc_id.hpp:56
uint64_t Eid
An Engine ID to differentiate two Engine objects instantiated in the same process.
Definition: engine_type.hpp:29
assorted::ProtectedBoundary * protected_boundaries_[kMaxBoundaries]
sanity check boundaries to detect bogus memory accesses that overrun a memory region ...
Done all initialization and running transactions.
void change_child_status(SocId node, ChildEngineStatus::StatusCode new_status)
#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
restart::RestartManagerControlBlock * restart_manager_memory_
Tiny memory for restart manager.
Forward declarations of classes in thread package.
ThreadMemoryAnchors * get_thread_memory_anchors(thread::ThreadId thread_id)
ModuleType uninitialized_modules_
The module that has been most recently closed in master.
Forward declarations of classes in proc package.
A base layout of shared data for all storage types.
Definition: storage.hpp:53
bool protected_boundaries_needs_release_
whether we have invoked mprotect on them
StatusCode read_status_atomic() const
Read status_code_ with fence.
memory::PagePoolControlBlock * volatile_pool_status_
PagePool's status and its synchronization mechanism for the volatile pool on this node...
bool protected_boundaries_needs_release_
whether we have invoked mprotect on them
ModuleType uninitialized_modules_
The module that has been most recently closed in this node.
Control block for MetaLogBuffer and MetaLogger.
ChildEngineStatus::StatusCode get_child_status(SocId node) const
Master engine successfully allocated shared memory and waiting for child's attach.
assorted::ProtectedBoundary * protected_boundaries_[kMaxBoundaries]
sanity check boundaries to detect bogus memory accesses that overrun a memory region ...
Master engine successfully confirmed child's attach and reserved the reclamation of the shared memori...
StatusCode
These statuses represent each step described in SocManager comment.
Master engine has just started.
The child engine observed some unrecoverable error and has exit.
MasterEngineStatus::StatusCode get_master_status() const
void memory_fence_acq_rel()
Equivalent to std::atomic_thread_fence(std::memory_order_acq_rel).
void * task_output_memory_
Output buffer for an impersonated task.