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
|
|
|
*
|
2016-04-05 11:02:54 +10:00
|
|
|
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
|
2011-08-29 15:28:11 +10:00
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "time.hpp"
|
2011-08-29 15:28:11 +10:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "log.hpp"
|
2011-09-16 22:55:56 +10:00
|
|
|
|
2016-04-05 11:02:54 +10:00
|
|
|
#include <chrono>
|
2011-08-29 15:28:11 +10:00
|
|
|
|
2018-08-05 14:42:02 +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
|
|
|
|
2011-09-25 14:24:53 +10:00
|
|
|
|
2016-04-05 11:02:54 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
uintmax_t
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::nanoseconds (void)
|
2016-04-05 11:02:54 +10:00
|
|
|
{
|
|
|
|
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 ():
|
2018-08-05 14:42:02 +10:00
|
|
|
time { cruft::nanoseconds (), cruft::nanoseconds () }
|
2013-08-05 16:40:43 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-07-23 14:13:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-04-05 18:48:23 +10:00
|
|
|
std::chrono::nanoseconds
|
|
|
|
delta_clock::dt (void)
|
2015-07-23 14:13:09 +10:00
|
|
|
{
|
2013-08-05 16:40:43 +10:00
|
|
|
time.prev = time.curr;
|
|
|
|
time.curr = nanoseconds ();
|
|
|
|
|
2018-04-05 18:48:23 +10:00
|
|
|
return std::chrono::nanoseconds (time.curr - time.prev);
|
2013-08-05 16:40:43 +10:00
|
|
|
}
|
|
|
|
|
2015-07-23 14:13:09 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +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 ();
|
2014-07-02 15:47:09 +10:00
|
|
|
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
|
2018-08-05 14:42:02 +10:00
|
|
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +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
|
2018-08-05 14:42:02 +10:00
|
|
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::polled_duration::polled_duration (std::string name, uint64_t interval):
|
2018-07-24 15:47:21 +10:00
|
|
|
m_name (std::move (name)),
|
2012-08-07 18:35:38 +10:00
|
|
|
m_interval (interval),
|
2015-04-13 18:06:08 +10:00
|
|
|
m_next (nanoseconds () + interval)
|
2012-08-07 18:35:38 +10:00
|
|
|
{ ; }
|
2012-05-01 12:14:38 +10:00
|
|
|
|
2012-08-07 18:35:38 +10:00
|
|
|
|
2015-07-23 14:13:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-08-07 18:35:38 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::polled_duration::start (void)
|
2015-07-23 14:13:09 +10:00
|
|
|
{
|
2012-08-07 18:35:38 +10:00
|
|
|
m_last = nanoseconds ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-23 14:13:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-08-07 18:35:38 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::polled_duration::stop (void) {
|
2012-08-07 18:35:38 +10:00
|
|
|
uint64_t now = nanoseconds ();
|
|
|
|
uint64_t dt = now - m_last;
|
2013-08-08 10:48:29 +10:00
|
|
|
m_series.add (dt / MILLISECOND);
|
2012-08-07 18:35:38 +10:00
|
|
|
|
|
|
|
if (m_next < now) {
|
2021-04-14 10:23:33 +10:00
|
|
|
LOG_DEBUG ("timing: '{:s}'. {:s}", m_name, m_series);
|
2012-08-07 18:35:38 +10:00
|
|
|
m_series.reset ();
|
|
|
|
m_next = now + m_interval;
|
|
|
|
}
|
|
|
|
}
|