cvar_t host_framerate = {CF_CLIENT | CF_SERVER, "host_framerate","0", "locks frame timing to this value in seconds, 0.05 is 20fps for example, note that this can easily run too fast, use cl_maxfps if you want to limit your framerate instead, or sys_ticrate to limit server speed"};
// shows time used by certain subsystems
cvar_t host_speeds = {CF_CLIENT | CF_SERVER, "host_speeds","0", "reports how much time is used in server/graphics/sound"};
-cvar_t host_maxwait = {CF_CLIENT | CF_SERVER, "host_maxwait","1000", "maximum sleep time requested from the operating system in millisecond. Larger sleeps will be done using multiple host_maxwait length sleeps. Lowering this value will increase CPU load, but may help working around problems with accuracy of sleep times."};
cvar_t developer = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "developer","0", "shows debugging messages and information (recommended for all developers and level designers); the value -1 also suppresses buffering and logging these messages"};
cvar_t developer_extra = {CF_CLIENT | CF_SERVER, "developer_extra", "0", "prints additional debugging messages, often very verbose!"};
Cvar_RegisterVariable (&host_framerate);
Cvar_RegisterCallback (&host_framerate, Host_Framerate_c);
Cvar_RegisterVariable (&host_speeds);
- Cvar_RegisterVariable (&host_maxwait);
Cvar_RegisterVariable (&host_isclient);
Cvar_RegisterVariable (&developer);
#endif
}
+
+
#ifdef WIN32
# define HAVE_TIMEGETTIME 1
# define HAVE_QUERYPERFORMANCECOUNTER 1
# define HAVE_WIN32_USLEEP 1
# define HAVE_Sleep 1
-#endif
-
-#ifndef WIN32
-#if defined(CLOCK_MONOTONIC) || defined(CLOCK_HIRES)
-# define HAVE_CLOCKGETTIME 1
-#endif
-// FIXME improve this check, manpage hints to DST_NONE
-# define HAVE_GETTIMEOFDAY 1
+#else
+# if defined(CLOCK_MONOTONIC) || defined(CLOCK_HIRES)
+# define HAVE_CLOCKGETTIME 1
+# endif
+# if _POSIX_VERSION >= 200112L
+# define HAVE_CLOCK_NANOSLEEP 1
+# endif
#endif
#ifdef FD_SET
# define HAVE_SELECT 1
#endif
-#ifndef WIN32
-// FIXME improve this check
-# define HAVE_USLEEP 1
-#endif
-
// these are referenced elsewhere
cvar_t sys_usenoclockbutbenchmark = {CF_SHARED, "sys_usenoclockbutbenchmark", "0", "don't use ANY real timing, and simulate a clock (for benchmarking); the game then runs as fast as possible. Run a QC mod with bots that does some stuff, then does a quit at the end, to benchmark a server. NEVER do this on a public server."};
cvar_t sys_libdir = {CF_READONLY | CF_SHARED, "sys_libdir", "", "Default engine library directory"};
#if HAVE_QUERYPERFORMANCECOUNTER
static cvar_t sys_usequeryperformancecounter = {CF_SHARED | CF_ARCHIVE, "sys_usequeryperformancecounter", "1", "use windows QueryPerformanceCounter timer (which has issues on systems lacking constant-rate TSCs synchronised across all cores, such as ancient PCs or VMs) for timing rather than timeGetTime function (which is low precision and had issues on some old motherboards)"};
#endif
-#if HAVE_CLOCKGETTIME
-static cvar_t sys_useclockgettime = {CF_SHARED | CF_ARCHIVE, "sys_useclockgettime", "1", "use POSIX clock_gettime function (not adjusted by NTP on some older Linux kernels) for timing rather than gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations)"};
-#endif
static cvar_t sys_stdout = {CF_SHARED, "sys_stdout", "1", "0: nothing is written to stdout (-nostdout cmdline option sets this), 1: normal messages are written to stdout, 2: normal messages are written to stderr (-stderr cmdline option sets this)"};
#ifndef WIN32
Cvar_RegisterVariable(&sys_debugsleep);
Cvar_RegisterVariable(&sys_usenoclockbutbenchmark);
Cvar_RegisterVariable(&sys_libdir);
-#if HAVE_TIMEGETTIME || HAVE_QUERYPERFORMANCECOUNTER || HAVE_CLOCKGETTIME || HAVE_GETTIMEOFDAY
+#if HAVE_TIMEGETTIME || HAVE_QUERYPERFORMANCECOUNTER || HAVE_CLOCKGETTIME
if(sys_supportsdlgetticks)
{
Cvar_RegisterVariable(&sys_usesdlgetticks);
#if HAVE_QUERYPERFORMANCECOUNTER
Cvar_RegisterVariable(&sys_usequeryperformancecounter);
#endif
-#if HAVE_CLOCKGETTIME
- Cvar_RegisterVariable(&sys_useclockgettime);
-#endif
+
Cvar_RegisterVariable(&sys_stdout);
Cvar_RegisterCallback(&sys_stdout, Sys_UpdateOutFD_c);
#ifndef WIN32
Sys_Error("sys_usenoclockbutbenchmark cannot run any longer, sorry");
return benchmark_time * 0.000001;
}
+
+ if(sys_supportsdlgetticks && sys_usesdlgetticks.integer)
+ return (double) Sys_SDL_GetTicks() / 1000.0;
+
#if HAVE_QUERYPERFORMANCECOUNTER
if (sys_usequeryperformancecounter.integer)
{
#endif
#if HAVE_CLOCKGETTIME
- if (sys_useclockgettime.integer)
{
struct timespec ts;
-# ifdef CLOCK_MONOTONIC
- // linux
+# ifdef CLOCK_MONOTONIC_RAW
+ // Linux-specific, SDL_GetPerformanceCounter() uses it
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+# elif defined(CLOCK_MONOTONIC)
+ // POSIX
clock_gettime(CLOCK_MONOTONIC, &ts);
# else
// sunos
#endif
// now all the FALLBACK timers
- if(sys_supportsdlgetticks && sys_usesdlgetticks.integer)
- return (double) Sys_SDL_GetTicks() / 1000.0;
-#if HAVE_GETTIMEOFDAY
- {
- struct timeval tp;
- gettimeofday(&tp, NULL);
- return (double) tp.tv_sec + tp.tv_usec / 1000000.0;
- }
-#elif HAVE_TIMEGETTIME
+#if HAVE_TIMEGETTIME
{
// timeGetTime
// platform:
#endif
}
-extern cvar_t host_maxwait;
double Sys_Sleep(double time)
{
double dt;
- uint32_t microseconds;
-
- // convert to microseconds
- time *= 1000000.0;
-
- if(host_maxwait.value <= 0)
- time = min(time, 1000000.0);
- else
- time = min(time, host_maxwait.value * 1000.0);
+ uint32_t msec, usec, nsec;
- if (time < 1 || host.restless)
+ if (time < 1.0/1000000.0 || host.restless)
return 0; // not sleeping this frame
-
- microseconds = time; // post-validation to prevent overflow
+ if (time >= 1)
+ time = 0.999999; // ensure passed values are in range
+ msec = time * 1000;
+ usec = time * 1000000;
+ nsec = time * 1000000000;
if(sys_usenoclockbutbenchmark.integer)
{
double old_benchmark_time = benchmark_time;
- benchmark_time += microseconds;
+ benchmark_time += usec;
if(benchmark_time == old_benchmark_time)
Sys_Error("sys_usenoclockbutbenchmark cannot run any longer, sorry");
return 0;
}
if(sys_debugsleep.integer)
- Con_Printf("sys_debugsleep: requesting %u ", microseconds);
+ Con_Printf("sys_debugsleep: requesting %u ", usec);
dt = Sys_DirtyTime();
// less important on newer libcurl so no need to disturb dedicated servers
- if (cls.state != ca_dedicated && Curl_Select(microseconds))
+ if (cls.state != ca_dedicated && Curl_Select(msec))
{
// a transfer is ready or we finished sleeping
}
else if(sys_supportsdlgetticks && sys_usesdldelay.integer)
- Sys_SDL_Delay(microseconds / 1000);
+ Sys_SDL_Delay(msec);
#if HAVE_SELECT
else if (cls.state == ca_dedicated && sv_checkforpacketsduringsleep.integer)
{
#endif
}
}
- tv.tv_sec = microseconds / 1000000;
- tv.tv_usec = microseconds % 1000000;
+ tv.tv_sec = 0;
+ tv.tv_usec = usec;
// on Win32, select() cannot be used with all three FD list args being NULL according to MSDN
// (so much for POSIX...), not with an empty fd_set either.
select(lastfd + 1, &fdreadset, NULL, NULL, &tv);
}
#endif
-#if HAVE_USLEEP
+#if HAVE_CLOCK_NANOSLEEP
else
- usleep(microseconds);
+ {
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = nsec;
+ clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
+ }
#elif HAVE_WIN32_USLEEP // Windows XP/2003 minimum
else
{
LARGE_INTEGER sleeptime;
// takes 100ns units, negative indicates relative time
- sleeptime.QuadPart = -(10 * (int64_t)microseconds);
+ sleeptime.QuadPart = -((int64_t)nsec / 100);
timer = CreateWaitableTimer(NULL, true, NULL);
SetWaitableTimer(timer, &sleeptime, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
}
#elif HAVE_Sleep
else
- Sleep(microseconds / 1000);
+ Sleep(msec);
#else
else
- Sys_SDL_Delay(microseconds / 1000);
+ Sys_SDL_Delay(msec);
#endif
dt = Sys_DirtyTime() - dt;
if(sys_debugsleep.integer)
- Con_Printf(" got %u oversleep %d\n", (unsigned int)(dt * 1000000), (unsigned int)(dt * 1000000) - microseconds);
+ Con_Printf(" got %u oversleep %d\n", (unsigned int)(dt * 1000000), (unsigned int)(dt * 1000000) - usec);
return (dt < 0 || dt >= 1800) ? 0 : dt;
}