source: rtems/cpukit/libtest/t-test-time.c @ cfcc2cbf

5
Last change on this file since cfcc2cbf was cfcc2cbf, checked in by Sebastian Huber <sebastian.huber@…>, on 01/31/19 at 13:45:31

Add RTEMS Test Framework

Update #3199.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2018 embedded brains GmbH
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#undef __STRICT_ANSI__
29
30#include <t.h>
31
32#include <inttypes.h>
33#include <stdatomic.h>
34#include <stdio.h>
35#include <time.h>
36
37#ifdef __rtems__
38#include <rtems/counter.h>
39#include <rtems/score/timecounter.h>
40#endif
41
42#ifdef __rtems__
43static T_time
44round_sbt(T_time time)
45{
46        /*
47         * One 1ns consists of 4.30 fractions of 1/2**32.  Round up close to
48         * the middle.  This turns the conversion mapping of struct timespec to
49         * sbintime_t and back into the identity function.
50         */
51        return time + 2;
52}
53#endif
54
55const char *
56T_time_to_string_ns(T_time time, T_time_string string)
57{
58        uint32_t s;
59        uint32_t f;
60
61#ifdef __rtems__
62        time = round_sbt(time);
63        s = (uint32_t)(time >> 32);
64        f = (uint32_t)(((uint64_t)1000000000 * (uint32_t)time) >> 32);
65#else
66        s = (uint32_t)(time / 1000000000);
67        f = (uint32_t)(time % 1000000000);
68#endif
69
70        (void)T_snprintf(string, sizeof(T_time_string),
71            "%" PRIu32 ".%09" PRIu32, s, f);
72        return string;
73}
74
75const char *
76T_time_to_string_us(T_time time, T_time_string string)
77{
78        uint32_t s;
79        uint32_t f;
80
81#ifdef __rtems__
82        time = round_sbt(time);
83        s = (uint32_t)(time >> 32);
84        f = (uint32_t)(((uint64_t)1000000 * (uint32_t)time) >> 32);
85#else
86        time /= 1000;
87        s = (uint32_t)(time / 1000000);
88        f = (uint32_t)(time % 1000000);
89#endif
90
91        (void)T_snprintf(string, sizeof(T_time_string),
92            "%" PRIu32 ".%06" PRIu32, s, f);
93        return string;
94}
95
96const char *
97T_time_to_string_ms(T_time time, T_time_string string)
98{
99        uint32_t s;
100        uint32_t f;
101
102#ifdef __rtems__
103        time = round_sbt(time);
104        s = (uint32_t)(time >> 32);
105        f = (uint32_t)(((uint64_t)1000 * (uint32_t)time) >> 32);
106#else
107        time /= 1000000;
108        s = (uint32_t)(time / 1000);
109        f = (uint32_t)(time % 1000);
110#endif
111
112        (void)T_snprintf(string, sizeof(T_time_string),
113            "%" PRIu32 ".%03" PRIu32, s, f);
114        return string;
115}
116
117const char *
118T_time_to_string_s(T_time time, T_time_string string)
119{
120        uint32_t s;
121
122#ifdef __rtems__
123        time = round_sbt(time);
124        s = (uint32_t)(time >> 32);
125#else
126        s = (uint32_t)(time / 1000000000);
127#endif
128
129        (void)T_snprintf(string, sizeof(T_time_string), "%" PRIu32, s);
130        return string;
131}
132
133const char *
134T_ticks_to_string_ns(T_ticks ticks, T_time_string string)
135{
136        return T_time_to_string_ns(T_ticks_to_time(ticks), string);
137}
138
139const char *
140T_ticks_to_string_us(T_ticks ticks, T_time_string string)
141{
142        return T_time_to_string_us(T_ticks_to_time(ticks), string);
143}
144
145const char *
146T_ticks_to_string_ms(T_ticks ticks, T_time_string string)
147{
148        return T_time_to_string_ms(T_ticks_to_time(ticks), string);
149}
150
151const char *
152T_ticks_to_string_s(T_ticks ticks, T_time_string string)
153{
154        return T_time_to_string_s(T_ticks_to_time(ticks), string);
155}
156
157uint64_t
158T_ticks_to_time(T_ticks ticks)
159{
160#ifdef __rtems__
161        return (uint64_t)rtems_counter_ticks_to_sbintime(ticks);
162#else
163        return ticks;
164#endif
165}
166
167T_ticks
168T_time_to_ticks(T_time time)
169{
170#ifdef __rtems__
171        return rtems_counter_sbintime_to_ticks((sbintime_t)time);
172#else
173        return time;
174#endif
175}
176
177T_time
178T_seconds_and_nanoseconds_to_time(uint32_t s, uint32_t ns)
179{
180#ifdef __rtems__
181        struct timespec ts;
182
183        ts.tv_sec = s;
184        ts.tv_nsec = (long)ns;
185        return (T_time)tstosbt(ts);
186#else
187        return (T_time)s * (T_time)1000000000 + (T_time)ns;
188#endif
189}
190
191void
192T_time_to_seconds_and_nanoseconds(T_time time, uint32_t *s, uint32_t *ns)
193{
194#ifdef __rtems__
195        time = round_sbt(time);
196        *s = (uint32_t)(time >> 32);
197        *ns = (uint32_t)(((uint64_t)1000000000 * (uint32_t)time) >> 32);
198#else
199        *s = (uint32_t)(time / 1000000000);
200        *ns = (uint32_t)(time % 1000000000);
201#endif
202}
203
204#ifndef __rtems__
205T_time
206T_now(void)
207{
208        struct timespec tp;
209
210        (void)clock_gettime(CLOCK_MONOTONIC, &tp);
211        return (T_time)tp.tv_sec * (T_time)1000000000 + (T_time)tp.tv_nsec;
212}
213
214T_ticks
215T_tick(void)
216{
217        return T_now();
218}
219#endif
220
221static atomic_uint dummy_time;
222
223T_time
224T_now_dummy(void)
225{
226        return atomic_fetch_add_explicit(&dummy_time, 1, memory_order_relaxed);
227}
228
229T_time
230T_now_via_tick(void)
231{
232        return T_ticks_to_time(T_tick());
233}
Note: See TracBrowser for help on using the repository browser.