[PATCH] score: Add and use _Thread_Update_cpu_time_used()

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Jul 29 15:54:53 UTC 2013


Fix _times().
---
 cpukit/libcsupport/src/__times.c              |   20 +++++++++++++++-----
 cpukit/score/include/rtems/score/threadimpl.h |   19 +++++++++++++++++++
 cpukit/score/src/threaddispatch.c             |   15 ++++-----------
 testsuites/psxtests/psxtimes01/init.c         |    9 +++++----
 4 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/cpukit/libcsupport/src/__times.c b/cpukit/libcsupport/src/__times.c
index f143bb8..4596453 100644
--- a/cpukit/libcsupport/src/__times.c
+++ b/cpukit/libcsupport/src/__times.c
@@ -29,6 +29,7 @@
 #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
   #include <rtems/score/timestamp.h>
 #endif
+#include <rtems/score/threadimpl.h>
 
 /**
  *  POSIX 1003.1b 4.5.2 - Get Process Times
@@ -38,6 +39,7 @@ clock_t _times(
 )
 {
   rtems_interval ticks;
+  Thread_Control *executing;
 
   if ( !ptms )
     rtems_set_errno_and_return_minus_one( EFAULT );
@@ -59,7 +61,7 @@ clock_t _times(
   #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
     {
       Timestamp_Control per_tick;
-      uint32_t          ticks;
+      uint32_t          ticks_of_executing;
       uint32_t          fractional_ticks;
 
       _Timestamp_Set(
@@ -70,16 +72,24 @@ clock_t _times(
             TOD_NANOSECONDS_PER_SECOND)
       );
 
+      _Thread_Disable_dispatch();
+      executing = _Thread_Executing;
+      _Thread_Update_cpu_time_used(
+        executing,
+        &_Thread_Time_of_last_context_switch
+      );
       _Timestamp_Divide(
-        &_Thread_Get_executing()->cpu_time_used,
+        &executing->cpu_time_used,
         &per_tick,
-        &ticks,
+        &ticks_of_executing,
         &fractional_ticks
       );
-      ptms->tms_utime = ticks;
+      _Thread_Enable_dispatch();
+      ptms->tms_utime = ticks_of_executing / 100;
     }
   #else
-    ptms->tms_utime  = _Thread_Get_executing()->cpu_time_used;
+    executing = _Thread_Get_executing();
+    ptms->tms_utime  = executing->cpu_time_used;
   #endif
   ptms->tms_stime  = ticks;
   ptms->tms_cutime = 0;
diff --git a/cpukit/score/include/rtems/score/threadimpl.h b/cpukit/score/include/rtems/score/threadimpl.h
index 350b124..ad438ce 100644
--- a/cpukit/score/include/rtems/score/threadimpl.h
+++ b/cpukit/score/include/rtems/score/threadimpl.h
@@ -23,6 +23,7 @@
 #include <rtems/score/isr.h>
 #include <rtems/score/objectimpl.h>
 #include <rtems/score/statesimpl.h>
+#include <rtems/score/tod.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -655,6 +656,24 @@ RTEMS_INLINE_ROUTINE void _Thread_Signal_notification( Thread_Control *thread )
   }
 }
 
+RTEMS_INLINE_ROUTINE void _Thread_Update_cpu_time_used(
+  Thread_Control *executing,
+  Timestamp_Control *time_of_last_context_switch
+)
+{
+  Timestamp_Control uptime;
+  Timestamp_Control ran;
+
+  _TOD_Get_uptime( &uptime );
+  _Timestamp_Subtract(
+    time_of_last_context_switch,
+    &uptime,
+    &ran
+  );
+  *time_of_last_context_switch = uptime;
+  _Timestamp_Add_to( &executing->cpu_time_used, &ran );
+}
+
 #if !defined(__DYNAMIC_REENT__)
 /**
  * This routine returns the C library re-enterant pointer.
diff --git a/cpukit/score/src/threaddispatch.c b/cpukit/score/src/threaddispatch.c
index c5d0a21..09b5dbe 100644
--- a/cpukit/score/src/threaddispatch.c
+++ b/cpukit/score/src/threaddispatch.c
@@ -105,17 +105,10 @@ void _Thread_Dispatch( void )
     _ISR_Enable( level );
 
     #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
-      {
-        Timestamp_Control uptime, ran;
-        _TOD_Get_uptime( &uptime );
-        _Timestamp_Subtract(
-          &_Thread_Time_of_last_context_switch,
-          &uptime,
-          &ran
-        );
-        _Timestamp_Add_to( &executing->cpu_time_used, &ran );
-        _Thread_Time_of_last_context_switch = uptime;
-      }
+      _Thread_Update_cpu_time_used(
+        executing,
+        &_Thread_Time_of_last_context_switch
+      );
     #else
       {
         _TOD_Get_uptime( &_Thread_Time_of_last_context_switch );
diff --git a/testsuites/psxtests/psxtimes01/init.c b/testsuites/psxtests/psxtimes01/init.c
index 640fe57..d308935 100644
--- a/testsuites/psxtests/psxtimes01/init.c
+++ b/testsuites/psxtests/psxtimes01/init.c
@@ -32,6 +32,7 @@ rtems_task Init(
   clock_t    difference;
   struct tms start_tm;
   struct tms end_tm;
+  int        interval = 5;
 
   puts( "\n\n*** TEST TIMES 01 ***" );
 
@@ -53,7 +54,7 @@ rtems_task Init(
   rtems_test_assert( start != 0 );
   rtems_test_assert( now != 0 );
   
-  rtems_test_spin_for_ticks(5);
+  rtems_test_spin_for_ticks( interval );
 
   puts( "_times( &end_tm ) -- OK" );
   end = _times( &end_tm );
@@ -61,10 +62,10 @@ rtems_task Init(
   
   puts( "Check various values" );
   difference = end - start;
-  rtems_test_assert( difference >= 5 );
+  rtems_test_assert( difference >= interval );
 
-  rtems_test_assert( end_tm.tms_utime >= start_tm.tms_utime );
-  rtems_test_assert( end_tm.tms_stime >= start_tm.tms_stime );
+  rtems_test_assert( end_tm.tms_utime - start_tm.tms_utime >= interval );
+  rtems_test_assert( end_tm.tms_stime - start_tm.tms_stime >= interval );
   rtems_test_assert( end_tm.tms_cutime == 0 );
   rtems_test_assert( end_tm.tms_cstime == 0 );
   
-- 
1.7.7




More information about the devel mailing list