source: rtems-docs/c-user/timespec_helpers.rst

Last change on this file was aaba6e5, checked in by Joel Sherrill <joel@…>, on 11/30/22 at 15:59:20

c-user/*: Add trailing parentheses on methods in index which were missing it

Closes #4766.

  • Property mode set to 100644
File size: 13.9 KB
Line 
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
8Timespec Helpers
9****************
10
11Introduction
12============
13
14The Timespec helpers manager provides directives to assist in manipulating
15instances of the POSIX ``struct timespec`` structure.
16
17The 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
47Background
48==========
49
50Time Storage Conventions
51------------------------
52
53Time can be stored in many ways. One of them is the ``struct timespec`` format
54which is a structure that consists of the fields ``tv_sec`` to represent
55seconds and ``tv_nsec`` to represent nanoseconds.  The``struct timeval``
56structure is simular and consists of seconds (stored in ``tv_sec``) and
57microseconds (stored in ``tv_usec``). Either``struct timespec`` or ``struct
58timeval`` can be used to represent elapsed time, time of executing some
59operations, or time of day.
60
61Operations
62==========
63
64Set and Obtain Timespec Value
65-----------------------------
66
67A user may write a specific time by passing the desired seconds and nanoseconds
68values and the destination ``struct timespec`` using the ``rtems_timespec_set``
69directive.
70
71The ``rtems_timespec_zero`` directive is used to zero the seconds
72and nanoseconds portions of a ``struct timespec`` instance.
73
74Users may obtain the seconds or nanoseconds portions of a ``struct timespec``
75instance with the ``rtems_timespec_get_seconds`` or
76``rtems_timespec_get_nanoseconds`` methods, respectively.
77
78Timespec Math
79-------------
80
81A user can perform multiple operations on ``struct timespec`` instances. The
82helpers in this manager assist in adding, subtracting, and performing divison
83on ``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
100Comparing struct timespec Instances
101-----------------------------------
102
103A 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
107Conversions and Validity Check
108------------------------------
109
110Conversion to and from clock ticks may be performed by using the
111``rtems_timespec_to_ticks`` and ``rtems_timespec_from_ticks`` directives.
112
113User can also check validity of timespec with ``rtems_timespec_is_valid``
114routine.
115
116Directives
117==========
118
119This section details the Timespec Helpers manager's directives.  A subsection
120is dedicated to each of this manager's directives and describes the calling
121sequence, 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
132TIMESPEC_SET - Set struct timespec Instance
133-------------------------------------------
134
135CALLING 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
144DIRECTIVE STATUS CODES:
145    NONE
146
147DESCRIPTION:
148    This directive sets the ``struct timespec`` *time* to the desired
149    ``seconds`` and ``nanoseconds`` values.
150
151NOTES:
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
163TIMESPEC_ZERO - Zero struct timespec Instance
164---------------------------------------------
165
166CALLING SEQUENCE:
167    .. code-block:: c
168
169        void rtems_timespec_zero(
170            struct timespec *time
171        );
172
173DIRECTIVE STATUS CODES:
174    NONE
175
176DESCRIPTION:
177    This routine sets the contents of the ``struct timespec`` instance ``time`` to
178    zero.
179
180NOTES:
181    NONE
182
183.. raw:: latex
184
185   \clearpage
186
187.. index:: rtems_timespec_is_valid()
188
189.. _rtems_timespec_is_valid:
190
191TIMESPEC_IS_VALID - Check validity of a struct timespec instance
192----------------------------------------------------------------
193
194CALLING SEQUENCE:
195    .. code-block:: c
196
197        bool rtems_timespec_is_valid(
198            const struct timespec *time
199        );
200
201DIRECTIVE STATUS CODES:
202    This method returns ``true`` if the instance is valid, and ``false``
203    otherwise.
204
205DESCRIPTION:
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
210NOTES:
211    NONE
212
213.. raw:: latex
214
215   \clearpage
216
217.. index:: rtems_timespec_add_to()
218
219.. _rtems_timespec_add_to:
220
221TIMESPEC_ADD_TO - Add Two struct timespec Instances
222---------------------------------------------------
223
224CALLING SEQUENCE:
225    .. code-block:: c
226
227        uint32_t rtems_timespec_add_to(
228            struct timespec       *time,
229            const struct timespec *add
230        );
231
232DIRECTIVE STATUS CODES:
233    The method returns the number of seconds ``time`` increased by.
234
235DESCRIPTION:
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
240NOTES:
241    NONE
242
243.. raw:: latex
244
245   \clearpage
246
247.. index:: rtems_timespec_subtract()
248
249.. _rtems_timespec_subtract:
250
251TIMESPEC_SUBTRACT - Subtract Two struct timespec Instances
252----------------------------------------------------------
253
254CALLING 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
263DIRECTIVE STATUS CODES:
264    NONE
265
266DESCRIPTION:
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
270NOTES:
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
286TIMESPEC_DIVIDE - Divide Two struct timespec Instances
287------------------------------------------------------
288
289CALLING 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
299DIRECTIVE STATUS CODES:
300    NONE
301
302DESCRIPTION:
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
311NOTES:
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
327TIMESPEC_DIVIDE_BY_INTEGER - Divide a struct timespec Instance by an Integer
328----------------------------------------------------------------------------
329
330CALLING 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
339DIRECTIVE STATUS CODES:
340    NONE
341
342DESCRIPTION:
343    This routine divides the ``struct timespec`` instance ``time`` by the
344    integer value ``iterations``.  The result is saved in ``result``.
345
346NOTES:
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
359TIMESPEC_LESS_THAN - Less than operator
360---------------------------------------
361
362CALLING SEQUENCE:
363    .. code-block:: c
364
365        bool rtems_timespec_less_than(
366            const struct timespec *lhs,
367            const struct timespec *rhs
368        );
369
370DIRECTIVE STATUS CODES:
371    This method returns ``struct true`` if ``lhs`` is less than ``rhs`` and
372    ``struct false`` otherwise.
373
374DESCRIPTION:
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
379NOTES:
380    NONE
381
382.. raw:: latex
383
384   \clearpage
385
386.. index:: rtems_timespec_greater_than()
387
388.. _rtems_timespec_greater_than:
389
390TIMESPEC_GREATER_THAN - Greater than operator
391---------------------------------------------
392
393CALLING SEQUENCE:
394    .. code-block:: c
395
396        bool rtems_timespec_greater_than(
397            const struct timespec *_lhs,
398            const struct timespec *_rhs
399        );
400
401DIRECTIVE STATUS CODES:
402    This method returns ``struct true`` if ``lhs`` is greater than ``rhs`` and
403    ``struct false`` otherwise.
404
405DESCRIPTION:
406    This method is greater than operator for ``struct timespec`` instances.
407
408NOTES:
409    NONE
410
411.. raw:: latex
412
413   \clearpage
414
415.. index:: rtems_timespec_equal_to()
416
417.. _rtems_timespec_equal_to:
418
419TIMESPEC_EQUAL_TO - Check equality of timespecs
420-----------------------------------------------
421
422CALLING SEQUENCE:
423    .. code-block:: c
424
425        bool rtems_timespec_equal_to(
426            const struct timespec *lhs,
427            const struct timespec *rhs
428        );
429
430DIRECTIVE STATUS CODES:
431    This method returns ``struct true`` if ``lhs`` is equal to ``rhs`` and
432    ``struct false`` otherwise.
433
434DESCRIPTION:
435    This method is equality operator for ``struct timespec`` instances.
436
437NOTES:
438    NONE
439
440.. raw:: latex
441
442   \clearpage
443
444.. index:: rtems_timespec_get_seconds()
445
446.. _rtems_timespec_get_seconds:
447
448TIMESPEC_GET_SECONDS - Get Seconds Portion of struct timespec Instance
449----------------------------------------------------------------------
450
451CALLING SEQUENCE:
452    .. code-block:: c
453
454        time_t rtems_timespec_get_seconds(
455            struct timespec *time
456        );
457
458DIRECTIVE STATUS CODES:
459    This method returns the seconds portion of the specified ``struct
460    timespec`` instance.
461
462DESCRIPTION:
463    This method returns the seconds portion of the specified ``struct
464    timespec`` instance ``time``.
465
466NOTES:
467    NONE
468
469.. raw:: latex
470
471   \clearpage
472
473.. index:: rtems_timespec_get_nanoseconds()
474
475.. _rtems_timespec_get_nanoseconds:
476
477TIMESPEC_GET_NANOSECONDS - Get Nanoseconds Portion of the struct timespec Instance
478----------------------------------------------------------------------------------
479
480CALLING SEQUENCE:
481    .. code-block:: c
482
483        uint32_t rtems_timespec_get_nanoseconds(
484            struct timespec *_time
485        );
486
487DIRECTIVE STATUS CODES:
488    This method returns the nanoseconds portion of the specified ``struct
489    timespec`` instance.
490
491DESCRIPTION:
492    This method returns the nanoseconds portion of the specified timespec which
493    is pointed by ``_time``.
494
495NOTES:
496    NONE
497
498.. raw:: latex
499
500   \clearpage
501
502.. index:: rtems_timespec_to_ticks()
503
504.. _rtems_timespec_to_ticks:
505
506TIMESPEC_TO_TICKS - Convert struct timespec Instance to Ticks
507-------------------------------------------------------------
508
509CALLING SEQUENCE:
510    .. code-block:: c
511
512        uint32_t rtems_timespec_to_ticks(
513            const struct timespec *time
514        );
515
516DIRECTIVE STATUS CODES:
517    This directive returns the number of ticks computed.
518
519DESCRIPTION:
520    This directive converts the ``time`` timespec to the corresponding number
521    of clock ticks.
522
523NOTES:
524    NONE
525
526.. raw:: latex
527
528   \clearpage
529
530.. index:: rtems_timespec_from_ticks()
531
532.. _rtems_timespec_from_ticks:
533
534TIMESPEC_FROM_TICKS - Convert Ticks to struct timespec Representation
535---------------------------------------------------------------------
536
537CALLING SEQUENCE:
538    .. code-block:: c
539
540        void rtems_timespec_from_ticks(
541            uint32_t         ticks,
542            struct timespec *time
543        );
544
545DIRECTIVE STATUS CODES:
546    NONE
547
548DESCRIPTION:
549    This routine converts the ``ticks`` to the corresponding ``struct
550    timespec`` representation and stores it in ``time``.
551
552NOTES:
553    NONE
Note: See TracBrowser for help on using the repository browser.