[pypy-svn] r31192 - in pypy/dist/pypy: interpreter objspace/std tool

mwh at codespeak.net mwh at codespeak.net
Wed Aug 9 12:34:23 CEST 2006


Author: mwh
Date: Wed Aug  9 12:34:22 2006
New Revision: 31192

Added:
   pypy/dist/pypy/tool/readdictinfo.py
Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/objspace/std/dictmultiobject.py
Log:
make the MeasuringDictImplemetation work in a translated pypy-c.
dumps information to a file called 'dictinfo.txt' in the local directory, you
can use the new pypy/tool/readdictinfo.py to make sense of this.


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Wed Aug  9 12:34:22 2006
@@ -163,6 +163,10 @@
         w_exitfunc = self.sys.getdictvalue_w(self, 'exitfunc')
         if w_exitfunc is not None:
             self.call_function(w_exitfunc)
+        # XXX ugh:
+        from pypy.objspace.std.dictmultiobject import MEASURE_DICT, report
+        if MEASURE_DICT:
+            report()
     
     def __repr__(self):
         try:

Modified: pypy/dist/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictmultiobject.py	(original)
+++ pypy/dist/pypy/objspace/std/dictmultiobject.py	Wed Aug  9 12:34:22 2006
@@ -1,7 +1,7 @@
 from pypy.objspace.std.objspace import *
 from pypy.interpreter import gateway
 
-from pypy.rpython.objectmodel import r_dict
+from pypy.rpython.objectmodel import r_dict, we_are_translated
 
 class DictImplementation(object):
     
@@ -116,10 +116,10 @@
 MEASURE_DICT = False
 
 if MEASURE_DICT:
-    import time
+    import time, py
     _dict_infos = []
 
-    class DictInfo:
+    class DictInfo(object):
         def __init__(self):
             self.getitems = 0;  self.setitems = 0; self.delitems = 0
             self.lengths = 0;   self.clears = 0;   self.has_keys = 0;  self.gets = 0
@@ -140,14 +140,19 @@
             self.createtime = time.time()
             self.lifetime = -1.0
 
-            # very probable stack from here:
-            # 0 - us
-            # 1 - MeasuringDictImplementation.__init__
-            # 2 - W_DictMultiObject.__init__
-            # 3 - space.newdict
-            # 4 - newdict's caller.  let's look at that
-            frame = sys._getframe(4)
-            self.sig = '(%s:%s)%s'%(frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name)
+            if not we_are_translated():
+                # very probable stack from here:
+                # 0 - us
+                # 1 - MeasuringDictImplementation.__init__
+                # 2 - W_DictMultiObject.__init__
+                # 3 - space.newdict
+                # 4 - newdict's caller.  let's look at that
+                try:
+                    frame = sys._getframe(4)
+                except ValueError:
+                    pass # might be at import time
+                else:
+                    self.sig = '(%s:%s)%s'%(frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name)
 
             _dict_infos.append(self)
         def __repr__(self):
@@ -252,6 +257,31 @@
             self.info.listings += 1
             return self.content.items()
 
+    _example = DictInfo()
+    del _dict_infos[-1]
+    tmpl = '''
+        os.write(fd, "%(attr)s")
+        os.write(fd, ": ")
+        os.write(fd, str(info.%(attr)s))
+        os.write(fd, "\\n")
+    '''
+    bodySrc = []
+    for attr in _example.__dict__:
+        if attr == 'sig':
+            continue
+        bodySrc.append(tmpl%locals())
+    exec py.code.Source('''
+    def _report_one(fd, info):
+        %s
+    '''%''.join(bodySrc)).compile()
+
+    def report():
+        fd = os.open('dictinfo.txt', os.O_CREAT|os.O_WRONLY, 0644)
+        for info in _dict_infos:
+            os.write(fd, '------------------\n')
+            _report_one(fd, info)
+        os.close(fd)
+
     def reportDictInfo():
         d = {}
         if not _dict_infos:
@@ -278,8 +308,8 @@
             print '('+str(stillAlive), 'still alive)'
         print d
 
-    import atexit
-    atexit.register(reportDictInfo)
+    #import atexit
+    #atexit.register(reportDictInfo)
 
 class W_DictMultiObject(W_Object):
     from pypy.objspace.std.dicttype import dict_typedef as typedef

Added: pypy/dist/pypy/tool/readdictinfo.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/tool/readdictinfo.py	Wed Aug  9 12:34:22 2006
@@ -0,0 +1,49 @@
+import autopath
+
+# this is for use with a pypy-c build with multidicts and using the
+# MeasuringDictImplementation -- it will create a file called
+# 'dictinfo.txt' in the local directory and this file will turn the
+# contents back into DictInfo objects.
+
+# run with python -i !
+
+from pypy.objspace.std.dictmultiobject import DictInfo
+
+import sys
+
+infile = open(sys.argv[1])
+
+infos = []
+
+for line in infile:
+    if line == '------------------\n':
+        curr = object.__new__(DictInfo)
+        infos.append(curr)
+    else:
+        attr, val = [s.strip() for s in line.split(':')]
+        if '.' in val:
+            val = float(val)
+        else:
+            val = int(val)
+        setattr(curr, attr, val)
+
+def histogram(infos, keyattr, *attrs):
+    r = {}
+    for info in infos:
+        v = getattr(info, keyattr)
+        l = r.setdefault(v, [0, {}])
+        l[0] += 1
+        for a in attrs:
+            d2 = l[1].setdefault(a, {})
+            v2 = getattr(info, a)
+            d2[v2] = d2.get(v2, 0) + 1
+    return sorted(r.items())
+
+import pprint
+try:
+    import readline
+except ImportError:
+    pass
+else:
+    import rlcompleter
+    readline.parse_and_bind('tab: complete')



More information about the Pypy-commit mailing list