From adef60f18a3bc7fc468a43aa31977637e65813bf Mon Sep 17 00:00:00 2001 From: Dave Schuyler Date: Tue, 15 May 2001 01:30:14 +0000 Subject: [PATCH] profileTimer.h --- panda/src/express/datagramInputFile.cxx | 17 +++++ panda/src/express/profileTimer.h | 92 +++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 panda/src/express/profileTimer.h diff --git a/panda/src/express/datagramInputFile.cxx b/panda/src/express/datagramInputFile.cxx index 24e745e219..f649f9a2cf 100644 --- a/panda/src/express/datagramInputFile.cxx +++ b/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; } diff --git a/panda/src/express/profileTimer.h b/panda/src/express/profileTimer.h new file mode 100644 index 0000000000..a039da29ef --- /dev/null +++ b/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 //]