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>
18 static TimerGroup *DefaultTimerGroup = 0;
19 static TimerGroup *getDefaultTimerGroup() {
20 if (DefaultTimerGroup) return DefaultTimerGroup;
21 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
24 Timer::Timer(const std::string &N)
25 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
26 Started(false), TG(getDefaultTimerGroup()) {
30 Timer::Timer(const std::string &N, TimerGroup &tg)
31 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
32 Started(false), TG(&tg) {
36 Timer::Timer(const Timer &T) {
38 if (TG) TG->addTimer();
43 // Copy ctor, initialize with no TG member.
44 Timer::Timer(bool, const Timer &T) {
45 TG = T.TG; // Avoid assertion in operator=
46 operator=(T); // Copy contents
55 TG->addTimerToPrint(*this);
62 double Elapsed, UserTime, SystemTime;
66 static TimeRecord getTimeRecord() {
70 if (getrusage(RUSAGE_SELF, &RU)) {
71 perror("getrusage call failed: -time-passes info incorrect!");
75 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
76 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
77 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
78 Result.MemUsed = mallinfo().uordblks;
82 void Timer::startTimer() {
84 TimeRecord TR = getTimeRecord();
85 Elapsed -= TR.Elapsed;
86 UserTime -= TR.UserTime;
87 SystemTime -= TR.SystemTime;
88 MemUsed -= TR.MemUsed;
91 void Timer::stopTimer() {
92 TimeRecord TR = getTimeRecord();
93 Elapsed += TR.Elapsed;
94 UserTime += TR.UserTime;
95 SystemTime += TR.SystemTime;
96 MemUsed += TR.MemUsed;
99 void Timer::sum(const Timer &T) {
100 Elapsed += T.Elapsed;
101 UserTime += T.UserTime;
102 SystemTime += T.SystemTime;
103 MemUsed += T.MemUsed;
106 //===----------------------------------------------------------------------===//
107 // TimerGroup Implementation
108 //===----------------------------------------------------------------------===//
110 static void printVal(double Val, double Total) {
111 if (Total < 1e-7) // Avoid dividing by zero...
112 fprintf(stderr, " ----- ");
114 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
117 void Timer::print(const Timer &Total) {
119 printVal(UserTime, Total.UserTime);
120 if (Total.SystemTime)
121 printVal(SystemTime, Total.SystemTime);
122 if (Total.getProcessTime())
123 printVal(getProcessTime(), Total.getProcessTime());
124 printVal(Elapsed, Total.Elapsed);
126 fprintf(stderr, " ");
129 fprintf(stderr, " %8ld ", MemUsed);
130 std::cerr << Name << "\n";
132 Started = false; // Once printed, don't print again
136 void TimerGroup::removeTimer() {
137 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
138 // Sort the timers in descending order by amount of time taken...
139 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
140 std::greater<Timer>());
142 // Figure out how many spaces to indent TimerGroup name...
143 unsigned Padding = (80-Name.length())/2;
144 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
147 { // Scope to contain Total timer... don't allow total timer to drop us to
149 Timer Total("TOTAL");
151 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
152 Total.sum(TimersToPrint[i]);
154 // Print out timing header...
155 std::cerr << "===" << std::string(73, '-') << "===\n"
156 << std::string(Padding, ' ') << Name << "\n"
157 << "===" << std::string(73, '-')
158 << "===\n Total Execution Time: " << Total.getProcessTime()
159 << " seconds (" << Total.getWallTime()
160 << " wall clock)\n\n";
163 std::cerr << " ---User Time---";
164 if (Total.SystemTime)
165 std::cerr << " --System Time--";
166 if (Total.getProcessTime())
167 std::cerr << " --User+System--";
168 std::cerr << " ---Wall Time---";
169 if (Total.getMemUsed())
170 std::cerr << " ---Mem---";
171 std::cerr << " --- Name ---\n";
173 // Loop through all of the timing data, printing it out...
174 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
175 TimersToPrint[i].print(Total);
178 std::cerr << std::endl; // Flush output
182 TimersToPrint.clear();
185 // Delete default timer group!
186 if (NumTimers == 0 && this == DefaultTimerGroup) {
187 delete DefaultTimerGroup;
188 DefaultTimerGroup = 0;