From 1bb84cdbcf155ef05f348a5b130e83a373a71ebe Mon Sep 17 00:00:00 2001 From: Gleb Natapov Date: Thu, 16 May 2019 16:27:47 +0300 Subject: [PATCH] cache_hitrate_calculator: do not copy stats map for each cpu invoke_on_all() copies provided function for each shard it is executed on, so by moving stats map into the capture we copy it for each shard too. Avoid it by putting it into the top level object which is already captured by reference. Message-Id: <20190516132748.6469-2-gleb@scylladb.com> (cherry picked from commit 4517c56a572236f0590624b6898d17aa7e6a895b) --- service/cache_hitrate_calculator.hh | 11 +++++++++++ service/misc_services.cc | 18 +++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/service/cache_hitrate_calculator.hh b/service/cache_hitrate_calculator.hh index 340f91ecb2..f6f13161d7 100644 --- a/service/cache_hitrate_calculator.hh +++ b/service/cache_hitrate_calculator.hh @@ -28,11 +28,22 @@ namespace service { class cache_hitrate_calculator : public seastar::async_sharded_service { + struct stat { + float h = 0; + float m = 0; + stat& operator+=(stat& o) { + h += o.h; + m += o.m; + return *this; + } + }; + seastar::sharded& _db; seastar::sharded& _me; timer _timer; bool _stopped = false; float _diff = 0; + std::unordered_map _rates; future<> _done = make_ready_future(); future recalculate_hitrates(); diff --git a/service/misc_services.cc b/service/misc_services.cc index f0483d6afe..d44426e4ad 100644 --- a/service/misc_services.cc +++ b/service/misc_services.cc @@ -112,16 +112,6 @@ void cache_hitrate_calculator::run_on(size_t master, lowres_clock::duration d) { } future cache_hitrate_calculator::recalculate_hitrates() { - struct stat { - float h = 0; - float m = 0; - stat& operator+=(stat& o) { - h += o.h; - m += o.m; - return *this; - } - }; - auto non_system_filter = [&] (const std::pair>& cf) { return _db.local().find_keyspace(cf.second->schema()->ks_name()).get_replication_strategy().get_type() != locator::replication_strategy_type::local; }; @@ -143,12 +133,13 @@ future cache_hitrate_calculator::recalculate_hitrates() return _db.map_reduce0(cf_to_cache_hit_stats, std::unordered_map(), sum_stats_per_cf).then([this, non_system_filter] (std::unordered_map rates) mutable { _diff = 0; + _rates = std::move(rates); // set calculated rates on all shards - return _db.invoke_on_all([this, rates = std::move(rates), cpuid = engine().cpu_id(), non_system_filter] (database& db) { + return _db.invoke_on_all([this, cpuid = engine().cpu_id(), non_system_filter] (database& db) { sstring gstate; for (auto& cf : db.get_column_families() | boost::adaptors::filtered(non_system_filter)) { - auto it = rates.find(cf.first); - if (it == rates.end()) { // a table may be added before map/reduce compltes and this code runs + auto it = _rates.find(cf.first); + if (it == _rates.end()) { // a table may be added before map/reduce completes and this code runs continue; } stat s = it->second; @@ -171,6 +162,7 @@ future cache_hitrate_calculator::recalculate_hitrates() return make_ready_future<>(); }); }).then([this] { + _rates.clear(); // if max difference during this round is big schedule next recalculate earlier if (_diff < 0.01) { return std::chrono::milliseconds(2000);