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>
15 static TimerGroup *DefaultTimerGroup = 0;
16 static TimerGroup *getDefaultTimerGroup() {
17 if (DefaultTimerGroup) return DefaultTimerGroup;
18 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
21 Timer::Timer(const std::string &N)
22 : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
23 Started(false), TG(getDefaultTimerGroup()) {
27 Timer::Timer(const std::string &N, TimerGroup &tg)
28 : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
29 Started(false), TG(&tg) {
33 Timer::Timer(const Timer &T) {
35 if (TG) TG->addTimer();
40 // Copy ctor, initialize with no TG member.
41 Timer::Timer(bool, const Timer &T) {
42 TG = T.TG; // Avoid assertion in operator=
43 operator=(T); // Copy contents
52 TG->addTimerToPrint(*this);
59 double Elapsed, UserTime, SystemTime;
63 static TimeRecord getTimeRecord() {
64 static unsigned long PageSize = 0;
68 PageSize = sysconf(_SC_PAGE_SIZE);
71 PageSize = sysconf(_SC_PAGESIZE);
73 PageSize = getpagesize();
81 if (getrusage(RUSAGE_SELF, &RU)) {
82 perror("getrusage call failed: -time-passes info incorrect!");
86 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
87 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
88 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
89 Result.MaxRSS = RU.ru_maxrss*PageSize;
94 void Timer::startTimer() {
96 TimeRecord TR = getTimeRecord();
97 Elapsed -= TR.Elapsed;
98 UserTime -= TR.UserTime;
99 SystemTime -= TR.SystemTime;
103 void Timer::stopTimer() {
104 TimeRecord TR = getTimeRecord();
105 Elapsed += TR.Elapsed;
106 UserTime += TR.UserTime;
107 SystemTime += TR.SystemTime;
111 void Timer::sum(const Timer &T) {
112 Elapsed += T.Elapsed;
113 UserTime += T.UserTime;
114 SystemTime += T.SystemTime;
118 //===----------------------------------------------------------------------===//
119 // TimerGroup Implementation
120 //===----------------------------------------------------------------------===//
122 static void printVal(double Val, double Total) {
123 if (Total < 1e-7) // Avoid dividing by zero...
124 fprintf(stderr, " ----- ");
126 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
129 void Timer::print(const Timer &Total) {
131 printVal(UserTime, Total.UserTime);
132 if (Total.SystemTime)
133 printVal(SystemTime, Total.SystemTime);
134 if (Total.getProcessTime())
135 printVal(getProcessTime(), Total.getProcessTime());
136 printVal(Elapsed, Total.Elapsed);
138 fprintf(stderr, " ");
141 std::cerr << MaxRSS << "\t";
142 std::cerr << Name << "\n";
144 Started = false; // Once printed, don't print again
148 void TimerGroup::removeTimer() {
149 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
150 // Sort the timers in descending order by amount of time taken...
151 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
152 std::greater<Timer>());
154 // Figure out how many spaces to indent TimerGroup name...
155 unsigned Padding = (80-Name.length())/2;
156 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
159 { // Scope to contain Total timer... don't allow total timer to drop us to
161 Timer Total("TOTAL");
163 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
164 Total.sum(TimersToPrint[i]);
166 // Print out timing header...
167 std::cerr << "===" << std::string(73, '-') << "===\n"
168 << std::string(Padding, ' ') << Name << "\n"
169 << "===" << std::string(73, '-')
170 << "===\n Total Execution Time: " << Total.getProcessTime()
171 << " seconds (" << Total.getWallTime()
172 << " wall clock)\n\n";
175 std::cerr << " ---User Time---";
176 if (Total.SystemTime)
177 std::cerr << " --System Time--";
178 if (Total.getProcessTime())
179 std::cerr << " --User+System--";
180 std::cerr << " ---Wall Time---";
182 if (Total.getMaxRSS())
183 std::cerr << " ---Mem---";
184 std::cerr << " --- Name ---\n";
186 // Loop through all of the timing data, printing it out...
187 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
188 TimersToPrint[i].print(Total);
191 std::cerr << std::endl; // Flush output
195 TimersToPrint.clear();
198 // Delete default timer group!
199 if (NumTimers == 0 && this == DefaultTimerGroup) {
200 delete DefaultTimerGroup;
201 DefaultTimerGroup = 0;