[pypy-svn] r23673 - in pypy/branch/njriley-trans/pypy: interpreter module/sys module/trans rpython rpython/module translator translator/c translator/c/src translator/goal translator/tool

njriley at codespeak.net njriley at codespeak.net
Sun Feb 26 03:46:00 CET 2006


Author: njriley
Date: Sun Feb 26 03:45:53 2006
New Revision: 23673

Added:
   pypy/branch/njriley-trans/pypy/module/trans/
   pypy/branch/njriley-trans/pypy/module/trans/__init__.py
   pypy/branch/njriley-trans/pypy/module/trans/interp_trans.py
   pypy/branch/njriley-trans/pypy/module/trans/rtrans.py
   pypy/branch/njriley-trans/pypy/rpython/module/ll_trans.py
   pypy/branch/njriley-trans/pypy/rpython/module/ll_tsc.py
   pypy/branch/njriley-trans/pypy/rpython/rtsc.py
   pypy/branch/njriley-trans/pypy/translator/c/src/ll_trans.h
   pypy/branch/njriley-trans/pypy/translator/c/src/ll_tsc.h
Modified:
   pypy/branch/njriley-trans/pypy/interpreter/executioncontext.py
   pypy/branch/njriley-trans/pypy/module/sys/__init__.py
   pypy/branch/njriley-trans/pypy/module/sys/vm.py
   pypy/branch/njriley-trans/pypy/rpython/extfunctable.py
   pypy/branch/njriley-trans/pypy/translator/c/extfunc.py
   pypy/branch/njriley-trans/pypy/translator/c/gc.py
   pypy/branch/njriley-trans/pypy/translator/c/genc.py
   pypy/branch/njriley-trans/pypy/translator/c/src/exception.h
   pypy/branch/njriley-trans/pypy/translator/c/src/g_include.h
   pypy/branch/njriley-trans/pypy/translator/c/src/main.h
   pypy/branch/njriley-trans/pypy/translator/c/src/stack.h
   pypy/branch/njriley-trans/pypy/translator/c/src/thread_pthread.h
   pypy/branch/njriley-trans/pypy/translator/driver.py
   pypy/branch/njriley-trans/pypy/translator/goal/translate.py
   pypy/branch/njriley-trans/pypy/translator/tool/cbuild.py
Log:
Per-bytecode TM support; per-thread exceptions; timestamp counter
support (not transaction-safe); temporarily removed stack overflow
checking (should be back soon); a couple of translator build fixes to
work around distutils brokenness.



Modified: pypy/branch/njriley-trans/pypy/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/interpreter/executioncontext.py	(original)
+++ pypy/branch/njriley-trans/pypy/interpreter/executioncontext.py	Sun Feb 26 03:45:53 2006
@@ -1,4 +1,4 @@
-import sys
+import os, sys
 from pypy.interpreter.miscutils import Stack
 from pypy.interpreter.error import OperationError
 
@@ -15,6 +15,10 @@
         self.ticker = 0
         self.compiler = space.createcompiler()
 
