/* Wrappers for platform timing functions.
- Copyright (C) 2003, 2007, 2009, 2011 Free Software Foundation, Inc.
+ Copyright (C) 2003-2015 Free Software Foundation, Inc.
This file is part of the GNU Fortran runtime library (libgfortran).
CPU_TIME intrinsics. Returns 0 for success or -1 if no
CPU time could be computed. */
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(__CYGWIN__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
struct tms buf;
clock_t err;
err = times (&buf);
- *user_sec = buf.tms_utime / HZ;
- *user_usec = buf.tms_utime % HZ * (1000000. / HZ);
- *system_sec = buf.tms_stime / HZ;
- *system_usec = buf.tms_stime % HZ * (1000000. / HZ);
+ long hz = HZ;
+ *user_sec = buf.tms_utime / hz;
+ *user_usec = (buf.tms_utime % hz) * (1000000. / hz);
+ *system_sec = buf.tms_stime / hz;
+ *system_usec = (buf.tms_stime % hz) * (1000000. / hz);
if ((err == (clock_t) -1) && errno != 0)
return -1;
return 0;
+#elif defined(HAVE_CLOCK_GETTIME) && (defined(CLOCK_PROCESS_CPUTIME_ID) \
+ || defined(CLOCK_THREAD_CPUTIME_ID))
+ /* Newer versions of VxWorks have CLOCK_THREAD_CPUTIME_ID giving
+ per-thread CPU time. CLOCK_PROCESS_CPUTIME_ID would be better
+ but is not available. */
+#ifndef CLOCK_PROCESS_CPUTIME_ID
+#define CLOCK_PROCESS_CPUTIME_ID CLOCK_THREAD_CPUTIME_ID
+#endif
+ struct timespec ts;
+ int err = clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
+ *user_sec = ts.tv_sec;
+ *user_usec = ts.tv_nsec / 1000;
+ *system_sec = *system_usec = 0;
+ return err;
+
#else
clock_t c = clock ();
*user_sec = c / CLOCKS_PER_SEC;
- *user_usec = c % CLOCKS_PER_SEC * (1000000. / CLOCKS_PER_SEC);
+ *user_usec = (c % CLOCKS_PER_SEC) * (1000000. / CLOCKS_PER_SEC);
*system_sec = *system_usec = 0;
if (c == (clock_t) -1)
return -1;
#endif
-/* Realtime clock with microsecond resolution, falling back to less
- precise functions if the target does not support gettimeofday().
+/* Realtime clock with microsecond resolution, falling back to other
+ functions if the target does not support gettimeofday().
Arguments:
secs - OUTPUT, seconds
usecs - OUTPUT, microseconds
The OUTPUT arguments shall represent the number of seconds and
- nanoseconds since the Epoch.
+ microseconds since the Epoch.
Return value: 0 for success, -1 for error. In case of error, errno
is set.
*secs = tv.tv_sec;
*usecs = tv.tv_usec;
return err;
+#elif defined(HAVE_CLOCK_GETTIME)
+ struct timespec ts;
+ int err = clock_gettime (CLOCK_REALTIME, &ts);
+ *secs = ts.tv_sec;
+ *usecs = ts.tv_nsec / 1000;
+ return err;
#else
time_t t = time (NULL);
*secs = t;