[pypy-svn] r68856 - in pypy/branch/logging2/pypy/translator/c: . src test

arigo at codespeak.net arigo at codespeak.net
Fri Oct 30 13:31:04 CET 2009


Author: arigo
Date: Fri Oct 30 13:31:03 2009
New Revision: 68856

Added:
   pypy/branch/logging2/pypy/translator/c/src/debug.h   (contents, props changed)
Modified:
   pypy/branch/logging2/pypy/translator/c/funcgen.py
   pypy/branch/logging2/pypy/translator/c/genc.py
   pypy/branch/logging2/pypy/translator/c/src/g_include.h
   pypy/branch/logging2/pypy/translator/c/test/test_standalone.py
Log:
The C implementation of debug_start and debug_stop,
as well as small rewrites in debug_print.

For now only for x86 gcc; need to generalize READ_TIMESTAMP...


Modified: pypy/branch/logging2/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/branch/logging2/pypy/translator/c/funcgen.py	(original)
+++ pypy/branch/logging2/pypy/translator/c/funcgen.py	Fri Oct 30 13:31:03 2009
@@ -714,6 +714,7 @@
         from pypy.rpython.lltypesystem.rstr import STR
         format = []
         argv = []
+        free_line = ""
         for arg in op.args:
             T = arg.concretetype
             if T == Ptr(STR):
@@ -722,6 +723,7 @@
                 else:
                     format.append('%s')
                     argv.append('RPyString_AsCharP(%s)' % self.expr(arg))
+                    free_line = "RPyString_FreeCache();"
                 continue
             elif T == Signed:
                 format.append('%d')
@@ -741,9 +743,21 @@
             else:
                 raise Exception("don't know how to debug_print %r" % (T,))
             argv.append(self.expr(arg))
