xref: /netbsd-src/external/gpl3/gcc.old/dist/libgfortran/intrinsics/system_clock.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg /* Implementation of the SYSTEM_CLOCK intrinsic.
2*4c3eb207Smrg    Copyright (C) 2004-2020 Free Software Foundation, Inc.
3627f7eb2Smrg 
4627f7eb2Smrg This file is part of the GNU Fortran runtime library (libgfortran).
5627f7eb2Smrg 
6627f7eb2Smrg Libgfortran is free software; you can redistribute it and/or
7627f7eb2Smrg modify it under the terms of the GNU General Public
8627f7eb2Smrg License as published by the Free Software Foundation; either
9627f7eb2Smrg version 3 of the License, or (at your option) any later version.
10627f7eb2Smrg 
11627f7eb2Smrg Libgfortran is distributed in the hope that it will be useful,
12627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14627f7eb2Smrg GNU General Public License for more details.
15627f7eb2Smrg 
16627f7eb2Smrg Under Section 7 of GPL version 3, you are granted additional
17627f7eb2Smrg permissions described in the GCC Runtime Library Exception, version
18627f7eb2Smrg 3.1, as published by the Free Software Foundation.
19627f7eb2Smrg 
20627f7eb2Smrg You should have received a copy of the GNU General Public License and
21627f7eb2Smrg a copy of the GCC Runtime Library Exception along with this program;
22627f7eb2Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23627f7eb2Smrg <http://www.gnu.org/licenses/>.  */
24627f7eb2Smrg 
25627f7eb2Smrg #include "libgfortran.h"
26627f7eb2Smrg 
27627f7eb2Smrg #include <limits.h>
28627f7eb2Smrg 
29627f7eb2Smrg #include "time_1.h"
30627f7eb2Smrg 
31627f7eb2Smrg 
32627f7eb2Smrg #if !defined(__MINGW32__)
33627f7eb2Smrg 
34627f7eb2Smrg /* POSIX states that CLOCK_REALTIME must be present if clock_gettime
35627f7eb2Smrg    is available, others are optional.  */
36627f7eb2Smrg #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETTIME_LIBRT)
37627f7eb2Smrg #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) \
38627f7eb2Smrg   && _POSIX_MONOTONIC_CLOCK >= 0
39627f7eb2Smrg #define GF_CLOCK_MONOTONIC CLOCK_MONOTONIC
40627f7eb2Smrg #else
41627f7eb2Smrg #define GF_CLOCK_MONOTONIC CLOCK_REALTIME
42627f7eb2Smrg #endif
43627f7eb2Smrg #endif
44627f7eb2Smrg 
45627f7eb2Smrg /* Weakref trickery for clock_gettime().  On Glibc <= 2.16,
46627f7eb2Smrg    clock_gettime() requires us to link in librt, which also pulls in
47627f7eb2Smrg    libpthread.  In order to avoid this by default, only call
48627f7eb2Smrg    clock_gettime() through a weak reference.  */
49627f7eb2Smrg #if SUPPORTS_WEAKREF && defined(HAVE_CLOCK_GETTIME_LIBRT)
50627f7eb2Smrg static int weak_gettime (clockid_t, struct timespec *)
51627f7eb2Smrg   __attribute__((__weakref__("clock_gettime")));
52627f7eb2Smrg #endif
53627f7eb2Smrg 
54627f7eb2Smrg 
55627f7eb2Smrg /* High resolution monotonic clock, falling back to the realtime clock
56627f7eb2Smrg    if the target does not support such a clock.
57627f7eb2Smrg 
58627f7eb2Smrg    Arguments:
59627f7eb2Smrg    secs     - OUTPUT, seconds
60627f7eb2Smrg    fracsecs - OUTPUT, fractional seconds, units given by tk argument
61627f7eb2Smrg    tk       - OUTPUT, clock resolution [counts/sec]
62627f7eb2Smrg 
63627f7eb2Smrg    If the target supports a monotonic clock, the OUTPUT arguments
64627f7eb2Smrg    represent a monotonically incrementing clock starting from some
65627f7eb2Smrg    unspecified time in the past.
66627f7eb2Smrg 
67627f7eb2Smrg    If a monotonic clock is not available, falls back to the realtime
68627f7eb2Smrg    clock which is not monotonic.
69627f7eb2Smrg 
70627f7eb2Smrg    Return value: 0 for success, -1 for error. In case of error, errno
71627f7eb2Smrg    is set.
72627f7eb2Smrg */
73627f7eb2Smrg static int
gf_gettime_mono(time_t * secs,long * fracsecs,long * tck)74627f7eb2Smrg gf_gettime_mono (time_t * secs, long * fracsecs, long * tck)
75627f7eb2Smrg {
76627f7eb2Smrg   int err;
77627f7eb2Smrg #ifdef HAVE_CLOCK_GETTIME
78627f7eb2Smrg   struct timespec ts;
79627f7eb2Smrg   *tck = 1000000000;
80627f7eb2Smrg   err = clock_gettime (GF_CLOCK_MONOTONIC, &ts);
81627f7eb2Smrg   *secs = ts.tv_sec;
82627f7eb2Smrg   *fracsecs = ts.tv_nsec;
83627f7eb2Smrg   return err;
84627f7eb2Smrg #else
85627f7eb2Smrg #if SUPPORTS_WEAKREF && defined(HAVE_CLOCK_GETTIME_LIBRT)
86627f7eb2Smrg   if (weak_gettime)
87627f7eb2Smrg     {
88627f7eb2Smrg       struct timespec ts;
89627f7eb2Smrg       *tck = 1000000000;
90627f7eb2Smrg       err = weak_gettime (GF_CLOCK_MONOTONIC, &ts);
91627f7eb2Smrg       *secs = ts.tv_sec;
92627f7eb2Smrg       *fracsecs = ts.tv_nsec;
93627f7eb2Smrg       return err;
94627f7eb2Smrg     }
95627f7eb2Smrg #endif
96627f7eb2Smrg   *tck = 1000000;
97627f7eb2Smrg   err = gf_gettime (secs, fracsecs);
98627f7eb2Smrg   return err;
99627f7eb2Smrg #endif
100627f7eb2Smrg }
101627f7eb2Smrg 
102627f7eb2Smrg #endif /* !__MINGW32__  */
103627f7eb2Smrg 
104627f7eb2Smrg extern void
105627f7eb2Smrg system_clock_4 (GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
106627f7eb2Smrg 		GFC_INTEGER_4 *count_max);
107627f7eb2Smrg export_proto(system_clock_4);
108627f7eb2Smrg 
109627f7eb2Smrg extern void
110627f7eb2Smrg system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
111627f7eb2Smrg 		GFC_INTEGER_8 *count_max);
112627f7eb2Smrg export_proto(system_clock_8);
113627f7eb2Smrg 
114627f7eb2Smrg 
115627f7eb2Smrg /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
116627f7eb2Smrg    intrinsic subroutine.  It returns the number of clock ticks for the current
117627f7eb2Smrg    system time, the number of ticks per second, and the maximum possible value
118627f7eb2Smrg    for COUNT.  */
119627f7eb2Smrg 
120627f7eb2Smrg void
system_clock_4(GFC_INTEGER_4 * count,GFC_INTEGER_4 * count_rate,GFC_INTEGER_4 * count_max)121627f7eb2Smrg system_clock_4 (GFC_INTEGER_4 *count, GFC_INTEGER_4 *count_rate,
122627f7eb2Smrg 	       GFC_INTEGER_4 *count_max)
123627f7eb2Smrg {
124627f7eb2Smrg #if defined(__MINGW32__)
125627f7eb2Smrg   if (count)
126627f7eb2Smrg     {
127627f7eb2Smrg       /* Use GetTickCount here as the resolution and range is
128627f7eb2Smrg 	 sufficient for the INTEGER(kind=4) version, and
129627f7eb2Smrg 	 QueryPerformanceCounter has potential issues.  */
130627f7eb2Smrg       uint32_t cnt = GetTickCount ();
131627f7eb2Smrg       if (cnt > GFC_INTEGER_4_HUGE)
132627f7eb2Smrg 	cnt = cnt - GFC_INTEGER_4_HUGE - 1;
133627f7eb2Smrg       *count = cnt;
134627f7eb2Smrg     }
135627f7eb2Smrg   if (count_rate)
136627f7eb2Smrg     *count_rate = 1000;
137627f7eb2Smrg   if (count_max)
138627f7eb2Smrg     *count_max = GFC_INTEGER_4_HUGE;
139627f7eb2Smrg #else
140627f7eb2Smrg   time_t secs;
141627f7eb2Smrg   long fracsecs, tck;
142627f7eb2Smrg 
143627f7eb2Smrg   if (gf_gettime_mono (&secs, &fracsecs, &tck) == 0)
144627f7eb2Smrg     {
145627f7eb2Smrg       long tck_out = tck > 1000 ? 1000 : tck;
146627f7eb2Smrg       long tck_r = tck / tck_out;
147627f7eb2Smrg       GFC_UINTEGER_4 ucnt = (GFC_UINTEGER_4) secs * tck_out;
148627f7eb2Smrg       ucnt += fracsecs / tck_r;
149627f7eb2Smrg       if (ucnt > GFC_INTEGER_4_HUGE)
150627f7eb2Smrg 	ucnt = ucnt - GFC_INTEGER_4_HUGE - 1;
151627f7eb2Smrg       if (count)
152627f7eb2Smrg 	*count = ucnt;
153627f7eb2Smrg       if (count_rate)
154627f7eb2Smrg 	*count_rate = tck_out;
155627f7eb2Smrg       if (count_max)
156627f7eb2Smrg 	*count_max = GFC_INTEGER_4_HUGE;
157627f7eb2Smrg     }
158627f7eb2Smrg   else
159627f7eb2Smrg     {
160627f7eb2Smrg       if (count)
161627f7eb2Smrg 	*count = - GFC_INTEGER_4_HUGE;
162627f7eb2Smrg       if (count_rate)
163627f7eb2Smrg 	*count_rate = 0;
164627f7eb2Smrg       if (count_max)
165627f7eb2Smrg 	*count_max = 0;
166627f7eb2Smrg     }
167627f7eb2Smrg #endif
168627f7eb2Smrg }
169627f7eb2Smrg 
170627f7eb2Smrg 
171627f7eb2Smrg /* INTEGER(8) version of the above routine.  */
172627f7eb2Smrg 
173627f7eb2Smrg void
system_clock_8(GFC_INTEGER_8 * count,GFC_INTEGER_8 * count_rate,GFC_INTEGER_8 * count_max)174627f7eb2Smrg system_clock_8 (GFC_INTEGER_8 *count, GFC_INTEGER_8 *count_rate,
175627f7eb2Smrg 		 GFC_INTEGER_8 *count_max)
176627f7eb2Smrg {
177627f7eb2Smrg #if defined(__MINGW32__)
178627f7eb2Smrg   LARGE_INTEGER cnt;
179627f7eb2Smrg   LARGE_INTEGER freq;
180627f7eb2Smrg   bool fail = false;
181627f7eb2Smrg   if (count && !QueryPerformanceCounter (&cnt))
182627f7eb2Smrg     fail = true;
183627f7eb2Smrg   if (count_rate && !QueryPerformanceFrequency (&freq))
184627f7eb2Smrg     fail = true;
185627f7eb2Smrg   if (fail)
186627f7eb2Smrg     {
187627f7eb2Smrg       if (count)
188627f7eb2Smrg 	*count = - GFC_INTEGER_8_HUGE;
189627f7eb2Smrg       if (count_rate)
190627f7eb2Smrg 	*count_rate = 0;
191627f7eb2Smrg       if (count_max)
192627f7eb2Smrg 	*count_max = 0;
193627f7eb2Smrg     }
194627f7eb2Smrg   else
195627f7eb2Smrg     {
196627f7eb2Smrg       if (count)
197627f7eb2Smrg 	*count = cnt.QuadPart;
198627f7eb2Smrg       if (count_rate)
199627f7eb2Smrg 	*count_rate = freq.QuadPart;
200627f7eb2Smrg       if (count_max)
201627f7eb2Smrg 	*count_max = GFC_INTEGER_8_HUGE;
202627f7eb2Smrg     }
203627f7eb2Smrg #else
204627f7eb2Smrg   time_t secs;
205627f7eb2Smrg   long fracsecs, tck;
206627f7eb2Smrg 
207627f7eb2Smrg   if (gf_gettime_mono (&secs, &fracsecs, &tck) == 0)
208627f7eb2Smrg     {
209627f7eb2Smrg       GFC_UINTEGER_8 ucnt = (GFC_UINTEGER_8) secs * tck;
210627f7eb2Smrg       ucnt += fracsecs;
211627f7eb2Smrg       if (ucnt > GFC_INTEGER_8_HUGE)
212627f7eb2Smrg 	ucnt = ucnt - GFC_INTEGER_8_HUGE - 1;
213627f7eb2Smrg       if (count)
214627f7eb2Smrg 	*count = ucnt;
215627f7eb2Smrg       if (count_rate)
216627f7eb2Smrg 	*count_rate = tck;
217627f7eb2Smrg       if (count_max)
218627f7eb2Smrg 	*count_max = GFC_INTEGER_8_HUGE;
219627f7eb2Smrg     }
220627f7eb2Smrg   else
221627f7eb2Smrg     {
222627f7eb2Smrg       if (count)
223627f7eb2Smrg 	*count = - GFC_INTEGER_8_HUGE;
224627f7eb2Smrg       if (count_rate)
225627f7eb2Smrg 	*count_rate = 0;
226627f7eb2Smrg       if (count_max)
227627f7eb2Smrg 	*count_max = 0;
228627f7eb2Smrg     }
229627f7eb2Smrg #endif
230627f7eb2Smrg }
231