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