[pypy-commit] pypy stm-thread-2: Adds instrumentation to transaction.py to report on the number of transactions run during a call to run().

taavi_burns noreply at buildbot.pypy.org
Mon Mar 18 21:15:57 CET 2013


Author: Taavi Burns <taavi.burns at gmail.com>
Branch: stm-thread-2
Changeset: r62403:7a29f5163381
Date: 2013-03-18 16:13 -0400
http://bitbucket.org/pypy/pypy/changeset/7a29f5163381/

Log:	Adds instrumentation to transaction.py to report on the number of
	transactions run during a call to run().

diff --git a/lib_pypy/pypy_test/test_transaction.py b/lib_pypy/pypy_test/test_transaction.py
--- a/lib_pypy/pypy_test/test_transaction.py
+++ b/lib_pypy/pypy_test/test_transaction.py
@@ -76,6 +76,21 @@
         assert num_foos == 1, lsts
 
 
+def test_number_of_transactions_reported():
+    transaction.add(lambda: None)
+    transaction.run()
+    assert transaction.number_of_transactions_in_last_run() == 1
+
+    def add_transactions(l):
+        if l:
+            for x in range(l[0]):
+                transaction.add(add_transactions, l[1:])
+
+    transaction.add(add_transactions, [10, 10, 10])
+    transaction.run()
+    assert transaction.number_of_transactions_in_last_run() == 1111
+
+
 def run_tests():
     for name in sorted(globals().keys()):
         if name.startswith('test_'):
diff --git a/lib_pypy/transaction.py b/lib_pypy/transaction.py
--- a/lib_pypy/transaction.py
+++ b/lib_pypy/transaction.py
@@ -96,6 +96,9 @@
         tpool.teardown()
     tpool.reraise()
 
+def number_of_transactions_in_last_run():
+    return _thread_pool.transactions_run
+
 # ____________________________________________________________
 
 
@@ -104,6 +107,7 @@
     def __init__(self):
         self.num_threads = 4    # XXX default value, tweak
         self.in_transaction = False
+        self.transactions_run = None
 
     def setup(self):
         # a mutex to protect parts of _grab_next_thing_to_do()
@@ -122,17 +126,20 @@
         _thread_local.pending = None
         #
         self.num_waiting_threads = 0
+        self.transactions_run = 0
         self.finished = False
         self.got_exception = []
         self.in_transaction = True
 
     def run(self):
         # start the N threads
-        for i in range(self.num_threads):
-            thread.start_new_thread(self._run_thread, ())
+        task_counters = [[0] for i in range(self.num_threads)]
+        for counter in task_counters:
+            thread.start_new_thread(self._run_thread, (counter,))
         # now wait.  When we manage to acquire the following lock, then
         # we are finished.
         self.lock_if_released_then_finished.acquire()
+        self.transactions_run = sum(x[0] for x in task_counters)
 
     def teardown(self):
         self.in_transaction = False
@@ -148,13 +155,14 @@
         if exc:
             raise exc[0], exc[1], exc[2]    # exception, value, traceback
 
-    def _run_thread(self):
+    def _run_thread(self, counter):
         tloc_pending = _thread_local.pending
         got_exception = self.got_exception
         try:
             while True:
                 self._do_it(self._grab_next_thing_to_do(tloc_pending),
                             got_exception)
+                counter[0] += 1
         except _Done:
             pass
 


More information about the pypy-commit mailing list