Author: Maciej Fijalkowski <fij...@gmail.com>
Branch: gc-counters
Changeset: r75073:9cdaba812857
Date: 2014-12-23 10:34 +0200
http://bitbucket.org/pypy/pypy/changeset/9cdaba812857/

Log:    Implement simple counting of different type ids surviving minor
        collections

diff --git a/rpython/memory/gc/incminimark.py b/rpython/memory/gc/incminimark.py
--- a/rpython/memory/gc/incminimark.py
+++ b/rpython/memory/gc/incminimark.py
@@ -175,6 +175,8 @@
 
 # ____________________________________________________________
 
+MAX_TID = 65536
+
 class IncrementalMiniMarkGC(MovingGCBase):
     _alloc_flavor_ = "raw"
     inline_simple_malloc = True
@@ -302,6 +304,8 @@
         self.debug_tiny_nursery = -1
         self.debug_rotating_nurseries = lltype.nullptr(NURSARRAY)
         self.extra_threshold = 0
+        self.tid_counters = lltype.malloc(lltype.Array(lltype.Signed), MAX_TID,
+                                          zero=True, immortal=True)
         #
         # The ArenaCollection() handles the nonmovable objects allocation.
         if ArenaCollectionClass is None:
@@ -1856,6 +1860,10 @@
         # A bit of no-ops to convince llarena that we are changing
         # the layout, in non-translated versions.
         typeid = self.get_type_id(obj)
+
+        # XXX
+        self.tid_counters[typeid] += 1
+        
         obj = llarena.getfakearenaaddress(obj)
         llarena.arena_reset(obj - size_gc_header, totalsize, 0)
         llarena.arena_reserve(obj - size_gc_header,
@@ -2590,3 +2598,12 @@
                 (obj + offset).address[0] = llmemory.NULL
         self.old_objects_with_weakrefs.delete()
         self.old_objects_with_weakrefs = new_with_weakref
+
+    def reset_tid_counters(self):
+        i = 0
+        while i < MAX_TID:
+            self.tid_counters[i] = 0
+            i += 1
+
+    def get_tid_counters(self):
+        return self.tid_counters
diff --git a/rpython/memory/gctransform/framework.py 
b/rpython/memory/gctransform/framework.py
--- a/rpython/memory/gctransform/framework.py
+++ b/rpython/memory/gctransform/framework.py
@@ -474,6 +474,14 @@
                                         [s_gc, SomeAddress()],
                                         annmodel.SomeBool())
 
+        if hasattr(GCClass, 'get_tid_counters'):
+            self.get_tid_ptr = getfn(GCClass.get_tid_counters,
+                                     [s_gc],
+                                     SomePtr(lltype.Ptr(
+                                         lltype.Array(lltype.Signed))))
+            self.reset_tid_ptr = getfn(GCClass.reset_tid_counters,
+                                       [s_gc], annmodel.s_None)
+
         self.write_barrier_ptr = None
         self.write_barrier_from_array_ptr = None
         if GCClass.needs_write_barrier:
@@ -1258,6 +1266,17 @@
         else:
             GCTransformer.gct_setfield(self, hop)
 
+    def gct_gc_reset_tid_counters(self, hop):
+        if hasattr(self, 'reset_tid_ptr'):
+            hop.genop("direct_call", [self.reset_tid_ptr,
+                                      self.c_const_gc])
+
+    def gct_gc_get_tid_counters(self, hop):
+        if hasattr(self, 'get_tid_ptr'):
+            hop.genop("direct_call", [self.get_tid_ptr,
+                                      self.c_const_gc],
+                resultvar=hop.spaceop.result)
+
     def var_needs_set_transform(self, var):
         return var_needsgc(var)
 
diff --git a/rpython/rlib/rgc.py b/rpython/rlib/rgc.py
--- a/rpython/rlib/rgc.py
+++ b/rpython/rlib/rgc.py
@@ -676,3 +676,32 @@
         lambda_func = hop.args_s[1].const
         hop.exception_cannot_occur()
         hop.rtyper.custom_trace_funcs.append((TP, lambda_func()))
+
+def get_tid_counters():
+    return []
+
+def reset_tid_counters():
+    pass
+
+class GetTidCountersEntry(ExtRegistryEntry):
+    _about_ = get_tid_counters
+
+    def compute_result_annotation(self):
+        from rpython.rtyper.llannotation import SomePtr
+        from rpython.rtyper.lltypesystem import lltype
+        
+        return SomePtr(lltype.Ptr(lltype.Array(lltype.Signed)))
+
+    def specialize_call(self, hop):
+        hop.exception_cannot_occur()
+        return hop.genop('gc_get_tid_counters', [], resulttype=hop.r_result)
+
+class ResetTidCountersEntry(ExtRegistryEntry):
+    _about_ = reset_tid_counters
+
+    def compute_result_annotation(self):
+        pass
+
+    def specialize_call(self, hop):
+        hop.exception_cannot_occur()
+        hop.genop('gc_reset_tid_counters', [])
diff --git a/rpython/rtyper/lltypesystem/lloperation.py 
b/rpython/rtyper/lltypesystem/lloperation.py
--- a/rpython/rtyper/lltypesystem/lloperation.py
+++ b/rpython/rtyper/lltypesystem/lloperation.py
@@ -495,6 +495,8 @@
     'gc_typeids_z'        : LLOp(),
     'gc_gcflag_extra'     : LLOp(),
     'gc_add_memory_pressure': LLOp(),
+    'gc_reset_tid_counters': LLOp(),
+    'gc_get_tid_counters'  : LLOp(),
 
     # ------- JIT & GC interaction, only for some GCs ----------
 
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
@@ -1548,6 +1548,32 @@
         res = self.run("random_pin")
         assert res == 28495
 
+    def define_get_tid_ptr(self):
+        class A(object):
+            pass
+
+        prebuilt = A()
+        prebuilt.a = []
+
+        def f():
+            rgc.reset_tid_counters()
+            for i in range(1000):
+                prebuilt.a.append(A())
+                prebuilt.a[-1] = A()
+                prebuilt.a[-1] = A()
+                prebuilt.a[-1] = A()
+                prebuilt.a[-1] = A()
+            llop.gc__collect(lltype.Void)
+            a = rgc.get_tid_counters()
+            sum = 0
+            for i in range(65536):
+                sum += a[i]
+            return sum
+        return f
+
+    def test_get_tid_ptr(self):
+        res = self.run("get_tid_ptr")
+        assert 1050 > res > 1000 # counting objects surviving minor collections
 
 # ____________________________________________________________________
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to