18 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."};
19 static unsigned long benchmark_time;
22 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)"};
25 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)"};
31 // =======================================================================
33 // =======================================================================
34 void Sys_Shutdown (void)
37 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
42 void Sys_Error (const char *error, ...)
45 char string[MAX_INPUTLINE];
47 // change stdin to non blocking
49 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
52 va_start (argptr,error);
53 dpvsnprintf (string, sizeof (string), error, argptr);
56 Con_Printf ("Quake Error: %s\n", string);
62 void Sys_PrintToTerminal(const char *text)
65 // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
66 int origflags = fcntl (1, F_GETFL, 0);
67 fcntl (1, F_SETFL, origflags & ~FNDELAY);
73 ssize_t written = write(1, text, strlen(text));
75 break; // sorry, I cannot do anything about this error - without an output
79 fcntl (1, F_SETFL, origflags);
81 //fprintf(stdout, "%s", text);
84 double Sys_DoubleTime (void)
86 static int first = true;
87 static double oldtime = 0.0, curtime = 0.0;
89 if(sys_usenoclockbutbenchmark.integer)
92 return ((double) benchmark_time) / 1e6;
95 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
96 if (sys_usetimegettime.integer)
98 static int firsttimegettime = true;
101 // Windows 95/98/ME/NT/2000/XP
103 // reasonable accuracy (millisecond)
105 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
107 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
108 if (firsttimegettime)
111 firsttimegettime = false;
114 newtime = (double) timeGetTime () / 1000.0;
118 // QueryPerformanceCounter
120 // Windows 95/98/ME/NT/2000/XP
122 // very accurate (CPU cycles)
124 // does not necessarily match realtime too well (tends to get faster and faster in win98)
125 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
127 LARGE_INTEGER PerformanceFreq;
128 LARGE_INTEGER PerformanceCount;
130 if (!QueryPerformanceFrequency (&PerformanceFreq))
132 Con_Printf ("No hardware timer available\n");
133 // fall back to timeGetTime
134 Cvar_SetValueQuick(&sys_usetimegettime, true);
135 return Sys_DoubleTime();
137 QueryPerformanceCounter (&PerformanceCount);
140 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
141 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
143 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
144 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
149 if (sys_useclockgettime.integer)
153 clock_gettime(CLOCK_HIGHRES, &ts);
155 clock_gettime(CLOCK_MONOTONIC, &ts);
157 newtime = (double) ts.tv_sec + ts.tv_nsec / 1000000000.0;
163 gettimeofday(&tp, NULL);
164 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
174 if (newtime < oldtime)
176 // warn if it's significant
177 if (newtime - oldtime < -0.01)
178 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
180 else if (newtime > oldtime + 1800)
182 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
185 curtime += newtime - oldtime;
191 char *Sys_ConsoleInput(void)
193 //if (cls.state == ca_dedicated)
195 static char text[MAX_INPUTLINE];
196 static unsigned int len = 0;
222 if (len < sizeof (text) - 1)
231 struct timeval timeout;
233 FD_SET(0, &fdset); // stdin
236 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
238 len = read (0, text, sizeof(text));
241 // rip off the \n and terminate
251 void Sys_Sleep(int microseconds)
253 if(sys_usenoclockbutbenchmark.integer)
255 benchmark_time += microseconds;
259 Sleep(microseconds / 1000);
261 usleep(microseconds);
265 char *Sys_GetClipboardData (void)
270 void Sys_InitConsole (void)
274 void Sys_Init_Commands (void)
276 Cvar_RegisterVariable(&sys_usenoclockbutbenchmark);
278 Cvar_RegisterVariable(&sys_usetimegettime);
281 Cvar_RegisterVariable(&sys_useclockgettime);
286 int main (int argc, char **argv)
288 signal(SIGFPE, SIG_IGN);
291 com_argv = (const char **)argv;
294 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);