diff --git a/nano/core_test/vote_cache.cpp b/nano/core_test/vote_cache.cpp index f94dda0679..7cb543b9c1 100644 --- a/nano/core_test/vote_cache.cpp +++ b/nano/core_test/vote_cache.cpp @@ -36,8 +36,9 @@ nano::keypair create_rep (nano::uint128_t weight) TEST (vote_cache, construction) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; ASSERT_EQ (0, vote_cache.size ()); ASSERT_TRUE (vote_cache.empty ()); auto hash1 = nano::test::random_hash (); @@ -49,8 +50,9 @@ TEST (vote_cache, construction) */ TEST (vote_cache, insert_one_hash) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); auto rep1 = create_rep (7); auto hash1 = nano::test::random_hash (); @@ -79,8 +81,9 @@ TEST (vote_cache, insert_one_hash) */ TEST (vote_cache, insert_one_hash_many_votes) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (7); @@ -114,8 +117,9 @@ TEST (vote_cache, insert_one_hash_many_votes) */ TEST (vote_cache, insert_many_hashes_many_votes) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); // There will be 3 random hashes to vote for auto hash1 = nano::test::random_hash (); @@ -194,8 +198,9 @@ TEST (vote_cache, insert_many_hashes_many_votes) */ TEST (vote_cache, insert_duplicate) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (9); @@ -211,8 +216,9 @@ TEST (vote_cache, insert_duplicate) */ TEST (vote_cache, insert_newer) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (9); @@ -236,8 +242,9 @@ TEST (vote_cache, insert_newer) */ TEST (vote_cache, insert_older) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (9); @@ -259,8 +266,9 @@ TEST (vote_cache, insert_older) */ TEST (vote_cache, erase) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto hash2 = nano::test::random_hash (); @@ -298,10 +306,11 @@ TEST (vote_cache, erase) */ TEST (vote_cache, overfill) { + nano::test::system system; // Create a vote cache with max size set to 1024 nano::vote_cache_config cfg; cfg.max_size = 1024; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); const int count = 16 * 1024; for (int n = 0; n < count; ++n) @@ -324,8 +333,9 @@ TEST (vote_cache, overfill) */ TEST (vote_cache, overfill_entry) { + nano::test::system system; nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::vote_cache vote_cache{ cfg, system.stats }; vote_cache.rep_weight_query = rep_weight_query (); const int count = 1024; auto hash1 = nano::test::random_hash (); @@ -336,4 +346,6 @@ TEST (vote_cache, overfill_entry) vote_cache.vote (vote1->hashes.front (), vote1); } ASSERT_EQ (1, vote_cache.size ()); -} \ No newline at end of file +} + +TEST (vote_cache, \ No newline at end of file diff --git a/nano/lib/stats_enums.hpp b/nano/lib/stats_enums.hpp index 79ef835802..00bc95872f 100644 --- a/nano/lib/stats_enums.hpp +++ b/nano/lib/stats_enums.hpp @@ -63,6 +63,8 @@ enum class detail : uint8_t update, request, broadcast, + cleanup, + top, // processing queue queue, diff --git a/nano/node/node.cpp b/nano/node/node.cpp index aede8116c8..e138f2e2f2 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -182,7 +182,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons history{ config.network_params.voting }, vote_uniquer (block_uniquer), confirmation_height_processor (ledger, write_database_queue, config.conf_height_processor_batch_min_time, config.logging, logger, node_initialized_latch, flags.confirmation_height_processor_mode), - vote_cache{ config.vote_cache }, + vote_cache{ config.vote_cache, stats }, generator{ config, ledger, wallets, vote_processor, history, network, stats, /* non-final */ false }, final_generator{ config, ledger, wallets, vote_processor, history, network, stats, /* final */ true }, active (*this, confirmation_height_processor), diff --git a/nano/node/vote_cache.cpp b/nano/node/vote_cache.cpp index 379ce27a66..f268db043e 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/vote_cache.cpp @@ -113,6 +113,7 @@ std::chrono::steady_clock::time_point nano::vote_cache::entry::last_vote () cons nano::vote_cache::vote_cache (vote_cache_config const & config_a, nano::stats & stats_a) : config{ config_a }, + stats{ stats_a }, cleanup_interval{ config_a.age_cutoff / 2 } { } @@ -128,12 +129,16 @@ void nano::vote_cache::vote (const nano::block_hash & hash, const std::shared_pt auto & cache_by_hash = cache.get (); if (auto existing = cache_by_hash.find (hash); existing != cache_by_hash.end ()) { + stats.inc (nano::stat::type::vote_cache, nano::stat::detail::update); + cache_by_hash.modify (existing, [this, &representative, ×tamp, &rep_weight] (entry & ent) { ent.vote (representative, timestamp, rep_weight, config.max_voters); }); } else { + stats.inc (nano::stat::type::vote_cache, nano::stat::detail::insert); + entry cache_entry{ hash }; cache_entry.vote (representative, timestamp, rep_weight, config.max_voters); @@ -187,6 +192,8 @@ bool nano::vote_cache::erase (const nano::block_hash & hash) std::vector nano::vote_cache::top (const nano::uint128_t & min_tally) { + stats.inc (nano::stat::type::vote_cache, nano::stat::detail::top); + std::vector results; { nano::lock_guard lock{ mutex }; @@ -225,6 +232,8 @@ void nano::vote_cache::cleanup () { debug_assert (!mutex.try_lock ()); + stats.inc (nano::stat::type::vote_cache, nano::stat::detail::cleanup); + auto const cutoff = std::chrono::steady_clock::now () - config.age_cutoff; auto it = cache.begin (); @@ -241,7 +250,7 @@ void nano::vote_cache::cleanup () } } -std::unique_ptr nano::vote_cache::collect_container_info (const std::string & name) +std::unique_ptr nano::vote_cache::collect_container_info (const std::string & name) const { auto composite = std::make_unique (name); composite->add_component (std::make_unique (container_info{ "cache", size (), sizeof (ordered_cache::value_type) })); diff --git a/nano/node/vote_cache.hpp b/nano/node/vote_cache.hpp index 1b814375db..4a353446af 100644 --- a/nano/node/vote_cache.hpp +++ b/nano/node/vote_cache.hpp @@ -91,7 +91,7 @@ class vote_cache final }; public: - explicit vote_cache (vote_cache_config const &); + explicit vote_cache (vote_cache_config const &, nano::stats &); /** * Adds a new vote to cache @@ -126,7 +126,7 @@ class vote_cache final std::vector top (nano::uint128_t const & min_tally); public: // Container info - std::unique_ptr collect_container_info (std::string const & name); + std::unique_ptr collect_container_info (std::string const & name) const; public: /** @@ -136,6 +136,7 @@ class vote_cache final private: // Dependencies vote_cache_config const & config; + nano::stats & stats; private: void cleanup ();