Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use minimal TLS implementation for thread-local mapping objects #1500

Merged
merged 31 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fb8f792
Add comment on relaxed load
senderista Mar 30, 2022
1756132
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Mar 30, 2022
b6f0b11
Refactor the gaia::db::memory_manager namespace into the gaia::common…
senderista Mar 30, 2022
96d8225
Don't use CAS to mark allocation bits
senderista Mar 31, 2022
0f34ad9
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Apr 4, 2022
8f28333
Revert "Increase hash bucket count and misc. cleanup (#1425)"
senderista Apr 6, 2022
67a34b3
Merge branch 'master' into tobin/fix_id_index
senderista Apr 11, 2022
514528a
Merge branch 'master' into tobin/fix_id_index
senderista Apr 12, 2022
1f39745
Merge branch 'master' into tobin/fix_id_index
senderista Apr 13, 2022
9eb5e85
Merge branch 'master' into tobin/fix_id_index
senderista Apr 14, 2022
dee200d
Merge branch 'master' into tobin/fix_id_index
senderista Apr 15, 2022
7156f32
Merge branch 'master' into tobin/fix_id_index
senderista Apr 15, 2022
0a3c62b
Fix ID index bucket overflow bug
senderista Apr 15, 2022
40ec16b
Merge branch 'master' into tobin/fix_id_index
senderista Apr 15, 2022
6fcb684
Merge branch 'master' into tobin/fix_id_index
senderista Apr 18, 2022
426c29e
Merge branch 'tobin/fix_id_index' into tobin/inline_shared_memory_acc…
senderista Apr 18, 2022
e59314f
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Apr 21, 2022
1b6b614
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Apr 25, 2022
1532617
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Apr 28, 2022
e2c2413
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Apr 28, 2022
aaf0297
Restore memory_manager namespace
senderista Apr 28, 2022
f026272
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Apr 29, 2022
75d56df
Inline shared memory accessors
senderista Apr 30, 2022
48a72ae
Merge branch 'master' into tobin/inline_shared_memory_accessors
senderista Apr 30, 2022
3c1a12f
Factor out chunk allocation slow path from allocate_object()
senderista Apr 30, 2022
ae8056e
Fix typo
senderista Apr 30, 2022
6d47632
Merge branch 'master' into tobin/trivial_thread_locals
senderista May 2, 2022
30f5301
Make objects stored in TLS have trivial constructors and destructors
senderista May 2, 2022
4cc343e
Merge branch 'master' into tobin/trivial_thread_locals
senderista May 2, 2022
9acde99
Add comments on TLS attributes
senderista May 2, 2022
e741a29
Remove close_internal() method and add comments on manual resource ma…
senderista May 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions production/db/inc/core/db_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ class client_t
thread_local static inline gaia_txn_id_t s_txn_id = c_invalid_gaia_txn_id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance improvements are dramatic (10^8 records, asserts disabled):

What does this mean? Were all asserts disabled or just specific ones (the ones recently converted to debug)? Also, what was the gain without disabling any new asserts?

If there is a great gain from disabling asserts, which asserts accounted for it (i,e, which ones would be good candidates for being changed into debug asserts)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disabled all asserts to help determine the magnitude of performance improvements (because assertion overhead will tend to dilute perf gains). Of course asserts were disabled in the baseline (master) as well, if you were wondering. I haven't measured with asserts enabled.

thread_local static inline log_offset_t s_txn_log_offset = c_invalid_log_offset;

thread_local static inline mapped_data_t<locators_t> s_private_locators;
// NB: We need to use the (nonstandard) __thread attribute rather than
// thread_local to ensure that a minimal TLS implementation is used.
__thread static inline mapped_data_t<locators_t> s_private_locators{};

thread_local static inline gaia::db::index::indexes_t s_local_indexes{};

// These fields have session lifetime.
Expand All @@ -131,11 +134,13 @@ class client_t

thread_local static inline int s_fd_locators = -1;

thread_local static inline mapped_data_t<counters_t> s_shared_counters;
thread_local static inline mapped_data_t<data_t> s_shared_data;
thread_local static inline mapped_data_t<logs_t> s_shared_logs;
thread_local static inline mapped_data_t<id_index_t> s_shared_id_index;
thread_local static inline mapped_data_t<type_index_t> s_shared_type_index;
// NB: We need to use the (nonstandard) __thread attribute rather than
// thread_local to ensure that a minimal TLS implementation is used.
__thread static inline mapped_data_t<counters_t> s_shared_counters{};
__thread static inline mapped_data_t<data_t> s_shared_data{};
__thread static inline mapped_data_t<logs_t> s_shared_logs{};
__thread static inline mapped_data_t<id_index_t> s_shared_id_index{};
__thread static inline mapped_data_t<type_index_t> s_shared_type_index{};

