Author: JohnDoe
Branch: get-heap-stats
Changeset: r83562:f794f644d3e2
Date: 2016-04-07 12:10 +0300
http://bitbucket.org/pypy/pypy/changeset/f794f644d3e2/

Log:    (fijal, catalin) - started working on better way to dump heap stats

diff --git a/rpython/memory/gc/inspector.py b/rpython/memory/gc/inspector.py
--- a/rpython/memory/gc/inspector.py
+++ b/rpython/memory/gc/inspector.py
@@ -219,6 +219,29 @@
         while pending.non_empty():
             self.unwriteobj(pending.pop())
 
+class HeapAggregator(object):
+    TYPEID_MAP = lltype.Struct('TYPEID_MAP', ('count', lltype.Signed),
+                                 ('size', lltype.Signed))
+
+
+    ARRAY_TYPEID_MAP = lltype.GcArray(TYPEID_MAP)
+
+    def __init__(self, gc, maxtypeid):
+        self.gc = gc
+        self.aggregated = lltype.malloc(self.ARRAY_TYPEID_MAP, maxtypeid)
+
+    def writeobj(self, obj):
+        super(HeapDumper, self).writeobj(obj)
+        typeid = self.gc.get_type_id(obj)
+        objsize = self.gc.get_size_incl_hash(obj)
+        if typeid in self.aggregated:
+            self.aggregated[typeid][0] += objsize
+            self.aggregated[typeid][1] += 1
+        else:
+            self.aggregated[typeid] = [0, 0]
+            self.aggregated[typeid][0] = objsize
+            self.aggregated[typeid][1] = 1
+
 def _hd_add_root(obj, heap_dumper):
     heap_dumper.add(obj)
 
@@ -236,6 +259,13 @@
     heapdumper.delete()
     return True
 
+def get_heap_stats(gc):
+    heapaggreg = HeapAggregator(gc, 200)
+    heapaggreg.add_roots()
+    heapaggreg.walk(heapaggreg.pending)
+
+    return heapaggreg.getdata()
+
 def get_typeids_z(gc):
     srcaddress = gc.root_walker.gcdata.typeids_z
     return llmemory.cast_adr_to_ptr(srcaddress, lltype.Ptr(rgc.ARRAY_OF_CHAR))
diff --git a/rpython/memory/gc/test/test_inspector.py 
b/rpython/memory/gc/test/test_inspector.py
--- a/rpython/memory/gc/test/test_inspector.py
+++ b/rpython/memory/gc/test/test_inspector.py
@@ -37,6 +37,15 @@
                     adr_q, 1, ASize(), -1]
         assert expected == seen
 
+    def test_get_heap_stats(self):
+        p = self.malloc(S)
+        p.x = 5
+        q = self.malloc(S)
+        q.x = 6
+        self.write(p, 'next', q)
+        self.stackroots.append(p)
+        #
+       inspector.H
 
 class TestHybridGC(InspectorTest):
     from rpython.memory.gc.hybrid import HybridGC as GCClass
diff --git a/rpython/translator/c/test/test_newgc.py 
b/rpython/translator/c/test/test_newgc.py
--- a/rpython/translator/c/test/test_newgc.py
+++ b/rpython/translator/c/test/test_newgc.py
@@ -1587,6 +1587,16 @@
 class TestIncrementalMiniMarkGC(TestMiniMarkGC):
     gcpolicy = "incminimark"
 
+    def define_dump_rpy_stats(self):
+        def fn():
+            return 0
+
+        return fn
+
+    def test_dump_rpy_stats(self):
+        res = self.run("dump_rpy_stats")
+        assert res == 0
+
     def define_random_pin(self):
         class A:
             foo = None
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to