2002-10-01 19:36:54 +00:00
|
|
|
//===-- Timer.cpp - Interval Timing Support -------------------------------===//
|
|
|
|
//
|
|
|
|
// Interval Timing implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Support/Timer.h"
|
2003-01-30 23:08:50 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-10-01 19:36:54 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/unistd.h>
|
2002-10-04 23:57:01 +00:00
|
|
|
#include <unistd.h>
|
2002-11-04 00:32:44 +00:00
|
|
|
#include <malloc.h>
|
2002-10-01 19:36:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
2002-10-27 19:08:03 +00:00
|
|
|
#include <functional>
|
2002-10-01 19:36:54 +00:00
|
|
|
|
2003-01-30 23:08:50 +00:00
|
|
|
namespace {
|
|
|
|
cl::opt<bool>
|
|
|
|
TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
|
|
|
|
"tracking (this may be slow)"),
|
|
|
|
cl::Hidden);
|
|
|
|
}
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
static TimerGroup *DefaultTimerGroup = 0;
|
|
|
|
static TimerGroup *getDefaultTimerGroup() {
|
|
|
|
if (DefaultTimerGroup) return DefaultTimerGroup;
|
|
|
|
return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer(const std::string &N)
|
2002-11-18 21:47:09 +00:00
|
|
|
: Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
|
2002-10-01 19:36:54 +00:00
|
|
|
Started(false), TG(getDefaultTimerGroup()) {
|
|
|
|
TG->addTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer(const std::string &N, TimerGroup &tg)
|
2002-11-18 21:47:09 +00:00
|
|
|
: Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
|
2002-10-01 19:36:54 +00:00
|
|
|
Started(false), TG(&tg) {
|
|
|
|
TG->addTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer(const Timer &T) {
|
|
|
|
TG = T.TG;
|
|
|
|
if (TG) TG->addTimer();
|
|
|
|
operator=(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Copy ctor, initialize with no TG member.
|
|
|
|
Timer::Timer(bool, const Timer &T) {
|
|
|
|
TG = T.TG; // Avoid assertion in operator=
|
|
|
|
operator=(T); // Copy contents
|
|
|
|
TG = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timer::~Timer() {
|
|
|
|
if (TG) {
|
|
|
|
if (Started) {
|
|
|
|
Started = false;
|
|
|
|
TG->addTimerToPrint(*this);
|
|
|
|
}
|
|
|
|
TG->removeTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-18 21:47:09 +00:00
|
|
|
static long getMemUsage() {
|
2003-01-30 23:08:50 +00:00
|
|
|
if (TrackSpace) {
|
|
|
|
struct mallinfo MI = mallinfo();
|
2003-02-13 05:07:53 +00:00
|
|
|
return MI.uordblks/*+MI.hblkhd*/;
|
2003-01-30 23:08:50 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2002-11-18 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
struct TimeRecord {
|
|
|
|
double Elapsed, UserTime, SystemTime;
|
2002-11-04 19:19:36 +00:00
|
|
|
long MemUsed;
|
2002-10-01 19:36:54 +00:00
|
|
|
};
|
|
|
|
|
2002-11-18 21:47:09 +00:00
|
|
|
static TimeRecord getTimeRecord(bool Start) {
|
2002-10-01 19:36:54 +00:00
|
|
|
struct rusage RU;
|
|
|
|
struct timeval T;
|
2003-02-05 21:44:28 +00:00
|
|
|
long MemUsed = 0;
|
2002-11-18 21:47:09 +00:00
|
|
|
if (Start) {
|
|
|
|
MemUsed = getMemUsage();
|
|
|
|
if (getrusage(RUSAGE_SELF, &RU))
|
|
|
|
perror("getrusage call failed: -time-passes info incorrect!");
|
|
|
|
}
|
2002-10-01 19:36:54 +00:00
|
|
|
gettimeofday(&T, 0);
|
2002-11-18 21:47:09 +00:00
|
|
|
|
|
|
|
if (!Start) {
|
|
|
|
MemUsed = getMemUsage();
|
|
|
|
if (getrusage(RUSAGE_SELF, &RU))
|
|
|
|
perror("getrusage call failed: -time-passes info incorrect!");
|
2002-10-01 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TimeRecord Result;
|
|
|
|
Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
|
|
|
|
Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
|
|
|
|
Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
|
2002-11-18 21:47:09 +00:00
|
|
|
Result.MemUsed = MemUsed;
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2002-11-18 21:47:09 +00:00
|
|
|
static std::vector<Timer*> ActiveTimers;
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
void Timer::startTimer() {
|
|
|
|
Started = true;
|
2002-11-18 21:47:09 +00:00
|
|
|
TimeRecord TR = getTimeRecord(true);
|
2002-10-01 19:36:54 +00:00
|
|
|
Elapsed -= TR.Elapsed;
|
|
|
|
UserTime -= TR.UserTime;
|
|
|
|
SystemTime -= TR.SystemTime;
|
2002-11-04 19:19:36 +00:00
|
|
|
MemUsed -= TR.MemUsed;
|
2002-11-18 21:47:09 +00:00
|
|
|
PeakMemBase = TR.MemUsed;
|
|
|
|
ActiveTimers.push_back(this);
|
2002-10-01 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::stopTimer() {
|
2002-11-18 21:47:09 +00:00
|
|
|
TimeRecord TR = getTimeRecord(false);
|
2002-10-01 19:36:54 +00:00
|
|
|
Elapsed += TR.Elapsed;
|
|
|
|
UserTime += TR.UserTime;
|
|
|
|
SystemTime += TR.SystemTime;
|
2002-11-04 19:19:36 +00:00
|
|
|
MemUsed += TR.MemUsed;
|
2002-11-18 21:47:09 +00:00
|
|
|
|
|
|
|
if (ActiveTimers.back() == this) {
|
|
|
|
ActiveTimers.pop_back();
|
|
|
|
} else {
|
|
|
|
std::vector<Timer*>::iterator I =
|
|
|
|
std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
|
|
|
|
assert(I != ActiveTimers.end() && "stop but no startTimer?");
|
|
|
|
ActiveTimers.erase(I);
|
|
|
|
}
|
2002-10-01 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::sum(const Timer &T) {
|
|
|
|
Elapsed += T.Elapsed;
|
|
|
|
UserTime += T.UserTime;
|
|
|
|
SystemTime += T.SystemTime;
|
2002-11-04 19:19:36 +00:00
|
|
|
MemUsed += T.MemUsed;
|
2002-11-18 21:47:09 +00:00
|
|
|
PeakMem += T.PeakMem;
|
2002-10-01 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
2002-11-18 21:47:09 +00:00
|
|
|
/// addPeakMemoryMeasurement - This method should be called whenever memory
|
|
|
|
/// usage needs to be checked. It adds a peak memory measurement to the
|
|
|
|
/// currently active timers, which will be printed when the timer group prints
|
|
|
|
///
|
|
|
|
void Timer::addPeakMemoryMeasurement() {
|
|
|
|
long MemUsed = getMemUsage();
|
|
|
|
|
|
|
|
for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
|
|
|
|
E = ActiveTimers.end(); I != E; ++I)
|
|
|
|
(*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TimerGroup Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static void printVal(double Val, double Total) {
|
|
|
|
if (Total < 1e-7) // Avoid dividing by zero...
|
|
|
|
fprintf(stderr, " ----- ");
|
|
|
|
else
|
|
|
|
fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::print(const Timer &Total) {
|
|
|
|
if (Total.UserTime)
|
|
|
|
printVal(UserTime, Total.UserTime);
|
|
|
|
if (Total.SystemTime)
|
|
|
|
printVal(SystemTime, Total.SystemTime);
|
|
|
|
if (Total.getProcessTime())
|
|
|
|
printVal(getProcessTime(), Total.getProcessTime());
|
|
|
|
printVal(Elapsed, Total.Elapsed);
|
|
|
|
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
|
2002-11-04 19:19:36 +00:00
|
|
|
if (Total.MemUsed)
|
|
|
|
fprintf(stderr, " %8ld ", MemUsed);
|
2002-11-18 21:47:09 +00:00
|
|
|
if (Total.PeakMem) {
|
|
|
|
if (PeakMem)
|
|
|
|
fprintf(stderr, " %8ld ", PeakMem);
|
|
|
|
else
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
2002-10-01 19:36:54 +00:00
|
|
|
std::cerr << Name << "\n";
|
|
|
|
|
|
|
|
Started = false; // Once printed, don't print again
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TimerGroup::removeTimer() {
|
|
|
|
if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
|
|
|
|
// Sort the timers in descending order by amount of time taken...
|
|
|
|
std::sort(TimersToPrint.begin(), TimersToPrint.end(),
|
|
|
|
std::greater<Timer>());
|
|
|
|
|
|
|
|
// Figure out how many spaces to indent TimerGroup name...
|
|
|
|
unsigned Padding = (80-Name.length())/2;
|
|
|
|
if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
|
|
|
|
|
|
|
|
++NumTimers;
|
|
|
|
{ // Scope to contain Total timer... don't allow total timer to drop us to
|
|
|
|
// zero timers...
|
|
|
|
Timer Total("TOTAL");
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
|
|
|
|
Total.sum(TimersToPrint[i]);
|
|
|
|
|
|
|
|
// Print out timing header...
|
|
|
|
std::cerr << "===" << std::string(73, '-') << "===\n"
|
|
|
|
<< std::string(Padding, ' ') << Name << "\n"
|
|
|
|
<< "===" << std::string(73, '-')
|
2003-02-13 16:25:28 +00:00
|
|
|
<< "===\n Total Execution Time: ";
|
|
|
|
|
|
|
|
// Hack for GCC 2.96... :( it doesn't support manipulators!
|
|
|
|
fprintf(stderr, "%.4f seconds (%.4f wall clock)\n\n",
|
|
|
|
Total.getProcessTime(), Total.getWallTime());
|
2002-10-01 19:36:54 +00:00
|
|
|
|
|
|
|
if (Total.UserTime)
|
|
|
|
std::cerr << " ---User Time---";
|
|
|
|
if (Total.SystemTime)
|
|
|
|
std::cerr << " --System Time--";
|
|
|
|
if (Total.getProcessTime())
|
|
|
|
std::cerr << " --User+System--";
|
|
|
|
std::cerr << " ---Wall Time---";
|
2002-11-04 19:19:36 +00:00
|
|
|
if (Total.getMemUsed())
|
2002-11-04 00:32:44 +00:00
|
|
|
std::cerr << " ---Mem---";
|
2002-11-18 21:47:09 +00:00
|
|
|
if (Total.getPeakMem())
|
|
|
|
std::cerr << " -PeakMem-";
|
2002-10-01 19:36:54 +00:00
|
|
|
std::cerr << " --- Name ---\n";
|
|
|
|
|
|
|
|
// Loop through all of the timing data, printing it out...
|
|
|
|
for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
|
|
|
|
TimersToPrint[i].print(Total);
|
|
|
|
|
|
|
|
Total.print(Total);
|
|
|
|
std::cerr << std::endl; // Flush output
|
|
|
|
}
|
|
|
|
--NumTimers;
|
|
|
|
|
|
|
|
TimersToPrint.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete default timer group!
|
|
|
|
if (NumTimers == 0 && this == DefaultTimerGroup) {
|
|
|
|
delete DefaultTimerGroup;
|
|
|
|
DefaultTimerGroup = 0;
|
|
|
|
}
|
|
|
|
}
|