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