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>
11 #include <sys/unistd.h>
21 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
22 "tracking (this may be slow)"),
26 static TimerGroup *DefaultTimerGroup = 0;
27 static TimerGroup *getDefaultTimerGroup() {
28 if (DefaultTimerGroup) return DefaultTimerGroup;
29 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
32 Timer::Timer(const std::string &N)
33 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
34 Started(false), TG(getDefaultTimerGroup()) {
38 Timer::Timer(const std::string &N, TimerGroup &tg)
39 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
40 Started(false), TG(&tg) {
44 Timer::Timer(const Timer &T) {
46 if (TG) TG->addTimer();
51 // Copy ctor, initialize with no TG member.
52 Timer::Timer(bool, const Timer &T) {
53 TG = T.TG; // Avoid assertion in operator=
54 operator=(T); // Copy contents
63 TG->addTimerToPrint(*this);
69 static long getMemUsage() {
71 struct mallinfo MI = mallinfo();
72 return MI.uordblks/*+MI.hblkhd*/;
79 double Elapsed, UserTime, SystemTime;
83 static TimeRecord getTimeRecord(bool Start) {
88 MemUsed = getMemUsage();
89 if (getrusage(RUSAGE_SELF, &RU))
90 perror("getrusage call failed: -time-passes info incorrect!");
95 MemUsed = getMemUsage();
96 if (getrusage(RUSAGE_SELF, &RU))
97 perror("getrusage call failed: -time-passes info incorrect!");
101 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
102 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
103 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
104 Result.MemUsed = MemUsed;
109 static std::vector<Timer*> ActiveTimers;
111 void Timer::startTimer() {
113 TimeRecord TR = getTimeRecord(true);
114 Elapsed -= TR.Elapsed;
115 UserTime -= TR.UserTime;
116 SystemTime -= TR.SystemTime;
117 MemUsed -= TR.MemUsed;
118 PeakMemBase = TR.MemUsed;
119 ActiveTimers.push_back(this);
122 void Timer::stopTimer() {
123 TimeRecord TR = getTimeRecord(false);
124 Elapsed += TR.Elapsed;
125 UserTime += TR.UserTime;
126 SystemTime += TR.SystemTime;
127 MemUsed += TR.MemUsed;
129 if (ActiveTimers.back() == this) {
130 ActiveTimers.pop_back();
132 std::vector<Timer*>::iterator I =
133 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
134 assert(I != ActiveTimers.end() && "stop but no startTimer?");
135 ActiveTimers.erase(I);
139 void Timer::sum(const Timer &T) {
140 Elapsed += T.Elapsed;
141 UserTime += T.UserTime;
142 SystemTime += T.SystemTime;
143 MemUsed += T.MemUsed;
144 PeakMem += T.PeakMem;
147 /// addPeakMemoryMeasurement - This method should be called whenever memory
148 /// usage needs to be checked. It adds a peak memory measurement to the
149 /// currently active timers, which will be printed when the timer group prints
151 void Timer::addPeakMemoryMeasurement() {
152 long MemUsed = getMemUsage();
154 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
155 E = ActiveTimers.end(); I != E; ++I)
156 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
160 //===----------------------------------------------------------------------===//
161 // TimerGroup Implementation
162 //===----------------------------------------------------------------------===//
164 static void printVal(double Val, double Total) {
165 if (Total < 1e-7) // Avoid dividing by zero...
166 fprintf(stderr, " ----- ");
168 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
171 void Timer::print(const Timer &Total) {
173 printVal(UserTime, Total.UserTime);
174 if (Total.SystemTime)
175 printVal(SystemTime, Total.SystemTime);
176 if (Total.getProcessTime())
177 printVal(getProcessTime(), Total.getProcessTime());
178 printVal(Elapsed, Total.Elapsed);
180 fprintf(stderr, " ");
183 fprintf(stderr, " %8ld ", MemUsed);
186 fprintf(stderr, " %8ld ", PeakMem);
188 fprintf(stderr, " ");
190 std::cerr << Name << "\n";
192 Started = false; // Once printed, don't print again
196 void TimerGroup::removeTimer() {
197 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
198 // Sort the timers in descending order by amount of time taken...
199 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
200 std::greater<Timer>());
202 // Figure out how many spaces to indent TimerGroup name...
203 unsigned Padding = (80-Name.length())/2;
204 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
207 { // Scope to contain Total timer... don't allow total timer to drop us to
209 Timer Total("TOTAL");
211 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
212 Total.sum(TimersToPrint[i]);
214 // Print out timing header...
215 std::cerr << "===" << std::string(73, '-') << "===\n"
216 << std::string(Padding, ' ') << Name << "\n"
217 << "===" << std::string(73, '-')
218 << "===\n Total Execution Time: ";
220 // Hack for GCC 2.96... :( it doesn't support manipulators!
221 fprintf(stderr, "%.4f seconds (%.4f wall clock)\n\n",
222 Total.getProcessTime(), Total.getWallTime());
225 std::cerr << " ---User Time---";
226 if (Total.SystemTime)
227 std::cerr << " --System Time--";
228 if (Total.getProcessTime())
229 std::cerr << " --User+System--";
230 std::cerr << " ---Wall Time---";
231 if (Total.getMemUsed())
232 std::cerr << " ---Mem---";
233 if (Total.getPeakMem())
234 std::cerr << " -PeakMem-";
235 std::cerr << " --- Name ---\n";
237 // Loop through all of the timing data, printing it out...
238 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
239 TimersToPrint[i].print(Total);
242 std::cerr << std::endl; // Flush output
246 TimersToPrint.clear();
249 // Delete default timer group!
250 if (NumTimers == 0 && this == DefaultTimerGroup) {
251 delete DefaultTimerGroup;
252 DefaultTimerGroup = 0;