llvm.org GIT mirror llvm / 3a7ab88
[Support] Allow multiple paired calls to {start,stop}Timer() Differential Revision: http://reviews.llvm.org/D15619 Reviewed-by: rafael git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256258 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 3 years ago
4 changed file(s) with 84 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
6161 MemUsed -= RHS.MemUsed;
6262 }
6363
64 /// print - Print the current timer to standard error, and reset the "Started"
65 /// flag.
64 /// Print the current time record to \p OS, with a breakdown showing
65 /// contributions to the \p Total time record.
6666 void print(const TimeRecord &Total, raw_ostream &OS) const;
6767 };
6868
7575 /// if they are never started.
7676 ///
7777 class Timer {
78 TimeRecord Time;
78 TimeRecord Time; // The total time captured
79 TimeRecord StartTime; // The time startTimer() was last called
7980 std::string Name; // The name of this time variable.
80 bool Started; // Has this time variable ever been started?
81 bool Running; // Is the timer currently running?
82 bool Triggered; // Has the timer ever been triggered?
8183 TimerGroup *TG; // The TimerGroup this Timer is in.
8284
8385 Timer **Prev, *Next; // Doubly linked list of timers in the group.
101103 const std::string &getName() const { return Name; }
102104 bool isInitialized() const { return TG != nullptr; }
103105
104 /// startTimer - Start the timer running. Time between calls to
105 /// startTimer/stopTimer is counted by the Timer class. Note that these calls
106 /// must be correctly paired.
107 ///
106 /// Check if startTimer() has ever been called on this timer.
107 bool hasTriggered() const { return Triggered; }
108
109 /// Start the timer running. Time between calls to startTimer/stopTimer is
110 /// counted by the Timer class. Note that these calls must be correctly
111 /// paired.
108112 void startTimer();
109113
110 /// stopTimer - Stop the timer.
111 ///
114 /// Stop the timer.
112115 void stopTimer();
116
117 /// Clear the timer state.
118 void clear();
119
120 /// Return the duration for which this timer has been running.
121 TimeRecord getTotalTime() const { return Time; }
113122
114123 private:
115124 friend class TimerGroup;
101101 void Timer::init(StringRef N, TimerGroup &tg) {
102102 assert(!TG && "Timer already initialized");
103103 Name.assign(N.begin(), N.end());
104 Started = false;
104 Running = Triggered = false;
105105 TG = &tg;
106106 TG->addTimer(*this);
107107 }
134134 return Result;
135135 }
136136
137 static ManagedStatic > ActiveTimers;
138
139137 void Timer::startTimer() {
140 Started = true;
141 ActiveTimers->push_back(this);
142 Time -= TimeRecord::getCurrentTime(true);
138 assert(!Running && "Cannot start a running timer");
139 Running = Triggered = true;
140 StartTime = TimeRecord::getCurrentTime(true);
143141 }
144142
145143 void Timer::stopTimer() {
144 assert(Running && "Cannot stop a paused timer");
145 Running = false;
146146 Time += TimeRecord::getCurrentTime(false);
147
148 if (ActiveTimers->back() == this) {
149 ActiveTimers->pop_back();
150 } else {
151 std::vector::iterator I =
152 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
153 assert(I != ActiveTimers->end() && "stop but no startTimer?");
154 ActiveTimers->erase(I);
155 }
147 Time -= StartTime;
148 }
149
150 void Timer::clear() {
151 Running = Triggered = false;
152 Time = StartTime = TimeRecord();
156153 }
157154
158155 static void printVal(double Val, double Total, raw_ostream &OS) {
270267 sys::SmartScopedLock L(*TimerLock);
271268
272269 // If the timer was started, move its data to TimersToPrint.
273 if (T.Started)
270 if (T.hasTriggered())
274271 TimersToPrint.emplace_back(T.Time, T.Name);
275272
276273 T.TG = nullptr;
356353 // See if any of our timers were started, if so add them to TimersToPrint and
357354 // reset them.
358355 for (Timer *T = FirstTimer; T; T = T->Next) {
359 if (!T->Started) continue;
356 if (!T->hasTriggered()) continue;
360357 TimersToPrint.emplace_back(T->Time, T->Name);
361358
362359 // Clear out the time.
363 T->Started = 0;
364 T->Time = TimeRecord();
360 T->clear();
365361 }
366362
367363 // If any timers were started, print the group.
4141 TargetRegistry.cpp
4242 ThreadLocalTest.cpp
4343 ThreadPool.cpp
44 TimerTest.cpp
4445 TimeValueTest.cpp
4546 TrailingObjectsTest.cpp
4647 UnicodeTest.cpp
0 //===- unittests/TimerTest.cpp - Timer tests ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/Timer.h"
10 #include "gtest/gtest.h"
11 #include
12 #include
13
14 using namespace llvm;
15
16 namespace {
17
18 TEST(Timer, Additivity) {
19 Timer T1("T1");
20
21 EXPECT_TRUE(T1.isInitialized());
22
23 T1.startTimer();
24 T1.stopTimer();
25 auto TR1 = T1.getTotalTime();
26
27 T1.startTimer();
28 std::this_thread::sleep_for(std::chrono::milliseconds(1));
29 T1.stopTimer();
30 auto TR2 = T1.getTotalTime();
31
32 EXPECT_TRUE(TR1 < TR2);
33 }
34
35 TEST(Timer, CheckIfTriggered) {
36 Timer T1("T1");
37
38 EXPECT_FALSE(T1.hasTriggered());
39 T1.startTimer();
40 EXPECT_TRUE(T1.hasTriggered());
41 T1.stopTimer();
42 EXPECT_TRUE(T1.hasTriggered());
43
44 T1.clear();
45 EXPECT_FALSE(T1.hasTriggered());
46 }
47
48 } // end anon namespace