blob: 96f440a4ed4189bbb15aa14681c20c43170fc917 [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6c38a792002-10-01 19:36:54 +00009//
10// Interval Timing implementation.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/Timer.h"
15#include "llvm/Support/CommandLine.h"
Chris Lattnerfc86c3c2010-03-29 21:28:41 +000016#include "llvm/Support/Debug.h"
Chris Lattner90aa8392006-10-04 21:52:35 +000017#include "llvm/Support/ManagedStatic.h"
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000018#include "llvm/Support/raw_ostream.h"
19#include "llvm/Support/Format.h"
Chris Lattnerfc86c3c2010-03-29 21:28:41 +000020#include "llvm/System/Mutex.h"
Reid Spencerdf52c9a2004-12-20 00:59:04 +000021#include "llvm/System/Process.h"
Chris Lattnera782e752010-03-30 04:03:22 +000022#include "llvm/ADT/StringMap.h"
Chris Lattnerb4db5f32004-06-07 19:34:51 +000023#include <map>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000024using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000025
Chris Lattnerb4db5f32004-06-07 19:34:51 +000026// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000027namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner71336a92003-07-31 19:38:34 +000029// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
30// of constructor/destructor ordering being unspecified by C++. Basically the
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000031// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattner71336a92003-07-31 19:38:34 +000032// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
33// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencerf6e5a252004-12-14 03:55:21 +000034// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
35// this by creating the string the first time it is needed and never destroying
36// it.
Chris Lattner90aa8392006-10-04 21:52:35 +000037static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000038static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000039 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000040}
Chris Lattner6c38a792002-10-01 19:36:54 +000041
Owen Anderson46d9a642009-06-23 20:52:29 +000042static ManagedStatic<sys::SmartMutex<true> > TimerLock;
43
Chris Lattner3f398492003-01-30 23:08:50 +000044namespace {
Dan Gohman3c02aca2008-04-23 23:15:23 +000045 static cl::opt<bool>
Chris Lattner3f398492003-01-30 23:08:50 +000046 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
47 "tracking (this may be slow)"),
48 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000049
Dan Gohman3c02aca2008-04-23 23:15:23 +000050 static cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000051 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000052 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000053 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000054}
55
Chris Lattnerf8df3e02010-03-29 21:34:06 +000056// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
57raw_ostream *llvm::GetLibSupportInfoOutputFile() {
58 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
59 if (LibSupportInfoOutputFilename.empty())
60 return &errs();
61 if (LibSupportInfoOutputFilename == "-")
62 return &outs();
63
64 std::string Error;
65 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
66 Error, raw_fd_ostream::F_Append);
67 if (Error.empty())
68 return Result;
69
70 errs() << "Error opening info-output-file '"
71 << LibSupportInfoOutputFilename << " for appending!\n";
72 delete Result;
73 return &errs();
74}
75
76
Owen Anderson200aa6d2009-06-23 16:36:10 +000077static TimerGroup *DefaultTimerGroup = 0;
Chris Lattner6c38a792002-10-01 19:36:54 +000078static TimerGroup *getDefaultTimerGroup() {
Chris Lattner9bb110f2010-03-29 20:35:01 +000079 TimerGroup *tmp = DefaultTimerGroup;
Owen Anderson3b8d1352009-06-23 17:33:37 +000080 sys::MemoryFence();
Chris Lattner9bb110f2010-03-29 20:35:01 +000081 if (tmp) return tmp;
82
83 llvm_acquire_global_lock();
84 tmp = DefaultTimerGroup;
Owen Anderson3b8d1352009-06-23 17:33:37 +000085 if (!tmp) {
Chris Lattner9bb110f2010-03-29 20:35:01 +000086 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
87 sys::MemoryFence();
88 DefaultTimerGroup = tmp;
Owen Anderson3b8d1352009-06-23 17:33:37 +000089 }
Chris Lattner9bb110f2010-03-29 20:35:01 +000090 llvm_release_global_lock();
Mikhail Glushenkovc11e84d2009-11-07 06:33:12 +000091
Owen Anderson3b8d1352009-06-23 17:33:37 +000092 return tmp;
Chris Lattner6c38a792002-10-01 19:36:54 +000093}
94
Chris Lattner9bb110f2010-03-29 20:35:01 +000095//===----------------------------------------------------------------------===//
96// Timer Implementation
97//===----------------------------------------------------------------------===//
98
Chris Lattnera782e752010-03-30 04:03:22 +000099void Timer::init(const std::string &N) {
100 assert(TG == 0 && "Timer already initialized");
101 Name = N;
102 Started = false;
103 TG = getDefaultTimerGroup();
Chris Lattner6c38a792002-10-01 19:36:54 +0000104 TG->addTimer();
105}
106
Chris Lattnera782e752010-03-30 04:03:22 +0000107void Timer::init(const std::string &N, TimerGroup &tg) {
108 assert(TG == 0 && "Timer already initialized");
109 Name = N;
110 Started = false;
111 TG = &tg;
Chris Lattner6c38a792002-10-01 19:36:54 +0000112 TG->addTimer();
113}
114
Chris Lattner6c38a792002-10-01 19:36:54 +0000115Timer::~Timer() {
Chris Lattnera782e752010-03-30 04:03:22 +0000116 if (!TG) return; // Never initialized.
Chris Lattner9bb110f2010-03-29 20:35:01 +0000117
118 if (Started) {
119 Started = false;
Chris Lattnera782e752010-03-30 04:03:22 +0000120 TG->addTimerToPrint(Time, Name);
Chris Lattner6c38a792002-10-01 19:36:54 +0000121 }
Chris Lattner9bb110f2010-03-29 20:35:01 +0000122 TG->removeTimer();
Chris Lattner6c38a792002-10-01 19:36:54 +0000123}
124
Jeff Cohene269a1a2005-01-08 20:15:57 +0000125static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000126 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +0000127 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +0000128 return 0;
129}
130
Chris Lattnera782e752010-03-30 04:03:22 +0000131TimeRecord TimeRecord::getCurrentTime(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000132 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000133
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000134 sys::TimeValue now(0,0);
135 sys::TimeValue user(0,0);
136 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000137
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000138 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000139 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000140 MemUsed = getMemUsage();
Chris Lattner9bb110f2010-03-29 20:35:01 +0000141 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000142 } else {
Chris Lattner9bb110f2010-03-29 20:35:01 +0000143 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000144 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000145 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000146
Chris Lattnera782e752010-03-30 04:03:22 +0000147 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattner9bb110f2010-03-29 20:35:01 +0000148 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattnera782e752010-03-30 04:03:22 +0000149 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattner9bb110f2010-03-29 20:35:01 +0000150 Result.MemUsed = MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000151 return Result;
152}
153
Chris Lattner90aa8392006-10-04 21:52:35 +0000154static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000155
Chris Lattner6c38a792002-10-01 19:36:54 +0000156void Timer::startTimer() {
157 Started = true;
Dan Gohman153d28a2008-06-24 22:07:07 +0000158 ActiveTimers->push_back(this);
Chris Lattnera782e752010-03-30 04:03:22 +0000159 Time -= TimeRecord::getCurrentTime(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000160}
161
162void Timer::stopTimer() {
Chris Lattnera782e752010-03-30 04:03:22 +0000163 Time += TimeRecord::getCurrentTime(false);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000164
Chris Lattner90aa8392006-10-04 21:52:35 +0000165 if (ActiveTimers->back() == this) {
166 ActiveTimers->pop_back();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000167 } else {
168 std::vector<Timer*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000169 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
170 assert(I != ActiveTimers->end() && "stop but no startTimer?");
171 ActiveTimers->erase(I);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000172 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000173}
174
Chris Lattner9bb110f2010-03-29 20:35:01 +0000175static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner0613edc2010-03-29 20:40:19 +0000176 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattner9bb110f2010-03-29 20:35:01 +0000177 OS << " ----- ";
178 else {
179 OS << " " << format("%7.4f", Val) << " (";
180 OS << format("%5.1f", Val*100/Total) << "%)";
181 }
182}
183
Chris Lattnera782e752010-03-30 04:03:22 +0000184void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
185 if (Total.getUserTime())
186 printVal(getUserTime(), Total.getUserTime(), OS);
187 if (Total.getSystemTime())
188 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattner9bb110f2010-03-29 20:35:01 +0000189 if (Total.getProcessTime())
190 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattnera782e752010-03-30 04:03:22 +0000191 printVal(getWallTime(), Total.getWallTime(), OS);
Chris Lattner9bb110f2010-03-29 20:35:01 +0000192
193 OS << " ";
194
Chris Lattnera782e752010-03-30 04:03:22 +0000195 if (Total.getMemUsed())
196 OS << format("%9lld", (long long)getMemUsed()) << " ";
Chris Lattner9bb110f2010-03-29 20:35:01 +0000197}
198
199
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000200//===----------------------------------------------------------------------===//
201// NamedRegionTimer Implementation
202//===----------------------------------------------------------------------===//
203
Chris Lattnera782e752010-03-30 04:03:22 +0000204typedef StringMap<Timer> Name2TimerMap;
205typedef StringMap<std::pair<TimerGroup, Name2TimerMap> > Name2PairMap;
Dan Gohman5e843682008-07-14 18:19:29 +0000206
Chris Lattnera782e752010-03-30 04:03:22 +0000207static ManagedStatic<Name2TimerMap> NamedTimers;
208static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000209
Chris Lattner90aa8392006-10-04 21:52:35 +0000210static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000211 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnera782e752010-03-30 04:03:22 +0000212
213 Timer &T = (*NamedTimers)[Name];
214 if (!T.isInitialized())
215 T.init(Name);
216 return T;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000217}
218
Dan Gohman5e843682008-07-14 18:19:29 +0000219static Timer &getNamedRegionTimer(const std::string &Name,
220 const std::string &GroupName) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000221 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000222
Chris Lattnera782e752010-03-30 04:03:22 +0000223 std::pair<TimerGroup, Name2TimerMap> &GroupEntry =
224 (*NamedGroupedTimers)[GroupName];
Dan Gohman5e843682008-07-14 18:19:29 +0000225
Chris Lattnera782e752010-03-30 04:03:22 +0000226 if (GroupEntry.second.empty())
227 GroupEntry.first.setName(GroupName);
Dan Gohman5e843682008-07-14 18:19:29 +0000228
Chris Lattnera782e752010-03-30 04:03:22 +0000229 Timer &T = GroupEntry.second[Name];
230 if (!T.isInitialized())
231 T.init(Name);
232 return T;
Dan Gohman5e843682008-07-14 18:19:29 +0000233}
234
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000235NamedRegionTimer::NamedRegionTimer(const std::string &Name)
236 : TimeRegion(getNamedRegionTimer(Name)) {}
237
Dan Gohman5e843682008-07-14 18:19:29 +0000238NamedRegionTimer::NamedRegionTimer(const std::string &Name,
239 const std::string &GroupName)
240 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Chris Lattner8f0d8242002-11-18 21:47:09 +0000241
Chris Lattner6c38a792002-10-01 19:36:54 +0000242//===----------------------------------------------------------------------===//
243// TimerGroup Implementation
244//===----------------------------------------------------------------------===//
245
Chris Lattner6c38a792002-10-01 19:36:54 +0000246void TimerGroup::removeTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000247 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9bb110f2010-03-29 20:35:01 +0000248 if (--NumTimers != 0 || TimersToPrint.empty())
249 return; // Don't print timing report.
250
251 // Sort the timers in descending order by amount of time taken.
Chris Lattnera782e752010-03-30 04:03:22 +0000252 std::sort(TimersToPrint.begin(), TimersToPrint.end());
Chris Lattner6c38a792002-10-01 19:36:54 +0000253
Chris Lattner9bb110f2010-03-29 20:35:01 +0000254 // Figure out how many spaces to indent TimerGroup name.
255 unsigned Padding = (80-Name.length())/2;
256 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Chris Lattner6c38a792002-10-01 19:36:54 +0000257
Chris Lattner9bb110f2010-03-29 20:35:01 +0000258 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000259
Chris Lattnera782e752010-03-30 04:03:22 +0000260 TimeRecord Total;
261 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
262 Total += TimersToPrint[i].first;
Misha Brukmanf976c852005-04-21 22:55:34 +0000263
Chris Lattnera782e752010-03-30 04:03:22 +0000264 // Print out timing header.
265 *OutStream << "===" << std::string(73, '-') << "===\n";
266 OutStream->indent(Padding) << Name << '\n';
267 *OutStream << "===" << std::string(73, '-') << "===\n";
Misha Brukmanf976c852005-04-21 22:55:34 +0000268
Chris Lattnera782e752010-03-30 04:03:22 +0000269 // If this is not an collection of ungrouped times, print the total time.
270 // Ungrouped timers don't really make sense to add up. We still print the
271 // TOTAL line to make the percentages make sense.
272 if (this != DefaultTimerGroup) {
273 *OutStream << " Total Execution Time: ";
274 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
275 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000276 }
Chris Lattnera782e752010-03-30 04:03:22 +0000277 *OutStream << "\n";
278
279 if (Total.getUserTime())
280 *OutStream << " ---User Time---";
281 if (Total.getSystemTime())
282 *OutStream << " --System Time--";
283 if (Total.getProcessTime())
284 *OutStream << " --User+System--";
285 *OutStream << " ---Wall Time---";
286 if (Total.getMemUsed())
287 *OutStream << " ---Mem---";
288 *OutStream << " --- Name ---\n";
289
290 // Loop through all of the timing data, printing it out.
291 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
292 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
293 Entry.first.print(Total, *OutStream);
294 *OutStream << Entry.second << '\n';
295 }
296
297 Total.print(Total, *OutStream);
298 *OutStream << "Total\n\n";
299 OutStream->flush();
Chris Lattner9bb110f2010-03-29 20:35:01 +0000300
301 TimersToPrint.clear();
302
Chris Lattner0ea86bc2010-03-29 21:24:52 +0000303 if (OutStream != &errs() && OutStream != &outs())
Chris Lattner0613edc2010-03-29 20:40:19 +0000304 delete OutStream; // Close the file.
Chris Lattner6c38a792002-10-01 19:36:54 +0000305}
Brian Gaeked0fde302003-11-11 22:41:34 +0000306
Owen Anderson46d9a642009-06-23 20:52:29 +0000307void TimerGroup::addTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000308 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson46d9a642009-06-23 20:52:29 +0000309 ++NumTimers;
310}
311
Chris Lattnera782e752010-03-30 04:03:22 +0000312void TimerGroup::addTimerToPrint(const TimeRecord &T, const std::string &Name) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000313 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnera782e752010-03-30 04:03:22 +0000314 TimersToPrint.push_back(std::make_pair(T, Name));
Owen Anderson46d9a642009-06-23 20:52:29 +0000315}
316