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

5
Last change on this file since c2ee227 was 3384994, checked in by Chris Johns <chrisj@…>, on 11/13/17 at 02:25:18

Clean up sphinx warnings.

  • Fix minor formatting issues.
  • Fix reference the gloassary TLS using ':term:'.
  • Make sure nothing is between an anchor and the heading where ':ref:' references the anchor. This meant moving all the recently added '.. index::' entries.

Update #3232.
Update #3229.

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