Creating a Timer Class as Part of a Timing Library

I’m creating a Timer class as part of a timing library I’m writing. I need a timing library for my work but I just don’t have time to do this during my workday, so I’m writing this on my own time. I’ll put the code up on this site for others to steal if they find it useful.

My work involves creating and supporting complex software on Linux, Solaris and Windows, so I will be focusing on creating C++ (first) and Java (second and when I get time) versions of this library. Here is my initial API. I am sure I will be modifying this as I think about it more. If you see anything that should be added, removed, or modified, please feel free to comment. The L prefix is intended to help me avoid name conflicts. The benchmark data structure can be used as you wish and of course the source will be available so you can modify the benchmark structure and code as you see fit.

// The developer should be able to choose what timing resolution he wants to work with
// and the base class should provide a method that tells him if that resolution is supported in his environment.

struct LBenchmarkEventStruct // struct is 128 bits long
EventType     // 8 bits
Event              // 8 bits
MsgID            // 32 bits
TimeStamp   // 64 bits – probably 32 bits each devoted to secs and nanosecs (each unsigned), or something like that.
Series             // 8 bits – can be used to identify a particular benchmark run among output from multiple runs
Subseries      // 8 bits – currently unused, use for subseries ID or whatever else you need

// Not much in the base class for now, but I suspect I’ll add more as time goes on
// and I see the need for more common timing functionality.
LTimer – Base Class
– bool IsResolutionSupported(LTIMER_RESOLUTION TimerResolution)

LCountdownTimer subclass
– LCountdownTimer(LTIMER_RESOLUTION TimerResolution, void *AlarmFunc)
– Start()
– Stop()
– Reset()

// A fairly complete implementation of an event recording class for use in benchmarking
// Note that the queue size determines how often the events will be written to disk since
// the queue will be written to disk when it becomes “full”.
LBenchmarkTimer subclass
– LBenchmarkTimer(LTIMER_RESOLUTION TimerResolution, string sOutputPath, string sOutputFileName);
– Init(unsigned int uiQueueSize); // Probably more parameters will be added to this method
– SetSeries(unsigned char ucSeries, unsigned char ucSubSeries);
– SetEvent(unsigned char ucEventType, unsigned char ucEvent);
– MarkEvent(MsgID, TimeStamp);
– MarkEvent(EventType, Event, MsgID, TimeStamp);
– FlushQueue(); // Flush the queued events to the output file
– GetEventCount(); // returns # of events since Init()
– GetQueueCount(); // returns # of events queued and waiting to be written to file
– GetNextMsgID(); // Returns next MsgID. MsgID is unique to a benchmark run and is simple count-up
// from 0 to 2^32 – 1
– SetMsgID(unsigned int uiMsgID); // Set MsgID to value in uiMsgID parameter. Not sure if there is a need for this?

LRepeatTimer subclass
– Start()
– Cancel()
– RepeatTimer(LTIMER_RESOLUTION TimerResolution, void *AlarmFunc)

LStopwatchTimer subclass
– LStopwatchTimer(LTIMER_RESOLUTION TimerResolution)
– Start() // Start or continue timer clock
– GetTotalTime() // Returns current time accrued on running or stopped timer
– StartLap() // Starts lap timer. Internally marks current accrued time for use when StopLap() or GetLapTime() is called
– GetLapTime() // Subtracts time when StartLap() was called from current accrued time and returns difference
– StopLap() // Stops lap timer. Subtracts time when StartLap() was called from current accrued time and returns difference
– Stop() // Stops timer and returns current time accrued
– Reset() // Clears time accrued, lap time, and other internal variables and sets timer to stopped state

There will be a (basic at first) parsing and analytical tool (maybe written in Java since performance is not an issue but portability is) that will read the output file of the LBenchmarkTimer and provide basic statistics and eventually MsgID tracking through a user-definable sequence of events.

Update: I have started a section for the timing library effort here.

Leave a Reply

Your email address will not be published. Required fields are marked *