Skip to content

Commit

Permalink
nano::elapse () helper
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Feb 12, 2024
1 parent fe44c07 commit 0b5ce4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
8 changes: 2 additions & 6 deletions nano/lib/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,29 +281,25 @@ void nano::stats::run_one (std::unique_lock<std::shared_mutex> & lock)
debug_assert (!mutex.try_lock ());
debug_assert (lock.owns_lock ());

auto now = std::chrono::steady_clock::now (); // Only sample clock if necessary as this impacts node performance due to frequent usage

// TODO: Replace with a proper std::chrono time
std::time_t time = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now ());
tm local_tm = *localtime (&time);

// Counters
if (config.log_counters_interval.count () > 0)
{
if (nano::elapsed (log_last_count_writeout, config.log_counters_interval))
if (nano::elapse (log_last_count_writeout, config.log_counters_interval))
{
log_counters_impl (log_count, local_tm);
log_last_count_writeout = now;
}
}

// Samples
if (config.log_samples_interval.count () > 0)
{
if (nano::elapsed (log_last_sample_writeout, config.log_samples_interval))
if (nano::elapse (log_last_sample_writeout, config.log_samples_interval))
{
log_samples_impl (log_sample, local_tm);
log_last_sample_writeout = now;
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,16 @@ constexpr TARGET_TYPE narrow_cast (SOURCE_TYPE const & val)

// Issue #3748
void sort_options_description (const boost::program_options::options_description & source, boost::program_options::options_description & target);
}

/*
* Clock utilities
*/
namespace nano
{
/**
* Steady clock should always be used for measuring time intervals
*/
using clock = std::chrono::steady_clock;

/**
Expand All @@ -214,6 +223,22 @@ bool elapsed (nano::clock::time_point const & last, Duration const & duration)
{
return elapsed (last, duration, nano::clock::now ());
}

/**
* Check whether time elapsed since `last` is greater than `duration` and update `last` if true
* Force usage of steady clock
*/
template <typename Duration>
bool elapse (nano::clock::time_point & last, Duration const & duration)
{
auto now = nano::clock::now ();
if (last + duration < now)
{
last = now;
return true;
}
return false;
}
}

namespace nano::util
Expand Down

0 comments on commit 0b5ce4e

Please sign in to comment.