From d37adfe5dd82cc3c933eb521b8f800c342af0e52 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 3 Mar 2016 07:02:03 +0100 Subject: score: Fix CPU time used by executing threads The CPU time used of a thread was previously maintained per-processor mostly during _Thread_Dispatch(). However, on SMP configurations the actual processor of a thread is difficult to figure out since thread dispatching is a highly asynchronous process (e.g. via inter-processor interrupts). Only the intended processor of a thread is known to the scheduler easily. Do the CPU usage accounting during thread heir updates in the context of the scheduler operations. Provide the function _Thread_Get_CPU_time_used() to get the CPU usage of a thread using proper locks to get a consistent value. Close #2627. --- cpukit/libcsupport/src/__times.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'cpukit/libcsupport/src/__times.c') diff --git a/cpukit/libcsupport/src/__times.c b/cpukit/libcsupport/src/__times.c index ea6d1c65a7..a30f720f20 100644 --- a/cpukit/libcsupport/src/__times.c +++ b/cpukit/libcsupport/src/__times.c @@ -26,9 +26,11 @@ #include #include -#include #include -#include + +#include +#include + #include #include #include @@ -42,11 +44,12 @@ clock_t _times( ) { rtems_interval ticks, us_per_tick; - Thread_Control *executing; if ( !ptms ) rtems_set_errno_and_return_minus_one( EFAULT ); + memset( ptms, 0, sizeof( *ptms ) ); + /* * This call does not depend on TOD being initialized and can't fail. */ @@ -62,11 +65,12 @@ clock_t _times( * this thread. */ { + Timestamp_Control cpu_time_used; Timestamp_Control per_tick; uint32_t ticks_of_executing; uint32_t fractional_ticks; - Per_CPU_Control *cpu_self; + _Thread_Get_CPU_time_used( _Thread_Get_executing(), &cpu_time_used ); _Timestamp_Set( &per_tick, rtems_configuration_get_microseconds_per_tick() / @@ -74,25 +78,17 @@ clock_t _times( (rtems_configuration_get_nanoseconds_per_tick() % TOD_NANOSECONDS_PER_SECOND) ); - - cpu_self = _Thread_Dispatch_disable(); - executing = _Thread_Executing; - _Thread_Update_cpu_time_used( - executing, - &_Thread_Time_of_last_context_switch - ); _Timestamp_Divide( - &executing->cpu_time_used, + &cpu_time_used, &per_tick, &ticks_of_executing, &fractional_ticks ); - _Thread_Dispatch_enable( cpu_self ); + ptms->tms_utime = ticks_of_executing * us_per_tick; } + ptms->tms_stime = ticks * us_per_tick; - ptms->tms_cutime = 0; - ptms->tms_cstime = 0; return ticks * us_per_tick; } -- cgit v1.2.3