thread_local static inline gaia::db::memory_manager::memory_manager_t s_memory_manager{};
thread_local static inline gaia::db::memory_manager::chunk_manager_t s_chunk_manager{};
Expand Down
5 changes: 4 additions & 1 deletion production/db/inc/core/db_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ class server_t
thread_local static inline std::vector<std::pair<gaia_txn_id_t, log_offset_t>> s_txn_logs_for_snapshot{};

// Local snapshot for server-side transactions.
thread_local static inline mapped_data_t<locators_t> s_local_snapshot_locators{};
// NB: We need to use the (nonstandard) __thread attribute rather than
// thread_local to ensure that a minimal TLS implementation is used.
__thread static inline mapped_data_t<locators_t> s_local_snapshot_locators{};

// Watermark that tracks how many log records have been used for the current snapshot instance.
// This is used to permit the incremental updating of the snapshot.
thread_local static inline size_t s_last_snapshot_processed_log_record_count{0};
Expand Down
18 changes: 11 additions & 7 deletions production/db/inc/core/mapped_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ template <typename T_data>
class core_mapped_data_t : public base_mapped_data_t
{
public:
core_mapped_data_t();
// Thread-local variables cannot have nontrivial constructors without
// prohibitive overhead.
constexpr core_mapped_data_t() = default;

// Copy semantics is disabled and moves should be performed via reset().
core_mapped_data_t(const core_mapped_data_t& other) = delete;
core_mapped_data_t(core_mapped_data_t&& other) = delete;
core_mapped_data_t& operator=(const core_mapped_data_t& rhs) = delete;
core_mapped_data_t& operator=(core_mapped_data_t&& rhs) = delete;

~core_mapped_data_t();
// Thread-local variables cannot have nontrivial destructors without
// prohibitive overhead.
~core_mapped_data_t() = default;

// Stops tracking any data and reverts back to uninitialized state.
void clear();
Expand All @@ -67,12 +71,12 @@ class core_mapped_data_t : public base_mapped_data_t
void close_internal();

protected:
bool m_is_set;
int m_fd;
T_data* m_data;
bool m_is_set{false};
int m_fd{-1};
T_data* m_data{nullptr};

// This is used to track the mapped data size, so we can call unmap_fd_data()/munmap() with the same value.
size_t m_mapped_data_size;
size_t m_mapped_data_size{0};
};

// This class abstracts the server and client operations with memory-mapped data.
Expand All @@ -81,7 +85,7 @@ template <typename T_data>
class mapped_data_t : public core_mapped_data_t<T_data>
{
public:
mapped_data_t() = default;
constexpr mapped_data_t() = default;

// Copy semantics is disabled and moves should be performed via reset().
mapped_data_t(const mapped_data_t& other) = delete;
Expand Down
12 changes: 0 additions & 12 deletions production/db/inc/core/mapped_data.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
// All rights reserved.
/////////////////////////////////////////////

template <typename T_data>
core_mapped_data_t<T_data>::core_mapped_data_t()
{
clear();
}

template <typename T_data>
core_mapped_data_t<T_data>::~core_mapped_data_t()
{
close_internal();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that close_internal was only called in two places, so if this goes away, can we merge the internal function into the other caller?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and I don't remember - how do we call this now - where is this cleanup being done after this change? Is it through the data_mapping functions?

We should also add an explicit comment to this class that the cleanup is manual and no longer automatic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all cleanup is being done through data_mapping_t::close(); I can add a comment to that effect. These classes could use some refactoring as you mentioned before, but I considered that out of scope for this PR; feel free to change as you see fit. (BTW, the only virtual calls I found were through the data_mapping_t wrappers, and they're not in hot paths, so I left them alone.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In violation of my stated goal of minimal changes to the data mapping classes, I went ahead and removed the close_internal() method :) Given the now-empty destructor, I thought it was superfluous to the point of being confusing.

Copy link
Contributor

@LaurentiuCristofor LaurentiuCristofor May 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These classes could use some refactoring as you mentioned before, but I considered that out of scope for this PR; feel free to change as you see fit. (BTW, the only virtual calls I found were through the data_mapping_t wrappers, and they're not in hot paths, so I left them alone.)

I'm not sure if there is much refactoring that can be done here. That's what I noticed also, when I took a closer look - that the hierarchy was needed for the data_mapping_t handling.

In violation of my stated goal of minimal changes to the data mapping classes, I went ahead and removed the close_internal() method :) Given the now-empty destructor, I thought it was superfluous to the point of being confusing.

Right. Thanks! It's a fairly small change and I think it helps to remove these "internal" helpers when they're no longer needed.

}

template <typename T_data>
void core_mapped_data_t<T_data>::clear()
{
Expand Down