[pypy-svn] r27517 - in pypy/dist/pypy: rpython rpython/lltypesystem rpython/memory translator/c

arigo at codespeak.net arigo at codespeak.net
Sat May 20 21:29:09 CEST 2006


Author: arigo
Date: Sat May 20 21:29:06 2006
New Revision: 27517

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/lltypesystem/lloperation.py
   pypy/dist/pypy/rpython/memory/gc.py
   pypy/dist/pypy/rpython/memory/gctransform.py
   pypy/dist/pypy/translator/c/funcgen.py
   pypy/dist/pypy/translator/c/support.py
Log:
(pedronis, arigo)

Added a 'debug_print' low-level operation.  Saner than using os.write()
for debugging the gc, because os.write() requires allocating strings for
the formatting...  This operation also produces reasonable-looking C
code.



Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Sat May 20 21:29:06 2006
@@ -405,6 +405,15 @@
         from pypy.translator.tool.lltracker import track
         track(*ll_objects)
 
+    def op_debug_print(self, *ll_args):
+        from pypy.rpython.lltypesystem.rstr import STR
+        for arg in ll_args:
+            T = lltype.typeOf(arg)
+            if T == lltype.Ptr(STR):
+                arg = ''.join(arg.chars)
+            print arg,
+        print
+
     def op_keepalive(self, value):
         pass
 

Modified: pypy/dist/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lloperation.py	Sat May 20 21:29:06 2006
@@ -319,13 +319,17 @@
     'gc_reload_possibly_moved': LLOp(),
     # experimental operations in support of thread cloning, only
     # implemented by the Mark&Sweep GC
-    'gc_x_swap_pool':       LLOp(canraise=(MemoryError,)),
-    'gc_x_clone':           LLOp(canraise=(MemoryError,)),
+    'gc_x_swap_pool':       LLOp(canraise=(Exception,)),
+    'gc_x_clone':           LLOp(canraise=(Exception,)),
+    'gc_x_size_header':     LLOp(),
     # this one is even more experimental; only implemented with the
     # Mark&Sweep GC, and likely only useful when combined with
     # stackless:
-    'gc_x_become':          LLOp(canraise=(RuntimeError,)),
-    
+    'gc_x_become':          LLOp(canraise=(Exception,)),
+
+    # NOTE NOTE NOTE! don't forget *** canraise=StackException *** or
+    # possibly Exception for anything that can unwind the stack, in
+    # particular anything that mallocs!
 
     # __________ stackless operation(s) __________
 
@@ -344,6 +348,7 @@
 
     # __________ debugging __________
     'debug_view':           LLOp(),
+    'debug_print':          LLOp(),
 }
 
     # __________ operations on PyObjects __________

Modified: pypy/dist/pypy/rpython/memory/gc.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gc.py	(original)
+++ pypy/dist/pypy/rpython/memory/gc.py	Sat May 20 21:29:06 2006
@@ -5,6 +5,7 @@
 from pypy.rpython.memory import lltypesimulation
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.rpython.objectmodel import free_non_gc_object
+from pypy.rpython.lltypesystem.lloperation import llop
 from pypy.rpython import rarithmetic
 
 import sys
@@ -199,12 +200,14 @@
         return llmemory.cast_adr_to_ptr(result, llmemory.GCREF)
 
     def collect(self):
-        import os, time
+        import time
         if DEBUG_PRINT:
-            os.write(2, 'collecting...\n')
+            llop.debug_print(lltype.Void, 'collecting...')
         start_time = time.time()
         roots = self.get_roots()
         size_gc_header = self.gcheaderbuilder.size_gc_header
+##        llop.debug_view(lltype.Void, self.malloced_objects, self.poolnodes,
+##                        size_gc_header)
         objects = self.AddressLinkedList()
         while 1:
             curr = roots.pop()
@@ -306,19 +309,24 @@
             self.bytes_malloced_threshold = curr_heap_size
         end_time = time.time()
         self.total_collection_time += end_time - start_time
-        # warning, the following debug prints allocate memory to manipulate
-        # the strings!  so they must be at the end
         if DEBUG_PRINT:
-            os.write(2, "  malloced since previous collection: %s bytes\n" %
-                     old_malloced)
-            os.write(2, "  heap usage at start of collection:  %s bytes\n" %
-                     (self.heap_usage + old_malloced))
-            os.write(2, "  freed:                              %s bytes\n" %
-                     freed_size)
-            os.write(2, "  new heap usage:                     %s bytes\n" %
-                     curr_heap_size)
-            os.write(2, "  total time spent collecting:        %s seconds\n" %
-                     self.total_collection_time)
+            llop.debug_print(lltype.Void,
+                             "  malloced since previous collection:",
+                             old_malloced, "bytes")
+            llop.debug_print(lltype.Void,
+                             "  heap usage at start of collection: ",
+                             self.heap_usage + old_malloced, "bytes")
+            llop.debug_print(lltype.Void,
+                             "  freed:                             ",
+                             freed_size, "bytes")
+            llop.debug_print(lltype.Void,
+                             "  new heap usage:                    ",
+                             curr_heap_size, "bytes")
+            llop.debug_print(lltype.Void,
+                             "  total time spent collecting:       ",
+                             self.total_collection_time, "seconds")
+##        llop.debug_view(lltype.Void, self.malloced_objects, self.poolnodes,
+##                        size_gc_header)
         assert self.heap_usage + old_malloced == curr_heap_size + freed_size
         self.heap_usage = curr_heap_size
 
