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