[cfe-commits] r117550 - in /cfe/trunk: include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp

Douglas Gregor dgregor at apple.com
Thu Oct 28 08:45:00 PDT 2010


Author: dgregor
Date: Thu Oct 28 10:44:59 2010
New Revision: 117550

URL: http://llvm.org/viewvc/llvm-project?rev=117550&view=rev
Log:
Simplify ASTUnit's internal timers, by not trying to keep a pile of
timers to be dumped whenever the ASTUnit is destroyed. Instead, just
print the time elapsed for each operation after we perform the
operation.

Modified:
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=117550&r1=117549&r2=117550&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Thu Oct 28 10:44:59 2010
@@ -27,7 +27,6 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/System/Path.h"
-#include "llvm/Support/Timer.h"
 #include <map>
 #include <string>
 #include <vector>
@@ -96,6 +95,9 @@
   /// \brief Whether this AST represents a complete translation unit.
   bool CompleteTranslationUnit;
 
+  /// \brief Whether we should time each operation.
+  bool WantTiming;
+  
   /// Track the top-level decls which appeared in an ASTUnit which was loaded
   /// from a source file.
   //
@@ -206,19 +208,10 @@
   /// a precompiled preamble.
   unsigned NumStoredDiagnosticsInPreamble;
 
-  /// \brief The group of timers associated with this translation unit.
-  llvm::OwningPtr<llvm::TimerGroup> TimerGroup;  
-
   /// \brief A list of the serialization ID numbers for each of the top-level
   /// declarations parsed within the precompiled preamble.
   std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
 
-  ///
-  /// \defgroup CodeCompleteCaching Code-completion caching
-  ///
-  /// \{
-  ///
-
   /// \brief Whether we should be caching code-completion results.
   bool ShouldCacheCodeCompletionResults;
   
@@ -301,14 +294,6 @@
   /// \brief Clear out and deallocate 
   void ClearCachedCompletionResults();
   
-  /// 
-  /// \}
-  ///
-  
-  /// \brief The timers we've created from the various parses, reparses, etc.
-  /// involved in this translation unit.
-  std::vector<llvm::Timer *> Timers;
-  
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
   
@@ -393,6 +378,9 @@
   void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
   ASTLocation getLastASTLocation() const { return LastLoc; }
 
+
+  llvm::StringRef getMainFileName() const;
+
   typedef std::vector<Decl *>::iterator top_level_iterator;
 
   top_level_iterator top_level_begin() {

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=117550&r1=117549&r2=117550&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Thu Oct 28 10:44:59 2010
@@ -44,6 +44,46 @@
 #include <sys/stat.h>
 using namespace clang;
 
+using llvm::TimeRecord;
+
+namespace {
+  class SimpleTimer {
+    bool WantTiming;
+    TimeRecord Start;
+    std::string Output;
+
+  public:    
+    explicit SimpleTimer(bool WantTiming) : WantTiming(true) {
+      Start = TimeRecord::getCurrentTime(); 
+    }
+    
+    void setOutput(llvm::StringRef Output) { 
+      if (WantTiming)
+        this->Output = Output; 
+    }
+
+    void setOutput(llvm::Twine Output) { 
+      if (WantTiming)
+        this->Output = Output.str(); 
+    }
+
+    void setOutput(const char *Output) { 
+      if (WantTiming)
+        this->Output = Output; 
+    }
+    
+    ~SimpleTimer() {
+      if (WantTiming) {
+        TimeRecord Elapsed = TimeRecord::getCurrentTime();
+        Elapsed -= Start;
+        llvm::errs() << Output << ':';
+        Elapsed.print(Elapsed, llvm::errs());
+        llvm::errs() << '\n';
+      }
+    }
+  };
+}
+
 /// \brief After failing to build a precompiled preamble (due to
 /// errors in the source that occurs in the preamble), the number of
 /// reparses during which we'll skip even trying to precompile the
@@ -52,7 +92,8 @@
 
 ASTUnit::ASTUnit(bool _MainFileIsAST)
   : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), 
-    CompleteTranslationUnit(true), NumStoredDiagnosticsFromDriver(0),
+    CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
+    NumStoredDiagnosticsFromDriver(0),
     ConcurrencyCheckValue(CheckUnlocked), 
     PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
     ShouldCacheCodeCompletionResults(false),
@@ -84,13 +125,7 @@
   delete SavedMainFileBuffer;
   delete PreambleBuffer;
 
-  ClearCachedCompletionResults();
-  
-  if (TimerGroup)
-    TimerGroup->printAll(llvm::errs());
-
-  for (unsigned I = 0, N = Timers.size(); I != N; ++I)
-    delete Timers[I];
+  ClearCachedCompletionResults();  
 }
 
 void ASTUnit::CleanTemporaryFiles() {
@@ -171,13 +206,9 @@
   if (!TheSema)
     return;
   
-  llvm::Timer *CachingTimer = 0;
-  if (TimerGroup.get()) {
-    CachingTimer = new llvm::Timer("Cache global code completions", 
-                                   *TimerGroup);
-    CachingTimer->startTimer();
-    Timers.push_back(CachingTimer);
-  }
+  SimpleTimer Timer(WantTiming);
+  if (WantTiming)
+    Timer.setOutput( "Cache global code completions for " + getMainFileName());
 
   // Clear out the previous results.
   ClearCachedCompletionResults();
@@ -303,9 +334,6 @@
     Results[I].Destroy();
   }
 
-  if (CachingTimer)
-    CachingTimer->stopTimer();
-  
   // Make a note of the state when we performed this caching.
   NumTopLevelDeclsAtLastCompletionCache = top_level_size();
   CacheCodeCompletionCoolDown = 15;
