[rtems commit] score: Fix performance issue for 64-bit timestamps

Sebastian Huber sebh at rtems.org
Thu Jun 14 12:17:33 UTC 2012


Module:    rtems
Branch:    master
Commit:    c34bb0dcfa8ba1294cd5182349e1ddbe34f81bf4
Changeset: http://git.rtems.org/rtems/commit/?id=c34bb0dcfa8ba1294cd5182349e1ddbe34f81bf4

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Wed Jun 13 11:39:43 2012 +0200

score: Fix performance issue for 64-bit timestamps

The 64-bit timestamps were introduced to simplify the timestamp
calculations.  This works well since nearly all operations are
additions.  The previous _TOD_Tickle_ticks() implementation had a
serious performance regression in case of 64-bit timestamps due to the
usage of two 64-bit divisions which are quite expensive on some
architectures.

A new field seconds_trigger in TOD_Control is introduced to trigger the
_Watchdog_Tickle_seconds() in _TOD_Tickle_ticks().  This avoids the
64-bit divisions completely and only 32-bit additions are used.

---

 cpukit/score/include/rtems/score/timestamp.h   |   25 --------------------
 cpukit/score/include/rtems/score/timestamp64.h |   30 ------------------------
 cpukit/score/include/rtems/score/tod.h         |    9 +++++++
 cpukit/score/src/coretodset.c                  |    2 +
 cpukit/score/src/coretodtickle.c               |   18 ++++++++-----
 5 files changed, 22 insertions(+), 62 deletions(-)

