[5f0cd34] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
[23a0105a] | 4 | * Nanoseconds accuracy timestamp test |
---|
[8ff5e59d] | 5 | */ |
---|
| 6 | |
---|
[5f0cd34] | 7 | /* |
---|
| 8 | * COPYRIGHT (c) 1989-2012. |
---|
[23a0105a] | 9 | * On-Line Applications Research Corporation (OAR). |
---|
| 10 | * |
---|
| 11 | * The license and distribution terms for this file may be |
---|
| 12 | * found in the file LICENSE in this distribution or at |
---|
| 13 | * http://www.rtems.com/license/LICENSE. |
---|
| 14 | */ |
---|
| 15 | |
---|
[e313551] | 16 | #ifdef HAVE_CONFIG_H |
---|
| 17 | #include "config.h" |
---|
| 18 | #endif |
---|
| 19 | |
---|
[23a0105a] | 20 | #include <rtems.h> |
---|
| 21 | #include <inttypes.h> |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include <stdlib.h> |
---|
[68e6f0cc] | 25 | #include <unistd.h> |
---|
[88c74ab] | 26 | #include <time.h> |
---|
[23a0105a] | 27 | #include <sys/time.h> |
---|
[99aebcb] | 28 | |
---|
| 29 | #define CONFIGURE_INIT |
---|
| 30 | #include "system.h" |
---|
| 31 | |
---|
[587da8b8] | 32 | #include <rtems/score/timespec.h> /* _Timespec_Substract */ |
---|
[d44c86b] | 33 | |
---|
[cf08e7b] | 34 | #include "tmacros.h" |
---|
[d44c86b] | 35 | #include "pritime.h" |
---|
[04abc23] | 36 | |
---|
[4534ba3] | 37 | static char *my_ctime( time_t t ) |
---|
[23a0105a] | 38 | { |
---|
| 39 | static char b[32]; |
---|
| 40 | ctime_r(&t, b); |
---|
| 41 | b[ strlen(b) - 1] = '\0'; |
---|
[b1274bd9] | 42 | return b; |
---|
[23a0105a] | 43 | } |
---|
| 44 | |
---|
[4534ba3] | 45 | static void subtract_em( |
---|
[23a0105a] | 46 | struct timespec *start, |
---|
| 47 | struct timespec *stop, |
---|
| 48 | struct timespec *t |
---|
| 49 | ) |
---|
| 50 | { |
---|
| 51 | t->tv_sec = 0; |
---|
| 52 | t->tv_nsec = 0; |
---|
[8ff5e59d] | 53 | _Timespec_Subtract( start, stop, t ); |
---|
[23a0105a] | 54 | } |
---|
| 55 | |
---|
[8bfffd9b] | 56 | |
---|
[23a0105a] | 57 | rtems_task Init( |
---|
| 58 | rtems_task_argument argument |
---|
| 59 | ) |
---|
| 60 | { |
---|
| 61 | rtems_status_code status; |
---|
| 62 | rtems_time_of_day time; |
---|
| 63 | int index; |
---|
| 64 | |
---|
| 65 | puts( "\n\n*** NANOSECOND CLOCK TEST ***" ); |
---|
| 66 | |
---|
| 67 | time.year = 2007; |
---|
| 68 | time.month = 03; |
---|
| 69 | time.day = 24; |
---|
| 70 | time.hour = 11; |
---|
| 71 | time.minute = 15; |
---|
| 72 | time.second = 0; |
---|
| 73 | time.ticks = 0; |
---|
| 74 | |
---|
| 75 | status = rtems_clock_set( &time ); |
---|
[7c1e6942] | 76 | directive_failed( status, "clock set" ); |
---|
[23a0105a] | 77 | |
---|
| 78 | /* |
---|
| 79 | * Iterate 10 times showing difference in TOD |
---|
| 80 | */ |
---|
| 81 | printf( "10 iterations of getting TOD\n" ); |
---|
| 82 | for (index=0 ; index <10 ; index++ ) { |
---|
| 83 | struct timespec start, stop; |
---|
| 84 | struct timespec diff; |
---|
[88c74ab] | 85 | |
---|
[23a0105a] | 86 | clock_gettime( CLOCK_REALTIME, &start ); |
---|
| 87 | clock_gettime( CLOCK_REALTIME, &stop ); |
---|
| 88 | |
---|
| 89 | subtract_em( &start, &stop, &diff ); |
---|
[04abc23] | 90 | printf( "Start: %s:%ld\nStop : %s:%ld", |
---|
[23a0105a] | 91 | my_ctime(start.tv_sec), start.tv_nsec, |
---|
| 92 | my_ctime(stop.tv_sec), stop.tv_nsec |
---|
| 93 | ); |
---|
| 94 | |
---|
[d44c86b] | 95 | printf( " --> %" PRIdtime_t ":%ld\n", diff.tv_sec, diff.tv_nsec ); |
---|
[23a0105a] | 96 | } |
---|
| 97 | |
---|
| 98 | /* |
---|
| 99 | * Iterate 10 times showing difference in Uptime |
---|
| 100 | */ |
---|
| 101 | printf( "\n10 iterations of getting Uptime\n" ); |
---|
| 102 | for (index=0 ; index <10 ; index++ ) { |
---|
| 103 | struct timespec start, stop; |
---|
| 104 | struct timespec diff; |
---|
| 105 | rtems_clock_get_uptime( &start ); |
---|
| 106 | rtems_clock_get_uptime( &stop ); |
---|
| 107 | |
---|
| 108 | subtract_em( &start, &stop, &diff ); |
---|
[d44c86b] | 109 | printf( "%" PRIdtime_t ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n", |
---|
[23a0105a] | 110 | start.tv_sec, start.tv_nsec, |
---|
| 111 | stop.tv_sec, stop.tv_nsec, |
---|
| 112 | diff.tv_sec, diff.tv_nsec |
---|
| 113 | ); |
---|
| 114 | } |
---|
| 115 | |
---|
[d3b05790] | 116 | /* |
---|
| 117 | * Iterate 10 times showing difference in Uptime with different counts |
---|
| 118 | */ |
---|
| 119 | printf( "\n10 iterations of getting Uptime with different loop values\n" ); |
---|
| 120 | for (index=1 ; index <=10 ; index++ ) { |
---|
| 121 | struct timespec start, stop; |
---|
| 122 | struct timespec diff; |
---|
| 123 | int j, max = (index * 10000); |
---|
| 124 | rtems_clock_get_uptime( &start ); |
---|
[b1274bd9] | 125 | for (j=0 ; j<max ; j++ ) |
---|
[2c0f748] | 126 | dummy_function_empty_body_to_force_call(); |
---|
[d3b05790] | 127 | rtems_clock_get_uptime( &stop ); |
---|
| 128 | |
---|
| 129 | subtract_em( &start, &stop, &diff ); |
---|
[d44c86b] | 130 | printf( "loop of %d %" PRIdtime_t ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n", |
---|
[d3b05790] | 131 | max, |
---|
| 132 | start.tv_sec, start.tv_nsec, |
---|
| 133 | stop.tv_sec, stop.tv_nsec, |
---|
| 134 | diff.tv_sec, diff.tv_nsec |
---|
| 135 | ); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | sleep(1); |
---|
| 139 | |
---|
[23a0105a] | 140 | puts( "*** END OF NANOSECOND CLOCK TEST ***" ); |
---|
| 141 | exit(0); |
---|
| 142 | } |
---|
| 143 | |
---|