Skip to content

Instantly share code, notes, and snippets.

@eloraburns
Last active December 15, 2015 02:49
Show Gist options
  • Save eloraburns/5189792 to your computer and use it in GitHub Desktop.
Save eloraburns/5189792 to your computer and use it in GitHub Desktop.
Adds instrumentation to pypy-stm's transaction.py to count and report on the number of transactions actually run.
diff -r a73a349ee7ec lib_pypy/pypy_test/test_transaction.py
--- a/lib_pypy/pypy_test/test_transaction.py Wed Mar 06 23:46:03 2013 +0100
+++ b/lib_pypy/pypy_test/test_transaction.py Mon Mar 18 15:55:30 2013 -0400
@@ -76,6 +76,27 @@
assert num_foos == 1, lsts
+def test_number_of_transactions_reported():
+ before = transaction.number_of_transactions_in_last_run()
+ transaction.run()
+ after = transaction.number_of_transactions_in_last_run()
+ assert before == after
+ #assert transaction.number_of_transactions_in_last_run() == 0
+
+ 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 -r a73a349ee7ec lib_pypy/transaction.py
--- a/lib_pypy/transaction.py Wed Mar 06 23:46:03 2013 +0100
+++ b/lib_pypy/transaction.py Mon Mar 18 15:55:30 2013 -0400
@@ -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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment