source: rtems-docs/c_user/timespec_helpers.rst @ 489740f

4.115
Last change on this file since 489740f was 489740f, checked in by Chris Johns <chrisj@…>, on 05/20/16 at 02:47:09

Set SPDX License Identifier in each source file.

  • Property mode set to 100644
File size: 13.0 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
7Timespec Helpers
8################
9
10Introduction
11============
12
13The Timespec helpers manager provides directives to assist in manipulating
14instances of the POSIX ``struct timespec`` structure.
15
16The 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
46Background
47==========
48
49Time Storage Conventions
50------------------------
51
52Time can be stored in many ways. One of them is the ``struct timespec`` format
53which is a structure that consists of the fields ``tv_sec`` to represent
54seconds and ``tv_nsec`` to represent nanoseconds.  The``struct timeval``
55structure is simular and consists of seconds (stored in ``tv_sec``) and
56microseconds (stored in ``tv_usec``). Either``struct timespec`` or ``struct
57timeval`` can be used to represent elapsed time, time of executing some
58operations, or time of day.
59
60Operations
61==========
62
63Set and Obtain Timespec Value
64-----------------------------
65
66A user may write a specific time by passing the desired seconds and nanoseconds
67values and the destination ``struct timespec`` using the ``rtems_timespec_set``
68directive.
69
70The ``rtems_timespec_zero`` directive is used to zero the seconds
71and nanoseconds portions of a ``struct timespec`` instance.
72
73Users may obtain the seconds or nanoseconds portions of a ``struct timespec``
74instance with the ``rtems_timespec_get_seconds`` or
75``rtems_timespec_get_nanoseconds`` methods, respectively.
76
77Timespec Math
78-------------
79
80A user can perform multiple operations on ``struct timespec`` instances. The
81helpers in this manager assist in adding, subtracting, and performing divison
82on ``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
99Comparing struct timespec Instances
100-----------------------------------
101
102A 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
106Conversions and Validity Check
107------------------------------
108
109Conversion to and from clock ticks may be performed by using the
110``rtems_timespec_to_ticks`` and ``rtems_timespec_from_ticks`` directives.
111
112User can also check validity of timespec with ``rtems_timespec_is_valid``
113routine.
114
115Directives
116==========
117
118This section details the Timespec Helpers manager's directives.  A subsection
119is dedicated to each of this manager's directives and describes the calling
120sequence, related constants, usage, and status codes.
121
122.. _rtems_timespec_set:
123
124TIMESPEC_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
142NONE
143
144**DESCRIPTION:**
145
146This directive sets the ``struct timespec`` *time* to the desired ``seconds``
147and ``nanoseconds`` values.
148
149**NOTES:**
150
151This method does NOT check if ``nanoseconds`` is less than the maximum number
152of nanoseconds in a second.
153
154.. _rtems_timespec_zero:
155
156TIMESPEC_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
171NONE
172
173**DESCRIPTION:**
174
175This routine sets the contents of the ``struct timespec`` instance ``time`` to
176zero.
177
178**NOTES:**
179
180NONE
181
182.. _rtems_timespec_is_valid:
183
184TIMESPEC_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
199This method returns ``true`` if the instance is valid, and ``false`` otherwise.
200
201**DESCRIPTION:**
202
203This routine check validity of a ``struct timespec`` instance. It checks if the
204nanoseconds 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
211TIMESPEC_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
227The method returns the number of seconds ``time`` increased by.
228
229**DESCRIPTION:**
230
231This routine adds two ``struct timespec`` instances. The second argument is
232added to the first. The parameter ``time`` is the base time to which the
233``add`` parameter is added.
234
235**NOTES:**
236
237NONE
238
239.. _rtems_timespec_subtract:
240
241TIMESPEC_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
258NONE
259
260**DESCRIPTION:**
261
262This 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
267It is possible to subtract when ``end`` is less than ``start`` and it produce
268negative ``result``. When doing this you should be careful and remember that
269only the seconds portion of a ``struct timespec`` instance is signed, which
270means that nanoseconds portion is always increasing. Due to that when your
271timespec 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
276TIMESPEC_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
294NONE
295
296**DESCRIPTION:**
297
298This routine divides the ``struct timespec`` instance ``lhs`` by the ``struct
299timespec`` instance ``rhs``. The result is returned in the ``ival_percentage``
300and ``fval_percentage``, representing the integer and fractional results of the
301division respectively.
302
303The ``ival_percentage`` is integer value of calculated percentage and
304``fval_percentage`` is fractional part of calculated percentage.
305
306**NOTES:**
307
308The intended use is calculating percentges to three decimal points.
309
310When dividing by zero, this routine return both ``ival_percentage`` and
311``fval_percentage`` equal zero.
312
313The division is performed using exclusively integer operations.
314
315.. _rtems_timespec_divide_by_integer:
316
317TIMESPEC_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
334NONE
335
336**DESCRIPTION:**
337
338This routine divides the ``struct timespec`` instance ``time`` by the integer
339value ``iterations``.  The result is saved in ``result``.
340
341**NOTES:**
342
343The expected use is to assist in benchmark calculations where you typically
344divide a duration (``time``) by a number of iterations what gives average time.
345
346.. _rtems_timespec_less_than:
347
348TIMESPEC_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
364This method returns ``struct true`` if ``lhs`` is less than ``rhs`` and
365``struct false`` otherwise.
366
367**DESCRIPTION:**
368
369This method is the less than operator for ``struct timespec`` instances. The
370first parameter is the left hand side and the second is the right hand side of
371the comparison.
372
373**NOTES:**
374
375NONE
376
377.. _rtems_timespec_greater_than:
378
379TIMESPEC_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
395This method returns ``struct true`` if ``lhs`` is greater than ``rhs`` and
396``struct false`` otherwise.
397
398**DESCRIPTION:**
399
400This method is greater than operator for ``struct timespec`` instances.
401
402**NOTES:**
403
404NONE
405
406.. _rtems_timespec_equal_to:
407
408TIMESPEC_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
424This method returns ``struct true`` if ``lhs`` is equal to ``rhs`` and ``struct
425false`` otherwise.
426
427**DESCRIPTION:**
428
429This method is equality operator for ``struct timespec`` instances.
430
431**NOTES:**
432
433NONE
434
435.. _rtems_timespec_get_seconds:
436
437TIMESPEC_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
452This method returns the seconds portion of the specified ``struct timespec``
453instance.
454
455**DESCRIPTION:**
456
457This method returns the seconds portion of the specified ``struct timespec``
458instance ``time``.
459
460**NOTES:**
461
462NONE
463
464.. _rtems_timespec_get_nanoseconds:
465
466TIMESPEC_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
481This method returns the nanoseconds portion of the specified ``struct
482timespec`` instance.
483
484**DESCRIPTION:**
485
486This method returns the nanoseconds portion of the specified timespec which is
487pointed by ``_time``.
488
489**NOTES:**
490
491.. _rtems_timespec_to_ticks:
492
493TIMESPEC_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
508This directive returns the number of ticks computed.
509
510**DESCRIPTION:**
511
512This directive converts the ``time`` timespec to the corresponding number of
513clock ticks.
514
515**NOTES:**
516
517NONE
518
519.. _rtems_timespec_from_ticks:
520
521TIMESPEC_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
539NONE
540
541**DESCRIPTION:**
542
543This routine converts the ``ticks`` to the corresponding ``struct timespec``
544representation and stores it in ``time``.
545
546**NOTES:**
547
548NONE
Note: See TracBrowser for help on using the repository browser.