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