summaryrefslogtreecommitdiffstats
path: root/cpukit/libmisc
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2016-03-03 07:02:03 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2016-03-17 08:27:47 +0100
commitd37adfe5dd82cc3c933eb521b8f800c342af0e52 (patch)
tree5f4a77976b9ae35594d00b0bb58e18bb62a9b541 /cpukit/libmisc
parentbsp/realview_pbx_a9_qemu: Fix compiler flags (diff)
downloadrtems-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/libmisc')
-rw-r--r--cpukit/libmisc/cpuuse/cpuusagereport.c51
-rw-r--r--cpukit/libmisc/cpuuse/cpuusagereset.c7
-rw-r--r--cpukit/libmisc/cpuuse/cpuusagetop.c23
3 files changed, 27 insertions, 54 deletions
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 <rtems/cpuuse.h>
#include <rtems/score/percpu.h>
#include <rtems/score/todimpl.h>
+#include <rtems/score/schedulerimpl.h>
#include <rtems/score/watchdogimpl.h>
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])
@@ -497,25 +499,6 @@ rtems_cpuusage_top_thread (rtems_task_argument arg)
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(&current_usage, &used);
- }
-
- /*
* Print the information
*/
print_time(data, &usage, 19);