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