From 9ea56ee2b6af0ea62230c75ce884e7f872d746cf Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Sun, 24 Nov 2019 12:43:06 +0200 Subject: [PATCH 1/8] Adds values of nsec in sec, msec in sec and makes ONE_THOUSAND refer to MSEC_IN_SEC (name consistency) --- include/ola/Clock.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/ola/Clock.h b/include/ola/Clock.h index 657f75bbeb..4df6dd471b 100644 --- a/include/ola/Clock.h +++ b/include/ola/Clock.h @@ -40,8 +40,10 @@ namespace ola { +static const uint64_t NSEC_IN_SEC = 1000000000; static const int USEC_IN_SECONDS = 1000000; -static const int ONE_THOUSAND = 1000; +static const int MSEC_IN_SEC = 1000; +static const int ONE_THOUSAND = MSEC_IN_SEC; /** * Don't use this class directly. It's an implementation detail of TimeInterval From 098bed36abf521ac4b63a39abd161ba880c72ec2 Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Sun, 24 Nov 2019 12:47:02 +0200 Subject: [PATCH 2/8] Swtiches function from ONE_THOUSAND to MSEC_IN_SEC --- common/utils/Clock.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/utils/Clock.cpp b/common/utils/Clock.cpp index bc1544905a..0a1a08fb11 100644 --- a/common/utils/Clock.cpp +++ b/common/utils/Clock.cpp @@ -125,8 +125,8 @@ void BaseTimeVal::AsTimeval(struct timeval *tv) const { } int64_t BaseTimeVal::InMilliSeconds() const { - return (m_tv.tv_sec * static_cast(ONE_THOUSAND) + - m_tv.tv_usec / ONE_THOUSAND); + return (m_tv.tv_sec * static_cast(MSEC_IN_SEC) + + m_tv.tv_usec / MSEC_IN_SEC); } int64_t BaseTimeVal::AsInt() const { From f8eb5963bb6051f9788f108b25c955dff75bbb78 Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Sun, 24 Nov 2019 12:52:36 +0200 Subject: [PATCH 3/8] Adds InMicroSeconds() wrapper for AsInt() --- include/ola/Clock.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/ola/Clock.h b/include/ola/Clock.h index 4df6dd471b..313a7710d6 100644 --- a/include/ola/Clock.h +++ b/include/ola/Clock.h @@ -106,6 +106,12 @@ class BaseTimeVal { */ int64_t AsInt() const; + /** + * @brief InMicroSeconds wrapper for AsInt() for name consistency. + * @return The entire BaseTimeVal in microseconds + */ + int64_t InMicroSeconds() const { return this->AsInt(); } + std::string ToString() const; private: @@ -162,6 +168,7 @@ class TimeInterval { int32_t MicroSeconds() const { return m_interval.MicroSeconds(); } int64_t InMilliSeconds() const { return m_interval.InMilliSeconds(); } + int64_t InMicroSeconds() const { return m_interval.InMicroSeconds(); } int64_t AsInt() const { return m_interval.AsInt(); } std::string ToString() const { return m_interval.ToString(); } From 889137bcf8fd100291ab590c043d103ef17bab2f Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Sun, 24 Nov 2019 13:01:40 +0200 Subject: [PATCH 4/8] Add Sleep class with implementation of usleep() and granularity detection. --- common/utils/Clock.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++ include/ola/Clock.h | 28 ++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/common/utils/Clock.cpp b/common/utils/Clock.cpp index 0a1a08fb11..07cfacf9cc 100644 --- a/common/utils/Clock.cpp +++ b/common/utils/Clock.cpp @@ -39,6 +39,8 @@ #include #include +#include "ola/Logging.h" + namespace ola { using std::string; @@ -272,4 +274,88 @@ void MockClock::CurrentTime(TimeStamp *timestamp) const { *timestamp = tv; *timestamp += m_offset; } + +Sleep::Sleep(std::string caller) : + m_caller(caller) { +} + +/** + * @brief Set wanted granularity for usleep and check it. + * @note does not check at the nanosecond level, + * since internal sturtures use usecs. + * + * @param wanted wanted/needed granularity in usecs + * @param maxDeviation max deviation in usecs tolerated by calling thread. + * + * @attention the granularity of sleep is highly fluctuating depending on the + * load of the system, a prior GOOD state is no guarantee for future proper + * timing. + */ +bool Sleep::CheckTimeGranularity(uint64_t wanted, uint64_t maxDeviation) { + TimeStamp ts1, ts2; + Clock clock; + + m_wanted_granularity = wanted; + m_max_granularity_deviation = maxDeviation; + + timespec t; + t.tv_sec = wanted / USEC_IN_SECONDS; + t.tv_nsec = (wanted % USEC_IN_SECONDS) * ONE_THOUSAND; + + clock.CurrentTime(&ts1); + this->usleep(1); + clock.CurrentTime(&ts2); + TimeInterval interval = ts2 - ts1; + m_clock_overhead = interval.InMicroSeconds(); + + clock.CurrentTime(&ts1); + this->usleep(t); + clock.CurrentTime(&ts2); + + interval = ts2 - ts1; + m_granularity = (interval.InMicroSeconds() > + (wanted + maxDeviation + m_clock_overhead)) ? BAD : GOOD; + + OLA_INFO << "Granularity for OlaSleep in " << m_caller << " is " + << ((m_granularity == GOOD) ? "GOOD" : "BAD") + << " Requested: " << wanted << " Got: " << interval.InMicroSeconds() + << " Overhead: " << m_clock_overhead; + if (m_granularity == GOOD) { + return true; + } + return false; +} + +void Sleep::usleep(TimeInterval requested) { + timespec req; + req.tv_sec = requested.Seconds(); + req.tv_nsec = requested.MicroSeconds() * ONE_THOUSAND; + + this->usleep(req); +} + +void Sleep::usleep(uint32_t requested) { + timespec req; + req.tv_sec = requested / USEC_IN_SECONDS; + req.tv_nsec = (requested % USEC_IN_SECONDS) * ONE_THOUSAND; + + this->usleep(req); +} + +void Sleep::usleep(timespec requested) { + timespec rem; + + if (nanosleep(&requested, &rem) < 0) { + if (errno == EINTR) { + while (rem.tv_nsec > 0 || rem.tv_sec > 0) { + requested.tv_nsec = rem.tv_nsec; + requested.tv_sec = rem.tv_sec; + nanosleep(&requested, &rem); + } + } else { + OLA_WARN << "nanosleep failed with state: " << errno; + } + } +} + } // namespace ola diff --git a/include/ola/Clock.h b/include/ola/Clock.h index 313a7710d6..3218481ecc 100644 --- a/include/ola/Clock.h +++ b/include/ola/Clock.h @@ -266,5 +266,33 @@ class MockClock: public Clock { private: TimeInterval m_offset; }; + +enum TimerGranularity { UNKNOWN, GOOD, BAD }; + +/** + * @brief The Sleep class implements usleep with some granualtiry detection. + */ +class Sleep { + public: + explicit Sleep(std::string caller); + + void setCaller(std::string caller) { m_caller = caller; } + + void usleep(TimeInterval requested); + void usleep(uint32_t requested); + void usleep(timespec requested); + + TimerGranularity getGranularity() { return m_granularity; } + bool CheckTimeGranularity(uint64_t wanted, uint64_t maxDeviation); + private: + std::string m_caller; + uint64_t m_wanted_granularity; + uint64_t m_max_granularity_deviation; + uint64_t m_clock_overhead; + + static const uint32_t BAD_GRANULARITY_LIMIT = 10; + + TimerGranularity m_granularity = UNKNOWN; +}; } // namespace ola #endif // INCLUDE_OLA_CLOCK_H_ From d1f6b5f4705f963445f3ff2ae551f66ef752b99e Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Sun, 24 Nov 2019 13:06:15 +0200 Subject: [PATCH 5/8] Remove extra newline --- common/utils/Clock.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/common/utils/Clock.cpp b/common/utils/Clock.cpp index 07cfacf9cc..7aee5fab04 100644 --- a/common/utils/Clock.cpp +++ b/common/utils/Clock.cpp @@ -357,5 +357,4 @@ void Sleep::usleep(timespec requested) { } } } - } // namespace ola From baa4005afb606983b1c70624e7ebcedc7a7206fc Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Mon, 9 Dec 2019 14:23:04 +0200 Subject: [PATCH 6/8] Various SPaG, rename variables, rename functions at request of @peternewman --- common/utils/Clock.cpp | 60 +++++++++++++++++++------------------- common/utils/ClockTest.cpp | 10 +++---- include/ola/Clock.h | 38 ++++++++++++------------ 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/common/utils/Clock.cpp b/common/utils/Clock.cpp index 7aee5fab04..096ec84bb7 100644 --- a/common/utils/Clock.cpp +++ b/common/utils/Clock.cpp @@ -126,13 +126,13 @@ void BaseTimeVal::AsTimeval(struct timeval *tv) const { *tv = m_tv; } -int64_t BaseTimeVal::InMilliSeconds() const { - return (m_tv.tv_sec * static_cast(MSEC_IN_SEC) + - m_tv.tv_usec / MSEC_IN_SEC); +int64_t BaseTimeVal::InMilliseconds() const { + return (m_tv.tv_sec * static_cast(MSECS_IN_SECOND) + + m_tv.tv_usec / MSECS_IN_SECOND); } int64_t BaseTimeVal::AsInt() const { - return (m_tv.tv_sec * static_cast(USEC_IN_SECONDS) + m_tv.tv_usec); + return (m_tv.tv_sec * static_cast(USECS_IN_SECOND) + m_tv.tv_usec); } string BaseTimeVal::ToString() const { @@ -146,9 +146,9 @@ void BaseTimeVal::TimerAdd(const struct timeval &tv1, const struct timeval &tv2, struct timeval *result) const { result->tv_sec = tv1.tv_sec + tv2.tv_sec; result->tv_usec = tv1.tv_usec + tv2.tv_usec; - if (result->tv_usec >= USEC_IN_SECONDS) { + if (result->tv_usec >= USECS_IN_SECOND) { result->tv_sec++; - result->tv_usec -= USEC_IN_SECONDS; + result->tv_usec -= USECS_IN_SECOND; } } @@ -158,22 +158,22 @@ void BaseTimeVal::TimerSub(const struct timeval &tv1, const struct timeval &tv2, result->tv_usec = tv1.tv_usec - tv2.tv_usec; if (result->tv_usec < 0) { result->tv_sec--; - result->tv_usec += USEC_IN_SECONDS; + result->tv_usec += USECS_IN_SECOND; } } void BaseTimeVal::Set(int64_t interval_useconds) { #ifdef HAVE_TIME_T m_tv.tv_sec = static_cast( - interval_useconds / USEC_IN_SECONDS); + interval_useconds / USECS_IN_SECOND); #else - m_tv.tv_sec = interval_useconds / USEC_IN_SECONDS; + m_tv.tv_sec = interval_useconds / USECS_IN_SECOND; #endif // HAVE_TIME_T #ifdef HAVE_SUSECONDS_T - m_tv.tv_usec = static_cast(interval_useconds % USEC_IN_SECONDS); + m_tv.tv_usec = static_cast(interval_useconds % USECS_IN_SECOND); #else - m_tv.tv_usec = interval_useconds % USEC_IN_SECONDS; + m_tv.tv_usec = interval_useconds % USECS_IN_SECOND; #endif // HAVE_SUSECONDS_T } @@ -282,7 +282,7 @@ Sleep::Sleep(std::string caller) : /** * @brief Set wanted granularity for usleep and check it. * @note does not check at the nanosecond level, - * since internal sturtures use usecs. + * since internal structures use usecs. * * @param wanted wanted/needed granularity in usecs * @param maxDeviation max deviation in usecs tolerated by calling thread. @@ -291,34 +291,34 @@ Sleep::Sleep(std::string caller) : * load of the system, a prior GOOD state is no guarantee for future proper * timing. */ -bool Sleep::CheckTimeGranularity(uint64_t wanted, uint64_t maxDeviation) { +bool Sleep::CheckTimeGranularity(uint64_t wanted, uint64_t max_deviation) { TimeStamp ts1, ts2; Clock clock; m_wanted_granularity = wanted; - m_max_granularity_deviation = maxDeviation; + m_max_granularity_deviation = max_deviation; timespec t; - t.tv_sec = wanted / USEC_IN_SECONDS; - t.tv_nsec = (wanted % USEC_IN_SECONDS) * ONE_THOUSAND; + t.tv_sec = wanted / USECS_IN_SECOND; + t.tv_nsec = (wanted % USECS_IN_SECOND) * ONE_THOUSAND; clock.CurrentTime(&ts1); - this->usleep(1); + this->Usleep(1); clock.CurrentTime(&ts2); TimeInterval interval = ts2 - ts1; - m_clock_overhead = interval.InMicroSeconds(); + m_clock_overhead = interval.InMicroseconds(); clock.CurrentTime(&ts1); - this->usleep(t); + this->Usleep(t); clock.CurrentTime(&ts2); interval = ts2 - ts1; - m_granularity = (interval.InMicroSeconds() > - (wanted + maxDeviation + m_clock_overhead)) ? BAD : GOOD; + m_granularity = (interval.InMicroseconds() > + (wanted + max_deviation + m_clock_overhead)) ? BAD : GOOD; OLA_INFO << "Granularity for OlaSleep in " << m_caller << " is " << ((m_granularity == GOOD) ? "GOOD" : "BAD") - << " Requested: " << wanted << " Got: " << interval.InMicroSeconds() + << " Requested: " << wanted << " Got: " << interval.InMicroseconds() << " Overhead: " << m_clock_overhead; if (m_granularity == GOOD) { return true; @@ -326,23 +326,23 @@ bool Sleep::CheckTimeGranularity(uint64_t wanted, uint64_t maxDeviation) { return false; } -void Sleep::usleep(TimeInterval requested) { +void Sleep::Usleep(TimeInterval requested) { timespec req; req.tv_sec = requested.Seconds(); - req.tv_nsec = requested.MicroSeconds() * ONE_THOUSAND; + req.tv_nsec = requested.Microseconds() * ONE_THOUSAND; - this->usleep(req); + this->Usleep(req); } -void Sleep::usleep(uint32_t requested) { +void Sleep::Usleep(uint32_t requested) { timespec req; - req.tv_sec = requested / USEC_IN_SECONDS; - req.tv_nsec = (requested % USEC_IN_SECONDS) * ONE_THOUSAND; + req.tv_sec = requested / USECS_IN_SECOND; + req.tv_nsec = (requested % USECS_IN_SECOND) * ONE_THOUSAND; - this->usleep(req); + this->Usleep(req); } -void Sleep::usleep(timespec requested) { +void Sleep::Usleep(timespec requested) { timespec rem; if (nanosleep(&requested, &rem) < 0) { diff --git a/common/utils/ClockTest.cpp b/common/utils/ClockTest.cpp index 8429844d9e..4a2823ebd2 100644 --- a/common/utils/ClockTest.cpp +++ b/common/utils/ClockTest.cpp @@ -101,7 +101,7 @@ void ClockTest::testTimeStamp() { OLA_ASSERT_EQ(static_cast(1500000), one_point_five_seconds.AsInt()); OLA_ASSERT_EQ(static_cast(1500), - one_point_five_seconds.InMilliSeconds()); + one_point_five_seconds.InMilliseconds()); } @@ -131,16 +131,16 @@ void ClockTest::testTimeInterval() { void ClockTest::testTimeIntervalMutliplication() { TimeInterval half_second(500000); // 0.5s TimeInterval zero_seconds = half_second * 0; - OLA_ASSERT_EQ((int64_t) 0, zero_seconds.InMilliSeconds()); + OLA_ASSERT_EQ((int64_t) 0, zero_seconds.InMilliseconds()); TimeInterval another_half_second = half_second * 1; - OLA_ASSERT_EQ((int64_t) 500, another_half_second.InMilliSeconds()); + OLA_ASSERT_EQ((int64_t) 500, another_half_second.InMilliseconds()); TimeInterval two_seconds = half_second * 4; - OLA_ASSERT_EQ((int64_t) 2000, two_seconds.InMilliSeconds()); + OLA_ASSERT_EQ((int64_t) 2000, two_seconds.InMilliseconds()); TimeInterval twenty_seconds = half_second * 40; - OLA_ASSERT_EQ((int64_t) 20000, twenty_seconds.InMilliSeconds()); + OLA_ASSERT_EQ((int64_t) 20000, twenty_seconds.InMilliseconds()); } diff --git a/include/ola/Clock.h b/include/ola/Clock.h index 3218481ecc..dd6657327b 100644 --- a/include/ola/Clock.h +++ b/include/ola/Clock.h @@ -40,10 +40,10 @@ namespace ola { -static const uint64_t NSEC_IN_SEC = 1000000000; -static const int USEC_IN_SECONDS = 1000000; -static const int MSEC_IN_SEC = 1000; -static const int ONE_THOUSAND = MSEC_IN_SEC; +static const int ONE_THOUSAND = 1000; +static const int MSECS_IN_SECOND = ONE_THOUSAND; +static const int USECS_IN_SECOND = MSECS_IN_SECOND * ONE_THOUSAND; +static const uint64_t NSECS_IN_SECOND = USECS_IN_SECOND * ONE_THOUSAND; /** * Don't use this class directly. It's an implementation detail of TimeInterval @@ -92,13 +92,13 @@ class BaseTimeVal { * @brief Returns the microseconds portion of the BaseTimeVal * @return The microseconds portion of the BaseTimeVal */ - int32_t MicroSeconds() const { return static_cast(m_tv.tv_usec); } + int32_t Microseconds() const { return static_cast(m_tv.tv_usec); } /** * @brief Returns the entire BaseTimeVal as milliseconds * @return The entire BaseTimeVal in milliseconds */ - int64_t InMilliSeconds() const; + int64_t InMilliseconds() const; /** * @brief Returns the entire BaseTimeVal as microseconds @@ -107,10 +107,10 @@ class BaseTimeVal { int64_t AsInt() const; /** - * @brief InMicroSeconds wrapper for AsInt() for name consistency. + * @brief InMicroseconds wrapper for AsInt() for name consistency. * @return The entire BaseTimeVal in microseconds */ - int64_t InMicroSeconds() const { return this->AsInt(); } + int64_t InMicroseconds() const { return this->AsInt(); } std::string ToString() const; @@ -165,10 +165,10 @@ class TimeInterval { void AsTimeval(struct timeval *tv) const { m_interval.AsTimeval(tv); } time_t Seconds() const { return m_interval.Seconds(); } - int32_t MicroSeconds() const { return m_interval.MicroSeconds(); } + int32_t Microseconds() const { return m_interval.Microseconds(); } - int64_t InMilliSeconds() const { return m_interval.InMilliSeconds(); } - int64_t InMicroSeconds() const { return m_interval.InMicroSeconds(); } + int64_t InMilliseconds() const { return m_interval.InMilliseconds(); } + int64_t InMicroseconds() const { return m_interval.InMicroseconds(); } int64_t AsInt() const { return m_interval.AsInt(); } std::string ToString() const { return m_interval.ToString(); } @@ -220,7 +220,7 @@ class TimeStamp { bool IsSet() const { return m_tv.IsSet(); } time_t Seconds() const { return m_tv.Seconds(); } - int32_t MicroSeconds() const { return m_tv.MicroSeconds(); } + int32_t Microseconds() const { return m_tv.Microseconds(); } std::string ToString() const { return m_tv.ToString(); } @@ -270,20 +270,20 @@ class MockClock: public Clock { enum TimerGranularity { UNKNOWN, GOOD, BAD }; /** - * @brief The Sleep class implements usleep with some granualtiry detection. + * @brief The Sleep class implements usleep with some granulatiry detection. */ class Sleep { public: explicit Sleep(std::string caller); - void setCaller(std::string caller) { m_caller = caller; } + void SetCaller(std::string caller) { m_caller = caller; } - void usleep(TimeInterval requested); - void usleep(uint32_t requested); - void usleep(timespec requested); + void Usleep(TimeInterval requested); + void Usleep(uint32_t requested); + void Usleep(timespec requested); - TimerGranularity getGranularity() { return m_granularity; } - bool CheckTimeGranularity(uint64_t wanted, uint64_t maxDeviation); + TimerGranularity GetGranularity() { return m_granularity; } + bool CheckTimeGranularity(uint64_t wanted, uint64_t max_deviation); private: std::string m_caller; uint64_t m_wanted_granularity; From bc04627505a8442b6e9973bd79022fb02c8a9658 Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Mon, 9 Dec 2019 14:24:53 +0200 Subject: [PATCH 7/8] Clock functions renamed. --- common/http/OlaHTTPServer.cpp | 2 +- common/io/EPoller.cpp | 2 +- common/io/KQueuePoller.cpp | 2 +- common/io/WindowsPoller.cpp | 2 +- common/math/Random.cpp | 2 +- common/thread/Mutex.cpp | 2 +- common/utils/TokenBucket.cpp | 4 ++-- examples/ShowSaver.cpp | 2 +- examples/ola-latency.cpp | 4 ++-- plugins/ftdidmx/FtdiDmxThread.cpp | 8 ++++---- plugins/uartdmx/UartDmxThread.cpp | 2 +- tools/rdmpro/rdm-sniffer.cpp | 2 +- 12 files changed, 17 insertions(+), 17 deletions(-) diff --git a/common/http/OlaHTTPServer.cpp b/common/http/OlaHTTPServer.cpp index 7b51449939..c18b31a186 100644 --- a/common/http/OlaHTTPServer.cpp +++ b/common/http/OlaHTTPServer.cpp @@ -76,7 +76,7 @@ int OlaHTTPServer::DisplayDebug(const HTTPRequest*, m_clock.CurrentTime(&now); ola::TimeInterval diff = now - m_start_time; ostringstream str; - str << diff.InMilliSeconds(); + str << diff.InMilliseconds(); m_export_map->GetStringVar(K_UPTIME_VAR)->Set(str.str()); vector variables = m_export_map->AllVariables(); diff --git a/common/io/EPoller.cpp b/common/io/EPoller.cpp index f6b4e47214..8615f2675e 100644 --- a/common/io/EPoller.cpp +++ b/common/io/EPoller.cpp @@ -310,7 +310,7 @@ bool EPoller::Poll(TimeoutManager *timeout_manager, (*m_loop_iterations)++; } - int ms_to_sleep = sleep_interval.InMilliSeconds(); + int ms_to_sleep = sleep_interval.InMilliseconds(); int ready = epoll_wait(m_epoll_fd, reinterpret_cast(&events), MAX_EVENTS, ms_to_sleep ? ms_to_sleep : 1); diff --git a/common/io/KQueuePoller.cpp b/common/io/KQueuePoller.cpp index ded9cbf0aa..da44993819 100644 --- a/common/io/KQueuePoller.cpp +++ b/common/io/KQueuePoller.cpp @@ -252,7 +252,7 @@ bool KQueuePoller::Poll(TimeoutManager *timeout_manager, struct timespec sleep_time; sleep_time.tv_sec = sleep_interval.Seconds(); - sleep_time.tv_nsec = sleep_interval.MicroSeconds() * 1000; + sleep_time.tv_nsec = sleep_interval.Microseconds() * ONE_THOUSAND; int ready = kevent( m_kqueue_fd, reinterpret_cast(m_change_set), diff --git a/common/io/WindowsPoller.cpp b/common/io/WindowsPoller.cpp index 884e9d5a85..c0afcd86f9 100644 --- a/common/io/WindowsPoller.cpp +++ b/common/io/WindowsPoller.cpp @@ -305,7 +305,7 @@ bool WindowsPoller::Poll(TimeoutManager *timeout_manager, (*m_loop_iterations)++; } - int ms_to_sleep = sleep_interval.InMilliSeconds(); + int ms_to_sleep = sleep_interval.InMilliseconds(); // Prepare events vector events; diff --git a/common/math/Random.cpp b/common/math/Random.cpp index 595a9db351..f83d84d31b 100644 --- a/common/math/Random.cpp +++ b/common/math/Random.cpp @@ -46,7 +46,7 @@ void InitRandom() { TimeStamp now; clock.CurrentTime(&now); - uint64_t seed = (static_cast(now.MicroSeconds()) << 32) + + uint64_t seed = (static_cast(now.Microseconds()) << 32) + static_cast(getpid()); #ifdef HAVE_RANDOM generator_.seed(seed); diff --git a/common/thread/Mutex.cpp b/common/thread/Mutex.cpp index 2d0cd1ea2c..dc1a1ace9c 100644 --- a/common/thread/Mutex.cpp +++ b/common/thread/Mutex.cpp @@ -123,7 +123,7 @@ void ConditionVariable::Wait(Mutex *mutex) { bool ConditionVariable::TimedWait(Mutex *mutex, const TimeStamp &wake_up_time) { struct timespec ts = { wake_up_time.Seconds(), - wake_up_time.MicroSeconds() * ONE_THOUSAND + wake_up_time.Microseconds() * ONE_THOUSAND }; int i = pthread_cond_timedwait(&m_condition, &mutex->m_mutex, &ts); return i == 0; diff --git a/common/utils/TokenBucket.cpp b/common/utils/TokenBucket.cpp index a7c1e09fd4..b90f660d1e 100644 --- a/common/utils/TokenBucket.cpp +++ b/common/utils/TokenBucket.cpp @@ -44,11 +44,11 @@ bool TokenBucket::GetToken(const TimeStamp &now) { */ unsigned int TokenBucket::Count(const TimeStamp &now) { int64_t delta = (now - m_last).AsInt(); - uint64_t tokens = delta * m_rate / USEC_IN_SECONDS; + uint64_t tokens = delta * m_rate / USECS_IN_SECOND; m_count = std::min(static_cast(m_max), m_count + tokens); if (tokens) - m_last += ola::TimeInterval(tokens * USEC_IN_SECONDS / m_rate); + m_last += ola::TimeInterval(tokens * USECS_IN_SECOND / m_rate); return m_count; } } // namespace ola diff --git a/examples/ShowSaver.cpp b/examples/ShowSaver.cpp index 255aff6d21..8d67e1fbc8 100644 --- a/examples/ShowSaver.cpp +++ b/examples/ShowSaver.cpp @@ -88,7 +88,7 @@ bool ShowSaver::NewFrame(const ola::TimeStamp &arrival_time, // this is not the first frame so write the delay in ms const ola::TimeInterval delta = arrival_time - m_last_frame; - m_show_file << delta.InMilliSeconds() << endl; + m_show_file << delta.InMilliseconds() << endl; } m_last_frame = arrival_time; m_show_file << universe << " " << data.ToString() << endl; diff --git a/examples/ola-latency.cpp b/examples/ola-latency.cpp index 8be90748c0..8bb7fabdec 100644 --- a/examples/ola-latency.cpp +++ b/examples/ola-latency.cpp @@ -96,7 +96,7 @@ void Tracker::Start() { // if you want. cout << "--------------" << endl; cout << "Sent " << m_count << " RPCs" << endl; - cout << "Max was " << m_max.MicroSeconds() << " microseconds" << endl; + cout << "Max was " << m_max.Microseconds() << " microseconds" << endl; cout << "Mean " << m_sum / m_count << " microseconds" << endl; } @@ -130,7 +130,7 @@ void Tracker::LogTime() { if (delta > m_max) { m_max = delta; } - m_sum += delta.MicroSeconds(); + m_sum += delta.Microseconds(); OLA_INFO << "RPC took " << delta; if (FLAGS_count == ++m_count) { diff --git a/plugins/ftdidmx/FtdiDmxThread.cpp b/plugins/ftdidmx/FtdiDmxThread.cpp index c31be8b898..1817cb23d2 100644 --- a/plugins/ftdidmx/FtdiDmxThread.cpp +++ b/plugins/ftdidmx/FtdiDmxThread.cpp @@ -132,7 +132,7 @@ void *FtdiDmxThread::Run() { TimeInterval elapsed = ts2 - ts1; if (m_granularity == GOOD) { - while (elapsed.InMilliSeconds() < frameTime) { + while (elapsed.InMilliseconds() < frameTime) { usleep(1000); clock.CurrentTime(&ts2); elapsed = ts2 - ts1; @@ -142,13 +142,13 @@ void *FtdiDmxThread::Run() { usleep(1000); clock.CurrentTime(&ts3); TimeInterval interval = ts3 - ts2; - if (interval.InMilliSeconds() < BAD_GRANULARITY_LIMIT) { + if (interval.InMilliseconds() < BAD_GRANULARITY_LIMIT) { m_granularity = GOOD; OLA_INFO << "Switching from BAD to GOOD granularity for ftdi thread"; } elapsed = ts3 - ts1; - while (elapsed.InMilliSeconds() < frameTime) { + while (elapsed.InMilliseconds() < frameTime) { clock.CurrentTime(&ts2); elapsed = ts2 - ts1; } @@ -170,7 +170,7 @@ void FtdiDmxThread::CheckTimeGranularity() { clock.CurrentTime(&ts2); TimeInterval interval = ts2 - ts1; - m_granularity = (interval.InMilliSeconds() > BAD_GRANULARITY_LIMIT) ? + m_granularity = (interval.InMilliseconds() > BAD_GRANULARITY_LIMIT) ? BAD : GOOD; OLA_INFO << "Granularity for FTDI thread is " << ((m_granularity == GOOD) ? "GOOD" : "BAD"); diff --git a/plugins/uartdmx/UartDmxThread.cpp b/plugins/uartdmx/UartDmxThread.cpp index cf3530eabf..27af9d25ce 100644 --- a/plugins/uartdmx/UartDmxThread.cpp +++ b/plugins/uartdmx/UartDmxThread.cpp @@ -132,7 +132,7 @@ void UartDmxThread::CheckTimeGranularity() { clock.CurrentTime(&ts2); TimeInterval interval = ts2 - ts1; - m_granularity = interval.InMilliSeconds() > threshold ? BAD : GOOD; + m_granularity = interval.InMilliseconds() > threshold ? BAD : GOOD; OLA_INFO << "Granularity for UART thread is " << (m_granularity == GOOD ? "GOOD" : "BAD"); } diff --git a/tools/rdmpro/rdm-sniffer.cpp b/tools/rdmpro/rdm-sniffer.cpp index 62ff56e609..e3d588f205 100644 --- a/tools/rdmpro/rdm-sniffer.cpp +++ b/tools/rdmpro/rdm-sniffer.cpp @@ -407,7 +407,7 @@ void RDMSniffer::MaybePrintTimestamp() { char output[24]; strftime(output, sizeof(output), "%d-%m-%Y %H:%M:%S", &local_time); - cout << output << "." << std::dec << static_cast(now.MicroSeconds()) << + cout << output << "." << std::dec << static_cast(now.Microseconds()) << " "; } From 25f5425bc3b1afc52857cc87ecdafc9a499bfd9a Mon Sep 17 00:00:00 2001 From: "E.S. Rosenberg a.k.a. Keeper of the Keys" Date: Mon, 9 Dec 2019 14:34:02 +0200 Subject: [PATCH 8/8] Fixed overhead calculation error. --- common/utils/Clock.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/utils/Clock.cpp b/common/utils/Clock.cpp index 096ec84bb7..f25f3fce09 100644 --- a/common/utils/Clock.cpp +++ b/common/utils/Clock.cpp @@ -306,7 +306,7 @@ bool Sleep::CheckTimeGranularity(uint64_t wanted, uint64_t max_deviation) { this->Usleep(1); clock.CurrentTime(&ts2); TimeInterval interval = ts2 - ts1; - m_clock_overhead = interval.InMicroseconds(); + m_clock_overhead = interval.InMicroseconds() - 1; clock.CurrentTime(&ts1); this->Usleep(t);