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/score/include/rtems/score/threadimpl.h | 67 ++++++++------------------- 1 file changed, 20 insertions(+), 47 deletions(-) (limited to 'cpukit/score/include/rtems/score/threadimpl.h') 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 @@ -577,36 +577,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 -- cgit v1.2.3