2002-10-01 19:36:54 +00:00
|
|
|
//===-- Timer.cpp - Interval Timing Support -------------------------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
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.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-01 19:36:54 +00:00
|
|
|
//
|
|
|
|
// Interval Timing implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-10-04 21:52:35 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2004-12-20 00:59:04 +00:00
|
|
|
#include "llvm/System/Process.h"
|
2004-12-20 03:59:23 +00:00
|
|
|
#include <algorithm>
|
2003-05-09 20:05:44 +00:00
|
|
|
#include <fstream>
|
2004-12-20 03:59:23 +00:00
|
|
|
#include <functional>
|
2004-12-20 00:59:04 +00:00
|
|
|
#include <iostream>
|
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
|
|
|
|
2004-06-07 19:34:51 +00:00
|
|
|
// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
|
2003-12-14 21:27:33 +00:00
|
|
|
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
|
2006-12-06 17:46:33 +00:00
|
|
|
// problem is that a Statistic object gets destroyed, which ends up calling
|
2003-07-31 19:38:34 +00:00
|
|
|
// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
|
|
|
|
// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
|
2004-12-14 03:55:21 +00:00
|
|
|
// 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.
|
2006-10-04 21:52:35 +00:00
|
|
|
static ManagedStatic<std::string> LibSupportInfoOutputFilename;
|
2003-07-31 19:38:34 +00:00
|
|
|
static std::string &getLibSupportInfoOutputFilename() {
|
2004-12-14 03:55:21 +00:00
|
|
|
return *LibSupportInfoOutputFilename;
|
2003-07-31 19:38:34 +00:00
|
|
|
}
|
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);
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-08 20:15:57 +00:00
|
|
|
static inline size_t getMemUsage() {
|
2004-12-27 08:03:04 +00:00
|
|
|
if (TrackSpace)
|
2005-01-08 20:15:57 +00:00
|
|
|
return sys::Process::GetMallocUsage();
|
2004-12-27 08:03:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
struct TimeRecord {
|
|
|
|
double Elapsed, UserTime, SystemTime;
|
2005-01-29 05:21:16 +00:00
|
|
|
ssize_t MemUsed;
|
2002-10-01 19:36:54 +00:00
|
|
|
};
|
|
|
|
|
2002-11-18 21:47:09 +00:00
|
|
|
static TimeRecord getTimeRecord(bool Start) {
|
2004-12-20 00:59:04 +00:00
|
|
|
TimeRecord Result;
|
2004-06-07 19:34:51 +00:00
|
|
|
|
2004-12-20 00:59:04 +00:00
|
|
|
sys::TimeValue now(0,0);
|
|
|
|
sys::TimeValue user(0,0);
|
|
|
|
sys::TimeValue sys(0,0);
|
2004-06-07 19:34:51 +00:00
|
|
|
|
2005-01-29 05:21:16 +00:00
|
|
|
ssize_t MemUsed = 0;
|
2004-12-20 21:44:27 +00:00
|
|
|
if (Start) {
|
2004-12-27 08:03:04 +00:00
|
|
|
MemUsed = getMemUsage();
|
2005-03-22 03:20:38 +00:00
|
|
|
sys::Process::GetTimeUsage(now,user,sys);
|
2004-12-20 21:44:27 +00:00
|
|
|
} else {
|
|
|
|
sys::Process::GetTimeUsage(now,user,sys);
|
2005-03-22 03:20:38 +00:00
|
|
|
MemUsed = getMemUsage();
|
2004-12-20 21:44:27 +00:00
|
|
|
}
|
2002-11-18 21:47:09 +00:00
|
|
|
|
2004-12-20 21:44:27 +00:00
|
|
|
Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
|
|
|
|
Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
|
|
|
|
Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
|
|
|
|
Result.MemUsed = MemUsed;
|
2002-10-01 19:36:54 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2006-10-04 21:52:35 +00:00
|
|
|
static ManagedStatic<std::vector<Timer*> > ActiveTimers;
|
2002-11-18 21:47:09 +00:00
|
|
|
|
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;
|
2006-10-04 21:52:35 +00:00
|
|
|
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
|
|
|
|
2006-10-04 21:52:35 +00:00
|
|
|
if (ActiveTimers->back() == this) {
|
|
|
|
ActiveTimers->pop_back();
|
2002-11-18 21:47:09 +00:00
|
|
|
} else {
|
|
|
|
std::vector<Timer*>::iterator I =
|
2006-10-04 21:52:35 +00:00
|
|
|
std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
|
|
|
|
assert(I != ActiveTimers->end() && "stop but no startTimer?");
|
|
|
|
ActiveTimers->erase(I);
|
2002-11-18 21:47:09 +00:00
|
|
|
}
|
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() {
|
2005-01-08 20:15:57 +00:00
|
|
|
size_t MemUsed = getMemUsage();
|
2002-11-18 21:47:09 +00:00
|
|
|
|
2006-10-04 21:52:35 +00:00
|
|
|
for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
|
|
|
|
E = ActiveTimers->end(); I != E; ++I)
|
2002-11-18 21:47:09 +00:00
|
|
|
(*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
|
|
|
|
}
|
|
|
|
|
2003-10-06 15:02:31 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NamedRegionTimer Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-10-04 21:52:35 +00:00
|
|
|
static ManagedStatic<std::map<std::string, Timer> > NamedTimers;
|
2003-10-06 15:02:31 +00:00
|
|
|
|
2006-10-04 21:52:35 +00:00
|
|
|
static Timer &getNamedRegionTimer(const std::string &Name) {
|
|
|
|
std::map<std::string, Timer>::iterator I = NamedTimers->lower_bound(Name);
|
|
|
|
if (I != NamedTimers->end() && I->first == Name)
|
2003-10-06 15:02:31 +00:00
|
|
|
return I->second;
|
|
|
|
|
2006-10-04 21:52:35 +00:00
|
|
|
return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
|
2003-10-06 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2005-04-21 22:55:34 +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");
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
|
|
|
|
Total.sum(TimersToPrint[i]);
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2002-10-01 19:36:54 +00:00
|
|
|
// 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, '-')
|
2005-02-09 18:41:32 +00:00
|
|
|
<< "===\n";
|
|
|
|
|
|
|
|
// If this is not an collection of ungrouped times, print the total time.
|
|
|
|
// Ungrouped timers don't really make sense to add up. We still print the
|
|
|
|
// TOTAL line to make the percentages make sense.
|
|
|
|
if (this != DefaultTimerGroup) {
|
|
|
|
*OutStream << " Total Execution Time: ";
|
|
|
|
|
|
|
|
printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
|
|
|
|
*OutStream << " seconds (";
|
|
|
|
printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
|
|
|
|
*OutStream << " wall clock)\n";
|
|
|
|
}
|
|
|
|
*OutStream << "\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";
|
2005-04-21 22:55:34 +00:00
|
|
|
|
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);
|
2005-04-21 22:55:34 +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
|
|
|
|