@@ -397,6 +405,7 @@
         oldpool = lltype.cast_opaque_ptr(self.POOLPTR, oldpool)
         addr = llmemory.cast_ptr_to_adr(oldpool)
         addr -= size_gc_header
+
         hdr = llmemory.cast_adr_to_ptr(addr, self.HDRPTR)
         hdr = hdr.next   # skip the POOL object itself
         while hdr:

Modified: pypy/dist/pypy/rpython/memory/gctransform.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gctransform.py	(original)
+++ pypy/dist/pypy/rpython/memory/gctransform.py	Sat May 20 21:29:06 2006
@@ -1191,6 +1191,13 @@
                                op.result)
         return [newop]
 
+    def replace_gc_x_size_header(self, op, livevars, block):
+        c_result = Constant(self.gcdata.gc.size_gc_header(), lltype.Signed)
+        newop = SpaceOperation("same_as",
+                               [c_result],
+                               op.result)
+        return [newop]
+
     def replace_gc_x_become(self, op, livevars, block):
         [v_target, v_source] = op.args
         newop = SpaceOperation("direct_call",

Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Sat May 20 21:29:06 2006
@@ -2,10 +2,11 @@
 from pypy.translator.c.support import USESLOTS # set to False if necessary while refactoring
 from pypy.translator.c.support import cdecl, ErrorValue
 from pypy.translator.c.support import llvalue_from_constant, gen_assignments
+from pypy.translator.c.support import c_string_constant
 from pypy.objspace.flow.model import Variable, Constant, Block
 from pypy.objspace.flow.model import c_last_exception, copygraph
 from pypy.rpython.lltypesystem.lltype import Ptr, PyObject, Void, Bool, Signed
-from pypy.rpython.lltypesystem.lltype import Unsigned, SignedLongLong
+from pypy.rpython.lltypesystem.lltype import Unsigned, SignedLongLong, Float
 from pypy.rpython.lltypesystem.lltype import UnsignedLongLong, Char, UniChar
 from pypy.rpython.lltypesystem.lltype import pyobjectptr, ContainerType
 from pypy.rpython.lltypesystem.lltype import Struct, Array, FixedSizeArray
@@ -658,5 +659,37 @@
         typename = cdecl(self.db.gettype(TYPE), '')        
         return "%(result)s = (%(typename)s)(%(val)s);" % locals()
 
+    def OP_DEBUG_PRINT(self, op):
+        # XXX
+        from pypy.rpython.lltypesystem.rstr import STR
+        format = []
+        argv = []
+        for arg in op.args:
+            T = arg.concretetype
+            if T == Ptr(STR):
+                if isinstance(arg, Constant):
+                    format.append(''.join(arg.value.chars).replace('%', '%%'))
+                else:
+                    format.append('%s')
+                    argv.append('RPyString_AsString(%s)' % self.expr(arg))
+                continue
+            elif T == Signed:
+                format.append('%d')
+            elif T == Float:
+                format.append('%f')
+            elif isinstance(T, Ptr):
+                format.append('%p')
+            elif T == Char:
+                if isinstance(arg, Constant):
+                    format.append(arg.value.replace('%', '%%'))
+                    continue
+                format.append('%c')
+            else:
+                raise Exception("don't know how to debug_print %r" % (T,))
+            argv.append(self.expr(arg))
+        return "fprintf(stderr, %s%s);" % (
+            c_string_constant(' '.join(format) + '\n'),
+            ''.join([', ' + s for s in argv]))
+
 
 assert not USESLOTS or '__dict__' not in dir(FunctionCodeGenerator)

Modified: pypy/dist/pypy/translator/c/support.py
==============================================================================
--- pypy/dist/pypy/translator/c/support.py	(original)
+++ pypy/dist/pypy/translator/c/support.py	Sat May 20 21:29:06 2006
@@ -68,7 +68,7 @@
            ''')
 
 
-def c_string_constant(s):
+def c_string_constant(s, force_quote=False):
     '''Returns EITHER a " "-delimited string literal for C
                OR a { }-delimited array of chars.
     '''
@@ -82,7 +82,7 @@
     if len(s) < 64:
         return '"%s"' % line_repr(s)
 
-    elif len(s) < 1024:
+    elif len(s) < 1024 or force_quote:
         lines = ['"']
         for i in range(0, len(s), 32):
             lines.append(line_repr(s[i:i+32]))



More information about the Pypy-commit mailing list