Index: include/llvm/Support/Timer.h =================================================================== --- include/llvm/Support/Timer.h +++ include/llvm/Support/Timer.h @@ -62,8 +62,8 @@ MemUsed -= RHS.MemUsed; } - /// print - Print the current timer to standard error, and reset the "Started" - /// flag. + /// print - Print the current time record to \p OS, with a breakdown showing + /// contributions to the \p Total time record. void print(const TimeRecord &Total, raw_ostream &OS) const; }; @@ -76,9 +76,10 @@ /// if they are never started. /// class Timer { - TimeRecord Time; + TimeRecord Time; // The total time captured + TimeRecord StartTime; // The time `startTimer' was last called std::string Name; // The name of this time variable. - bool Started; // Has this time variable ever been started? + bool Started; // Is the timer currently running? TimerGroup *TG; // The TimerGroup this Timer is in. Timer **Prev, *Next; // Doubly linked list of timers in the group. @@ -112,6 +113,12 @@ /// void stopTimer(); + /// getTotalTime - Return the duration for which this timer has been running. + TimeRecord getTotalTime() const { return Time; } + + /// print - Print the total time captured without clearing the timer. + void print(raw_ostream &OS) const { Time.print(Time, OS); } + private: friend class TimerGroup; }; Index: lib/Support/Timer.cpp =================================================================== --- lib/Support/Timer.cpp +++ lib/Support/Timer.cpp @@ -96,11 +96,7 @@ //===----------------------------------------------------------------------===// void Timer::init(StringRef N) { - assert(!TG && "Timer already initialized"); - Name.assign(N.begin(), N.end()); - Started = false; - TG = getDefaultTimerGroup(); - TG->addTimer(*this); + init(N, *getDefaultTimerGroup()); } void Timer::init(StringRef N, TimerGroup &tg) { @@ -139,25 +135,17 @@ return Result; } -static ManagedStatic > ActiveTimers; - void Timer::startTimer() { + assert(!Started && "Cannot start a running timer"); Started = true; - ActiveTimers->push_back(this); - Time -= TimeRecord::getCurrentTime(true); + StartTime = TimeRecord::getCurrentTime(true); } void Timer::stopTimer() { + assert(Started && "Cannot stop a paused timer"); + Started = false; Time += TimeRecord::getCurrentTime(false); - - if (ActiveTimers->back() == this) { - ActiveTimers->pop_back(); - } else { - std::vector::iterator I = - std::find(ActiveTimers->begin(), ActiveTimers->end(), this); - assert(I != ActiveTimers->end() && "stop but no startTimer?"); - ActiveTimers->erase(I); - } + Time -= StartTime; } static void printVal(double Val, double Total, raw_ostream &OS) {