1 //===-- Timer.cpp - Interval Timing Support -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Interval Timing implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Timer.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/Mutex.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/raw_ostream.h"
25 // CreateInfoOutputFile - Return a file stream to print our output on.
26 namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
28 // getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
29 // of constructor/destructor ordering being unspecified by C++. Basically the
30 // problem is that a Statistic object gets destroyed, which ends up calling
31 // 'GetLibSupportInfoOutputFile()' (below), which calls this function.
32 // LibSupportInfoOutputFilename used to be a global variable, but sometimes it
33 // would get destroyed before the Statistic, causing havoc to ensue. We "fix"
34 // this by creating the string the first time it is needed and never destroying
36 static ManagedStatic<std::string> LibSupportInfoOutputFilename;
37 static std::string &getLibSupportInfoOutputFilename() {
38 return *LibSupportInfoOutputFilename;
41 static ManagedStatic<sys::SmartMutex<true> > TimerLock;
45 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
46 "tracking (this may be slow)"),
49 static cl::opt<std::string, true>
50 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
51 cl::desc("File to append -stats and -timer output to"),
52 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
55 // CreateInfoOutputFile - Return a file stream to print our output on.
56 raw_ostream *llvm::CreateInfoOutputFile() {
57 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
58 if (OutputFilename.empty())
59 return new raw_fd_ostream(2, false); // stderr.
60 if (OutputFilename == "-")
61 return new raw_fd_ostream(1, false); // stdout.
63 // Append mode is used because the info output file is opened and closed
64 // each time -stats or -time-passes wants to print output to it. To
65 // compensate for this, the test-suite Makefiles have code to delete the
66 // info output file before running commands which write to it.
68 raw_ostream *Result = new raw_fd_ostream(OutputFilename, EC,
69 sys::fs::F_Append | sys::fs::F_Text);
73 errs() << "Error opening info-output-file '"
74 << OutputFilename << " for appending!\n";
76 return new raw_fd_ostream(2, false); // stderr.
80 static TimerGroup *DefaultTimerGroup = nullptr;
81 static TimerGroup *getDefaultTimerGroup() {
82 TimerGroup *tmp = DefaultTimerGroup;
86 sys::SmartScopedLock<true> Lock(*TimerLock);
87 tmp = DefaultTimerGroup;
89 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
91 DefaultTimerGroup = tmp;
97 //===----------------------------------------------------------------------===//
98 // Timer Implementation
99 //===----------------------------------------------------------------------===//
101 void Timer::init(StringRef N) {
102 assert(!TG && "Timer already initialized");
103 Name.assign(N.begin(), N.end());
105 TG = getDefaultTimerGroup();
109 void Timer::init(StringRef N, TimerGroup &tg) {
110 assert(!TG && "Timer already initialized");
111 Name.assign(N.begin(), N.end());
118 if (!TG) return; // Never initialized, or already cleared.
119 TG->removeTimer(*this);
122 static inline size_t getMemUsage() {
123 if (!TrackSpace) return 0;
124 return sys::Process::GetMallocUsage();
127 TimeRecord TimeRecord::getCurrentTime(bool Start) {
129 sys::TimeValue now(0,0), user(0,0), sys(0,0);
132 Result.MemUsed = getMemUsage();
133 sys::Process::GetTimeUsage(now, user, sys);
135 sys::Process::GetTimeUsage(now, user, sys);
136 Result.MemUsed = getMemUsage();
139 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
140 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
141 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
145 static ManagedStatic<std::vector<Timer*> > ActiveTimers;
147 void Timer::startTimer() {
149 ActiveTimers->push_back(this);
150 Time -= TimeRecord::getCurrentTime(true);
153 void Timer::stopTimer() {
154 Time += TimeRecord::getCurrentTime(false);
156 if (ActiveTimers->back() == this) {
157 ActiveTimers->pop_back();
159 std::vector<Timer*>::iterator I =
160 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
161 assert(I != ActiveTimers->end() && "stop but no startTimer?");
162 ActiveTimers->erase(I);
166 static void printVal(double Val, double Total, raw_ostream &OS) {
167 if (Total < 1e-7) // Avoid dividing by zero.
170 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
173 void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
174 if (Total.getUserTime())
175 printVal(getUserTime(), Total.getUserTime(), OS);
176 if (Total.getSystemTime())
177 printVal(getSystemTime(), Total.getSystemTime(), OS);
178 if (Total.getProcessTime())
179 printVal(getProcessTime(), Total.getProcessTime(), OS);
180 printVal(getWallTime(), Total.getWallTime(), OS);
184 if (Total.getMemUsed())
185 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
189 //===----------------------------------------------------------------------===//
190 // NamedRegionTimer Implementation
191 //===----------------------------------------------------------------------===//
195 typedef StringMap<Timer> Name2TimerMap;
198 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
201 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
202 I = Map.begin(), E = Map.end(); I != E; ++I)
203 delete I->second.first;
206 Timer &get(StringRef Name, StringRef GroupName) {
207 sys::SmartScopedLock<true> L(*TimerLock);
209 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
211 if (!GroupEntry.first)
212 GroupEntry.first = new TimerGroup(GroupName);
214 Timer &T = GroupEntry.second[Name];
215 if (!T.isInitialized())
216 T.init(Name, *GroupEntry.first);
223 static ManagedStatic<Name2TimerMap> NamedTimers;
224 static ManagedStatic<Name2PairMap> NamedGroupedTimers;
226 static Timer &getNamedRegionTimer(StringRef Name) {
227 sys::SmartScopedLock<true> L(*TimerLock);
229 Timer &T = (*NamedTimers)[Name];
230 if (!T.isInitialized())
235 NamedRegionTimer::NamedRegionTimer(StringRef Name,
237 : TimeRegion(!Enabled ? nullptr : &getNamedRegionTimer(Name)) {}
239 NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName,
241 : TimeRegion(!Enabled ? nullptr : &NamedGroupedTimers->get(Name, GroupName)){}
243 //===----------------------------------------------------------------------===//
244 // TimerGroup Implementation
245 //===----------------------------------------------------------------------===//
247 /// TimerGroupList - This is the global list of TimerGroups, maintained by the
248 /// TimerGroup ctor/dtor and is protected by the TimerLock lock.
249 static TimerGroup *TimerGroupList = nullptr;
251 TimerGroup::TimerGroup(StringRef name)
252 : Name(name.begin(), name.end()), FirstTimer(nullptr) {
254 // Add the group to TimerGroupList.
255 sys::SmartScopedLock<true> L(*TimerLock);
257 TimerGroupList->Prev = &Next;
258 Next = TimerGroupList;
259 Prev = &TimerGroupList;
260 TimerGroupList = this;
263 TimerGroup::~TimerGroup() {
264 // If the timer group is destroyed before the timers it owns, accumulate and
265 // print the timing data.
267 removeTimer(*FirstTimer);
269 // Remove the group from the TimerGroupList.
270 sys::SmartScopedLock<true> L(*TimerLock);
277 void TimerGroup::removeTimer(Timer &T) {
278 sys::SmartScopedLock<true> L(*TimerLock);
280 // If the timer was started, move its data to TimersToPrint.
282 TimersToPrint.push_back(std::make_pair(T.Time, T.Name));
286 // Unlink the timer from our list.
289 T.Next->Prev = T.Prev;
291 // Print the report when all timers in this group are destroyed if some of
292 // them were started.
293 if (FirstTimer || TimersToPrint.empty())
296 raw_ostream *OutStream = CreateInfoOutputFile();
297 PrintQueuedTimers(*OutStream);
298 delete OutStream; // Close the file.
301 void TimerGroup::addTimer(Timer &T) {
302 sys::SmartScopedLock<true> L(*TimerLock);
304 // Add the timer to our list.
306 FirstTimer->Prev = &T.Next;
308 T.Prev = &FirstTimer;
312 void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
313 // Sort the timers in descending order by amount of time taken.
314 std::sort(TimersToPrint.begin(), TimersToPrint.end());
317 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
318 Total += TimersToPrint[i].first;
320 // Print out timing header.
321 OS << "===" << std::string(73, '-') << "===\n";
322 // Figure out how many spaces to indent TimerGroup name.
323 unsigned Padding = (80-Name.length())/2;
324 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
325 OS.indent(Padding) << Name << '\n';
326 OS << "===" << std::string(73, '-') << "===\n";
328 // If this is not an collection of ungrouped times, print the total time.
329 // Ungrouped timers don't really make sense to add up. We still print the
330 // TOTAL line to make the percentages make sense.
331 if (this != DefaultTimerGroup)
332 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
333 Total.getProcessTime(), Total.getWallTime());
336 if (Total.getUserTime())
337 OS << " ---User Time---";
338 if (Total.getSystemTime())
339 OS << " --System Time--";
340 if (Total.getProcessTime())
341 OS << " --User+System--";
342 OS << " ---Wall Time---";
343 if (Total.getMemUsed())
345 OS << " --- Name ---\n";
347 // Loop through all of the timing data, printing it out.
348 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
349 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
350 Entry.first.print(Total, OS);
351 OS << Entry.second << '\n';
354 Total.print(Total, OS);
358 TimersToPrint.clear();
361 /// print - Print any started timers in this group and zero them.
362 void TimerGroup::print(raw_ostream &OS) {
363 sys::SmartScopedLock<true> L(*TimerLock);
365 // See if any of our timers were started, if so add them to TimersToPrint and
367 for (Timer *T = FirstTimer; T; T = T->Next) {
368 if (!T->Started) continue;
369 TimersToPrint.push_back(std::make_pair(T->Time, T->Name));
371 // Clear out the time.
373 T->Time = TimeRecord();
376 // If any timers were started, print the group.
377 if (!TimersToPrint.empty())
378 PrintQueuedTimers(OS);
381 /// printAll - This static method prints all timers and clears them all out.
382 void TimerGroup::printAll(raw_ostream &OS) {
383 sys::SmartScopedLock<true> L(*TimerLock);
385 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)