+        self.tscdump = False
+        self.trans = False
+        self.ticked = False
+
     def enter(self, frame):
         if self.framestack.depth() > self.space.sys.recursionlimit:
             raise OperationError(self.space.w_RuntimeError,
@@ -58,13 +62,35 @@
 
     def bytecode_trace(self, frame):
         "Trace function called before each bytecode."
-        # First, call yield_thread() before each Nth bytecode,
-        #     as selected by sys.setcheckinterval()
-        ticker = self.ticker
-        if ticker <= 0:
-            self.space.threadlocals.yield_thread()
-            ticker = self.space.sys.checkinterval
-        self.ticker = ticker - 1
+        from pypy.module.trans import rtrans
+
+        if self.tscdump:
+            from pypy.rpython.rtsc import read_diff, reset_diff
+            from thread import get_ident
+            code = getattr(frame, 'pycode')
+            opcode = code is None and 0 or ord(code.co_code[frame.next_instr])
+            s = 'tid=%d opcode=%d t=%d inst=%d\n' % \
+                (get_ident(), opcode, int(self.ticked), read_diff())
+            if self.trans:
+                rtrans.end()
+            os.write(2, s)
+            if self.trans:
+                rtrans.begin()
+            else:
+                self.ticked = False
+            reset_diff()
+        elif self.trans:
+            rtrans.end()
+            rtrans.begin()
+        if not self.trans:
+            # First, call yield_thread() before each Nth bytecode,
+            #     as selected by sys.setcheckinterval()
+            ticker = self.ticker
+            if ticker <= 0:
+                self.space.threadlocals.yield_thread()
+                self.ticked = True
+                ticker = self.space.sys.checkinterval
+            self.ticker = ticker - 1
         if frame.w_f_trace is None or self.is_tracing:
             return
         self._do_bytecode_trace(frame)
@@ -143,6 +169,32 @@
         else:
             self.w_profilefunc = w_func
 
+    def settscdump(self, w_bool):
+        from pypy.rpython.objectmodel import we_are_translated
+        if not we_are_translated():
+            raise OperationError(self.space.w_NotImplementedError,
+                                 self.space.wrap("No access to timestamp counter in untranslated PyPy"))
+        self.tscdump = self.space.is_true(w_bool)
+
+    def settrans(self, w_bool):
+        from pypy.rpython.objectmodel import we_are_translated
+        if not we_are_translated():
+            raise OperationError(self.space.w_NotImplementedError,
+                                 self.space.wrap("No transactions in untranslated PyPy"))
+        if self.space.is_true(w_bool) is self.trans:
+            return
+        from pypy.module.trans import rtrans
+        if self.trans:
+            rtrans.end()
+            rtrans.disable()
+            self.space.threadlocals.GIL.acquire(True)
+            self.trans = False
+        else:
+            self.trans = True
+            rtrans.enable()
+            self.space.threadlocals.GIL.release()
+            rtrans.begin()
+
     def call_tracing(self, w_func, w_args):
         is_tracing = self.is_tracing
         self.is_tracing = 0

Modified: pypy/branch/njriley-trans/pypy/module/sys/__init__.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/module/sys/__init__.py	(original)
+++ pypy/branch/njriley-trans/pypy/module/sys/__init__.py	Sun Feb 26 03:45:53 2006
@@ -45,6 +45,8 @@
         'settrace'              : 'vm.settrace',
         'setprofile'            : 'vm.setprofile',
         'call_tracing'          : 'vm.call_tracing',
+        'settscdump'		: 'vm.settscdump',
+        'settrans'              : 'vm.settrans',
         
         'executable'            : 'space.wrap("py.py")', 
         'copyright'             : 'space.wrap("MIT-License")', 

Modified: pypy/branch/njriley-trans/pypy/module/sys/vm.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/module/sys/vm.py	(original)
+++ pypy/branch/njriley-trans/pypy/module/sys/vm.py	Sun Feb 26 03:45:53 2006
@@ -113,3 +113,18 @@
 saved, and restored afterwards.  This is intended to be called from
 a debugger from a checkpoint, to recursively debug some other code."""
     return space.getexecutioncontext().call_tracing(w_func, w_args)
+
+def settscdump(space, w_bool):
+    """settscdump(bool)
+
+If true, tell the Python interpreter to dump VM measurements to
+stderr.  If false, turn off dump.  The measurements are based on the
+processor's time-stamp counter."""
+    space.getexecutioncontext().settscdump(w_bool)
+
+def settrans(space, w_bool):
+    """settrans(bool)
+
+If true, release the global interpreter lock and wrap each bytecode
+execution in a transaction on the current thread."""
+    space.getexecutioncontext().settrans(w_bool)

Added: pypy/branch/njriley-trans/pypy/module/trans/__init__.py
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/module/trans/__init__.py	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,16 @@
+from pypy.interpreter.mixedmodule import MixedModule
+
+class Module(MixedModule):
+    """Use explicit hardware-supported transactions from Python."""
+
+    appleveldefs = {
+    }
+
+    interpleveldefs = {
+        'begin'   : 'interp_trans.begin',
+        'end'     : 'interp_trans.end',
+        'abort'   : 'interp_trans.abort',
+        'pause'   : 'interp_trans.pause',
+        'unpause' : 'interp_trans.unpause',
+        'verbose' : 'interp_trans.verbose',
+    }

Added: pypy/branch/njriley-trans/pypy/module/trans/interp_trans.py
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/module/trans/interp_trans.py	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,27 @@
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.baseobjspace import ObjSpace
+from pypy.module.trans import rtrans
+
+def begin(space):
+    rtrans.begin()
+    return space.w_None
+
+def end(space):
+    rtrans.end()
+    return space.w_None
+
+def abort(space):
+    rtrans.abort()
+    return space.w_None
+
+def pause(space):
+    rtrans.pause()
+    return space.w_None
+
+def unpause(space):
+    rtrans.unpause()
+    return space.w_None
+
+def verbose(space):
+    rtrans.verbose()
+    return space.w_None

Added: pypy/branch/njriley-trans/pypy/module/trans/rtrans.py
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/module/trans/rtrans.py	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,27 @@
+# dummy implementations
+
+import os
+
+def begin():
+    os.write(2, '= rtrans.begin\n')
+
+def end():
+    os.write(2, '= rtrans.end\n')
+
+def abort():
+    os.write(2, '= rtrans.abort\n')
+
+def pause():
+    os.write(2, '= rtrans.pause\n')
+
+def unpause():
+    os.write(2, '= rtrans.unpause\n')
+
+def verbose():
+    os.write(2, '= rtrans.verbose\n')
+
+def enable():
+    os.write(2, '= rtrans.enable\n')
+
+def disable():
+    os.write(2, '= rtrans.disable\n')

Modified: pypy/branch/njriley-trans/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/rpython/extfunctable.py	(original)
+++ pypy/branch/njriley-trans/pypy/rpython/extfunctable.py	Sun Feb 26 03:45:53 2006
@@ -233,6 +233,25 @@
                                                   'll_stackless/switch'))
 
 # ___________________________________________________________
