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 sbintime_t 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 | *_time = tstosbt(_ts); |
---|
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 = 0; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * @brief Less than operator for timestamps. |
---|
99 | * |
---|
100 | * This method is the less than operator for timestamps. |
---|
101 | * |
---|
102 | * @param[in] _lhs points to the left hand side timestamp |
---|
103 | * @param[in] _rhs points to the right hand side timestamp |
---|
104 | * |
---|
105 | * @retval This method returns true if @a _lhs is less than the @a _rhs and |
---|
106 | * false otherwise. |
---|
107 | */ |
---|
108 | |
---|
109 | RTEMS_INLINE_ROUTINE bool _Timestamp_Less_than( |
---|
110 | const Timestamp_Control *_lhs, |
---|
111 | const Timestamp_Control *_rhs |
---|
112 | ) |
---|
113 | { |
---|
114 | return *_lhs < *_rhs; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * @brief Greater than operator for timestamps. |
---|
119 | * |
---|
120 | * This method is the greater than operator for timestamps. |
---|
121 | * |
---|
122 | * @param[in] _lhs points to the left hand side timestamp |
---|
123 | * @param[in] _rhs points to the right hand side timestamp |
---|
124 | * |
---|
125 | * @retval This method returns true if @a _lhs is greater than the @a _rhs and |
---|
126 | * false otherwise. |
---|
127 | */ |
---|
128 | |
---|
129 | RTEMS_INLINE_ROUTINE bool _Timestamp_Greater_than( |
---|
130 | const Timestamp_Control *_lhs, |
---|
131 | const Timestamp_Control *_rhs |
---|
132 | ) |
---|
133 | { |
---|
134 | return *_lhs > *_rhs; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * @brief Equal to than operator for timestamps. |
---|
139 | * |
---|
140 | * This method is the is equal to than operator for timestamps. |
---|
141 | * |
---|
142 | * @param[in] _lhs points to the left hand side timestamp |
---|
143 | * @param[in] _rhs points to the right hand side timestamp |
---|
144 | * |
---|
145 | * @retval This method returns true if @a _lhs is equal to @a _rhs and |
---|
146 | * false otherwise. |
---|
147 | */ |
---|
148 | |
---|
149 | RTEMS_INLINE_ROUTINE bool _Timestamp_Equal_to( |
---|
150 | const Timestamp_Control *_lhs, |
---|
151 | const Timestamp_Control *_rhs |
---|
152 | ) |
---|
153 | { |
---|
154 | return *_lhs == *_rhs; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * @brief Adds two timestamps. |
---|
159 | * |
---|
160 | * This routine adds two timestamps. The second argument is added |
---|
161 | * to the first. |
---|
162 | * |
---|
163 | * @param[in] _time points to the base time to be added to |
---|
164 | * @param[in] _add points to the timestamp to add to the first argument |
---|
165 | */ |
---|
166 | RTEMS_INLINE_ROUTINE void _Timestamp_Add_to( |
---|
167 | Timestamp_Control *_time, |
---|
168 | const Timestamp_Control *_add |
---|
169 | ) |
---|
170 | { |
---|
171 | *_time += *_add; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * @brief Subtracts two timestamps. |
---|
176 | * |
---|
177 | * This routine subtracts two timestamps. @a result is set to |
---|
178 | * @a end - @a start. |
---|
179 | * |
---|
180 | * @param[in] _start points to the starting time |
---|
181 | * @param[in] _end points to the ending time |
---|
182 | * @param[in] _result points to the difference between |
---|
183 | * starting and ending time. |
---|
184 | * |
---|
185 | * @retval This method fills in @a _result. |
---|
186 | */ |
---|
187 | RTEMS_INLINE_ROUTINE void _Timestamp_Subtract( |
---|
188 | const Timestamp_Control *_start, |
---|
189 | const Timestamp_Control *_end, |
---|
190 | Timestamp_Control *_result |
---|
191 | ) |
---|
192 | { |
---|
193 | *_result = *_end - *_start; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * @brief Divides a timestamp by another timestamp. |
---|
198 | * |
---|
199 | * This routine divides a timestamp by another timestamp. The |
---|
200 | * intended use is for calculating percentages to three decimal points. |
---|
201 | * |
---|
202 | * @param[in] _lhs points to the left hand number |
---|
203 | * @param[in] _rhs points to the right hand number |
---|
204 | * @param[in] _ival_percentage points to the integer portion of the average |
---|
205 | * @param[in] _fval_percentage points to the thousandths of percentage |
---|
206 | * |
---|
207 | * @retval This method fills in @a result. |
---|
208 | */ |
---|
209 | RTEMS_INLINE_ROUTINE void _Timestamp_Divide( |
---|
210 | const Timestamp_Control *_lhs, |
---|
211 | const Timestamp_Control *_rhs, |
---|
212 | uint32_t *_ival_percentage, |
---|
213 | uint32_t *_fval_percentage |
---|
214 | ) |
---|
215 | { |
---|
216 | struct timespec _ts_left; |
---|
217 | struct timespec _ts_right; |
---|
218 | |
---|
219 | _ts_left = sbttots( *_lhs ); |
---|
220 | _ts_right = sbttots( *_rhs ); |
---|
221 | |
---|
222 | _Timespec_Divide( |
---|
223 | &_ts_left, |
---|
224 | &_ts_right, |
---|
225 | _ival_percentage, |
---|
226 | _fval_percentage |
---|
227 | ); |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * @brief Get seconds portion of timestamp. |
---|
232 | * |
---|
233 | * This method returns the seconds portion of the specified timestamp |
---|
234 | * |
---|
235 | * @param[in] _time points to the timestamp |
---|
236 | * |
---|
237 | * @retval The seconds portion of @a _time. |
---|
238 | */ |
---|
239 | RTEMS_INLINE_ROUTINE time_t _Timestamp_Get_seconds( |
---|
240 | const Timestamp_Control *_time |
---|
241 | ) |
---|
242 | { |
---|
243 | return (*_time >> 32); |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * @brief Get nanoseconds portion of timestamp. |
---|
248 | * |
---|
249 | * This method returns the nanoseconds portion of the specified timestamp |
---|
250 | * |
---|
251 | * @param[in] _time points to the timestamp |
---|
252 | * |
---|
253 | * @retval The nanoseconds portion of @a _time. |
---|
254 | */ |
---|
255 | RTEMS_INLINE_ROUTINE uint32_t _Timestamp_Get_nanoseconds( |
---|
256 | const Timestamp_Control *_time |
---|
257 | ) |
---|
258 | { |
---|
259 | struct timespec _ts; |
---|
260 | |
---|
261 | _ts = sbttots( *_time ); |
---|
262 | |
---|
263 | return (uint32_t) _ts.tv_nsec; |
---|
264 | } |
---|
265 | |
---|
266 | /** |
---|
267 | * @brief Get the timestamp as nanoseconds. |
---|
268 | * |
---|
269 | * This method returns the timestamp as nanoseconds. |
---|
270 | * |
---|
271 | * @param[in] _time points to the timestamp |
---|
272 | * |
---|
273 | * @retval The time in nanoseconds. |
---|
274 | */ |
---|
275 | RTEMS_INLINE_ROUTINE uint64_t _Timestamp_Get_as_nanoseconds( |
---|
276 | const Timestamp_Control *_time |
---|
277 | ) |
---|
278 | { |
---|
279 | struct timespec _ts; |
---|
280 | |
---|
281 | _ts = sbttots( *_time ); |
---|
282 | |
---|
283 | return _Timespec_Get_as_nanoseconds( &_ts ); |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * @brief Convert timestamp to struct timespec. |
---|
288 | * |
---|
289 | * This method returns the seconds portion of the specified @a _timestamp. |
---|
290 | * |
---|
291 | * @param[in] _timestamp points to the timestamp |
---|
292 | * @param[in] _timespec points to the timespec |
---|
293 | */ |
---|
294 | RTEMS_INLINE_ROUTINE void _Timestamp_To_timespec( |
---|
295 | const Timestamp_Control *_timestamp, |
---|
296 | struct timespec *_timespec |
---|
297 | ) |
---|
298 | { |
---|
299 | *_timespec = sbttots( *_timestamp ); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * @brief Convert timestamp to struct timeval. |
---|
304 | * |
---|
305 | * @param[in] _timestamp points to the timestamp |
---|
306 | * @param[in] _timeval points to the timeval |
---|
307 | */ |
---|
308 | RTEMS_INLINE_ROUTINE void _Timestamp_To_timeval( |
---|
309 | const Timestamp_Control *_timestamp, |
---|
310 | struct timeval *_timeval |
---|
311 | ) |
---|
312 | { |
---|
313 | *_timeval = sbttotv( *_timestamp ); |
---|
314 | } |
---|
315 | |
---|
316 | #ifdef __cplusplus |
---|
317 | } |
---|
318 | #endif |
---|
319 | |
---|
320 | /**@}*/ |
---|
321 | |
---|
322 | #endif |
---|
323 | /* end of include file */ |
---|