Skip to content

Commit

Permalink
ched/clock/: Remove g_monotonic_basetime and g_clock_monotonic_time s…
Browse files Browse the repository at this point in the history
…ince we don't need ensure monotonic time start from zero as state here: http://pubs.opengroup.org/onlinepubs/009696899/functions/clock_getres.html
  • Loading branch information
xiaoxiang781216 authored and gregory-nutt committed Nov 12, 2018
1 parent 1ef2602 commit 46bd879
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 127 deletions.
3 changes: 1 addition & 2 deletions include/nuttx/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ void clock_synchronize(void);
*
****************************************************************************/

#if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS) && \
!defined(CONFIG_CLOCK_TIMEKEEPING)
#if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS)
void clock_resynchronize(FAR struct timespec *rtc_diff);
#endif

Expand Down
4 changes: 0 additions & 4 deletions sched/clock/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ extern volatile uint32_t g_system_timer;

#ifndef CONFIG_CLOCK_TIMEKEEPING
extern struct timespec g_basetime;

#ifdef CONFIG_CLOCK_MONOTONIC
extern struct timespec g_monotonic_basetime;
#endif
#endif

/****************************************************************************
Expand Down
31 changes: 1 addition & 30 deletions sched/clock/clock_gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,40 +93,11 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
* reset.
*/

#if defined(CONFIG_CLOCK_TIMEKEEPING)
ret = clock_timekeeping_get_monotonic_time(tp);
#elif defined(CONFIG_SCHED_TICKLESS)
#if defined(CONFIG_SCHED_TICKLESS)
ret = up_timer_gettime(tp);
#else
ret = clock_systimespec(tp);
#endif

#ifndef CONFIG_CLOCK_TIMEKEEPING
if (ret == OK)
{
irqstate_t flags;

/* Add the offset time to this. The offset time allows
* CLOCK_MONOTONIC be introduced additional increases to systime.
*/

flags = spin_lock_irqsave();

tp->tv_sec += (uint32_t)g_monotonic_basetime.tv_sec;
tp->tv_nsec += (uint32_t)g_monotonic_basetime.tv_nsec;

spin_unlock_irqrestore(flags);

/* Handle carry to seconds. */

if (tp->tv_nsec >= NSEC_PER_SEC)
{
carry = tp->tv_nsec / NSEC_PER_SEC;
tp->tv_sec += carry;
tp->tv_nsec -= (carry * NSEC_PER_SEC);
}
}
#endif /* CONFIG_CLOCK_TIMEKEEPING */
}
else
#endif
Expand Down
69 changes: 7 additions & 62 deletions sched/clock/clock_initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ volatile uint32_t g_system_timer;

#ifndef CONFIG_CLOCK_TIMEKEEPING
struct timespec g_basetime;

#ifdef CONFIG_CLOCK_MONOTONIC
struct timespec g_monotonic_basetime;
#endif
#endif

/****************************************************************************
Expand Down Expand Up @@ -199,18 +195,6 @@ static void clock_inittime(void)
g_basetime.tv_nsec += NSEC_PER_SEC;
g_basetime.tv_sec--;
}

#ifdef CONFIG_CLOCK_MONOTONIC
/* Adjust monotonic clock offset to hide initial timer ticks. */

g_monotonic_basetime.tv_sec -= ts.tv_sec;
g_monotonic_basetime.tv_nsec -= ts.tv_nsec;
while (g_monotonic_basetime.tv_nsec < 0)
{
g_monotonic_basetime.tv_nsec += NSEC_PER_SEC;
g_monotonic_basetime.tv_sec--;
}
#endif /* CONFIG_CLOCK_MONOTONIC */
}
#endif /* !CONFIG_SCHED_TICKLESS */
#else
Expand Down Expand Up @@ -312,8 +296,7 @@ void clock_synchronize(void)
*
****************************************************************************/

#if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS) && \
!defined(CONFIG_CLOCK_TIMEKEEPING)
#if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS)
void clock_resynchronize(FAR struct timespec *rtc_diff)
{
struct timespec rtc_time, bias, curr_ts;
Expand Down Expand Up @@ -384,35 +367,10 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
}
else
{
/* Save RTC time as the new base time. */

g_basetime.tv_sec = rtc_time.tv_sec;
g_basetime.tv_nsec = rtc_time.tv_nsec;

/* Subtract that bias from the basetime so that when the system
* timer is again added to the base time, the result is the current
* time relative to basetime.
*/

if (g_basetime.tv_nsec < bias.tv_nsec)
{
g_basetime.tv_nsec += NSEC_PER_SEC;
g_basetime.tv_sec--;
}

/* Result could be negative seconds */

g_basetime.tv_nsec -= bias.tv_nsec;
g_basetime.tv_sec -= bias.tv_sec;

sinfo("basetime=(%ld,%lu) bias=(%ld,%lu)\n",
(long)g_basetime.tv_sec, (unsigned long)g_basetime.tv_nsec,
(long)bias.tv_sec, (unsigned long)bias.tv_nsec);

/* Output difference between time at entry and new current time. */

rtc_diff->tv_sec = (bias.tv_sec + g_basetime.tv_sec) - curr_ts.tv_sec;
rtc_diff->tv_nsec = (bias.tv_nsec + g_basetime.tv_nsec) - curr_ts.tv_nsec;
rtc_diff->tv_sec = rtc_time.tv_sec - curr_ts.tv_sec;
rtc_diff->tv_nsec = rtc_time.tv_nsec - curr_ts.tv_nsec;

/* Handle carry to seconds. */

Expand All @@ -429,29 +387,16 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
carry = 0;
}

