//===----------------------------------------------------------------------===//
#include "Support/Timer.h"
+#include "Support/CommandLine.h"
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/unistd.h>
#include <unistd.h>
+#include <malloc.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <functional>
+namespace {
+ cl::opt<bool>
+ TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
+ "tracking (this may be slow)"),
+ cl::Hidden);
+}
+
static TimerGroup *DefaultTimerGroup = 0;
static TimerGroup *getDefaultTimerGroup() {
if (DefaultTimerGroup) return DefaultTimerGroup;
}
Timer::Timer(const std::string &N)
- : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
+ : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Started(false), TG(getDefaultTimerGroup()) {
TG->addTimer();
}
Timer::Timer(const std::string &N, TimerGroup &tg)
- : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0), Name(N),
+ : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Started(false), TG(&tg) {
TG->addTimer();
}
}
}
+static long getMemUsage() {
+ if (TrackSpace) {
+ struct mallinfo MI = mallinfo();
+ return MI.uordblks/*+MI.hblkhd*/;
+ } else {
+ return 0;
+ }
+}
+
struct TimeRecord {
double Elapsed, UserTime, SystemTime;
- unsigned long MaxRSS;
+ long MemUsed;
};
-static TimeRecord getTimeRecord() {
- static unsigned long PageSize = 0;
-
- if (PageSize == 0) {
-#ifdef _SC_PAGE_SIZE
- PageSize = sysconf(_SC_PAGE_SIZE);
-#else
-#ifdef _SC_PAGESIZE
- PageSize = sysconf(_SC_PAGESIZE);
-#else
- PageSize = getpagesize();
-#endif
-#endif
- }
-
+static TimeRecord getTimeRecord(bool Start) {
struct rusage RU;
struct timeval T;
+ long MemUsed = 0;
+ if (Start) {
+ MemUsed = getMemUsage();
+ if (getrusage(RUSAGE_SELF, &RU))
+ perror("getrusage call failed: -time-passes info incorrect!");
+ }
gettimeofday(&T, 0);
- if (getrusage(RUSAGE_SELF, &RU)) {
- perror("getrusage call failed: -time-passes info incorrect!");
+
+ if (!Start) {
+ MemUsed = getMemUsage();
+ if (getrusage(RUSAGE_SELF, &RU))
+ perror("getrusage call failed: -time-passes info incorrect!");
}
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;
- Result.MaxRSS = RU.ru_maxrss*PageSize;
+ Result.MemUsed = MemUsed;
+
return Result;
}
+static std::vector<Timer*> ActiveTimers;
void Timer::startTimer() {
Started = true;
- TimeRecord TR = getTimeRecord();
+ TimeRecord TR = getTimeRecord(true);
Elapsed -= TR.Elapsed;
UserTime -= TR.UserTime;
SystemTime -= TR.SystemTime;
- MaxRSS -= TR.MaxRSS;
+ MemUsed -= TR.MemUsed;
+ PeakMemBase = TR.MemUsed;
+ ActiveTimers.push_back(this);
}
void Timer::stopTimer() {
- TimeRecord TR = getTimeRecord();
+ TimeRecord TR = getTimeRecord(false);
Elapsed += TR.Elapsed;
UserTime += TR.UserTime;
SystemTime += TR.SystemTime;
- MaxRSS += TR.MaxRSS;
+ MemUsed += TR.MemUsed;
+
+ 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);
+ }
}
void Timer::sum(const Timer &T) {
Elapsed += T.Elapsed;
UserTime += T.UserTime;
SystemTime += T.SystemTime;
- MaxRSS += T.MaxRSS;
+ MemUsed += T.MemUsed;
+ PeakMem += T.PeakMem;
+}
+
+/// 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);
}
+
//===----------------------------------------------------------------------===//
// TimerGroup Implementation
//===----------------------------------------------------------------------===//
fprintf(stderr, " ");
- if (Total.MaxRSS)
- std::cerr << MaxRSS << "\t";
+ if (Total.MemUsed)
+ fprintf(stderr, " %8ld ", MemUsed);
+ if (Total.PeakMem) {
+ if (PeakMem)
+ fprintf(stderr, " %8ld ", PeakMem);
+ else
+ fprintf(stderr, " ");
+ }
std::cerr << Name << "\n";
Started = false; // Once printed, don't print again
std::cerr << "===" << std::string(73, '-') << "===\n"
<< std::string(Padding, ' ') << Name << "\n"
<< "===" << std::string(73, '-')
- << "===\n Total Execution Time: " << Total.getProcessTime()
- << " seconds (" << Total.getWallTime()
- << " wall clock)\n\n";
+ << "===\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());
if (Total.UserTime)
std::cerr << " ---User Time---";
if (Total.getProcessTime())
std::cerr << " --User+System--";
std::cerr << " ---Wall Time---";
-
- if (Total.getMaxRSS())
- std::cerr << " ---Mem---";
+ if (Total.getMemUsed())
+ std::cerr << " ---Mem---";
+ if (Total.getPeakMem())
+ std::cerr << " -PeakMem-";
std::cerr << " --- Name ---\n";
// Loop through all of the timing data, printing it out...