[cfe-commits] r169831 - in /cfe/trunk: include/clang/Basic/FileManager.h include/clang/Basic/FileSystemStatCache.h lib/Basic/FileManager.cpp lib/Basic/FileSystemStatCache.cpp lib/Frontend/CacheTokens.cpp lib/Lex/PTHLexer.cpp unittests/Basic/FileManagerTest.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Dec 10 23:48:23 PST 2012


Author: akirtzidis
Date: Tue Dec 11 01:48:23 2012
New Revision: 169831

URL: http://llvm.org/viewvc/llvm-project?rev=169831&view=rev
Log:
Extend stat query APIs to explicitly specify if the query is for
a file or directory, allowing just a stat call if a file descriptor
is not needed.

Doing just 'stat' is faster than 'open/fstat/close'.
This has the effect of cutting down system time for validating the input files of a PCH.

Modified:
    cfe/trunk/include/clang/Basic/FileManager.h
    cfe/trunk/include/clang/Basic/FileSystemStatCache.h
    cfe/trunk/lib/Basic/FileManager.cpp
    cfe/trunk/lib/Basic/FileSystemStatCache.cpp
    cfe/trunk/lib/Frontend/CacheTokens.cpp
    cfe/trunk/lib/Lex/PTHLexer.cpp
    cfe/trunk/unittests/Basic/FileManagerTest.cpp

Modified: cfe/trunk/include/clang/Basic/FileManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/FileManager.h?rev=169831&r1=169830&r2=169831&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/FileManager.h (original)
+++ cfe/trunk/include/clang/Basic/FileManager.h Tue Dec 11 01:48:23 2012
@@ -164,7 +164,7 @@
   OwningPtr<FileSystemStatCache> StatCache;
 
   bool getStatValue(const char *Path, struct stat &StatBuf,
-                    int *FileDescriptor);
+                    bool isFile, int *FileDescriptor);
 
   /// Add all ancestors of the given path (pointing to either a file
   /// or a directory) as virtual directories.

Modified: cfe/trunk/include/clang/Basic/FileSystemStatCache.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/FileSystemStatCache.h?rev=169831&r1=169830&r2=169831&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/FileSystemStatCache.h (original)
+++ cfe/trunk/include/clang/Basic/FileSystemStatCache.h Tue Dec 11 01:48:23 2012
@@ -44,13 +44,13 @@
   ///
   /// \returns \c true if the path does not exist or \c false if it exists.
   ///
-  /// If FileDescriptor is non-null, then this lookup should only return success
-  /// for files (not directories).  If it is null this lookup should only return
+  /// If isFile is true, then this lookup should only return success for files
+  /// (not directories).  If it is false this lookup should only return
   /// success for directories (not files).  On a successful file lookup, the
   /// implementation can optionally fill in FileDescriptor with a valid
   /// descriptor and the client guarantees that it will close it.
-  static bool get(const char *Path, struct stat &StatBuf, int *FileDescriptor,
-                  FileSystemStatCache *Cache);
+  static bool get(const char *Path, struct stat &StatBuf,
+                  bool isFile, int *FileDescriptor, FileSystemStatCache *Cache);
   
   
   /// \brief Sets the next stat call cache in the chain of stat caches.
@@ -69,16 +69,17 @@
   
 protected:
   virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
-                               int *FileDescriptor) = 0;
+                               bool isFile, int *FileDescriptor) = 0;
 
   LookupResult statChained(const char *Path, struct stat &StatBuf,
-                           int *FileDescriptor) {
+                           bool isFile, int *FileDescriptor) {
     if (FileSystemStatCache *Next = getNextStatCache())
-      return Next->getStat(Path, StatBuf, FileDescriptor);
+      return Next->getStat(Path, StatBuf, isFile, FileDescriptor);
     
     // If we hit the end of the list of stat caches to try, just compute and
     // return it without a cache.
-    return get(Path, StatBuf, FileDescriptor, 0) ? CacheMissing : CacheExists;
+    return get(Path, StatBuf,
+               isFile, FileDescriptor, 0) ? CacheMissing : CacheExists;
   }
 };
 
@@ -97,7 +98,7 @@
   iterator end() const { return StatCalls.end(); }
   
   virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
-                               int *FileDescriptor);
+                               bool isFile, int *FileDescriptor);
 };
 
 } // end namespace clang

Modified: cfe/trunk/lib/Basic/FileManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/FileManager.cpp?rev=169831&r1=169830&r2=169831&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/FileManager.cpp (original)
+++ cfe/trunk/lib/Basic/FileManager.cpp Tue Dec 11 01:48:23 2012
@@ -311,7 +311,7 @@
 
   // Check to see if the directory exists.
   struct stat StatBuf;
