diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2016-03-03 07:02:03 +0100 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2016-03-17 08:27:47 +0100 |
commit | d37adfe5dd82cc3c933eb521b8f800c342af0e52 (patch) | |
tree | 5f4a77976b9ae35594d00b0bb58e18bb62a9b541 /cpukit/score/include | |
parent | bsp/realview_pbx_a9_qemu: Fix compiler flags (diff) | |
download | rtems-d37adfe5dd82cc3c933eb521b8f800c342af0e52.tar.bz2 |
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.
Diffstat (limited to 'cpukit/score/include')
-rw-r--r-- | cpukit/score/include/rtems/score/percpu.h | 15 | ||||
-rw-r--r-- | cpukit/score/include/rtems/score/schedulerimpl.h | 32 | ||||
-rw-r--r-- | cpukit/score/include/rtems/score/threadimpl.h | 67 |
3 files changed, 50 insertions, 64 deletions
diff --git a/cpukit/score/include/rtems/score/percpu.h b/cpukit/score/include/rtems/score/percpu.h index 22017889b3..95c93ddf3a 100644 --- a/cpukit/score/include/rtems/score/percpu.h +++ b/cpukit/score/include/rtems/score/percpu.h @@ -333,8 +333,17 @@ typedef struct Per_CPU_Control { */ volatile bool dispatch_necessary; - /** This is the time of the last context switch on this CPU. */ - Timestamp_Control time_of_last_context_switch; + /** + * @brief The CPU usage timestamp contains the time point of the last heir + * thread change or last CPU usage update of the executing thread of this + * processor. + * + * Protected by the scheduler lock. + * + * @see _Scheduler_Update_heir(), _Thread_Dispatch_update_heir() and + * _Thread_Get_CPU_time_used(). + */ + Timestamp_Control cpu_usage_timestamp; /** * @brief Watchdog state for this processor. @@ -681,8 +690,6 @@ bool _Per_CPU_State_wait_for_non_initial_state( _Per_CPU_Get()->interrupt_stack_high #define _Thread_Dispatch_necessary \ _Per_CPU_Get()->dispatch_necessary -#define _Thread_Time_of_last_context_switch \ - _Per_CPU_Get()->time_of_last_context_switch /** * @brief Returns the thread control block of the executing thread. diff --git a/cpukit/score/include/rtems/score/schedulerimpl.h b/cpukit/score/include/rtems/score/schedulerimpl.h index d50c36a7b9..a41b7ea62f 100644 --- a/cpukit/score/include/rtems/score/schedulerimpl.h +++ b/cpukit/score/include/rtems/score/schedulerimpl.h @@ -627,19 +627,6 @@ bool _Scheduler_Set_affinity( #endif /* defined(__RTEMS_HAVE_SYS_CPUSET_H__) */ -RTEMS_INLINE_ROUTINE void _Scheduler_Update_heir( - Thread_Control *new_heir, - bool force_dispatch -) -{ - Thread_Control *heir = _Thread_Heir; - - if ( heir != new_heir && ( heir->is_preemptible || force_dispatch ) ) { - _Thread_Heir = new_heir; - _Thread_Dispatch_necessary = true; - } -} - RTEMS_INLINE_ROUTINE void _Scheduler_Generic_block( const Scheduler_Control *scheduler, Thread_Control *the_thread, @@ -1356,6 +1343,25 @@ RTEMS_INLINE_ROUTINE bool _Scheduler_Ask_blocked_node_for_help( ISR_LOCK_DECLARE( extern, _Scheduler_Lock ) +RTEMS_INLINE_ROUTINE void _Scheduler_Update_heir( + Thread_Control *new_heir, + bool force_dispatch +) +{ + Thread_Control *heir = _Thread_Heir; + + if ( heir != new_heir && ( heir->is_preemptible || force_dispatch ) ) { +#if defined(RTEMS_SMP) + /* We need this state only for _Thread_Get_CPU_time_used() */ + _Scheduler_Thread_change_state( heir, THREAD_SCHEDULER_BLOCKED ); + _Scheduler_Thread_change_state( new_heir, THREAD_SCHEDULER_SCHEDULED ); +#endif + _Thread_Update_CPU_time_used( heir, _Thread_Get_CPU( heir ) ); + _Thread_Heir = new_heir; + _Thread_Dispatch_necessary = true; + } +} + /** * @brief Acquires the scheduler instance of the thread. * diff --git a/cpukit/score/include/rtems/score/threadimpl.h b/cpukit/score/include/rtems/score/threadimpl.h index ec9851dba0..3f8df856d5 100644 --- a/cpukit/score/include/rtems/score/threadimpl.h +++ b/cpukit/score/include/rtems/score/threadimpl.h @@ -578,36 +578,6 @@ RTEMS_INLINE_ROUTINE bool _Thread_Is_executing_on_a_processor( #endif /** - * @brief Returns @a true and sets time_of_context_switch to the - * time of the last context switch when the thread is currently executing - * in the system, otherwise @a false. - */ -RTEMS_INLINE_ROUTINE bool _Thread_Get_time_of_last_context_switch( - Thread_Control *the_thread, - Timestamp_Control *time_of_context_switch -) -{ - bool retval = false; - - _Thread_Disable_dispatch(); - #ifndef RTEMS_SMP - if ( _Thread_Executing->Object.id == the_thread->Object.id ) { - *time_of_context_switch = _Thread_Time_of_last_context_switch; - retval = true; - } - #else - if ( _Thread_Is_executing_on_a_processor( the_thread ) ) { - *time_of_context_switch = - _Thread_Get_CPU( the_thread )->time_of_last_context_switch; - retval = true; - } - #endif - _Thread_Enable_dispatch(); - return retval; -} - - -/** * This function returns true if the_thread is the heir * thread, and false otherwise. */ @@ -803,6 +773,20 @@ RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Get_heir_and_make_it_executing( return heir; } +RTEMS_INLINE_ROUTINE void _Thread_Update_CPU_time_used( + Thread_Control *the_thread, + Per_CPU_Control *cpu +) +{ + Timestamp_Control last; + Timestamp_Control ran; + + last = cpu->cpu_usage_timestamp; + _TOD_Get_uptime( &cpu->cpu_usage_timestamp ); + _Timestamp_Subtract( &last, &cpu->cpu_usage_timestamp, &ran ); + _Timestamp_Add_to( &the_thread->cpu_time_used, &ran ); +} + #if defined( RTEMS_SMP ) RTEMS_INLINE_ROUTINE void _Thread_Dispatch_update_heir( Per_CPU_Control *cpu_self, @@ -810,6 +794,8 @@ RTEMS_INLINE_ROUTINE void _Thread_Dispatch_update_heir( Thread_Control *heir ) { + _Thread_Update_CPU_time_used( cpu_for_heir->heir, cpu_for_heir ); + cpu_for_heir->heir = heir; if ( cpu_for_heir == cpu_self ) { @@ -820,23 +806,10 @@ RTEMS_INLINE_ROUTINE void _Thread_Dispatch_update_heir( } #endif -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 ); -} +void _Thread_Get_CPU_time_used( + Thread_Control *the_thread, + Timestamp_Control *cpu_time_used +); RTEMS_INLINE_ROUTINE void _Thread_Action_control_initialize( Thread_Action_control *action_control |