[pypy-svn] r51677 - in pypy/dist/pypy: rlib rpython/memory/gc rpython/memory/gctransform translator/c/test translator/goal translator/sandbox

arigo at codespeak.net arigo at codespeak.net
Wed Feb 20 11:42:18 CET 2008


Author: arigo
Date: Wed Feb 20 11:42:17 2008
New Revision: 51677

Modified:
   pypy/dist/pypy/rlib/rgc.py
   pypy/dist/pypy/rpython/memory/gc/base.py
   pypy/dist/pypy/rpython/memory/gc/semispace.py
   pypy/dist/pypy/rpython/memory/gctransform/framework.py
   pypy/dist/pypy/translator/c/test/test_newgc.py
   pypy/dist/pypy/translator/goal/targetpypystandalone.py
   pypy/dist/pypy/translator/sandbox/pypy_interact.py
Log:
A trivial but working implementation of rgc.set_max_heap_size()
for SemiSpaceGC.


Modified: pypy/dist/pypy/rlib/rgc.py
==============================================================================
--- pypy/dist/pypy/rlib/rgc.py	(original)
+++ pypy/dist/pypy/rlib/rgc.py	Wed Feb 20 11:42:17 2008
@@ -7,7 +7,8 @@
 
 def set_max_heap_size(nbytes):
     """Limit the heap size to n bytes.
-    So far only implemented by the Boehm GC."""
+    So far only implemented by the Boehm GC and the semispace/generation GCs.
+    """
     pass
 
 # ____________________________________________________________

Modified: pypy/dist/pypy/rpython/memory/gc/base.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gc/base.py	(original)
+++ pypy/dist/pypy/rpython/memory/gc/base.py	Wed Feb 20 11:42:17 2008
@@ -105,6 +105,9 @@
     def id(self, ptr):
         return lltype.cast_ptr_to_int(ptr)
 
+    def set_max_heap_size(self, size):
+        pass
+
     def x_swap_pool(self, newpool):
         return newpool
 

Modified: pypy/dist/pypy/rpython/memory/gc/semispace.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gc/semispace.py	(original)
+++ pypy/dist/pypy/rpython/memory/gc/semispace.py	Wed Feb 20 11:42:17 2008
@@ -187,6 +187,14 @@
         self.space_size = newsize
         return True    # success
 
+    def set_max_heap_size(self, size):
+        # Set the maximum semispace size.  Note that the logic above will
+        # round this number *up* to the next power of two.  Also, this is
+        # the size of one semispace only, so actual usage can be the double
+        # during a collection.  Finally, note that this will never shrink
+        # an already-allocated heap.
+        self.max_space_size = size
+
     def collect(self):
         self.semispace_collect()
         # the indirection is required by the fact that collect() is referred

Modified: pypy/dist/pypy/rpython/memory/gctransform/framework.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gctransform/framework.py	(original)
+++ pypy/dist/pypy/rpython/memory/gctransform/framework.py	Wed Feb 20 11:42:17 2008
@@ -275,6 +275,11 @@
         else:
             self.id_ptr = None
 
+        self.set_max_heap_size_ptr = getfn(GCClass.set_max_heap_size.im_func,
+                                           [s_gc,
+                                            annmodel.SomeInteger(nonneg=True)],
+                                           annmodel.s_None)
+
         if GCClass.needs_write_barrier:
             self.write_barrier_ptr = getfn(GCClass.write_barrier.im_func,
                                            [s_gc, annmodel.SomeAddress(),
@@ -603,6 +608,12 @@
         else:
             hop.rename('cast_ptr_to_int')     # works nicely for non-moving GCs
 
+    def gct_gc_set_max_heap_size(self, hop):
+        [v_size] = hop.spaceop.args
+        hop.genop("direct_call", [self.set_max_heap_size_ptr,
+                                  self.c_const_gc,
+                                  v_size])
+
     def transform_generic_set(self, hop):
         from pypy.objspace.flow.model import Constant
         opname = hop.spaceop.opname

Modified: pypy/dist/pypy/translator/c/test/test_newgc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_newgc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_newgc.py	Wed Feb 20 11:42:17 2008
@@ -875,6 +875,26 @@
         res = fn()
         assert res == -2
 
+    def test_gc_set_max_heap_size(self):
+        def g(n):
+            return 'x' * n
+        def fn():
+            # the semispace size starts at 8MB for now, so setting a
+            # smaller limit has no effect
+            from pypy.rlib import rgc
+            rgc.set_max_heap_size(20000000)   # almost 20 MB
+            s1 = s2 = s3 = None
+            try:
+                s1 = g(400000)      # ~ 400 KB
+                s2 = g(4000000)     # ~ 4 MB
+                s3 = g(40000000)    # ~ 40 MB
+            except MemoryError:
+                pass
+            return (s1 is not None) + (s2 is not None) + (s3 is not None)
+        c_fn = self.getcompiled(fn)
+        res = c_fn()
+        assert res == 2
+
 
 class TestGenerationalGC(TestSemiSpaceGC):
     gcpolicy = "generation"

Modified: pypy/dist/pypy/translator/goal/targetpypystandalone.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetpypystandalone.py	(original)
+++ pypy/dist/pypy/translator/goal/targetpypystandalone.py	Wed Feb 20 11:42:17 2008
@@ -35,7 +35,9 @@
         #    debug(" argv -> " + arg)
         if len(argv) > 2 and argv[1] == '--heapsize':
             # Undocumented option, handled at interp-level.
-            # It has silently no effect in non-Boehm translations.
+            # It has silently no effect with some GCs.
+            # It works in Boehm and in the semispace or generational GCs
+            # (but see comments in semispace.py:set_max_heap_size()).
             # At the moment this option exists mainly to support sandboxing.
             from pypy.rlib import rgc
             rgc.set_max_heap_size(int(argv[2]))

Modified: pypy/dist/pypy/translator/sandbox/pypy_interact.py
==============================================================================
--- pypy/dist/pypy/translator/sandbox/pypy_interact.py	(original)
+++ pypy/dist/pypy/translator/sandbox/pypy_interact.py	Wed Feb 20 11:42:17 2008
@@ -10,7 +10,8 @@
                   which is the virtual current dir (always read-only for now)
     --heapsize=N  limit memory usage to N bytes, or kilo- mega- giga-bytes
                   with the 'k', 'm' or 'g' suffix respectively.
-                  ATM this only works with PyPy translated with Boehm.
+                  ATM this only works with PyPy translated with Boehm or
+                  the semispace or generation GCs.
     --timeout=N   limit execution time to N (real-time) seconds.
     --log=FILE    log all user input into the FILE
 



More information about the Pypy-commit mailing list