libcruft-util/time.cpp

164 lines
4.0 KiB
C++
Raw Normal View History

2011-08-29 15:28:11 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2011-08-29 15:28:11 +10:00
*
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
2011-08-29 15:28:11 +10:00
*/
#include "time.hpp"
2011-08-29 15:28:11 +10:00
#include "log.hpp"
#include <chrono>
2011-08-29 15:28:11 +10:00
using cruft::delta_clock;
2011-08-29 15:28:11 +10:00
2022-03-22 14:18:01 +11:00
2015-07-23 14:13:09 +10:00
///////////////////////////////////////////////////////////////////////////////
static const uint64_t SECOND = 1'000'000'000UL;
static const uint64_t MILLISECOND = 1'000'000UL;
2013-08-08 10:48:29 +10:00
///////////////////////////////////////////////////////////////////////////////
uintmax_t
cruft::nanoseconds (void)
{
return std::chrono::duration_cast<
std::chrono::duration<uintmax_t, std::nano>
> (
std::chrono::high_resolution_clock::now ().time_since_epoch ()
).count ();
}
2015-07-23 14:13:09 +10:00
///////////////////////////////////////////////////////////////////////////////
2015-01-19 19:11:41 +11:00
delta_clock::delta_clock ():
time { cruft::nanoseconds (), cruft::nanoseconds () }
{ ; }
2015-07-23 14:13:09 +10:00
//-----------------------------------------------------------------------------
std::chrono::nanoseconds
delta_clock::dt (void)
2015-07-23 14:13:09 +10:00
{
time.prev = time.curr;
time.curr = nanoseconds ();
return std::chrono::nanoseconds (time.curr - time.prev);
}
2015-07-23 14:13:09 +10:00
///////////////////////////////////////////////////////////////////////////////
cruft::period_query::period_query (float seconds)
2015-07-23 14:13:09 +10:00
{
2013-09-20 17:33:08 +10:00
m_time.start = nanoseconds ();
m_time.period = static_cast<uint64_t> (seconds * SECOND);
2013-09-20 17:33:08 +10:00
}
2015-07-23 14:13:09 +10:00
//-----------------------------------------------------------------------------
2013-09-20 17:33:08 +10:00
bool
cruft::period_query::poll (void)
2015-07-23 14:13:09 +10:00
{
2013-09-20 17:33:08 +10:00
uint64_t now = nanoseconds ();
uint64_t diff = now - m_time.start;
if (diff < m_time.period)
return false;
m_time.start += diff % m_time.period;
return true;
}
2012-05-01 12:14:38 +10:00
2015-07-23 14:13:09 +10:00
///////////////////////////////////////////////////////////////////////////////
cruft::rate_limiter::rate_limiter (unsigned rate):
2013-08-08 10:48:44 +10:00
m_last (nanoseconds ()),
2014-07-02 15:45:06 +10:00
m_target (SECOND / rate)
2013-08-08 10:48:44 +10:00
{ ; }
2015-07-23 14:13:09 +10:00
//-----------------------------------------------------------------------------
2013-08-08 10:48:44 +10:00
void
cruft::rate_limiter::poll (void)
2015-07-23 14:13:09 +10:00
{
2013-08-08 10:48:44 +10:00
uint64_t now = nanoseconds ();
uint64_t total = now - m_last;
if (total < m_target)
sleep (m_target - total);
m_last = now;
}
2022-03-22 14:18:01 +11:00
///////////////////////////////////////////////////////////////////////////////
cruft::period_limiter::period_limiter (std::chrono::nanoseconds _period)
: period_limiter (_period.count ())
{ ; }
//-----------------------------------------------------------------------------
cruft::period_limiter::period_limiter (u64 _period)
: m_period (_period)
, m_prev (nanoseconds () - _period)
{ ; }
//-----------------------------------------------------------------------------
2022-03-30 14:29:33 +11:00
void
cruft::period_limiter::wait (void) const
2022-03-22 14:18:01 +11:00
{
auto const now = nanoseconds ();
auto const diff = now - m_prev;
if (diff < m_period)
sleep (m_period - diff);
2022-03-30 14:29:33 +11:00
}
2022-03-22 14:18:01 +11:00
2022-03-30 14:29:33 +11:00
//-----------------------------------------------------------------------------
void cruft::period_limiter::reset (void)
{
2022-03-22 14:18:01 +11:00
m_prev = nanoseconds ();
}
2022-03-30 14:29:33 +11:00
//-----------------------------------------------------------------------------
void cruft::period_limiter::poll (void)
{
wait ();
reset ();
}
2015-07-23 14:13:09 +10:00
///////////////////////////////////////////////////////////////////////////////
cruft::polled_duration::polled_duration (std::string name, uint64_t interval):
m_name (std::move (name)),
m_interval (interval),
2015-04-13 18:06:08 +10:00
m_next (nanoseconds () + interval)
{ ; }
2012-05-01 12:14:38 +10:00
2015-07-23 14:13:09 +10:00
//-----------------------------------------------------------------------------
void
cruft::polled_duration::start (void)
2015-07-23 14:13:09 +10:00
{
m_last = nanoseconds ();
}
2015-07-23 14:13:09 +10:00
//-----------------------------------------------------------------------------
void
cruft::polled_duration::stop (void) {
uint64_t now = nanoseconds ();
uint64_t dt = now - m_last;
2013-08-08 10:48:29 +10:00
m_series.add (dt / MILLISECOND);
if (m_next < now) {
LOG_DEBUG ("timing: '{:s}'. {}", m_name, m_series);
m_series.reset ();
m_next = now + m_interval;
}
}