Browse Source

add ClockObject::get_long_time()

David Rose 24 years ago
parent
commit
89c9b06bb1

+ 23 - 16
panda/src/express/clockObject.I

@@ -70,34 +70,41 @@ get_frame_time() const {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: ClockObject::get_intra_frame_time
+//     Function: ClockObject::get_real_time
 //       Access: Published
 //       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.
+//  Description: Returns the actual number of seconds elapsed since
+//               the ClockObject was created, or since it was last
+//               reset.  This is useful for doing real timing
+//               measurements, e.g. for performance statistics.
 //
 //
-//               This function returns an honest value whether the
-//               ClockObject is in normal, real-time mode or in
-//               non-real-time mode.
+//               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::
 INLINE double ClockObject::
-get_intra_frame_time() const {
-  return get_real_time() - _actual_frame_time;
+get_real_time() const {
+  return (_true_clock->get_short_time() - _start_short_time);
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: ClockObject::get_real_time
+//     Function: ClockObject::get_long_time
 //       Access: Published
 //       Access: Published
 //  Description: Returns the actual number of seconds elapsed since
 //  Description: Returns the actual number of seconds elapsed since
 //               the ClockObject was created, or since it was last
 //               the ClockObject was created, or since it was last
-//               reset.  This is useful for doing real timing
-//               measurements, e.g. for performance statistics.
+//               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::
 INLINE double ClockObject::
-get_real_time() const {
-  return (_true_clock->get_real_time() - _start_time);
+get_long_time() const {
+  return (_true_clock->get_long_time() - _start_long_time);
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////

+ 4 - 2
panda/src/express/clockObject.cxx

@@ -32,7 +32,8 @@ ClockObject::
 ClockObject() {
 ClockObject() {
   _true_clock = TrueClock::get_ptr();
   _true_clock = TrueClock::get_ptr();
   _mode = M_normal;
   _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;
   _frame_count = 0;
   _actual_frame_time = 0.0;
   _actual_frame_time = 0.0;
   _reported_frame_time = 0.0;
   _reported_frame_time = 0.0;
@@ -59,7 +60,8 @@ set_real_time(double time) {
       << " seconds.\n";
       << " seconds.\n";
   }
   }
 #endif  // NOTIFY_DEBUG
 #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;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////

+ 3 - 2
panda/src/express/clockObject.h

@@ -77,8 +77,8 @@ PUBLISHED:
   INLINE Mode get_mode() const;
   INLINE Mode get_mode() const;
 
 
   INLINE double get_frame_time() const;
   INLINE double get_frame_time() const;
-  INLINE double get_intra_frame_time() const;
   INLINE double get_real_time() const;
   INLINE double get_real_time() const;
+  INLINE double get_long_time() const;
 
 
   INLINE void reset();
   INLINE void reset();
   void set_real_time(double time);
   void set_real_time(double time);
@@ -101,7 +101,8 @@ PUBLISHED:
 private:
 private:
   TrueClock *_true_clock;
   TrueClock *_true_clock;
   Mode _mode;
   Mode _mode;
-  double _start_time;
+  double _start_short_time;
+  double _start_long_time;
   int _frame_count;
   int _frame_count;
   double _actual_frame_time;
   double _actual_frame_time;
   double _reported_frame_time;
   double _reported_frame_time;

+ 8 - 8
panda/src/express/memoryUsage.cxx

@@ -462,7 +462,7 @@ ns_record_pointer(ReferenceCount *ptr) {
     info._ref_ptr = ptr;
     info._ref_ptr = ptr;
     info._static_type = ReferenceCount::get_class_type();
     info._static_type = ReferenceCount::get_class_type();
     info._dynamic_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._freeze_index = _freeze_index;
     info._flags |= (MemoryInfo::F_reconsider_dynamic_type | MemoryInfo::F_got_ref);
     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;
     info._typed_ptr = (TypedObject *)NULL;
 
 
     if (info._freeze_index == _freeze_index) {
     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
       // We have to protect modifications to the table from recursive
       // calls by toggling _recursion_protect while we adjust it.
       // 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._void_ptr = ptr;
     info._size = size;
     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._freeze_index = _freeze_index;
     info._flags |= (MemoryInfo::F_got_void | MemoryInfo::F_size_known);
     info._flags |= (MemoryInfo::F_got_void | MemoryInfo::F_size_known);
   }
   }
@@ -767,7 +767,7 @@ ns_get_pointers(MemoryUsagePointers &result) {
   nassertv(_track_memory_usage);
   nassertv(_track_memory_usage);
   result.clear();
   result.clear();
 
 
-  double now = TrueClock::get_ptr()->get_real_time();
+  double now = TrueClock::get_ptr()->get_long_time();
   Table::iterator ti;
   Table::iterator ti;
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
     MemoryInfo &info = (*ti).second;
     MemoryInfo &info = (*ti).second;
@@ -791,7 +791,7 @@ ns_get_pointers_of_type(MemoryUsagePointers &result, TypeHandle type) {
   nassertv(_track_memory_usage);
   nassertv(_track_memory_usage);
   result.clear();
   result.clear();
 
 
-  double now = TrueClock::get_ptr()->get_real_time();
+  double now = TrueClock::get_ptr()->get_long_time();
   Table::iterator ti;
   Table::iterator ti;
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
     MemoryInfo &info = (*ti).second;
     MemoryInfo &info = (*ti).second;
@@ -820,7 +820,7 @@ ns_get_pointers_of_age(MemoryUsagePointers &result,
   nassertv(_track_memory_usage);
   nassertv(_track_memory_usage);
   result.clear();
   result.clear();
 
 
-  double now = TrueClock::get_ptr()->get_real_time();
+  double now = TrueClock::get_ptr()->get_long_time();
   Table::iterator ti;
   Table::iterator ti;
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
     MemoryInfo &info = (*ti).second;
     MemoryInfo &info = (*ti).second;
@@ -863,7 +863,7 @@ ns_get_pointers_with_zero_count(MemoryUsagePointers &result) {
   nassertv(_track_memory_usage);
   nassertv(_track_memory_usage);
   result.clear();
   result.clear();
 
 
-  double now = TrueClock::get_ptr()->get_real_time();
+  double now = TrueClock::get_ptr()->get_long_time();
   Table::iterator ti;
   Table::iterator ti;
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
     MemoryInfo &info = (*ti).second;
     MemoryInfo &info = (*ti).second;
@@ -951,7 +951,7 @@ ns_show_current_ages() {
   _recursion_protect = true;
   _recursion_protect = true;
 
 
   AgeHistogram hist;
   AgeHistogram hist;
-  double now = TrueClock::get_ptr()->get_real_time();
+  double now = TrueClock::get_ptr()->get_long_time();
 
 
   Table::iterator ti;
   Table::iterator ti;
   for (ti = _table.begin(); ti != _table.end(); ++ti) {
   for (ti = _table.begin(); ti != _table.end(); ++ti) {

+ 4 - 4
panda/src/express/profileTimer.I

@@ -18,13 +18,13 @@
 
 
 INLINE void ProfileTimer::
 INLINE void ProfileTimer::
 on() {
 on() {
-  _on = TrueClock::get_ptr()->get_real_time();
+  _on = TrueClock::get_ptr()->get_short_time();
 }
 }
 
 
 
 
 INLINE double ProfileTimer::
 INLINE double ProfileTimer::
 getTime() {
 getTime() {
-  double time = TrueClock::get_ptr()->get_real_time();
+  double time = TrueClock::get_ptr()->get_short_time();
   double et=_elapsedTime+=time-_on;
   double et=_elapsedTime+=time-_on;
   _on=time;
   _on=time;
   _elapsedTime=0.0;
   _elapsedTime=0.0;
@@ -51,14 +51,14 @@ mark(const char* tag) {
 
 
 INLINE void ProfileTimer::
 INLINE void ProfileTimer::
 off() {
 off() {
-  double time = TrueClock::get_ptr()->get_real_time();
+  double time = TrueClock::get_ptr()->get_short_time();
   _elapsedTime+=time-_on;
   _elapsedTime+=time-_on;
 }
 }
 
 
 
 
 INLINE void ProfileTimer::
 INLINE void ProfileTimer::
 off(const char* tag) {
 off(const char* tag) {
-  double time = TrueClock::get_ptr()->get_real_time();
+  double time = TrueClock::get_ptr()->get_short_time();
   _elapsedTime+=time-_on;
   _elapsedTime+=time-_on;
   mark(tag);
   mark(tag);
 }
 }

+ 40 - 4
panda/src/express/trueClock.cxx

@@ -53,7 +53,13 @@ void get_true_time_of_day(ulong &sec, ulong &usec) {
 }
 }
 
 
 double TrueClock::
 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) {
   if (_has_high_res) {
     LARGE_INTEGER count;
     LARGE_INTEGER count;
     QueryPerformanceCounter(&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
   // using the high resolution clock, or to cross-check the high
   // resolution clock if we are using it.
   // resolution clock if we are using it.
   _init_tc = GetTickCount();
   _init_tc = GetTickCount();
@@ -158,7 +165,12 @@ void get_true_time_of_day(ulong &sec, ulong &msec) {
 }
 }
 
 
 double TrueClock::
 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);
   return (double) _sec + ((double) _msec / 1000.0);
 }
 }
 
 
@@ -219,7 +231,31 @@ void get_true_time_of_day(ulong &sec, ulong &msec) {
 }
 }
 
 
 double TrueClock::
 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;
   struct timeval tv;
 
 
   int result;
   int result;

+ 12 - 1
panda/src/express/trueClock.h

@@ -39,7 +39,18 @@
 class EXPCL_PANDAEXPRESS TrueClock {
 class EXPCL_PANDAEXPRESS TrueClock {
 public:
 public:
   INLINE static TrueClock *get_ptr();
   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:
 protected:
   TrueClock();
   TrueClock();