Skip to content
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
5 changes: 5 additions & 0 deletions Implementation/Common/common_chrono.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ namespace AMCCommon {
return convertToISO8601TimeUTC(getUTCTimeStampInMicrosecondsSince1970());
}

uint64_t CChrono::getElapsedMicroseconds()
{
return m_pChronoImpl->getElapsedMicroseconds();
}

void CChrono::sleepSeconds(const uint64_t seconds)
{
sleepMicroseconds(seconds * 1000000ULL);
Expand Down
5 changes: 5 additions & 0 deletions Implementation/Common/common_chrono.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ namespace AMCCommon {
// Returns Absolute UTC Time Stamp in ISO8601 formatting
std::string getUTCTimeInISO8601();

// Returns Monotonic Elapsed Time in microseconds since instance creation
// This time is NOT affected by system clock adjustments (NTP, manual changes)
// Use this for timing and delays, NOT for absolute timestamps
uint64_t getElapsedMicroseconds();

// Sleeps for some seconds
static void sleepSeconds(const uint64_t seconds);

Expand Down
10 changes: 6 additions & 4 deletions Implementation/Core/amc_statemachinestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ namespace AMC {

void CStateMachineState::updateExecutionTime()
{
m_LastExecutionTimeStampInMicroseconds = m_pGlobalChrono->getUTCTimeStampInMicrosecondsSince1970 ();
// Use monotonic elapsed time to avoid issues with NTP clock adjustments
m_LastExecutionTimeStampInMicroseconds = m_pGlobalChrono->getElapsedMicroseconds();
}


Expand Down Expand Up @@ -161,8 +162,9 @@ namespace AMC {

if (chunkInMilliseconds < AMC_MINREPEATDELAY_MS)
throw ELibMCCustomException(LIBMC_ERROR_INVALIDREPEATDELAY, m_sInstanceName);

uint64_t executionTimeInMicroSeconds = m_pGlobalChrono->getUTCTimeStampInMicrosecondsSince1970();

// Use monotonic elapsed time - not affected by NTP or system clock adjustments
uint64_t executionTimeInMicroSeconds = m_pGlobalChrono->getElapsedMicroseconds();
if (m_LastExecutionTimeStampInMicroseconds > executionTimeInMicroSeconds)
throw ELibMCCustomException(LIBMC_ERROR_INVALIDEXECUTIONDELAY, std::to_string (m_LastExecutionTimeStampInMicroseconds));

Expand All @@ -171,7 +173,7 @@ namespace AMC {
while (deltaExecutionTimeInMicroSeconds < (m_nRepeatDelay * 1000ULL)) {
m_pGlobalChrono->sleepMilliseconds(chunkInMilliseconds);

uint64_t newExecutionTimeInMicroSeconds = m_pGlobalChrono->getUTCTimeStampInMicrosecondsSince1970();
uint64_t newExecutionTimeInMicroSeconds = m_pGlobalChrono->getElapsedMicroseconds();
if (m_LastExecutionTimeStampInMicroseconds > newExecutionTimeInMicroSeconds)
throw ELibMCCustomException(LIBMC_ERROR_INVALIDEXECUTIONDELAY, std::to_string(m_LastExecutionTimeStampInMicroseconds));

Expand Down
Loading