+# timestamp counter
+from pypy.rpython import rtsc
+declare(rtsc.read, r_longlong, 'll_tsc/read')
+declare(rtsc.read_diff, int, 'll_tsc/read_diff')
+declare(rtsc.reset_diff, noneannotation, 'll_tsc/reset_diff')
+
+# ___________________________________________________________
+# transactional execution
+from pypy.module.trans import rtrans
+declare(rtrans.begin , noneannotation, 'll_trans/begin')
+declare(rtrans.end   , noneannotation, 'll_trans/end')
+declare(rtrans.abort , noneannotation, 'll_trans/abort')
+declare(rtrans.pause , noneannotation, 'll_trans/pause')
+declare(rtrans.unpause, noneannotation, 'll_trans/unpause')
+declare(rtrans.verbose, noneannotation, 'll_trans/verbose')
+declare(rtrans.enable, noneannotation, 'll_trans/enable')
+declare(rtrans.disable, noneannotation, 'll_trans/disable')
+
+# ___________________________________________________________
 # javascript
 from pypy.rpython import rjs
 declare(rjs.jseval, str, 'll_js/jseval')

Added: pypy/branch/njriley-trans/pypy/rpython/module/ll_trans.py
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/rpython/module/ll_trans.py	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,31 @@
+def ll_trans_begin():
+    pass
+ll_trans_begin.suggested_primitive = True
+
+def ll_trans_end():
+    pass
+ll_trans_end.suggested_primitive = True
+
+def ll_trans_abort():
+    pass
+ll_trans_abort.suggested_primitive = True
+
+def ll_trans_pause():
+    pass
+ll_trans_pause.suggested_primitive = True
+
+def ll_trans_unpause():
+    pass
+ll_trans_unpause.suggested_primitive = True
+
+def ll_trans_verbose():
+    pass
+ll_trans_verbose.suggested_primitive = True
+
+def ll_trans_enable():
+    pass
+ll_trans_enable.suggested_primitive = True
+
+def ll_trans_disable():
+    pass
+ll_trans_disable.suggested_primitive = True

Added: pypy/branch/njriley-trans/pypy/rpython/module/ll_tsc.py
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/rpython/module/ll_tsc.py	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,13 @@
+from pypy.rpython.rarithmetic import r_longlong
+
+def ll_tsc_read():
+    return r_longlong(0)
+ll_tsc_read.suggested_primitive = True
+
+def ll_tsc_read_diff():
+    return 0
+ll_tsc_read_diff.suggested_primitive = True
+
+def ll_tsc_reset_diff():
+    pass
+ll_tsc_reset_diff.suggested_primitive = True

