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/libmisc/cpuuse/cpuusagereport.c | 51 ++++++++++++---------------------- cpukit/libmisc/cpuuse/cpuusagereset.c | 7 ++++- cpukit/libmisc/cpuuse/cpuusagetop.c | 23 ++------------- 3 files changed, 27 insertions(+), 54 deletions(-) (limited to 'cpukit/libmisc') diff --git a/cpukit/libmisc/cpuuse/cpuusagereport.c b/cpukit/libmisc/cpuuse/cpuusagereport.c index 98b5f8bfae..95d06480d6 100644 --- a/cpukit/libmisc/cpuuse/cpuusagereport.c +++ b/cpukit/libmisc/cpuuse/cpuusagereport.c @@ -44,7 +44,7 @@ void rtems_cpu_usage_report_with_plugin( Objects_Information *information; char name[13]; uint32_t ival, fval; - Timestamp_Control uptime, total, ran, uptime_at_last_reset; + Timestamp_Control uptime, total, used, uptime_at_last_reset; uint32_t seconds, nanoseconds; if ( !print ) @@ -90,38 +90,23 @@ void rtems_cpu_usage_report_with_plugin( name ); - { - Timestamp_Control last; - - /* - * If this is the currently executing thread, account for time - * since the last context switch. - */ - ran = the_thread->cpu_time_used; - if ( _Thread_Get_time_of_last_context_switch( the_thread, &last ) ) { - Timestamp_Control used; - _TOD_Get_uptime( &uptime ); - _Timestamp_Subtract( &last, &uptime, &used ); - _Timestamp_Add_to( &ran, &used ); - } else { - _TOD_Get_uptime( &uptime ); - } - _Timestamp_Subtract( &uptime_at_last_reset, &uptime, &total ); - _Timestamp_Divide( &ran, &total, &ival, &fval ); - - /* - * Print the information - */ - - seconds = _Timestamp_Get_seconds( &ran ); - nanoseconds = _Timestamp_Get_nanoseconds( &ran ) / - TOD_NANOSECONDS_PER_MICROSECOND; - (*print)( context, - "%7" PRIu32 ".%06" PRIu32 " |%4" PRIu32 ".%03" PRIu32 "\n", - seconds, nanoseconds, - ival, fval - ); - } + _Thread_Get_CPU_time_used( the_thread, &used ); + _TOD_Get_uptime( &uptime ); + _Timestamp_Subtract( &uptime_at_last_reset, &uptime, &total ); + _Timestamp_Divide( &used, &total, &ival, &fval ); + + /* + * Print the information + */ + + seconds = _Timestamp_Get_seconds( &used ); + nanoseconds = _Timestamp_Get_nanoseconds( &used ) / + TOD_NANOSECONDS_PER_MICROSECOND; + (*print)( context, + "%7" PRIu32 ".%06" PRIu32 " |%4" PRIu32 ".%03" PRIu32 "\n", + seconds, nanoseconds, + ival, fval + ); } } } diff --git a/cpukit/libmisc/cpuuse/cpuusagereset.c b/cpukit/libmisc/cpuuse/cpuusagereset.c index 8e9fe54ced..6ef50f9b25 100644 --- a/cpukit/libmisc/cpuuse/cpuusagereset.c +++ b/cpukit/libmisc/cpuuse/cpuusagereset.c @@ -21,13 +21,18 @@ #include #include #include +#include #include static void CPU_usage_Per_thread_handler( Thread_Control *the_thread ) { + ISR_lock_Context lock_context; + + _Scheduler_Acquire( the_thread, &lock_context ); _Timestamp_Set_to_zero( &the_thread->cpu_time_used ); + _Scheduler_Release( the_thread, &lock_context ); } /* @@ -44,7 +49,7 @@ void rtems_cpu_usage_reset( void ) for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) { Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index ); - cpu->time_of_last_context_switch = CPU_usage_Uptime_at_last_reset; + cpu->cpu_usage_timestamp = CPU_usage_Uptime_at_last_reset; } rtems_iterate_over_all_threads(CPU_usage_Per_thread_handler); diff --git a/cpukit/libmisc/cpuuse/cpuusagetop.c b/cpukit/libmisc/cpuuse/cpuusagetop.c index 6355e4e636..9b928588c7 100644 --- a/cpukit/libmisc/cpuuse/cpuusagetop.c +++ b/cpukit/libmisc/cpuuse/cpuusagetop.c @@ -218,12 +218,14 @@ static void task_usage(Thread_Control* thread, void* arg) { rtems_cpu_usage_data* data = (rtems_cpu_usage_data*) arg; - Timestamp_Control usage = thread->cpu_time_used; + Timestamp_Control usage; Timestamp_Control current = data->zero; int j; data->stack_size += thread->Start.Initial_stack.size; + _Thread_Get_CPU_time_used(thread, &usage); + for (j = 0; j < data->last_task_count; j++) { if (thread == data->last_tasks[j]) @@ -496,25 +498,6 @@ rtems_cpuusage_top_thread (rtems_task_argument arg) usage = data->usage[i]; current_usage = data->current_usage[i]; - /* - * If this is the currently executing thread, account for time since - * the last context switch. - */ - if (_Thread_Get_time_of_last_context_switch(thread, &last)) - { - Timestamp_Control used; - Timestamp_Control now; - - /* - * Get the current uptime and assume we are not pre-empted to - * measure the time from the last switch this thread and now. - */ - _TOD_Get_uptime(&now); - _Timestamp_Subtract(&last, &now, &used); - _Timestamp_Add_to(&usage, &used); - _Timestamp_Add_to(¤t_usage, &used); - } - /* * Print the information */ -- cgit v1.2.3