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);
74 ssize_t written = write(1, text, strlen(text));
76 break; // sorry, I cannot do anything about this error - without an output
80 fcntl (1, F_SETFL, origflags);
82 //fprintf(stdout, "%s", text);
85 double Sys_DoubleTime (void)
87 static int first = true;
88 static double oldtime = 0.0, curtime = 0.0;
90 if(sys_usenoclockbutbenchmark.integer)
93 return ((double) benchmark_time) / 1e6;
96 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
97 if (sys_usetimegettime.integer)
99 static int firsttimegettime = true;
102 // Windows 95/98/ME/NT/2000/XP
104 // reasonable accuracy (millisecond)
106 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
108 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
109 if (firsttimegettime)
112 firsttimegettime = false;
115 newtime = (double) timeGetTime () / 1000.0;
119 // QueryPerformanceCounter
121 // Windows 95/98/ME/NT/2000/XP
123 // very accurate (CPU cycles)
125 // does not necessarily match realtime too well (tends to get faster and faster in win98)
126 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
128 LARGE_INTEGER PerformanceFreq;
129 LARGE_INTEGER PerformanceCount;
131 if (!QueryPerformanceFrequency (&PerformanceFreq))
133 Con_Printf ("No hardware timer available\n");
134 // fall back to timeGetTime
135 Cvar_SetValueQuick(&sys_usetimegettime, true);
136 return Sys_DoubleTime();
138 QueryPerformanceCounter (&PerformanceCount);
141 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
142 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
144 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
145 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
150 if (sys_useclockgettime.integer)
154 clock_gettime(CLOCK_HIGHRES, &ts);
156 clock_gettime(CLOCK_MONOTONIC, &ts);
158 newtime = (double) ts.tv_sec + ts.tv_nsec / 1000000000.0;
164 gettimeofday(&tp, NULL);
165 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
175 if (newtime < oldtime)
177 // warn if it's significant
178 if (newtime - oldtime < -0.01)
179 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
181 else if (newtime > oldtime + 1800)
183 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
186 curtime += newtime - oldtime;
192 char *Sys_ConsoleInput(void)
194 //if (cls.state == ca_dedicated)
196 static char text[MAX_INPUTLINE];
197 static unsigned int len = 0;
223 if (len < sizeof (text) - 1)
232 struct timeval timeout;
234 FD_SET(0, &fdset); // stdin
237 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
239 len = read (0, text, sizeof(text) - 1);
242 // rip off the \n and terminate
243 // div0: WHY? console code can deal with \n just fine
244 // this caused problems with pasting stuff into a terminal window
245 // so, not ripping off the \n, but STILL keeping a NUL terminator
255 void Sys_Sleep(int microseconds)
257 if(sys_usenoclockbutbenchmark.integer)
259 benchmark_time += microseconds;
263 Sleep(microseconds / 1000);
265 usleep(microseconds);
269 char *Sys_GetClipboardData (void)
274 void Sys_InitConsole (void)
278 void Sys_Init_Commands (void)
280 Cvar_RegisterVariable(&sys_usenoclockbutbenchmark);
282 Cvar_RegisterVariable(&sys_usetimegettime);
285 Cvar_RegisterVariable(&sys_useclockgettime);
290 int main (int argc, char **argv)
292 signal(SIGFPE, SIG_IGN);
295 com_argv = (const char **)argv;
298 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);