blob: 17fea8b93c88b30370bd2ed7e88476de7f09528d [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
Chris Lattner8f0d8242002-11-18 21:47:09 +000018// getNumBytesToNotCount - This function is supposed to return the number of
19// bytes that are to be considered not allocated, even though malloc thinks they
20// are allocated.
21//
22static unsigned getNumBytesToNotCount();
23
Chris Lattner6c38a792002-10-01 19:36:54 +000024static TimerGroup *DefaultTimerGroup = 0;
25static TimerGroup *getDefaultTimerGroup() {
26 if (DefaultTimerGroup) return DefaultTimerGroup;
27 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
28}
29
30Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000031 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000032 Started(false), TG(getDefaultTimerGroup()) {
33 TG->addTimer();
34}
35
36Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000037 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000038 Started(false), TG(&tg) {
39 TG->addTimer();
40}
41
42Timer::Timer(const Timer &T) {
43 TG = T.TG;
44 if (TG) TG->addTimer();
45 operator=(T);
46}
47
48
49// Copy ctor, initialize with no TG member.
50Timer::Timer(bool, const Timer &T) {
51 TG = T.TG; // Avoid assertion in operator=
52 operator=(T); // Copy contents
53 TG = 0;
54}
55
56
57Timer::~Timer() {
58 if (TG) {
59 if (Started) {
60 Started = false;
61 TG->addTimerToPrint(*this);
62 }
63 TG->removeTimer();
64 }
65}
66
Chris Lattner8f0d8242002-11-18 21:47:09 +000067static long getMemUsage() {
68 struct mallinfo MI = mallinfo();
69 return MI.uordblks/*+MI.hblkhd-getNumBytesToNotCount()*/;
70}
71
Chris Lattner6c38a792002-10-01 19:36:54 +000072struct TimeRecord {
73 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000074 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000075};
76
Chris Lattner8f0d8242002-11-18 21:47:09 +000077static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +000078 struct rusage RU;
79 struct timeval T;
Chris Lattner8f0d8242002-11-18 21:47:09 +000080 long MemUsed;
81 if (Start) {
82 MemUsed = getMemUsage();
83 if (getrusage(RUSAGE_SELF, &RU))
84 perror("getrusage call failed: -time-passes info incorrect!");
85 }
Chris Lattner6c38a792002-10-01 19:36:54 +000086 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +000087
88 if (!Start) {
89 MemUsed = getMemUsage();
90 if (getrusage(RUSAGE_SELF, &RU))
91 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +000092 }
93
94 TimeRecord Result;
95 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
96 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
97 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000098 Result.MemUsed = MemUsed;
99
Chris Lattner6c38a792002-10-01 19:36:54 +0000100 return Result;
101}
102
Chris Lattner8f0d8242002-11-18 21:47:09 +0000103static std::vector<Timer*> ActiveTimers;
104
Chris Lattner6c38a792002-10-01 19:36:54 +0000105void Timer::startTimer() {
106 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000107 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000108 Elapsed -= TR.Elapsed;
109 UserTime -= TR.UserTime;
110 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000111 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000112 PeakMemBase = TR.MemUsed;
113 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000114}
115
116void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000117 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000118 Elapsed += TR.Elapsed;
119 UserTime += TR.UserTime;
120 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000121 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000122
123 if (ActiveTimers.back() == this) {
124 ActiveTimers.pop_back();
125 } else {
126 std::vector<Timer*>::iterator I =
127 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
128 assert(I != ActiveTimers.end() && "stop but no startTimer?");
129 ActiveTimers.erase(I);
130 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000131}
132
133void Timer::sum(const Timer &T) {
134 Elapsed += T.Elapsed;
135 UserTime += T.UserTime;
136 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000137 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000138 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000139}
140
Chris Lattner8f0d8242002-11-18 21:47:09 +0000141/// addPeakMemoryMeasurement - This method should be called whenever memory
142/// usage needs to be checked. It adds a peak memory measurement to the
143/// currently active timers, which will be printed when the timer group prints
144///
145void Timer::addPeakMemoryMeasurement() {
146 long MemUsed = getMemUsage();
147
148 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
149 E = ActiveTimers.end(); I != E; ++I)
150 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
151}
152
153
Chris Lattner6c38a792002-10-01 19:36:54 +0000154//===----------------------------------------------------------------------===//
155// TimerGroup Implementation
156//===----------------------------------------------------------------------===//
157
158static void printVal(double Val, double Total) {
159 if (Total < 1e-7) // Avoid dividing by zero...
160 fprintf(stderr, " ----- ");
161 else
162 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
163}
164
165void Timer::print(const Timer &Total) {
166 if (Total.UserTime)
167 printVal(UserTime, Total.UserTime);
168 if (Total.SystemTime)
169 printVal(SystemTime, Total.SystemTime);
170 if (Total.getProcessTime())
171 printVal(getProcessTime(), Total.getProcessTime());
172 printVal(Elapsed, Total.Elapsed);
173
174 fprintf(stderr, " ");
175
Chris Lattner18eba912002-11-04 19:19:36 +0000176 if (Total.MemUsed)
177 fprintf(stderr, " %8ld ", MemUsed);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000178 if (Total.PeakMem) {
179 if (PeakMem)
180 fprintf(stderr, " %8ld ", PeakMem);
181 else
182 fprintf(stderr, " ");
183 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000184 std::cerr << Name << "\n";
185
186 Started = false; // Once printed, don't print again
187}
188
189
190void TimerGroup::removeTimer() {
191 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
192 // Sort the timers in descending order by amount of time taken...
193 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
194 std::greater<Timer>());
195
196 // Figure out how many spaces to indent TimerGroup name...
197 unsigned Padding = (80-Name.length())/2;
198 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
199
200 ++NumTimers;
201 { // Scope to contain Total timer... don't allow total timer to drop us to
202 // zero timers...
203 Timer Total("TOTAL");
204
205 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
206 Total.sum(TimersToPrint[i]);
207
208 // Print out timing header...
209 std::cerr << "===" << std::string(73, '-') << "===\n"
210 << std::string(Padding, ' ') << Name << "\n"
211 << "===" << std::string(73, '-')
212 << "===\n Total Execution Time: " << Total.getProcessTime()
213 << " seconds (" << Total.getWallTime()
214 << " wall clock)\n\n";
215
216 if (Total.UserTime)
217 std::cerr << " ---User Time---";
218 if (Total.SystemTime)
219 std::cerr << " --System Time--";
220 if (Total.getProcessTime())
221 std::cerr << " --User+System--";
222 std::cerr << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000223 if (Total.getMemUsed())
Chris Lattner9d4ef122002-11-04 00:32:44 +0000224 std::cerr << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000225 if (Total.getPeakMem())
226 std::cerr << " -PeakMem-";
Chris Lattner6c38a792002-10-01 19:36:54 +0000227 std::cerr << " --- Name ---\n";
228
229 // Loop through all of the timing data, printing it out...
230 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
231 TimersToPrint[i].print(Total);
232
233 Total.print(Total);
234 std::cerr << std::endl; // Flush output
235 }
236 --NumTimers;
237
238 TimersToPrint.clear();
239 }
240
241 // Delete default timer group!
242 if (NumTimers == 0 && this == DefaultTimerGroup) {
243 delete DefaultTimerGroup;
244 DefaultTimerGroup = 0;
245 }
246}
Chris Lattner8f0d8242002-11-18 21:47:09 +0000247
248
249
250#if (__GNUC__ == 3) && (__GNUC_MINOR__ == 2) && (__GNUC_PATCHLEVEL__ == 0)
251// If we have GCC 3.2.0, we can calculate pool allocation bookkeeping info
252#define HAVE_POOL
253extern "C" {
254 // Taken from GCC 3.2's stl_alloc.h file:
255 enum {_ALIGN = 8, _MAX_BYTES = 128, NFREE = _MAX_BYTES / _ALIGN};
256 struct FreeList { FreeList *Next; };
257
258 FreeList *_ZNSt24__default_alloc_templateILb1ELi0EE12_S_free_listE[NFREE];
259 char *_ZNSt24__default_alloc_templateILb1ELi0EE13_S_start_freeE;
260 char *_ZNSt24__default_alloc_templateILb1ELi0EE11_S_end_freeE;
261 size_t _ZNSt24__default_alloc_templateILb1ELi0EE12_S_heap_sizeE;
262
263 // Make the symbols possible to use...
264 FreeList* (&TheFreeList)[NFREE] = _ZNSt24__default_alloc_templateILb1ELi0EE12_S_free_listE;
265 char * &StartFree = _ZNSt24__default_alloc_templateILb1ELi0EE13_S_start_freeE;
266 char * &EndFree = _ZNSt24__default_alloc_templateILb1ELi0EE11_S_end_freeE;
267 size_t &HeapSize = _ZNSt24__default_alloc_templateILb1ELi0EE12_S_heap_sizeE;
268}
269#endif
270
271// getNumBytesToNotCount - This function is supposed to return the number of
272// bytes that are to be considered not allocated, even though malloc thinks they
273// are allocated.
274//
275static unsigned getNumBytesToNotCount() {
276#ifdef HAVE_POOL
277 // If we have GCC 3.2.0, we can subtract off pool allocation bookkeeping info
278
279 // Size of the free slab section...
280 unsigned FreePoolMem = (unsigned)(EndFree-StartFree);
281
282 // Walk all of the free lists, adding memory to the free counter whenever we
283 // have a free bucket.
284 for (unsigned i = 0; i != NFREE; ++i) {
285 unsigned NumEntries = 0;
286 for (FreeList *FL = TheFreeList[i]; FL; ++NumEntries, FL = FL->Next)
287 /*empty*/ ;
288
289#if 0
290 if (NumEntries)
291 std::cerr << " For Size[" << (i+1)*_ALIGN << "]: " << NumEntries
292 << " Free entries\n";
293#endif
294 FreePoolMem += NumEntries*(i+1)*_ALIGN;
295 }
296 return FreePoolMem;
297
298#else
299#warning "Don't know how to avoid pool allocation accounting overhead for this"
300#warning " compiler: Space usage numbers (with -time-passes) may be off!"
301 return 0;
302#endif
303}