2 * $Logfile: /Freespace2/code/Io/Timer.cpp $
7 * Include file for timer stuff
10 * Revision 1.2 2002/05/07 03:16:46 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:09 root
17 * 4 5/26/99 3:19p Dave
18 * Fixed release build errors.
20 * 3 5/17/99 6:03p Dave
21 * Added new timing code. Put in code to allow camera speed zooming.
23 * 2 10/07/98 10:53a Dave
26 * 1 10/07/98 10:49a Dave
28 * 14 4/13/98 10:16a John
29 * Switched gettime back to timer_get_milliseconds, which is now thread
32 * 13 4/13/98 10:11a John
33 * Made timer functions thread safe. Made timer_init be called in all
36 * 12 4/13/98 8:09a John
37 * Made timer_get functions thread safe.
39 * 11 12/02/97 3:56p John
40 * fixed a bug with timestamp_until, created one with it rolling over.
42 * 10 9/11/97 7:12p Hoffoss
43 * Added more functionality to training sexp handling code.
45 * 9 8/29/97 4:49p Allender
46 * added mprintf to check for wrap problems with timestamp ticker
48 * 8 8/28/97 1:38p Allender
49 * from John: changes to timer func to detect early rollever
51 * 7 7/29/97 5:30p Lawrance
52 * move gettime() from keyboard module to timer module
54 * 6 7/16/97 4:42p Mike
55 * Make afterburner shake viewer, not ship.
56 * Shake for limited time.
57 * Add timestamp_until() function to timer library.
59 * 5 2/17/97 5:18p John
60 * Added a bunch of RCS headers to a bunch of old files that don't have
74 #include "alphacolors.h"
80 static longlong Timer_last_value, Timer_base;
81 static uint Timer_freq=0;
83 static int Timer_inited = 0;
85 static CRITICAL_SECTION Timer_lock;
94 DeleteCriticalSection( &Timer_lock );
101 if ( !Timer_inited ) {
106 QueryPerformanceFrequency(&tmp);
107 Assert( tmp.HighPart == 0 );
108 Timer_freq = tmp.LowPart;
110 QueryPerformanceCounter((LARGE_INTEGER *)&Timer_base);
111 QueryPerformanceCounter((LARGE_INTEGER *)&Timer_last_value);
113 InitializeCriticalSection(&Timer_lock);
122 // Fills Time_now with the ticks since program start
123 static void timer_get(LARGE_INTEGER * out)
128 EnterCriticalSection(&Timer_lock);
133 QueryPerformanceCounter((LARGE_INTEGER *)&time_tmp);
134 if ( time_tmp < Timer_last_value ) {
135 // The clock has rolled!
136 Timer_base = time_tmp;
137 mprintf(( "TIMER ROLLED!\n" ));
138 // Hack: I'm not accounting for the time before roll occured,
139 // since I'm not sure at what value this timer is going to roll at.
142 Time_now = time_tmp - Timer_base;
143 Timer_last_value = time_tmp;
145 out->QuadPart = Time_now;
147 LeaveCriticalSection(&Timer_lock);
151 fix timer_get_fixed_seconds()
158 LARGE_INTEGER temp_large;
161 Int3(); // Make sure you call timer_init before anything that uses timer functions!
165 timer_get(&temp_large);
167 // Timing in fixed point (16.16) seconds.
168 // Can be used for up to 1000 hours
169 _asm mov edx, temp_large.HighPart
170 _asm mov eax, temp_large.LowPart
172 _asm shld edx, eax, 16 ; Keep 32+11 bits
174 // edx:eax = number of 1.19Mhz pulses elapsed.
175 _asm mov ebx, Timer_freq
177 // Make sure we won't divide overflow. Make time wrap at about 9 hours
179 _asm sub edx, ebx ; subtract until negative...
180 _asm jns sub_again ; ...to prevent divide overflow...
181 _asm add edx, ebx ; ...then add in to get correct value.
183 //eax = fixed point seconds elapsed...
191 fix timer_get_fixed_secondsX()
193 return timer_get_fixed_seconds();
196 fix timer_get_approx_seconds()
198 return timer_get_fixed_seconds();
201 int timer_get_milliseconds()
208 LARGE_INTEGER temp_large;
211 Int3(); // Make sure you call timer_init before anything that uses timer functions!
215 timer_get(&temp_large);
217 temp_large.QuadPart *= (longlong)1000;
219 // Timing in milliseconds.
220 _asm mov edx, temp_large.HighPart
221 _asm mov eax, temp_large.LowPart
223 //_asm shld edx, eax, 16 ; Keep 32+11 bits
225 // edx:eax = number of 1.19Mhz pulses elapsed.
226 _asm mov ebx, Timer_freq
228 // Make sure we won't divide overflow. Make time wrap at about 9 hours
230 _asm sub edx, ebx ; subtract until negative...
231 _asm jns sub_again ; ...to prevent divide overflow...
232 _asm add edx, ebx ; ...then add in to get correct value.
234 //eax = milliseconds elapsed...
242 int timer_get_microseconds()
249 LARGE_INTEGER temp_large;
252 Int3(); // Make sure you call timer_init before anything that uses timer functions!
256 timer_get(&temp_large);
258 temp_large.QuadPart *= (longlong)1000000;
260 // Timing in milliseconds.
261 _asm mov edx, temp_large.HighPart
262 _asm mov eax, temp_large.LowPart
264 //_asm shld edx, eax, 16 ; Keep 32+11 bits
266 // edx:eax = number of 1.19Mhz pulses elapsed.
267 _asm mov ebx, Timer_freq
269 // Make sure we won't divide overflow. Make time wrap at about 9 hours
271 _asm sub edx, ebx ; subtract until negative...
272 _asm jns sub_again ; ...to prevent divide overflow...
273 _asm add edx, ebx ; ...then add in to get correct value.
275 //eax = milliseconds elapsed...
284 // 1 means always return true
285 // 2 and above actually check the time
286 int timestamp_ticker = 2;
288 void timestamp_reset()
290 timestamp_ticker = 2;
293 // Restrict all time values between 0 and MAX_TIME
294 // so we don't have to use UINTs to calculate rollover.
295 // For debugging & testing, you could set this to
296 // something like 1 minute (6000).
297 #define MAX_TIME (INT_MAX/2)
299 void timestamp_inc(float frametime)
301 timestamp_ticker += (int)(frametime*TIMESTAMP_FREQUENCY);
303 if ( timestamp_ticker > MAX_TIME ) {
304 timestamp_ticker = 2; // Roll!
307 if (timestamp_ticker < 2 ) {
308 mprintf(("Whoa!!! timestamp_ticker < 2 -- resetting to 2!!!\n"));
309 timestamp_ticker = 2;
313 int timestamp(int delta_ms )
316 if (delta_ms < 0 ) return 0;
317 if (delta_ms == 0 ) return 1;
318 t2 = timestamp_ticker + delta_ms;
319 if ( t2 > MAX_TIME ) {
321 t2 = delta_ms - (MAX_TIME-timestamp_ticker);
323 if (t2 < 2 ) t2 = 2; // hack??
327 // Returns milliseconds until timestamp will elapse.
328 // Negative value gives milliseconds ago that timestamp elapsed.
329 int timestamp_until(int stamp)
332 // HACK!! This doesn't handle rollover!
333 // (Will it ever happen?)
335 return stamp - timestamp_ticker;
340 delta = stamp - timestamp_ticker;
343 if (delta > UINT_MAX/2)
344 delta = UINT_MAX - delta + 1;
345 else if (delta < - ( (int) (UINT_MAX/2)))
346 delta = UINT_MAX + delta + 1;
352 // alternate timestamp functions. The way these work is you call xtimestamp() to get the
353 // current counter value, and then call
356 return timestamp_ticker;
359 int timestamp_has_time_elapsed(int stamp, int time)
367 if (t <= timestamp_ticker)
368 return 1; // if we are unlucky enough to have it wrap on us, this will assume time has elapsed.
373 // timing functions -------------------------------------------------------------------------------
375 #define MAX_TIMING_EVENTS 15
380 typedef struct timing {
382 int microseconds_total;
388 timing Timing_events[MAX_TIMING_EVENTS];
389 int Timing_event_count = 0;
393 // lookup a timing event
394 int timing_event_lookup(char *event_name)
402 if(event_name == NULL){
406 // look through all events
407 for(idx=0; idx<MAX_TIMING_EVENTS; idx++){
408 if(!stricmp(Timing_events[idx].name, event_name)){
417 // start timing frame stuff
418 void timing_frame_start()
426 Timing_event_count = 0;
427 Timing_frame.start = timer_get_microseconds();
428 for(idx=0; idx<MAX_TIMING_EVENTS; idx++){
429 Timing_events[idx].microseconds_total = 0;
430 strcpy(Timing_events[idx].name, "");
431 Timing_events[idx].ref_count = 0;
436 // done timing the frame
437 void timing_frame_stop()
445 stop_time = timer_get_microseconds();
448 Timing_frame.microseconds_total = stop_time - Timing_frame.start;
452 // get the total frame time in microseconds
453 int timing_frame_total()
458 return Timing_frame.microseconds_total;
462 // time an individual event
463 void timing_event_start(char *event_name)
471 if(event_name == NULL){
475 // try and find the event
476 event = timing_event_lookup(event_name);
478 // if we already have one
480 Assert(Timing_events[event].ref_count == 0);
481 Timing_events[event].start = timer_get_microseconds();
482 Timing_events[event].ref_count++;
484 // if we need to add a new one
486 if(Timing_event_count < MAX_TIMING_EVENTS){
487 strcpy(Timing_events[Timing_event_count].name, event_name);
488 Timing_events[Timing_event_count].start = timer_get_microseconds();
489 Timing_events[Timing_event_count++].ref_count++;
495 // stop timing an event
496 void timing_event_stop(char *event_name)
504 if(event_name == NULL){
508 // try and find the event
509 event = timing_event_lookup(event_name);
511 // if we already have one
513 Assert(Timing_events[event].ref_count == 1);
514 Timing_events[event].microseconds_total += timer_get_microseconds() - Timing_events[event].start;
515 Timing_events[event].ref_count--;
520 // get the total time for an event in microseconds
521 int timing_event_total(char *event_name)
529 if(event_name == NULL){
533 // try and find the event
534 event = timing_event_lookup(event_name);
539 return Timing_events[event].microseconds_total;
543 // get the percentage of total frametime for the event (0.0 to 1.0)
544 float timing_event_pct(char *event_name)
552 if(event_name == NULL){
556 // try and find the event
557 event = timing_event_lookup(event_name);
562 return (float)((double)Timing_events[event].microseconds_total / (double)Timing_frame.microseconds_total);
567 void timing_display(int x, int y)
574 gr_set_color_fast(&Color_bright_blue);
577 gr_printf(x, y, "Total time %f\n", (float)timing_frame_total() / 1000000.0f);
580 // each event percentage
581 for(idx=0; idx<Timing_event_count; idx++){
582 gr_printf(x, y, "%s : %f\n", Timing_events[idx].name, timing_event_pct(Timing_events[idx].name));