1 | .. comment SPDX-License-Identifier: CC-BY-SA-4.0 |
---|
2 | |
---|
3 | .. COMMENT: COPYRIGHT (c) 2011. |
---|
4 | .. COMMENT: On-Line Applications Research Corporation (OAR). |
---|
5 | .. COMMENT: All rights reserved. |
---|
6 | |
---|
7 | Timespec Helpers |
---|
8 | ################ |
---|
9 | |
---|
10 | Introduction |
---|
11 | ============ |
---|
12 | |
---|
13 | The Timespec helpers manager provides directives to assist in manipulating |
---|
14 | instances of the POSIX ``struct timespec`` structure. |
---|
15 | |
---|
16 | The directives provided by the timespec helpers manager are: |
---|
17 | |
---|
18 | - rtems_timespec_set_ - Set timespec's value |
---|
19 | |
---|
20 | - rtems_timespec_zero_ - Zero timespec's value |
---|
21 | |
---|
22 | - rtems_timespec_is_valid_ - Check if timespec is valid |
---|
23 | |
---|
24 | - rtems_timespec_add_to_ - Add two timespecs |
---|
25 | |
---|
26 | - rtems_timespec_subtract_ - Subtract two timespecs |
---|
27 | |
---|
28 | - rtems_timespec_divide_ - Divide two timespecs |
---|
29 | |
---|
30 | - rtems_timespec_divide_by_integer_ - Divide timespec by integer |
---|
31 | |
---|
32 | - rtems_timespec_less_than_ - Less than operator |
---|
33 | |
---|
34 | - rtems_timespec_greater_than_ - Greater than operator |
---|
35 | |
---|
36 | - rtems_timespec_equal_to_ - Check if two timespecs are equal |
---|
37 | |
---|
38 | - rtems_timespec_get_seconds_ - Obtain seconds portion of timespec |
---|
39 | |
---|
40 | - rtems_timespec_get_nanoseconds_ - Obtain nanoseconds portion of timespec |
---|
41 | |
---|
42 | - rtems_timespec_to_ticks_ - Convert timespec to number of ticks |
---|
43 | |
---|
44 | - rtems_timespec_from_ticks_ - Convert ticks to timespec |
---|
45 | |
---|
46 | Background |
---|
47 | ========== |
---|
48 | |
---|
49 | Time Storage Conventions |
---|
50 | ------------------------ |
---|
51 | |
---|
52 | Time can be stored in many ways. One of them is the ``struct timespec`` format |
---|
53 | which is a structure that consists of the fields ``tv_sec`` to represent |
---|
54 | seconds and ``tv_nsec`` to represent nanoseconds. The``struct timeval`` |
---|
55 | structure is simular and consists of seconds (stored in ``tv_sec``) and |
---|
56 | microseconds (stored in ``tv_usec``). Either``struct timespec`` or ``struct |
---|
57 | timeval`` can be used to represent elapsed time, time of executing some |
---|
58 | operations, or time of day. |
---|
59 | |
---|
60 | Operations |
---|
61 | ========== |
---|
62 | |
---|
63 | Set and Obtain Timespec Value |
---|
64 | ----------------------------- |
---|
65 | |
---|
66 | A user may write a specific time by passing the desired seconds and nanoseconds |
---|
67 | values and the destination ``struct timespec`` using the ``rtems_timespec_set`` |
---|
68 | directive. |
---|
69 | |
---|
70 | The ``rtems_timespec_zero`` directive is used to zero the seconds |
---|
71 | and nanoseconds portions of a ``struct timespec`` instance. |
---|
72 | |
---|
73 | Users may obtain the seconds or nanoseconds portions of a ``struct timespec`` |
---|
74 | instance with the ``rtems_timespec_get_seconds`` or |
---|
75 | ``rtems_timespec_get_nanoseconds`` methods, respectively. |
---|
76 | |
---|
77 | Timespec Math |
---|
78 | ------------- |
---|
79 | |
---|
80 | A user can perform multiple operations on ``struct timespec`` instances. The |
---|
81 | helpers in this manager assist in adding, subtracting, and performing divison |
---|
82 | on ``struct timespec`` instances. |
---|
83 | |
---|
84 | - Adding two ``struct timespec`` can be done using the |
---|
85 | ``rtems_timespec_add_to`` directive. This directive is used mainly to |
---|
86 | calculate total amount of time consumed by multiple operations. |
---|
87 | |
---|
88 | - The ``rtems_timespec_subtract`` is used to subtract two ``struct timespecs`` |
---|
89 | instances and determine the elapsed time between those two points in time. |
---|
90 | |
---|
91 | - The ``rtems_timespec_divide`` is used to use to divide one ``struct |
---|
92 | timespec`` instance by another. This calculates the percentage with a |
---|
93 | precision to three decimal points. |
---|
94 | |
---|
95 | - The ``rtems_timespec_divide_by_integer`` is used to divide a ``struct |
---|
96 | timespec`` instance by an integer. It is commonly used in benchmark |
---|
97 | calculations to dividing duration by the number of iterations performed. |
---|
98 | |
---|
99 | Comparing struct timespec Instances |
---|
100 | ----------------------------------- |
---|
101 | |
---|
102 | A user can compare two ``struct timespec`` instances using the |
---|
103 | ``rtems_timespec_less_than``, ``rtems_timespec_greater_than`` or |
---|
104 | ``rtems_timespec_equal_to`` routines. |
---|
105 | |
---|
106 | Conversions and Validity Check |
---|
107 | ------------------------------ |
---|
108 | |
---|
109 | Conversion to and from clock ticks may be performed by using the |
---|
110 | ``rtems_timespec_to_ticks`` and ``rtems_timespec_from_ticks`` directives. |
---|
111 | |
---|
112 | User can also check validity of timespec with ``rtems_timespec_is_valid`` |
---|
113 | routine. |
---|
114 | |
---|
115 | Directives |
---|
116 | ========== |
---|
117 | |
---|
118 | This section details the Timespec Helpers manager's directives. A subsection |
---|
119 | is dedicated to each of this manager's directives and describes the calling |
---|
120 | sequence, related constants, usage, and status codes. |
---|
121 | |
---|
122 | .. _rtems_timespec_set: |
---|
123 | |
---|
124 | TIMESPEC_SET - Set struct timespec Instance |
---|
125 | ------------------------------------------- |
---|
126 | .. index:: set struct timespec instance |
---|
127 | |
---|
128 | **CALLING SEQUENCE:** |
---|
129 | |
---|
130 | .. index:: rtems_timespec_set |
---|
131 | |
---|
132 | .. code-block:: c |
---|
133 | |
---|
134 | void rtems_timespec_set( |
---|
135 | struct timespec *time, |
---|
136 | time_t seconds, |
---|
137 | uint32_t nanoseconds |
---|
138 | ); |
---|
139 | |
---|
140 | **STATUS CODES:** |
---|
141 | |
---|
142 | NONE |
---|
143 | |
---|
144 | **DESCRIPTION:** |
---|
145 | |
---|
146 | This directive sets the ``struct timespec`` *time* to the desired ``seconds`` |
---|
147 | and ``nanoseconds`` values. |
---|
148 | |
---|
149 | **NOTES:** |
---|
150 | |
---|
151 | This method does NOT check if ``nanoseconds`` is less than the maximum number |
---|
152 | of nanoseconds in a second. |
---|
153 | |
---|
154 | .. _rtems_timespec_zero: |
---|
155 | |
---|
156 | TIMESPEC_ZERO - Zero struct timespec Instance |
---|
157 | --------------------------------------------- |
---|
158 | |
---|
159 | **CALLING SEQUENCE:** |
---|
160 | |
---|
161 | .. index:: rtems_timespec_zero |
---|
162 | |
---|
163 | .. code-block:: c |
---|
164 | |
---|
165 | void rtems_timespec_zero( |
---|
166 | struct timespec *time |
---|
167 | ); |
---|
168 | |
---|
169 | **STATUS CODES:** |
---|
170 | |
---|
171 | NONE |
---|
172 | |
---|
173 | **DESCRIPTION:** |
---|
174 | |
---|
175 | This routine sets the contents of the ``struct timespec`` instance ``time`` to |
---|
176 | zero. |
---|
177 | |
---|
178 | **NOTES:** |
---|
179 | |
---|
180 | NONE |
---|
181 | |
---|
182 | .. _rtems_timespec_is_valid: |
---|
183 | |
---|
184 | TIMESPEC_IS_VALID - Check validity of a struct timespec instance |
---|
185 | ---------------------------------------------------------------- |
---|
186 | |
---|
187 | **CALLING SEQUENCE:** |
---|
188 | |
---|
189 | .. index:: rtems_timespec_is_valid |
---|
190 | |
---|
191 | .. code-block:: c |
---|
192 | |
---|
193 | bool rtems_timespec_is_valid( |
---|
194 | const struct timespec *time |
---|
195 | ); |
---|
196 | |
---|
197 | **STATUS CODES:** |
---|
198 | |
---|
199 | This method returns ``true`` if the instance is valid, and ``false`` otherwise. |
---|
200 | |
---|
201 | **DESCRIPTION:** |
---|
202 | |
---|
203 | This routine check validity of a ``struct timespec`` instance. It checks if the |
---|
204 | nanoseconds portion of the ``struct timespec`` instanceis in allowed range |
---|
205 | (less than the maximum number of nanoseconds per second). |
---|
206 | |
---|
207 | **NOTES:** |
---|
208 | |
---|
209 | .. _rtems_timespec_add_to: |
---|
210 | |
---|
211 | TIMESPEC_ADD_TO - Add Two struct timespec Instances |
---|
212 | --------------------------------------------------- |
---|
213 | |
---|
214 | **CALLING SEQUENCE:** |
---|
215 | |
---|
216 | .. index:: rtems_timespec_add_to |
---|
217 | |
---|
218 | .. code-block:: c |
---|
219 | |
---|
220 | uint32_t rtems_timespec_add_to( |
---|
221 | struct timespec *time, |
---|
222 | const struct timespec *add |
---|
223 | ); |
---|
224 | |
---|
225 | **STATUS CODES:** |
---|
226 | |
---|
227 | The method returns the number of seconds ``time`` increased by. |
---|
228 | |
---|
229 | **DESCRIPTION:** |
---|
230 | |
---|
231 | This routine adds two ``struct timespec`` instances. The second argument is |
---|
232 | added to the first. The parameter ``time`` is the base time to which the |
---|
233 | ``add`` parameter is added. |
---|
234 | |
---|
235 | **NOTES:** |
---|
236 | |
---|
237 | NONE |
---|
238 | |
---|
239 | .. _rtems_timespec_subtract: |
---|
240 | |
---|
241 | TIMESPEC_SUBTRACT - Subtract Two struct timespec Instances |
---|
242 | ---------------------------------------------------------- |
---|
243 | |
---|
244 | **CALLING SEQUENCE:** |
---|
245 | |
---|
246 | .. index:: rtems_timespec_subtract |
---|
247 | |
---|
248 | .. code-block:: c |
---|
249 | |
---|
250 | void rtems_timespec_subtract( |
---|
251 | const struct timespec *start, |
---|
252 | const struct timespec *end, |
---|
253 | struct timespec *result |
---|
254 | ); |
---|
255 | |
---|
256 | **STATUS CODES:** |
---|
257 | |
---|
258 | NONE |
---|
259 | |
---|
260 | **DESCRIPTION:** |
---|
261 | |
---|
262 | This routine subtracts ``start`` from ``end`` saves the difference in |
---|
263 | ``result``. The primary use of this directive is to calculate elapsed time. |
---|
264 | |
---|
265 | **NOTES:** |
---|
266 | |
---|
267 | It is possible to subtract when ``end`` is less than ``start`` and it produce |
---|
268 | negative ``result``. When doing this you should be careful and remember that |
---|
269 | only the seconds portion of a ``struct timespec`` instance is signed, which |
---|
270 | means that nanoseconds portion is always increasing. Due to that when your |
---|
271 | timespec has seconds = -1 and nanoseconds = 500,000,000 it means that result is |
---|
272 | -0.5 second, NOT the expected -1.5! |
---|
273 | |
---|
274 | .. _rtems_timespec_divide: |
---|
275 | |
---|
276 | TIMESPEC_DIVIDE - Divide Two struct timespec Instances |
---|
277 | ------------------------------------------------------ |
---|
278 | |
---|
279 | **CALLING SEQUENCE:** |
---|
280 | |
---|
281 | .. index:: rtems_timespec_divide |
---|
282 | |
---|
283 | .. code-block:: c |
---|
284 | |
---|
285 | void rtems_timespec_divide( |
---|
286 | const struct timespec *lhs, |
---|
287 | const struct timespec *rhs, |
---|
288 | uint32_t *ival_percentage, |
---|
289 | uint32_t *fval_percentage |
---|
290 | ); |
---|
291 | |
---|
292 | **STATUS CODES:** |
---|
293 | |
---|
294 | NONE |
---|
295 | |
---|
296 | **DESCRIPTION:** |
---|
297 | |
---|
298 | This routine divides the ``struct timespec`` instance ``lhs`` by the ``struct |
---|
299 | timespec`` instance ``rhs``. The result is returned in the ``ival_percentage`` |
---|
300 | and ``fval_percentage``, representing the integer and fractional results of the |
---|
301 | division respectively. |
---|
302 | |
---|
303 | The ``ival_percentage`` is integer value of calculated percentage and |
---|
304 | ``fval_percentage`` is fractional part of calculated percentage. |
---|
305 | |
---|
306 | **NOTES:** |
---|
307 | |
---|
308 | The intended use is calculating percentges to three decimal points. |
---|
309 | |
---|
310 | When dividing by zero, this routine return both ``ival_percentage`` and |
---|
311 | ``fval_percentage`` equal zero. |
---|
312 | |
---|
313 | The division is performed using exclusively integer operations. |
---|
314 | |
---|
315 | .. _rtems_timespec_divide_by_integer: |
---|
316 | |
---|
317 | TIMESPEC_DIVIDE_BY_INTEGER - Divide a struct timespec Instance by an Integer |
---|
318 | ---------------------------------------------------------------------------- |
---|
319 | |
---|
320 | **CALLING SEQUENCE:** |
---|
321 | |
---|
322 | .. index:: rtems_timespec_divide_by_integer |
---|
323 | |
---|
324 | .. code-block:: c |
---|
325 | |
---|
326 | int rtems_timespec_divide_by_integer( |
---|
327 | const struct timespec *time, |
---|
328 | uint32_t iterations, |
---|
329 | struct timespec *result |
---|
330 | ); |
---|
331 | |
---|
332 | **STATUS CODES:** |
---|
333 | |
---|
334 | NONE |
---|
335 | |
---|
336 | **DESCRIPTION:** |
---|
337 | |
---|
338 | This routine divides the ``struct timespec`` instance ``time`` by the integer |
---|
339 | value ``iterations``. The result is saved in ``result``. |
---|
340 | |
---|
341 | **NOTES:** |
---|
342 | |
---|
343 | The expected use is to assist in benchmark calculations where you typically |
---|
344 | divide a duration (``time``) by a number of iterations what gives average time. |
---|
345 | |
---|
346 | .. _rtems_timespec_less_than: |
---|
347 | |
---|
348 | TIMESPEC_LESS_THAN - Less than operator |
---|
349 | --------------------------------------- |
---|
350 | |
---|
351 | **CALLING SEQUENCE:** |
---|
352 | |
---|
353 | .. index:: rtems_timespec_less_than |
---|
354 | |
---|
355 | .. code-block:: c |
---|
356 | |
---|
357 | bool rtems_timespec_less_than( |
---|
358 | const struct timespec *lhs, |
---|
359 | const struct timespec *rhs |
---|
360 | ); |
---|
361 | |
---|
362 | **STATUS CODES:** |
---|
363 | |
---|
364 | This method returns ``struct true`` if ``lhs`` is less than ``rhs`` and |
---|
365 | ``struct false`` otherwise. |
---|
366 | |
---|
367 | **DESCRIPTION:** |
---|
368 | |
---|
369 | This method is the less than operator for ``struct timespec`` instances. The |
---|
370 | first parameter is the left hand side and the second is the right hand side of |
---|
371 | the comparison. |
---|
372 | |
---|
373 | **NOTES:** |
---|
374 | |
---|
375 | NONE |
---|
376 | |
---|
377 | .. _rtems_timespec_greater_than: |
---|
378 | |
---|
379 | TIMESPEC_GREATER_THAN - Greater than operator |
---|
380 | --------------------------------------------- |
---|
381 | |
---|
382 | **CALLING SEQUENCE:** |
---|
383 | |
---|
384 | .. index:: rtems_timespec_greater_than |
---|
385 | |
---|
386 | .. code-block:: c |
---|
387 | |
---|
388 | bool rtems_timespec_greater_than( |
---|
389 | const struct timespec *_lhs, |
---|
390 | const struct timespec *_rhs |
---|
391 | ); |
---|
392 | |
---|
393 | **STATUS CODES:** |
---|
394 | |
---|
395 | This method returns ``struct true`` if ``lhs`` is greater than ``rhs`` and |
---|
396 | ``struct false`` otherwise. |
---|
397 | |
---|
398 | **DESCRIPTION:** |
---|
399 | |
---|
400 | This method is greater than operator for ``struct timespec`` instances. |
---|
401 | |
---|
402 | **NOTES:** |
---|
403 | |
---|
404 | NONE |
---|
405 | |
---|
406 | .. _rtems_timespec_equal_to: |
---|
407 | |
---|
408 | TIMESPEC_EQUAL_TO - Check equality of timespecs |
---|
409 | ----------------------------------------------- |
---|
410 | |
---|
411 | **CALLING SEQUENCE:** |
---|
412 | |
---|
413 | .. index:: rtems_timespec_equal_to |
---|
414 | |
---|
415 | .. code-block:: c |
---|
416 | |
---|
417 | bool rtems_timespec_equal_to( |
---|
418 | const struct timespec *lhs, |
---|
419 | const struct timespec *rhs |
---|
420 | ); |
---|
421 | |
---|
422 | **STATUS CODES:** |
---|
423 | |
---|
424 | This method returns ``struct true`` if ``lhs`` is equal to ``rhs`` and ``struct |
---|
425 | false`` otherwise. |
---|
426 | |
---|
427 | **DESCRIPTION:** |
---|
428 | |
---|
429 | This method is equality operator for ``struct timespec`` instances. |
---|
430 | |
---|
431 | **NOTES:** |
---|
432 | |
---|
433 | NONE |
---|
434 | |
---|
435 | .. _rtems_timespec_get_seconds: |
---|
436 | |
---|
437 | TIMESPEC_GET_SECONDS - Get Seconds Portion of struct timespec Instance |
---|
438 | ---------------------------------------------------------------------- |
---|
439 | |
---|
440 | **CALLING SEQUENCE:** |
---|
441 | |
---|
442 | .. index:: rtems_timespec_get_seconds |
---|
443 | |
---|
444 | .. code-block:: c |
---|
445 | |
---|
446 | time_t rtems_timespec_get_seconds( |
---|
447 | struct timespec *time |
---|
448 | ); |
---|
449 | |
---|
450 | **STATUS CODES:** |
---|
451 | |
---|
452 | This method returns the seconds portion of the specified ``struct timespec`` |
---|
453 | instance. |
---|
454 | |
---|
455 | **DESCRIPTION:** |
---|
456 | |
---|
457 | This method returns the seconds portion of the specified ``struct timespec`` |
---|
458 | instance ``time``. |
---|
459 | |
---|
460 | **NOTES:** |
---|
461 | |
---|
462 | NONE |
---|
463 | |
---|
464 | .. _rtems_timespec_get_nanoseconds: |
---|
465 | |
---|
466 | TIMESPEC_GET_NANOSECONDS - Get Nanoseconds Portion of the struct timespec Instance |
---|
467 | ---------------------------------------------------------------------------------- |
---|
468 | |
---|
469 | **CALLING SEQUENCE:** |
---|
470 | |
---|
471 | .. index:: rtems_timespec_get_nanoseconds |
---|
472 | |
---|
473 | .. code-block:: c |
---|
474 | |
---|
475 | uint32_t rtems_timespec_get_nanoseconds( |
---|
476 | struct timespec *_time |
---|
477 | ); |
---|
478 | |
---|
479 | **STATUS CODES:** |
---|
480 | |
---|
481 | This method returns the nanoseconds portion of the specified ``struct |
---|
482 | timespec`` instance. |
---|
483 | |
---|
484 | **DESCRIPTION:** |
---|
485 | |
---|
486 | This method returns the nanoseconds portion of the specified timespec which is |
---|
487 | pointed by ``_time``. |
---|
488 | |
---|
489 | **NOTES:** |
---|
490 | |
---|
491 | .. _rtems_timespec_to_ticks: |
---|
492 | |
---|
493 | TIMESPEC_TO_TICKS - Convert struct timespec Instance to Ticks |
---|
494 | ------------------------------------------------------------- |
---|
495 | |
---|
496 | **CALLING SEQUENCE:** |
---|
497 | |
---|
498 | .. index:: rtems_timespec_to_ticks |
---|
499 | |
---|
500 | .. code-block:: c |
---|
501 | |
---|
502 | uint32_t rtems_timespec_to_ticks( |
---|
503 | const struct timespec *time |
---|
504 | ); |
---|
505 | |
---|
506 | **STATUS CODES:** |
---|
507 | |
---|
508 | This directive returns the number of ticks computed. |
---|
509 | |
---|
510 | **DESCRIPTION:** |
---|
511 | |
---|
512 | This directive converts the ``time`` timespec to the corresponding number of |
---|
513 | clock ticks. |
---|
514 | |
---|
515 | **NOTES:** |
---|
516 | |
---|
517 | NONE |
---|
518 | |
---|
519 | .. _rtems_timespec_from_ticks: |
---|
520 | |
---|
521 | TIMESPEC_FROM_TICKS - Convert Ticks to struct timespec Representation |
---|
522 | --------------------------------------------------------------------- |
---|
523 | |
---|
524 | **CALLING SEQUENCE:** |
---|
525 | |
---|
526 | .. index:: rtems_timespec_from_ticks |
---|
527 | |
---|
528 | .. code-block:: c |
---|
529 | |
---|
530 | void rtems_timespec_from_ticks( |
---|
531 | uint32_t ticks, |
---|
532 | struct timespec *time |
---|
533 | ); |
---|
534 | |
---|
535 | .. index:: rtems_timespec_from_ticks |
---|
536 | |
---|
537 | **STATUS CODES:** |
---|
538 | |
---|
539 | NONE |
---|
540 | |
---|
541 | **DESCRIPTION:** |
---|
542 | |
---|
543 | This routine converts the ``ticks`` to the corresponding ``struct timespec`` |
---|
544 | representation and stores it in ``time``. |
---|
545 | |
---|
546 | **NOTES:** |
---|
547 | |
---|
548 | NONE |
---|