Chris Lattner | 6c38a79 | 2002-10-01 19:36:54 +0000 | [diff] [blame] | 1 | //===-- 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 Shukla | 3400837 | 2002-10-04 23:57:01 +0000 | [diff] [blame] | 11 | #include <unistd.h> |
Chris Lattner | 6c38a79 | 2002-10-01 19:36:54 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <iostream> |
| 14 | #include <algorithm> |
Chris Lattner | 9550dc2 | 2002-10-27 19:08:03 +0000 | [diff] [blame] | 15 | #include <functional> |
Chris Lattner | 6c38a79 | 2002-10-01 19:36:54 +0000 | [diff] [blame] | 16 | |
| 17 | static TimerGroup *DefaultTimerGroup = 0; |
| 18 | static TimerGroup *getDefaultTimerGroup() { |
| 19 | if (DefaultTimerGroup) return DefaultTimerGroup; |
| 20 | return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers"); |
| 21 | } |
| 22 | |
| 23 | Timer::Timer(const std::string &N) |
| 24 | : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N), |
| 25 | Started(false), TG(getDefaultTimerGroup()) { |
| 26 | TG->addTimer(); |
| 27 | } |
| 28 | |
| 29 | Timer::Timer(const std::string &N, TimerGroup &tg) |
| 30 | : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N), |
| 31 | Started(false), TG(&tg) { |
| 32 | TG->addTimer(); |
| 33 | } |
| 34 | |
| 35 | Timer::Timer(const Timer &T) { |
| 36 | TG = T.TG; |
| 37 | if (TG) TG->addTimer(); |
| 38 | operator=(T); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // Copy ctor, initialize with no TG member. |
| 43 | Timer::Timer(bool, const Timer &T) { |
| 44 | TG = T.TG; // Avoid assertion in operator= |
| 45 | operator=(T); // Copy contents |
| 46 | TG = 0; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | Timer::~Timer() { |
| 51 | if (TG) { |
| 52 | if (Started) { |
| 53 | Started = false; |
| 54 | TG->addTimerToPrint(*this); |
| 55 | } |
| 56 | TG->removeTimer(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | struct TimeRecord { |
| 61 | double Elapsed, UserTime, SystemTime; |
| 62 | unsigned long MaxRSS; |
| 63 | }; |
| 64 | |
| 65 | static TimeRecord getTimeRecord() { |
| 66 | static unsigned long PageSize = 0; |
| 67 | |
| 68 | if (PageSize == 0) { |
| 69 | #ifdef _SC_PAGE_SIZE |
| 70 | PageSize = sysconf(_SC_PAGE_SIZE); |
| 71 | #else |
| 72 | #ifdef _SC_PAGESIZE |
| 73 | PageSize = sysconf(_SC_PAGESIZE); |
| 74 | #else |
| 75 | PageSize = getpagesize(); |
| 76 | #endif |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | struct rusage RU; |
| 81 | struct timeval T; |
| 82 | gettimeofday(&T, 0); |
| 83 | if (getrusage(RUSAGE_SELF, &RU)) { |
| 84 | perror("getrusage call failed: -time-passes info incorrect!"); |
| 85 | } |
| 86 | |
| 87 | TimeRecord Result; |
| 88 | Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0; |
| 89 | Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0; |
| 90 | Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0; |
| 91 | Result.MaxRSS = RU.ru_maxrss*PageSize; |
| 92 | return Result; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void Timer::startTimer() { |
| 97 | Started = true; |
| 98 | TimeRecord TR = getTimeRecord(); |
| 99 | Elapsed -= TR.Elapsed; |
| 100 | UserTime -= TR.UserTime; |
| 101 | SystemTime -= TR.SystemTime; |
| 102 | MaxRSS -= TR.MaxRSS; |
| 103 | } |
| 104 | |
| 105 | void Timer::stopTimer() { |
| 106 | TimeRecord TR = getTimeRecord(); |
| 107 | Elapsed += TR.Elapsed; |
| 108 | UserTime += TR.UserTime; |
| 109 | SystemTime += TR.SystemTime; |
| 110 | MaxRSS += TR.MaxRSS; |
| 111 | } |
| 112 | |
| 113 | void Timer::sum(const Timer &T) { |
| 114 | Elapsed += T.Elapsed; |
| 115 | UserTime += T.UserTime; |
| 116 | SystemTime += T.SystemTime; |
| 117 | MaxRSS += T.MaxRSS; |
| 118 | } |
| 119 | |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | // TimerGroup Implementation |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | |
| 124 | static void printVal(double Val, double Total) { |
| 125 | if (Total < 1e-7) // Avoid dividing by zero... |
| 126 | fprintf(stderr, " ----- "); |
| 127 | else |
| 128 | fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total); |
| 129 | } |
| 130 | |
| 131 | void Timer::print(const Timer &Total) { |
| 132 | if (Total.UserTime) |
| 133 | printVal(UserTime, Total.UserTime); |
| 134 | if (Total.SystemTime) |
| 135 | printVal(SystemTime, Total.SystemTime); |
| 136 | if (Total.getProcessTime()) |
| 137 | printVal(getProcessTime(), Total.getProcessTime()); |
| 138 | printVal(Elapsed, Total.Elapsed); |
| 139 | |
| 140 | fprintf(stderr, " "); |
| 141 | |
| 142 | if (Total.MaxRSS) |
| 143 | std::cerr << MaxRSS << "\t"; |
| 144 | std::cerr << Name << "\n"; |
| 145 | |
| 146 | Started = false; // Once printed, don't print again |
| 147 | } |
| 148 | |
| 149 | |
| 150 | void TimerGroup::removeTimer() { |
| 151 | if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report... |
| 152 | // Sort the timers in descending order by amount of time taken... |
| 153 | std::sort(TimersToPrint.begin(), TimersToPrint.end(), |
| 154 | std::greater<Timer>()); |
| 155 | |
| 156 | // Figure out how many spaces to indent TimerGroup name... |
| 157 | unsigned Padding = (80-Name.length())/2; |
| 158 | if (Padding > 80) Padding = 0; // Don't allow "negative" numbers |
| 159 | |
| 160 | ++NumTimers; |
| 161 | { // Scope to contain Total timer... don't allow total timer to drop us to |
| 162 | // zero timers... |
| 163 | Timer Total("TOTAL"); |
| 164 | |
| 165 | for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) |
| 166 | Total.sum(TimersToPrint[i]); |
| 167 | |
| 168 | // Print out timing header... |
| 169 | std::cerr << "===" << std::string(73, '-') << "===\n" |
| 170 | << std::string(Padding, ' ') << Name << "\n" |
| 171 | << "===" << std::string(73, '-') |
| 172 | << "===\n Total Execution Time: " << Total.getProcessTime() |
| 173 | << " seconds (" << Total.getWallTime() |
| 174 | << " wall clock)\n\n"; |
| 175 | |
| 176 | if (Total.UserTime) |
| 177 | std::cerr << " ---User Time---"; |
| 178 | if (Total.SystemTime) |
| 179 | std::cerr << " --System Time--"; |
| 180 | if (Total.getProcessTime()) |
| 181 | std::cerr << " --User+System--"; |
| 182 | std::cerr << " ---Wall Time---"; |
| 183 | |
| 184 | if (Total.getMaxRSS()) |
| 185 | std::cerr << " ---Mem---"; |
| 186 | std::cerr << " --- Name ---\n"; |
| 187 | |
| 188 | // Loop through all of the timing data, printing it out... |
| 189 | for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) |
| 190 | TimersToPrint[i].print(Total); |
| 191 | |
| 192 | Total.print(Total); |
| 193 | std::cerr << std::endl; // Flush output |
| 194 | } |
| 195 | --NumTimers; |
| 196 | |
| 197 | TimersToPrint.clear(); |
| 198 | } |
| 199 | |
| 200 | // Delete default timer group! |
| 201 | if (NumTimers == 0 && this == DefaultTimerGroup) { |
| 202 | delete DefaultTimerGroup; |
| 203 | DefaultTimerGroup = 0; |
| 204 | } |
| 205 | } |