1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 2014, 2016 embedded brains GmbH & Co. KG |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #ifdef HAVE_CONFIG_H |
---|
29 | #include "config.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <stdio.h> |
---|
33 | #include <inttypes.h> |
---|
34 | |
---|
35 | #include <rtems.h> |
---|
36 | #include <rtems/counter.h> |
---|
37 | |
---|
38 | #include "tmacros.h" |
---|
39 | |
---|
40 | const char rtems_test_name[] = "SPCPUCOUNTER 1"; |
---|
41 | |
---|
42 | #define NS_PER_TICK 1000000 |
---|
43 | |
---|
44 | #define N 10 |
---|
45 | |
---|
46 | typedef struct { |
---|
47 | rtems_counter_ticks delay_ns_t[N][2]; |
---|
48 | rtems_counter_ticks delay_ticks_t[N][2]; |
---|
49 | rtems_counter_ticks overhead_t[N][5]; |
---|
50 | rtems_counter_ticks overhead_delta; |
---|
51 | } test_context; |
---|
52 | |
---|
53 | static test_context test_instance; |
---|
54 | |
---|
55 | static rtems_interval sync_with_clock_tick(void) |
---|
56 | { |
---|
57 | rtems_interval start = rtems_clock_get_ticks_since_boot(); |
---|
58 | rtems_interval current; |
---|
59 | |
---|
60 | do { |
---|
61 | current = rtems_clock_get_ticks_since_boot(); |
---|
62 | } while (current == start); |
---|
63 | |
---|
64 | return current; |
---|
65 | } |
---|
66 | |
---|
67 | static void test_converter(void) |
---|
68 | { |
---|
69 | CPU_Counter_ticks frequency; |
---|
70 | CPU_Counter_ticks frequency2; |
---|
71 | uint64_t ns; |
---|
72 | int64_t sbt; |
---|
73 | |
---|
74 | frequency = rtems_counter_nanoseconds_to_ticks(1000000000); |
---|
75 | ns = rtems_counter_ticks_to_nanoseconds(frequency); |
---|
76 | |
---|
77 | printf("CPU counter frequency: %" PRIu32 "Hz\n", frequency); |
---|
78 | printf("nanoseconds for frequency count ticks: %" PRIu64 "\n", ns); |
---|
79 | |
---|
80 | rtems_test_assert(ns == 1000000000); |
---|
81 | |
---|
82 | sbt = rtems_counter_ticks_to_sbintime(frequency); |
---|
83 | rtems_test_assert(sbt == (INT64_C(1) << 32)); |
---|
84 | |
---|
85 | frequency2 = rtems_counter_sbintime_to_ticks(sbt); |
---|
86 | rtems_test_assert(frequency == frequency2); |
---|
87 | } |
---|
88 | |
---|
89 | static void test_delay_nanoseconds(test_context *ctx) |
---|
90 | { |
---|
91 | int i; |
---|
92 | |
---|
93 | for (i = 0; i < N; ++i) { |
---|
94 | rtems_counter_ticks t0; |
---|
95 | rtems_counter_ticks t1; |
---|
96 | rtems_interval tick; |
---|
97 | |
---|
98 | tick = sync_with_clock_tick(); |
---|
99 | |
---|
100 | t0 = rtems_counter_read(); |
---|
101 | rtems_counter_delay_nanoseconds(NS_PER_TICK); |
---|
102 | t1 = rtems_counter_read(); |
---|
103 | |
---|
104 | ctx->delay_ns_t[i][0] = t0; |
---|
105 | ctx->delay_ns_t[i][1] = t1; |
---|
106 | |
---|
107 | rtems_test_assert(tick < rtems_clock_get_ticks_since_boot()); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | static void test_delay_ticks(test_context *ctx) |
---|
112 | { |
---|
113 | rtems_counter_ticks ticks = rtems_counter_nanoseconds_to_ticks(NS_PER_TICK); |
---|
114 | int i; |
---|
115 | |
---|
116 | for (i = 0; i < N; ++i) { |
---|
117 | rtems_counter_ticks t0; |
---|
118 | rtems_counter_ticks t1; |
---|
119 | rtems_interval tick; |
---|
120 | |
---|
121 | tick = sync_with_clock_tick(); |
---|
122 | |
---|
123 | t0 = rtems_counter_read(); |
---|
124 | rtems_counter_delay_ticks(ticks); |
---|
125 | t1 = rtems_counter_read(); |
---|
126 | |
---|
127 | ctx->delay_ticks_t[i][0] = t0; |
---|
128 | ctx->delay_ticks_t[i][1] = t1; |
---|
129 | |
---|
130 | rtems_test_assert(tick < rtems_clock_get_ticks_since_boot()); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | static void test_overheads(test_context *ctx) |
---|
135 | { |
---|
136 | int i; |
---|
137 | |
---|
138 | for (i = 0; i < N; ++i) { |
---|
139 | rtems_counter_ticks t0; |
---|
140 | rtems_counter_ticks t1; |
---|
141 | rtems_counter_ticks t2; |
---|
142 | rtems_counter_ticks t3; |
---|
143 | rtems_counter_ticks t4; |
---|
144 | rtems_counter_ticks d; |
---|
145 | |
---|
146 | t0 = rtems_counter_read(); |
---|
147 | t1 = rtems_counter_read(); |
---|
148 | d = rtems_counter_difference(t1, t0); |
---|
149 | t2 = rtems_counter_read(); |
---|
150 | rtems_counter_delay_nanoseconds(0); |
---|
151 | t3 = rtems_counter_read(); |
---|
152 | rtems_counter_delay_ticks(0); |
---|
153 | t4 = rtems_counter_read(); |
---|
154 | |
---|
155 | ctx->overhead_t[i][0] = t0; |
---|
156 | ctx->overhead_t[i][1] = t1; |
---|
157 | ctx->overhead_t[i][2] = t2; |
---|
158 | ctx->overhead_t[i][3] = t3; |
---|
159 | ctx->overhead_t[i][4] = t4; |
---|
160 | ctx->overhead_delta = d; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | static void report_overhead( |
---|
165 | const char *name, |
---|
166 | rtems_counter_ticks t1, |
---|
167 | rtems_counter_ticks t0 |
---|
168 | ) |
---|
169 | { |
---|
170 | rtems_counter_ticks d; |
---|
171 | uint64_t ns; |
---|
172 | |
---|
173 | d = rtems_counter_difference(t1, t0); |
---|
174 | ns = rtems_counter_ticks_to_nanoseconds(d); |
---|
175 | |
---|
176 | printf( |
---|
177 | "overhead %s: %" PRIu64 " ticks, %" PRIu64 "ns\n", |
---|
178 | name, |
---|
179 | (uint64_t) d, |
---|
180 | ns |
---|
181 | ); |
---|
182 | } |
---|
183 | |
---|
184 | static uint64_t large_delta_to_ns(rtems_counter_ticks d) |
---|
185 | { |
---|
186 | uint64_t ns; |
---|
187 | |
---|
188 | ns = rtems_counter_ticks_to_nanoseconds(d); |
---|
189 | |
---|
190 | /* Special case for CPU counters using the clock driver counter */ |
---|
191 | if (ns < rtems_configuration_get_nanoseconds_per_tick()) { |
---|
192 | printf( |
---|
193 | "warning: the RTEMS counter seems to be unable to\n" |
---|
194 | " measure intervals greater than the clock tick interval\n" |
---|
195 | ); |
---|
196 | |
---|
197 | ns += rtems_configuration_get_nanoseconds_per_tick(); |
---|
198 | } |
---|
199 | |
---|
200 | return ns; |
---|
201 | } |
---|
202 | |
---|
203 | static void test_report(test_context *ctx) |
---|
204 | { |
---|
205 | double ns_per_tick = NS_PER_TICK; |
---|
206 | rtems_counter_ticks d; |
---|
207 | uint64_t ns; |
---|
208 | size_t i; |
---|
209 | |
---|
210 | printf("test delay nanoseconds (%i times)\n", N); |
---|
211 | |
---|
212 | for (i = 0; i < N; ++i) { |
---|
213 | d = rtems_counter_difference(ctx->delay_ns_t[i][1], ctx->delay_ns_t[i][0]); |
---|
214 | ns = large_delta_to_ns(d); |
---|
215 | |
---|
216 | printf( |
---|
217 | "ns busy wait duration: %" PRIu64 "ns\n" |
---|
218 | "ns busy wait relative to clock tick: %f\n", |
---|
219 | ns, |
---|
220 | (ns - ns_per_tick) / ns_per_tick |
---|
221 | ); |
---|
222 | } |
---|
223 | |
---|
224 | printf("test delay ticks (%i times)\n", N); |
---|
225 | |
---|
226 | for (i = 0; i < N; ++i) { |
---|
227 | d = rtems_counter_difference( |
---|
228 | ctx->delay_ticks_t[i][1], |
---|
229 | ctx->delay_ticks_t[i][0] |
---|
230 | ); |
---|
231 | ns = large_delta_to_ns(d); |
---|
232 | |
---|
233 | printf( |
---|
234 | "ticks busy wait duration: %" PRIu64 "ns\n" |
---|
235 | "ticks busy wait relative to clock tick: %f\n", |
---|
236 | ns, |
---|
237 | (ns - ns_per_tick) / ns_per_tick |
---|
238 | ); |
---|
239 | } |
---|
240 | |
---|
241 | printf("test overheads (%i times)\n", N); |
---|
242 | |
---|
243 | for (i = 0; i < N; ++i) { |
---|
244 | report_overhead("read", ctx->overhead_t[i][1], ctx->overhead_t[i][0]); |
---|
245 | report_overhead("difference", ctx->overhead_t[i][2], ctx->overhead_t[i][1]); |
---|
246 | report_overhead("delay ns", ctx->overhead_t[i][3], ctx->overhead_t[i][2]); |
---|
247 | report_overhead("delay ticks", ctx->overhead_t[i][4], ctx->overhead_t[i][3]); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | static void Init(rtems_task_argument arg) |
---|
252 | { |
---|
253 | test_context *ctx = &test_instance; |
---|
254 | |
---|
255 | rtems_print_printer_fprintf_putc(&rtems_test_printer); |
---|
256 | TEST_BEGIN(); |
---|
257 | |
---|
258 | test_delay_nanoseconds(ctx); |
---|
259 | test_delay_ticks(ctx); |
---|
260 | test_overheads(ctx); |
---|
261 | test_converter(); |
---|
262 | test_report(ctx); |
---|
263 | |
---|
264 | TEST_END(); |
---|
265 | |
---|
266 | rtems_test_exit(0); |
---|
267 | } |
---|
268 | |
---|
269 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
270 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
271 | |
---|
272 | #define CONFIGURE_MICROSECONDS_PER_TICK (NS_PER_TICK / 1000) |
---|
273 | |
---|
274 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
275 | |
---|
276 | #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT |
---|
277 | |
---|
278 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
279 | |
---|
280 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
281 | |
---|
282 | #define CONFIGURE_INIT |
---|
283 | |
---|
284 | #include <rtems/confdefs.h> |
---|