diff --git a/configure.ac b/configure.ac index 2fd124576..87d3a470a 100644 --- a/configure.ac +++ b/configure.ac @@ -908,7 +908,7 @@ AC_STRUCT_TM AC_STRUCT_TIMEZONE AC_CHECK_FUNCS([localtime]) -AC_CHECK_FUNCS([ftime gettimeofday]) +AC_CHECK_FUNCS([gettimeofday times ftime]) # Do not use time or getrusage function for CPU time measurement under OpenMP if test "x$enable_openmp" = xno; then AC_CHECK_FUNCS([time getrusage]) diff --git a/src/frontend/resource.c b/src/frontend/resource.c index 09b40863b..09e116eb7 100644 --- a/src/frontend/resource.c +++ b/src/frontend/resource.c @@ -22,7 +22,7 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group #include -#include "../misc/misc_time.h" /* timediff */ +#include "../misc/misc_time.h" /* timebegin */ #ifdef XSPICE /* gtri - add - 12/12/90 - wbk - include ipc stuff */ @@ -81,15 +81,7 @@ init_rlimits(void) void init_time(void) { -#ifdef HAVE_GETRUSAGE -#else -# ifdef HAVE_TIMES -# else -# ifdef HAVE_FTIME - ftime(&timebegin); -# endif -# endif -#endif + timebegin(); } diff --git a/src/include/ngspice/ngspice.h b/src/include/ngspice/ngspice.h index 0937f597d..842a90e4f 100644 --- a/src/include/ngspice/ngspice.h +++ b/src/include/ngspice/ngspice.h @@ -117,15 +117,17 @@ # include # include # endif -#else -# ifdef HAVE_TIMES -# include -# include -# else -# ifdef HAVE_FTIME -# include -# endif -# endif +#endif + +#ifdef HAVE_TIMES +# include +# include +#endif +#ifdef HAVE_GETTIMEOFDAY +# include +#endif +#ifdef HAVE_FTIME +# include #endif #ifdef HAVE_UNISTD_H diff --git a/src/misc/misc_time.c b/src/misc/misc_time.c index 415089956..39a06dad1 100644 --- a/src/misc/misc_time.c +++ b/src/misc/misc_time.c @@ -14,28 +14,6 @@ Copyright 1990 Regents of the University of California. All rights reserved. #include #endif -#ifdef HAVE_GETRUSAGE -# include -# include -# include -#else -# ifdef HAVE_TIMES -# include -# include -# include -# else -# ifdef HAVE_FTIME -/* default to ftime if we can't get real CPU times */ -# include -# include -# endif -# endif -#endif - -#ifdef HAVE_FTIME -# include -#endif - /* Return the date. Return value is static data. */ @@ -70,8 +48,6 @@ datestring(void) #ifdef HAVE_FTIME -struct timeb timebegin; - void timediff(struct timeb *now, struct timeb *begin, int *sec, int *msec) { @@ -87,32 +63,60 @@ void timediff(struct timeb *now, struct timeb *begin, int *sec, int *msec) #endif -/* - * How many seconds have elapsed in running time. - * This is the routine called in IFseconds + +/* Initialize time */ + +#ifdef HAVE_GETTIMEOFDAY +struct timeval timezero; +void timebegin(void) { + gettimeofday(&timezero, NULL); +} +#else +#ifdef HAVE_TIMES + clock_t timezero; + void timebegin(void) { + struct tms ruse; + timezero = times(&ruse); + } +#else +#ifdef HAVE_FTIME + struct timeb timezero; + void timebegin(void) { + ftime(&timezero); + } +#endif /* FTIME */ +#endif /* TIMES */ +#endif /* GETTIMEOFDAY */ + + +/* + * How many seconds have elapsed in running time. + * This is the routine called in IFseconds */ double seconds(void) { -#ifdef HAVE_GETRUSAGE - int ret; - struct rusage ruse; +#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); - 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; - struct tms tmsbuf; - - times(&tmsbuf); - return((double) tmsbuf.tms_utime / HZ); + 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); #else #ifdef HAVE_FTIME @@ -120,14 +124,16 @@ seconds(void) int sec, msec; ftime(&timenow); - timediff(&timenow, &timebegin, &sec, &msec); + + sec = (int) timenow.time - (int) timezero.time; + msec = (int) timenow.millitm - (int) timezero.millitm; return(sec + (double) msec / 1000.0); #else /* unknown */ - /* don't know how to do this in general. */ - return(-1.0); /* Obvious error condition */ -#endif /* !FTIME */ -#endif /* !SYSV */ -#endif /* !BSD */ + return(-1.0); /* Obvious error condition */ + +#endif /* FTIME */ +#endif /* TIMES */ +#endif /* GETTIMEOFDAY */ } diff --git a/src/misc/misc_time.h b/src/misc/misc_time.h index 95cc1bcb2..8ef1a0ac1 100644 --- a/src/misc/misc_time.h +++ b/src/misc/misc_time.h @@ -8,11 +8,10 @@ char * datestring(void); double seconds(void); +void timebegin(void); #ifdef HAVE_FTIME -extern struct timeb timebegin; - void timediff(struct timeb *, struct timeb *, int *, int *); #endif