Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct upload and download bandwidth telemetry #2211

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ hunter_config(
SECP256K1_ENABLE_MODULE_RECOVERY=ON
)

hunter_config(
libp2p
URL https://github.com/ErakhtinB/cpp-libp2p/archive/e10b86d6ffe726f39ca6e9a1dfc5826e9249d7e7.tar.gz
SHA1 84e1a72a13ab83baff8afe4c2f327a412563763b
)

hunter_config(
erasure_coding_crust
# VERSION 0.0.8
Expand Down
58 changes: 45 additions & 13 deletions core/telemetry/impl/service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ namespace rapidjson {
#include <libp2p/basic/scheduler/asio_scheduler_backend.hpp>
#include <libp2p/basic/scheduler/scheduler_impl.hpp>
#include <libp2p/multi/multiaddress.hpp>
#include <libp2p/transport/tcp/tcp_connection.hpp>

#include <network/helpers/stream_read_buffer.hpp>
#include "common/uri.hpp"
#include "telemetry/impl/connection_impl.hpp"
#include "telemetry/impl/telemetry_thread_pool.hpp"
Expand Down Expand Up @@ -452,21 +454,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.SetInt(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);

Expand All @@ -489,4 +486,39 @@ namespace kagome::telemetry {
bool TelemetryServiceImpl::isEnabled() const {
return enabled_;
}

TelemetryServiceImpl::Bandwidth TelemetryServiceImpl::getBandwidth() {
if (not previous_bandwidth_calculated_.has_value()) {
previous_bandwidth_calculated_ = std::chrono::high_resolution_clock::now();
}

auto calculateBandwidth = [](uint64_t &previousBytes,
uint64_t totalBytes,
auto &bandwidth,
const std::chrono::seconds &timeElapsed) {
if (timeElapsed.count() > 0) {
bandwidth = (totalBytes - previousBytes) / timeElapsed.count();
} else {
bandwidth = 0;
turuslan marked this conversation as resolved.
Show resolved Hide resolved
}
previousBytes = totalBytes;
};

const auto currentTime = std::chrono::high_resolution_clock::now();
const auto timeElapsed = std::chrono::duration_cast<std::chrono::seconds>(
currentTime - *previous_bandwidth_calculated_);

Bandwidth bandwidth;
const auto totalBytesRead = libp2p::transport::TcpConnection::getBytesRead();
calculateBandwidth(
previous_bytes_read_, totalBytesRead, bandwidth.down, timeElapsed);

const auto totalBytesWritten = libp2p::transport::TcpConnection::getBytesWritten();
calculateBandwidth(
previous_bytes_written_, totalBytesWritten, bandwidth.up, timeElapsed);

previous_bandwidth_calculated_ = currentTime;

return bandwidth;
}
} // namespace kagome::telemetry
14 changes: 14 additions & 0 deletions core/telemetry/impl/service_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ namespace kagome::telemetry {
void stop();

private:
/// structure to store last calculated bandwidth values
struct Bandwidth {
turuslan marked this conversation as resolved.
Show resolved Hide resolved
uint64_t down{0};
uint64_t up{0};
};

/// parse telemetry endpoints from chain specification
std::vector<TelemetryEndpoint> chainSpecEndpoints() const;

Expand All @@ -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.
Expand Down Expand Up @@ -169,6 +178,11 @@ namespace kagome::telemetry {
std::string genesis_hash_;
std::shared_ptr<MessagePool> message_pool_;
bool was_synchronized_ = false;

uint64_t previous_bytes_read_{0};
uint64_t previous_bytes_written_{0};
std::optional<std::chrono::system_clock::time_point>
previous_bandwidth_calculated_;
};

} // namespace kagome::telemetry
Loading