[pypy-svn] r58666 - in pypy/dist/pypy/translator/tool: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Oct 6 17:07:21 CEST 2008


Author: cfbolz
Date: Mon Oct  6 17:07:20 2008
New Revision: 58666

Added:
   pypy/dist/pypy/translator/tool/staticsizereport.py   (contents, props changed)
   pypy/dist/pypy/translator/tool/test/test_staticsizereport.py   (contents, props changed)
Log:
(xoraxax, cfbolz): Some helper functions to print statistics about all static
data in a database.


Added: pypy/dist/pypy/translator/tool/staticsizereport.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/tool/staticsizereport.py	Mon Oct  6 17:07:20 2008
@@ -0,0 +1,37 @@
+from __future__ import division
+
+from pypy.rpython.lltypesystem.lltype import typeOf, _ptr, Ptr
+from pypy.rpython.lltypesystem import llmemory
+from pypy.rpython.memory.lltypelayout import convert_offset_to_int
+
+def guess_size(obj):
+    TYPE = typeOf(obj)
+    ptr = _ptr(Ptr(TYPE), obj)
+    if TYPE._is_varsize():
+        arrayfld = getattr(TYPE, '_arrayfld', None)
+        if arrayfld:
+            length = len(getattr(ptr, arrayfld))
+        else:
+            length = len(ptr)
+        return convert_offset_to_int(llmemory.sizeof(TYPE, length))
+    return convert_offset_to_int(llmemory.sizeof(TYPE))
+
+
+def group_static_size_by_lltype(database):
+    totalsize = {}
+    numobjects = {}
+    for node in database.globalcontainers():
+        obj = node.obj
+        group = typeOf(obj)
+        totalsize[group] = totalsize.get(group, 0) + guess_size(obj)
+        numobjects[group] = numobjects.get(group, 0) + 1
+    return totalsize, numobjects
+
+def print_static_size_by_lltype(database):
+    totalsize, numobjects = group_static_size_by_lltype(database)
+    l = [(size, key) for key, size in totalsize.iteritems()]
+    l.sort()
+    l.reverse()
+    for size, key in l:
+        print key, size, numobjects[key], size / numobjects[key]
+

Added: pypy/dist/pypy/translator/tool/test/test_staticsizereport.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/tool/test/test_staticsizereport.py	Mon Oct  6 17:07:20 2008
@@ -0,0 +1,23 @@
+from pypy.translator.c.test.test_typed import CompilationTestCase
+from pypy.translator.tool.staticsizereport import group_static_size_by_lltype
+
+class TestStaticSizeReport(CompilationTestCase):
+    def test_simple(self):
+        class A:
+            def __init__(self, n):
+                if n:
+                    self.next = A(n - 1)
+                else:
+                    self.next = None
+                self.key = repr(self)
+        a = A(100)
+        def f(x):
+            if x:
+                return a.key
+            return a.next.key
+        func = self.getcompiled(f, [int])
+        size, num = group_static_size_by_lltype(self.builder.db)
+        for key, value in num.iteritems():
+            if "staticsizereport.A" in str(key) and "vtable" not in str(key):
+                assert value == 101
+



More information about the Pypy-commit mailing list