fb38fcb6fe69b5e53ee7d269411bd7b6a9e69e54
[oota-llvm.git] / support / lib / Support / Timer.cpp
1 //===-- Timer.cpp - Interval Timing Support -------------------------------===//
2 //
3 // Interval Timing implementation.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "Support/Timer.h"
8 #include <sys/resource.h>
9 #include <sys/time.h>
10 #include <sys/unistd.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <iostream>
14 #include <algorithm>
15
16 static TimerGroup *DefaultTimerGroup = 0;
17 static TimerGroup *getDefaultTimerGroup() {
18   if (DefaultTimerGroup) return DefaultTimerGroup;
19   return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
20 }
21
22 Timer::Timer(const std::string &N)
23   : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
24     Started(false), TG(getDefaultTimerGroup()) {
25   TG->addTimer();
26 }
27
28 Timer::Timer(const std::string &N, TimerGroup &tg)
29   : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
30     Started(false), TG(&tg) {
31   TG->addTimer();
32 }
33
34 Timer::Timer(const Timer &T) {
35   TG = T.TG;
36   if (TG) TG->addTimer();
37   operator=(T);
38 }
39
40
41 // Copy ctor, initialize with no TG member.
42 Timer::Timer(bool, const Timer &T) {
43   TG = T.TG;     // Avoid assertion in operator=
44   operator=(T);  // Copy contents
45   TG = 0;
46 }
47
48
49 Timer::~Timer() {
50   if (TG) {
51     if (Started) {
52       Started = false;
53       TG->addTimerToPrint(*this);
54     }
55     TG->removeTimer();
56   }
57 }
58
59 struct TimeRecord {
60   double Elapsed, UserTime, SystemTime;
61   unsigned long MaxRSS;
62 };
63
64 static TimeRecord getTimeRecord() {
65   static unsigned long PageSize = 0;
66
67   if (PageSize == 0) {
68 #ifdef _SC_PAGE_SIZE
69     PageSize = sysconf(_SC_PAGE_SIZE);
70 #else
71 #ifdef _SC_PAGESIZE
72     PageSize = sysconf(_SC_PAGESIZE);
73 #else
74     PageSize = getpagesize();
75 #endif
76 #endif
77   }
78
79   struct rusage RU;
80   struct timeval T;
81   gettimeofday(&T, 0);
82   if (getrusage(RUSAGE_SELF, &RU)) {
83     perror("getrusage call failed: -time-passes info incorrect!");
84   }
85
86   TimeRecord Result;
87   Result.Elapsed    =           T.tv_sec +           T.tv_usec/1000000.0;
88   Result.UserTime   = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
89   Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
90   Result.MaxRSS = RU.ru_maxrss*PageSize;
91   return Result;
92 }
93
94
95 void Timer::startTimer() {
96   Started = true;
97   TimeRecord TR = getTimeRecord();
98   Elapsed    -= TR.Elapsed;
99   UserTime   -= TR.UserTime;
100   SystemTime -= TR.SystemTime;
101   MaxRSS     -= TR.MaxRSS;
102 }
103
104 void Timer::stopTimer() {
105   TimeRecord TR = getTimeRecord();
106   Elapsed    += TR.Elapsed;
107   UserTime   += TR.UserTime;
108   SystemTime += TR.SystemTime;
109   MaxRSS     += TR.MaxRSS;
110 }
111
112 void Timer::sum(const Timer &T) {
113   Elapsed    += T.Elapsed;
114   UserTime   += T.UserTime;
115   SystemTime += T.SystemTime;
116   MaxRSS     += T.MaxRSS;
117 }
118
119 //===----------------------------------------------------------------------===//
120 //   TimerGroup Implementation
121 //===----------------------------------------------------------------------===//
122
123 static void printVal(double Val, double Total) {
124   if (Total < 1e-7)   // Avoid dividing by zero...
125     fprintf(stderr, "        -----     ");
126   else
127     fprintf(stderr, "  %7.4f (%5.1f%%)", Val, Val*100/Total);
128 }
129
130 void Timer::print(const Timer &Total) {
131   if (Total.UserTime)
132     printVal(UserTime, Total.UserTime);
133   if (Total.SystemTime)
134     printVal(SystemTime, Total.SystemTime);
135   if (Total.getProcessTime())
136     printVal(getProcessTime(), Total.getProcessTime());
137   printVal(Elapsed, Total.Elapsed);
138   
139   fprintf(stderr, "  ");
140
141   if (Total.MaxRSS)
142     std::cerr << MaxRSS << "\t";
143   std::cerr << Name << "\n";
144
145   Started = false;  // Once printed, don't print again
146 }
147
148
149 void TimerGroup::removeTimer() {
150   if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
151     // Sort the timers in descending order by amount of time taken...
152     std::sort(TimersToPrint.begin(), TimersToPrint.end(),
153               std::greater<Timer>());
154
155     // Figure out how many spaces to indent TimerGroup name...
156     unsigned Padding = (80-Name.length())/2;
157     if (Padding > 80) Padding = 0;         // Don't allow "negative" numbers
158
159     ++NumTimers;
160     {  // Scope to contain Total timer... don't allow total timer to drop us to
161        // zero timers...
162       Timer Total("TOTAL");
163   
164       for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
165         Total.sum(TimersToPrint[i]);
166       
167       // Print out timing header...
168       std::cerr << "===" << std::string(73, '-') << "===\n"
169                 << std::string(Padding, ' ') << Name << "\n"
170                 << "===" << std::string(73, '-')
171                 << "===\n  Total Execution Time: " << Total.getProcessTime()
172                 << " seconds (" << Total.getWallTime()
173                 << " wall clock)\n\n";
174
175       if (Total.UserTime)
176         std::cerr << "   ---User Time---";
177       if (Total.SystemTime)
178         std::cerr << "   --System Time--";
179       if (Total.getProcessTime())
180         std::cerr << "   --User+System--";
181       std::cerr << "   ---Wall Time---";
182       
183       if (Total.getMaxRSS())
184         std::cerr << " ---Mem---";
185       std::cerr << "  --- Name ---\n";
186       
187       // Loop through all of the timing data, printing it out...
188       for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
189         TimersToPrint[i].print(Total);
190     
191       Total.print(Total);
192       std::cerr << std::endl;  // Flush output
193     }
194     --NumTimers;
195
196     TimersToPrint.clear();
197   }
198
199   // Delete default timer group!
200   if (NumTimers == 0 && this == DefaultTimerGroup) {
201     delete DefaultTimerGroup;
202     DefaultTimerGroup = 0;
203   }
204 }