libfoedus-core
FOEDUS Core Library
proc_manager_pimpl.cpp
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  */
19 
20 #include <glog/logging.h>
21 
22 #include <sstream>
23 #include <string>
24 
25 #include "foedus/engine.hpp"
27 #include "foedus/epoch.hpp"
32 
33 namespace foedus {
34 namespace proc {
36  // attach shared memories of all SOCs
37  all_soc_procs_.clear();
40  for (soc::SocId node = 0; node < soc_count; ++node) {
41  soc::NodeMemoryAnchors* anchors = memory_repo->get_node_memory_anchors(node);
42  SharedData data;
43  data.control_block_ = anchors->proc_manager_memory_;
44  data.name_sort_ = anchors->proc_name_sort_memory_;
45  data.procs_ = anchors->proc_memory_;
46  all_soc_procs_.push_back(data);
47  }
48 
49  if (!engine_->is_master()) {
50  LOG(INFO) << "Initializing ProcManager(" << engine_->describe_short() << ")..";
52  }
53 
54  // TODO(Hideaki) load shared libraries
55  return kRetOk;
56 }
57 
59  ErrorStackBatch batch;
60  // TODO(Hideaki) unload shared libraries
61  if (!engine_->is_master()) {
62  LOG(INFO) << "Uninitializing ProcManager(" << engine_->describe_short() << ")..";
64  return kRetOk;
65  }
66  all_soc_procs_.clear();
67  return SUMMARIZE_ERROR_BATCH(batch);
68 }
69 
70 
72  soc::SocId node = engine_->get_soc_id();
73  LocalProcId id = find_by_name(name, &all_soc_procs_[node]);
74  if (id == kLocalProcInvalid) {
76  }
77  *out = all_soc_procs_[node].procs_[id].second;
78  return kRetOk;
79 }
82  return &all_soc_procs_[engine_->get_soc_id()];
83 }
86  return &all_soc_procs_[engine_->get_soc_id()];
87 }
88 
90  if (is_initialized()) {
91  LOG(ERROR) << "Incorrect use of pre_register(): "
94  }
95  if (!engine_->is_master()) {
96  LOG(ERROR) << "Incorrect use of pre_register(): "
99  }
101  if (soc_type != kChildEmulated && soc_type != kChildForked) {
103  }
104  pre_registered_procs_.push_back(proc_and_name);
105  // This is BEFORE the init, so we shouldn't use GLOG
106  // LOG(INFO) << "pre-registered a user procedure: " << proc_and_name.first;
107  return kRetOk;
108 }
110  if (!is_initialized()) {
111  LOG(ERROR) << "Incorrect use of local_register(): "
114  }
115  if (engine_->is_master()) {
116  LOG(ERROR) << "Incorrect use of local_register(): "
119  }
120  LocalProcId result = insert(proc_and_name, get_local_data());
121  if (result == kLocalProcInvalid) {
122  LOG(ERROR) << "A procedure of this name is already registered in this engine: "
123  << proc_and_name.first;
125  }
126  LOG(INFO) << "local-registered a user procedure: " << proc_and_name.first;
127  return kRetOk;
128 }
130  if (!is_initialized()) {
131  LOG(ERROR) << "Incorrect use of emulated_register(): "
134  }
135 
137  if (soc_type != kChildEmulated) {
138  LOG(ERROR) << "Incorrect use of emulated_register(): "
141  }
142  LOG(INFO) << "emulated-registered a user procedure: " << proc_and_name.first;
143  return kRetOk;
144 }
145 
147  // so far just a seqnetial search.
148  LocalProcId count = shared_data->control_block_->count_;
150  for (LocalProcId i = 0; i < count; ++i) {
151  if (shared_data->procs_[i].first == name) {
152  return i;
153  }
154  }
155  return kLocalProcInvalid;
156 }
157 
158 LocalProcId ProcManagerPimpl::insert(const ProcAndName& proc_and_name, SharedData* shared_data) {
159  soc::SharedMutexScope lock_scope(&shared_data->control_block_->lock_);
160  LocalProcId found = find_by_name(proc_and_name.first, shared_data);
161  // TASK(Hideaki) max_proc_count check.
162  if (found != kLocalProcInvalid) {
163  return kLocalProcInvalid;
164  }
165  LocalProcId new_id = shared_data->control_block_->count_;
166  shared_data->procs_[new_id] = proc_and_name;
167  ++shared_data->control_block_->count_;
168  // TASK(Hideaki) insert-sort to name_sort
169  return new_id;
170 }
171 
173  if (engine_->is_master()) {
174  return "<Master engine has no proc>";
175  }
176  std::stringstream str;
177  const SharedData* data = get_local_data();
178  str << "Proc Count=" << data->control_block_->count_ << ", (name,address)=[";
179  for (LocalProcId i = 0; i < data->control_block_->count_; ++i) {
180  if (i > 0) {
181  str << ",";
182  }
183  str << "(" << data->procs_[i].first << "," << data->procs_[i].second << ")";
184  }
185  str << "]";
186 
187  return str.str();
188 }
189 
190 
191 } // namespace proc
192 } // namespace foedus
ErrorStack uninitialize_once() override
LocalProcId * name_sort_
IDs sorted by name for quick lookup.
std::string describe_short() const
Definition: engine.cpp:38
std::vector< SharedData > all_soc_procs_
Shared data of all SOCs.
ErrorStack local_register(const ProcAndName &proc_and_name)
#define ERROR_STACK(e)
Instantiates ErrorStack with the given foedus::error_code, creating an error stack with the current f...
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
static LocalProcId find_by_name(const ProcName &name, SharedData *shared_data)
proc::LocalProcId * proc_name_sort_memory_
This memory stores the ID of procedures sorted by their names.
0x0D02 : "PROC : Post-register can be called only after engine initialization." ...
Definition: error_code.hpp:234
0x0D04 : "PROC : This registration type can be invoked only at master engine." .
Definition: error_code.hpp:236
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
soc::SharedMutex lock_
Mutex to protect data.
EngineType soc_type_
How to launch SOC engine instances.
Definition: soc_options.hpp:54
Same as GlobalMemoryAnchors except this is for node_memories_.
0x0D03 : "PROC : This registration type cannot be used for this SOC type." .
Definition: error_code.hpp:235
NodeMemoryAnchors * get_node_memory_anchors(SocId node)
const EngineOptions & get_options() const
Definition: engine.cpp:39
bool is_master() const
Returns if this engine object is a master instance.
Definition: engine.cpp:68
proc::ProcManagerControlBlock * proc_manager_memory_
ProcManagers's status and its synchronization mechanism on this node.
ErrorStack initialize_once() override
ErrorStack pre_register(const ProcAndName &proc_and_name)
Batches zero or more ErrorStack objects to represent in one ErrorStack.
0x0D07 : "PROC : The specified procedure name already exists in this engine." . ...
Definition: error_code.hpp:239
std::string describe_registered_procs() const
Auto-lock scope object for SharedMutex.
0x0D01 : "PROC : Pre-register can be called only before engine initialization." ...
Definition: error_code.hpp:233
std::pair< ProcName, Proc > ProcAndName
Just a std::pair.
Definition: proc_id.hpp:119
0x0D05 : "PROC : This registration type can be invoked only at child engine." . ...
Definition: error_code.hpp:237
EngineType
Type of an engine instance of how to launch it.
Definition: engine_type.hpp:35
ErrorStack get_proc(const ProcName &name, Proc *out)
const LocalProcId kLocalProcInvalid
Definition: proc_id.hpp:58
A child SOC instance launched via fork().
Definition: engine_type.hpp:65
uint16_t group_count_
Number of ThreadGroup in the engine.
Repository of all shared memory in one FOEDUS instance.
ErrorStack emulated_register(const ProcAndName &proc_and_name)
#define SUMMARIZE_ERROR_BATCH(x)
This macro calls ErrorStackBatch::summarize() with automatically provided parameters.
uint16_t SocId
Represents an ID of an SOC, or NUMA node.
Definition: soc_id.hpp:41
thread::ThreadOptions thread_
ProcAndName * procs_
The procedure list maintained in this module is an array of ProcName.
proc::ProcAndName * proc_memory_
Procedure list on this node.
const ErrorStack kRetOk
Normal return value for no-error case.
soc::SocManager * get_soc_manager() const
See SOC and IPC.
Definition: engine.cpp:59
const char * get_error_message(ErrorCode code)
Returns the error messages corresponding to ErrorCode enum defined in error_code.xmacro.
Definition: error_code.hpp:120
Atomic fence methods and load/store with fences that work for both C++11/non-C++11 code...
0x0D06 : "PROC : The specified procedure name is not found in this engine." .
Definition: error_code.hpp:238
#define ERROR_STACK_MSG(e, m)
Overload of ERROR_STACK(e) to receive a custom error message.
void memory_fence_acquire()
Equivalent to std::atomic_thread_fence(std::memory_order_acquire).
soc::SocId get_soc_id() const
If this is a child instance, returns its SOC ID (NUMA node).
Definition: engine.cpp:73
uint32_t LocalProcId
Represents a locally-unique ID of a procedure in one SOC.
Definition: proc_id.hpp:56
ErrorStack(* Proc)(const ProcArguments &args)
A function pointer of a user/system stored procedure.
Definition: proc_id.hpp:113
#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
const CHAR * c_str() const
Convert to a C string.
static LocalProcId insert(const ProcAndName &proc_and_name, SharedData *shared_data)
bool is_initialized() const override final
Returns whether the object has been already initialized or not.
std::vector< ProcAndName > pre_registered_procs_
SharedMemoryRepo * get_shared_memory_repo()
Returns the shared memories maintained across SOCs.
Definition: soc_manager.cpp:38
A child SOC instance launched just as a thread in the same process as master.
Definition: engine_type.hpp:51