/**************************************************************************** ** ** This file is part of GAP, a system for computational discrete algebra. ** ** Copyright of GAP belongs to its developers, whose names are too numerous ** to list here. Please refer to the COPYRIGHT file for details. ** ** SPDX-License-Identifier: GPL-2.0-or-later **
*/
/**************************************************************************** ** *F SyTime() . . . . . . . . . . . . . . . return time spent in milliseconds ** ** 'SyTime' returns the number of milliseconds spent by GAP so far. ** ** Should be as accurate as possible, because it is used for profiling.
*/
UInt SyTime(void)
{ #ifdef EMSCRIPTEN // Emscripten's standard library always returns the same values // for getrusage, which makes some GAP functions enter an // infinite loop, as they use this function to try running // for some period of time. Use NanosecondsSinceEpoch() as // a substitute (it is not perfect, as NanosecondsSinceEpoch() // is walltime, while RUSAGE_SELF is CPU time). return SyNanosecondsSinceEpoch()/1000000000; #else struct rusage buf;
if (getrusage(RUSAGE_SELF, &buf)) {
ErrorMayQuit("SyTime: could not get time: " "%s (errno %d)",
(Int)strerror(errno), (Int)errno);
} return buf.ru_utime.tv_sec * 1000 + buf.ru_utime.tv_usec / 1000; #endif
}
/**************************************************************************** ** *F SyNanosecondsSinceEpoch() ** ** 'SyNanosecondsSinceEpoch' returns a 64-bit integer which represents the ** number of nanoseconds since some unspecified starting point. This means ** that the number returned by this function is not in itself meaningful, ** but the difference between the values returned by two consecutive calls ** can be used to measure wallclock time. ** ** The accuracy of this is system dependent. For systems that implement ** clock_getres, we could get the promised accuracy. ** ** Note that gettimeofday has been marked obsolete in the POSIX standard. ** We are using it because it is implemented in most systems still. ** ** If we are using gettimeofday we cannot guarantee the values that ** are returned by SyNanosecondsSinceEpoch to be monotonic. ** ** Returns -1 to represent failure **
*/
Int8 SyNanosecondsSinceEpoch(void)
{
Int8 res;
#ifdefined(__APPLE__) && defined(__MACH__) // macOS static mach_timebase_info_data_t timeinfo; if (timeinfo.denom == 0) {
(void)mach_timebase_info(&timeinfo);
}
res = mach_absolute_time();
res *= timeinfo.numer;
res /= timeinfo.denom; #elifdefined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
res = ts.tv_sec;
res *= 1000000000L;
res += ts.tv_nsec;
} else {
res = -1;
} #elifdefined(HAVE_GETTIMEOFDAY) struct timeval tv;
if (gettimeofday(&tv, NULL) == 0) {
res = tv.tv_sec;
res *= 1000000L;
res += tv.tv_usec;
res *= 1000;
} else {
res = -1;
}; #else
res = -1; #endif
return res;
}
/**************************************************************************** ** *F SyNanosecondsSinceEpochResolution() ** ** 'SyNanosecondsSinceEpochResolution' returns a 64-bit integer which ** represents the resolution in nanoseconds of the timer used for ** SyNanosecondsSinceEpoch. ** ** If the return value is positive then the value has been returned ** by the operating system can probably be relied on. If the ** return value is negative it is just an estimate (as in the case ** of gettimeofday we have no way to get the exact resolution so we ** just pretend that the resolution is 1000 nanoseconds). ** ** A result of 0 signifies inability to obtain any sensible value.
*/ static Int8 SyNanosecondsSinceEpochResolution(void)
{
Int8 res;
#ifdefined(__APPLE__) && defined(__MACH__) static mach_timebase_info_data_t timeinfo; if (timeinfo.denom == 0) {
(void)mach_timebase_info(&timeinfo);
}
res = timeinfo.numer;
res /= timeinfo.denom; #elifdefined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) struct timespec ts;
if (clock_getres(CLOCK_MONOTONIC, &ts) == 0) {
res = ts.tv_sec;
res *= 1000000000L;
res += ts.tv_nsec;
} else {
res = 0;
} #elifdefined(HAVE_GETTIMEOFDAY)
res = -1000; #else
res = 0; #endif
return res;
}
/**************************************************************************** ** *F FuncRuntime( <self> ) . . . . . . . . . . . . internal function 'Runtime' ** ** 'FuncRuntime' implements the internal function 'Runtime'. ** ** 'Runtime()' ** ** 'Runtime' returns the time spent since the start of GAP in milliseconds. ** How much time execution of statements take is of course system dependent. ** The accuracy of this number is also system dependent.
*/ static Obj FuncRuntime(Obj self)
{ return ObjInt_UInt(SyTime());
}
/**************************************************************************** ** *F FuncNanosecondsSinceEpoch( <self> ) ** ** 'FuncNanosecondsSinceEpoch' returns an integer which represents the ** number of nanoseconds since some unspecified starting point. This ** function wraps SyNanosecondsSinceEpoch.
*/ static Obj FuncNanosecondsSinceEpoch(Obj self)
{
Int8 val = SyNanosecondsSinceEpoch(); return val >= 0 ? ObjInt_Int8(val) : Fail;
}
/**************************************************************************** ** *F FuncNanosecondsSinceEpochInfo( <self> ) ** ** 'FuncNanosecondsSinceEpochInformation' returns a plain record which ** contains information about the timers used for FuncNanosecondsSinceEpoch. **
*/ static Obj FuncNanosecondsSinceEpochInfo(Obj self)
{
Obj res, tmp;
Int8 resolution; constchar * method = "unsupported"; Int monotonic = 0;
// either we used up the time, or we were interrupted. if (HaveInterrupt()) {
ClearError(); // The interrupt may still be pending
ErrorReturnVoid("user interrupt in sleep", 0, 0, "you can 'return;' as if the sleep was finished");
}
// either we used up the time, or we were interrupted. if (HaveInterrupt()) {
ClearError(); // The interrupt may still be pending
ErrorReturnVoid( "user interrupt in microsleep", 0, 0, "you can 'return;' as if the microsleep was finished");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.