From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5765 invoked by alias); 11 Apr 2013 20:14:19 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 5628 invoked by uid 48); 11 Apr 2013 20:14:15 -0000 From: "burnus at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/56919] [4.6/4.7/4.8/4.9 Regression] Wrong result for SYSTEM_CLOCK on Cygwin Date: Thu, 11 Apr 2013 20:14:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: burnus at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 X-SW-Source: 2013-04/txt/msg01016.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56919 --- Comment #3 from Tobias Burnus 2013-04-11 20:14:13 UTC --- First, I think we made a thinko with the random_seed example at http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html it uses (as does Angelo's code): CALL SYSTEM_CLOCK(COUNT=clock) seed = clock + 37 * (/ (i - 1, i = 1, n) /) CALL RANDOM_SEED(PUT = seed) If clock_gettime uses the seconds since the epoch (or since system start), everything is fine. However, under Cygwin (cf. comment 1) the time since process start seems to be used. Result: The timing might be quite similar. Actually, at least with the testing of Angelo, http://gcc.gnu.org/ml/fortran/2013-04/msg00092.html + http://gcc.gnu.org/ml/fortran/2013-04/msg00084.html, it even seems as if the clock rate is that slow that one can do several system calls in no time (= 0 nanoseconds). Also gettimeofday and clock_gettime(CLOCK_REALTIME,...) show the same number of nanoseconds with Cygwin. (Contrary to Linux.) Thus, we should really update the RANDOM_SEED example to use something wall-time based. * * * (In reply to comment #2) > Aagh, one wonders what demented logic caused them to claim they support it and > then not actually do so in any kind of useful manner.. *sigh* If one looks at newlib, one finds in src/newlib/libc/include/time.h: #define CLOCK_REALTIME (clockid_t)1 #define CLOCK_PROCESS_CPUTIME_ID (clockid_t)2 #define CLOCK_THREAD_CPUTIME_ID (clockid_t)3 #define CLOCK_MONOTONIC (clockid_t)4 And src/newlib/libc/sys/linux/clock_gettime.c does not handle CLOCK_MONOTONIC: http://sourceware.org/cgi-bin/cvsweb.cgi/src/newlib/libc/sys/linux/clock_gettime.c?rev=1.1&content-type=text/x-cvsweb-markup&cvsroot=src If one looks at the code, that should return "-1" as status code, if CLOCK_MONOTONIC has failed. I think we should change system_clock to use: /* Newlib defines CLOCK_MONOTONIC but doesn't support it. Thus, if clock_gettime failed, fall through to gf_gettime. */ if (return_value == 0) return; ... That way, all those embedded systems which use newlib will get a proper result. (gfortran has embedded users, at least one user on VxWorks.) * * * > > Nick suggests to simply call gf_gettime unconditionally. [3] > I wonder what, if any, specific issues there actually are... I wonder as well - at least for newlib it is clear why it fails. > I don't know how cygwin implements CLOCK_MONOTONIC See: http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/times.cc?cvsroot=src It uses QueryPerformanceCounter (cf. hires_ns::nsecs, called by clock_gettime). I have not yet fully understand why it is 0. Cf. http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904%28v=vs.85%29.aspx > but anyway, AFAIK on > windows a robust monotonic clock is available via the GetTickCount (and on > Vista/2008 and up, GetTickCount64 as well) function. Thus I'd suggest that > both MINGW and CYGWIN version should use those functions in the > implementation of the SYSTEM_CLOCK intrinsic. Looking through the list of functions at the URL below, I think calling QueryPerformanceCounter makes most sense - which is what Cygwin does: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644900(v=vs.85).aspx * * * In summary: - Cygwin is probably okay - it just starts from 0 with the first call to system_clock(monotonic,...) which is fine but different to Linux. - Newlib's system_clock handling is completely broken, but as it returns -1, one could fall through to gf_gettime - gfortran's random_seed example is bad if the system_clock(monotonic,...) behaves as with Cygwin - or everything else based on the timing of the process instead of the system up time (like GetTickCount - with overflow issues) or ticks since the epoch (as system_clock on Linux). - There seems to be a design issue with system_clock according to Nick, but we don't know which one it is.