[llvm-commits] CVS: llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.cpp PtrCompAllocator.h

Chris Lattner lattner at cs.uiuc.edu
Tue Feb 22 12:11:42 PST 2005



Changes in directory llvm-poolalloc/runtime/PtrCompAllocator:

PtrCompAllocator.cpp updated: 1.1 -> 1.2
PtrCompAllocator.h updated: 1.1 -> 1.2
---
Log message:

implement tracing and debugging support.


---
Diffs of the changes:  (+147 -0)

 PtrCompAllocator.cpp |  143 +++++++++++++++++++++++++++++++++++++++++++++++++++
 PtrCompAllocator.h   |    4 +
 2 files changed, 147 insertions(+)


Index: llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.cpp
diff -u llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.cpp:1.1 llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.cpp:1.2
--- llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.cpp:1.1	Tue Feb 22 13:35:07 2005
+++ llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.cpp	Tue Feb 22 14:11:26 2005
@@ -16,16 +16,137 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/mman.h>
 
 #define POOLSIZE (256*1024*1024)
 
+#ifndef NDEBUG
+// Configuration macros.  Define up to one of these.
+//#define PRINT_NUM_POOLS          // Print use dynamic # pools info
+//#define PRINT_POOLDESTROY_STATS  // When pools are destroyed, print stats
+#define PRINT_POOL_TRACE         // Print a full trace
+#endif
+
+
+//===----------------------------------------------------------------------===//
+// Pool Debugging stuff.
+//===----------------------------------------------------------------------===//
+
+#ifdef PRINT_POOL_TRACE
+#define PRINT_POOLDESTROY_STATS
+
+struct PoolID {
+  PoolTy *PD;
+  unsigned ID;
+};
+
+struct PoolID *PoolIDs = 0;
+static unsigned NumLivePools = 0;
+static unsigned NumPoolIDsAllocated = 0;
+static unsigned CurPoolID = 0;
+
+static unsigned addPoolNumber(PoolTy *PD) {
+  if (NumLivePools == NumPoolIDsAllocated) {
+    NumPoolIDsAllocated = (10+NumPoolIDsAllocated)*2;
+    PoolIDs = (PoolID*)realloc(PoolIDs, sizeof(PoolID)*NumPoolIDsAllocated);
+  }
+  
+  PoolIDs[NumLivePools].PD = PD;
+  PoolIDs[NumLivePools].ID = ++CurPoolID;
+  NumLivePools++;
+  return CurPoolID;
+}
+
+static unsigned getPoolNumber(PoolTy *PD) {
+  if (PD == 0) return 1234567;
+  for (unsigned i = 0; i != NumLivePools; ++i)
+    if (PoolIDs[i].PD == PD)
+      return PoolIDs[i].ID;
+  fprintf(stderr, "INVALID/UNKNOWN POOL DESCRIPTOR: 0x%lX\n", (unsigned long)PD);
+  return 0;
+}
+
+static unsigned removePoolNumber(PoolTy *PD) {
+  for (unsigned i = 0; i != NumLivePools; ++i)
+    if (PoolIDs[i].PD == PD) {
+      unsigned PN = PoolIDs[i].ID;
+      memmove(&PoolIDs[i], &PoolIDs[i+1], sizeof(PoolID)*(NumLivePools-i-1));
+      --NumLivePools;
+      return PN;
+    }
+  fprintf(stderr, "INVALID/UNKNOWN POOL DESCRIPTOR: 0x%lX\n", (unsigned long)PD);
+  return 0;
+}
+
+static void PrintPoolStats(PoolTy *Pool);
+static void PrintLivePoolInfo() {
+  for (unsigned i = 0; i != NumLivePools; ++i) {
+    PoolTy *Pool = PoolIDs[i].PD;
+    fprintf(stderr, "[%d] pool at exit ", PoolIDs[i].ID);
+    PrintPoolStats(Pool);
+  }
+}
+
+#define DO_IF_TRACE(X) X
+#else
+#define DO_IF_TRACE(X)
+#endif
+
+#ifdef PRINT_POOLDESTROY_STATS
+#define DO_IF_POOLDESTROY_STATS(X) X
+#define PRINT_NUM_POOLS
+
+static void PrintPoolStats(PoolTy *Pool) {
+  fprintf(stderr,
+          "(0x%X) BytesAlloc=%d  NumObjs=%d"
+          " bitvectorsize=%d  numused=%d  OrigSize=%d NewSize=%d\n",
+          Pool, Pool->BytesAllocated, Pool->NumObjects,
+          Pool->NumNodesInBitVector, Pool->NumUsed, Pool->OrigSize,
+          Pool->NewSize);
+}
+
+#else
+#define DO_IF_POOLDESTROY_STATS(X)
+#endif
+
+#ifdef PRINT_NUM_POOLS
+static unsigned PoolCounter = 0;
+static unsigned PoolsInited = 0;
+static void PoolCountPrinter() {
+  DO_IF_TRACE(PrintLivePoolInfo());
+  fprintf(stderr, "\n\n"
+          "*** %d DYNAMIC POOLS INITIALIZED ***\n\n"
+          "*** %d DYNAMIC POOLS ALLOCATED FROM ***\n\n",
+          PoolsInited, PoolCounter);
+}
+
+static void InitPrintNumPools() {
+  static bool Initialized = 0;
+  if (!Initialized) {
+    Initialized = 1;
+    atexit(PoolCountPrinter);
+  }
+}
+
+#define DO_IF_PNP(X) X
+#else
+#define DO_IF_PNP(X)
+#endif
+
+
+//===----------------------------------------------------------------------===//
+// Pointer Compression Runtime implementation
+//===----------------------------------------------------------------------===//
+
 // poolinit_pc - Initialize a pool descriptor to empty
 //
 void poolinit_pc(PoolTy *Pool, unsigned NewSize, unsigned OldSize,
                  unsigned ObjAlignment) {
   assert(Pool && OldSize && NewSize && ObjAlignment);
   assert((ObjAlignment & (ObjAlignment-1)) == 0 && "Alignment not 2^k!");
+
+  DO_IF_PNP(memset(Pool, 0, sizeof(PoolTy)));
   Pool->OrigSize = OldSize;
 
   // Round up to the next alignment boundary.
@@ -33,6 +154,11 @@
 
   Pool->PoolBase = 0;
   Pool->BitVector = 0;
+
+  DO_IF_TRACE(fprintf(stderr, "[%d] poolinit_pc(0x%X, %d)\n",
+                      addPoolNumber(Pool), Pool, ObjAlignment));
+  DO_IF_PNP(++PoolsInited);  // Track # pools initialized
+  DO_IF_PNP(InitPrintNumPools());
 }
 
 // pooldestroy_pc - Release all memory allocated for a pool
