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