-        return "fprintf(stderr, %s%s); RPyString_FreeCache();" % (
-            c_string_constant(' '.join(format) + '\n\000'),
-            ''.join([', ' + s for s in argv]))
+        argv.insert(0, c_string_constant(' '.join(format) + '\n'))
+        return ("if (PYPY_DEBUG_ENABLED) { fprintf(PYPY_DEBUG_FILE, %s); %s}"
+                % (', '.join(argv), free_line))
+
+    def OP_DEBUG_START(self, op):
+        arg = op.args[0]
+        assert isinstance(arg, Constant)
+        return "PYPY_DEBUG_START(%s);" % (
+            c_string_constant(''.join(arg.value.chars)),)
+
+    def OP_DEBUG_STOP(self, op):
+        arg = op.args[0]
+        assert isinstance(arg, Constant)
+        return "PYPY_DEBUG_STOP(%s);" % (
+            c_string_constant(''.join(arg.value.chars)),)
 
     def OP_DEBUG_ASSERT(self, op):
         return 'RPyAssert(%s, %s);' % (self.expr(op.args[0]),

Modified: pypy/branch/logging2/pypy/translator/c/genc.py
==============================================================================
--- pypy/branch/logging2/pypy/translator/c/genc.py	(original)
+++ pypy/branch/logging2/pypy/translator/c/genc.py	Fri Oct 30 13:31:03 2009
@@ -430,12 +430,14 @@
         bk = self.translator.annotator.bookkeeper
         return getfunctionptr(bk.getdesc(self.entrypoint).getuniquegraph())
 
-    def cmdexec(self, args='', env=None):
+    def cmdexec(self, args='', env=None, err=False):
         assert self._compiled
         res = self.translator.platform.execute(self.executable_name, args,
                                                env=env)
         if res.returncode != 0:
             raise Exception("Returned %d" % (res.returncode,))
+        if err:
+            return res.out, res.err
         return res.out
 
     def compile(self):

Added: pypy/branch/logging2/pypy/translator/c/src/debug.h
==============================================================================
--- (empty file)
+++ pypy/branch/logging2/pypy/translator/c/src/debug.h	Fri Oct 30 13:31:03 2009
@@ -0,0 +1,82 @@
+/************************************************************/
+ /***  C header subsection: debug_print & related tools    ***/
+
+
+/* macros used by the generated code */
+#define PYPY_DEBUG_ENABLED     (pypy_debug_enabled && pypy_debug_is_ready())
+#define PYPY_DEBUG_FILE        pypy_debug_file
+#define PYPY_DEBUG_START(cat)  if (pypy_debug_enabled) pypy_debug_start(cat)
+#define PYPY_DEBUG_STOP(cat)   if (pypy_debug_enabled) pypy_debug_stop(cat)
+
+
+/************************************************************/
+
+/* prototypes (internal use only) */
+bool_t pypy_debug_is_ready(void);
+void pypy_debug_start(const char *category);
+void pypy_debug_stop(const char *category);
+
+extern bool_t pypy_debug_enabled;
+extern FILE *pypy_debug_file;
+
+
+/* implementations */
+
+#ifndef PYPY_NOT_MAIN_FILE
+#include <sys/time.h>
+
+static bool_t pypy_debug_initialized = 0;
+bool_t pypy_debug_enabled = 1;   /* set to 0 if PYPYLOG is not defined */
+FILE *pypy_debug_file;
+
+static void pypy_debug_open(void)
+{
+  char *filename = getenv("PYPYLOG");
+  if (filename && filename[0])
+    {
+      if (filename[0] == '-' && filename[1] == 0)
+        pypy_debug_file = stderr;
+      else
+        pypy_debug_file = fopen(filename, "w");
+    }
+  else
+    {
+      pypy_debug_file = NULL;
+    }
+  pypy_debug_enabled = (pypy_debug_file != NULL);
+  pypy_debug_initialized = 1;
+}
+
+bool_t pypy_debug_is_ready(void)
+{
+  if (!pypy_debug_initialized)
+    pypy_debug_open();
+  return pypy_debug_enabled;
+}
+
+
+/* XXXXXXXXXX   x86 Pentium only! */
+#define READ_TIMESTAMP(val) \
+     __asm__ __volatile__("rdtsc" : "=A" (val))
+
+
+static void pypy_debug_category(const char *start, const char *category)
+{
+  long long timestamp;
+  if (!pypy_debug_is_ready())
+    return;
+  READ_TIMESTAMP(timestamp);
+  fprintf(pypy_debug_file, "{%llx} -%s- %s\n", timestamp, start, category);
+}
+
+void pypy_debug_start(const char *category)
+{
+  pypy_debug_category("start", category);
+}
+
+void pypy_debug_stop(const char *category)
+{
+  pypy_debug_category("stop", category);
+}
+
+#endif /* PYPY_NOT_MAIN_FILE */

Modified: pypy/branch/logging2/pypy/translator/c/src/g_include.h
==============================================================================
--- pypy/branch/logging2/pypy/translator/c/src/g_include.h	(original)
+++ pypy/branch/logging2/pypy/translator/c/src/g_include.h	Fri Oct 30 13:31:03 2009
@@ -51,6 +51,7 @@
 /*** modules ***/
 #ifdef HAVE_RTYPER      /* only if we have an RTyper */
 #  include "src/rtyper.h"
+#  include "src/debug.h"
 #ifndef AVR
 #  include "src/ll_os.h"
 #  include "src/ll_strtod.h"

Modified: pypy/branch/logging2/pypy/translator/c/test/test_standalone.py
==============================================================================
--- pypy/branch/logging2/pypy/translator/c/test/test_standalone.py	(original)
+++ pypy/branch/logging2/pypy/translator/c/test/test_standalone.py	Fri Oct 30 13:31:03 2009
@@ -2,7 +2,7 @@
 import sys, os, re
 
 from pypy.rlib.rarithmetic import r_longlong
-from pypy.rlib.debug import ll_assert, debug_print
+from pypy.rlib.debug import ll_assert, debug_print, debug_start, debug_stop
 from pypy.translator.translator import TranslationContext
 from pypy.translator.backendopt import all
 from pypy.translator.c.genc import CStandaloneBuilder, ExternalCompilationInfo
@@ -11,9 +11,22 @@
 from pypy.tool.autopath import pypydir
 
 
-class TestStandalone(object):
+class StandaloneTests(object):
     config = None
-    
+
+    def compile(self, entry_point):
+        t = TranslationContext(self.config)
+        t.buildannotator().build_types(entry_point, [s_list_of_strings])
+        t.buildrtyper().specialize()
+
+        cbuilder = CStandaloneBuilder(t, entry_point, t.config)
+        cbuilder.generate_source()
+        cbuilder.compile()
+        return t, cbuilder
+
+
+class TestStandalone(StandaloneTests):
+
     def test_hello_world(self):
         def entry_point(argv):
             os.write(1, "hello world\n")
@@ -23,13 +36,7 @@
                 os.write(1, "   '" + str(s) + "'\n")
             return 0
 
-        t = TranslationContext(self.config)
-        t.buildannotator().build_types(entry_point, [s_list_of_strings])
-        t.buildrtyper().specialize()
-
-        cbuilder = CStandaloneBuilder(t, entry_point, t.config)
-        cbuilder.generate_source()
-        cbuilder.compile()
+        t, cbuilder = self.compile(entry_point)
         data = cbuilder.cmdexec('hi there')
         assert data.startswith('''hello world\nargument count: 2\n   'hi'\n   'there'\n''')
 
@@ -43,13 +50,7 @@
             print [len(s) for s in argv]
             return 0
 
-        t = TranslationContext(self.config)
-        t.buildannotator().build_types(entry_point, [s_list_of_strings])
-        t.buildrtyper().specialize()
-
-        cbuilder = CStandaloneBuilder(t, entry_point, t.config)
-        cbuilder.generate_source()
-        cbuilder.compile()
+        t, cbuilder = self.compile(entry_point)
         data = cbuilder.cmdexec('hi there')
         assert data.startswith('''hello simpler world\n'''
                                '''argument count: 2\n'''
@@ -130,13 +131,7 @@
             print m, x
             return 0
 
-        t = TranslationContext(self.config)
-        t.buildannotator().build_types(entry_point, [s_list_of_strings])
-        t.buildrtyper().specialize()
-
-        cbuilder = CStandaloneBuilder(t, entry_point, t.config)
-        cbuilder.generate_source()
-        cbuilder.compile()
+        t, cbuilder = self.compile(entry_point)
         data = cbuilder.cmdexec('hi there')
         assert map(float, data.split()) == [0.0, 0.0]
 
@@ -173,13 +168,8 @@
                 os.setpgrp()
                 return 0
 
-            t = TranslationContext(self.config)
-            t.buildannotator().build_types(entry_point, [s_list_of_strings])
-            t.buildrtyper().specialize()
-
-            cbuilder = CStandaloneBuilder(t, entry_point, t.config)
-            cbuilder.generate_source()
-            cbuilder.compile()
+            t, cbuilder = self.compile(entry_point)
+            cbuilder.cmdexec("")
 
 
     def test_profopt_mac_osx_bug(self):
@@ -223,12 +213,7 @@
                 print "BAD POS"
             os.close(fd)
             return 0
-        t = TranslationContext(self.config)
-        t.buildannotator().build_types(entry_point, [s_list_of_strings])
-        t.buildrtyper().specialize()
-        cbuilder = CStandaloneBuilder(t, entry_point, t.config)
-        cbuilder.generate_source()
-        cbuilder.compile()
+        t, cbuilder = self.compile(entry_point)
         data = cbuilder.cmdexec('hi there')
         assert data.strip() == "OK"
 
@@ -270,6 +255,36 @@
         assert "  ll_strtod.h" in makefile
         assert "  ll_strtod.o" in makefile
 
+    def test_debug_print_start_stop(self):
+        def entry_point(argv):
+            debug_start("mycat")
+            debug_print("foo", 2, "bar", 3)
+            debug_stop("mycat")
+            return 0
+        t, cbuilder = self.compile(entry_point)
+        # check with PYPYLOG undefined
+        out, err = cbuilder.cmdexec("", err=True, env={})
+        assert not out
+        assert not err
+        # check with PYPYLOG defined to an empty string
+        out, err = cbuilder.cmdexec("", err=True, env={'PYPYLOG': ''})
+        assert not out
+        assert not err
+        # check with PYPYLOG=-
+        out, err = cbuilder.cmdexec("", err=True, env={'PYPYLOG': '-'})
+        assert not out
+        assert 'mycat' in err
+        assert 'foo 2 bar 3' in err
+        # check with PYPYLOG=somefilename
+        path = udir.join('test_debug_xxx.log')
+        out, err = cbuilder.cmdexec("", err=True, env={'PYPYLOG': str(path)})
+        assert not out
+        assert not err
+        assert path.check(file=1)
+        assert 'mycat' in path.read()
+        assert 'foo 2 bar 3' in path.read()
+
+
 class TestMaemo(TestStandalone):
     def setup_class(cls):
         from pypy.translator.platform.maemo import check_scratchbox



More information about the Pypy-commit mailing list