diff --git a/panda/src/express/clockObject.I b/panda/src/express/clockObject.I index f6a57262c3..71289b93e0 100644 --- a/panda/src/express/clockObject.I +++ b/panda/src/express/clockObject.I @@ -69,24 +69,6 @@ get_frame_time() const { return _reported_frame_time; } -//////////////////////////////////////////////////////////////////// -// Function: ClockObject::get_intra_frame_time -// Access: Published -// Description: Returns the number of seconds that have elapsed since -// the start of the frame. This might be useful for -// certain specialty applications, for instance to -// measure how much time we've spent working on this one -// particular frame. -// -// This function returns an honest value whether the -// ClockObject is in normal, real-time mode or in -// non-real-time mode. -//////////////////////////////////////////////////////////////////// -INLINE double ClockObject:: -get_intra_frame_time() const { - return get_real_time() - _actual_frame_time; -} - //////////////////////////////////////////////////////////////////// // Function: ClockObject::get_real_time // Access: Published @@ -94,10 +76,35 @@ get_intra_frame_time() const { // the ClockObject was created, or since it was last // reset. This is useful for doing real timing // measurements, e.g. for performance statistics. +// +// This returns the most precise timer we have for short +// time intervals, but it may tend to drift over the +// long haul. If more accurate timekeeping is needed +// over a long period of time, use get_long_time() +// instead. //////////////////////////////////////////////////////////////////// INLINE double ClockObject:: get_real_time() const { - return (_true_clock->get_real_time() - _start_time); + return (_true_clock->get_short_time() - _start_short_time); +} + +//////////////////////////////////////////////////////////////////// +// Function: ClockObject::get_long_time +// Access: Published +// Description: Returns the actual number of seconds elapsed since +// the ClockObject was created, or since it was last +// reset. +// +// This is similar to get_real_time(), except that it +// uses the most accurate counter we have over a long +// period of time, and so it is less likely to drift. +// However, it may not be very precise for measuring +// short intervals. On Windows, for instace, this is +// only accurate to within about 55 milliseconds. +//////////////////////////////////////////////////////////////////// +INLINE double ClockObject:: +get_long_time() const { + return (_true_clock->get_long_time() - _start_long_time); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/express/clockObject.cxx b/panda/src/express/clockObject.cxx index e1f6e66614..a0ff1f8d80 100644 --- a/panda/src/express/clockObject.cxx +++ b/panda/src/express/clockObject.cxx @@ -32,7 +32,8 @@ ClockObject:: ClockObject() { _true_clock = TrueClock::get_ptr(); _mode = M_normal; - _start_time = _true_clock->get_real_time(); + _start_short_time = _true_clock->get_short_time(); + _start_long_time = _true_clock->get_long_time(); _frame_count = 0; _actual_frame_time = 0.0; _reported_frame_time = 0.0; @@ -59,7 +60,8 @@ set_real_time(double time) { << " seconds.\n"; } #endif // NOTIFY_DEBUG - _start_time = _true_clock->get_real_time() - time; + _start_short_time = _true_clock->get_short_time() - time; + _start_long_time = _true_clock->get_long_time() - time; } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/express/clockObject.h b/panda/src/express/clockObject.h index 0e20629c41..cee4a93b1e 100644 --- a/panda/src/express/clockObject.h +++ b/panda/src/express/clockObject.h @@ -77,8 +77,8 @@ PUBLISHED: INLINE Mode get_mode() const; INLINE double get_frame_time() const; - INLINE double get_intra_frame_time() const; INLINE double get_real_time() const; + INLINE double get_long_time() const; INLINE void reset(); void set_real_time(double time); @@ -101,7 +101,8 @@ PUBLISHED: private: TrueClock *_true_clock; Mode _mode; - double _start_time; + double _start_short_time; + double _start_long_time; int _frame_count; double _actual_frame_time; double _reported_frame_time; diff --git a/panda/src/express/memoryUsage.cxx b/panda/src/express/memoryUsage.cxx index 6d1d48d655..3770319691 100644 --- a/panda/src/express/memoryUsage.cxx +++ b/panda/src/express/memoryUsage.cxx @@ -462,7 +462,7 @@ ns_record_pointer(ReferenceCount *ptr) { info._ref_ptr = ptr; info._static_type = ReferenceCount::get_class_type(); info._dynamic_type = ReferenceCount::get_class_type(); - info._time = TrueClock::get_ptr()->get_real_time(); + info._time = TrueClock::get_ptr()->get_long_time(); info._freeze_index = _freeze_index; info._flags |= (MemoryInfo::F_reconsider_dynamic_type | MemoryInfo::F_got_ref); } @@ -564,7 +564,7 @@ ns_remove_pointer(ReferenceCount *ptr) { info._typed_ptr = (TypedObject *)NULL; if (info._freeze_index == _freeze_index) { - double now = TrueClock::get_ptr()->get_real_time(); + double now = TrueClock::get_ptr()->get_long_time(); // We have to protect modifications to the table from recursive // calls by toggling _recursion_protect while we adjust it. @@ -632,7 +632,7 @@ ns_record_void_pointer(void *ptr, size_t size) { info._void_ptr = ptr; info._size = size; - info._time = TrueClock::get_ptr()->get_real_time(); + info._time = TrueClock::get_ptr()->get_long_time(); info._freeze_index = _freeze_index; info._flags |= (MemoryInfo::F_got_void | MemoryInfo::F_size_known); } @@ -767,7 +767,7 @@ ns_get_pointers(MemoryUsagePointers &result) { nassertv(_track_memory_usage); result.clear(); - double now = TrueClock::get_ptr()->get_real_time(); + double now = TrueClock::get_ptr()->get_long_time(); Table::iterator ti; for (ti = _table.begin(); ti != _table.end(); ++ti) { MemoryInfo &info = (*ti).second; @@ -791,7 +791,7 @@ ns_get_pointers_of_type(MemoryUsagePointers &result, TypeHandle type) { nassertv(_track_memory_usage); result.clear(); - double now = TrueClock::get_ptr()->get_real_time(); + double now = TrueClock::get_ptr()->get_long_time(); Table::iterator ti; for (ti = _table.begin(); ti != _table.end(); ++ti) { MemoryInfo &info = (*ti).second; @@ -820,7 +820,7 @@ ns_get_pointers_of_age(MemoryUsagePointers &result, nassertv(_track_memory_usage); result.clear(); - double now = TrueClock::get_ptr()->get_real_time(); + double now = TrueClock::get_ptr()->get_long_time(); Table::iterator ti; for (ti = _table.begin(); ti != _table.end(); ++ti) { MemoryInfo &info = (*ti).second; @@ -863,7 +863,7 @@ ns_get_pointers_with_zero_count(MemoryUsagePointers &result) { nassertv(_track_memory_usage); result.clear(); - double now = TrueClock::get_ptr()->get_real_time(); + double now = TrueClock::get_ptr()->get_long_time(); Table::iterator ti; for (ti = _table.begin(); ti != _table.end(); ++ti) { MemoryInfo &info = (*ti).second; @@ -951,7 +951,7 @@ ns_show_current_ages() { _recursion_protect = true; AgeHistogram hist; - double now = TrueClock::get_ptr()->get_real_time(); + double now = TrueClock::get_ptr()->get_long_time(); Table::iterator ti; for (ti = _table.begin(); ti != _table.end(); ++ti) { diff --git a/panda/src/express/profileTimer.I b/panda/src/express/profileTimer.I index 5f9cd28aca..db8a19fb7c 100644 --- a/panda/src/express/profileTimer.I +++ b/panda/src/express/profileTimer.I @@ -18,13 +18,13 @@ INLINE void ProfileTimer:: on() { - _on = TrueClock::get_ptr()->get_real_time(); + _on = TrueClock::get_ptr()->get_short_time(); } INLINE double ProfileTimer:: getTime() { - double time = TrueClock::get_ptr()->get_real_time(); + double time = TrueClock::get_ptr()->get_short_time(); double et=_elapsedTime+=time-_on; _on=time; _elapsedTime=0.0; @@ -51,14 +51,14 @@ mark(const char* tag) { INLINE void ProfileTimer:: off() { - double time = TrueClock::get_ptr()->get_real_time(); + double time = TrueClock::get_ptr()->get_short_time(); _elapsedTime+=time-_on; } INLINE void ProfileTimer:: off(const char* tag) { - double time = TrueClock::get_ptr()->get_real_time(); + double time = TrueClock::get_ptr()->get_short_time(); _elapsedTime+=time-_on; mark(tag); } diff --git a/panda/src/express/trueClock.cxx b/panda/src/express/trueClock.cxx index 8e7c4243a3..96862f9cf8 100644 --- a/panda/src/express/trueClock.cxx +++ b/panda/src/express/trueClock.cxx @@ -53,7 +53,13 @@ void get_true_time_of_day(ulong &sec, ulong &usec) { } double TrueClock:: -get_real_time() const { +get_long_time() const { + DWORD tc = GetTickCount(); + return (double)(tc - _init_tc) / 1000.0; +} + +double TrueClock:: +get_short_time() const { if (_has_high_res) { LARGE_INTEGER count; QueryPerformanceCounter(&count); @@ -108,7 +114,8 @@ TrueClock() { } } - // Also store the initial tick count. We'll need this if we're not + // Also store the initial tick count. We'll need this for + // get_long_time(), as well as for get_short_time() if we're not // using the high resolution clock, or to cross-check the high // resolution clock if we are using it. _init_tc = GetTickCount(); @@ -158,7 +165,12 @@ void get_true_time_of_day(ulong &sec, ulong &msec) { } double TrueClock:: -get_real_time() const { +get_long_time() const { + return (double) _sec + ((double) _msec / 1000.0); +} + +double TrueClock:: +get_short_time() const { return (double) _sec + ((double) _msec / 1000.0); } @@ -219,7 +231,31 @@ void get_true_time_of_day(ulong &sec, ulong &msec) { } double TrueClock:: -get_real_time() const { +get_long_time() const { + struct timeval tv; + + int result; + +#ifdef GETTIMEOFDAY_ONE_PARAM + result = gettimeofday(&tv); +#else + result = gettimeofday(&tv, (struct timezone *)NULL); +#endif + + if (result < 0) { + // Error in gettimeofday(). + return 0.0; + } + + // We subtract out the time at which the clock was initialized, + // because we don't care about the number of seconds all the way + // back to 1970, and we want to leave the double with as much + // precision as it can get. + return (double)(tv.tv_sec - _init_sec) + (double)tv.tv_usec / 1000000.0; +} + +double TrueClock:: +get_short_time() const { struct timeval tv; int result; diff --git a/panda/src/express/trueClock.h b/panda/src/express/trueClock.h index 709375aeea..ce1e9d53bb 100644 --- a/panda/src/express/trueClock.h +++ b/panda/src/express/trueClock.h @@ -39,7 +39,18 @@ class EXPCL_PANDAEXPRESS TrueClock { public: INLINE static TrueClock *get_ptr(); - double get_real_time() const; + + // get_long_time() returns the most accurate timer we have over a + // long interval. It may not be very precise for measuring short + // intervals, but it should not drift substantially over the long + // haul. + double get_long_time() const; + + // get_short_time() returns the most precise timer we have over a + // short interval. It may tend to drift over the long haul, but it + // should have lots of digits to measure short intervals very + // precisely. + double get_short_time() const; protected: TrueClock();