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

RPC to clear stats #1625

Merged
merged 3 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions nano/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4136,6 +4136,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 @@ -2972,14 +2972,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 @@ -4259,6 +4268,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 @@ -190,6 +190,7 @@ class rpc_handler : public std::enable_shared_from_this<nano::rpc_handler>
void search_pending_all ();
void send ();
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 @@ -381,6 +381,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 @@ -426,6 +432,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