7 # include <mmsystem.h> // timeGetTime
8 # include <time.h> // localtime
12 # include <sys/time.h>
19 static char sys_timestring[128];
20 char *Sys_TimeString(const char *timeformat)
22 time_t mytime = time(NULL);
25 localtime_s(&mytm, &mytime);
26 strftime(sys_timestring, sizeof(sys_timestring), timeformat, &mytm);
28 strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
30 return sys_timestring;
34 extern qboolean host_shuttingdown;
35 void Sys_Quit (int returnvalue)
37 if (COM_CheckParm("-profilegameonly"))
38 Sys_AllowProfiling(false);
39 host_shuttingdown = true;
44 #if defined(__linux__) || defined(__FreeBSD__)
51 void Sys_AllowProfiling(qboolean enable)
53 #if defined(__linux__) || defined(__FreeBSD__)
60 ===============================================================================
64 ===============================================================================
67 qboolean Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts)
70 const dllfunction_t *func;
71 dllhandle_t dllhandle = 0;
79 dllhandle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
82 for (func = fcts; func && func->name != NULL; func++)
83 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
88 Con_DPrintf ("All of %s's functions were already linked in! Not loading dynamically...\n", dllnames[0]);
97 for (func = fcts; func && func->name != NULL; func++)
98 *func->funcvariable = NULL;
100 // Try every possible name
101 Con_DPrintf ("Trying to load library...");
102 for (i = 0; dllnames[i] != NULL; i++)
104 Con_DPrintf (" \"%s\"", dllnames[i]);
106 dllhandle = LoadLibrary (dllnames[i]);
108 dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL);
114 // see if the names can be loaded relative to the executable path
115 // (this is for Mac OSX which does not check next to the executable)
116 if (!dllhandle && strrchr(com_argv[0], '/'))
118 char path[MAX_OSPATH];
119 strlcpy(path, com_argv[0], sizeof(path));
120 strrchr(path, '/')[1] = 0;
121 for (i = 0; dllnames[i] != NULL; i++)
123 char temp[MAX_OSPATH];
124 strlcpy(temp, path, sizeof(temp));
125 strlcat(temp, dllnames[i], sizeof(temp));
126 Con_DPrintf (" \"%s\"", temp);
128 dllhandle = LoadLibrary (temp);
130 dllhandle = dlopen (temp, RTLD_LAZY | RTLD_GLOBAL);
140 Con_DPrintf(" - failed.\n");
144 Con_DPrintf(" - loaded.\n");
146 // Get the function adresses
147 for (func = fcts; func && func->name != NULL; func++)
148 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
150 Con_DPrintf ("Missing function \"%s\" - broken library!\n", func->name);
151 Sys_UnloadLibrary (&dllhandle);
162 void Sys_UnloadLibrary (dllhandle_t* handle)
165 if (handle == NULL || *handle == NULL)
169 FreeLibrary (*handle);
178 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
182 return (void *)GetProcAddress (handle, name);
184 return (void *)dlsym (handle, name);
192 # define HAVE_TIMEGETTIME 1
193 # define HAVE_QUERYPERFORMANCECOUNTER 1
194 # define HAVE_Sleep 1
197 #if defined(CLOCK_MONOTONIC) || defined(CLOCK_HIRES)
198 # define HAVE_CLOCKGETTIME 1
202 // FIXME improve this check, manpage hints to DST_NONE
203 # define HAVE_GETTIMEOFDAY 1
207 # define HAVE_SELECT 1
211 // FIXME improve this check
212 # define HAVE_USLEEP 1
215 cvar_t sys_debugsleep = {0, "sys_debugsleep", "0", "write requested and attained sleep times to standard output, to be used with gnuplot"};
216 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."};
217 cvar_t sys_usesdlgetticks = {CVAR_SAVE, "sys_usesdlgetticks", "0", "use SDL_GetTicks() timer (less accurate, for debugging)"};
218 cvar_t sys_usesdldelay = {CVAR_SAVE, "sys_usesdldelay", "0", "use SDL_Delay() (less accurate, for debugging)"};
219 #if HAVE_QUERYPERFORMANCECOUNTER
220 cvar_t sys_usequeryperformancecounter = {CVAR_SAVE, "sys_usequeryperformancecounter", "0", "use windows QueryPerformanceCounter timer (which has issues on multicore/multiprocessor machines and processors which are designed to conserve power) for timing rather than timeGetTime function (which has issues on some motherboards)"};
222 #if HAVE_CLOCKGETTIME
223 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)"};
226 static unsigned long benchmark_time;
228 void Sys_Init_Commands (void)
230 Cvar_RegisterVariable(&sys_debugsleep);
231 Cvar_RegisterVariable(&sys_usenoclockbutbenchmark);
232 #if HAVE_TIMEGETTIME || HAVE_QUERYPERFORMANCECOUNTER || HAVE_CLOCKGETTIME || HAVE_GETTIMEOFDAY
233 if(sys_supportsdlgetticks)
235 Cvar_RegisterVariable(&sys_usesdlgetticks);
236 Cvar_RegisterVariable(&sys_usesdldelay);
239 #if HAVE_QUERYPERFORMANCECOUNTER
240 Cvar_RegisterVariable(&sys_usequeryperformancecounter);
242 #if HAVE_CLOCKGETTIME
243 Cvar_RegisterVariable(&sys_useclockgettime);
247 double Sys_DoubleTime(void)
249 static int first = true;
250 static double oldtime = 0.0, curtime = 0.0;
252 if(sys_usenoclockbutbenchmark.integer)
255 return ((double) benchmark_time) / 1e6;
258 // first all the OPTIONAL timers
260 #if HAVE_QUERYPERFORMANCECOUNTER
261 else if (sys_usequeryperformancecounter.integer)
263 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
264 // QueryPerformanceCounter
266 // Windows 95/98/ME/NT/2000/XP
268 // very accurate (CPU cycles)
270 // does not necessarily match realtime too well (tends to get faster and faster in win98)
271 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
273 LARGE_INTEGER PerformanceFreq;
274 LARGE_INTEGER PerformanceCount;
276 if (!QueryPerformanceFrequency (&PerformanceFreq))
278 Con_Printf ("No hardware timer available\n");
279 // fall back to timeGetTime
280 Cvar_SetValueQuick(&sys_usequeryperformancecounter, false);
281 return Sys_DoubleTime();
283 QueryPerformanceCounter (&PerformanceCount);
286 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
287 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
289 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
290 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
295 #if HAVE_CLOCKGETTIME
296 else if (sys_useclockgettime.integer)
299 # ifdef CLOCK_MONOTONIC
301 clock_gettime(CLOCK_MONOTONIC, &ts);
304 clock_gettime(CLOCK_HIGHRES, &ts);
306 newtime = (double) ts.tv_sec + ts.tv_nsec / 1000000000.0;
310 // now all the FALLBACK timers
311 else if(sys_supportsdlgetticks && sys_usesdlgetticks.integer)
313 newtime = (double) Sys_SDL_GetTicks() / 1000.0;
315 #if HAVE_GETTIMEOFDAY
319 gettimeofday(&tp, NULL);
320 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
322 #elif HAVE_TIMEGETTIME
325 static int firsttimegettime = true;
328 // Windows 95/98/ME/NT/2000/XP
330 // reasonable accuracy (millisecond)
332 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
334 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
335 if (firsttimegettime)
338 firsttimegettime = false;
341 newtime = (double) timeGetTime () / 1000.0;
344 // fallback for using the SDL timer if no other timer is available
347 newtime = (double) Sys_SDL_GetTicks() / 1000.0;
348 // this calls Sys_Error() if not linking against SDL
358 if (newtime < oldtime)
360 // warn if it's significant
361 if (newtime - oldtime < -0.01)
362 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
364 else if (newtime > oldtime + 1800)
366 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
369 curtime += newtime - oldtime;
375 void Sys_Sleep(int microseconds)
378 if(sys_usenoclockbutbenchmark.integer)
380 benchmark_time += microseconds;
383 if(sys_debugsleep.integer)
385 t = Sys_DoubleTime();
387 if(sys_supportsdlgetticks && sys_usesdldelay.integer)
389 Sys_SDL_Delay(microseconds / 1000);
395 tv.tv_sec = microseconds / 1000000;
396 tv.tv_usec = microseconds % 1000000;
397 select(0, NULL, NULL, NULL, &tv);
402 usleep(microseconds);
407 Sleep(microseconds / 1000);
412 Sys_SDL_Delay(microseconds / 1000);
415 if(sys_debugsleep.integer)
417 t = Sys_DoubleTime() - t;
418 printf("%d %d # debugsleep\n", microseconds, (unsigned int)(t * 1000000));