16 cvar_t sys_usenoclockbutbenchmark = {CVAR_SAVE, "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."};
17 static unsigned long benchmark_time;
20 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1", "use windows timeGetTime function (which has issues on some motherboards) for timing rather than QueryPerformanceCounter timer (which has issues on multicore/multiprocessor machines and processors which are designed to conserve power)"};
23 cvar_t sys_useclockgettime = {CVAR_SAVE, "sys_useclockgettime", "0", "use POSIX clock_gettime function (which has issues if the system clock speed is far off, as it can't get fixed by NTP) for timing rather than gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations)"};
29 // =======================================================================
31 // =======================================================================
32 void Sys_Shutdown (void)
35 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
40 void Sys_Error (const char *error, ...)
43 char string[MAX_INPUTLINE];
45 // change stdin to non blocking
47 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
50 va_start (argptr,error);
51 dpvsnprintf (string, sizeof (string), error, argptr);
54 Con_Printf ("Quake Error: %s\n", string);
60 void Sys_PrintToTerminal(const char *text)
63 // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
64 int origflags = fcntl (1, F_GETFL, 0);
65 fcntl (1, F_SETFL, origflags & ~FNDELAY);
71 ssize_t written = write(1, text, strlen(text));
73 break; // sorry, I cannot do anything about this error - without an output
77 fcntl (1, F_SETFL, origflags);
79 //fprintf(stdout, "%s", text);
82 double Sys_DoubleTime (void)
84 static int first = true;
85 static double oldtime = 0.0, curtime = 0.0;
87 if(sys_usenoclockbutbenchmark.integer)
90 return ((double) benchmark_time) / 1e6;
94 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
95 if (sys_usetimegettime.integer)
97 static int firsttimegettime = true;
100 // Windows 95/98/ME/NT/2000/XP
102 // reasonable accuracy (millisecond)
104 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
106 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
107 if (firsttimegettime)
110 firsttimegettime = false;
113 newtime = (double) timeGetTime () / 1000.0;
117 // QueryPerformanceCounter
119 // Windows 95/98/ME/NT/2000/XP
121 // very accurate (CPU cycles)
123 // does not necessarily match realtime too well (tends to get faster and faster in win98)
124 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
126 LARGE_INTEGER PerformanceFreq;
127 LARGE_INTEGER PerformanceCount;
129 if (!QueryPerformanceFrequency (&PerformanceFreq))
131 Con_Printf ("No hardware timer available\n");
132 // fall back to timeGetTime
133 Cvar_SetValueQuick(&sys_usetimegettime, true);
134 return Sys_DoubleTime();
136 QueryPerformanceCounter (&PerformanceCount);
139 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
140 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
142 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
143 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
148 if (sys_useclockgettime.integer)
152 clock_gettime(CLOCK_HIGHRES, &ts);
154 clock_gettime(CLOCK_MONOTONIC, &ts);
156 newtime = (double) ts.tv_sec + ts.tv_nsec / 1000000000.0;
162 gettimeofday(&tp, NULL);
163 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
173 if (newtime < oldtime)
175 // warn if it's significant
176 if (newtime - oldtime < -0.01)
177 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
179 else if (newtime > oldtime + 1800)
181 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
184 curtime += newtime - oldtime;
190 char *Sys_ConsoleInput(void)
192 //if (cls.state == ca_dedicated)
194 static char text[MAX_INPUTLINE];
195 static unsigned int len = 0;
221 if (len < sizeof (text) - 1)
230 struct timeval timeout;
232 FD_SET(0, &fdset); // stdin
235 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
237 len = read (0, text, sizeof(text));
240 // rip off the \n and terminate
250 void Sys_Sleep(int microseconds)
252 if(sys_usenoclockbutbenchmark.integer)
254 benchmark_time += microseconds;
258 Sleep(microseconds / 1000);
260 usleep(microseconds);
264 char *Sys_GetClipboardData (void)
269 void Sys_InitConsole (void)
273 void Sys_Init_Commands (void)
275 Cvar_RegisterVariable(&sys_usenoclockbutbenchmark);
277 Cvar_RegisterVariable(&sys_usetimegettime);
280 Cvar_RegisterVariable(&sys_useclockgettime);
285 int main (int argc, char **argv)
287 signal(SIGFPE, SIG_IGN);
290 com_argv = (const char **)argv;
293 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);