[cfe-commits] r130433 - in /cfe/trunk: include/clang-c/Index.h include/clang/Basic/SourceManager.h lib/Basic/SourceManager.cpp tools/libclang/CIndex.cpp

Ted Kremenek kremenek at apple.com
Thu Apr 28 13:36:42 PDT 2011


Author: kremenek
Date: Thu Apr 28 15:36:42 2011
New Revision: 130433

URL: http://llvm.org/viewvc/llvm-project?rev=130433&view=rev
Log:
Enhance clang_getCXTUResourceUsage() to report how much memory is used by SourceManager's memory buffers.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Basic/SourceManager.h
    cfe/trunk/lib/Basic/SourceManager.cpp
    cfe/trunk/tools/libclang/CIndex.cpp

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=130433&r1=130432&r2=130433&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Apr 28 15:36:42 2011
@@ -1023,13 +1023,15 @@
   CXTUResourceUsage_GlobalCompletionResults = 4,
   CXTUResourceUsage_SourceManagerContentCache = 5,
   CXTUResourceUsage_AST_SideTables = 6,
+  CXTUResourceUsage_SourceManager_Membuffer_Malloc = 7,
+  CXTUResourceUsage_SourceManager_Membuffer_MMap = 8,  
 
   CXTUResourceUsage_MEMORY_IN_BYTES_BEGIN = CXTUResourceUsage_AST,
   CXTUResourceUsage_MEMORY_IN_BYTES_END =
-    CXTUResourceUsage_AST_SideTables,
+    CXTUResourceUsage_SourceManager_Membuffer_MMap,
 
   CXTUResourceUsage_First = CXTUResourceUsage_AST,
-  CXTUResourceUsage_Last = CXTUResourceUsage_AST_SideTables
+  CXTUResourceUsage_Last = CXTUResourceUsage_SourceManager_Membuffer_MMap
 };
 
 /**

Modified: cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/SourceManager.h?rev=130433&r1=130432&r2=130433&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Thu Apr 28 15:36:42 2011
@@ -21,11 +21,11 @@
 #include "llvm/ADT/PointerUnion.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include <vector>
 #include <cassert>
 
 namespace llvm {
-class MemoryBuffer;
 class StringRef;
 }
 
@@ -111,6 +111,10 @@
     ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
     ///  instantiated.
     unsigned getSizeBytesMapped() const;
+    
+    /// Returns the kind of memory used to back the memory buffer for
+    /// this content cache.  This is used for performance analysis.
+    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
 
     void setBuffer(const llvm::MemoryBuffer *B) {
       assert(!Buffer.getPointer() && "MemoryBuffer already set.");
@@ -858,6 +862,18 @@
   size_t getContentCacheSize() const {
     return ContentCacheAlloc.getTotalMemory();
   }
+  
+  struct MemoryBufferSizes {
+    const size_t malloc_bytes;
+    const size_t mmap_bytes;
+    
+    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
+      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
+  };
+
+  /// Return the amount of memory used by memory buffers, breaking down
+  /// by heap-backed versus mmap'ed memory.
+  MemoryBufferSizes getMemoryBufferSizes() const;
 
   //===--------------------------------------------------------------------===//
   // Other miscellaneous methods.

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=130433&r1=130432&r2=130433&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Thu Apr 28 15:36:42 2011
@@ -46,6 +46,19 @@
   return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0;
 }
 
+/// Returns the kind of memory used to back the memory buffer for
+/// this content cache.  This is used for performance analysis.
+llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const {
+  assert(Buffer.getPointer());
+
+  // Should be unreachable, but keep for sanity.
+  if (!Buffer.getPointer())
+    return llvm::MemoryBuffer::MemoryBuffer_Malloc;
+  
+  const llvm::MemoryBuffer *buf = Buffer.getPointer();
+  return buf->getBufferKind();
+}
+
 /// getSize - Returns the size of the content encapsulated by this ContentCache.
 ///  This can be the size of the source file or the size of an arbitrary
 ///  scratch buffer.  If the ContentCache encapsulates a source file, that
@@ -1493,3 +1506,24 @@
 }
 
 ExternalSLocEntrySource::~ExternalSLocEntrySource() { }
+
+/// Return the amount of memory used by memory buffers, breaking down
+/// by heap-backed versus mmap'ed memory.
+SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const {
+  size_t malloc_bytes = 0;
+  size_t mmap_bytes = 0;
+  
+  for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i)
+    if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped())
+      switch (MemBufferInfos[i]->getMemoryBufferKind()) {
+        case llvm::MemoryBuffer::MemoryBuffer_MMap:
+          mmap_bytes += sized_mapped;
+          break;
+        case llvm::MemoryBuffer::MemoryBuffer_Malloc:
+          malloc_bytes += sized_mapped;
+          break;
+      }
+  
+  return MemoryBufferSizes(malloc_bytes, mmap_bytes);
+}
+

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=130433&r1=130432&r2=130433&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Thu Apr 28 15:36:42 2011
@@ -5226,6 +5226,12 @@
     case CXTUResourceUsage_AST_SideTables:
       str = "ASTContext: side tables";
       break;
+    case CXTUResourceUsage_SourceManager_Membuffer_Malloc:
+      str = "SourceManager: malloc'ed memory buffers";
+      break;
+    case CXTUResourceUsage_SourceManager_Membuffer_MMap:
+      str = "SourceManager: mmap'ed memory buffers";
+      break;
   }
   return str;
 }
@@ -5270,7 +5276,17 @@
   createCXTUResourceUsageEntry(*entries,
           CXTUResourceUsage_SourceManagerContentCache,
           (unsigned long) astContext.getSourceManager().getContentCacheSize());
-
+  
+  // How much memory is being used by the MemoryBuffer's in SourceManager?
+  const SourceManager::MemoryBufferSizes &srcBufs =
+    astUnit->getSourceManager().getMemoryBufferSizes();
+  
+  createCXTUResourceUsageEntry(*entries,
+                               CXTUResourceUsage_SourceManager_Membuffer_Malloc,
+                               (unsigned long) srcBufs.malloc_bytes);
+  createCXTUResourceUsageEntry(*entries,
+                               CXTUResourceUsage_SourceManager_Membuffer_MMap,
+                               (unsigned long) srcBufs.mmap_bytes);
 
   CXTUResourceUsage usage = { (void*) entries.get(),
                             (unsigned) entries->size(),





More information about the cfe-commits mailing list