Skip to content

Commit

Permalink
RPC to clear stats (#1625)
Browse files Browse the repository at this point in the history
* RPC to clear stats, and seconds since last clear

* Add rpc.stats_clear tests

* Add status check
  • Loading branch information
cryptocode authored Jan 29, 2019
1 parent 9106483 commit c5d943a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
22 changes: 22 additions & 0 deletions nano/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4168,6 +4168,28 @@ TEST (rpc, node_id_delete)
ASSERT_NE (node_id.pub.to_string (), system.nodes[0]->node_id.pub.to_string ());
}

TEST (rpc, stats_clear)
{
nano::system system (24000, 1);
nano::keypair key;
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
rpc.start ();
system.nodes[0]->stats.inc (nano::stat::type::ledger, nano::stat::dir::in);
ASSERT_EQ (1, system.nodes[0]->stats.count (nano::stat::type::ledger, nano::stat::dir::in));
boost::property_tree::ptree request;
request.put ("action", "stats_clear");
test_response response (request, rpc, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
std::string success (response.json.get<std::string> ("success"));
ASSERT_TRUE (success.empty ());
ASSERT_EQ (0, system.nodes[0]->stats.count (nano::stat::type::ledger, nano::stat::dir::in));
ASSERT_LE (system.nodes[0]->stats.last_reset ().count (), 5);
}

TEST (rpc, uptime)
{
nano::system system (24000, 1);
Expand Down
15 changes: 14 additions & 1 deletion nano/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3100,14 +3100,23 @@ void nano::rpc_handler::stats ()
}
if (!ec)
{
response (*static_cast<boost::property_tree::ptree *> (sink->to_object ()));
auto stat_tree_l (*static_cast<boost::property_tree::ptree *> (sink->to_object ()));
stat_tree_l.put ("stat_duration_seconds", node.stats.last_reset ().count ());
response (stat_tree_l);
}
else
{
response_errors ();
}
}

void nano::rpc_handler::stats_clear ()
{
node.stats.clear ();
response_l.put ("success", "");
response (response_l);
}

void nano::rpc_handler::stop ()
{
rpc_control_impl ();
Expand Down Expand Up @@ -4450,6 +4459,10 @@ void nano::rpc_handler::process_request ()
{
stats ();
}
else if (action == "stats_clear")
{
stats_clear ();
}
else if (action == "stop")
{
stop ();
Expand Down
1 change: 1 addition & 0 deletions nano/node/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class rpc_handler : public std::enable_shared_from_this<nano::rpc_handler>
void send ();
void sign ();
void stats ();
void stats_clear ();
void stop ();
void unchecked ();
void unchecked_clear ();
Expand Down
14 changes: 14 additions & 0 deletions nano/node/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ void nano::stat::update (uint32_t key_a, uint64_t value)
}
}

std::chrono::seconds nano::stat::last_reset ()
{
std::unique_lock<std::mutex> lock (stat_mutex);
auto now (std::chrono::steady_clock::now ());
return std::chrono::duration_cast<std::chrono::seconds> (now - timestamp);
}

void nano::stat::clear ()
{
std::unique_lock<std::mutex> lock (stat_mutex);
entries.clear ();
timestamp = std::chrono::steady_clock::now ();
}

std::string nano::stat::type_to_string (uint32_t key)
{
auto type = static_cast<stat::type> (key >> 16 & 0x000000ff);
Expand Down
9 changes: 9 additions & 0 deletions nano/node/stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ class stat
return get_entry (key_of (type, detail, dir))->counter.value;
}

/** Returns the number of seconds since clear() was last called, or node startup if it's never called. */
std::chrono::seconds last_reset ();

/** Clear all stats */
void clear ();

/** Log counters to the given log link */
void log_counters (stat_log_sink & sink);

Expand Down Expand Up @@ -431,6 +437,9 @@ class stat
/** Unlocked implementation of log_samples() to avoid using recursive locking */
void log_samples_impl (stat_log_sink & sink);

/** Time of last clear() call */
std::chrono::steady_clock::time_point timestamp{ std::chrono::steady_clock::now () };

/** Configuration deserialized from config.json */
nano::stat_config config;

Expand Down

0 comments on commit c5d943a

Please sign in to comment.