[PATCH] D20735: [Support] Allow nesting paired calls to {start, stop}Timer
Vedant Kumar via llvm-commits
llvm-commits at lists.llvm.org
Fri May 27 09:48:20 PDT 2016
vsk created this revision.
vsk added reviewers: davide, rafael.
vsk added a subscriber: llvm-commits.
The old Timer implementation (before r256258) allowed nesting calls to {start,stop}Timer. Bring that behavior back and add a regression test for it.
http://reviews.llvm.org/D20735
Files:
include/llvm/Support/Timer.h
lib/Support/Timer.cpp
unittests/Support/TimerTest.cpp
Index: unittests/Support/TimerTest.cpp
===================================================================
--- unittests/Support/TimerTest.cpp
+++ unittests/Support/TimerTest.cpp
@@ -62,4 +62,13 @@
EXPECT_FALSE(T1.hasTriggered());
}
+TEST(Timer, Nesting) {
+ Timer T1("T1");
+ T1.startTimer();
+ T1.startTimer();
+ T1.stopTimer();
+ T1.stopTimer();
+ EXPECT_TRUE(T1.hasTriggered());
+}
+
} // end anon namespace
Index: lib/Support/Timer.cpp
===================================================================
--- lib/Support/Timer.cpp
+++ lib/Support/Timer.cpp
@@ -101,8 +101,10 @@
void Timer::init(StringRef N, TimerGroup &tg) {
assert(!TG && "Timer already initialized");
+ Time = TimeRecord();
Name.assign(N.begin(), N.end());
- Running = Triggered = false;
+ Triggered = false;
+ RefCount = 0;
TG = &tg;
TG->addTimer(*this);
}
@@ -136,21 +138,23 @@
}
void Timer::startTimer() {
- assert(!Running && "Cannot start a running timer");
- Running = Triggered = true;
- StartTime = TimeRecord::getCurrentTime(true);
+ Triggered = true;
+ if (!RefCount)
+ Time -= TimeRecord::getCurrentTime(true);
+ ++RefCount;
}
void Timer::stopTimer() {
- assert(Running && "Cannot stop a paused timer");
- Running = false;
- Time += TimeRecord::getCurrentTime(false);
- Time -= StartTime;
+ assert(RefCount > 0 && "Cannot stop a paused timer");
+ --RefCount;
+ if (!RefCount)
+ Time += TimeRecord::getCurrentTime(false);
}
void Timer::clear() {
- Running = Triggered = false;
- Time = StartTime = TimeRecord();
+ RefCount = 0;
+ Triggered = false;
+ Time = TimeRecord();
}
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
@@ -77,9 +77,8 @@
///
class Timer {
TimeRecord Time; // The total time captured
- TimeRecord StartTime; // The time startTimer() was last called
std::string Name; // The name of this time variable.
- bool Running; // Is the timer currently running?
+ unsigned RefCount; // How many times has this timer been started?
bool Triggered; // Has the timer ever been triggered?
TimerGroup *TG; // The TimerGroup this Timer is in.
@@ -119,7 +118,10 @@
void clear();
/// Return the duration for which this timer has been running.
- TimeRecord getTotalTime() const { return Time; }
+ TimeRecord getTotalTime() const {
+ assert(RefCount == 0 && "Cannot query active timer");
+ return Time;
+ }
private:
friend class TimerGroup;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20735.58800.patch
Type: text/x-patch
Size: 2664 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160527/fc1ce05e/attachment.bin>
More information about the llvm-commits
mailing list