Memory used is a delta between memuse at the start of the time and the
[oota-llvm.git] / lib / Support / Timer.cpp
index 4b88be2b094749ead52cc1270ef0e4927e66049e..3713f7f69ca57237f0c2dada883a09c97f4644b5 100644 (file)
 
 #include "llvm/Support/Timer.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/System/Process.h"
 #include <algorithm>
-#include <iostream>
-#include <functional>
 #include <fstream>
+#include <functional>
+#include <iostream>
 #include <map>
-#include "llvm/Config/sys/resource.h"
-#include "llvm/Config/sys/time.h"
-#include "llvm/Config/unistd.h"
-#include "llvm/Config/malloc.h"
-#include "llvm/Config/windows.h"
 using namespace llvm;
 
 // GetLibSupportInfoOutputFile - Return a file stream to print our output on.
@@ -33,19 +29,19 @@ namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
 // 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.
+// 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;
-  return LibSupportInfoOutputFilename;
+  static std::string *LibSupportInfoOutputFilename = new std::string();
+  return *LibSupportInfoOutputFilename;
 }
 
 namespace {
-#ifdef HAVE_MALLINFO
   cl::opt<bool>
   TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
                                       "tracking (this may be slow)"),
              cl::Hidden);
-#endif
 
   cl::opt<std::string, true>
   InfoOutputFilename("info-output-file", cl::value_desc("filename"),
@@ -96,65 +92,39 @@ Timer::~Timer() {
   }
 }
 
-static long getMemUsage() {
-#ifdef HAVE_MALLINFO
-  if (TrackSpace) {
-    struct mallinfo MI = mallinfo();
-    return MI.uordblks/*+MI.hblkhd*/;
-  }
-#endif
+static inline size_t getMemUsage() {
+  if (TrackSpace)
+    return sys::Process::GetMallocUsage();
   return 0;
 }
 
 struct TimeRecord {
   double Elapsed, UserTime, SystemTime;
-  long MemUsed;
+  ssize_t MemUsed;
 };
 
 static TimeRecord getTimeRecord(bool Start) {
-#if defined(HAVE_WINDOWS_H)
-  unsigned __int64 ProcCreate, ProcExit, KernelTime, UserTime, CurTime;
-
-  GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, 
-                  (FILETIME*)&ProcExit, (FILETIME*)&KernelTime, 
-                  (FILETIME*)&UserTime);
-  GetSystemTimeAsFileTime((FILETIME*)&CurTime);
+  TimeRecord Result;
 
-  // FILETIME's are # of 100 nanosecond ticks.
-  double ScaleFactor = 1.0/(10*1000*1000);
+  sys::TimeValue now(0,0);
+  sys::TimeValue user(0,0);
+  sys::TimeValue sys(0,0);
 
-  TimeRecord Result;
-  Result.Elapsed    = (CurTime-ProcCreate)*ScaleFactor;  // Wall time
-  Result.UserTime   = UserTime*ScaleFactor;
-  Result.SystemTime = KernelTime*ScaleFactor;
-  return Result;
-#elif defined(HAVE_GETRUSAGE)
-  struct rusage RU;
-  struct timeval T;
-  long MemUsed = 0;
+  ssize_t MemUsed = 0;
   if (Start) {
+    sys::Process::GetTimeUsage(now,user,sys);
     MemUsed = getMemUsage();
-    if (getrusage(RUSAGE_SELF, &RU))
-      perror("getrusage call failed: -time-passes info incorrect!");
-  }
-  gettimeofday(&T, 0);
-
-  if (!Start) {
-    if (getrusage(RUSAGE_SELF, &RU))
-      perror("getrusage call failed: -time-passes info incorrect!");
+  } else {
     MemUsed = getMemUsage();
+    sys::Process::GetTimeUsage(now,user,sys);
   }
 
-  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.MemUsed = MemUsed;
+  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;
+
   return Result;
-#else
-  // Can't get resource usage.
-  return TimeRecord();
-#endif
 }
 
 static std::vector<Timer*> ActiveTimers;
@@ -200,7 +170,7 @@ void Timer::sum(const Timer &T) {
 /// currently active timers, which will be printed when the timer group prints
 ///
 void Timer::addPeakMemoryMeasurement() {
-  long MemUsed = getMemUsage();
+  size_t MemUsed = getMemUsage();
 
   for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
          E = ActiveTimers.end(); I != E; ++I)