1 //===-- Timer.cpp - Interval Timing Support -------------------------------===//
3 // Interval Timing implementation.
5 //===----------------------------------------------------------------------===//
7 #include "Support/Timer.h"
8 #include "Support/CommandLine.h"
9 #include <sys/resource.h>
11 #include <sys/unistd.h>
21 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
22 "tracking (this may be slow)"),
26 // getNumBytesToNotCount - This function is supposed to return the number of
27 // bytes that are to be considered not allocated, even though malloc thinks they
30 static unsigned getNumBytesToNotCount();
32 static TimerGroup *DefaultTimerGroup = 0;
33 static TimerGroup *getDefaultTimerGroup() {
34 if (DefaultTimerGroup) return DefaultTimerGroup;
35 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
38 Timer::Timer(const std::string &N)
39 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
40 Started(false), TG(getDefaultTimerGroup()) {
44 Timer::Timer(const std::string &N, TimerGroup &tg)
45 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
46 Started(false), TG(&tg) {
50 Timer::Timer(const Timer &T) {
52 if (TG) TG->addTimer();
57 // Copy ctor, initialize with no TG member.
58 Timer::Timer(bool, const Timer &T) {
59 TG = T.TG; // Avoid assertion in operator=
60 operator=(T); // Copy contents
69 TG->addTimerToPrint(*this);
75 static long getMemUsage() {
77 struct mallinfo MI = mallinfo();
78 return MI.uordblks/*+MI.hblkhd-getNumBytesToNotCount()*/;
85 double Elapsed, UserTime, SystemTime;
89 static TimeRecord getTimeRecord(bool Start) {
94 MemUsed = getMemUsage();
95 if (getrusage(RUSAGE_SELF, &RU))
96 perror("getrusage call failed: -time-passes info incorrect!");
101 MemUsed = getMemUsage();
102 if (getrusage(RUSAGE_SELF, &RU))
103 perror("getrusage call failed: -time-passes info incorrect!");
107 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
108 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
109 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
110 Result.MemUsed = MemUsed;
115 static std::vector<Timer*> ActiveTimers;
117 void Timer::startTimer() {
119 TimeRecord TR = getTimeRecord(true);
120 Elapsed -= TR.Elapsed;
121 UserTime -= TR.UserTime;
122 SystemTime -= TR.SystemTime;
123 MemUsed -= TR.MemUsed;
124 PeakMemBase = TR.MemUsed;
125 ActiveTimers.push_back(this);
128 void Timer::stopTimer() {
129 TimeRecord TR = getTimeRecord(false);
130 Elapsed += TR.Elapsed;
131 UserTime += TR.UserTime;
132 SystemTime += TR.SystemTime;
133 MemUsed += TR.MemUsed;
135 if (ActiveTimers.back() == this) {
136 ActiveTimers.pop_back();
138 std::vector<Timer*>::iterator I =
139 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
140 assert(I != ActiveTimers.end() && "stop but no startTimer?");
141 ActiveTimers.erase(I);
145 void Timer::sum(const Timer &T) {
146 Elapsed += T.Elapsed;
147 UserTime += T.UserTime;
148 SystemTime += T.SystemTime;
149 MemUsed += T.MemUsed;
150 PeakMem += T.PeakMem;
153 /// addPeakMemoryMeasurement - This method should be called whenever memory
154 /// usage needs to be checked. It adds a peak memory measurement to the
155 /// currently active timers, which will be printed when the timer group prints
157 void Timer::addPeakMemoryMeasurement() {
158 long MemUsed = getMemUsage();
160 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
161 E = ActiveTimers.end(); I != E; ++I)
162 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
166 //===----------------------------------------------------------------------===//
167 // TimerGroup Implementation
168 //===----------------------------------------------------------------------===//
170 static void printVal(double Val, double Total) {
171 if (Total < 1e-7) // Avoid dividing by zero...
172 fprintf(stderr, " ----- ");
174 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
177 void Timer::print(const Timer &Total) {
179 printVal(UserTime, Total.UserTime);
180 if (Total.SystemTime)
181 printVal(SystemTime, Total.SystemTime);
182 if (Total.getProcessTime())
183 printVal(getProcessTime(), Total.getProcessTime());
184 printVal(Elapsed, Total.Elapsed);
186 fprintf(stderr, " ");
189 fprintf(stderr, " %8ld ", MemUsed);
192 fprintf(stderr, " %8ld ", PeakMem);
194 fprintf(stderr, " ");
196 std::cerr << Name << "\n";
198 Started = false; // Once printed, don't print again
202 void TimerGroup::removeTimer() {
203 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
204 // Sort the timers in descending order by amount of time taken...
205 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
206 std::greater<Timer>());
208 // Figure out how many spaces to indent TimerGroup name...
209 unsigned Padding = (80-Name.length())/2;
210 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
213 { // Scope to contain Total timer... don't allow total timer to drop us to
215 Timer Total("TOTAL");
217 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
218 Total.sum(TimersToPrint[i]);
220 // Print out timing header...
221 std::cerr << "===" << std::string(73, '-') << "===\n"
222 << std::string(Padding, ' ') << Name << "\n"
223 << "===" << std::string(73, '-')
224 << "===\n Total Execution Time: " << Total.getProcessTime()
225 << " seconds (" << Total.getWallTime()
226 << " wall clock)\n\n";
229 std::cerr << " ---User Time---";
230 if (Total.SystemTime)
231 std::cerr << " --System Time--";
232 if (Total.getProcessTime())
233 std::cerr << " --User+System--";
234 std::cerr << " ---Wall Time---";
235 if (Total.getMemUsed())
236 std::cerr << " ---Mem---";
237 if (Total.getPeakMem())
238 std::cerr << " -PeakMem-";
239 std::cerr << " --- Name ---\n";
241 // Loop through all of the timing data, printing it out...
242 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
243 TimersToPrint[i].print(Total);
246 std::cerr << std::endl; // Flush output
250 TimersToPrint.clear();
253 // Delete default timer group!
254 if (NumTimers == 0 && this == DefaultTimerGroup) {
255 delete DefaultTimerGroup;
256 DefaultTimerGroup = 0;
262 #if (__GNUC__ == 3) && (__GNUC_MINOR__ == 2) && (__GNUC_PATCHLEVEL__ == 0)
263 // If we have GCC 3.2.0, we can calculate pool allocation bookkeeping info
266 // Taken from GCC 3.2's stl_alloc.h file:
267 enum {_ALIGN = 8, _MAX_BYTES = 128, NFREE = _MAX_BYTES / _ALIGN};
268 struct FreeList { FreeList *Next; };
270 FreeList *_ZNSt24__default_alloc_templateILb1ELi0EE12_S_free_listE[NFREE];
271 char *_ZNSt24__default_alloc_templateILb1ELi0EE13_S_start_freeE;
272 char *_ZNSt24__default_alloc_templateILb1ELi0EE11_S_end_freeE;
273 size_t _ZNSt24__default_alloc_templateILb1ELi0EE12_S_heap_sizeE;
275 // Make the symbols possible to use...
276 FreeList* (&TheFreeList)[NFREE] = _ZNSt24__default_alloc_templateILb1ELi0EE12_S_free_listE;
277 char * &StartFree = _ZNSt24__default_alloc_templateILb1ELi0EE13_S_start_freeE;
278 char * &EndFree = _ZNSt24__default_alloc_templateILb1ELi0EE11_S_end_freeE;
279 size_t &HeapSize = _ZNSt24__default_alloc_templateILb1ELi0EE12_S_heap_sizeE;
283 // getNumBytesToNotCount - This function is supposed to return the number of
284 // bytes that are to be considered not allocated, even though malloc thinks they
287 static unsigned getNumBytesToNotCount() {
289 // If we have GCC 3.2.0, we can subtract off pool allocation bookkeeping info
291 // Size of the free slab section...
292 unsigned FreePoolMem = (unsigned)(EndFree-StartFree);
294 // Walk all of the free lists, adding memory to the free counter whenever we
295 // have a free bucket.
296 for (unsigned i = 0; i != NFREE; ++i) {
297 unsigned NumEntries = 0;
298 for (FreeList *FL = TheFreeList[i]; FL; ++NumEntries, FL = FL->Next)
303 std::cerr << " For Size[" << (i+1)*_ALIGN << "]: " << NumEntries
304 << " Free entries\n";
306 FreePoolMem += NumEntries*(i+1)*_ALIGN;
311 #warning "Don't know how to avoid pool allocation accounting overhead for this"
312 #warning " compiler: Space usage numbers (with -time-passes) may be off!"