summaryrefslogtreecommitdiffstats
path: root/cpukit/score
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/score
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/score')
-rw-r--r--cpukit/score/Makefile.am1
-rw-r--r--cpukit/score/include/rtems/score/percpu.h15
-rw-r--r--cpukit/score/include/rtems/score/schedulerimpl.h32
-rw-r--r--cpukit/score/include/rtems/score/threadimpl.h67
-rw-r--r--cpukit/score/src/schedulersmpstartidle.c1
-rw-r--r--cpukit/score/src/threaddispatch.c5
-rw-r--r--cpukit/score/src/threadgetcputimeused.c47
7 files changed, 99 insertions, 69 deletions
diff --git a/cpukit/score/Makefile.am b/cpukit/score/Makefile.am
index af78f15645..b6824ad446 100644
--- a/cpukit/score/Makefile.am
+++ b/cpukit/score/Makefile.am
@@ -305,6 +305,7 @@ libscore_a_SOURCES += src/thread.c src/threadchangepriority.c \
libscore_a_SOURCES += src/threadentryadaptoridle.c
libscore_a_SOURCES += src/threadentryadaptornumeric.c
libscore_a_SOURCES += src/threadentryadaptorpointer.c
+libscore_a_SOURCES += src/threadgetcputimeused.c
libscore_a_SOURCES += src/threadglobalconstruction.c
libscore_a_SOURCES += src/threadtimeout.c
libscore_a_SOURCES += src/threadyield.c
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
diff --git a/cpukit/score/src/schedulersmpstartidle.c b/cpukit/score/src/schedulersmpstartidle.c
index de125d3323..0ffb628550 100644
--- a/cpukit/score/src/schedulersmpstartidle.c
+++ b/cpukit/score/src/schedulersmpstartidle.c
@@ -22,6 +22,7 @@ void _Scheduler_SMP_Start_idle(
Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
Scheduler_SMP_Node *node = _Scheduler_SMP_Thread_get_node( thread );
+ _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_SCHEDULED );
node->state = SCHEDULER_SMP_NODE_SCHEDULED;
_Thread_Set_CPU( thread, cpu );
diff --git a/cpukit/score/src/threaddispatch.c b/cpukit/score/src/threaddispatch.c
index 1b36c93fbb..ce5d118a22 100644
--- a/cpukit/score/src/threaddispatch.c
+++ b/cpukit/score/src/threaddispatch.c
@@ -108,11 +108,6 @@ void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level )
_ISR_Enable( level );
#endif
- _Thread_Update_cpu_time_used(
- executing,
- &cpu_self->time_of_last_context_switch
- );
-
_User_extensions_Thread_switch( executing, heir );
_Thread_Save_fp( executing );
_Context_Switch( &executing->Registers, &heir->Registers );
diff --git a/cpukit/score/src/threadgetcputimeused.c b/cpukit/score/src/threadgetcputimeused.c
new file mode 100644
index 0000000000..6bfe8ea216
--- /dev/null
+++ b/cpukit/score/src/threadgetcputimeused.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2016 embedded brains GmbH. All rights reserved.
+ *
+ * embedded brains GmbH
+ * Dornierstr. 4
+ * 82178 Puchheim
+ * Germany
+ * <rtems@embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/score/threadimpl.h>
+#include <rtems/score/schedulerimpl.h>
+
+static bool _Thread_Is_scheduled( const Thread_Control *the_thread )
+{
+#if defined(RTEMS_SMP)
+ return the_thread->Scheduler.state == THREAD_SCHEDULER_SCHEDULED;
+#else
+ return _Thread_Is_executing( the_thread );
+#endif
+}
+
+void _Thread_Get_CPU_time_used(
+ Thread_Control *the_thread,
+ Timestamp_Control *cpu_time_used
+)
+{
+ ISR_lock_Context lock_context;
+
+ _Scheduler_Acquire( the_thread, &lock_context );
+
+ if ( _Thread_Is_scheduled( the_thread ) ) {
+ _Thread_Update_CPU_time_used( the_thread, _Thread_Get_CPU( the_thread ) );
+ }
+
+ *cpu_time_used = the_thread->cpu_time_used;
+
+ _Scheduler_Release( the_thread, &lock_context );
+}