Skip to content
Permalink
Browse files

New interactive daemon command 'print_net_stats': Global traffic stats

  • Loading branch information...
rbrunner7 committed Mar 21, 2019
1 parent f2f725d commit 3b904182b52e7e58bea69dae1f20f89583fc7d42
@@ -66,6 +66,8 @@ class network_throttle : public i_network_throttle {
network_time_seconds m_last_sample_time; // time of last history[0] - so we know when to rotate the buffer
network_time_seconds m_start_time; // when we were created
bool m_any_packet_yet; // did we yet got any packet to count
uint64_t m_total_packets;
uint64_t m_total_bytes;

std::string m_name; // my name for debug and logs
std::string m_nameshort; // my name for debug and logs (used in log file name)
@@ -95,6 +97,7 @@ class network_throttle : public i_network_throttle {
virtual size_t get_recommended_size_of_planned_transport() const; ///< what should be the size (bytes) of next data block to be transported
virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const; ///< ditto, but for given windows time frame
virtual double get_current_speed() const;
virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const;

private:
virtual network_time_seconds time_to_slot(network_time_seconds t) const { return std::floor( t ); } // convert exact time eg 13.7 to rounded time for slot number in history 13
@@ -152,7 +152,8 @@ class i_network_throttle {
virtual size_t get_recommended_size_of_planned_transport() const =0; // what should be the recommended limit of data size that we can transport over current network_throttle in near future

virtual double get_time_seconds() const =0; // a timer
virtual void logger_handle_net(const std::string &filename, double time, size_t size)=0;
virtual void logger_handle_net(const std::string &filename, double time, size_t size)=0;
virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const =0;


};
@@ -136,6 +136,8 @@ network_throttle::network_throttle(const std::string &nameshort, const std::stri
m_target_speed = 16 * 1024; // other defaults are probably defined in the command-line parsing code when this class is used e.g. as main global throttle
m_last_sample_time = 0;
m_history.resize(m_window_size);
m_total_packets = 0;
m_total_bytes = 0;
}

void network_throttle::set_name(const std::string &name)
@@ -192,6 +194,8 @@ void network_throttle::_handle_trafic_exact(size_t packet_size, size_t orginal_s
calculate_times_struct cts ; calculate_times(packet_size, cts , false, -1);
calculate_times_struct cts2; calculate_times(packet_size, cts2, false, 5);
m_history.front().m_size += packet_size;
m_total_packets++;
m_total_bytes += packet_size;

std::ostringstream oss; oss << "["; for (auto sample: m_history) oss << sample.m_size << " "; oss << "]" << std::ends;
std::string history_str = oss.str();
@@ -352,6 +356,12 @@ double network_throttle::get_current_speed() const {
return bytes_transferred / ((m_history.size() - 1) * m_slot_size);
}

void network_throttle::get_stats(uint64_t &total_packets, uint64_t &total_bytes) const {
total_packets = m_total_packets;
total_bytes = m_total_bytes;
}


} // namespace
} // namespace

@@ -80,6 +80,7 @@ using namespace epee;
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
#include <boost/format.hpp>
#include <openssl/sha.h>

#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -1063,4 +1064,26 @@ std::string get_nix_version_display_string()
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
return std::string(buffer);
}

std::string get_human_readable_bytes(uint64_t bytes)
{
// Use 1024 for "kilo", 1024*1024 for "mega" and so on instead of the more modern and standard-conforming
// 1000, 1000*1000 and so on, to be consistent with other Monero code that also uses base 2 units
static const char *SIZES[] = { "B", "kB", "MB", "GB", "TB" };
if (bytes < 1024)
{
return boost::lexical_cast<std::string>(bytes) + " " + SIZES[0];
}
uint64_t div = 0;
uint64_t rem = 0;
while (bytes >= 1024 && div < 5)
{
rem = bytes % 1024;
div++;
bytes /= 1024;
}
double bytes_d = (double)bytes + (double)rem / 1024.0;
return (boost::format("%.2f ") % bytes_d).str() + SIZES[div];
}

}
@@ -244,4 +244,6 @@ namespace tools
void closefrom(int fd);

