unify os dependent time code in one file - rm win_time.c

This commit is contained in:
dwarning 2024-11-03 09:00:04 +01:00 committed by Holger Vogt
parent 5000e0d57a
commit 40aa445ee8
6 changed files with 179 additions and 185 deletions

View File

@ -2,8 +2,6 @@
noinst_LTLIBRARIES = libmisc.la noinst_LTLIBRARIES = libmisc.la
EXTRA_DIST = win_time.c
libmisc_la_SOURCES = \ libmisc_la_SOURCES = \
getopt_long_bsd.c \ getopt_long_bsd.c \
getopt_bsd.h \ getopt_bsd.h \

View File

@ -1,148 +1,179 @@
/********** /**********
Copyright 1990 Regents of the University of California. All rights reserved. Copyright 1990 Regents of the University of California. All rights reserved.
**********/ **********/
/* /*
* Date and time utility functions * Date and time utility functions
*/ */
#include "ngspice/ngspice.h" #include "ngspice/ngspice.h"
#include <string.h> #include <string.h>
#if defined(HAS_WINGUI) || defined(__MINGW32__) || defined(_MSC_VER) #if defined(HAS_WINGUI) || defined(__MINGW32__) || defined(_MSC_VER)
#ifdef HAVE_QUERYPERFORMANCECOUNTER #ifdef HAVE_QUERYPERFORMANCECOUNTER
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
/* /*
* The ngspice.h file included above defines BOOLEAN (via bool.h) and this * The ngspice.h file included above defines BOOLEAN (via bool.h) and this
* clashes with the definition obtained from windows.h (via winnt.h). * clashes with the definition obtained from windows.h (via winnt.h).
* However, BOOLEAN is not used by this file so we can work round this problem * However, BOOLEAN is not used by this file so we can work round this problem
* by undefining BOOLEAN before including windows.h * by undefining BOOLEAN before including windows.h
* SJB - April 2005 * SJB - April 2005
*/ */
#undef BOOLEAN #undef BOOLEAN
#include <windows.h> #include <windows.h>
#endif #ifndef HAVE_GETTIMEOFDAY
#endif #include <winsock2.h>
#include <stdint.h> // portable: uint64_t MSVC: __int64
#include "misc_time.h"
/*/ MSVC defines this in winsock2.h!?
#ifdef USE_OMP typedef struct timeval {
#include <omp.h> long tv_sec;
#endif long tv_usec;
} timeval;
/* Return the date. Return value is static data. */ */
int gettimeofday(struct timeval * tp, void * unused)
char * {
datestring(void) // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
{ // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
#ifdef HAVE_LOCALTIME static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
static char tbuf[45];
struct tm *tp; SYSTEMTIME system_time;
char *ap; FILETIME file_time;
size_t i; uint64_t time;
time_t tloc; GetSystemTime( &system_time );
time(&tloc); SystemTimeToFileTime( &system_time, &file_time );
tp = localtime(&tloc); time = ((uint64_t)file_time.dwLowDateTime ) ;
ap = asctime(tp); time += ((uint64_t)file_time.dwHighDateTime) << 32;
(void) sprintf(tbuf, "%.20s", ap);
(void) strcat(tbuf, ap + 19); tp->tv_sec = (long) ((time - EPOCH) / 10000000L);
i = strlen(tbuf); tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
tbuf[i - 1] = '\0'; return 0;
return (tbuf); }
#endif
#else #endif
#endif
return ("today");
#include "misc_time.h"
#endif
} #ifdef USE_OMP
#include <omp.h>
/* return time interval in seconds and milliseconds */ #endif
PerfTime timebegin; /* Return the date. Return value is static data. */
void timediff(PerfTime *now, PerfTime *begin, int *sec, int *msec) char *
{ datestring(void)
{
*msec = (int) now->milliseconds - (int) begin->milliseconds;
*sec = (int) now->seconds - (int) begin->seconds; #ifdef HAVE_LOCALTIME
if (*msec < 0) { static char tbuf[45];
*msec += 1000; struct tm *tp;
(*sec)--; char *ap;
} size_t i;
return;
time_t tloc;
} time(&tloc);
tp = localtime(&tloc);
/* ap = asctime(tp);
* How many seconds have elapsed in running time. (void) sprintf(tbuf, "%.20s", ap);
* This is the routine called in IFseconds (void) strcat(tbuf, ap + 19);
*/ i = strlen(tbuf);
tbuf[i - 1] = '\0';
double return (tbuf);
seconds(void)
{ #else
#ifdef USE_OMP
// Usage of OpenMP time function return ("today");
return omp_get_wtime();
#elif defined(HAVE_QUERYPERFORMANCECOUNTER) #endif
// Windows (MSC and mingw) specific implementation }
LARGE_INTEGER frequency, counter;
QueryPerformanceFrequency(&frequency); /* return time interval in seconds and milliseconds */
QueryPerformanceCounter(&counter);
return (double)counter.QuadPart / frequency.QuadPart; PerfTime timebegin;
#elif defined(HAVE_CLOCK_GETTIME)
struct timespec ts; void timediff(PerfTime *now, PerfTime *begin, int *sec, int *msec)
clock_gettime(CLOCK_MONOTONIC, &ts); {
return ts.tv_sec + ts.tv_nsec / 1e9;
#elif defined(HAVE_GETTIMEOFDAY) *msec = (int) now->milliseconds - (int) begin->milliseconds;
// Usage of gettimeofday *sec = (int) now->seconds - (int) begin->seconds;
struct timeval tv; if (*msec < 0) {
gettimeofday(&tv, NULL); *msec += 1000;
return tv.tv_sec + tv.tv_usec / 1e6; (*sec)--;
#elif defined(HAVE_TIMES) }
// Usage of times return;
struct tms t;
clock_t ticks = times(&t); }
return (double)ticks / sysconf(_SC_CLK_TCK);
#elif defined(HAVE_GETRUSAGE) /*
// Usage of getrusage * How many seconds have elapsed in running time.
struct rusage usage; * This is the routine called in IFseconds
getrusage(RUSAGE_SELF, &usage); */
return usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1e6;
#elif defined(HAVE_FTIME) double
// Usage of ftime seconds(void)
struct timeb tb; {
ftime(&tb); #ifdef USE_OMP
return tb.time + tb.millitm / 1000.0; // Usage of OpenMP time function
#else return omp_get_wtime();
#error "No timer function available." #elif defined(HAVE_QUERYPERFORMANCECOUNTER)
#endif // Windows (MSC and mingw) specific implementation
} LARGE_INTEGER frequency, counter;
QueryPerformanceFrequency(&frequency);
void perf_timer_start(PerfTimer *timer) QueryPerformanceCounter(&counter);
{ return (double)counter.QuadPart / frequency.QuadPart;
timer->start = seconds(); #elif defined(HAVE_CLOCK_GETTIME)
} struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
void perf_timer_stop(PerfTimer *timer) return ts.tv_sec + ts.tv_nsec / 1e9;
{ #elif defined(HAVE_GETTIMEOFDAY)
timer->end = seconds(); // Usage of gettimeofday
} struct timeval tv;
gettimeofday(&tv, NULL);
void perf_timer_elapsed_sec_ms(const PerfTimer *timer, int *seconds, int *milliseconds) return tv.tv_sec + tv.tv_usec / 1e6;
{ #elif defined(HAVE_TIMES)
double elapsed = timer->end - timer->start; // Usage of times
*seconds = (int)elapsed; struct tms t;
*milliseconds = (int)((elapsed - *seconds) * 1000.0); clock_t ticks = times(&t);
} return (double)ticks / sysconf(_SC_CLK_TCK);
#elif defined(HAVE_GETRUSAGE)
void perf_timer_get_time(PerfTime *time) // Usage of getrusage
{ struct rusage usage;
double secs = seconds(); getrusage(RUSAGE_SELF, &usage);
time->seconds = (int)secs; return usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1e6;
time->milliseconds = (int)((secs - time->seconds) * 1000.0); #elif defined(HAVE_FTIME)
// Usage of ftime
} struct timeb tb;
ftime(&tb);
return tb.time + tb.millitm / 1000.0;
#else
#error "No timer function available."
#endif
}
void perf_timer_start(PerfTimer *timer)
{
timer->start = seconds();
}
void perf_timer_stop(PerfTimer *timer)
{
timer->end = seconds();
}
void perf_timer_elapsed_sec_ms(const PerfTimer *timer, int *seconds, int *milliseconds)
{
double elapsed = timer->end - timer->start;
*seconds = (int)elapsed;
*milliseconds = (int)((elapsed - *seconds) * 1000.0);
}
void perf_timer_get_time(PerfTime *time)
{
double secs = seconds();
time->seconds = (int)secs;
time->milliseconds = (int)((secs - time->seconds) * 1000.0);
}

View File

@ -1,32 +0,0 @@
#define WIN32_LEAN_AND_MEAN
//#include "ngspice/ngspice.h"
#include <Windows.h>
#include <winsock2.h>
#include <stdint.h> // portable: uint64_t MSVC: __int64
/*/ MSVC defines this in winsock2.h!?
typedef struct timeval {
long tv_sec;
long tv_usec;
} timeval;
*/
int gettimeofday(struct timeval * tp, void * unused)
{
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;
GetSystemTime( &system_time );
SystemTimeToFileTime( &system_time, &file_time );
time = ((uint64_t)file_time.dwLowDateTime ) ;
time += ((uint64_t)file_time.dwHighDateTime) << 32;
tp->tv_sec = (long) ((time - EPOCH) / 10000000L);
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
return 0;
}

View File

@ -1466,7 +1466,6 @@ lib /machine:x64 /def:..\..\fftw-3.3-dll64\libfftw3-3.def /out:$(IntDir)libfftw3
<ClCompile Include="..\src\misc\string.c" /> <ClCompile Include="..\src\misc\string.c" />
<ClCompile Include="..\src\misc\tilde.c" /> <ClCompile Include="..\src\misc\tilde.c" />
<ClCompile Include="..\src\misc\util.c" /> <ClCompile Include="..\src\misc\util.c" />
<ClCompile Include="..\src\misc\win_time.c" />
<ClCompile Include="..\src\misc\wlist.c" /> <ClCompile Include="..\src\misc\wlist.c" />
<ClCompile Include="..\src\ngspice.c" /> <ClCompile Include="..\src\ngspice.c" />
<ClCompile Include="..\src\osdi\osdiacld.c" /> <ClCompile Include="..\src\osdi\osdiacld.c" />

View File

@ -1687,7 +1687,6 @@ lib /machine:x64 /def:..\..\fftw-3.3-dll64\libfftw3-3.def /out:$(IntDir)libfftw3
<ClCompile Include="..\src\misc\string.c" /> <ClCompile Include="..\src\misc\string.c" />
<ClCompile Include="..\src\misc\tilde.c" /> <ClCompile Include="..\src\misc\tilde.c" />
<ClCompile Include="..\src\misc\util.c" /> <ClCompile Include="..\src\misc\util.c" />
<ClCompile Include="..\src\misc\win_time.c" />
<ClCompile Include="..\src\misc\wlist.c" /> <ClCompile Include="..\src\misc\wlist.c" />
<ClCompile Include="..\src\ngspice.c" /> <ClCompile Include="..\src\ngspice.c" />
<ClCompile Include="..\src\osdi\osdiacld.c" /> <ClCompile Include="..\src\osdi\osdiacld.c" />

View File

@ -1702,7 +1702,6 @@
<ClCompile Include="..\src\misc\string.c" /> <ClCompile Include="..\src\misc\string.c" />
<ClCompile Include="..\src\misc\tilde.c" /> <ClCompile Include="..\src\misc\tilde.c" />
<ClCompile Include="..\src\misc\util.c" /> <ClCompile Include="..\src\misc\util.c" />
<ClCompile Include="..\src\misc\win_time.c" />
<ClCompile Include="..\src\misc\wlist.c" /> <ClCompile Include="..\src\misc\wlist.c" />
<ClCompile Include="..\src\ngspice.c" /> <ClCompile Include="..\src\ngspice.c" />
<ClCompile Include="..\src\osdi\osdiacld.c" /> <ClCompile Include="..\src\osdi\osdiacld.c" />