Rename AddUsesToWorkList -> AddUsersToWorkList, as that is what it does.
[oota-llvm.git] / lib / Support / Timer.cpp
index 85114b57f2b27f03ed4a8ebf49387852992e8bd6..5d493b8a15e2e329b073362fea01235e384468b3 100644 (file)
@@ -1,4 +1,11 @@
 //===-- Timer.cpp - Interval Timing Support -------------------------------===//
+// 
+//                     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.
+// 
+//===----------------------------------------------------------------------===//
 //
 // Interval Timing implementation.
 //
 
 #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 "Config/sys/resource.h"
+#include "Config/sys/time.h"
+#include "Config/unistd.h"
+#include "Config/malloc.h"
 #include <iostream>
 #include <algorithm>
 #include <functional>
 #include <fstream>
+#include <map>
+using namespace llvm;
 
-std::string LibSupportInfoOutputFilename;
+// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
+namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
+
+// 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;
+}
 
 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",
+  InfoOutputFilename("info-output-file", cl::value_desc("filename"),
                      cl::desc("File to append -stats and -timer output to"),
-                     cl::Hidden, cl::location(LibSupportInfoOutputFilename));
+                   cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
 }
 
 static TimerGroup *DefaultTimerGroup = 0;
@@ -75,12 +98,13 @@ Timer::~Timer() {
 }
 
 static long getMemUsage() {
+#ifdef HAVE_MALLINFO
   if (TrackSpace) {
     struct mallinfo MI = mallinfo();
     return MI.uordblks/*+MI.hblkhd*/;
-  } else {
-    return 0;
   }
+#endif
+  return 0;
 }
 
 struct TimeRecord {
@@ -100,9 +124,9 @@ static TimeRecord getTimeRecord(bool Start) {
   gettimeofday(&T, 0);
 
   if (!Start) {
-    MemUsed = getMemUsage();
     if (getrusage(RUSAGE_SELF, &RU))
       perror("getrusage call failed: -time-passes info incorrect!");
+    MemUsed = getMemUsage();
   }
 
   TimeRecord Result;
@@ -164,6 +188,23 @@ void Timer::addPeakMemoryMeasurement() {
     (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
 }
 
+//===----------------------------------------------------------------------===//
+//   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)) {}
+
 
 //===----------------------------------------------------------------------===//
 //   TimerGroup Implementation
@@ -228,14 +269,16 @@ void Timer::print(const Timer &Total, std::ostream &OS) {
 }
 
 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
-std::ostream *GetLibSupportInfoOutputFile() {
+std::ostream *
+llvm::GetLibSupportInfoOutputFile() {
+  std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
   if (LibSupportInfoOutputFilename.empty())
     return &std::cerr;
   if (LibSupportInfoOutputFilename == "-")
     return &std::cout;
 
   std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
-                                           std::ios_base::app);
+                                           std::ios::app);
   if (!Result->good()) {
     std::cerr << "Error opening info-output-file '"
               << LibSupportInfoOutputFilename << " for appending!\n";
@@ -311,3 +354,4 @@ void TimerGroup::removeTimer() {
     DefaultTimerGroup = 0;
   }
 }
+