Author: Remi Meier <remi.me...@gmail.com>
Branch: nogil-unsafe-2
Changeset: r90509:3fe01091f755
Date: 2017-03-03 15:21 +0100
http://bitbucket.org/pypy/pypy/changeset/3fe01091f755/

Log:    add colourful, thread-prefixed debug print

        (imported from stmgc-c8 branch)

diff --git a/rpython/translator/c/src/debug_print.c 
b/rpython/translator/c/src/debug_print.c
--- a/rpython/translator/c/src/debug_print.c
+++ b/rpython/translator/c/src/debug_print.c
@@ -12,15 +12,16 @@
 #include <windows.h>
 #endif
 #include "common_header.h"
+#include "structdef.h"
 #include "src/profiling.h"
 #include "src/debug_print.h"
 
-long pypy_have_debug_prints = -1;
 FILE *pypy_debug_file = NULL;
 static unsigned char debug_ready = 0;
 static unsigned char debug_profile = 0;
-static char *debug_start_colors_1 = "";
-static char *debug_start_colors_2 = "";
+static __thread char debug_start_colors_1[32];
+static __thread char debug_start_colors_2[28];
+__thread char pypy_debug_threadid[16] = {0};
 static char *debug_stop_colors = "";
 static char *debug_prefix = NULL;
 
@@ -90,8 +91,6 @@
       pypy_debug_file = stderr;
       if (isatty(2))
         {
-          debug_start_colors_1 = "\033[1m\033[31m";
-          debug_start_colors_2 = "\033[31m";
           debug_stop_colors = "\033[0m";
         }
     }
@@ -210,6 +209,40 @@
           debug_stop_colors);
 }
 
+
+#define bool_cas __sync_bool_compare_and_swap
+static Signed threadcounter = 0;
+
+static void _prepare_display_colors(void)
+{
+    Signed counter;
+    char *p;
+    while (1) {
+        counter = threadcounter;
+        if (bool_cas(&threadcounter, counter, counter + 1))
+            break;
+    }
+    if (debug_stop_colors[0] == 0) {
+        /* not a tty output: no colors */
+        sprintf(debug_start_colors_1, "%d# ", (int)counter);
+        sprintf(debug_start_colors_2, "%d# ", (int)counter);
+        sprintf(pypy_debug_threadid, "%d#", (int)counter);
+    }
+    else {
+        /* tty output */
+        int color = 0;
+        color = 31 + (int)(counter % 7);
+        sprintf(debug_start_colors_1, "\033[%dm%d# \033[1m",
+                color, (int)counter);
+        sprintf(debug_start_colors_2, "\033[%dm%d# ",
+                color, (int)counter);
+#ifdef RPY_STM
+        sprintf(pypy_debug_threadid, "\033[%dm%d#\033[0m",
+                color, (int)counter);
+#endif
+    }
+}
+
 void pypy_debug_start(const char *category)
 {
   pypy_debug_ensure_opened();
@@ -229,6 +262,8 @@
       /* else make this subsection active */
       pypy_have_debug_prints |= 1;
     }
+  if (!debug_start_colors_1[0])
+    _prepare_display_colors();
   display_startstop("{", "", category, debug_start_colors_1);
 }
 
diff --git a/rpython/translator/c/src/debug_print.h 
b/rpython/translator/c/src/debug_print.h
--- a/rpython/translator/c/src/debug_print.h
+++ b/rpython/translator/c/src/debug_print.h
@@ -27,6 +27,10 @@
    removed from the environment and not passed to subprocesses.
 */
 
+RPY_EXTERN __thread struct pypy_ExcData0 pypy_g_ExcData;
+#define pypy_have_debug_prints    pypy_g_ExcData.ed_have_debug_prints
+
+
 /* macros used by the generated code */
 #define PYPY_HAVE_DEBUG_PRINTS    (pypy_have_debug_prints & 1 ? \
                                    (pypy_debug_ensure_opened(), 1) : 0)
@@ -48,7 +52,7 @@
 RPY_EXTERN void pypy_debug_forked(long original_offset);
 RPY_EXTERN long pypy_have_debug_prints_for(const char *category_prefix);
 
-RPY_EXTERN long pypy_have_debug_prints;
+RPY_EXTERN __thread char pypy_debug_threadid[];
 RPY_EXPORTED FILE *pypy_debug_file;
 
 #define OP_LL_READ_TIMESTAMP(val) READ_TIMESTAMP(val)
diff --git a/rpython/translator/c/test/test_standalone.py 
b/rpython/translator/c/test/test_standalone.py
--- a/rpython/translator/c/test/test_standalone.py
+++ b/rpython/translator/c/test/test_standalone.py
@@ -1442,9 +1442,9 @@
         def bootstrap():
             rthread.gc_thread_start()
             x = None
-            for i in range(10000000):
+            for i in range(100000000):
                 x = X(x)
-                if i % 10001 == 0:
+                if i % 5001 == 0:
                     x = None
 
             state.lock.acquire(True)
diff --git a/rpython/translator/exceptiontransform.py 
b/rpython/translator/exceptiontransform.py
--- a/rpython/translator/exceptiontransform.py
+++ b/rpython/translator/exceptiontransform.py
@@ -453,10 +453,12 @@
         EXCDATA = lltype.Struct('ExcData',
             ('exc_type',  self.lltype_of_exception_type),
             ('exc_value', self.lltype_of_exception_value),
+            ('have_debug_prints', lltype.Signed),
             hints={'thread_local': True, 'is_excdata': True})
         self.EXCDATA = EXCDATA
 
         exc_data = lltype.malloc(EXCDATA, immortal=True)
+        exc_data.have_debug_prints = -1
         null_type = lltype.nullptr(self.lltype_of_exception_type.TO)
         null_value = lltype.nullptr(self.lltype_of_exception_value.TO)
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to