if (carry)
if (carry != 0)
{
rtc_diff->tv_sec += carry;
rtc_diff->tv_nsec -= (carry * NSEC_PER_SEC);
}

#ifdef CONFIG_CLOCK_MONOTONIC
/* Monotonic clock follows wall time since system start-up. Adjust
* CLOCK_MONOTONIC same amount as CLOCK_REALTIME.
*/

g_monotonic_basetime.tv_sec += (uint32_t)rtc_diff->tv_sec;
g_monotonic_basetime.tv_nsec += (uint32_t)rtc_diff->tv_nsec;

/* Handle carry to seconds. */
/* Add the sleep time to correct system timer */

if (g_monotonic_basetime.tv_nsec >= NSEC_PER_SEC)
{
carry = g_monotonic_basetime.tv_nsec / NSEC_PER_SEC;
g_monotonic_basetime.tv_sec += carry;
g_monotonic_basetime.tv_nsec -= (carry * NSEC_PER_SEC);
}
#endif
g_system_timer += SEC2TICK(rtc_diff->tv_sec);
g_system_timer += NSEC2TICK(rtc_diff->tv_nsec);
}

skip:
Expand Down
8 changes: 0 additions & 8 deletions sched/clock/clock_systimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ clock_t clock_systimer(void)

/* Get the time from the platform specific hardware */

#ifndef CONFIG_CLOCK_TIMEKEEPING
(void)up_timer_gettime(&ts);
#else
(void)clock_timekeeping_get_monotonic_time(&ts);
#endif

/* Convert to a 64-bit value in microseconds, then in clock tick units */

Expand All @@ -114,11 +110,7 @@ clock_t clock_systimer(void)

/* Get the time from the platform specific hardware */

#ifndef CONFIG_CLOCK_TIMEKEEPING
(void)up_timer_gettime(&ts);
#else
(void)clock_timekeeping_get_monotonic_time(&ts);
#endif

/* Convert to a 64- then a 32-bit value */

Expand Down
20 changes: 0 additions & 20 deletions sched/clock/clock_timekeeping.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
**********************************************************************/

static struct timespec g_clock_wall_time;
static struct timespec g_clock_monotonic_time;
static uint64_t g_clock_last_counter;
static uint64_t g_clock_mask;
static long g_clock_adjust;
Expand Down Expand Up @@ -118,15 +117,6 @@ static int clock_get_current_time(FAR struct timespec *ts,
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: clock_timekeeping_get_monotonic_time
****************************************************************************/

int clock_timekeeping_get_monotonic_time(FAR struct timespec *ts)
{
return clock_get_current_time(ts, &g_clock_monotonic_time);
}

/****************************************************************************
* Name: clock_timekeeping_get_wall_time
****************************************************************************/
Expand Down Expand Up @@ -257,14 +247,6 @@ void clock_update_wall_time(void)
sec = nsec / NSEC_PER_SEC;
nsec -= sec * NSEC_PER_SEC;

g_clock_monotonic_time.tv_sec += sec;
g_clock_monotonic_time.tv_nsec += nsec;
if (g_clock_monotonic_time.tv_nsec > NSEC_PER_SEC)
{
g_clock_monotonic_time.tv_nsec -= NSEC_PER_SEC;
g_clock_monotonic_time.tv_sec += 1;
}

nsec += g_clock_wall_time.tv_nsec;
if (nsec > NSEC_PER_SEC)
{
Expand Down Expand Up @@ -324,8 +306,6 @@ void clock_inittimekeeping(void)

g_clock_wall_time.tv_sec = mktime(&rtctime);
g_clock_wall_time.tv_nsec = 0;

memset(&g_clock_monotonic_time, 0, sizeof(g_clock_monotonic_time));
}

#endif /* CONFIG_CLOCK_TIMEKEEPING */
1 change: 0 additions & 1 deletion sched/clock/clock_timekeeping.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
* Public Function Prototypes
****************************************************************************/

int clock_timekeeping_get_monotonic_time(FAR struct timespec *ts);
int clock_timekeeping_get_wall_time(FAR struct timespec *ts);
int clock_timekeeping_set_wall_time(FAR struct timespec *ts);

Expand Down

0 comments on commit 46bd879

Please sign in to comment.