diff --git a/src/fsw_misc.c b/src/fsw_misc.c --- a/src/fsw_misc.c +++ b/src/fsw_misc.c @@ -793,6 +793,10 @@ void get_v_e1_e2_f3( unsigned char *spac void get_cpu_load( unsigned char *resource_statistics ) { +#define LOAD_AVG_SIZE 60 + static unsigned char cpu_load_hist[LOAD_AVG_SIZE]={0}; + static char old_avg_pos=0; + static unsigned int cpu_load_avg; unsigned char cpu_load; cpu_load = lfr_rtems_cpu_usage_report(); @@ -806,8 +810,12 @@ void get_cpu_load( unsigned char *resour resource_statistics[1] = cpu_load; } + cpu_load_avg = cpu_load_avg - (unsigned int)cpu_load_hist[(int)old_avg_pos] + (unsigned int)cpu_load; + cpu_load_hist[(int)old_avg_pos] = cpu_load; + old_avg_pos += 1; + old_avg_pos %= LOAD_AVG_SIZE; // CPU_LOAD_AVE - resource_statistics[BYTE_2] = 0; + resource_statistics[BYTE_2] = (unsigned char)(cpu_load_avg / LOAD_AVG_SIZE); #ifndef PRINT_TASK_STATISTICS rtems_cpu_usage_reset(); diff --git a/src/lfr_cpu_usage_report.c b/src/lfr_cpu_usage_report.c --- a/src/lfr_cpu_usage_report.c +++ b/src/lfr_cpu_usage_report.c @@ -12,10 +12,14 @@ */ #include "lfr_cpu_usage_report.h" +#include "fsw_params.h" + +extern rtems_id Task_id[]; unsigned char lfr_rtems_cpu_usage_report( void ) { uint32_t api_index; + uint32_t information_index; Thread_Control *the_thread; Objects_Information *information; uint32_t ival; @@ -24,8 +28,13 @@ unsigned char lfr_rtems_cpu_usage_report Timestamp_Control uptime; Timestamp_Control total; Timestamp_Control ran; + Timestamp_Control abs_total; + Timestamp_Control abs_ran; + + static Timestamp_Control last_total={0,0}; + static Timestamp_Control last_ran={0,0}; #else - uint32_t total_units = 0; + #error "Can't compute CPU usage using ticks on LFR" #endif unsigned char cpu_load; @@ -33,33 +42,8 @@ unsigned char lfr_rtems_cpu_usage_report ival = 0; cpu_load = 0; - /* - * When not using nanosecond CPU usage resolution, we have to count - * the number of "ticks" we gave credit for to give the user a rough - * guideline as to what each number means proportionally. - */ -#ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ _TOD_Get_uptime( &uptime ); - _Timestamp_Subtract( &CPU_usage_Uptime_at_last_reset, &uptime, &total ); -#else - for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST ; api_index++ ) { - if ( !_Objects_Information_table[ api_index ] ) { } - else - { - information = _Objects_Information_table[ api_index ][ 1 ]; - if ( information != NULL ) - { - for ( i=1 ; i <= information->maximum ; i++ ) { - the_thread = (Thread_Control *)information->local_table[ i ]; - - if ( the_thread != NULL ) { - total_units += the_thread->cpu_time_used; } - } - } - } - } -#endif - + _Timestamp_Subtract( &CPU_usage_Uptime_at_last_reset, &uptime, &abs_total ); for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST ; api_index++ ) { if ( !_Objects_Information_table[ api_index ] ) { } @@ -68,50 +52,42 @@ unsigned char lfr_rtems_cpu_usage_report information = _Objects_Information_table[ api_index ][ 1 ]; if ( information != NULL ) { - the_thread = (Thread_Control *)information->local_table[ 1 ]; - - if ( the_thread == NULL ) { } - else + for(information_index=1;information_index<=information->maximum;information_index++) { - #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ - /* - * If this is the currently executing thread, account for time - * since the last context switch. - */ - ran = the_thread->cpu_time_used; - if ( _Thread_Executing->Object.id == the_thread->Object.id ) + the_thread = (Thread_Control *)information->local_table[ information_index ]; + + if ( the_thread == NULL) { } + else if(the_thread->Object.id == Task_id[TASKID_SCRB]) // Only measure scrubbing task load, CPU load is 100%-Scrubbing { - Timestamp_Control used; - _Timestamp_Subtract( - &_Thread_Time_of_last_context_switch, &uptime, &used - ); - _Timestamp_Add_to( &ran, &used ); + /* + * If this is the currently executing thread, account for time + * since the last context switch. + */ + abs_ran = the_thread->cpu_time_used; + if ( _Thread_Executing->Object.id == the_thread->Object.id ) + { + Timestamp_Control used; + _Timestamp_Subtract( + &_Thread_Time_of_last_context_switch, &uptime, &used + ); + _Timestamp_Add_to( &abs_ran, &used ); + } + /* + * Only consider the time since last call + */ + _Timespec_Subtract(&last_ran, &abs_ran, &ran); + _Timespec_Subtract(&last_total, &abs_total, &total); + + last_ran = abs_ran; + last_total = abs_total; + + _Timestamp_Divide( &ran, &total, &ival, &fval); + cpu_load = (unsigned char)(CONST_100 - ival); } - _Timestamp_Divide( &ran, &total, &ival, &fval ); - - #else - if (total_units != 0) - { - uint64_t ival_64; - - ival_64 = the_thread->cpu_time_used; - ival_64 *= CONST_100000; - ival = ival_64 / total_units; - } - else - { - ival = 0; - } - - fval = ival % CONST_1000; - ival /= CONST_1000; - #endif } } } } - cpu_load = (unsigned char) (CONST_100 - ival); - return cpu_load; }