1 | /* |
---|
2 | * Rate Monotonic Manager - Period Blocking and Status |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2007. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <rtems/system.h> |
---|
19 | #include <rtems/rtems/status.h> |
---|
20 | #include <rtems/rtems/support.h> |
---|
21 | #include <rtems/score/isr.h> |
---|
22 | #include <rtems/score/object.h> |
---|
23 | #include <rtems/rtems/ratemon.h> |
---|
24 | #include <rtems/score/thread.h> |
---|
25 | |
---|
26 | void _Rate_monotonic_Update_statistics( |
---|
27 | Rate_monotonic_Control *the_period |
---|
28 | ) |
---|
29 | { |
---|
30 | uint32_t ticks_since_last_period; |
---|
31 | uint32_t ticks_executed_since_last_period; |
---|
32 | |
---|
33 | /* |
---|
34 | * Assume we are only called in states where it is appropriate |
---|
35 | * to update the statistics. This should only be RATE_MONOTONIC_ACTIVE |
---|
36 | * and RATE_MONOTONIC_EXPIRED. |
---|
37 | */ |
---|
38 | |
---|
39 | /* |
---|
40 | * Grab basic information |
---|
41 | */ |
---|
42 | |
---|
43 | ticks_since_last_period = |
---|
44 | _Watchdog_Ticks_since_boot - the_period->time_at_period; |
---|
45 | |
---|
46 | ticks_executed_since_last_period = the_period->owner->ticks_executed - |
---|
47 | the_period->owner_ticks_executed_at_period; |
---|
48 | |
---|
49 | /* |
---|
50 | * Now update the statistics |
---|
51 | */ |
---|
52 | |
---|
53 | the_period->Statistics.count++; |
---|
54 | if ( the_period->state == RATE_MONOTONIC_EXPIRED ) |
---|
55 | the_period->Statistics.missed_count++; |
---|
56 | the_period->Statistics.total_cpu_time += ticks_executed_since_last_period; |
---|
57 | the_period->Statistics.total_wall_time += ticks_since_last_period; |
---|
58 | |
---|
59 | /* |
---|
60 | * Update CPU time |
---|
61 | */ |
---|
62 | |
---|
63 | if ( ticks_executed_since_last_period < the_period->Statistics.min_cpu_time ) |
---|
64 | the_period->Statistics.min_cpu_time = ticks_executed_since_last_period; |
---|
65 | |
---|
66 | if ( ticks_executed_since_last_period > the_period->Statistics.max_cpu_time ) |
---|
67 | the_period->Statistics.max_cpu_time = ticks_executed_since_last_period; |
---|
68 | |
---|
69 | /* |
---|
70 | * Update Wall time |
---|
71 | */ |
---|
72 | |
---|
73 | if ( ticks_since_last_period < the_period->Statistics.min_wall_time ) |
---|
74 | the_period->Statistics.min_wall_time = ticks_since_last_period; |
---|
75 | |
---|
76 | if ( ticks_since_last_period > the_period->Statistics.max_wall_time ) |
---|
77 | the_period->Statistics.max_wall_time = ticks_since_last_period; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | /*PAGE |
---|
82 | * |
---|
83 | * rtems_rate_monotonic_period |
---|
84 | * |
---|
85 | * This directive allows a thread to manipulate a rate monotonic timer. |
---|
86 | * |
---|
87 | * Input parameters: |
---|
88 | * id - rate monotonic id |
---|
89 | * length - length of period (in ticks) |
---|
90 | * |
---|
91 | * Output parameters: |
---|
92 | * RTEMS_SUCCESSFUL - if successful |
---|
93 | * error code - if unsuccessful |
---|
94 | */ |
---|
95 | |
---|
96 | rtems_status_code rtems_rate_monotonic_period( |
---|
97 | Objects_Id id, |
---|
98 | rtems_interval length |
---|
99 | ) |
---|
100 | { |
---|
101 | Rate_monotonic_Control *the_period; |
---|
102 | Objects_Locations location; |
---|
103 | rtems_status_code return_value; |
---|
104 | rtems_rate_monotonic_period_states local_state; |
---|
105 | ISR_Level level; |
---|
106 | |
---|
107 | the_period = _Rate_monotonic_Get( id, &location ); |
---|
108 | switch ( location ) { |
---|
109 | case OBJECTS_REMOTE: /* should never return this */ |
---|
110 | return RTEMS_INTERNAL_ERROR; |
---|
111 | |
---|
112 | case OBJECTS_ERROR: |
---|
113 | return RTEMS_INVALID_ID; |
---|
114 | |
---|
115 | case OBJECTS_LOCAL: |
---|
116 | if ( !_Thread_Is_executing( the_period->owner ) ) { |
---|
117 | _Thread_Enable_dispatch(); |
---|
118 | return RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
119 | } |
---|
120 | |
---|
121 | if ( length == RTEMS_PERIOD_STATUS ) { |
---|
122 | switch ( the_period->state ) { |
---|
123 | case RATE_MONOTONIC_INACTIVE: |
---|
124 | return_value = RTEMS_NOT_DEFINED; |
---|
125 | break; |
---|
126 | case RATE_MONOTONIC_ACTIVE: |
---|
127 | return_value = RTEMS_SUCCESSFUL; |
---|
128 | break; |
---|
129 | case RATE_MONOTONIC_EXPIRED: |
---|
130 | return_value = RTEMS_TIMEOUT; |
---|
131 | break; |
---|
132 | default: /* unreached -- only to remove warnings */ |
---|
133 | return_value = RTEMS_INTERNAL_ERROR; |
---|
134 | break; |
---|
135 | } |
---|
136 | _Thread_Enable_dispatch(); |
---|
137 | return( return_value ); |
---|
138 | } |
---|
139 | |
---|
140 | _ISR_Disable( level ); |
---|
141 | switch ( the_period->state ) { |
---|
142 | case RATE_MONOTONIC_INACTIVE: |
---|
143 | /* |
---|
144 | * No need to update statistics -- there are not a period active |
---|
145 | */ |
---|
146 | |
---|
147 | _ISR_Enable( level ); |
---|
148 | |
---|
149 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
150 | _Watchdog_Initialize( |
---|
151 | &the_period->Timer, |
---|
152 | _Rate_monotonic_Timeout, |
---|
153 | id, |
---|
154 | NULL |
---|
155 | ); |
---|
156 | |
---|
157 | the_period->owner_ticks_executed_at_period = |
---|
158 | _Thread_Executing->ticks_executed; |
---|
159 | |
---|
160 | the_period->time_at_period = _Watchdog_Ticks_since_boot; |
---|
161 | the_period->next_length = length; |
---|
162 | |
---|
163 | _Watchdog_Insert_ticks( &the_period->Timer, length ); |
---|
164 | _Thread_Enable_dispatch(); |
---|
165 | return RTEMS_SUCCESSFUL; |
---|
166 | |
---|
167 | case RATE_MONOTONIC_ACTIVE: |
---|
168 | |
---|
169 | /* |
---|
170 | * Update statistics from the concluding period |
---|
171 | */ |
---|
172 | _Rate_monotonic_Update_statistics( the_period ); |
---|
173 | |
---|
174 | /* |
---|
175 | * This tells the _Rate_monotonic_Timeout that this task is |
---|
176 | * in the process of blocking on the period and that we |
---|
177 | * may be changing the length of the next period. |
---|
178 | */ |
---|
179 | |
---|
180 | the_period->state = RATE_MONOTONIC_OWNER_IS_BLOCKING; |
---|
181 | the_period->next_length = length; |
---|
182 | |
---|
183 | _ISR_Enable( level ); |
---|
184 | |
---|
185 | _Thread_Executing->Wait.id = the_period->Object.id; |
---|
186 | _Thread_Set_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD ); |
---|
187 | |
---|
188 | /* |
---|
189 | * Did the watchdog timer expire while we were actually blocking |
---|
190 | * on it? |
---|
191 | */ |
---|
192 | |
---|
193 | _ISR_Disable( level ); |
---|
194 | local_state = the_period->state; |
---|
195 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
196 | _ISR_Enable( level ); |
---|
197 | |
---|
198 | /* |
---|
199 | * If it did, then we want to unblock ourself and continue as |
---|
200 | * if nothing happen. The period was reset in the timeout routine. |
---|
201 | */ |
---|
202 | |
---|
203 | if ( local_state == RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING ) |
---|
204 | _Thread_Clear_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD ); |
---|
205 | |
---|
206 | _Thread_Enable_dispatch(); |
---|
207 | return RTEMS_SUCCESSFUL; |
---|
208 | break; |
---|
209 | |
---|
210 | case RATE_MONOTONIC_EXPIRED: |
---|
211 | /* |
---|
212 | * Update statistics from the concluding period |
---|
213 | */ |
---|
214 | _Rate_monotonic_Update_statistics( the_period ); |
---|
215 | |
---|
216 | _ISR_Enable( level ); |
---|
217 | |
---|
218 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
219 | the_period->owner_ticks_executed_at_period = |
---|
220 | _Thread_Executing->ticks_executed; |
---|
221 | the_period->time_at_period = _Watchdog_Ticks_since_boot; |
---|
222 | the_period->next_length = length; |
---|
223 | |
---|
224 | _Watchdog_Insert_ticks( &the_period->Timer, length ); |
---|
225 | _Thread_Enable_dispatch(); |
---|
226 | return RTEMS_TIMEOUT; |
---|
227 | |
---|
228 | case RATE_MONOTONIC_OWNER_IS_BLOCKING: |
---|
229 | case RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING: |
---|
230 | /* |
---|
231 | * These should never happen. |
---|
232 | */ |
---|
233 | break; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
238 | } |
---|