source: rtems/cpukit/score/src/threaddispatch.c @ 469dc47

5
Last change on this file since 469dc47 was d37adfe5, checked in by Sebastian Huber <sebastian.huber@…>, on 03/03/16 at 06:02:03

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.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief Dispatch Thread
5 * @ingroup ScoreThread
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2009.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  Copyright (c) 2014 embedded brains GmbH.
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <rtems/score/threaddispatch.h>
24#include <rtems/score/assert.h>
25#include <rtems/score/isr.h>
26#include <rtems/score/threadimpl.h>
27#include <rtems/score/todimpl.h>
28#include <rtems/score/userextimpl.h>
29#include <rtems/score/wkspace.h>
30#include <rtems/config.h>
31
32#if __RTEMS_ADA__
33void *rtems_ada_self;
34#endif
35
36#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
37Thread_Control *_Thread_Allocated_fp;
38#endif
39
40CHAIN_DEFINE_EMPTY( _User_extensions_Switches_list );
41
42static Thread_Action *_Thread_Get_post_switch_action(
43  Thread_Control *executing
44)
45{
46  Chain_Control *chain = &executing->Post_switch_actions.Chain;
47
48  return (Thread_Action *) _Chain_Get_unprotected( chain );
49}
50
51static void _Thread_Run_post_switch_actions( Thread_Control *executing )
52{
53  ISR_Level        level;
54  Per_CPU_Control *cpu_self;
55  Thread_Action   *action;
56
57  cpu_self = _Thread_Action_ISR_disable_and_acquire( executing, &level );
58  action = _Thread_Get_post_switch_action( executing );
59
60  while ( action != NULL ) {
61    _Chain_Set_off_chain( &action->Node );
62
63    ( *action->handler )( executing, action, cpu_self, level );
64
65    cpu_self = _Thread_Action_ISR_disable_and_acquire( executing, &level );
66    action = _Thread_Get_post_switch_action( executing );
67  }
68
69  _Thread_Action_release_and_ISR_enable( cpu_self, level );
70}
71
72void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level )
73{
74  Thread_Control *executing;
75
76  _Assert( cpu_self->thread_dispatch_disable_level == 1 );
77
78  executing = cpu_self->executing;
79
80  do {
81    Thread_Control *heir = _Thread_Get_heir_and_make_it_executing( cpu_self );
82
83    /*
84     *  When the heir and executing are the same, then we are being
85     *  requested to do the post switch dispatching.  This is normally
86     *  done to dispatch signals.
87     */
88    if ( heir == executing )
89      goto post_switch;
90
91    /*
92     *  Since heir and executing are not the same, we need to do a real
93     *  context switch.
94     */
95#if __RTEMS_ADA__
96    executing->rtems_ada_self = rtems_ada_self;
97    rtems_ada_self = heir->rtems_ada_self;
98#endif
99    if ( heir->budget_algorithm == THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE )
100      heir->cpu_time_budget = rtems_configuration_get_ticks_per_timeslice();
101
102    /*
103     * On SMP the complete context switch must be atomic with respect to one
104     * processor.  See also _Thread_Handler() since _Context_switch() may branch
105     * to this function.
106     */
107#if !defined( RTEMS_SMP )
108    _ISR_Enable( level );
109#endif
110
111    _User_extensions_Thread_switch( executing, heir );
112    _Thread_Save_fp( executing );
113    _Context_Switch( &executing->Registers, &heir->Registers );
114    _Thread_Restore_fp( executing );
115
116    /*
117     * We have to obtain this value again after the context switch since the
118     * heir thread may have migrated from another processor.  Values from the
119     * stack or non-volatile registers reflect the old execution environment.
120     */
121    cpu_self = _Per_CPU_Get();
122
123    _Thread_Debug_set_real_processor( executing, cpu_self );
124
125#if !defined( RTEMS_SMP )
126    _ISR_Disable( level );
127#endif
128  } while (
129#if defined( RTEMS_SMP )
130    false
131#else
132    cpu_self->dispatch_necessary
133#endif
134  );
135
136post_switch:
137  _Assert( cpu_self->thread_dispatch_disable_level == 1 );
138  cpu_self->thread_dispatch_disable_level = 0;
139  _Profiling_Thread_dispatch_enable( cpu_self, 0 );
140
141  _ISR_Enable_without_giant( level );
142
143  _Thread_Run_post_switch_actions( executing );
144}
145
146void _Thread_Dispatch( void )
147{
148  ISR_Level        level;
149  Per_CPU_Control *cpu_self;
150
151  _ISR_Disable_without_giant( level );
152
153  cpu_self = _Per_CPU_Get();
154
155  if ( cpu_self->dispatch_necessary ) {
156    _Profiling_Thread_dispatch_disable( cpu_self, 0 );
157    cpu_self->thread_dispatch_disable_level = 1;
158    _Thread_Do_dispatch( cpu_self, level );
159  } else {
160    _ISR_Enable_without_giant( level );
161  }
162}
Note: See TracBrowser for help on using the repository browser.