diff --git a/configure.ac b/configure.ac index 780973b1c..4d5d55c37 100644 --- a/configure.ac +++ b/configure.ac @@ -910,13 +910,10 @@ AC_CHECK_FUNCS([localtime]) case $host_os in *mingw* | *msys* ) - AC_CHECK_FUNCS([gettimeofday times]) - AC_CHECK_LIB([msvcr100], [ftime], - [AC_DEFINE([HAVE_FTIME], [1], [Have ftime routines in libmsvcr100]) - LIBS="$LIBS -lmsvcr100"]) + AC_CHECK_FUNCS([clock_gettime gettimeofday times ftime]) ;; *) - AC_CHECK_FUNCS([gettimeofday times ftime]) + AC_CHECK_FUNCS([clock_gettime gettimeofday times ftime]) ;; esac diff --git a/src/frontend/resource.c b/src/frontend/resource.c index b54ee8772..0851adeb2 100644 --- a/src/frontend/resource.c +++ b/src/frontend/resource.c @@ -81,7 +81,16 @@ init_rlimits(void) void init_time(void) { - timebegin(); +#ifdef HAVE_GETRUSAGE +#else +# ifdef HAVE_TIMES +# else +# if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY + struct PortableTime timebegin; + get_portable_time(&timebegin); +# endif +# endif +#endif } @@ -182,11 +191,10 @@ printres(char *name) total_msec = ((x % hz) * 1000) / hz; cpu_elapsed = "CPU"; # else -# ifdef HAVE_FTIME - struct timeb timenow; - struct timeb ftimezero; - ftime(&timenow); - timediff(&timenow, &ftimezero, &total_sec, &total_msec); +#if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY + struct PortableTime timenow; + get_portable_time(&timenow); + timediff(&timenow, &timebegin, &total_sec, &total_msec); cpu_elapsed = "elapsed"; # else # define NO_RUDATA diff --git a/src/misc/misc_time.c b/src/misc/misc_time.c index 2abd1b511..5ef0bc18c 100644 --- a/src/misc/misc_time.c +++ b/src/misc/misc_time.c @@ -46,13 +46,15 @@ datestring(void) /* return time interval in seconds and milliseconds */ -#ifdef HAVE_FTIME +#if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY -void timediff(struct timeb *now, struct timeb *begin, int *sec, int *msec) +PortableTime timebegin; + +void timediff(PortableTime *now, PortableTime *begin, int *sec, int *msec) { - *msec = (int) now->millitm - (int) begin->millitm; - *sec = (int) now->time - (int) begin->time; + *msec = (int) now->milliseconds - (int) begin->milliseconds; + *sec = (int) now->seconds - (int) begin->seconds; if (*msec < 0) { *msec += 1000; (*sec)--; @@ -61,33 +63,24 @@ void timediff(struct timeb *now, struct timeb *begin, int *sec, int *msec) } -#endif - - -/* Initialize time */ - -#ifdef HAVE_GETTIMEOFDAY -struct timeval timezero; -void timebegin(void) { - gettimeofday(&timezero, NULL); +#ifdef HAVE_CLOCK_GETTIME +void get_portable_time(PortableTime *pt) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + + pt->seconds = ts.tv_sec; + pt->milliseconds = ts.tv_nsec / 1000000; // Convert nanoseconds to milliseconds } #else -#ifdef HAVE_TIMES - clock_t timezero; - void timebegin(void) { - struct tms ruse; - timezero = times(&ruse); - } -#else -#ifdef HAVE_FTIME - struct timeb ftimezero; - void timebegin(void) { - ftime(&ftimezero); - } -#endif /* FTIME */ -#endif /* TIMES */ -#endif /* GETTIMEOFDAY */ +void get_portable_time(PortableTime *pt) { + struct timeval tv; + gettimeofday(&tv, NULL); + pt->seconds = tv.tv_sec; + pt->milliseconds = tv.tv_usec / 1000; +} +#endif +#endif /* * How many seconds have elapsed in running time. @@ -97,43 +90,39 @@ void timebegin(void) { double seconds(void) { -#ifdef HAVE_GETTIMEOFDAY - struct timeval timenow; - int sec, msec, usec; - - gettimeofday(&timenow, NULL); - - sec = (int) timenow.tv_sec - (int) timezero.tv_sec; - usec = (int) timenow.tv_usec - (int) timezero.tv_usec; - msec = usec / 1000; // Get rid of extra accuracy - return(sec + (double) msec / 1000.0); +#ifdef HAVE_GETRUSAGE + int ret; + struct rusage ruse; + memset(&ruse, 0, sizeof(ruse)); + ret = getrusage(RUSAGE_SELF, &ruse); + if(ret == -1) { + perror("getrusage(): "); + return 1; + } + return ((double)ruse.ru_utime.tv_sec + (double) ruse.ru_utime.tv_usec / 1000000.0); #else #ifdef HAVE_TIMES - struct tms ruse; - long long msec; - clock_t timenow = times(&ruse); - double hz = (double) sysconf(_SC_CLK_TCK); - msec = (timenow - timezero) / hz * 1000.0; // Get rid of extra accuracy - return((double) msec / 1000.0); + struct tms tmsbuf; + + times(&tmsbuf); + return((double) tmsbuf.tms_utime / HZ); #else -#ifdef HAVE_FTIME - struct timeb timenow; +#if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY + struct PortableTime timenow; int sec, msec; - ftime(&timenow); - - sec = (int) timenow.time - (int) timezero.time; - msec = (int) timenow.millitm - (int) timezero.millitm; + get_portable_time(&timenow); + timediff(&timenow, &timebegin, &sec, &msec); return(sec + (double) msec / 1000.0); #else /* unknown */ return(-1.0); /* Obvious error condition */ -#endif /* FTIME */ +#endif /* GETTIMEOFDAY || CLOCK_GETTIME */ #endif /* TIMES */ -#endif /* GETTIMEOFDAY */ +#endif /* GETRUSAGE */ } diff --git a/src/misc/misc_time.h b/src/misc/misc_time.h index 8ef1a0ac1..46abe32b2 100644 --- a/src/misc/misc_time.h +++ b/src/misc/misc_time.h @@ -8,11 +8,16 @@ char * datestring(void); double seconds(void); -void timebegin(void); -#ifdef HAVE_FTIME +#if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY -void timediff(struct timeb *, struct timeb *, int *, int *); +typedef struct { + long seconds; + long milliseconds; +} PortableTime; + +void get_portable_time(PortableTime *); +void timediff(PortableTime *, PortableTime *, int *, int *); #endif