Added: pypy/branch/njriley-trans/pypy/rpython/rtsc.py
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/rpython/rtsc.py	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,8 @@
+def read():
+    raise NotImplementedError("only works in translated versions")
+
+def read_diff():
+    raise NotImplementedError("only works in translated versions")
+
+def reset_diff():
+    raise NotImplementedError("only works in translated versions")

Modified: pypy/branch/njriley-trans/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/extfunc.py	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/extfunc.py	Sun Feb 26 03:45:53 2006
@@ -5,7 +5,8 @@
 from pypy.rpython.rstr import STR
 from pypy.rpython import rlist, rstr
 from pypy.rpython.module import ll_os, ll_time, ll_math, ll_strtod
-from pypy.rpython.module import ll_stackless, ll_stack
+from pypy.rpython.module import ll_stackless, ll_stack, ll_tsc
+from pypy.rpython.module import ll_trans
 from pypy.module.thread.rpython import ll_thread
 from pypy.module._socket.rpython import ll__socket
 
@@ -54,6 +55,17 @@
     ll_thread.ll_fused_releaseacquirelock: 'LL_thread_fused_releaseacquirelock',
     ll_thread.ll_thread_start:     'LL_thread_start',
     ll_thread.ll_thread_get_ident: 'LL_thread_get_ident',
+    ll_trans.ll_trans_begin:   'LL_trans_begin',
+    ll_trans.ll_trans_end:     'LL_trans_end',
+    ll_trans.ll_trans_abort:   'LL_trans_abort',
+    ll_trans.ll_trans_pause:   'LL_trans_pause',
+    ll_trans.ll_trans_unpause: 'LL_trans_unpause',
+    ll_trans.ll_trans_verbose: 'LL_trans_verbose',
+    ll_trans.ll_trans_enable:  'LL_trans_enable',
+    ll_trans.ll_trans_disable: 'LL_trans_disable',
+    ll_tsc.ll_tsc_read:      'LL_tsc_read',
+    ll_tsc.ll_tsc_read_diff: 'LL_tsc_read_diff',
+    ll_tsc.ll_tsc_reset_diff:'LL_tsc_reset_diff',
     ll_stackless.ll_stackless_switch:             'LL_stackless_switch',
     ll_stackless.ll_stackless_stack_frames_depth: 'LL_stackless_stack_frames_depth',
     ll_stack.ll_stack_unwind: 'LL_stack_unwind',

Modified: pypy/branch/njriley-trans/pypy/translator/c/gc.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/gc.py	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/gc.py	Sun Feb 26 03:45:53 2006
@@ -119,8 +119,7 @@
     def OP_GC_FETCH_EXCEPTION(self, funcgen, op, err):
         result = funcgen.expr(op.result)
         return ('%s = rpython_exc_value;\n'
-                'rpython_exc_type = NULL;\n'
-                'rpython_exc_value = NULL;') % (result, )
+                '_RPySetException(NULL, NULL)') % (result, )
 
     def OP_GC_RESTORE_EXCEPTION(self, funcgen, op, err):
         argh = funcgen.expr(op.args[0])

Modified: pypy/branch/njriley-trans/pypy/translator/c/genc.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/genc.py	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/genc.py	Sun Feb 26 03:45:53 2006
@@ -1,4 +1,5 @@
 import autopath
+import os
 import py
 from pypy.translator.c.node import PyObjectNode
 from pypy.translator.c.database import LowLevelDatabase
@@ -21,11 +22,12 @@
     stackless = False
     use_stackless_transformation = False
     
-    def __init__(self, translator, entrypoint, gcpolicy=None, libraries=None, thread_enabled=False):
+    def __init__(self, translator, entrypoint, gcpolicy=None, libraries=None, thread_enabled=False, tsc_enabled=False):
         self.translator = translator
         self.entrypoint = entrypoint
         self.gcpolicy = gcpolicy
         self.thread_enabled = thread_enabled
+        self.tsc_enabled = tsc_enabled
 
         if libraries is None:
             libraries = []
@@ -84,6 +86,8 @@
                 if self.use_stackless_transformation: #set in test_stackless.py
                     from pypy.translator.backendopt.stackless import stackless
                     stackless(translator, db.stacklessdata)
