Skip to content

Commit

Permalink
Merge branch 'master' into fix/kusama_backed_candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
iceseer authored Sep 30, 2024
2 parents 97ed05a + abae93f commit 7826cea
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
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/libp2p/cpp-libp2p/archive/66764acb294517f8249aea6d63c6e6cc0be5686f.tar.gz
SHA1 45b73da05e1b59f46b9f4cb39a24c485ee6d5ba1
)

hunter_config(
erasure_coding_crust
# VERSION 0.0.8
Expand Down
62 changes: 49 additions & 13 deletions core/telemetry/impl/service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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/bytes_counter.hpp>

#include "common/uri.hpp"
#include "telemetry/impl/connection_impl.hpp"
Expand Down Expand Up @@ -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);

Expand All @@ -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<std::chrono::seconds>(
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
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 {
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::high_resolution_clock::time_point>
previous_bandwidth_calculated_;
};

} // namespace kagome::telemetry

0 comments on commit 7826cea

Please sign in to comment.