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>
16 static TimerGroup *DefaultTimerGroup = 0;
17 static TimerGroup *getDefaultTimerGroup() {
18 if (DefaultTimerGroup) return DefaultTimerGroup;
19 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
22 Timer::Timer(const std::string &N)
23 : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
24 Started(false), TG(getDefaultTimerGroup()) {
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) {
34 Timer::Timer(const Timer &T) {
36 if (TG) TG->addTimer();
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
53 TG->addTimerToPrint(*this);
60 double Elapsed, UserTime, SystemTime;
64 static TimeRecord getTimeRecord() {
65 static unsigned long PageSize = 0;
69 PageSize = sysconf(_SC_PAGE_SIZE);
72 PageSize = sysconf(_SC_PAGESIZE);
74 PageSize = getpagesize();
82 if (getrusage(RUSAGE_SELF, &RU)) {
83 perror("getrusage call failed: -time-passes info incorrect!");
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;
95 void Timer::startTimer() {
97 TimeRecord TR = getTimeRecord();
98 Elapsed -= TR.Elapsed;
99 UserTime -= TR.UserTime;
100 SystemTime -= TR.SystemTime;
104 void Timer::stopTimer() {
105 TimeRecord TR = getTimeRecord();
106 Elapsed += TR.Elapsed;
107 UserTime += TR.UserTime;
108 SystemTime += TR.SystemTime;
112 void Timer::sum(const Timer &T) {
113 Elapsed += T.Elapsed;
114 UserTime += T.UserTime;
115 SystemTime += T.SystemTime;
119 //===----------------------------------------------------------------------===//
120 // TimerGroup Implementation
121 //===----------------------------------------------------------------------===//
123 static void printVal(double Val, double Total) {
124 if (Total < 1e-7) // Avoid dividing by zero...
125 fprintf(stderr, " ----- ");
127 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
130 void Timer::print(const Timer &Total) {
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);
139 fprintf(stderr, " ");
142 std::cerr << MaxRSS << "\t";
143 std::cerr << Name << "\n";
145 Started = false; // Once printed, don't print again
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>());
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
160 { // Scope to contain Total timer... don't allow total timer to drop us to
162 Timer Total("TOTAL");
164 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
165 Total.sum(TimersToPrint[i]);
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";
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---";
183 if (Total.getMaxRSS())
184 std::cerr << " ---Mem---";
185 std::cerr << " --- Name ---\n";
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);
192 std::cerr << std::endl; // Flush output
196 TimersToPrint.clear();
199 // Delete default timer group!
200 if (NumTimers == 0 && this == DefaultTimerGroup) {
201 delete DefaultTimerGroup;
202 DefaultTimerGroup = 0;