1 | /** |
---|
2 | * @file rtems/score/timestamp.h |
---|
3 | * |
---|
4 | * @brief Helpers for Manipulating Timestamps |
---|
5 | * |
---|
6 | * This include file contains helpers for manipulating timestamps. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2009. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_SCORE_TIMESTAMP_H |
---|
19 | #define _RTEMS_SCORE_TIMESTAMP_H |
---|
20 | |
---|
21 | /** |
---|
22 | * @defgroup SuperCoreTimeStamp Score Timestamp |
---|
23 | * |
---|
24 | * @ingroup Score |
---|
25 | * |
---|
26 | * This handler encapsulates functionality related to manipulating |
---|
27 | * SuperCore Timestamps. SuperCore Timestamps may be used to |
---|
28 | * represent time of day, uptime, or intervals. |
---|
29 | * |
---|
30 | * The key attribute of the SuperCore Timestamp handler is that it |
---|
31 | * is a completely opaque handler. There can be multiple implementations |
---|
32 | * of the required functionality and with a recompile, RTEMS can use |
---|
33 | * any implementation. It is intended to be a simple wrapper. |
---|
34 | * |
---|
35 | * This handler can be implemented as either struct timespec or |
---|
36 | * unsigned64 bit numbers. The use of a wrapper class allows the |
---|
37 | * the implementation of timestamps to change on a per architecture |
---|
38 | * basis. This is an important option as the performance of this |
---|
39 | * handler is critical. |
---|
40 | */ |
---|
41 | /**@{*/ |
---|
42 | |
---|
43 | #include <sys/time.h> |
---|
44 | |
---|
45 | #include <rtems/score/basedefs.h> |
---|
46 | #include <rtems/score/timespec.h> |
---|
47 | |
---|
48 | #ifdef __cplusplus |
---|
49 | extern "C" { |
---|
50 | #endif |
---|
51 | |
---|
52 | /** |
---|
53 | * Define the Timestamp control type. |
---|
54 | */ |
---|
55 | typedef struct bintime Timestamp_Control; |
---|
56 | |
---|
57 | /** |
---|
58 | * @brief Set timestamp to specified seconds and nanoseconds. |
---|
59 | * |
---|
60 | * This method sets the timestamp to the specified @a _seconds and @a _nanoseconds |
---|
61 | * value. |
---|
62 | * |
---|
63 | * @param[in] _time points to the timestamp instance to validate. |
---|
64 | * @param[in] _seconds is the seconds portion of the timestamp |
---|
65 | * @param[in] _nanoseconds is the nanoseconds portion of the timestamp |
---|
66 | */ |
---|
67 | RTEMS_INLINE_ROUTINE void _Timestamp_Set( |
---|
68 | Timestamp_Control *_time, |
---|
69 | time_t _seconds, |
---|
70 | long _nanoseconds |
---|
71 | ) |
---|
72 | { |
---|
73 | struct timespec _ts; |
---|
74 | |
---|
75 | _ts.tv_sec = _seconds; |
---|
76 | _ts.tv_nsec = _nanoseconds; |
---|
77 | |
---|
78 | timespec2bintime( &_ts, _time ); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * @brief Sets the timestamp to zero. |
---|
83 | * |
---|
84 | * This method sets the timestamp to zero. |
---|
85 | * value. |
---|
86 | * |
---|
87 | * @param[in] _time points to the timestamp instance to zero. |
---|
88 | */ |
---|
89 | |
---|
90 | RTEMS_INLINE_ROUTINE void _Timestamp_Set_to_zero( |
---|
91 | Timestamp_Control *_time |
---|
92 | ) |
---|
93 | { |
---|
94 | _time->sec = 0; |
---|
95 | _time->frac = 0; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * @brief Less than operator for timestamps. |
---|
100 | * |
---|
101 | * This method is the less than operator for timestamps. |
---|
102 | * |
---|
103 | * @param[in] _lhs points to the left hand side timestamp |
---|
104 | * @param[in] _rhs points to the right hand side timestamp |
---|
105 | * |
---|
106 | * @retval This method returns true if @a _lhs is less than the @a _rhs and |
---|
107 | * false otherwise. |
---|
108 | */ |
---|
109 | |
---|
110 | RTEMS_INLINE_ROUTINE bool _Timestamp_Less_than( |
---|
111 | const Timestamp_Control *_lhs, |
---|
112 | const Timestamp_Control *_rhs |
---|
113 | ) |
---|
114 | { |
---|
115 | if ( _lhs->sec < _rhs->sec ) |
---|
116 | return true; |
---|
117 | |
---|
118 | if ( _lhs->sec > _rhs->sec ) |
---|
119 | return false; |
---|
120 | |
---|
121 | return _lhs->frac < _rhs->frac; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * @brief Greater than operator for timestamps. |
---|
126 | * |
---|
127 | * This method is the greater than operator for timestamps. |
---|
128 | * |
---|
129 | * @param[in] _lhs points to the left hand side timestamp |
---|
130 | * @param[in] _rhs points to the right hand side timestamp |
---|
131 | * |
---|
132 | * @retval This method returns true if @a _lhs is greater than the @a _rhs and |
---|
133 | * false otherwise. |
---|
134 | */ |
---|
135 | |
---|
136 | RTEMS_INLINE_ROUTINE bool _Timestamp_Greater_than( |
---|
137 | const Timestamp_Control *_lhs, |
---|
138 | const Timestamp_Control *_rhs |
---|
139 | ) |
---|
140 | { |
---|
141 | if ( _lhs->sec > _rhs->sec ) |
---|
142 | return true; |
---|
143 | |
---|
144 | if ( _lhs->sec < _rhs->sec ) |
---|
145 | return false; |
---|
146 | |
---|
147 | return _lhs->frac > _rhs->frac; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * @brief Equal to than operator for timestamps. |
---|
152 | * |
---|
153 | * This method is the is equal to than operator for timestamps. |
---|
154 | * |
---|
155 | * @param[in] _lhs points to the left hand side timestamp |
---|
156 | * @param[in] _rhs points to the right hand side timestamp |
---|
157 | * |
---|
158 | * @retval This method returns true if @a _lhs is equal to @a _rhs and |
---|
159 | * false otherwise. |
---|
160 | */ |
---|
161 | |
---|
162 | RTEMS_INLINE_ROUTINE bool _Timestamp_Equal_to( |
---|
163 | const Timestamp_Control *_lhs, |
---|
164 | const Timestamp_Control *_rhs |
---|
165 | ) |
---|
166 | { |
---|
167 | return _lhs->sec == _rhs->sec && _lhs->frac == _rhs->frac; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * @brief Adds two timestamps. |
---|
172 | * |
---|
173 | * This routine adds two timestamps. The second argument is added |
---|
174 | * to the first. |
---|
175 | * |
---|
176 | * @param[in] _time points to the base time to be added to |
---|
177 | * @param[in] _add points to the timestamp to add to the first argument |
---|
178 | * |
---|
179 | * @retval This method returns the number of seconds @a time increased by. |
---|
180 | */ |
---|
181 | RTEMS_INLINE_ROUTINE time_t _Timestamp_Add_to( |
---|
182 | Timestamp_Control *_time, |
---|
183 | const Timestamp_Control *_add |
---|
184 | ) |
---|
185 | { |
---|
186 | time_t seconds = _time->sec; |
---|
187 | |
---|
188 | bintime_add( _time, _add ); |
---|
189 | |
---|
190 | return _time->sec - seconds; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * @brief Subtracts two timestamps. |
---|
195 | * |
---|
196 | * This routine subtracts two timestamps. @a result is set to |
---|
197 | * @a end - @a start. |
---|
198 | * |
---|
199 | * @param[in] _start points to the starting time |
---|
200 | * @param[in] _end points to the ending time |
---|
201 | * @param[in] _result points to the difference between |
---|
202 | * starting and ending time. |
---|
203 | * |
---|
204 | * @retval This method fills in @a _result. |
---|
205 | */ |
---|
206 | RTEMS_INLINE_ROUTINE void _Timestamp_Subtract( |
---|
207 | const Timestamp_Control *_start, |
---|
208 | const Timestamp_Control *_end, |
---|
209 | Timestamp_Control *_result |
---|
210 | ) |
---|
211 | { |
---|
212 | _result->sec = _end->sec; |
---|
213 | _result->frac = _end->frac; |
---|
214 | |
---|
215 | bintime_sub( _result, _start ); |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * @brief Divides a timestamp by another timestamp. |
---|
220 | * |
---|
221 | * This routine divides a timestamp by another timestamp. The |
---|
222 | * intended use is for calculating percentages to three decimal points. |
---|
223 | * |
---|
224 | * @param[in] _lhs points to the left hand number |
---|
225 | * @param[in] _rhs points to the right hand number |
---|
226 | * @param[in] _ival_percentage points to the integer portion of the average |
---|
227 | * @param[in] _fval_percentage points to the thousandths of percentage |
---|
228 | * |
---|
229 | * @retval This method fills in @a result. |
---|
230 | */ |
---|
231 | RTEMS_INLINE_ROUTINE void _Timestamp_Divide( |
---|
232 | const Timestamp_Control *_lhs, |
---|
233 | const Timestamp_Control *_rhs, |
---|
234 | uint32_t *_ival_percentage, |
---|
235 | uint32_t *_fval_percentage |
---|
236 | ) |
---|
237 | { |
---|
238 | struct timespec _ts_left; |
---|
239 | struct timespec _ts_right; |
---|
240 | |
---|
241 | bintime2timespec( _lhs, &_ts_left ); |
---|
242 | bintime2timespec( _rhs, &_ts_right ); |
---|
243 | |
---|
244 | _Timespec_Divide( |
---|
245 | &_ts_left, |
---|
246 | &_ts_right, |
---|
247 | _ival_percentage, |
---|
248 | _fval_percentage |
---|
249 | ); |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * @brief Get seconds portion of timestamp. |
---|
254 | * |
---|
255 | * This method returns the seconds portion of the specified timestamp |
---|
256 | * |
---|
257 | * @param[in] _time points to the timestamp |
---|
258 | * |
---|
259 | * @retval The seconds portion of @a _time. |
---|
260 | */ |
---|
261 | RTEMS_INLINE_ROUTINE time_t _Timestamp_Get_seconds( |
---|
262 | const Timestamp_Control *_time |
---|
263 | ) |
---|
264 | { |
---|
265 | return _time->sec; |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * @brief Get nanoseconds portion of timestamp. |
---|
270 | * |
---|
271 | * This method returns the nanoseconds portion of the specified timestamp |
---|
272 | * |
---|
273 | * @param[in] _time points to the timestamp |
---|
274 | * |
---|
275 | * @retval The nanoseconds portion of @a _time. |
---|
276 | */ |
---|
277 | RTEMS_INLINE_ROUTINE uint32_t _Timestamp_Get_nanoseconds( |
---|
278 | const Timestamp_Control *_time |
---|
279 | ) |
---|
280 | { |
---|
281 | struct timespec _ts; |
---|
282 | |
---|
283 | bintime2timespec( _time, &_ts ); |
---|
284 | |
---|
285 | return _ts.tv_nsec; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * @brief Get the timestamp as nanoseconds. |
---|
290 | * |
---|
291 | * This method returns the timestamp as nanoseconds. |
---|
292 | * |
---|
293 | * @param[in] _time points to the timestamp |
---|
294 | * |
---|
295 | * @retval The time in nanoseconds. |
---|
296 | */ |
---|
297 | RTEMS_INLINE_ROUTINE uint64_t _Timestamp_Get_as_nanoseconds( |
---|
298 | const Timestamp_Control *_time |
---|
299 | ) |
---|
300 | { |
---|
301 | struct timespec _ts; |
---|
302 | |
---|
303 | bintime2timespec( _time, &_ts ); |
---|
304 | |
---|
305 | return _Timespec_Get_as_nanoseconds( &_ts ); |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * @brief Convert timestamp to struct timespec. |
---|
310 | * |
---|
311 | * This method returns the seconds portion of the specified @a _timestamp. |
---|
312 | * |
---|
313 | * @param[in] _timestamp points to the timestamp |
---|
314 | * @param[in] _timespec points to the timespec |
---|
315 | */ |
---|
316 | RTEMS_INLINE_ROUTINE void _Timestamp_To_timespec( |
---|
317 | const Timestamp_Control *_timestamp, |
---|
318 | struct timespec *_timespec |
---|
319 | ) |
---|
320 | { |
---|
321 | bintime2timespec( _timestamp, _timespec ); |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * @brief Convert timestamp to struct timeval. |
---|
326 | * |
---|
327 | * @param[in] _timestamp points to the timestamp |
---|
328 | * @param[in] _timeval points to the timeval |
---|
329 | */ |
---|
330 | RTEMS_INLINE_ROUTINE void _Timestamp_To_timeval( |
---|
331 | const Timestamp_Control *_timestamp, |
---|
332 | struct timeval *_timeval |
---|
333 | ) |
---|
334 | { |
---|
335 | bintime2timeval( _timestamp, _timeval ); |
---|
336 | } |
---|
337 | |
---|
338 | #ifdef __cplusplus |
---|
339 | } |
---|
340 | #endif |
---|
341 | |
---|
342 | /**@}*/ |
---|
343 | |
---|
344 | #endif |
---|
345 | /* end of include file */ |
---|