Browse Source

profileTimer.h

Dave Schuyler 24 years ago
parent
commit
adef60f18a
2 changed files with 109 additions and 0 deletions
  1. 17 0
      panda/src/express/datagramInputFile.cxx
  2. 92 0
      panda/src/express/profileTimer.h

+ 17 - 0
panda/src/express/datagramInputFile.cxx

@@ -6,6 +6,11 @@
 #include "datagramInputFile.h"
 #include "numeric_types.h"
 #include "datagramIterator.h"
+#include "profileTimer.h"
+#define SKYLER_TIMER 1
+#ifdef SKYLER_TIMER //[
+  EXPCL_PANDAEXPRESS ProfileTimer Skyler_timer_file;
+#endif //]
 
 ////////////////////////////////////////////////////////////////////
 //     Function: DatagramInputFile::read_header
@@ -41,6 +46,9 @@ read_header(string &header, size_t num_bytes) {
 ////////////////////////////////////////////////////////////////////
 bool DatagramInputFile::
 get_datagram(Datagram &data) {
+  #ifdef SKYLER_TIMER //[
+    Skyler_timer_file.on();
+  #endif //]
   _read_first_datagram = true;
 
   // First, get the size of the upcoming datagram.  We do this with
@@ -48,6 +56,9 @@ get_datagram(Datagram &data) {
   char sizebuf[sizeof(PN_uint32)];
   _in.read(sizebuf, sizeof(PN_uint32));
   if (_in.fail() || _in.eof()) {
+    #ifdef SKYLER_TIMER //[
+      Skyler_timer_file.off("DatagramInputFile::get_datagram");
+    #endif //]
     return false;
   }
 
@@ -63,11 +74,17 @@ get_datagram(Datagram &data) {
   if (_in.fail() || _in.eof()) {
     _error = true;
     delete[] buffer;
+    #ifdef SKYLER_TIMER //[
+      Skyler_timer_file.off("DatagramInputFile::get_datagram");
+    #endif //]
     return false;
   }
 
   data = Datagram(buffer, num_bytes);
   delete[] buffer;
+  #ifdef SKYLER_TIMER //[
+    Skyler_timer_file.off("DatagramInputFile::get_datagram");
+  #endif //]
   return true;
 }
 

+ 92 - 0
panda/src/express/profileTimer.h

@@ -0,0 +1,92 @@
+// ProfileTimer.h
+#ifndef PROFILETIMER_H //[
+#define PROFILETIMER_H
+
+#include "pandabase.h"
+#include "trueClock.h"
+
+/*
+    ProfileTimer
+    
+    HowTo:
+      Create a ProfileTimer and hold onto it.
+      Call init() whenever you like (the timer doesn't
+        start yet).
+      Call on() to start the timer.
+      While the timer is on, call mark() at each point of interest,
+        in the code you are timing.
+      You can turn the timer off() and on() to skip things you
+        don't want to time.
+      When your timing is finished, call printTo() to see the 
+        results (e.g. myTimer.printTo(cerr)).
+    
+    Notes:
+      You should be able to time things down to the millisecond
+      well enough, but if you call on() and off() within micro-
+      seconds of each other, I don't think you'll get very good
+      results.
+*/
+class EXPCL_PANDAEXPRESS ProfileTimer {
+  enum { MaxEntriesDefault=4096 };
+PUBLISHED:
+  ProfileTimer(const char* name=0, int maxEntries=MaxEntriesDefault);
+  ProfileTimer(const ProfileTimer& other);
+  ~ProfileTimer();
+  
+  void init(const char* name, int maxEntries=MaxEntriesDefault);
+  
+  void on();
+  void mark(const char* tag);
+  void off();
+  void off(const char* tag);
+  
+  // Don't call any of the following during timing:
+  // (Because they are slow, not because anything will break).
+  double getTotalTime() const;
+  static void consolidateAllTo(ostream &out=cout);
+  void consolidateTo(ostream &out=cout) const;
+  static void printAllTo(ostream &out=cout);
+  void printTo(ostream &out=cout) const;
+
+public:
+  /*
+      e.g.
+      void Foo() {
+        ProfileTimer::AutoTimer(myProfiler, "Foo()");
+        ...
+      }
+  */
+  class EXPCL_PANDAEXPRESS AutoTimer {
+  public:
+    AutoTimer(ProfileTimer& profile, const char* tag);
+    ~AutoTimer();
+  
+  protected:
+    ProfileTimer& _profile;
+    const char* _tag;
+  };
+
+protected:
+  static ProfileTimer* _head;
+  ProfileTimer* _next;
+  class TimerEntry {
+  public:
+    const char* _tag; // not owned by this.
+    double _time;
+  };
+  double _on;
+  double _elapsedTime;
+  const char* _name; // not owned by this.
+  int _maxEntries;
+  int _entryCount;
+  TimerEntry* _entries;
+  int _autoTimerCount; // see class AutoTimer
+
+  double getTime();
+  
+  friend ProfileTimer::AutoTimer;
+};
+
+#include "profileTimer.I"
+
+#endif //]