]> git.ipfire.org Git - thirdparty/glibc.git/blob - support/timespec.c
Fix time/tst-cpuclock1 intermitent failures
[thirdparty/glibc.git] / support / timespec.c
1 /* Support code for timespec checks.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <support/timespec.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <assert.h>
23 #include <intprops.h>
24
25 void
26 test_timespec_before_impl (const char *file, int line,
27 const struct timespec left,
28 const struct timespec right)
29 {
30 if (left.tv_sec > right.tv_sec
31 || (left.tv_sec == right.tv_sec
32 && left.tv_nsec > right.tv_nsec)) {
33 support_record_failure ();
34 const struct timespec diff = timespec_sub (left, right);
35 printf ("%s:%d: %jd.%09jds not before %jd.%09jds "
36 "(difference %jd.%09jds)\n",
37 file, line,
38 (intmax_t) left.tv_sec, (intmax_t) left.tv_nsec,
39 (intmax_t) right.tv_sec, (intmax_t) right.tv_nsec,
40 (intmax_t) diff.tv_sec, (intmax_t) diff.tv_nsec);
41 }
42 }
43
44 void
45 test_timespec_equal_or_after_impl (const char *file, int line,
46 const struct timespec left,
47 const struct timespec right)
48 {
49 if (left.tv_sec < right.tv_sec
50 || (left.tv_sec == right.tv_sec
51 && left.tv_nsec < right.tv_nsec)) {
52 support_record_failure ();
53 const struct timespec diff = timespec_sub (right, left);
54 printf ("%s:%d: %jd.%09jds not after %jd.%09jds "
55 "(difference %jd.%09jds)\n",
56 file, line,
57 (intmax_t) left.tv_sec, (intmax_t) left.tv_nsec,
58 (intmax_t) right.tv_sec, (intmax_t) right.tv_nsec,
59 (intmax_t) diff.tv_sec, (intmax_t) diff.tv_nsec);
60 }
61 }
62
63 /* Convert TIME to nanoseconds stored in a long.
64 Returns long maximum or minimum if the conversion overflows
65 or underflows, respectively. */
66 long
67 support_timespec_ns (struct timespec time)
68 {
69 long time_ns;
70 if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns))
71 {
72 return (time.tv_sec < 0) ? TYPE_MINIMUM(long): TYPE_MAXIMUM(long);
73 }
74 if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns))
75 {
76 return (time.tv_nsec < 0) ? TYPE_MINIMUM(long): TYPE_MAXIMUM(long);
77 }
78 return time_ns;
79 }
80
81 /* Returns time normalized timespec with .tv_nsec < TIMESPEC_HZ
82 and the whole seconds added to .tv_sec. If an overflow or
83 underflow occurs the values are clamped to its maximum or
84 minimum respectively. */
85 struct timespec
86 support_timespec_normalize (struct timespec time)
87 {
88 struct timespec norm;
89 if (INT_ADD_WRAPV (time.tv_sec, (time.tv_nsec / TIMESPEC_HZ), &norm.tv_sec))
90 {
91 norm.tv_sec = (time.tv_nsec < 0) ? TYPE_MINIMUM (time_t): TYPE_MAXIMUM (time_t);
92 norm.tv_nsec = (time.tv_nsec < 0) ? -1 * (TIMESPEC_HZ - 1) : TIMESPEC_HZ - 1;
93 return norm;
94 }
95 norm.tv_nsec = time.tv_nsec % TIMESPEC_HZ;
96 return norm;
97 }
98
99 /* Returns TRUE if the observed time is within the given percentage
100 bounds of the expected time, and FALSE otherwise.
101 For example the call
102
103 support_timespec_check_in_range(expected, observed, 0.5, 1.2);
104
105 will check if
106
107 0.5 of expected <= observed <= 1.2 of expected
108
109 In other words it will check if observed time is within 50% to
110 120% of the expected time. */
111 int
112 support_timespec_check_in_range (struct timespec expected, struct timespec observed,
113 double lower_bound, double upper_bound)
114 {
115 assert (upper_bound >= lower_bound);
116 long expected_norm, observed_norm;
117 expected_norm = support_timespec_ns (expected);
118 /* Don't divide by zero */
119 assert(expected_norm != 0);
120 observed_norm = support_timespec_ns (observed);
121 double ratio = (double)observed_norm / expected_norm;
122 return (lower_bound <= ratio && ratio <= upper_bound);
123 }