+            if self.tsc_enabled:
+                defines['USE_TSC'] = '1'
             cfile, extra = gen_source_standalone(db, modulename, targetdir,
                                                  entrypointname = pfname,
                                                  defines = defines)
@@ -202,10 +206,14 @@
         args = ['-L'+path for path in compiler.library_dirs]
         print >> f, 'LIBDIRS =', ' '.join(args)
         args = ['-I'+path for path in compiler.include_dirs]
+        cppflags = os.getenv('CPPFLAGS')
+        if cppflags: args.insert(0, cppflags)
         write_list(args, 'INCLUDEDIRS =')
         print >> f
-        print >> f, 'CFLAGS =', ' '.join(compiler.compile_extra)
-        print >> f, 'LDFLAGS =', ' '.join(compiler.link_extra)
+        print >> f, 'CFLAGS =', ' '.join(compiler.compile_extra), \
+              os.getenv('CFLAGS', '')
+        print >> f, 'LDFLAGS =', ' '.join(compiler.link_extra), \
+              os.getenv('LDFLAGS', '')
         print >> f, MAKEFILE.strip()
         f.close()
 

Modified: pypy/branch/njriley-trans/pypy/translator/c/src/exception.h
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/src/exception.h	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/src/exception.h	Sun Feb 26 03:45:53 2006
@@ -11,26 +11,48 @@
 /******************************************************************/
 
 #ifdef PYPY_NOT_MAIN_FILE
+#ifdef WITH_THREAD
+extern RPyThreadTLS             rpython_exc_type_key;
+extern RPyThreadTLS             rpython_exc_value_key;
+#else
 extern RPYTHON_EXCEPTION_VTABLE	rpython_exc_type;
 extern RPYTHON_EXCEPTION	rpython_exc_value;
+#endif /* WITH_THREAD */
+#else
+#ifdef WITH_THREAD
+RPyThreadTLS            	rpython_exc_type_key = 0;
+RPyThreadTLS            	rpython_exc_value_key = 0;
 #else
 RPYTHON_EXCEPTION_VTABLE	rpython_exc_type = NULL;
 RPYTHON_EXCEPTION		rpython_exc_value = NULL;
-#endif
+#endif /* WITH_THREAD */
+#endif /* PYPY_NOT_MAIN_FILE */
+
+#ifdef WITH_THREAD
+#define rpython_exc_type						\
+	((RPYTHON_EXCEPTION_VTABLE)RPyThreadTLS_Get(rpython_exc_type_key))
+#define rpython_exc_value						\
+	((RPYTHON_EXCEPTION)RPyThreadTLS_Get(rpython_exc_value_key))
+#define _RPySetException(etype, evalue)                                 \
+		RPyThreadTLS_Set(rpython_exc_type_key, etype);       \
+		RPyThreadTLS_Set(rpython_exc_value_key, evalue);
+#else
+#define _RPySetException(etype, evalue)			\
+		rpython_exc_type = etype;		\
+		rpython_exc_value = evalue
+#endif /* WITH_THREAD */
 
 #define RPyExceptionOccurred()	(rpython_exc_type != NULL)
 
 #define RPyRaiseException(etype, evalue)	do {	\
 		assert(!RPyExceptionOccurred());	\
-		rpython_exc_type = etype;		\
-		rpython_exc_value = evalue;		\
+		_RPySetException(etype, evalue);	\
 	} while (0)
 
 #define RPyFetchException(etypevar, evaluevar, type_of_evaluevar) do {  \
 		etypevar = rpython_exc_type;				\
 		evaluevar = (type_of_evaluevar) rpython_exc_value;	\
-		rpython_exc_type = NULL;				\
-		rpython_exc_value = NULL;				\
+		_RPySetException(NULL, NULL);				\
 	} while (0)
 
 #define RPyMatchException(etype)	RPYTHON_EXCEPTION_MATCH(rpython_exc_type,  \
@@ -61,8 +83,7 @@
 {
 	/* XXX 1. uses officially bad fishing */
 	/* XXX 2. msg is ignored */
-	rpython_exc_type = rexc->o_typeptr;
-	rpython_exc_value = rexc;
+	_RPySetException(rexc->o_typeptr, rexc);
 	PUSH_ALIVE(rpython_exc_value);
 }
 
@@ -74,7 +95,7 @@
 	assert(PyErr_Occurred());
 	assert(!RPyExceptionOccurred());
 	PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
