diff --git a/cmake/Hunter/config.cmake b/cmake/Hunter/config.cmake index fb69acb6ea..a7c6955961 100644 --- a/cmake/Hunter/config.cmake +++ b/cmake/Hunter/config.cmake @@ -105,6 +105,12 @@ hunter_config( SECP256K1_ENABLE_MODULE_RECOVERY=ON ) +hunter_config( + libp2p + URL https://github.com/libp2p/cpp-libp2p/archive/66764acb294517f8249aea6d63c6e6cc0be5686f.tar.gz + SHA1 45b73da05e1b59f46b9f4cb39a24c485ee6d5ba1 +) + hunter_config( erasure_coding_crust # VERSION 0.0.8 diff --git a/core/telemetry/impl/service_impl.cpp b/core/telemetry/impl/service_impl.cpp index 3c0a9b4d7e..77dc66f8d3 100644 --- a/core/telemetry/impl/service_impl.cpp +++ b/core/telemetry/impl/service_impl.cpp @@ -23,6 +23,7 @@ namespace rapidjson { #include #include #include +#include #include "common/uri.hpp" #include "telemetry/impl/connection_impl.hpp" @@ -449,21 +450,16 @@ namespace kagome::telemetry { rapidjson::Value payload(rapidjson::kObjectType); - rapidjson::Value bandwidth_down, bandwidth_up, peers_count; - auto active_peers = peer_manager_->activePeersNumber(); - // we are not actually measuring bandwidth. the following will just let us - // see the history of active peers count change in the telemetry UI - auto peers_to_bandwidth = active_peers * 1'000'000; - // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions) - bandwidth_down.SetInt(peers_to_bandwidth); - // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions) - bandwidth_up.SetInt(peers_to_bandwidth); - // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions) - peers_count.SetInt(active_peers); + rapidjson::Value peers_count; + peers_count.SetUint(peer_manager_->activePeersNumber()); + auto bandwidth = getBandwidth(); + rapidjson::Value upBandwidth, downBandwidth; + downBandwidth.SetUint64(bandwidth.down); + upBandwidth.SetUint64(bandwidth.up); // fields order is preserved the same way substrate orders it - payload.AddMember("bandwidth_download", bandwidth_down, allocator) - .AddMember("bandwidth_upload", bandwidth_up, allocator) + payload.AddMember("bandwidth_download", downBandwidth, allocator) + .AddMember("bandwidth_upload", upBandwidth, allocator) .AddMember("msg", str_val("system.interval"), allocator) .AddMember("peers", peers_count, allocator); @@ -486,4 +482,44 @@ namespace kagome::telemetry { bool TelemetryServiceImpl::isEnabled() const { return enabled_; } + + TelemetryServiceImpl::Bandwidth TelemetryServiceImpl::getBandwidth() { + if (not previous_bandwidth_calculated_) { + previous_bandwidth_calculated_ = + std::chrono::high_resolution_clock::now(); + } + + auto calculateBandwidth = [](uint64_t &previousBytes, + uint64_t totalBytes, + auto &bandwidth, + const std::chrono::seconds &timeElapsed) { + const auto bytesDiff = totalBytes - previousBytes; + if (const auto secondsElapsed = timeElapsed.count(); secondsElapsed > 0) { + bandwidth = bytesDiff / secondsElapsed; + } else { + bandwidth = bytesDiff; + } + previousBytes = totalBytes; + }; + + const auto currentTime = std::chrono::high_resolution_clock::now(); + const auto timeElapsed = std::chrono::duration_cast( + currentTime - *previous_bandwidth_calculated_); + + Bandwidth bandwidth; + const auto &bytesCounter = libp2p::transport::ByteCounter::getInstance(); + calculateBandwidth(previous_bytes_read_, + bytesCounter.getBytesRead(), + bandwidth.down, + timeElapsed); + + calculateBandwidth(previous_bytes_written_, + bytesCounter.getBytesWritten(), + bandwidth.up, + timeElapsed); + + previous_bandwidth_calculated_ = currentTime; + + return bandwidth; + } } // namespace kagome::telemetry diff --git a/core/telemetry/impl/service_impl.hpp b/core/telemetry/impl/service_impl.hpp index 9f9b91ab26..192c1a38f7 100644 --- a/core/telemetry/impl/service_impl.hpp +++ b/core/telemetry/impl/service_impl.hpp @@ -88,6 +88,12 @@ namespace kagome::telemetry { void stop(); private: + /// structure to store last calculated bandwidth values + struct Bandwidth { + uint64_t down{0}; + uint64_t up{0}; + }; + /// parse telemetry endpoints from chain specification std::vector chainSpecEndpoints() const; @@ -100,6 +106,9 @@ namespace kagome::telemetry { /// produces and sends system health notifications void delayedNotificationsRoutine(); + /// calculates and returns current bandwidth values + Bandwidth getBandwidth(); + /** * Constructs the main and immutable part of JSON to be serialized later as * greeting message on new telemetry connections. @@ -169,6 +178,11 @@ namespace kagome::telemetry { std::string genesis_hash_; std::shared_ptr message_pool_; bool was_synchronized_ = false; + + uint64_t previous_bytes_read_{0}; + uint64_t previous_bytes_written_{0}; + std::optional + previous_bandwidth_calculated_; }; } // namespace kagome::telemetry