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 // getNumBytesToNotCount - This function is supposed to return the number of
19 // bytes that are to be considered not allocated, even though malloc thinks they
22 static unsigned getNumBytesToNotCount();
24 static TimerGroup *DefaultTimerGroup = 0;
25 static TimerGroup *getDefaultTimerGroup() {
26 if (DefaultTimerGroup) return DefaultTimerGroup;
27 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
30 Timer::Timer(const std::string &N)
31 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
32 Started(false), TG(getDefaultTimerGroup()) {
36 Timer::Timer(const std::string &N, TimerGroup &tg)
37 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
38 Started(false), TG(&tg) {
42 Timer::Timer(const Timer &T) {
44 if (TG) TG->addTimer();
49 // Copy ctor, initialize with no TG member.
50 Timer::Timer(bool, const Timer &T) {
51 TG = T.TG; // Avoid assertion in operator=
52 operator=(T); // Copy contents
61 TG->addTimerToPrint(*this);
67 static long getMemUsage() {
68 struct mallinfo MI = mallinfo();
69 return MI.uordblks/*+MI.hblkhd-getNumBytesToNotCount()*/;
73 double Elapsed, UserTime, SystemTime;
77 static TimeRecord getTimeRecord(bool Start) {
82 MemUsed = getMemUsage();
83 if (getrusage(RUSAGE_SELF, &RU))
84 perror("getrusage call failed: -time-passes info incorrect!");
89 MemUsed = getMemUsage();
90 if (getrusage(RUSAGE_SELF, &RU))
91 perror("getrusage call failed: -time-passes info incorrect!");
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;
98 Result.MemUsed = MemUsed;
103 static std::vector<Timer*> ActiveTimers;
105 void Timer::startTimer() {
107 TimeRecord TR = getTimeRecord(true);
108 Elapsed -= TR.Elapsed;
109 UserTime -= TR.UserTime;
110 SystemTime -= TR.SystemTime;
111 MemUsed -= TR.MemUsed;
112 PeakMemBase = TR.MemUsed;
113 ActiveTimers.push_back(this);
116 void Timer::stopTimer() {
117 TimeRecord TR = getTimeRecord(false);
118 Elapsed += TR.Elapsed;
119 UserTime += TR.UserTime;
120 SystemTime += TR.SystemTime;
121 MemUsed += TR.MemUsed;
123 if (ActiveTimers.back() == this) {
124 ActiveTimers.pop_back();
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);
133 void Timer::sum(const Timer &T) {
134 Elapsed += T.Elapsed;
135 UserTime += T.UserTime;
136 SystemTime += T.SystemTime;
137 MemUsed += T.MemUsed;
138 PeakMem += T.PeakMem;
141 /// 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
145 void Timer::addPeakMemoryMeasurement() {
146 long MemUsed = getMemUsage();
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);
154 //===----------------------------------------------------------------------===//
155 // TimerGroup Implementation
156 //===----------------------------------------------------------------------===//
158 static void printVal(double Val, double Total) {
159 if (Total < 1e-7) // Avoid dividing by zero...
160 fprintf(stderr, " ----- ");
162 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
165 void Timer::print(const Timer &Total) {
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);
174 fprintf(stderr, " ");
177 fprintf(stderr, " %8ld ", MemUsed);
180 fprintf(stderr, " %8ld ", PeakMem);
182 fprintf(stderr, " ");
184 std::cerr << Name << "\n";
186 Started = false; // Once printed, don't print again
190 void 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>());
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
201 { // Scope to contain Total timer... don't allow total timer to drop us to
203 Timer Total("TOTAL");
205 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
206 Total.sum(TimersToPrint[i]);
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";
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---";
223 if (Total.getMemUsed())
224 std::cerr << " ---Mem---";
225 if (Total.getPeakMem())
226 std::cerr << " -PeakMem-";
227 std::cerr << " --- Name ---\n";
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);
234 std::cerr << std::endl; // Flush output
238 TimersToPrint.clear();
241 // Delete default timer group!
242 if (NumTimers == 0 && this == DefaultTimerGroup) {
243 delete DefaultTimerGroup;
244 DefaultTimerGroup = 0;
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
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; };
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;
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;
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
275 static unsigned getNumBytesToNotCount() {
277 // If we have GCC 3.2.0, we can subtract off pool allocation bookkeeping info
279 // Size of the free slab section...
280 unsigned FreePoolMem = (unsigned)(EndFree-StartFree);
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)
291 std::cerr << " For Size[" << (i+1)*_ALIGN << "]: " << NumEntries
292 << " Free entries\n";
294 FreePoolMem += NumEntries*(i+1)*_ALIGN;
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!"