2002-10-01 19:36:54 +00:00
|
|
|
//===-- Timer.cpp - Interval Timing Support -------------------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-01 19:36:54 +00:00
|
|
|
//
|
|
|
|
// Interval Timing implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Support/Timer.h"
|
2003-01-30 23:08:50 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2003-06-30 21:59:07 +00:00
|
|
|
#include "Config/sys/resource.h"
|
|
|
|
#include "Config/sys/time.h"
|
|
|
|
#include "Config/unistd.h"
|
|
|
|
#include "Config/malloc.h"
|
2002-10-01 19:36:54 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
2002-10-27 19:08:03 +00:00
|
|
|
#include <functional>
|
2003-05-09 20:05:44 +00:00
|
|
|
#include <fstream>
|
2003-10-06 15:02:31 +00:00
|
|
|
#include <map>
|
2003-12-14 21:27:33 +00:00
|
|
|
using namespace llvm;
|
2003-05-09 20:05:44 +00:00
|
|
|
|
2003-12-14 21:27:33 +00:00
|
|
|
// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
|
|
|
|
namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-07-31 19:38:34 +00:00
|
|
|
// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
|
|
|
|
// of constructor/destructor ordering being unspecified by C++. Basically the
|
|
|
|
// problem is that a Statistic<> object gets destroyed, which ends up calling
|
|
|
|
// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
|
|
|
|
// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
|
|
|
|
// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
|
|
|
|
// this by creating the string the first time it is needed and never destroying
|
|
|
|
// it.
|
|
|
|
static std::string &getLibSupportInfoOutputFilename() {
|
|
|
|
static std::string *LibSupportInfoOutputFilename = new std::string();
|
|
|
|
return *LibSupportInfoOutputFilename;
|
|
|
|
}
|
2002-10-01 19:36:54 +00:00
|
|
|
|
2003-01-30 23:08:50 +00:00
|
|
|
namespace {
|
2003-06-30 21:59:07 +00:00
|
|
|
#ifdef HAVE_MALLINFO
|
2003-01-30 23:08:50 +00:00
|
|
|
cl::opt<bool>
|
|
|
|
TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
|
|
|
|
"tracking (this may be slow)"),
|
|
|
|
cl::Hidden);
|
2003-06-30 21:59:07 +00:00
|
|
|
#endif
|
2003-05-09 20:05:44 +00:00
|
|
|
|
|
|
|
cl::opt<std::string, true>
|
2003-08-01 22:15:15 +00:00
|
|
|
InfoOutputFilename("info-output-file", cl::value_desc("filename"),
|
2003-05-09 20:05:44 +00:00
|
|
|
cl::desc("File to append -stats and -timer output to"),
|
2003-07-31 19:38:34 +00:00
|
|
|
cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
|
2003-01-30 23:08:50 +00:00
|
|
|
}
|
|
|
|
|
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-06-30 21:59:07 +00:00
|
|
|
#ifdef HAVE_MALLINFO
|
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
|
|
|
}
|
2003-06-30 21:59:07 +00:00
|
|
|
#endif
|
2003-06-17 19:54:00 +00:00
|
|
|
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) {
|
|
|
|
if (getrusage(RUSAGE_SELF, &RU))
|
|
|
|
perror("getrusage call failed: -time-passes info incorrect!");
|
2003-12-14 21:27:33 +00:00
|
|
|
MemUsed = getMemUsage();
|
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);
|
|
|
|
}
|
|
|
|
|
2003-10-06 15:02:31 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NamedRegionTimer Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static Timer &getNamedRegionTimer(const std::string &Name) {
|
|
|
|
static std::map<std::string, Timer> NamedTimers;
|
|
|
|
|
|
|
|
std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
|
|
|
|
if (I != NamedTimers.end() && I->first == Name)
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
NamedRegionTimer::NamedRegionTimer(const std::string &Name)
|
|
|
|
: TimeRegion(getNamedRegionTimer(Name)) {}
|
|
|
|
|
2002-11-18 21:47:09 +00:00
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TimerGroup Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
|
|
|
|
// TotalWidth size, and B is the AfterDec size.
|
|
|
|
//
|
|
|
|
static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
|
|
|
|
std::ostream &OS) {
|
|
|
|
assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
|
|
|
|
OS.width(TotalWidth-AfterDec-1);
|
|
|
|
char OldFill = OS.fill();
|
|
|
|
OS.fill(' ');
|
|
|
|
OS << (int)Val; // Integer part;
|
|
|
|
OS << ".";
|
|
|
|
OS.width(AfterDec);
|
|
|
|
OS.fill('0');
|
|
|
|
unsigned ResultFieldSize = 1;
|
|
|
|
while (AfterDec--) ResultFieldSize *= 10;
|
|
|
|
OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
|
|
|
|
OS.fill(OldFill);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printVal(double Val, double Total, std::ostream &OS) {
|
2002-10-01 19:36:54 +00:00
|
|
|
if (Total < 1e-7) // Avoid dividing by zero...
|
2003-05-09 20:05:44 +00:00
|
|
|
OS << " ----- ";
|
|
|
|
else {
|
|
|
|
OS << " ";
|
|
|
|
printAlignedFP(Val, 4, 7, OS);
|
|
|
|
OS << " (";
|
|
|
|
printAlignedFP(Val*100/Total, 1, 5, OS);
|
|
|
|
OS << "%)";
|
|
|
|
}
|
2002-10-01 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
void Timer::print(const Timer &Total, std::ostream &OS) {
|
2002-10-01 19:36:54 +00:00
|
|
|
if (Total.UserTime)
|
2003-05-09 20:05:44 +00:00
|
|
|
printVal(UserTime, Total.UserTime, OS);
|
2002-10-01 19:36:54 +00:00
|
|
|
if (Total.SystemTime)
|
2003-05-09 20:05:44 +00:00
|
|
|
printVal(SystemTime, Total.SystemTime, OS);
|
2002-10-01 19:36:54 +00:00
|
|
|
if (Total.getProcessTime())
|
2003-05-09 20:05:44 +00:00
|
|
|
printVal(getProcessTime(), Total.getProcessTime(), OS);
|
|
|
|
printVal(Elapsed, Total.Elapsed, OS);
|
2002-10-01 19:36:54 +00:00
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
OS << " ";
|
2002-10-01 19:36:54 +00:00
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
if (Total.MemUsed) {
|
|
|
|
OS.width(9);
|
|
|
|
OS << MemUsed << " ";
|
|
|
|
}
|
2002-11-18 21:47:09 +00:00
|
|
|
if (Total.PeakMem) {
|
2003-05-09 20:05:44 +00:00
|
|
|
if (PeakMem) {
|
|
|
|
OS.width(9);
|
|
|
|
OS << PeakMem << " ";
|
|
|
|
} else
|
|
|
|
OS << " ";
|
2002-11-18 21:47:09 +00:00
|
|
|
}
|
2003-05-09 20:05:44 +00:00
|
|
|
OS << Name << "\n";
|
2002-10-01 19:36:54 +00:00
|
|
|
|
|
|
|
Started = false; // Once printed, don't print again
|
|
|
|
}
|
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
|
2003-11-11 22:41:34 +00:00
|
|
|
std::ostream *
|
2003-12-14 21:27:33 +00:00
|
|
|
llvm::GetLibSupportInfoOutputFile() {
|
2003-07-31 19:38:34 +00:00
|
|
|
std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
|
2003-05-09 20:05:44 +00:00
|
|
|
if (LibSupportInfoOutputFilename.empty())
|
|
|
|
return &std::cerr;
|
|
|
|
if (LibSupportInfoOutputFilename == "-")
|
|
|
|
return &std::cout;
|
|
|
|
|
|
|
|
std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
|
2003-06-06 22:13:01 +00:00
|
|
|
std::ios::app);
|
2003-05-09 20:05:44 +00:00
|
|
|
if (!Result->good()) {
|
|
|
|
std::cerr << "Error opening info-output-file '"
|
|
|
|
<< LibSupportInfoOutputFilename << " for appending!\n";
|
|
|
|
delete Result;
|
|
|
|
return &std::cerr;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
std::ostream *OutStream = GetLibSupportInfoOutputFile();
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
++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...
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << "===" << std::string(73, '-') << "===\n"
|
|
|
|
<< std::string(Padding, ' ') << Name << "\n"
|
|
|
|
<< "===" << std::string(73, '-')
|
|
|
|
<< "===\n Total Execution Time: ";
|
2003-02-13 16:25:28 +00:00
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
|
|
|
|
*OutStream << " seconds (";
|
|
|
|
printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
|
|
|
|
*OutStream << " wall clock)\n\n";
|
2002-10-01 19:36:54 +00:00
|
|
|
|
|
|
|
if (Total.UserTime)
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << " ---User Time---";
|
2002-10-01 19:36:54 +00:00
|
|
|
if (Total.SystemTime)
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << " --System Time--";
|
2002-10-01 19:36:54 +00:00
|
|
|
if (Total.getProcessTime())
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << " --User+System--";
|
|
|
|
*OutStream << " ---Wall Time---";
|
2002-11-04 19:19:36 +00:00
|
|
|
if (Total.getMemUsed())
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << " ---Mem---";
|
2002-11-18 21:47:09 +00:00
|
|
|
if (Total.getPeakMem())
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << " -PeakMem-";
|
|
|
|
*OutStream << " --- Name ---\n";
|
2002-10-01 19:36:54 +00:00
|
|
|
|
|
|
|
// Loop through all of the timing data, printing it out...
|
|
|
|
for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
|
2003-05-09 20:05:44 +00:00
|
|
|
TimersToPrint[i].print(Total, *OutStream);
|
2002-10-01 19:36:54 +00:00
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
Total.print(Total, *OutStream);
|
|
|
|
*OutStream << std::endl; // Flush output
|
2002-10-01 19:36:54 +00:00
|
|
|
}
|
|
|
|
--NumTimers;
|
|
|
|
|
|
|
|
TimersToPrint.clear();
|
2003-05-09 20:05:44 +00:00
|
|
|
|
|
|
|
if (OutStream != &std::cerr && OutStream != &std::cout)
|
|
|
|
delete OutStream; // Close the file...
|
2002-10-01 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete default timer group!
|
|
|
|
if (NumTimers == 0 && this == DefaultTimerGroup) {
|
|
|
|
delete DefaultTimerGroup;
|
|
|
|
DefaultTimerGroup = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|