std::string get_human_readable_timestamp(uint64_t ts);

std::string get_human_readable_bytes(uint64_t bytes);
}
@@ -127,6 +127,13 @@ bool t_command_parser_executor::print_connections(const std::vector<std::string>
return m_executor.print_connections();
}

bool t_command_parser_executor::print_net_stats(const std::vector<std::string>& args)
{
if (!args.empty()) return false;

return m_executor.print_net_stats();
}

bool t_command_parser_executor::print_blockchain_info(const std::vector<std::string>& args)
{
if(!args.size())
@@ -148,6 +148,8 @@ class t_command_parser_executor final
bool prune_blockchain(const std::vector<std::string>& args);

bool check_blockchain_pruning(const std::vector<std::string>& args);

bool print_net_stats(const std::vector<std::string>& args);
};

} // namespace daemonize
@@ -77,6 +77,11 @@ t_command_server::t_command_server(
, std::bind(&t_command_parser_executor::print_connections, &m_parser, p::_1)
, "Print the current connections."
);
m_command_lookup.set_handler(
"print_net_stats"
, std::bind(&t_command_parser_executor::print_net_stats, &m_parser, p::_1)
, "Print network statistics."
);
m_command_lookup.set_handler(
"print_bc"
, std::bind(&t_command_parser_executor::print_blockchain_info, &m_parser, p::_1)
@@ -627,6 +627,75 @@ bool t_rpc_command_executor::print_connections() {
return true;
}

bool t_rpc_command_executor::print_net_stats()
{
cryptonote::COMMAND_RPC_GET_NET_STATS::request net_stats_req;
cryptonote::COMMAND_RPC_GET_NET_STATS::response net_stats_res;
cryptonote::COMMAND_RPC_GET_LIMIT::request limit_req;
cryptonote::COMMAND_RPC_GET_LIMIT::response limit_res;

std::string fail_message = "Unsuccessful";

if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(net_stats_req, net_stats_res, "get_net_stats", fail_message.c_str()))
{
return true;
}
if (!m_rpc_client->json_rpc_request(limit_req, limit_res, "get_limit", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_net_stats(net_stats_req, net_stats_res) || net_stats_res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << make_error(fail_message, net_stats_res.status);
return true;
}
if (!m_rpc_server->on_get_limit(limit_req, limit_res) || limit_res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << make_error(fail_message, limit_res.status);
return true;
}
}

uint64_t seconds = (uint64_t)time(NULL) - net_stats_res.start_time;
uint64_t average = seconds > 0 ? net_stats_res.total_bytes_in / seconds : 0;
uint64_t limit = limit_res.limit_down * 1024; // convert to bytes, as limits are always kB/s
double percent = (double)average / (double)limit * 100.0;
tools::success_msg_writer() << boost::format("Received %u bytes (%s) in %u packets, average %s/s = %.2f%% of the limit of %s/s")
% net_stats_res.total_bytes_in
% tools::get_human_readable_bytes(net_stats_res.total_bytes_in)
% net_stats_res.total_packets_in
% tools::get_human_readable_bytes(average)
% percent
% tools::get_human_readable_bytes(limit);

average = seconds > 0 ? net_stats_res.total_bytes_out / seconds : 0;
limit = limit_res.limit_up * 1024;
percent = (double)average / (double)limit * 100.0;
tools::success_msg_writer() << boost::format("Sent %u bytes (%s) in %u packets, average %s/s = %.2f%% of the limit of %s/s")
% net_stats_res.total_bytes_out
% tools::get_human_readable_bytes(net_stats_res.total_bytes_out)
% net_stats_res.total_packets_out
% tools::get_human_readable_bytes(average)
% percent
% tools::get_human_readable_bytes(limit);

uint64_t total_bytes = net_stats_res.total_bytes_in + net_stats_res.total_bytes_out;
double percent_in = (double)net_stats_res.total_bytes_in / (double)total_bytes * 100.0;
double percent_out = 100.0 - percent_in;
tools::success_msg_writer() << boost::format("Total %u bytes (%s), %.2f%% received / %.2f%% sent")
% total_bytes
% tools::get_human_readable_bytes(total_bytes)
% percent_in
% percent_out;

return true;
}

