Correct elapsed time calculation

Elapsed time calculation must always be be the current time minus a time in the past.  Not vice versa.

Also corrects and improves some comments.
This commit is contained in:
Gregory Nutt
2021-03-19 09:02:18 -06:00
committed by Xiang Xiao
parent 5b7dfa0213
commit 2208aabbc6
+26 -13
View File
@@ -390,8 +390,8 @@ static int sporadic_replenish_start(FAR struct replenishment_s *repl)
* Start the delay prior to providing the replenishment. * Start the delay prior to providing the replenishment.
* *
* Input Parameters: * Input Parameters:
* tcb - Current thread's tCB
* repl - Replenishment timer to be used * repl - Replenishment timer to be used
* period - Delay to the timer expiration
* replenish - The replenish time to be applied after the delay * replenish - The replenish time to be applied after the delay
* *
* Returned Value: * Returned Value:
@@ -487,11 +487,11 @@ static void sporadic_budget_expire(wdparm_t arg)
uint32_t unrealized; uint32_t unrealized;
/* The unrealized time is the interval from when the thread was /* The unrealized time is the interval from when the thread was
* suspended (or which the budget interval was started in the case * suspended (or when the budget interval was started in the case
* that the thread was delayed for the entire interval). * that the thread was delayed for the entire interval).
*/ */
unrealized = sporadic->eventtime - clock_systime_ticks(); unrealized = clock_systime_ticks() - sporadic->eventtime;
if (unrealized > 0) if (unrealized > 0)
{ {
/* Allocate a new replenishment timer. This will limit us to the /* Allocate a new replenishment timer. This will limit us to the
@@ -501,12 +501,19 @@ static void sporadic_budget_expire(wdparm_t arg)
repl = sporadic_alloc_repl(sporadic); repl = sporadic_alloc_repl(sporadic);
if (repl != NULL) if (repl != NULL)
{ {
/* The delay is one half of the scheduler cycle relative to
* the suspend time. Hence, we subtract the unrealized amount.
*/
uint32_t period; uint32_t period;
/* Calculate the delay to when replenishment interval begins.
* That time is one half of the scheduler cycle relative to
* the suspend time. The delay relative to the current time
* is then:
*
* repl_time = susp_time + repl_interval / 2;
* delay = repl_time - curr_time
* delay = susp_time - curr_time + repl_interval / 2
* delay = repl_interval / 2 - unrealized
*/
DEBUGASSERT(unrealized <= (sporadic->repl_period >> 1)); DEBUGASSERT(unrealized <= (sporadic->repl_period >> 1));
period = (sporadic->repl_period >> 1) - unrealized; period = (sporadic->repl_period >> 1) - unrealized;
@@ -1066,13 +1073,19 @@ int nxsched_resume_sporadic(FAR struct tcb_s *tcb)
repl = sporadic_alloc_repl(sporadic); repl = sporadic_alloc_repl(sporadic);
if (repl != NULL) if (repl != NULL)
{ {
/* The delay is one half of the scheduler cycle relative
* to the suspend time. Hence, we subtract the unrealized.
* amount.
*/
uint32_t period; uint32_t period;
/* Calculate the delay to when replenishment interval
* begins. That time is one half of the scheduler cycle
* relative to the suspend time. The delay relative to
* the current time is then:
*
* repl_time = susp_time + repl_interval / 2;
* delay = repl_time - curr_time
* delay = susp_time - curr_time + repl_interval / 2
* delay = repl_interval / 2 - unrealized
*/
DEBUGASSERT(unrealized <= (sporadic->repl_period >> 1)); DEBUGASSERT(unrealized <= (sporadic->repl_period >> 1));
period = (sporadic->repl_period >> 1) - unrealized; period = (sporadic->repl_period >> 1) - unrealized;
@@ -1102,7 +1115,7 @@ int nxsched_resume_sporadic(FAR struct tcb_s *tcb)
* Name: nxsched_suspend_sporadic * Name: nxsched_suspend_sporadic
* *
* Description: * Description:
* Called to when a thread with sporadic scheduling is suspended. In this * Called when a thread with sporadic scheduling is suspended. In this
* case, there will be unaccounted for time from the time that the last * case, there will be unaccounted for time from the time that the last
* when the task is resumed. All that we need to do here is remember * when the task is resumed. All that we need to do here is remember
* that time that we were suspended. * that time that we were suspended.