Bug 633653 - revamp about:memory (NJ-specific part). The original patch was landed on mozilla-central. But because NJ-to-TM merges clobber any NJ changes made in TM, I need to land the NJ-specific directly onto nanojit-central. r=edwsmith.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 11 May 2011 16:53:11 -0700
changeset 6331 cb0301d0f80de32ab4a1a45712b94907c49f5292
parent 6330 102f92c21c056e9b5cae7fde08982daad7b42403
child 6332 787e07e4fb6bfa1a82b5d3a591ed1d852c26578e
push id3697
push userwmaddox@adobe.com
push dateTue, 24 May 2011 07:40:12 +0000
reviewersedwsmith
bugs633653
Bug 633653 - revamp about:memory (NJ-specific part). The original patch was landed on mozilla-central. But because NJ-to-TM merges clobber any NJ changes made in TM, I need to land the NJ-specific directly onto nanojit-central. r=edwsmith.
nanojit/Allocator.cpp
nanojit/Allocator.h
nanojit/CodeAlloc.cpp
nanojit/CodeAlloc.h
--- a/nanojit/Allocator.cpp
+++ b/nanojit/Allocator.cpp
@@ -84,20 +84,32 @@ namespace nanojit
     {
         if (nbytes < MIN_CHUNK_SZB)
             nbytes = MIN_CHUNK_SZB;
         size_t chunkbytes = sizeof(Chunk) + nbytes - sizeof(int64_t);
         void* mem = allocChunk(chunkbytes, fallible);
         if (mem) {
             Chunk* chunk = (Chunk*) mem;
             chunk->prev = current_chunk;
+            chunk->size = chunkbytes;
             current_chunk = chunk;
             current_top = (char*)chunk->data;
             current_limit = (char*)mem + chunkbytes;
             return true;
         } else {
             NanoAssert(fallible);
             return false;
         }
     }
+
+    size_t Allocator::getBytesAllocated()
+    {
+        size_t n = 0;
+        Chunk *c = current_chunk;
+        while (c) {
+            n += c->size;
+            c = c->prev;
+        }
+        return n;
+    }
 }
 
 #endif // FEATURE_NANOJIT
--- a/nanojit/Allocator.h
+++ b/nanojit/Allocator.h
@@ -85,23 +85,26 @@ namespace nanojit
                 p = current_top;
                 current_top += nbytes;
             } else {
                 p = allocSlow(nbytes, /* fallible = */true);
             }
             return p;
         }
 
+        size_t getBytesAllocated();
+
     protected:
         void* allocSlow(size_t nbytes, bool fallible = false);
         bool fill(size_t minbytes, bool fallible);
 
         class Chunk {
         public:
             Chunk* prev;
+            size_t size;
             int64_t data[1]; // int64_t forces 8-byte alignment.
         };
 
         Chunk* current_chunk;
         char* current_top;
         char* current_limit;
 
         // allocator SPI
--- a/nanojit/CodeAlloc.cpp
+++ b/nanojit/CodeAlloc.cpp
@@ -93,32 +93,37 @@ namespace nanojit
         uintptr_t end = (uintptr_t)alignUp(term, bytesPerPage);
         return (CodeList*) (end - (uintptr_t)bytesPerAlloc);
     }
 
     static int round(size_t x) {
         return (int)((x + 512) >> 10);
     }
 
-    void CodeAlloc::logStats() {
-        size_t total = 0;
-        size_t frag_size = 0;
-        size_t free_size = 0;
+    void CodeAlloc::getStats(size_t& total, size_t& frag_size, size_t& free_size) {
+        total = 0;
+        frag_size = 0;
+        free_size = 0;
         int free_count = 0;
         for (CodeList* hb = heapblocks; hb != 0; hb = hb->next) {
             total += bytesPerAlloc;
             for (CodeList* b = hb->lower; b != 0; b = b->lower) {
                 if (b->isFree) {
                     free_count++;
                     free_size += b->blockSize();
                     if (b->size() < minAllocSize)
                         frag_size += b->blockSize();
                 }
             }
         }
+    }
+
+    void CodeAlloc::logStats() {
+        size_t total, frag_size, free_size;
+        getStats(total, frag_size, free_size);
         avmplus::AvmLog("code-heap: %dk free %dk fragmented %d\n",
             round(total), round(free_size), frag_size);
     }
 
     inline void CodeAlloc::markBlockWrite(CodeList* b) {
         NanoAssert(b->terminator != NULL);
         CodeList* term = b->terminator;
         if (term->isExec) {
--- a/nanojit/CodeAlloc.h
+++ b/nanojit/CodeAlloc.h
@@ -223,16 +223,19 @@ namespace nanojit
         /** return the number of bytes in all the code blocks in "code", including block overhead */
 #ifdef PERFM
         static size_t size(const CodeList* code);
 #endif
 
         /** return the total number of bytes held by this CodeAlloc. */
         size_t size();
 
+        /** get stats about heap usage */
+        void getStats(size_t& total, size_t& frag_size, size_t& free_size);
+
         /** print out stats about heap usage */
         void logStats();
 
         /** protect all code managed by this CodeAlloc */
         void markAllExec();
 
         /** protect all mem in the block list */
         void markExec(CodeList* &blocks);