1 //===-- Timer.cpp - Interval Timing Support -------------------------------===//
3 // Interval Timing implementation.
5 //===----------------------------------------------------------------------===//
7 #include "Support/Timer.h"
8 #include <sys/resource.h>
10 #include <sys/unistd.h>
17 static TimerGroup *DefaultTimerGroup = 0;
18 static TimerGroup *getDefaultTimerGroup() {
19 if (DefaultTimerGroup) return DefaultTimerGroup;
20 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
23 Timer::Timer(const std::string &N)
24 : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
25 Started(false), TG(getDefaultTimerGroup()) {
29 Timer::Timer(const std::string &N, TimerGroup &tg)
30 : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
31 Started(false), TG(&tg) {
35 Timer::Timer(const Timer &T) {
37 if (TG) TG->addTimer();
42 // Copy ctor, initialize with no TG member.
43 Timer::Timer(bool, const Timer &T) {
44 TG = T.TG; // Avoid assertion in operator=
45 operator=(T); // Copy contents
54 TG->addTimerToPrint(*this);
61 double Elapsed, UserTime, SystemTime;
65 static TimeRecord getTimeRecord() {
66 static unsigned long PageSize = 0;
70 PageSize = sysconf(_SC_PAGE_SIZE);
73 PageSize = sysconf(_SC_PAGESIZE);
75 PageSize = getpagesize();
83 if (getrusage(RUSAGE_SELF, &RU)) {
84 perror("getrusage call failed: -time-passes info incorrect!");
88 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
89 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
90 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
91 Result.MaxRSS = RU.ru_maxrss*PageSize;
96 void Timer::startTimer() {
98 TimeRecord TR = getTimeRecord();
99 Elapsed -= TR.Elapsed;
100 UserTime -= TR.UserTime;
101 SystemTime -= TR.SystemTime;
105 void Timer::stopTimer() {
106 TimeRecord TR = getTimeRecord();
107 Elapsed += TR.Elapsed;
108 UserTime += TR.UserTime;
109 SystemTime += TR.SystemTime;
113 void Timer::sum(const Timer &T) {
114 Elapsed += T.Elapsed;
115 UserTime += T.UserTime;
116 SystemTime += T.SystemTime;
120 //===----------------------------------------------------------------------===//
121 // TimerGroup Implementation
122 //===----------------------------------------------------------------------===//
124 static void printVal(double Val, double Total) {
125 if (Total < 1e-7) // Avoid dividing by zero...
126 fprintf(stderr, " ----- ");
128 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
131 void Timer::print(const Timer &Total) {
133 printVal(UserTime, Total.UserTime);
134 if (Total.SystemTime)
135 printVal(SystemTime, Total.SystemTime);
136 if (Total.getProcessTime())
137 printVal(getProcessTime(), Total.getProcessTime());
138 printVal(Elapsed, Total.Elapsed);
140 fprintf(stderr, " ");
143 std::cerr << MaxRSS << "\t";
144 std::cerr << Name << "\n";
146 Started = false; // Once printed, don't print again
150 void TimerGroup::removeTimer() {
151 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
152 // Sort the timers in descending order by amount of time taken...
153 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
154 std::greater<Timer>());
156 // Figure out how many spaces to indent TimerGroup name...
157 unsigned Padding = (80-Name.length())/2;
158 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
161 { // Scope to contain Total timer... don't allow total timer to drop us to
163 Timer Total("TOTAL");
165 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
166 Total.sum(TimersToPrint[i]);
168 // Print out timing header...
169 std::cerr << "===" << std::string(73, '-') << "===\n"
170 << std::string(Padding, ' ') << Name << "\n"
171 << "===" << std::string(73, '-')
172 << "===\n Total Execution Time: " << Total.getProcessTime()
173 << " seconds (" << Total.getWallTime()
174 << " wall clock)\n\n";
177 std::cerr << " ---User Time---";
178 if (Total.SystemTime)
179 std::cerr << " --System Time--";
180 if (Total.getProcessTime())
181 std::cerr << " --User+System--";
182 std::cerr << " ---Wall Time---";
184 if (Total.getMaxRSS())
185 std::cerr << " ---Mem---";
186 std::cerr << " --- Name ---\n";
188 // Loop through all of the timing data, printing it out...
189 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
190 TimersToPrint[i].print(Total);
193 std::cerr << std::endl; // Flush output
197 TimersToPrint.clear();
200 // Delete default timer group!
201 if (NumTimers == 0 && this == DefaultTimerGroup) {
202 delete DefaultTimerGroup;
203 DefaultTimerGroup = 0;