From a24caba61708b0f534ab130f17f53aa0248354bd Mon Sep 17 00:00:00 2001 From: Valentin Balaschenko <13349202+vlntb@users.noreply.github.com> Date: Mon, 25 Aug 2025 17:06:46 +0100 Subject: [PATCH] concurrent access --- include/xrpl/basics/TaggedCache.ipp | 76 ++++++++++++++------------ src/xrpld/app/ledger/LedgerHistory.cpp | 38 +++++++------ 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/include/xrpl/basics/TaggedCache.ipp b/include/xrpl/basics/TaggedCache.ipp index 5e6b7b0e5d..6137b33db0 100644 --- a/include/xrpl/basics/TaggedCache.ipp +++ b/include/xrpl/basics/TaggedCache.ipp @@ -23,6 +23,8 @@ #include #include +#include + namespace ripple { template < @@ -105,10 +107,10 @@ TaggedCache< KeyEqual, Mutex>::size() const { - static unsigned long call_count{0}; - JLOG(m_journal.debug()) << "TaggedCache (" << m_name << ") lock stats," - << ", size, call_count = " << call_count++; std::lock_guard lock(m_mutex); + static unsigned long call_count{0}; + std::cout << "TaggedCache (" << m_name << ") lock stats," + << ", size, call_count = " << ++call_count << std::endl; return m_cache.size(); } @@ -258,12 +260,12 @@ TaggedCache< KeyEqual, Mutex>::touch_if_exists(KeyComparable const& key) { - static unsigned long call_count{0}; - JLOG(m_journal.debug()) - << "TaggedCache (" << m_name << ") lock stats," - << ", touch_if_exists, call_count = " << call_count++; - std::lock_guard lock(m_mutex); + + static unsigned long call_count{0}; + std::cout << "TaggedCache (" << m_name << ") lock stats," + << "touch_if_exists, call_count = " << ++call_count << std::endl; + auto const iter(m_cache.find(key)); if (iter == m_cache.end()) { @@ -378,11 +380,12 @@ TaggedCache< // Remove from cache, if !valid, remove from map too. Returns true if // removed from cache - static unsigned long call_count{0}; - JLOG(m_journal.debug()) << "TaggedCache (" << m_name << ") lock stats," - << ", del, call_count = " << call_count++; std::lock_guard lock(m_mutex); + static unsigned long call_count{0}; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "del, call_count = " << ++call_count << std::endl; + auto cit = m_cache.find(key); if (cit == m_cache.end()) @@ -435,8 +438,8 @@ TaggedCache< std::lock_guard lock(m_mutex); static unsigned long call_count{0}; - JLOG(m_journal.debug()) << "TaggedCache (" << m_name << ") lock stats," - << ", canonicalize, call_count = " << call_count++; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "canonicalize, call_count = " << ++call_count << std::endl; auto cit = m_cache.find(key); if (cit == m_cache.end()) @@ -575,11 +578,12 @@ TaggedCache< KeyEqual, Mutex>::fetch(key_type const& key) { - static unsigned long call_count{0}; - JLOG(m_journal.debug()) << "TaggedCache (" << m_name << ") lock stats," - << ", fetch, call_count = " << call_count++; - std::lock_guard l(m_mutex); + + static unsigned long call_count{0}; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "fetch, call_count = " << ++call_count << std::endl; + auto ret = initialFetch(key, l); if (!ret) ++m_misses; @@ -646,11 +650,12 @@ TaggedCache< Mutex>::insert(key_type const& key) -> std::enable_if_t { - static unsigned long call_count{0}; - JLOG(m_journal.debug()) << "TaggedCache (" << m_name << ") lock stats," - << ", insert, call_count = " << call_count++; - std::lock_guard lock(m_mutex); + + static unsigned long call_count{0}; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "insert, call_count = " << ++call_count << std::endl; + clock_type::time_point const now(m_clock.now()); auto [it, inserted] = m_cache.emplace( std::piecewise_construct, @@ -734,14 +739,15 @@ TaggedCache< KeyEqual, Mutex>::getKeys() const -> std::vector { - static unsigned long call_count{0}; - JLOG(m_journal.debug()) << "TaggedCache (" << m_name << ") lock stats," - << ", getKeys, call_count = " << call_count++; - std::vector v; { std::lock_guard lock(m_mutex); + + static unsigned long call_count{0}; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "getKeys, call_count = " << ++call_count << std::endl; + v.reserve(m_cache.size()); for (auto const& _ : m_cache) v.push_back(_.first); @@ -798,12 +804,13 @@ TaggedCache< KeyEqual, Mutex>::fetch(key_type const& digest, Handler const& h) { - static unsigned long call_count{0}; - JLOG(m_journal.debug()) << "TaggedCache (" << m_name << ") lock stats," - << ", fetch, call_count = " << call_count++; - { std::lock_guard l(m_mutex); + + static unsigned long call_count{0}; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "fetch, call_count = " << ++call_count << std::endl; + if (auto ret = initialFetch(digest, l)) return ret; } @@ -933,9 +940,9 @@ TaggedCache< int mapRemovals = 0; static unsigned long call_count{0}; - JLOG(m_journal.debug()) - << "TaggedCache (" << m_name << ") lock stats," - << ", sweep-KVCache, call_count = " << call_count++; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "sweep-KVCache, call_count = " << ++call_count + << std::endl; // Keep references to all the stuff we sweep // so that we can destroy them outside the lock. @@ -1027,9 +1034,8 @@ TaggedCache< int mapRemovals = 0; static unsigned long call_count{0}; - JLOG(m_journal.debug()) - << "TaggedCache (" << m_name << ") lock stats," - << ", sweep-KCache, call_count = " << call_count++; + std::cout << "TaggedCache (" << m_name << ") lock stats, " + << "sweep-KCache, call_count = " << ++call_count << std::endl; // Keep references to all the stuff we sweep // so that we can destroy them outside the lock. diff --git a/src/xrpld/app/ledger/LedgerHistory.cpp b/src/xrpld/app/ledger/LedgerHistory.cpp index ab0a57de1c..f5cfd9a9d5 100644 --- a/src/xrpld/app/ledger/LedgerHistory.cpp +++ b/src/xrpld/app/ledger/LedgerHistory.cpp @@ -25,6 +25,8 @@ #include #include +#include + namespace ripple { // FIXME: Need to clean up ledgers by index at some point @@ -56,10 +58,6 @@ LedgerHistory::insert( std::shared_ptr const& ledger, bool validated) { - static unsigned long call_count{0}; - JLOG(j_.debug()) << "LedgerHistory lock stats," - << ", insert, call_count = " << call_count++; - if (!ledger->isImmutable()) LogicError("mutable Ledger in insert"); @@ -69,6 +67,10 @@ LedgerHistory::insert( std::unique_lock sl(m_ledgers_by_hash.peekMutex()); + static unsigned long call_count{0}; + std::cout << "LedgerHistory lock stats, " + << "insert, call_count = " << ++call_count << std::endl; + bool const alreadyHad = m_ledgers_by_hash.canonicalize_replace_cache( ledger->info().hash, ledger); if (validated) @@ -80,11 +82,12 @@ LedgerHistory::insert( LedgerHash LedgerHistory::getLedgerHash(LedgerIndex index) { - static unsigned long call_count{0}; - JLOG(j_.debug()) << "LedgerHistory lock stats," - << ", getLedgerHash, call_count = " << call_count++; - std::unique_lock sl(m_ledgers_by_hash.peekMutex()); + + static unsigned long call_count{0}; + std::cout << "LedgerHistory lock stats, " + << "getLedgerHash, call_count = " << ++call_count << std::endl; + if (auto it = mLedgersByIndex.find(index); it != mLedgersByIndex.end()) return it->second; return {}; @@ -93,12 +96,14 @@ LedgerHistory::getLedgerHash(LedgerIndex index) std::shared_ptr LedgerHistory::getLedgerBySeq(LedgerIndex index) { - static unsigned long call_count{0}; - JLOG(j_.debug()) << "LedgerHistory lock stats," - << ", getLedgerBySeq, call_count = " << call_count++; - { std::unique_lock sl(m_ledgers_by_hash.peekMutex()); + + static unsigned long call_count{0}; + std::cout << "LedgerHistory lock stats, " + << "getLedgerBySeq, call_count = " << ++call_count + << std::endl; + auto it = mLedgersByIndex.find(index); if (it != mLedgersByIndex.end()) @@ -547,11 +552,12 @@ LedgerHistory::validatedLedger( bool LedgerHistory::fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash) { - static unsigned long call_count{0}; - JLOG(j_.debug()) << "LedgerHistory lock stats," - << ", fixIndex, call_count = " << call_count++; - std::unique_lock sl(m_ledgers_by_hash.peekMutex()); + + static unsigned long call_count{0}; + std::cout << "LedgerHistory lock stats, " + << "fixIndex, call_count = " << ++call_count << std::endl; + auto it = mLedgersByIndex.find(ledgerIndex); if ((it != mLedgersByIndex.end()) && (it->second != ledgerHash))