@@ -41,6 +167,9 @@
   assert(Pool && "Null pool pointer passed in to pooldestroy!\n");
   if (!Pool->PoolBase) return;  // No memory ever allocated.
 
+  DO_IF_TRACE(fprintf(stderr, "[%d] pooldestroy_pc", removePoolNumber(Pool)));
+  DO_IF_POOLDESTROY_STATS(PrintPoolStats(Pool));
+
   munmap(Pool->PoolBase, POOLSIZE);
   Pool->PoolBase = 0;
   free(Pool->BitVector);
@@ -69,6 +198,8 @@
 unsigned long poolalloc_pc(PoolTy *Pool, unsigned NumBytes) {
   assert(Pool && "Null pool pointer passed in to poolalloc!\n");
 
+  DO_IF_PNP(if (Pool->NumObjects == 0) ++PoolCounter);  // Track # pools.
+
   unsigned OrigSize = Pool->OrigSize;
   unsigned NodesToAllocate;
   if (NumBytes == OrigSize)
@@ -76,6 +207,13 @@
   else
     NodesToAllocate = (NumBytes+OrigSize-1)/OrigSize;
 
+  DO_IF_TRACE(fprintf(stderr, "[%d] poolalloc_pc(%d [%d node%s]) -> ",
+                      getPoolNumber(Pool), NumBytes, NodesToAllocate,
+                      NodesToAllocate == 1 ? "" : "s"));
+
+  DO_IF_PNP(++Pool->NumObjects);
+  DO_IF_PNP(Pool->BytesAllocated += NumBytes);
+
   assert(NodesToAllocate == 1 && "Array allocation not implemented yet!");
 
   if (Pool->PoolBase == 0)
@@ -88,6 +226,7 @@
   if (Pool->NumUsed < Pool->NumNodesInBitVector) {
     unsigned long Result = Pool->NumUsed++;
     MarkNodeAllocated(Pool, Result);
+    DO_IF_TRACE(fprintf(stderr, "0x%X\n", (unsigned)Result));
     return Result;
   }
 
@@ -99,11 +238,15 @@
 }
 
 void poolfree_pc(PoolTy *Pool, unsigned long Node) {
+  if (Node == 0) return;
   assert(Pool && Node < Pool->NumUsed && "Node or pool incorrect!");
+  DO_IF_TRACE(fprintf(stderr, "[%d] poolfree_pc(0x%X) ",
+                      getPoolNumber(Pool), (unsigned)Node));
 
   // If freeing the last node, just pop it from the end.
   if (Node == Pool->NumUsed-1) {
     --Pool->NumUsed;
+    DO_IF_TRACE(fprintf(stderr, "1 node\n"));
     return;
   }
 


Index: llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.h
diff -u llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.h:1.1 llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.h:1.2
--- llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.h:1.1	Tue Feb 22 13:35:07 2005
+++ llvm-poolalloc/runtime/PtrCompAllocator/PtrCompAllocator.h	Tue Feb 22 14:11:26 2005
@@ -33,6 +33,10 @@
   // NumUsed - The number of nodes that are currently initialized out of
   // NumNodesInBitVector.  Invariant: NumUsed <= NumNodesInBitVector.
   unsigned long NumUsed;
+
+
+  // These fields are only used in debug mode.
+  unsigned BytesAllocated, NumObjects;
 };
 
 extern "C" {






More information about the llvm-commits mailing list