libfoedus-core
FOEDUS Core Library
thread.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_THREAD_THREAD_HPP_
19 #define FOEDUS_THREAD_THREAD_HPP_
20 #include <iosfwd>
21 
22 #include "foedus/fwd.hpp"
23 #include "foedus/initializable.hpp"
25 #include "foedus/log/fwd.hpp"
26 #include "foedus/memory/fwd.hpp"
28 #include "foedus/storage/fwd.hpp"
30 #include "foedus/thread/fwd.hpp"
32 #include "foedus/xct/fwd.hpp"
33 #include "foedus/xct/xct_id.hpp"
34 
35 namespace foedus {
36 namespace thread {
48 class Thread CXX11_FINAL : public virtual Initializable {
49  public:
50  enum Constants {
56  };
58  Thread(Engine* engine, ThreadId id, ThreadGlobalOrdinal global_ordinal);
59  ~Thread();
61  bool is_initialized() const CXX11_OVERRIDE;
62  ErrorStack uninitialize() CXX11_OVERRIDE;
63 
64  Engine* get_engine() const;
65  ThreadId get_thread_id() const;
68 
74  bool is_running_xct() const;
75 
80 
85 
93 
95  uint64_t get_snapshot_cache_hits() const;
97  uint64_t get_snapshot_cache_misses() const;
99  void reset_snapshot_cache_counts() const;
100 
110  template <typename P> P* resolve_cast(storage::VolatilePagePointer ptr) const {
111  return reinterpret_cast<P*>(resolve(ptr));
112  }
113  template <typename P> P* resolve_newpage_cast(storage::VolatilePagePointer ptr) const {
114  return reinterpret_cast<P*>(resolve_newpage(ptr));
115  }
116  template <typename P> P* resolve_cast(memory::PagePoolOffset offset) const {
117  return reinterpret_cast<P*>(resolve(offset));
118  }
119  template <typename P> P* resolve_newpage_cast(memory::PagePoolOffset offset) const {
120  return reinterpret_cast<P*>(resolve_newpage(offset));
121  }
122 
123 
129  storage::Page** out);
140  uint16_t batch_size,
141  const storage::SnapshotPagePointer* page_ids,
142  storage::Page** out);
143 
151 
154  storage::SnapshotPagePointer page_id_begin,
155  uint32_t page_count,
156  storage::Page* buffer);
157 
171  storage::DualPagePointer* pointer,
172  storage::Page** installed_page);
173 
204  storage::VolatilePageInit page_initializer,
205  bool tolerate_null_pointer,
206  bool will_modify,
207  bool take_ptr_set_snapshot,
208  storage::DualPagePointer* pointer,
209  storage::Page** page,
210  const storage::Page* parent,
211  uint16_t index_in_parent);
212 
235  uint16_t batch_size,
236  storage::VolatilePageInit page_initializer,
237  bool tolerate_null_pointer,
238  bool take_ptr_set_snapshot,
239  storage::DualPagePointer** pointers,
240  storage::Page** parents,
241  const uint16_t* index_in_parents,
242  bool* followed_snapshots,
243  storage::Page** out);
244 
259  uint16_t batch_size,
260  storage::VolatilePageInit page_initializer,
261  storage::DualPagePointer** pointers,
262  storage::Page** parents,
263  const uint16_t* index_in_parents,
264  storage::Page** out);
265 
274 
287 
298  if (address == xct::kNullUniversalLockId) {
300  } else {
301  cll_giveup_all_locks_after(address - 1U);
302  }
303  }
311  if (address == xct::kNullUniversalLockId) {
313  } else {
314  cll_release_all_locks_after(address - 1U);
315  }
316  }
317  void cll_release_all_locks();
318 
323 
326  ErrorCode run_nested_sysxct(xct::SysxctFunctor* functor, uint32_t max_retries = 0);
333  xct::SysxctWorkspace* sysxct_workspace,
335  xct::RwLockableXctId* lock);
342  xct::SysxctWorkspace* sysxct_workspace,
344  uint32_t lock_count,
345  xct::RwLockableXctId** locks);
356  xct::SysxctWorkspace* sysxct_workspace,
357  uint32_t lock_count,
358  storage::Page** pages);
361 
364 
366  ThreadPimpl* get_pimpl() const { return pimpl_; }
367 
368  inline assorted::UniformRandom& get_lock_rnd() { return lock_rnd_; }
369 
374  bool is_hot_page(const storage::Page* page) const;
375 
376  friend std::ostream& operator<<(std::ostream& o, const Thread& v);
377 
378  private:
379  ThreadPimpl* pimpl_;
380 
381  // The following should be in pimpl. later.
382  assorted::UniformRandom lock_rnd_;
383 };
384 
392  public:
394  : context_(context), offsets_(offsets), count_(0) {}
396  release();
397  }
398 
403  ErrorCode grab(uint32_t count);
405  void release();
406  uint32_t get_count() const { return count_; }
407 
409  void dispatch(uint32_t index) {
410  ASSERT_ND(index < count_);
411  offsets_[index] = 0; // This entry will be skipped in release()
412  }
413  memory::PagePoolOffset get(uint32_t index) const {
414  ASSERT_ND(index < count_);
415  return offsets_[index];
416  }
417 
418  private:
419  Thread* const context_;
420  memory::PagePoolOffset* const offsets_;
421  uint32_t count_;
422 };
423 
424 } // namespace thread
425 } // namespace foedus
426 #endif // FOEDUS_THREAD_THREAD_HPP_
friend std::ostream & operator<<(std::ostream &o, const Thread &v)
Definition: thread.cpp:118
const memory::GlobalVolatilePageResolver & get_global_volatile_page_resolver() const
Returns the page resolver to convert page ID to page pointer.
Definition: thread.cpp:125
xct::Xct & get_current_xct()
Returns the transaction that is currently running on this thread.
Definition: thread.cpp:75
ErrorCode find_or_read_a_snapshot_page(storage::SnapshotPagePointer page_id, storage::Page **out)
Find the given page in snapshot cache, reading it if not found.
Definition: thread.cpp:95
ErrorStack initialize() override
Acquires resources in this object, usually called right after constructor.
Definition: thread.cpp:45
ThreadGlobalOrdinal get_thread_global_ordinal() const
Definition: thread.cpp:54
ErrorCode install_a_volatile_page(storage::DualPagePointer *pointer, storage::Page **installed_page)
Installs a volatile page to the given dual pointer as a copy of the snapshot page.
Definition: thread.cpp:107
Represents a pointer to another page (usually a child page).
Definition: storage_id.hpp:271
ErrorCode find_or_read_snapshot_pages_batch(uint16_t batch_size, const storage::SnapshotPagePointer *page_ids, storage::Page **out)
Batched version of find_or_read_a_snapshot_page().
Definition: thread.cpp:100
memory::NumaCoreMemory * get_thread_memory() const
Returns the private memory repository of this thread.
Definition: thread.cpp:57
ErrorCode follow_page_pointers_for_write_batch(uint16_t batch_size, storage::VolatilePageInit page_initializer, storage::DualPagePointer **pointers, storage::Page **parents, const uint16_t *index_in_parents, storage::Page **out)
Batched version of follow_page_pointer with will_modify==true and tolerate_null_pointer==true.
uint64_t get_snapshot_cache_misses() const
[statistics] count of cache misses in snapshot caches
Definition: thread.cpp:66
Definitions of IDs in this package and a few related constant values.
A thread-local log buffer.
The pure-virtual interface to initialize/uninitialize non-trivial resources.
Forward declarations of classes in log manager package.
ErrorCode sysxct_batch_page_locks(xct::SysxctWorkspace *sysxct_workspace, uint32_t lock_count, storage::Page **pages)
Takes a bunch of page locks for a sysxct running under this thread.
ErrorCode cll_try_or_acquire_single_lock(xct::LockListPosition pos)
Methods related to Current Lock List (CLL) These are the only interface in Thread to lock records...
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
ErrorCode sysxct_batch_record_locks(xct::SysxctWorkspace *sysxct_workspace, storage::VolatilePagePointer page_id, uint32_t lock_count, xct::RwLockableXctId **locks)
Takes a bunch of locks in the same page for a sysxct running under this thread.
ErrorCode sysxct_record_lock(xct::SysxctWorkspace *sysxct_workspace, storage::VolatilePagePointer page_id, xct::RwLockableXctId *lock)
Takes a lock for a sysxct running under this thread.
Represents one thread running on one NUMA core.
Definition: thread.hpp:48
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.
void collect_retired_volatile_page(storage::VolatilePagePointer ptr)
Keeps the specified volatile page as retired as of the current epoch.
Definition: thread.cpp:113
xct::McsRwExtendedBlock * get_mcs_rw_extended_blocks()
bool is_initialized() const override
Returns whether the object has been already initialized or not.
Definition: thread.cpp:49
memory::NumaNodeMemory * get_node_memory() const
Returns the node-shared memory repository of the NUMA node this thread belongs to.
Definition: thread.cpp:58
Forward declarations of classes in transaction package.
ThreadId get_thread_id() const
Definition: thread.cpp:53
Represents a pointer to a volatile page with modification count for preventing ABA.
Definition: storage_id.hpp:194
Represents a user transaction.
Definition: xct.hpp:58
Forward declarations of classes in root package.
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
Represents a time epoch.
Definition: epoch.hpp:61
ErrorCode follow_page_pointer(storage::VolatilePageInit page_initializer, bool tolerate_null_pointer, bool will_modify, bool take_ptr_set_snapshot, storage::DualPagePointer *pointer, storage::Page **page, const storage::Page *parent, uint16_t index_in_parent)
A general method to follow (read) a page pointer.
Max size for find_or_read_snapshot_pages_batch() etc.
Definition: thread.hpp:55
void cll_release_all_locks_at_and_after(xct::UniversalLockId address)
same as mcs_release_all_current_locks_after(address - 1)
Definition: thread.hpp:310
void cll_giveup_all_locks_at_and_after(xct::UniversalLockId address)
Definition: thread.hpp:297
Engine * get_engine() const
Definition: thread.cpp:52
storage::Page * resolve_newpage(storage::VolatilePagePointer ptr) const
Shorthand for get_global_volatile_page_resolver.resolve_offset_newpage()
Definition: thread.cpp:132
Epoch * get_in_commit_epoch_address()
Currently we don't have sysxct_release_locks() etc.
Definition: thread.cpp:55
Repository of memories dynamically acquired within one CPU core (thread).
ThreadPimpl * get_pimpl() const
Returns the pimpl of this object.
Definition: thread.hpp:366
Pimpl object of Thread.
ErrorCode grab(uint32_t count)
If this thread doesn't have enough free pages, no page is obtained, returning kErrorCodeMemoryNoFreeP...
Definition: thread.cpp:147
uintptr_t UniversalLockId
Universally ordered identifier of each lock.
Definition: xct_id.hpp:134
The MCS reader-writer lock variant of LockableXctId.
Definition: xct_id.hpp:1132
storage::Page * resolve(storage::VolatilePagePointer ptr) const
Shorthand for get_global_volatile_page_resolver.resolve_offset()
Definition: thread.cpp:129
log::ThreadLogBuffer & get_thread_log_buffer()
Returns the private log buffer for this thread.
Definition: thread.cpp:78
P * resolve_newpage_cast(memory::PagePoolOffset offset) const
Definition: thread.hpp:119
A functor representing the logic in a system transaction via virtual-function.
void dispatch(uint32_t index)
Call this when the page is placed somewhere.
Definition: thread.hpp:409
P * resolve_newpage_cast(storage::VolatilePagePointer ptr) const
Definition: thread.hpp:113
Forward declarations of classes in storage package.
Definitions of IDs in this package and a few related constant values.
bool is_running_xct() const
Returns if this thread is running an active transaction.
Definition: thread.cpp:76
ErrorCode sysxct_page_lock(xct::SysxctWorkspace *sysxct_workspace, storage::Page *page)
Takes a page lock in the same page for a sysxct running under this thread.
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
uint32_t LockListPosition
Index in a lock-list, either RLL or CLL.
Definition: xct_id.hpp:148
uint64_t SnapshotPagePointer
Page ID of a snapshot page.
Definition: storage_id.hpp:79
Pre-allocated MCS block for extended version of RW-locks.
Definition: xct_id.hpp:513
ErrorCode follow_page_pointers_for_read_batch(uint16_t batch_size, storage::VolatilePageInit page_initializer, bool tolerate_null_pointer, bool take_ptr_set_snapshot, storage::DualPagePointer **pointers, storage::Page **parents, const uint16_t *index_in_parents, bool *followed_snapshots, storage::Page **out)
Batched version of follow_page_pointer with will_modify==false.
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
Repository of memories dynamically acquired and shared within one NUMA node (socket).
Just a marker to denote that the memory region represents a data page.
Definition: page.hpp:334
void cll_release_all_locks_after(xct::UniversalLockId address)
Release all locks in CLL of this thread whose addresses are canonically ordered before the parameter...
void cll_giveup_all_locks_after(xct::UniversalLockId address)
This gives-up locks in CLL that are not yet taken.
P * resolve_cast(memory::PagePoolOffset offset) const
Definition: thread.hpp:116
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
P * resolve_cast(storage::VolatilePagePointer ptr) const
resolve() plus reinterpret_cast
Definition: thread.hpp:110
void reset_snapshot_cache_counts() const
[statistics] resets the above two
Definition: thread.cpp:70
bool is_hot_page(const storage::Page *page) const
Definition: thread.cpp:142
const memory::LocalPageResolver & get_local_volatile_page_resolver() const
Returns page resolver to convert only local page ID to page pointer.
Definition: thread.cpp:80
uint64_t get_snapshot_cache_hits() const
[statistics] count of cache hits in snapshot caches
Definition: thread.cpp:62
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
uint16_t ThreadGlobalOrdinal
Typedef for a globally and contiguously numbered ID of thread.
Definition: thread_id.hpp:98
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
uint16_t ThreadId
Typedef for a global ID of Thread (core), which is unique across NUMA nodes.
Definition: thread_id.hpp:80
A very simple and deterministic random generator that is more aligned with standard benchmark such as...
Resolves an offset in local (same NUMA node) page pool to a pointer and vice versa.
xct::McsRwSimpleBlock * get_mcs_rw_simple_blocks()
Unconditionally takes MCS lock on the given mcs_lock.
ThreadGroupId get_numa_node() const
Definition: thread.hpp:66
assorted::UniformRandom & get_lock_rnd()
Definition: thread.hpp:368
void(* VolatilePageInit)(const VolatilePageInitArguments &args)
A function pointer to initialize a volatile page.
Definition: page.hpp:387
Resolves an offset in a volatile page pool to an actual pointer and vice versa.
ErrorStack uninitialize() override
An idempotent method to release all resources of this object, if any.
Definition: thread.cpp:50
#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
ErrorCode run_nested_sysxct(xct::SysxctFunctor *functor, uint32_t max_retries=0)
Methods related to System transactions (sysxct) nested under this thread.
Forward declarations of classes in thread package.
uint8_t ThreadGroupId
Typedef for an ID of ThreadGroup (NUMA node).
Definition: thread_id.hpp:38
Obtains multiple free volatile pages at once and releases them automatically when this object gets ou...
Definition: thread.hpp:391
GrabFreeVolatilePagesScope(Thread *context, memory::PagePoolOffset *offsets)
Definition: thread.hpp:393
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
ErrorCode read_snapshot_pages(storage::SnapshotPagePointer page_id_begin, uint32_t page_count, storage::Page *buffer)
Read contiguous pages in one shot.
Definition: thread.cpp:89
Per-thread reused work memory for system transactions.
const UniversalLockId kNullUniversalLockId
This never points to a valid lock, and also evaluates less than any vaild alocks. ...
Definition: xct_id.hpp:137
ErrorCode cll_try_or_acquire_multiple_locks(xct::LockListPosition upto_pos)
Acquire multiple locks up to the given position in canonical order.
ErrorCode read_a_snapshot_page(storage::SnapshotPagePointer page_id, storage::Page *buffer)
Read a snapshot page using the thread-local file descriptor set.
Definition: thread.cpp:84