@@ -1118,12 +1146,9 @@
   }
   
   // We did not previously compute a preamble, or it can't be reused anyway.
-  llvm::Timer *PreambleTimer = 0;
-  if (TimerGroup.get()) {
-    PreambleTimer = new llvm::Timer("Precompiling preamble", *TimerGroup);
-    PreambleTimer->startTimer();
-    Timers.push_back(PreambleTimer);
-  }
+  SimpleTimer PreambleTimer(WantTiming);
+  if (WantTiming)
+    PreambleTimer.setOutput("Precompiling preamble");
   
   // Create a new buffer that stores the preamble. The buffer also contains
   // extra space for the original contents of the file (which will be present
@@ -1183,8 +1208,6 @@
     Preamble.clear();
     if (CreatedPreambleBuffer)
       delete NewPreamble.first;
-    if (PreambleTimer)
-      PreambleTimer->stopTimer();
     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
     PreprocessorOpts.eraseRemappedFile(
                                PreprocessorOpts.remapped_file_buffer_end() - 1);
@@ -1228,8 +1251,6 @@
     Preamble.clear();
     if (CreatedPreambleBuffer)
       delete NewPreamble.first;
-    if (PreambleTimer)
-      PreambleTimer->stopTimer();
     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
     PreprocessorOpts.eraseRemappedFile(
                                PreprocessorOpts.remapped_file_buffer_end() - 1);
@@ -1248,8 +1269,6 @@
     Preamble.clear();
     if (CreatedPreambleBuffer)
       delete NewPreamble.first;
-    if (PreambleTimer)
-      PreambleTimer->stopTimer();
     TopLevelDeclsInPreamble.clear();
     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
     PreprocessorOpts.eraseRemappedFile(
@@ -1280,9 +1299,6 @@
       = std::make_pair(F->second->getSize(), File->getModificationTime());
   }
   
-  if (PreambleTimer)
-    PreambleTimer->stopTimer();
-  
   PreambleRebuildCounter = 1;
   PreprocessorOpts.eraseRemappedFile(
                                PreprocessorOpts.remapped_file_buffer_end() - 1);
@@ -1314,6 +1330,10 @@
   return 0;
 }
 
+llvm::StringRef ASTUnit::getMainFileName() const {
+  return Invocation->getFrontendOpts().Inputs[0].second;
+}
+
 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
   if (!Invocation)
     return true;
@@ -1322,11 +1342,6 @@
   Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
   Invocation->getFrontendOpts().DisableFree = false;
 
-  if (getenv("LIBCLANG_TIMING"))
-    TimerGroup.reset(
-          new llvm::TimerGroup(Invocation->getFrontendOpts().Inputs[0].second));
-  
-  
   llvm::MemoryBuffer *OverrideMainBuffer = 0;
   if (PrecompilePreamble) {
     PreambleRebuildCounter = 1;
@@ -1334,18 +1349,11 @@
       = getMainBufferWithPrecompiledPreamble(*Invocation);
   }
   
-  llvm::Timer *ParsingTimer = 0;
-  if (TimerGroup.get()) {
-    ParsingTimer = new llvm::Timer("Initial parse", *TimerGroup);
-    ParsingTimer->startTimer();
-    Timers.push_back(ParsingTimer);
-  }
+  SimpleTimer ParsingTimer(WantTiming);
+  if (WantTiming)
+    ParsingTimer.setOutput( "Parsing " + getMainFileName());
   
-  bool Failed = Parse(OverrideMainBuffer);
-  if (ParsingTimer)
-    ParsingTimer->stopTimer();
-
-  return Failed;
+  return Parse(OverrideMainBuffer);
 }
 
 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
@@ -1487,13 +1495,10 @@
   if (!Invocation.get())
     return true;
   
-  llvm::Timer *ReparsingTimer = 0;
-  if (TimerGroup.get()) {
-    ReparsingTimer = new llvm::Timer("Reparse", *TimerGroup);
-    ReparsingTimer->startTimer();
-    Timers.push_back(ReparsingTimer);
-  }
-  
+  SimpleTimer ParsingTimer(WantTiming);
+  if (WantTiming)
+    ParsingTimer.setOutput( "Reparsing " + getMainFileName());
+
   // Remap files.
   PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
   for (PreprocessorOptions::remapped_file_buffer_iterator 
@@ -1522,8 +1527,6 @@
   
   // Parse the sources
   bool Result = Parse(OverrideMainBuffer);  
-  if (ReparsingTimer)
-    ReparsingTimer->stopTimer();
   
   if (ShouldCacheCodeCompletionResults) {
     if (CacheCodeCompletionCoolDown > 0)
@@ -1778,15 +1781,13 @@
   if (!Invocation.get())
     return;
 
-  llvm::Timer *CompletionTimer = 0;
-  if (TimerGroup.get()) {
+  SimpleTimer CompletionTimer(WantTiming);
+  if (WantTiming) {
     llvm::SmallString<128> TimerName;
     llvm::raw_svector_ostream TimerNameOut(TimerName);
     TimerNameOut << "Code completion @ " << File << ":" << Line << ":" 
                  << Column;
-    CompletionTimer = new llvm::Timer(TimerNameOut.str(), *TimerGroup);
-    CompletionTimer->startTimer();
-    Timers.push_back(CompletionTimer);
+    CompletionTimer.setOutput(TimerNameOut.str());
   }
 
   CompilerInvocation CCInvocation(*Invocation);
@@ -1914,9 +1915,6 @@
     Act->EndSourceFile();
   }
 
-  if (CompletionTimer)
-    CompletionTimer->stopTimer();
-  
   // Steal back our resources. 
   Clang.takeFileManager();
   Clang.takeSourceManager();





More information about the cfe-commits mailing list