-  if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) {
+  if (getStatValue(InterndDirName, StatBuf, false, 0/*directory lookup*/)) {
     // There's no real directory at the given path.
     if (!CacheFailure)
       SeenDirEntries.erase(DirName);
@@ -376,7 +376,8 @@
   // Nope, there isn't.  Check to see if the file exists.
   int FileDescriptor = -1;
   struct stat StatBuf;
-  if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) {
+  if (getStatValue(InterndFileName, StatBuf, true,
+                   openFile ? &FileDescriptor : 0)) {
     // There's no real file at the given path.
     if (!CacheFailure)
       SeenFileEntries.erase(Filename);
@@ -444,14 +445,9 @@
          "The directory of a virtual file should already be in the cache.");
 
   // Check to see if the file exists. If so, drop the virtual file
-  int FileDescriptor = -1;
   struct stat StatBuf;
   const char *InterndFileName = NamedFileEnt.getKeyData();
-  if (getStatValue(InterndFileName, StatBuf, &FileDescriptor) == 0) {
-    // If the stat process opened the file, close it to avoid a FD leak.
-    if (FileDescriptor != -1)
-      close(FileDescriptor);
-
+  if (getStatValue(InterndFileName, StatBuf, true, 0) == 0) {
     StatBuf.st_size = Size;
     StatBuf.st_mtime = ModificationTime;
     UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
@@ -564,18 +560,18 @@
 /// false if it's an existent real file.  If FileDescriptor is NULL,
 /// do directory look-up instead of file look-up.
 bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
-                               int *FileDescriptor) {
+                               bool isFile, int *FileDescriptor) {
   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
   // absolute!
   if (FileSystemOpts.WorkingDir.empty())
-    return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
+    return FileSystemStatCache::get(Path, StatBuf, isFile, FileDescriptor,
                                     StatCache.get());
 
   SmallString<128> FilePath(Path);
   FixupRelativePath(FilePath);
 
-  return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
-                                  StatCache.get());
+  return FileSystemStatCache::get(FilePath.c_str(), StatBuf,
+                                  isFile, FileDescriptor, StatCache.get());
 }
 
 bool FileManager::getNoncachedStatValue(StringRef Path, 

Modified: cfe/trunk/lib/Basic/FileSystemStatCache.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/FileSystemStatCache.cpp?rev=169831&r1=169830&r2=169831&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/FileSystemStatCache.cpp (original)
+++ cfe/trunk/lib/Basic/FileSystemStatCache.cpp Tue Dec 11 01:48:23 2012
@@ -34,21 +34,23 @@
 /// path, using the cache to accelerate it if possible.  This returns true if
 /// the path does not exist or false if it exists.
 ///
-/// If FileDescriptor is non-null, then this lookup should only return success
-/// for files (not directories).  If it is null this lookup should only return
+/// If isFile is true, then this lookup should only return success for files
+/// (not directories).  If it is false this lookup should only return
 /// success for directories (not files).  On a successful file lookup, the
 /// implementation can optionally fill in FileDescriptor with a valid
 /// descriptor and the client guarantees that it will close it.
 bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
-                              int *FileDescriptor, FileSystemStatCache *Cache) {
+                              bool isFile, int *FileDescriptor,
+                              FileSystemStatCache *Cache) {
   LookupResult R;
-  bool isForDir = FileDescriptor == 0;
+  bool isForDir = !isFile;
 
   // If we have a cache, use it to resolve the stat query.
   if (Cache)
-    R = Cache->getStat(Path, StatBuf, FileDescriptor);
-  else if (isForDir) {
-    // If this is a directory and we have no cache, just go to the file system.
+    R = Cache->getStat(Path, StatBuf, isFile, FileDescriptor);
+  else if (isForDir || !FileDescriptor) {
+    // If this is a directory or a file descriptor is not needed and we have
+    // no cache, just go to the file system.
     R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
   } else {
     // Otherwise, we have to go to the filesystem.  We can always just use
@@ -104,8 +106,8 @@
 
 MemorizeStatCalls::LookupResult
 MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
-                           int *FileDescriptor) {
-  LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
+                           bool isFile, int *FileDescriptor) {
+  LookupResult Result = statChained(Path, StatBuf, isFile, FileDescriptor);
   
   // Do not cache failed stats, it is easy to construct common inconsistent
   // situations if we do, and they are not important for PCH performance (which

Modified: cfe/trunk/lib/Frontend/CacheTokens.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CacheTokens.cpp?rev=169831&r1=169830&r2=169831&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CacheTokens.cpp (original)
+++ cfe/trunk/lib/Frontend/CacheTokens.cpp Tue Dec 11 01:48:23 2012
@@ -517,8 +517,8 @@
   ~StatListener() {}
 
   LookupResult getStat(const char *Path, struct stat &StatBuf,
-                       int *FileDescriptor) {
-    LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
+                       bool isFile, int *FileDescriptor) {
+    LookupResult Result = statChained(Path, StatBuf, isFile, FileDescriptor);
 
     if (Result == CacheMissing) // Failed 'stat'.
       PM.insert(PTHEntryKeyVariant(Path), PTHEntry());

Modified: cfe/trunk/lib/Lex/PTHLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PTHLexer.cpp?rev=169831&r1=169830&r2=169831&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PTHLexer.cpp (original)
+++ cfe/trunk/lib/Lex/PTHLexer.cpp Tue Dec 11 01:48:23 2012
@@ -679,13 +679,13 @@
   ~PTHStatCache() {}
 
   LookupResult getStat(const char *Path, struct stat &StatBuf,
-                       int *FileDescriptor) {
+                       bool isFile, int *FileDescriptor) {
     // Do the lookup for the file's data in the PTH file.
     CacheTy::iterator I = Cache.find(Path);
 
     // If we don't get a hit in the PTH file just forward to 'stat'.
     if (I == Cache.end())
-      return statChained(Path, StatBuf, FileDescriptor);
+      return statChained(Path, StatBuf, isFile, FileDescriptor);
 
     const PTHStatData &Data = *I;
 

Modified: cfe/trunk/unittests/Basic/FileManagerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Basic/FileManagerTest.cpp?rev=169831&r1=169830&r2=169831&view=diff
==============================================================================
--- cfe/trunk/unittests/Basic/FileManagerTest.cpp (original)
+++ cfe/trunk/unittests/Basic/FileManagerTest.cpp Tue Dec 11 01:48:23 2012
@@ -51,7 +51,7 @@
 
   // Implement FileSystemStatCache::getStat().
   virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
-                               int *FileDescriptor) {
+                               bool isFile, int *FileDescriptor) {
     if (StatCalls.count(Path) != 0) {
       StatBuf = StatCalls[Path];
       return CacheExists;





More information about the cfe-commits mailing list