1 //===-- Timer.cpp - Interval Timing Support -------------------------------===//
3 // Interval Timing implementation.
5 //===----------------------------------------------------------------------===//
7 #include "Support/Timer.h"
8 #include "Support/CommandLine.h"
9 #include <sys/resource.h>
21 std::string LibSupportInfoOutputFilename;
25 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
26 "tracking (this may be slow)"),
29 cl::opt<std::string, true>
30 InfoOutputFilename("info-output-file",
31 cl::desc("File to append -stats and -timer output to"),
32 cl::Hidden, cl::location(LibSupportInfoOutputFilename));
35 static TimerGroup *DefaultTimerGroup = 0;
36 static TimerGroup *getDefaultTimerGroup() {
37 if (DefaultTimerGroup) return DefaultTimerGroup;
38 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
41 Timer::Timer(const std::string &N)
42 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
43 Started(false), TG(getDefaultTimerGroup()) {
47 Timer::Timer(const std::string &N, TimerGroup &tg)
48 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
49 Started(false), TG(&tg) {
53 Timer::Timer(const Timer &T) {
55 if (TG) TG->addTimer();
60 // Copy ctor, initialize with no TG member.
61 Timer::Timer(bool, const Timer &T) {
62 TG = T.TG; // Avoid assertion in operator=
63 operator=(T); // Copy contents
72 TG->addTimerToPrint(*this);
78 static long getMemUsage() {
81 struct mallinfo MI = mallinfo();
82 return MI.uordblks/*+MI.hblkhd*/;
89 double Elapsed, UserTime, SystemTime;
93 static TimeRecord getTimeRecord(bool Start) {
98 MemUsed = getMemUsage();
99 if (getrusage(RUSAGE_SELF, &RU))
100 perror("getrusage call failed: -time-passes info incorrect!");
105 MemUsed = getMemUsage();
106 if (getrusage(RUSAGE_SELF, &RU))
107 perror("getrusage call failed: -time-passes info incorrect!");
111 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
112 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
113 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
114 Result.MemUsed = MemUsed;
119 static std::vector<Timer*> ActiveTimers;
121 void Timer::startTimer() {
123 TimeRecord TR = getTimeRecord(true);
124 Elapsed -= TR.Elapsed;
125 UserTime -= TR.UserTime;
126 SystemTime -= TR.SystemTime;
127 MemUsed -= TR.MemUsed;
128 PeakMemBase = TR.MemUsed;
129 ActiveTimers.push_back(this);
132 void Timer::stopTimer() {
133 TimeRecord TR = getTimeRecord(false);
134 Elapsed += TR.Elapsed;
135 UserTime += TR.UserTime;
136 SystemTime += TR.SystemTime;
137 MemUsed += TR.MemUsed;
139 if (ActiveTimers.back() == this) {
140 ActiveTimers.pop_back();
142 std::vector<Timer*>::iterator I =
143 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
144 assert(I != ActiveTimers.end() && "stop but no startTimer?");
145 ActiveTimers.erase(I);
149 void Timer::sum(const Timer &T) {
150 Elapsed += T.Elapsed;
151 UserTime += T.UserTime;
152 SystemTime += T.SystemTime;
153 MemUsed += T.MemUsed;
154 PeakMem += T.PeakMem;
157 /// addPeakMemoryMeasurement - This method should be called whenever memory
158 /// usage needs to be checked. It adds a peak memory measurement to the
159 /// currently active timers, which will be printed when the timer group prints
161 void Timer::addPeakMemoryMeasurement() {
162 long MemUsed = getMemUsage();
164 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
165 E = ActiveTimers.end(); I != E; ++I)
166 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
170 //===----------------------------------------------------------------------===//
171 // TimerGroup Implementation
172 //===----------------------------------------------------------------------===//
174 // printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
175 // TotalWidth size, and B is the AfterDec size.
177 static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
179 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
180 OS.width(TotalWidth-AfterDec-1);
181 char OldFill = OS.fill();
183 OS << (int)Val; // Integer part;
187 unsigned ResultFieldSize = 1;
188 while (AfterDec--) ResultFieldSize *= 10;
189 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
193 static void printVal(double Val, double Total, std::ostream &OS) {
194 if (Total < 1e-7) // Avoid dividing by zero...
198 printAlignedFP(Val, 4, 7, OS);
200 printAlignedFP(Val*100/Total, 1, 5, OS);
205 void Timer::print(const Timer &Total, std::ostream &OS) {
207 printVal(UserTime, Total.UserTime, OS);
208 if (Total.SystemTime)
209 printVal(SystemTime, Total.SystemTime, OS);
210 if (Total.getProcessTime())
211 printVal(getProcessTime(), Total.getProcessTime(), OS);
212 printVal(Elapsed, Total.Elapsed, OS);
218 OS << MemUsed << " ";
223 OS << PeakMem << " ";
229 Started = false; // Once printed, don't print again
232 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
233 std::ostream *GetLibSupportInfoOutputFile() {
234 if (LibSupportInfoOutputFilename.empty())
236 if (LibSupportInfoOutputFilename == "-")
239 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
241 if (!Result->good()) {
242 std::cerr << "Error opening info-output-file '"
243 << LibSupportInfoOutputFilename << " for appending!\n";
251 void TimerGroup::removeTimer() {
252 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
253 // Sort the timers in descending order by amount of time taken...
254 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
255 std::greater<Timer>());
257 // Figure out how many spaces to indent TimerGroup name...
258 unsigned Padding = (80-Name.length())/2;
259 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
261 std::ostream *OutStream = GetLibSupportInfoOutputFile();
264 { // Scope to contain Total timer... don't allow total timer to drop us to
266 Timer Total("TOTAL");
268 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
269 Total.sum(TimersToPrint[i]);
271 // Print out timing header...
272 *OutStream << "===" << std::string(73, '-') << "===\n"
273 << std::string(Padding, ' ') << Name << "\n"
274 << "===" << std::string(73, '-')
275 << "===\n Total Execution Time: ";
277 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
278 *OutStream << " seconds (";
279 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
280 *OutStream << " wall clock)\n\n";
283 *OutStream << " ---User Time---";
284 if (Total.SystemTime)
285 *OutStream << " --System Time--";
286 if (Total.getProcessTime())
287 *OutStream << " --User+System--";
288 *OutStream << " ---Wall Time---";
289 if (Total.getMemUsed())
290 *OutStream << " ---Mem---";
291 if (Total.getPeakMem())
292 *OutStream << " -PeakMem-";
293 *OutStream << " --- Name ---\n";
295 // Loop through all of the timing data, printing it out...
296 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
297 TimersToPrint[i].print(Total, *OutStream);
299 Total.print(Total, *OutStream);
300 *OutStream << std::endl; // Flush output
304 TimersToPrint.clear();
306 if (OutStream != &std::cerr && OutStream != &std::cout)
307 delete OutStream; // Close the file...
310 // Delete default timer group!
311 if (NumTimers == 0 && this == DefaultTimerGroup) {
312 delete DefaultTimerGroup;
313 DefaultTimerGroup = 0;