From f58705b473a518e85ba05c5d59d9ae56e32a5362 Mon Sep 17 00:00:00 2001 From: dwarning Date: Mon, 21 Oct 2024 15:36:33 +0200 Subject: [PATCH] separation between rusage and second() calculation, no timediff() for second() --- configure.ac | 6 +++--- src/frontend/resource.c | 46 ++++++++++++++++++++++------------------- src/misc/misc_time.c | 38 ++++++++++++++++++++++++++++++---- src/misc/misc_time.h | 24 +++++++++++++++++++++ 4 files changed, 86 insertions(+), 28 deletions(-) diff --git a/configure.ac b/configure.ac index 9d0e12ceb..c9a38e080 100644 --- a/configure.ac +++ b/configure.ac @@ -906,13 +906,13 @@ AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h stddef.h sys/file.h sys/param # Check time and resources headers and functions: AC_STRUCT_TM AC_STRUCT_TIMEZONE -AC_CHECK_FUNCS([localtime]) +AC_CHECK_FUNCS([time localtime]) AC_CHECK_FUNCS([clock_gettime gettimeofday ftime times]) -# Do not use time or getrusage function for CPU time measurement under OpenMP +# Do not use getrusage function for CPU time measurement under OpenMP if test "x$enable_openmp" = xno; then - AC_CHECK_FUNCS([time getrusage]) + AC_CHECK_FUNCS([getrusage]) fi AC_CHECK_FUNCS([utimes]) AC_CHECK_FUNCS([getrlimit ulimit], [break]) diff --git a/src/frontend/resource.c b/src/frontend/resource.c index 8990fa789..40b73f91b 100644 --- a/src/frontend/resource.c +++ b/src/frontend/resource.c @@ -80,18 +80,27 @@ init_rlimits(void) #if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY || defined HAVE_FTIME PortableTime timebegin; #endif +#ifdef HAVE_GETRUSAGE + GTimer gtimer; +#endif +#ifdef HAVE_TIMES + TTimer ttimer; +#endif void init_time(void) { -#ifdef HAVE_GETRUSAGE +#if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY || defined HAVE_FTIME + get_portable_time(&timebegin); #else -# ifdef HAVE_TIMES -# else -# if defined HAVE_CLOCK_GETTIME || defined HAVE_GETTIMEOFDAY || defined HAVE_FTIME - get_portable_time(&timebegin); -# endif -# endif +#ifdef HAVE_GETRUSAGE + start_timer(>imer); +#else +#ifdef HAVE_TIMES + clock_t start_clock; + start_clock = start_timer(&ttimer); +#endif +#endif #endif } @@ -179,24 +188,20 @@ printres(char *name) cpu_elapsed = "elapsed"; #else #ifdef HAVE_GETRUSAGE - int ret; - struct rusage ruse; - memset(&ruse, 0, sizeof(ruse)); - ret = getrusage(RUSAGE_SELF, &ruse); - if (ret == -1) - perror("getrusage(): "); + stop_timer(>imer); - total_sec = (int) (ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec); - total_msec = (int) (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) / 1000; + total_sec = (int) (gtimer.end.ru_utime.tv_sec + gtimer.start.ru_stime.tv_sec); + total_msec = (int) (gtimer.end.ru_utime.tv_usec + gtimer.start.ru_stime.tv_usec) / 1000; cpu_elapsed = "CPU"; #else #ifdef HAVE_TIMES - struct tms ruse; - times(&ruse); - clock_t x = ruse.tms_utime + ruse.tms_stime; + clock_t stop_clock = stop_timer(&ttimer); + clock_t user_time = (ttimer.end.tms_utime - ttimer.start.tms_utime); + clock_t system_time = (ttimer.end.tms_stime - ttimer.start.tms_stime); + clock_t x = user_time + system_time; clock_t hz = (clock_t) sysconf(_SC_CLK_TCK); - total_sec = x / hz; - total_msec = ((x % hz) * 1000) / hz; + total_sec = (int) (x / hz); + total_msec = (int) (((x % hz) * 1000) / hz); cpu_elapsed = "CPU"; #else # define NO_RUDATA @@ -204,7 +209,6 @@ printres(char *name) # endif # endif - #ifndef NO_RUDATA if (total_msec >= 1000) { diff --git a/src/misc/misc_time.c b/src/misc/misc_time.c index 8de83df2a..1b36352b5 100644 --- a/src/misc/misc_time.c +++ b/src/misc/misc_time.c @@ -93,7 +93,8 @@ void get_portable_time(PortableTime *pt) { /* * How many seconds have elapsed in running time. - * This is the routine called in IFseconds + * This is the routine called in IFseconds where start / stop is handled + * and we don't need calculate timediff here. */ double @@ -105,8 +106,7 @@ seconds(void) int sec, msec; get_portable_time(&timenow); - timediff(&timenow, &timebegin, &sec, &msec); - return(sec + (double) msec / 1000.0); + return((double) timenow.seconds + (double) timenow.milliseconds / 1000.0); #else #ifdef HAVE_GETRUSAGE int ret; @@ -125,7 +125,7 @@ seconds(void) struct tms tmsbuf; times(&tmsbuf); - return((double) tmsbuf.tms_utime / HZ); + return((double) tmsbuf.tms_utime / (clock_t) sysconf(_SC_CLK_TCK)); #else /* unknown */ @@ -135,3 +135,33 @@ seconds(void) #endif /* TIMES */ #endif /* CLOCK_GETTIME || GETTIMEOFDAY || FTIME */ } + +#ifdef HAVE_GETRUSAGE +void start_timer(GTimer *timer) { + int ret; + ret = getrusage(RUSAGE_SELF, &timer->start); + if(ret == -1) { + perror("getrusage(): "); + } +} +void stop_timer(GTimer *timer) { + int ret; + ret = getrusage(RUSAGE_SELF, &timer->end); + if(ret == -1) { + perror("getrusage(): "); + } +} +#endif /* GETRUSAGE */ + +#ifdef HAVE_TIMES +clock_t start_timer(TTimer *timer) { + clock_t start_clock; + start_clock = times(&timer->start); + return start_clock; +} +clock_t stop_timer(TTimer *timer) { + clock_t stop_clock; + stop_clock = times(&timer->end); + return stop_clock; +} +#endif /* TIMES */ diff --git a/src/misc/misc_time.h b/src/misc/misc_time.h index f394310a0..59088b7b9 100644 --- a/src/misc/misc_time.h +++ b/src/misc/misc_time.h @@ -21,4 +21,28 @@ void timediff(PortableTime *, PortableTime *, int *, int *); #endif +#ifdef HAVE_GETRUSAGE + +typedef struct { + struct rusage start; + struct rusage end; +} GTimer; + +void start_timer(GTimer *); +void stop_timer(GTimer *); + +#endif + +#ifdef HAVE_TIMES + +typedef struct { + struct tms start; + struct tms end; +} TTimer; + +clock_t start_timer(TTimer *); +clock_t stop_timer(TTimer *); + +#endif + #endif