diff --git a/cpukit/score/include/rtems/score/timestamp.h b/cpukit/score/include/rtems/score/timestamp.h
index b860909..83948b4 100644
--- a/cpukit/score/include/rtems/score/timestamp.h
+++ b/cpukit/score/include/rtems/score/timestamp.h
@@ -194,31 +194,6 @@ extern "C" {
 #endif
 
 /**
- *  @brief Add to a Timestamp (At Clock Tick)
- *
- *  This routine adds two timestamps.  The second argument is added
- *  to the first.
- *
- *  @node This routine places a special requirement on the addition
- *        operation.  It must return the number of units that the
- *        seconds field changed as the result of the addition.  Since this
- *        operation is ONLY used as part of processing a clock tick,
- *        it is generally safe to assume that only one second changed.
- *
- *  @param[in] _time points to the base time to be added to
- *  @param[in] _add points to the timestamp to add to the first argument
- *
- *  @return This method returns the number of seconds @a time increased by.
- */
-#if CPU_TIMESTAMP_USE_STRUCT_TIMESPEC == TRUE
-  #define _Timestamp_Add_to_at_tick( _time, _add ) \
-          _Timespec_Add_to( _time, _add )
-#else
-  #define _Timestamp_Add_to_at_tick( _time, _add ) \
-          _Timestamp64_Add_to_at_tick( _time, _add )
-#endif
-
-/**
  *  @brief Convert Timestamp to Number of Ticks
  *
  *  This routine convert the @a time timestamp to the corresponding number
diff --git a/cpukit/score/include/rtems/score/timestamp64.h b/cpukit/score/include/rtems/score/timestamp64.h
index 08d3ac7..1c1ea2c 100644
--- a/cpukit/score/include/rtems/score/timestamp64.h
+++ b/cpukit/score/include/rtems/score/timestamp64.h
@@ -209,36 +209,6 @@ static inline void _Timestamp64_implementation_Add_to(
 #endif
 
 /**
- *  @brief Add to a Timestamp (At Clock Tick)
- *
- *  This routine adds two timestamps.  The second argument is added
- *  to the first.
- *
- *  @node This routine places a special requirement on the addition
- *        operation.  It must return the number of units that the
- *        seconds field changed as the result of the addition.  Since this
- *        operation is ONLY used as part of processing a clock tick,
- *        it is generally safe to assume that only one second changed.
- *
- *  @param[in] _time points to the base time to be added to
- *  @param[in] _add points to the timestamp to add to the first argument
- *
- *  @return This method returns the number of seconds @a time increased by.
- */
-static inline uint32_t _Timestamp64_Add_to_at_tick(
-  Timestamp64_Control *_time,
-  const Timestamp64_Control *_add
-)
-{
-  Timestamp64_Control _start = *_time / 1000000000L;
-  *_time += *_add;
-  if ( ((*_time) / 1000000000L) != _start ) {
-    return 1;
-  }
-  return 0;
-}
-
-/**
  *  @brief Convert Timestamp to Number of Ticks
  *
  *  This routine convert the @a time timestamp to the corresponding number
diff --git a/cpukit/score/include/rtems/score/tod.h b/cpukit/score/include/rtems/score/tod.h
index 06cc900..cfb0fe7 100644
--- a/cpukit/score/include/rtems/score/tod.h
+++ b/cpukit/score/include/rtems/score/tod.h
@@ -137,6 +137,15 @@ typedef struct {
   Timestamp_Control uptime;
 
   /**
+   * @brief Time of day seconds trigger.
+   *
+   * This value specifies the nanoseconds since the last time of day second.
+   * It is updated and evaluated in _TOD_Tickle_ticks().  It is set in
+   * _TOD_Set_with_timestamp().
+   */
+  uint32_t seconds_trigger;
+
+  /**
    *  @brief Indicates if the time of day is set.
    *
    *  This is true if the application has set the current
diff --git a/cpukit/score/src/coretodset.c b/cpukit/score/src/coretodset.c
index 9708ef9..8b0928a 100644
--- a/cpukit/score/src/coretodset.c
+++ b/cpukit/score/src/coretodset.c
@@ -25,6 +25,7 @@ void _TOD_Set_with_timestamp(
   const Timestamp_Control *tod
 )
 {
+  uint32_t nanoseconds = _Timestamp_Get_nanoseconds( tod );
   Watchdog_Interval seconds_next = _Timestamp_Get_seconds( tod );
   Watchdog_Interval seconds_now;
 
@@ -39,6 +40,7 @@ void _TOD_Set_with_timestamp(
     _Watchdog_Adjust_seconds( WATCHDOG_FORWARD, seconds_next - seconds_now );
 
   _TOD.now = *tod;
+  _TOD.seconds_trigger = nanoseconds;
   _TOD.is_set = true;
 
   _TOD_Activate();
diff --git a/cpukit/score/src/coretodtickle.c b/cpukit/score/src/coretodtickle.c
index fbf67fd..d0412f8 100644
--- a/cpukit/score/src/coretodtickle.c
+++ b/cpukit/score/src/coretodtickle.c
@@ -35,23 +35,27 @@
 void _TOD_Tickle_ticks( void )
 {
   Timestamp_Control tick;
-  uint32_t          seconds;
+  uint32_t          nanoseconds_per_tick;
+
+  nanoseconds_per_tick = rtems_configuration_get_nanoseconds_per_tick();
 
   /* Convert the tick quantum to a timestamp */
-  _Timestamp_Set( &tick, 0, rtems_configuration_get_nanoseconds_per_tick() );
+  _Timestamp_Set( &tick, 0, nanoseconds_per_tick );
 
   /* Update the counter of ticks since boot */
   _Watchdog_Ticks_since_boot += 1;
 
-  /* Update the timespec format uptime */
+  /* Update the uptime */
   _Timestamp_Add_to( &_TOD.uptime, &tick );
   /* we do not care how much the uptime changed */
 
-  /* Update the timespec format TOD */
-  seconds = _Timestamp_Add_to_at_tick( &_TOD.now, &tick );
-  while ( seconds ) {
+  /* Update the current TOD */
+  _Timestamp_Add_to( &_TOD.now, &tick );
+
+  _TOD.seconds_trigger += nanoseconds_per_tick;
+  if ( _TOD.seconds_trigger >= 1000000000UL ) {
+    _TOD.seconds_trigger -= 1000000000UL;
     _Watchdog_Tickle_seconds();
-    seconds--;
   }
 }
 




More information about the vc mailing list