Skip to content

Commit

Permalink
Using shared_mutex for AGAS cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Feb 5, 2024
1 parent 7077d00 commit f86f0a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion libs/full/agas/include/hpx/agas/addressing_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <hpx/naming_base/address.hpp>
#include <hpx/naming_base/id_type.hpp>
#include <hpx/parcelset/parcelset_fwd.hpp>
#include <hpx/synchronization/shared_mutex.hpp>
#include <hpx/synchronization/spinlock.hpp>

#include <atomic>
Expand Down Expand Up @@ -70,7 +71,7 @@ namespace hpx { namespace agas {
using migrated_objects_table_type = std::set<naming::gid_type>;
using refcnt_requests_type = std::map<naming::gid_type, std::int64_t>;

mutable mutex_type gva_cache_mtx_;
mutable hpx::shared_mutex gva_cache_mtx_;
std::shared_ptr<gva_cache_type> gva_cache_;

mutable mutex_type migrated_objects_mtx_;
Expand Down
36 changes: 19 additions & 17 deletions libs/full/agas/src/addressing_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <hpx/runtime_local/runtime_local_fwd.hpp>
#include <hpx/serialization/serialize.hpp>
#include <hpx/serialization/vector.hpp>
#include <hpx/synchronization/shared_mutex.hpp>
#include <hpx/thread_support/assert_owns_lock.hpp>
#include <hpx/thread_support/unlock_guard.hpp>
#include <hpx/util/get_entry_as.hpp>
Expand All @@ -42,6 +43,7 @@
#include <map>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <sstream>
#include <string>
#include <system_error>
Expand Down Expand Up @@ -1736,7 +1738,7 @@ namespace hpx::agas {
gva_cache_key const key(gid, count);

{
std::unique_lock<mutex_type> lock(gva_cache_mtx_);
std::unique_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
if (!gva_cache_->update_if(key, g, check_for_collisions))
{
if (LAGAS_ENABLED(warning))
Expand Down Expand Up @@ -1789,7 +1791,7 @@ namespace hpx::agas {

gva_cache_key const k(gid);

std::unique_lock<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
if (gva_cache_key idbase_key; gva_cache_->get_entry(k, idbase_key, gva))
{
std::uint64_t const id_msb =
Expand Down Expand Up @@ -1827,7 +1829,7 @@ namespace hpx::agas {
LAGAS_(warning).format(
"addressing_service::clear_cache, clearing cache");

std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::unique_lock<hpx::shared_mutex> lock(gva_cache_mtx_);

gva_cache_->clear();

Expand Down Expand Up @@ -1874,7 +1876,7 @@ namespace hpx::agas {
{
LAGAS_(warning).format("addressing_service::remove_cache_entry");

std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::unique_lock<hpx::shared_mutex> lock(gva_cache_mtx_);

gva_cache_->erase([&gid](std::pair<gva_cache_key, gva> const& p) {
return gid == p.first.get_gid();
Expand Down Expand Up @@ -1905,87 +1907,87 @@ namespace hpx::agas {
// Helper functions to access the current cache statistics
std::uint64_t addressing_service::get_cache_entries(bool /* reset */) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->size();
}

std::uint64_t addressing_service::get_cache_hits(bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().hits(reset);
}

std::uint64_t addressing_service::get_cache_misses(bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().misses(reset);
}

std::uint64_t addressing_service::get_cache_evictions(bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().evictions(reset);
}

std::uint64_t addressing_service::get_cache_insertions(bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().insertions(reset);
}

///////////////////////////////////////////////////////////////////////////
std::uint64_t addressing_service::get_cache_get_entry_count(
bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_get_entry_count(reset);
}

std::uint64_t addressing_service::get_cache_insertion_entry_count(
bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_insert_entry_count(reset);
}

std::uint64_t addressing_service::get_cache_update_entry_count(
bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_update_entry_count(reset);
}

std::uint64_t addressing_service::get_cache_erase_entry_count(
bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_erase_entry_count(reset);
}

std::uint64_t addressing_service::get_cache_get_entry_time(bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_get_entry_time(reset);
}

std::uint64_t addressing_service::get_cache_insertion_entry_time(
bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_insert_entry_time(reset);
}

std::uint64_t addressing_service::get_cache_update_entry_time(
bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_update_entry_time(reset);
}

std::uint64_t addressing_service::get_cache_erase_entry_time(
bool reset) const
{
std::lock_guard<mutex_type> lock(gva_cache_mtx_);
std::shared_lock<hpx::shared_mutex> lock(gva_cache_mtx_);
return gva_cache_->get_statistics().get_erase_entry_time(reset);
}

Expand Down

0 comments on commit f86f0a9

Please sign in to comment.