//
//===----------------------------------------------------------------------===//
+#include "llvm/Support/Debug.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
-#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Format.h"
#include "llvm/System/Process.h"
#include <algorithm>
-#include <fstream>
#include <functional>
#include <map>
using namespace llvm;
// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
-namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
+namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
// of constructor/destructor ordering being unspecified by C++. Basically the
return *LibSupportInfoOutputFilename;
}
+static ManagedStatic<sys::SmartMutex<true> > TimerLock;
+
namespace {
static cl::opt<bool>
TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
static TimerGroup *DefaultTimerGroup = 0;
static TimerGroup *getDefaultTimerGroup() {
- if (DefaultTimerGroup) return DefaultTimerGroup;
- return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
+ TimerGroup* tmp = DefaultTimerGroup;
+ sys::MemoryFence();
+ if (!tmp) {
+ llvm_acquire_global_lock();
+ tmp = DefaultTimerGroup;
+ if (!tmp) {
+ tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
+ sys::MemoryFence();
+ DefaultTimerGroup = tmp;
+ }
+ llvm_release_global_lock();
+ }
+
+ return tmp;
}
Timer::Timer(const std::string &N)
static ManagedStatic<std::vector<Timer*> > ActiveTimers;
void Timer::startTimer() {
+ sys::SmartScopedLock<true> L(*TimerLock);
Started = true;
ActiveTimers->push_back(this);
TimeRecord TR = getTimeRecord(true);
}
void Timer::stopTimer() {
+ sys::SmartScopedLock<true> L(*TimerLock);
TimeRecord TR = getTimeRecord(false);
Elapsed += TR.Elapsed;
UserTime += TR.UserTime;
/// currently active timers, which will be printed when the timer group prints
///
void Timer::addPeakMemoryMeasurement() {
+ sys::SmartScopedLock<true> L(*TimerLock);
size_t MemUsed = getMemUsage();
for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
static ManagedStatic<Name2Pair> NamedGroupedTimers;
static Timer &getNamedRegionTimer(const std::string &Name) {
+ sys::SmartScopedLock<true> L(*TimerLock);
Name2Timer::iterator I = NamedTimers->find(Name);
if (I != NamedTimers->end())
return I->second;
static Timer &getNamedRegionTimer(const std::string &Name,
const std::string &GroupName) {
+ sys::SmartScopedLock<true> L(*TimerLock);
Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
if (I == NamedGroupedTimers->end()) {
// TimerGroup Implementation
//===----------------------------------------------------------------------===//
-// 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) {
+static void printVal(double Val, double Total, raw_ostream &OS) {
if (Total < 1e-7) // Avoid dividing by zero...
OS << " ----- ";
else {
- OS << " ";
- printAlignedFP(Val, 4, 7, OS);
- OS << " (";
- printAlignedFP(Val*100/Total, 1, 5, OS);
- OS << "%)";
+ OS << " " << format("%7.4f", Val) << " (";
+ OS << format("%5.1f", Val*100/Total) << "%)";
}
}
-void Timer::print(const Timer &Total, std::ostream &OS) {
+void Timer::print(const Timer &Total, raw_ostream &OS) {
+ sys::SmartScopedLock<true> L(*TimerLock);
if (Total.UserTime)
printVal(UserTime, Total.UserTime, OS);
if (Total.SystemTime)
OS << " ";
if (Total.MemUsed) {
- OS.width(9);
- OS << MemUsed << " ";
+ OS << format("%9lld", (long long)MemUsed) << " ";
}
if (Total.PeakMem) {
if (PeakMem) {
- OS.width(9);
- OS << PeakMem << " ";
+ OS << format("%9lld", (long long)PeakMem) << " ";
} else
OS << " ";
}
}
// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
-std::ostream *
+raw_ostream *
llvm::GetLibSupportInfoOutputFile() {
std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
if (LibSupportInfoOutputFilename.empty())
- return cerr.stream();
+ return &errs();
if (LibSupportInfoOutputFilename == "-")
- return cout.stream();
+ return &outs();
+
- std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
- std::ios::app);
- if (!Result->good()) {
- cerr << "Error opening info-output-file '"
+ std::string Error;
+ raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
+ Error, raw_fd_ostream::F_Append);
+ if (Error.empty())
+ return Result;
+
+ errs() << "Error opening info-output-file '"
<< LibSupportInfoOutputFilename << " for appending!\n";
- delete Result;
- return cerr.stream();
- }
- return Result;
+ delete Result;
+ return &errs();
}
void TimerGroup::removeTimer() {
+ sys::SmartScopedLock<true> L(*TimerLock);
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(),
unsigned Padding = (80-Name.length())/2;
if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
- std::ostream *OutStream = GetLibSupportInfoOutputFile();
+ raw_ostream *OutStream = GetLibSupportInfoOutputFile();
++NumTimers;
{ // Scope to contain Total timer... don't allow total timer to drop us to
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 << format("%5.4f", Total.getProcessTime()) << " seconds (";
+ *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
}
*OutStream << "\n";
TimersToPrint[i].print(Total, *OutStream);
Total.print(Total, *OutStream);
- *OutStream << std::endl; // Flush output
+ *OutStream << '\n';
+ OutStream->flush();
}
--NumTimers;
TimersToPrint.clear();
- if (OutStream != cerr.stream() && OutStream != cout.stream())
+ if (OutStream != &errs() && OutStream != &outs() && OutStream != &dbgs())
delete OutStream; // Close the file...
}
+}
- // Delete default timer group!
- if (NumTimers == 0 && this == DefaultTimerGroup) {
- delete DefaultTimerGroup;
- DefaultTimerGroup = 0;
- }
+void TimerGroup::addTimer() {
+ sys::SmartScopedLock<true> L(*TimerLock);
+ ++NumTimers;
+}
+
+void TimerGroup::addTimerToPrint(const Timer &T) {
+ sys::SmartScopedLock<true> L(*TimerLock);
+ TimersToPrint.push_back(Timer(true, T));
}