blob: a214cf419acd02dccbf1b378ef3f43b65d815040 [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- 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>
Anand Shukla34008372002-10-04 23:57:01 +000011#include <unistd.h>
Chris Lattner9d4ef122002-11-04 00:32:44 +000012#include <malloc.h>
Chris Lattner6c38a792002-10-01 19:36:54 +000013#include <stdio.h>
14#include <iostream>
15#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000016#include <functional>
Chris Lattner6c38a792002-10-01 19:36:54 +000017
18static TimerGroup *DefaultTimerGroup = 0;
19static TimerGroup *getDefaultTimerGroup() {
20 if (DefaultTimerGroup) return DefaultTimerGroup;
21 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
22}
23
24Timer::Timer(const std::string &N)
Chris Lattner18eba912002-11-04 19:19:36 +000025 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000026 Started(false), TG(getDefaultTimerGroup()) {
27 TG->addTimer();
28}
29
30Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner18eba912002-11-04 19:19:36 +000031 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000032 Started(false), TG(&tg) {
33 TG->addTimer();
34}
35
36Timer::Timer(const Timer &T) {
37 TG = T.TG;
38 if (TG) TG->addTimer();
39 operator=(T);
40}
41
42
43// Copy ctor, initialize with no TG member.
44Timer::Timer(bool, const Timer &T) {
45 TG = T.TG; // Avoid assertion in operator=
46 operator=(T); // Copy contents
47 TG = 0;
48}
49
50
51Timer::~Timer() {
52 if (TG) {
53 if (Started) {
54 Started = false;
55 TG->addTimerToPrint(*this);
56 }
57 TG->removeTimer();
58 }
59}
60
61struct TimeRecord {
62 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000063 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000064};
65
66static TimeRecord getTimeRecord() {
Chris Lattner6c38a792002-10-01 19:36:54 +000067 struct rusage RU;
68 struct timeval T;
69 gettimeofday(&T, 0);
70 if (getrusage(RUSAGE_SELF, &RU)) {
71 perror("getrusage call failed: -time-passes info incorrect!");
72 }
73
74 TimeRecord Result;
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;
Chris Lattner6cd11f62002-11-04 19:01:41 +000078
79#ifndef __sparc__
80 struct mallinfo MI = mallinfo();
Chris Lattner18eba912002-11-04 19:19:36 +000081 Result.MemUsed = MI.uordblks;
Chris Lattner6cd11f62002-11-04 19:01:41 +000082#else
Chris Lattner18eba912002-11-04 19:19:36 +000083 Result.MemUsed = 0;
Chris Lattner6cd11f62002-11-04 19:01:41 +000084#endif
85
Chris Lattner6c38a792002-10-01 19:36:54 +000086 return Result;
87}
88
Chris Lattner6c38a792002-10-01 19:36:54 +000089void Timer::startTimer() {
90 Started = true;
91 TimeRecord TR = getTimeRecord();
92 Elapsed -= TR.Elapsed;
93 UserTime -= TR.UserTime;
94 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000095 MemUsed -= TR.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000096}
97
98void Timer::stopTimer() {
99 TimeRecord TR = getTimeRecord();
100 Elapsed += TR.Elapsed;
101 UserTime += TR.UserTime;
102 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000103 MemUsed += TR.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000104}
105
106void Timer::sum(const Timer &T) {
107 Elapsed += T.Elapsed;
108 UserTime += T.UserTime;
109 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000110 MemUsed += T.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000111}
112
113//===----------------------------------------------------------------------===//
114// TimerGroup Implementation
115//===----------------------------------------------------------------------===//
116
117static void printVal(double Val, double Total) {
118 if (Total < 1e-7) // Avoid dividing by zero...
119 fprintf(stderr, " ----- ");
120 else
121 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
122}
123
124void Timer::print(const Timer &Total) {
125 if (Total.UserTime)
126 printVal(UserTime, Total.UserTime);
127 if (Total.SystemTime)
128 printVal(SystemTime, Total.SystemTime);
129 if (Total.getProcessTime())
130 printVal(getProcessTime(), Total.getProcessTime());
131 printVal(Elapsed, Total.Elapsed);
132
133 fprintf(stderr, " ");
134
Chris Lattner18eba912002-11-04 19:19:36 +0000135 if (Total.MemUsed)
136 fprintf(stderr, " %8ld ", MemUsed);
Chris Lattner6c38a792002-10-01 19:36:54 +0000137 std::cerr << Name << "\n";
138
139 Started = false; // Once printed, don't print again
140}
141
142
143void TimerGroup::removeTimer() {
144 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
145 // Sort the timers in descending order by amount of time taken...
146 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
147 std::greater<Timer>());
148
149 // Figure out how many spaces to indent TimerGroup name...
150 unsigned Padding = (80-Name.length())/2;
151 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
152
153 ++NumTimers;
154 { // Scope to contain Total timer... don't allow total timer to drop us to
155 // zero timers...
156 Timer Total("TOTAL");
157
158 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
159 Total.sum(TimersToPrint[i]);
160
161 // Print out timing header...
162 std::cerr << "===" << std::string(73, '-') << "===\n"
163 << std::string(Padding, ' ') << Name << "\n"
164 << "===" << std::string(73, '-')
165 << "===\n Total Execution Time: " << Total.getProcessTime()
166 << " seconds (" << Total.getWallTime()
167 << " wall clock)\n\n";
168
169 if (Total.UserTime)
170 std::cerr << " ---User Time---";
171 if (Total.SystemTime)
172 std::cerr << " --System Time--";
173 if (Total.getProcessTime())
174 std::cerr << " --User+System--";
175 std::cerr << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000176 if (Total.getMemUsed())
Chris Lattner9d4ef122002-11-04 00:32:44 +0000177 std::cerr << " ---Mem---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000178 std::cerr << " --- Name ---\n";
179
180 // Loop through all of the timing data, printing it out...
181 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
182 TimersToPrint[i].print(Total);
183
184 Total.print(Total);
185 std::cerr << std::endl; // Flush output
186 }
187 --NumTimers;
188
189 TimersToPrint.clear();
190 }
191
192 // Delete default timer group!
193 if (NumTimers == 0 && this == DefaultTimerGroup) {
194 delete DefaultTimerGroup;
195 DefaultTimerGroup = 0;
196 }
197}