bool t_rpc_command_executor::print_blockchain_info(uint64_t start_block_index, uint64_t end_block_index) {
cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request req;
cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response res;
@@ -160,6 +160,8 @@ class t_rpc_command_executor final {
bool prune_blockchain();

bool check_blockchain_pruning();

bool print_net_stats();
};

} // namespace daemonize
@@ -260,6 +260,35 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_get_net_stats(const COMMAND_RPC_GET_NET_STATS::request& req, COMMAND_RPC_GET_NET_STATS::response& res, const connection_context *ctx)
{
PERF_TIMER(on_get_net_stats);
// No bootstrap daemon check: Only ever get stats about local server
const bool restricted = m_restricted && ctx;
if (restricted)
{
res.start_time = 0;
res.total_packets_in = 0;
res.total_bytes_in = 0;
res.total_packets_out = 0;
res.total_bytes_out = 0;
}
else
{
res.start_time = (uint64_t)m_core.get_start_time();
{
CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_in);
epee::net_utils::network_throttle_manager::get_global_throttle_in().get_stats(res.total_packets_in, res.total_bytes_in);
}
{
CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_out);
epee::net_utils::network_throttle_manager::get_global_throttle_out().get_stats(res.total_packets_out, res.total_bytes_out);
}
}
res.status = CORE_RPC_STATUS_OK;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
class pruned_transaction {
transaction& tx;
public:
@@ -1742,6 +1771,23 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_get_net_stats_json(const COMMAND_RPC_GET_NET_STATS::request& req, COMMAND_RPC_GET_NET_STATS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
{
PERF_TIMER(on_get_net_stats);
// No bootstrap daemon check: Only ever get stats about local server
res.start_time = (uint64_t)m_core.get_start_time();
{
CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_in);
epee::net_utils::network_throttle_manager::get_global_throttle_in().get_stats(res.total_packets_in, res.total_bytes_in);
}
{
CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_out);
epee::net_utils::network_throttle_manager::get_global_throttle_out().get_stats(res.total_packets_out, res.total_bytes_out);
}
res.status = CORE_RPC_STATUS_OK;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_hard_fork_info(const COMMAND_RPC_HARD_FORK_INFO::request& req, COMMAND_RPC_HARD_FORK_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
{
PERF_TIMER(on_hard_fork_info);
@@ -115,6 +115,7 @@ namespace cryptonote
MAP_URI_AUTO_JON2_IF("/stop_daemon", on_stop_daemon, COMMAND_RPC_STOP_DAEMON, !m_restricted)
MAP_URI_AUTO_JON2("/get_info", on_get_info, COMMAND_RPC_GET_INFO)
MAP_URI_AUTO_JON2("/getinfo", on_get_info, COMMAND_RPC_GET_INFO)
MAP_URI_AUTO_JON2("/get_net_stats", on_get_net_stats, COMMAND_RPC_GET_NET_STATS)
MAP_URI_AUTO_JON2("/get_limit", on_get_limit, COMMAND_RPC_GET_LIMIT)
MAP_URI_AUTO_JON2_IF("/set_limit", on_set_limit, COMMAND_RPC_SET_LIMIT, !m_restricted)
MAP_URI_AUTO_JON2_IF("/out_peers", on_out_peers, COMMAND_RPC_OUT_PEERS, !m_restricted)
@@ -147,6 +148,7 @@ namespace cryptonote
MAP_JON_RPC_WE("getblock", on_get_block, COMMAND_RPC_GET_BLOCK)
MAP_JON_RPC_WE_IF("get_connections", on_get_connections, COMMAND_RPC_GET_CONNECTIONS, !m_restricted)
MAP_JON_RPC_WE("get_info", on_get_info_json, COMMAND_RPC_GET_INFO)
MAP_JON_RPC_WE_IF("get_net_stats", on_get_net_stats_json, COMMAND_RPC_GET_NET_STATS, !m_restricted)
MAP_JON_RPC_WE("hard_fork_info", on_hard_fork_info, COMMAND_RPC_HARD_FORK_INFO)
MAP_JON_RPC_WE_IF("set_bans", on_set_bans, COMMAND_RPC_SETBANS, !m_restricted)
MAP_JON_RPC_WE_IF("get_bans", on_get_bans, COMMAND_RPC_GETBANS, !m_restricted)
@@ -179,6 +181,7 @@ namespace cryptonote
bool on_get_outs_bin(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req, COMMAND_RPC_GET_OUTPUTS_BIN::response& res, const connection_context *ctx = NULL);
bool on_get_outs(const COMMAND_RPC_GET_OUTPUTS::request& req, COMMAND_RPC_GET_OUTPUTS::response& res, const connection_context *ctx = NULL);
bool on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, const connection_context *ctx = NULL);
bool on_get_net_stats(const COMMAND_RPC_GET_NET_STATS::request& req, COMMAND_RPC_GET_NET_STATS::response& res, const connection_context *ctx = NULL);
bool on_save_bc(const COMMAND_RPC_SAVE_BC::request& req, COMMAND_RPC_SAVE_BC::response& res, const connection_context *ctx = NULL);
bool on_get_peer_list(const COMMAND_RPC_GET_PEER_LIST::request& req, COMMAND_RPC_GET_PEER_LIST::response& res, const connection_context *ctx = NULL);
bool on_set_log_hash_rate(const COMMAND_RPC_SET_LOG_HASH_RATE::request& req, COMMAND_RPC_SET_LOG_HASH_RATE::response& res, const connection_context *ctx = NULL);
@@ -212,6 +215,7 @@ namespace cryptonote
bool on_get_block(const COMMAND_RPC_GET_BLOCK::request& req, COMMAND_RPC_GET_BLOCK::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
bool on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
bool on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
bool on_get_net_stats_json(const COMMAND_RPC_GET_NET_STATS::request& req, COMMAND_RPC_GET_NET_STATS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
bool on_hard_fork_info(const COMMAND_RPC_HARD_FORK_INFO::request& req, COMMAND_RPC_HARD_FORK_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
bool on_set_bans(const COMMAND_RPC_SETBANS::request& req, COMMAND_RPC_SETBANS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
bool on_get_bans(const COMMAND_RPC_GETBANS::request& req, COMMAND_RPC_GETBANS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
@@ -1013,6 +1013,39 @@ namespace cryptonote
};


//-----------------------------------------------
struct COMMAND_RPC_GET_NET_STATS
{
struct request_t
{

BEGIN_KV_SERIALIZE_MAP()
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;


struct response_t
{
std::string status;
uint64_t start_time;
uint64_t total_packets_in;
uint64_t total_bytes_in;
uint64_t total_packets_out;
uint64_t total_bytes_out;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(status)
KV_SERIALIZE(start_time)
KV_SERIALIZE(total_packets_in)
KV_SERIALIZE(total_bytes_in)
KV_SERIALIZE(total_packets_out)
KV_SERIALIZE(total_bytes_out)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<response_t> response;
};

//-----------------------------------------------
struct COMMAND_RPC_STOP_MINING
{

0 comments on commit 3b90418

Please sign in to comment.
You can’t perform that action at this time.