-	/* XXX loosing the error message here */
+	/* XXX losing the error message here */
 	rpython_exc_value = RPYTHON_PYEXCCLASS2EXC(exc_type);
 	rpython_exc_type = RPYTHON_TYPE_OF_EXC_INST(rpython_exc_value);
 }

Modified: pypy/branch/njriley-trans/pypy/translator/c/src/g_include.h
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/src/g_include.h	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/src/g_include.h	Sun Feb 26 03:45:53 2006
@@ -40,6 +40,8 @@
 #  include "src/ll_thread.h"
 #  include "src/ll_stackless.h"
 #  include "src/ll__socket.h"
+#  include "src/ll_trans.h"
+#  include "src/ll_tsc.h"
 #endif
 
 #include "src/stack.h"

Added: pypy/branch/njriley-trans/pypy/translator/c/src/ll_trans.h
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/translator/c/src/ll_trans.h	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,106 @@
+/************************************************************/
+/***  C header subsection: transactional memory support   ***/
+
+/* prototypes */
+void LL_trans_begin(void);
+void LL_trans_end(void);
+void LL_trans_abort(void);
+void LL_trans_pause(void);
+void LL_trans_unpause(void);
+void LL_trans_verbose(void);
+void LL_trans_enable(void);
+void LL_trans_disable(void);
+
+/* implementations */
+
+#ifndef PYPY_NOT_MAIN_FILE
+
+#include <trans/primitives.h>
+
+void
+LL_trans_begin(void)
+{
+	XACT_BEGIN;
+}
+
+void
+LL_trans_end(void)
+{
+	XACT_END;
+}
+
+void
+LL_trans_abort(void)
+{
+	XACT_ABORT(&&abort);
+	XACT_END;
+
+ abort:
+	;
+}
+
+/* XXX deliberately not RPyThreadTLS here => dependency problems */
+static pthread_key_t pause_state_key = 0;
+
+void
+LL_trans_pause(void)
+{
+	int *pause_state;
+	if (pause_state_key == 0)
+		assert(pthread_key_create(&pause_state_key, free) == 0);
+	pause_state = (int *)pthread_getspecific(pause_state_key);
+	if (pause_state == NULL) {
+		pause_state = malloc(sizeof(int));
+		assert(pthread_setspecific(pause_state_key, pause_state) == 0);
+	}
+	XACT_PAUSE(*pause_state);
+}
+
+void
+LL_trans_unpause(void)
+{
+	int *pause_state = (int *)pthread_getspecific(pause_state_key);
+	assert(pause_state != NULL);
+	XACT_UNPAUSE(*pause_state);
+}
+
+void
+LL_trans_verbose(void)
+{
+	MY_MAGIC1(trans_verbose);
+}
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+
+void
+LL_trans_enable(void)
+{
+	int ret_val;
+	ret_val = enable_transactions();
+	assert(ret_val == 0);
+	// XXX HACK HACK HACK, 1024 is first thread id
+	if (pthread_self() == 1024) {
+		static int suspended = 0;
+		if (suspended)
+			return;
+		suspended = 1;
+		pid_t pid = getpid();
+		fprintf(stderr, "LL_trans_enable: suspending, pid is %d\n", pid);
+		kill(pid, SIGSTOP);
+	}
+	XACT_BEGIN;
+	XACT_PAUSE(ret_val);
+	set_auto_xact(1);
+	XACT_UNPAUSE(ret_val);
+	XACT_END;
+}
+
+void
+LL_trans_disable(void)
+{
+	set_auto_xact(0);
+}
+
+#endif /* PYPY_NOT_MAIN_FILE */

