[PATCH] D15619: [Support] Make llvm::Timer reusable

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 17 11:07:48 PST 2015


vsk created this revision.
vsk added reviewers: bruno, joker.eph.
vsk added a subscriber: llvm-commits.

Make llvm::Timer reusable by allowing it to be started + stopped repeatedly.

This gets rid of an awkward static variable (`ActiveTimers`) and makes the API more flexible.

In my use case, I have a long-running tool which periodically calls into llvm. I need to time a few regions multiple times. It makes life much easier to use a single Timer for this.

This patch also cleans up some documentation -- that will go in a separate NFC commit.

http://reviews.llvm.org/D15619

Files:
  include/llvm/Support/Timer.h
  lib/Support/Timer.cpp

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<std::vector<Timer*> > 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<Timer*>::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) {
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;
 };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15619.43158.patch
Type: text/x-patch
Size: 2834 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151217/866e20d6/attachment.bin>


More information about the llvm-commits mailing list