From 02f3e48eaadde1cc33a7382a12c2c5adbdd6a5d2 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 29 Jul 2014 16:35:03 +0000 Subject: [PATCH] make performance counters able to blend in values, reducing a peer_connection -> session dependency on a sliding average state (this can be used for disk stats too) --- include/libtorrent/aux_/session_impl.hpp | 10 ---------- include/libtorrent/aux_/session_interface.hpp | 2 -- include/libtorrent/performance_counters.hpp | 1 + src/peer_connection.cpp | 5 +---- src/performance_counters.cpp | 16 ++++++++++++++++ src/session_stats.cpp | 5 +++++ 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 8e82c0162..4a682f4e1 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -409,12 +409,6 @@ namespace libtorrent void set_port_filter(port_filter const& f); port_filter const& get_port_filter() const; - void request_latency_sample(int us) - { - m_request_latency.add_sample(us); - m_stats_counters.set_value(counters::request_latency, m_request_latency.mean()); - } - // TODO: move the login info into the tracker_request object void queue_tracker_request(tracker_request& req , std::string login, boost::weak_ptr c @@ -1224,10 +1218,6 @@ namespace libtorrent counters m_stats_counters; - // the sliding average of the latency (in microseconds) from an - // incoming request to sending the data back over the socket - sliding_average<50> m_request_latency; - #ifdef TORRENT_UPNP_LOGGING std::ofstream m_upnp_log; #endif diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index eec6a626b..0139dda0f 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -241,8 +241,6 @@ namespace libtorrent { namespace aux virtual int peak_up_rate() const = 0; - virtual void request_latency_sample(int us) = 0; - enum torrent_list_index { // this is the set of (subscribed) torrents that have changed diff --git a/include/libtorrent/performance_counters.hpp b/include/libtorrent/performance_counters.hpp index 485123915..d4b6ee01b 100644 --- a/include/libtorrent/performance_counters.hpp +++ b/include/libtorrent/performance_counters.hpp @@ -388,6 +388,7 @@ namespace libtorrent boost::int64_t operator[](int i) const; void set_value(int c, boost::int64_t value); + void blend_stats_counter(int c, boost::int64_t value, int ratio); private: diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 89fd4e443..7b5a292c2 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -5189,10 +5189,7 @@ namespace libtorrent , r.piece, r.start, r.length); #endif - // TODO: 3 it would be nice if the sliding_average state required - // for this would be baked into the counters object, in which case this - // would not be a session_impl dependency. - m_ses.request_latency_sample(disk_rtt); + m_counters.blend_stats_counter(counters::request_latency, disk_rtt, 5); // we probably just pulled this piece into the cache. // if it's rare enough to make it into the suggested piece diff --git a/src/performance_counters.cpp b/src/performance_counters.cpp index 7687008a5..bcdf434e7 100644 --- a/src/performance_counters.cpp +++ b/src/performance_counters.cpp @@ -71,6 +71,22 @@ namespace libtorrent { return m_stats_counter[c] += value; } + // ratio is a vaue between 0 and 100 representing the percentage the value + // is blended in at. + void counters::blend_stats_counter(int c, boost::int64_t value, int ratio) + { + TORRENT_ASSERT(c >= 0); + TORRENT_ASSERT(c < num_counters); + TORRENT_ASSERT(ratio >= 0); + TORRENT_ASSERT(ratio <= 100); + + TORRENT_ASSERT(num_stats_counters); + + // TODO: 2 to make this thread safe, use compare_exchange_weak + boost::uint64_t current = m_stats_counter[c]; + m_stats_counter[c] = (current * (100-ratio) + value * ratio) / 100; + } + void counters::set_value(int c, boost::int64_t value) { TORRENT_ASSERT(c >= 0); diff --git a/src/session_stats.cpp b/src/session_stats.cpp index 6624e5741..cbc694e8a 100644 --- a/src/session_stats.cpp +++ b/src/session_stats.cpp @@ -38,6 +38,8 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { + // TODO: 3 this function could just use the static metrics array + // instead of taking it as an argument. Also, document it int find_metric_idx(std::vector const& metrics , char const* name) { @@ -48,6 +50,9 @@ namespace libtorrent return i->value_index; } + // TODO: 3 the type of counter does not need to be stored in this array. + // when the user asks for the list of counters, that field could be + // generated based on the range of the counter index. #define METRIC(category, name, type) { #category "." #name, counters:: name, stats_metric:: type}, const static stats_metric metrics[] = {