Skip to content

Commit

Permalink
Track tcp traffic type
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 11, 2024
1 parent e902b69 commit 0811661
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
8 changes: 6 additions & 2 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ enum class type
_invalid = 0, // Default value, should not be used

test,
traffic_tcp,
error,
message,
block,
ledger,
rollback,
network,
tcp_server,
vote,
vote_processor,
vote_processor_tier,
Expand All @@ -31,11 +29,14 @@ enum class type
http_callback,
ipc,
tcp,
tcp_server,
tcp_channels,
tcp_channels_rejected,
tcp_channels_purge,
tcp_listener,
tcp_listener_rejected,
traffic_tcp,
traffic_tcp_type,
channel,
socket,
confirmation_height,
Expand Down Expand Up @@ -294,6 +295,9 @@ enum class detail
reachout_live,
reachout_cached,

// traffic
generic,

// tcp
tcp_write_drop,
tcp_write_no_socket_drop,
Expand Down
2 changes: 2 additions & 0 deletions nano/node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ add_library(
transport/tcp_server.cpp
transport/tcp_socket.hpp
transport/tcp_socket.cpp
transport/traffic_type.hpp
transport/traffic_type.cpp
transport/transport.hpp
transport/transport.cpp
unchecked_map.cpp
Expand Down
20 changes: 11 additions & 9 deletions nano/node/transport/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,18 @@ void nano::transport::tcp_socket::write_queued_messages ()
return;
}

auto next = send_queue.pop ();
if (!next)
auto maybe_next = send_queue.pop ();
if (!maybe_next)
{
return;
}
auto const & [next, type] = *maybe_next;

set_default_timeout ();

write_in_progress = true;
nano::async_write (raw_socket, next->buffer,
boost::asio::bind_executor (strand, [this_l = shared_from_this (), next /* `next` object keeps buffer in scope */] (boost::system::error_code ec, std::size_t size) {
nano::async_write (raw_socket, next.buffer,
boost::asio::bind_executor (strand, [this_l = shared_from_this (), next /* `next` object keeps buffer in scope */, type] (boost::system::error_code ec, std::size_t size) {
debug_assert (this_l->strand.running_in_this_thread ());

auto node_l = this_l->node_w.lock ();
Expand All @@ -214,12 +215,13 @@ void nano::transport::tcp_socket::write_queued_messages ()
else
{
node_l->stats.add (nano::stat::type::traffic_tcp, nano::stat::detail::all, nano::stat::dir::out, size, /* aggregate all */ true);
node_l->stats.add (nano::stat::type::traffic_tcp_type, to_stat_detail (type), nano::stat::dir::out, size);
this_l->set_last_completion ();
}

if (next->callback)
if (next.callback)
{
next->callback (ec, size);
next.callback (ec, size);
}

if (!ec)
Expand Down Expand Up @@ -436,17 +438,17 @@ bool nano::transport::socket_queue::insert (const buffer_t & buffer, callback_t
return false; // Not queued
}

std::optional<nano::transport::socket_queue::entry> nano::transport::socket_queue::pop ()
auto nano::transport::socket_queue::pop () -> std::optional<result_t>
{
nano::lock_guard<nano::mutex> guard{ mutex };

auto try_pop = [this] (nano::transport::traffic_type type) -> std::optional<entry> {
auto try_pop = [this] (nano::transport::traffic_type type) -> std::optional<result_t> {
auto & que = queues[type];
if (!que.empty ())
{
auto item = que.front ();
que.pop ();
return item;
return std::make_pair (item, type);
}
return std::nullopt;
};
Expand Down
4 changes: 3 additions & 1 deletion nano/node/transport/tcp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ class socket_queue final
};

public:
using result_t = std::pair<entry, nano::transport::traffic_type>;

explicit socket_queue (std::size_t max_size);

bool insert (buffer_t const &, callback_t, nano::transport::traffic_type);
std::optional<entry> pop ();
std::optional<result_t> pop ();
void clear ();
std::size_t size (nano::transport::traffic_type) const;
bool empty () const;
Expand Down
7 changes: 7 additions & 0 deletions nano/node/transport/traffic_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <nano/lib/enum_util.hpp>
#include <nano/node/transport/traffic_type.hpp>

nano::stat::detail nano::transport::to_stat_detail (nano::transport::traffic_type type)
{
return nano::enum_util::cast<nano::stat::detail> (type);
}
4 changes: 4 additions & 0 deletions nano/node/transport/traffic_type.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <nano/lib/stats.hpp>

namespace nano::transport
{
/**
Expand All @@ -10,4 +12,6 @@ enum class traffic_type
generic,
bootstrap, // Ascending bootstrap (asc_pull_ack, asc_pull_req) traffic
};

nano::stat::detail to_stat_detail (traffic_type);
}

0 comments on commit 0811661

Please sign in to comment.