[pypy-commit] lang-smalltalk strategies-tagging: Added sorted output of strategy statistics, added other flag to control the output, added test.

anton_gulenko noreply at buildbot.pypy.org
Tue Mar 25 14:31:44 CET 2014


Author: Anton Gulenko <anton.gulenko at googlemail.com>
Branch: strategies-tagging
Changeset: r690:a58baa9918d7
Date: 2014-03-21 12:50 +0100
http://bitbucket.org/pypy/lang-smalltalk/changeset/a58baa9918d7/

Log:	Added sorted output of strategy statistics, added other flag to
	control the output, added test.

diff --git a/spyvm/model.py b/spyvm/model.py
--- a/spyvm/model.py
+++ b/spyvm/model.py
@@ -24,6 +24,7 @@
 from rpython.tool.pairtype import extendabletype
 from rpython.rlib.objectmodel import instantiate, compute_hash, import_from_mixin, we_are_translated
 from rpython.rtyper.lltypesystem import lltype, rffi
+from rpython.rlib.listsort import TimSort
 from rsdl import RSDL, RSDL_helper
 
 class W_Object(object):
@@ -613,12 +614,24 @@
                                 className='W_PointersObject',
                                 additionalInformation='len=%d' % self.size())
 
+class StatsSorter(TimSort):
+    def lt(self, a, b):
+        if a[0] == b[0]:
+            if a[1] == b[1]:
+                return a[2] < b[2]
+            else:
+                return a[1] < b[1]
+        else:
+            return a[0] < b[0]
+
 class StrategyStatistics(object):
     # Key: (operation_name, old_strategy, new_strategy)
     # Value: [sizes]
     stats = {}
     do_log = False
     do_stats = False
+    do_stats_sizes = False
+    
     def stat_operation(self, operation_name, old_strategy, new_strategy, size):
         key = (operation_name, old_strategy, new_strategy)
         if not key in self.stats:
@@ -626,14 +639,19 @@
         self.stats[key].append(size)
     def log_operation(self, op, new_strategy_tag, old_strategy_tag, classname, size):
         print "%s (%s, was %s) of %s size %d" % (op, new_strategy_tag, old_strategy_tag, classname, size)
+    def sorted_keys(self):
+        keys = [ x for x in self.stats ]
+        StatsSorter(keys).sort()
+        return keys
     def print_stats(self):
-        for key in self.stats:
+        for key in self.sorted_keys():
             sizes = self.stats[key]
             sum = 0
             for s in sizes:
                 sum += s
             print "%s: %d times, avg size: %d" % (key, len(sizes), sum/len(sizes))
-            print "       All sizes: %s" % sizes
+            if self.do_stats_sizes:
+                print "       All sizes: %s" % sizes
 strategy_stats = StrategyStatistics()
 
 class W_PointersObject(W_AbstractPointersObject):
diff --git a/spyvm/test/test_strategies.py b/spyvm/test/test_strategies.py
--- a/spyvm/test/test_strategies.py
+++ b/spyvm/test/test_strategies.py
@@ -170,4 +170,21 @@
     a.store(space, 1, space.wrap_int(2))
     assert isinstance(a.strategy, strategies.ListStorageStrategy)
     check_arr(a, [1.2, 2, w_nil, w_nil, w_nil])
+
+def test_statistics():
+    stats = model.StrategyStatistics()
+    stats.stat_operation("B", "old", "new", 3)
+    stats.stat_operation("B", "old", "new", 4)
+    stats.stat_operation("B", "old2", "new2", 20)
+    stats.stat_operation("B", "old", "new", 5)
+    stats.stat_operation("A", "old", "new", 1)
+    stats.stat_operation("A", "old", "new", 2)
+    stats.stat_operation("C", "old", "new", 10)
+    stats.stat_operation("C", "old", "new", 11)
+    keys = stats.sorted_keys()
+    assert keys == [ ("A", "old", "new"), ("B", "old", "new"), ("B", "old2", "new2"), ("C", "old", "new") ]
+    assert stats.stats[keys[0]] == [1, 2]
+    assert stats.stats[keys[1]] == [3, 4, 5]
+    assert stats.stats[keys[2]] == [20]
+    assert stats.stats[keys[3]] == [10, 11]
     
\ No newline at end of file
diff --git a/targetimageloadingsmalltalk.py b/targetimageloadingsmalltalk.py
--- a/targetimageloadingsmalltalk.py
+++ b/targetimageloadingsmalltalk.py
@@ -128,6 +128,7 @@
           -p|--poll_events
           --strategy-log
           --strategy-stats
+          --strategy-stats-with-sizes
           [image path, default: Squeak.image]
     """ % argv[0]
 
@@ -188,6 +189,9 @@
             model.strategy_stats.do_log = True
         elif arg == "--strategy-stats":
             model.strategy_stats.do_stats = True
+        elif arg == "--strategy-stats-with-sizes":
+            model.strategy_stats.do_stats = True
+            model.strategy_stats.do_stats_sizes = True
         elif path is None:
             path = argv[idx]
         else:


More information about the pypy-commit mailing list