Added: pypy/branch/njriley-trans/pypy/translator/c/src/ll_tsc.h
==============================================================================
--- (empty file)
+++ pypy/branch/njriley-trans/pypy/translator/c/src/ll_tsc.h	Sun Feb 26 03:45:53 2006
@@ -0,0 +1,85 @@
+/************************************************************/
+/***  C header subsection: timestamp counter access       ***/
+
+
+#if defined(USE_TSC)
+
+typedef unsigned long long uint64;
+
+/* prototypes */
+
+uint64 LL_tsc_read(void);
+long LL_tsc_read_diff(void);
+void LL_tsc_reset_diff(void);
+
+/* implementations */
+
+#ifndef PYPY_NOT_MAIN_FILE
+
+#if defined(__alpha__)
+
+#define rdtscll(pcc) asm volatile ("rpcc %0" : "=r" (pcc))
+
+#elif defined(__ppc__)
+
+#define rdtscll(var) ppc_getcounter(&var)
+
+static void
+ppc_getcounter(uint64 *v)
+{
+	register unsigned long tbu, tb, tbu2;
+
+  loop:
+	asm volatile ("mftbu %0" : "=r" (tbu) );
+	asm volatile ("mftb  %0" : "=r" (tb)  );
+	asm volatile ("mftbu %0" : "=r" (tbu2));
+	if (__builtin_expect(tbu != tbu2, 0)) goto loop;
+
+	((long*)(v))[0] = tbu;
+	((long*)(v))[1] = tb;
+}
+
+#else /* this section is for linux/x86 */
+
+#define rdtscll(val) asm volatile ("rdtsc" : "=A" (val))
+
+#endif
+
+uint64
+LL_tsc_read(void)
+{
+	uint64 tsc;
+	rdtscll(tsc);
+
+	return tsc;
+}
+
+static uint64 tsc_last = 0;
+
+/* don't use for too long a diff, overflow problems:
+   http://www.sandpile.org/post/msgs/20003444.htm */
+
+long
+LL_tsc_read_diff(void)
+{
+	uint64 new_tsc;
+	unsigned long tsc_diff;
+
+	/* returns garbage the first time you call it */
+	rdtscll(new_tsc);
+	tsc_diff = new_tsc - tsc_last;
+	tsc_last = new_tsc;
+	
+	return tsc_diff;
+}
+
+void
+LL_tsc_reset_diff(void)
+{
+	rdtscll(tsc_last);
+}
+
+#endif /* PYPY_NOT_MAIN_FILE */
+
+#endif /* defined(USE_TSC) */
+

Modified: pypy/branch/njriley-trans/pypy/translator/c/src/main.h
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/src/main.h	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/src/main.h	Sun Feb 26 03:45:53 2006
@@ -23,6 +23,18 @@
     errmsg = RPython_StartupCode();
     if (errmsg) goto error;
 
+#ifdef WITH_THREAD
+    int err;
+    if ( (err = pthread_key_create(&rpython_exc_type_key, free)) != 0) {
+	errmsg = strerror(err);
+	goto error;
+    }
+    if ( (err = pthread_key_create(&rpython_exc_value_key, free)) != 0) {
+	errmsg = strerror(err);
+	goto error;
+    }
+#endif
+
     list = _RPyListOfString_New(argc);
     if (RPyExceptionOccurred()) goto memory_out;
     for (i=0; i<argc; i++) {

Modified: pypy/branch/njriley-trans/pypy/translator/c/src/stack.h
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/src/stack.h	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/src/stack.h	Sun Feb 26 03:45:53 2006
@@ -31,6 +31,8 @@
 
 int LL_stack_too_big(void)
 {
+#warning noop pending fix for lower-overhead TLS
+#if 0
 	char local;
 	long diff;
 	char *baseptr;
@@ -94,6 +96,7 @@
 	baseptr = &local;
 	RPyThreadTLS_Set(stack_base_pointer_key, baseptr);
 	stack_base_pointer = baseptr;
+#endif 
 	return 0;
 }
 

Modified: pypy/branch/njriley-trans/pypy/translator/c/src/thread_pthread.h
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/c/src/thread_pthread.h	(original)
+++ pypy/branch/njriley-trans/pypy/translator/c/src/thread_pthread.h	Sun Feb 26 03:45:53 2006
@@ -84,6 +84,14 @@
 int RPyThreadAcquireLock(struct RPyOpaque_ThreadLock *lock, int waitflag);
 void RPyThreadReleaseLock(struct RPyOpaque_ThreadLock *lock);
 
+/* Thread-local storage */
+#define RPyThreadTLS                    pthread_key_t
+#define RPyThreadTLS_Get(key)		pthread_getspecific(key)
+#define RPyThreadTLS_Set(key, value)	pthread_setspecific(key, value)
+
+#ifdef USE_SEMAPHORES
+
+#endif
 
 /* implementations */
 
@@ -317,8 +325,6 @@
 
 
 /* Thread-local storage */
-#define RPyThreadTLS	pthread_key_t
-
 char *RPyThreadTLS_Create(RPyThreadTLS *result)
 {
 	if (pthread_key_create(result, NULL) != 0)
@@ -327,8 +333,4 @@
 		return NULL;
 }
 
-#define RPyThreadTLS_Get(key)		pthread_getspecific(key)
-#define RPyThreadTLS_Set(key, value)	pthread_setspecific(key, value)
-
-
 #endif /* PYPY_NOT_MAIN_FILE */

Modified: pypy/branch/njriley-trans/pypy/translator/driver.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/driver.py	(original)
+++ pypy/branch/njriley-trans/pypy/translator/driver.py	Sun Feb 26 03:45:53 2006
@@ -20,6 +20,7 @@
   'thread': False, # influences GC policy
 
   'stackless': False,
+  'tsc': False,
   'debug': True,
   'insist': False,
   'backend': 'c',
@@ -217,7 +218,8 @@
             from pypy.translator.c.genc import CExtModuleBuilder as CBuilder
         cbuilder = CBuilder(self.translator, self.entry_point,
                             gcpolicy       = gcpolicy,
-                            thread_enabled = getattr(opt, 'thread', False))
+                            thread_enabled = getattr(opt, 'thread', False),
+                            tsc_enabled    = getattr(opt, 'tsc', False))
         cbuilder.stackless = opt.stackless
         database = cbuilder.build_database()
         self.log.info("database for generating C source was created")

Modified: pypy/branch/njriley-trans/pypy/translator/goal/translate.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/goal/translate.py	(original)
+++ pypy/branch/njriley-trans/pypy/translator/goal/translate.py	Sun Feb 26 03:45:53 2006
@@ -50,7 +50,8 @@
 
     '2_gc': [OPT(('--gc',), "Garbage collector", ['boehm', 'ref', 'none'])],
     '3_stackless': [OPT(('--stackless',), "Stackless code generation", True)],
-    '4_merge_if_blocks': [OPT(('--no-if-blocks-merge',), "Do not merge if ... elif ... chains and use a switch statement for them.", False)],
+    '4_tsc': [OPT(('--tsc',), "(x86, PowerPC, Alpha) Timestamp counter profile", True)],
+    '5_merge_if_blocks': [OPT(('--no-if-blocks-merge',), "Do not merge if ... elif ... chains and use a switch statement for them.", False)],
     },
 
 
@@ -102,6 +103,7 @@
     'gc': 'boehm',
     'backend': 'c',
     'stackless': False,
+    'tsc': False,
     'merge_if_blocks': True,
     
     'batch': False,

Modified: pypy/branch/njriley-trans/pypy/translator/tool/cbuild.py
==============================================================================
--- pypy/branch/njriley-trans/pypy/translator/tool/cbuild.py	(original)
+++ pypy/branch/njriley-trans/pypy/translator/tool/cbuild.py	Sun Feb 26 03:45:53 2006
@@ -306,6 +306,11 @@
     def _build(self):
         from distutils.ccompiler import new_compiler 
         compiler = new_compiler()
+        from distutils.sysconfig import get_config_vars, customize_compiler
+        # customize_compiler is stupid and uses OPT even if you pick a
+        # different compiler from the one used to compile Python
+        if os.environ.get('CC'): get_config_vars()['OPT'] = ''
+        customize_compiler(compiler)
         compiler.spawn = log_spawned_cmd(compiler.spawn)
         objects = []
         for cfile in self.cfilenames: 
@@ -320,10 +325,15 @@
                 assert cobjfile.check()
                 objects.append(str(cobjfile))
             finally: 
-                old.chdir() 
+                old.chdir()
+        # customize_compiler also assumes LDFLAGS only applies to
+        # building shared libraries
+        from distutils.util import split_quoted
+        ldflags = split_quoted(os.environ.get('LDFLAGS'))
         compiler.link_executable(objects, str(self.outputfilename),
                                  libraries=self.libraries,
                                  extra_preargs=self.link_extra,
+                                 extra_postargs=ldflags,
                                  library_dirs=self.library_dirs)
 
 def build_executable(*args, **kwds):



More information about the Pypy-commit mailing list