1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * Nanoseconds accuracy timestamp test |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2012. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * Redistribution and use in source and binary forms, with or without |
---|
14 | * modification, are permitted provided that the following conditions |
---|
15 | * are met: |
---|
16 | * 1. Redistributions of source code must retain the above copyright |
---|
17 | * notice, this list of conditions and the following disclaimer. |
---|
18 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer in the |
---|
20 | * documentation and/or other materials provided with the distribution. |
---|
21 | * |
---|
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
32 | * POSSIBILITY OF SUCH DAMAGE. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifdef HAVE_CONFIG_H |
---|
36 | #include "config.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #include <rtems.h> |
---|
40 | #include <inttypes.h> |
---|
41 | #include <stdio.h> |
---|
42 | #include <string.h> |
---|
43 | #include <stdlib.h> |
---|
44 | #include <unistd.h> |
---|
45 | #include <time.h> |
---|
46 | #include <sys/time.h> |
---|
47 | |
---|
48 | #define CONFIGURE_INIT |
---|
49 | #include "system.h" |
---|
50 | |
---|
51 | #include <rtems/score/timespec.h> /* _Timespec_Substract */ |
---|
52 | |
---|
53 | #include "tmacros.h" |
---|
54 | #include "pritime.h" |
---|
55 | |
---|
56 | const char rtems_test_name[] = "NANOSECOND CLOCK"; |
---|
57 | |
---|
58 | static char *my_ctime( time_t t ) |
---|
59 | { |
---|
60 | static char b[32]; |
---|
61 | ctime_r(&t, b); |
---|
62 | b[ strlen(b) - 1] = '\0'; |
---|
63 | return b; |
---|
64 | } |
---|
65 | |
---|
66 | static void subtract_em( |
---|
67 | struct timespec *start, |
---|
68 | struct timespec *stop, |
---|
69 | struct timespec *t |
---|
70 | ) |
---|
71 | { |
---|
72 | t->tv_sec = 0; |
---|
73 | t->tv_nsec = 0; |
---|
74 | _Timespec_Subtract( start, stop, t ); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | rtems_task Init( |
---|
79 | rtems_task_argument argument |
---|
80 | ) |
---|
81 | { |
---|
82 | rtems_status_code status; |
---|
83 | rtems_time_of_day time; |
---|
84 | int index; |
---|
85 | |
---|
86 | TEST_BEGIN(); |
---|
87 | |
---|
88 | time.year = 2007; |
---|
89 | time.month = 03; |
---|
90 | time.day = 24; |
---|
91 | time.hour = 11; |
---|
92 | time.minute = 15; |
---|
93 | time.second = 0; |
---|
94 | time.ticks = 0; |
---|
95 | |
---|
96 | status = rtems_clock_set( &time ); |
---|
97 | directive_failed( status, "clock set" ); |
---|
98 | |
---|
99 | /* |
---|
100 | * Iterate 10 times showing difference in TOD |
---|
101 | */ |
---|
102 | printf( "10 iterations of getting TOD\n" ); |
---|
103 | for (index=0 ; index <10 ; index++ ) { |
---|
104 | struct timespec start, stop; |
---|
105 | struct timespec diff; |
---|
106 | |
---|
107 | clock_gettime( CLOCK_REALTIME, &start ); |
---|
108 | clock_gettime( CLOCK_REALTIME, &stop ); |
---|
109 | |
---|
110 | subtract_em( &start, &stop, &diff ); |
---|
111 | printf( "Start: %s:%ld\nStop : %s:%ld", |
---|
112 | my_ctime(start.tv_sec), start.tv_nsec, |
---|
113 | my_ctime(stop.tv_sec), stop.tv_nsec |
---|
114 | ); |
---|
115 | |
---|
116 | printf( " --> %" PRIdtime_t ":%ld\n", diff.tv_sec, diff.tv_nsec ); |
---|
117 | } |
---|
118 | |
---|
119 | /* |
---|
120 | * Iterate 10 times showing difference in Uptime |
---|
121 | */ |
---|
122 | printf( "\n10 iterations of getting Uptime\n" ); |
---|
123 | for (index=0 ; index <10 ; index++ ) { |
---|
124 | struct timespec start, stop; |
---|
125 | struct timespec diff; |
---|
126 | rtems_clock_get_uptime( &start ); |
---|
127 | rtems_clock_get_uptime( &stop ); |
---|
128 | |
---|
129 | subtract_em( &start, &stop, &diff ); |
---|
130 | printf( "%" PRIdtime_t ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n", |
---|
131 | start.tv_sec, start.tv_nsec, |
---|
132 | stop.tv_sec, stop.tv_nsec, |
---|
133 | diff.tv_sec, diff.tv_nsec |
---|
134 | ); |
---|
135 | } |
---|
136 | |
---|
137 | /* |
---|
138 | * Iterate 10 times showing difference in Uptime with different counts |
---|
139 | */ |
---|
140 | printf( "\n10 iterations of getting Uptime with different loop values\n" ); |
---|
141 | for (index=1 ; index <=10 ; index++ ) { |
---|
142 | struct timespec start, stop; |
---|
143 | struct timespec diff; |
---|
144 | long j, max = (index * 10000L); |
---|
145 | rtems_clock_get_uptime( &start ); |
---|
146 | for (j=0 ; j<max ; j++ ) |
---|
147 | dummy_function_empty_body_to_force_call(); |
---|
148 | rtems_clock_get_uptime( &stop ); |
---|
149 | |
---|
150 | subtract_em( &start, &stop, &diff ); |
---|
151 | printf( "loop of %ld %" PRIdtime_t |
---|
152 | ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n", |
---|
153 | max, |
---|
154 | start.tv_sec, start.tv_nsec, |
---|
155 | stop.tv_sec, stop.tv_nsec, |
---|
156 | diff.tv_sec, diff.tv_nsec |
---|
157 | ); |
---|
158 | } |
---|
159 | |
---|
160 | sleep(1); |
---|
161 | |
---|
162 | TEST_END(); |
---|
163 | exit(0); |
---|
164 | } |
---|
165 | |
---|