Blame SOURCES/00147-add-debug-malloc-stats.patch

c8ca20
diff -up Python-2.7.2/Include/dictobject.h.add-debug-malloc-stats Python-2.7.2/Include/dictobject.h
c8ca20
--- Python-2.7.2/Include/dictobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/dictobject.h	2011-09-16 19:03:25.105821625 -0400
c8ca20
@@ -150,6 +150,8 @@ PyAPI_FUNC(PyObject *) PyDict_GetItemStr
c8ca20
 PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
c8ca20
 PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Include/floatobject.h.add-debug-malloc-stats Python-2.7.2/Include/floatobject.h
c8ca20
--- Python-2.7.2/Include/floatobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/floatobject.h	2011-09-16 19:03:25.106821625 -0400
c8ca20
@@ -132,6 +132,7 @@ PyAPI_FUNC(PyObject *) _PyFloat_FormatAd
c8ca20
    failure.  Used in builtin_round in bltinmodule.c. */
c8ca20
 PyAPI_FUNC(PyObject *) _Py_double_round(double x, int ndigits);
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);
c8ca20
 
c8ca20
 
c8ca20
 #ifdef __cplusplus
c8ca20
diff -up Python-2.7.2/Include/frameobject.h.add-debug-malloc-stats Python-2.7.2/Include/frameobject.h
c8ca20
--- Python-2.7.2/Include/frameobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/frameobject.h	2011-09-16 19:03:25.107821625 -0400
c8ca20
@@ -80,6 +80,8 @@ PyAPI_FUNC(void) PyFrame_FastToLocals(Py
c8ca20
 
c8ca20
 PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);
c8ca20
+
c8ca20
 /* Return the line of code the frame is currently executing. */
c8ca20
 PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
c8ca20
 
c8ca20
diff -up Python-2.7.2/Include/intobject.h.add-debug-malloc-stats Python-2.7.2/Include/intobject.h
c8ca20
--- Python-2.7.2/Include/intobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/intobject.h	2011-09-16 19:03:25.107821625 -0400
c8ca20
@@ -74,6 +74,8 @@ PyAPI_FUNC(PyObject *) _PyInt_FormatAdva
c8ca20
 					     char *format_spec,
c8ca20
 					     Py_ssize_t format_spec_len);
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyInt_DebugMallocStats(FILE *out);
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Include/listobject.h.add-debug-malloc-stats Python-2.7.2/Include/listobject.h
c8ca20
--- Python-2.7.2/Include/listobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/listobject.h	2011-09-16 19:03:25.107821625 -0400
c8ca20
@@ -62,6 +62,8 @@ PyAPI_FUNC(PyObject *) _PyList_Extend(Py
c8ca20
 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
c8ca20
 #define PyList_GET_SIZE(op)    Py_SIZE(op)
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Include/methodobject.h.add-debug-malloc-stats Python-2.7.2/Include/methodobject.h
c8ca20
--- Python-2.7.2/Include/methodobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/methodobject.h	2011-09-16 19:03:25.108821625 -0400
c8ca20
@@ -87,6 +87,10 @@ typedef struct {
c8ca20
 
c8ca20
 PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyCFunction_DebugMallocStats(FILE *out);
c8ca20
+PyAPI_FUNC(void) _PyMethod_DebugMallocStats(FILE *out);
c8ca20
+
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Include/object.h.add-debug-malloc-stats Python-2.7.2/Include/object.h
c8ca20
--- Python-2.7.2/Include/object.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/object.h	2011-09-16 19:03:25.108821625 -0400
c8ca20
@@ -980,6 +980,13 @@ PyAPI_DATA(PyObject *) _PyTrash_delete_l
c8ca20
             _PyTrash_thread_deposit_object((PyObject*)op); \
c8ca20
     } while (0);
c8ca20
 
c8ca20
+PyAPI_FUNC(void)
c8ca20
+_PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
c8ca20
+		       size_t sizeof_block);
c8ca20
+
c8ca20
+PyAPI_FUNC(void)
c8ca20
+_PyObject_DebugTypeStats(FILE *out);
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Include/objimpl.h.add-debug-malloc-stats Python-2.7.2/Include/objimpl.h
c8ca20
--- Python-2.7.2/Include/objimpl.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/objimpl.h	2011-09-16 19:03:25.108821625 -0400
c8ca20
@@ -101,13 +101,13 @@ PyAPI_FUNC(void) PyObject_Free(void *);
c8ca20
 
c8ca20
 /* Macros */
c8ca20
 #ifdef WITH_PYMALLOC
c8ca20
+PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out);
c8ca20
 #ifdef PYMALLOC_DEBUG   /* WITH_PYMALLOC && PYMALLOC_DEBUG */
c8ca20
 PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
c8ca20
 PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
c8ca20
 PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
c8ca20
 PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
c8ca20
 PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
c8ca20
-PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
c8ca20
 PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes);
c8ca20
 PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes);
c8ca20
 PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p);
c8ca20
diff -up Python-2.7.2/Include/stringobject.h.add-debug-malloc-stats Python-2.7.2/Include/stringobject.h
c8ca20
--- Python-2.7.2/Include/stringobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/stringobject.h	2011-09-16 19:03:25.109821625 -0400
c8ca20
@@ -204,6 +204,8 @@ PyAPI_FUNC(PyObject *) _PyBytes_FormatAd
c8ca20
 					       char *format_spec,
c8ca20
 					       Py_ssize_t format_spec_len);
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyString_DebugMallocStats(FILE *out);
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Include/unicodeobject.h.add-debug-malloc-stats Python-2.7.2/Include/unicodeobject.h
c8ca20
--- Python-2.7.2/Include/unicodeobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
c8ca20
+++ Python-2.7.2/Include/unicodeobject.h	2011-09-16 19:03:25.109821625 -0400
c8ca20
@@ -1406,6 +1406,8 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
c8ca20
     Py_UNICODE ch       /* Unicode character */
c8ca20
     );
c8ca20
 
c8ca20
+PyAPI_FUNC(void) _PyUnicode_DebugMallocStats(FILE *out);
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Lib/test/test_sys.py.add-debug-malloc-stats Python-2.7.2/Lib/test/test_sys.py
c8ca20
--- Python-2.7.2/Lib/test/test_sys.py.add-debug-malloc-stats	2011-09-16 19:03:25.048821626 -0400
c8ca20
+++ Python-2.7.2/Lib/test/test_sys.py	2011-09-16 19:03:25.110821625 -0400
c8ca20
@@ -473,6 +473,32 @@ class SysModuleTest(unittest.TestCase):
c8ca20
         p.wait()
c8ca20
         self.assertIn(executable, ["''", repr(sys.executable)])
c8ca20
 
c8ca20
+    def test_debugmallocstats(self):
c8ca20
+        # Test sys._debugmallocstats()
c8ca20
+
c8ca20
+        import subprocess
c8ca20
+
c8ca20
+        # Verify the default of writing to stderr:
c8ca20
+        p = subprocess.Popen([sys.executable,
c8ca20
+                              '-c', 'import sys; sys._debugmallocstats()'],
c8ca20
+                             stderr=subprocess.PIPE)
c8ca20
+        out, err = p.communicate()
c8ca20
+        p.wait()
c8ca20
+        self.assertIn("arenas allocated current", err)
c8ca20
+                                     
c8ca20
+        # Verify that we can redirect the output to a file (not a file-like
c8ca20
+        # object, though):
c8ca20
+        with open('mallocstats.txt', 'w') as out:
c8ca20
+            sys._debugmallocstats(out)
c8ca20
+        result = open('mallocstats.txt').read()
c8ca20
+        self.assertIn("arenas allocated current", result)
c8ca20
+        os.unlink('mallocstats.txt')
c8ca20
+
c8ca20
+        # Verify that the destination must be a file:
c8ca20
+        with self.assertRaises(TypeError):
c8ca20
+            sys._debugmallocstats(42)
c8ca20
+        
c8ca20
+
c8ca20
 class SizeofTest(unittest.TestCase):
c8ca20
 
c8ca20
     def setUp(self):
c8ca20
diff -up Python-2.7.2/Objects/classobject.c.add-debug-malloc-stats Python-2.7.2/Objects/classobject.c
c8ca20
--- Python-2.7.2/Objects/classobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/classobject.c	2011-09-16 19:03:25.110821625 -0400
c8ca20
@@ -2670,3 +2670,12 @@ PyMethod_Fini(void)
c8ca20
 {
c8ca20
     (void)PyMethod_ClearFreeList();
c8ca20
 }
c8ca20
+
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyMethod_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyDebugAllocatorStats(out,
c8ca20
+                           "free PyMethodObject",
c8ca20
+                           numfree, sizeof(PyMethodObject));
c8ca20
+}
c8ca20
diff -up Python-2.7.2/Objects/dictobject.c.add-debug-malloc-stats Python-2.7.2/Objects/dictobject.c
c8ca20
--- Python-2.7.2/Objects/dictobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/dictobject.c	2011-09-16 19:03:25.111821625 -0400
c8ca20
@@ -225,6 +225,15 @@ show_track(void)
c8ca20
 static PyDictObject *free_list[PyDict_MAXFREELIST];
c8ca20
 static int numfree = 0;
c8ca20
 
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyDict_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyDebugAllocatorStats(out,
c8ca20
+                           "free PyDictObject", numfree, sizeof(PyDictObject));
c8ca20
+}
c8ca20
+
c8ca20
+
c8ca20
 void
c8ca20
 PyDict_Fini(void)
c8ca20
 {
c8ca20
diff -up Python-2.7.2/Objects/floatobject.c.add-debug-malloc-stats Python-2.7.2/Objects/floatobject.c
c8ca20
--- Python-2.7.2/Objects/floatobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/floatobject.c	2011-09-16 19:03:25.111821625 -0400
c8ca20
@@ -35,6 +35,22 @@ typedef struct _floatblock PyFloatBlock;
c8ca20
 static PyFloatBlock *block_list = NULL;
c8ca20
 static PyFloatObject *free_list = NULL;
c8ca20
 
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyFloat_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+  int num_blocks = 0;
c8ca20
+  PyFloatBlock *block;
c8ca20
+
c8ca20
+  /* Walk the block list, counting */
c8ca20
+  for (block = block_list; block ; block = block->next) {
c8ca20
+      num_blocks++;
c8ca20
+  }
c8ca20
+
c8ca20
+  _PyDebugAllocatorStats(out,
c8ca20
+                         "PyFloatBlock", num_blocks, sizeof(PyFloatBlock));
c8ca20
+}
c8ca20
+
c8ca20
 static PyFloatObject *
c8ca20
 fill_free_list(void)
c8ca20
 {
c8ca20
diff -up Python-2.7.2/Objects/frameobject.c.add-debug-malloc-stats Python-2.7.2/Objects/frameobject.c
c8ca20
--- Python-2.7.2/Objects/frameobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/frameobject.c	2011-09-16 19:03:25.112821625 -0400
c8ca20
@@ -980,3 +980,13 @@ PyFrame_Fini(void)
c8ca20
     Py_XDECREF(builtin_object);
c8ca20
     builtin_object = NULL;
c8ca20
 }
c8ca20
+
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyFrame_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyDebugAllocatorStats(out,
c8ca20
+                           "free PyFrameObject",
c8ca20
+                           numfree, sizeof(PyFrameObject));
c8ca20
+}
c8ca20
+
c8ca20
diff -up Python-2.7.2/Objects/intobject.c.add-debug-malloc-stats Python-2.7.2/Objects/intobject.c
c8ca20
--- Python-2.7.2/Objects/intobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/intobject.c	2011-09-16 19:03:25.112821625 -0400
c8ca20
@@ -44,6 +44,23 @@ typedef struct _intblock PyIntBlock;
c8ca20
 static PyIntBlock *block_list = NULL;
c8ca20
 static PyIntObject *free_list = NULL;
c8ca20
 
c8ca20
+
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyInt_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    int num_blocks = 0;
c8ca20
+    PyIntBlock *block;
c8ca20
+
c8ca20
+    /* Walk the block list, counting */
c8ca20
+    for (block = block_list; block ; block = block->next) {
c8ca20
+        num_blocks++;
c8ca20
+    }
c8ca20
+
c8ca20
+    _PyDebugAllocatorStats(out,
c8ca20
+                           "PyIntBlock", num_blocks, sizeof(PyIntBlock));
c8ca20
+}
c8ca20
+
c8ca20
 static PyIntObject *
c8ca20
 fill_free_list(void)
c8ca20
 {
c8ca20
diff -up Python-2.7.2/Objects/listobject.c.add-debug-malloc-stats Python-2.7.2/Objects/listobject.c
c8ca20
--- Python-2.7.2/Objects/listobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/listobject.c	2011-09-16 19:03:25.113821625 -0400
c8ca20
@@ -109,6 +109,15 @@ PyList_Fini(void)
c8ca20
     }
c8ca20
 }
c8ca20
 
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyList_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyDebugAllocatorStats(out,
c8ca20
+                           "free PyListObject",
c8ca20
+                           numfree, sizeof(PyListObject));
c8ca20
+}
c8ca20
+
c8ca20
 PyObject *
c8ca20
 PyList_New(Py_ssize_t size)
c8ca20
 {
c8ca20
diff -up Python-2.7.2/Objects/methodobject.c.add-debug-malloc-stats Python-2.7.2/Objects/methodobject.c
c8ca20
--- Python-2.7.2/Objects/methodobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/methodobject.c	2011-09-16 19:03:25.113821625 -0400
c8ca20
@@ -412,6 +412,15 @@ PyCFunction_Fini(void)
c8ca20
     (void)PyCFunction_ClearFreeList();
c8ca20
 }
c8ca20
 
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyCFunction_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyDebugAllocatorStats(out,
c8ca20
+                           "free PyCFunction",
c8ca20
+                           numfree, sizeof(PyCFunction));
c8ca20
+}
c8ca20
+
c8ca20
 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
c8ca20
    but it's part of the API so we need to keep a function around that
c8ca20
    existing C extensions can call.
c8ca20
diff -up Python-2.7.2/Objects/object.c.add-debug-malloc-stats Python-2.7.2/Objects/object.c
c8ca20
--- Python-2.7.2/Objects/object.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/object.c	2011-09-16 19:04:46.463820849 -0400
c8ca20
@@ -2334,6 +2334,23 @@ PyMem_Free(void *p)
c8ca20
     PyMem_FREE(p);
c8ca20
 }
c8ca20
 
c8ca20
+void
c8ca20
+_PyObject_DebugTypeStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyString_DebugMallocStats(out);
c8ca20
+    _PyCFunction_DebugMallocStats(out);
c8ca20
+    _PyDict_DebugMallocStats(out);
c8ca20
+    _PyFloat_DebugMallocStats(out);
c8ca20
+    _PyFrame_DebugMallocStats(out);
c8ca20
+    _PyInt_DebugMallocStats(out);
c8ca20
+    _PyList_DebugMallocStats(out);
c8ca20
+    _PyMethod_DebugMallocStats(out);
c8ca20
+    _PySet_DebugMallocStats(out);
c8ca20
+    _PyTuple_DebugMallocStats(out);
c8ca20
+#if Py_USING_UNICODE
c8ca20
+    _PyUnicode_DebugMallocStats(out);
c8ca20
+#endif
c8ca20
+}
c8ca20
 
c8ca20
 /* These methods are used to control infinite recursion in repr, str, print,
c8ca20
    etc.  Container objects that may recursively contain themselves,
c8ca20
diff -up Python-2.7.2/Objects/obmalloc.c.add-debug-malloc-stats Python-2.7.2/Objects/obmalloc.c
c8ca20
--- Python-2.7.2/Objects/obmalloc.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/obmalloc.c	2011-09-16 19:03:25.114821625 -0400
c8ca20
@@ -508,12 +508,10 @@ static struct arena_object* usable_arena
c8ca20
 /* Number of arenas allocated that haven't been free()'d. */
c8ca20
 static size_t narenas_currently_allocated = 0;
c8ca20
 
c8ca20
-#ifdef PYMALLOC_DEBUG
c8ca20
 /* Total number of times malloc() called to allocate an arena. */
c8ca20
 static size_t ntimes_arena_allocated = 0;
c8ca20
 /* High water mark (max value ever seen) for narenas_currently_allocated. */
c8ca20
 static size_t narenas_highwater = 0;
c8ca20
-#endif
c8ca20
 
c8ca20
 /* Allocate a new arena.  If we run out of memory, return NULL.  Else
c8ca20
  * allocate a new arena, and return the address of an arena_object
c8ca20
@@ -528,7 +526,7 @@ new_arena(void)
c8ca20
 
c8ca20
 #ifdef PYMALLOC_DEBUG
c8ca20
     if (Py_GETENV("PYTHONMALLOCSTATS"))
c8ca20
-        _PyObject_DebugMallocStats();
c8ca20
+        _PyObject_DebugMallocStats(stderr);
c8ca20
 #endif
c8ca20
     if (unused_arena_objects == NULL) {
c8ca20
         uint i;
c8ca20
@@ -588,11 +586,9 @@ new_arena(void)
c8ca20
     }
c8ca20
 
c8ca20
     ++narenas_currently_allocated;
c8ca20
-#ifdef PYMALLOC_DEBUG
c8ca20
     ++ntimes_arena_allocated;
c8ca20
     if (narenas_currently_allocated > narenas_highwater)
c8ca20
         narenas_highwater = narenas_currently_allocated;
c8ca20
-#endif
c8ca20
     arenaobj->freepools = NULL;
c8ca20
     /* pool_address <- first pool-aligned address in the arena
c8ca20
        nfreepools <- number of whole pools that fit after alignment */
c8ca20
@@ -1694,17 +1690,19 @@ _PyObject_DebugDumpAddress(const void *p
c8ca20
     }
c8ca20
 }
c8ca20
 
c8ca20
+#endif  /* PYMALLOC_DEBUG */
c8ca20
+
c8ca20
 static size_t
c8ca20
-printone(const char* msg, size_t value)
c8ca20
+printone(FILE *out, const char* msg, size_t value)
c8ca20
 {
c8ca20
     int i, k;
c8ca20
     char buf[100];
c8ca20
     size_t origvalue = value;
c8ca20
 
c8ca20
-    fputs(msg, stderr);
c8ca20
+    fputs(msg, out);
c8ca20
     for (i = (int)strlen(msg); i < 35; ++i)
c8ca20
-        fputc(' ', stderr);
c8ca20
-    fputc('=', stderr);
c8ca20
+        fputc(' ', out);
c8ca20
+    fputc('=', out);
c8ca20
 
c8ca20
     /* Write the value with commas. */
c8ca20
     i = 22;
c8ca20
@@ -1725,17 +1723,32 @@ printone(const char* msg, size_t value)
c8ca20
 
c8ca20
     while (i >= 0)
c8ca20
         buf[i--] = ' ';
c8ca20
-    fputs(buf, stderr);
c8ca20
+    fputs(buf, out);
c8ca20
 
c8ca20
     return origvalue;
c8ca20
 }
c8ca20
 
c8ca20
-/* Print summary info to stderr about the state of pymalloc's structures.
c8ca20
+void
c8ca20
+_PyDebugAllocatorStats(FILE *out,
c8ca20
+                       const char *block_name, int num_blocks, size_t sizeof_block)
c8ca20
+{
c8ca20
+       char buf1[128];
c8ca20
+       char buf2[128];
c8ca20
+       PyOS_snprintf(buf1, sizeof(buf1),
c8ca20
+                     "%d %ss * %zd bytes each",
c8ca20
+                     num_blocks, block_name, sizeof_block);
c8ca20
+       PyOS_snprintf(buf2, sizeof(buf2),
c8ca20
+                     "%48s ", buf1);
c8ca20
+      (void)printone(out, buf2, num_blocks * sizeof_block);
c8ca20
+}
c8ca20
+
c8ca20
+
c8ca20
+/* Print summary info to "out" about the state of pymalloc's structures.
c8ca20
  * In Py_DEBUG mode, also perform some expensive internal consistency
c8ca20
  * checks.
c8ca20
  */
c8ca20
 void
c8ca20
-_PyObject_DebugMallocStats(void)
c8ca20
+_PyObject_DebugMallocStats(FILE *out)
c8ca20
 {
c8ca20
     uint i;
c8ca20
     const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
c8ca20
@@ -1764,7 +1777,7 @@ _PyObject_DebugMallocStats(void)
c8ca20
     size_t total;
c8ca20
     char buf[128];
c8ca20
 
c8ca20
-    fprintf(stderr, "Small block threshold = %d, in %u size classes.\n",
c8ca20
+    fprintf(out, "Small block threshold = %d, in %u size classes.\n",
c8ca20
             SMALL_REQUEST_THRESHOLD, numclasses);
c8ca20
 
c8ca20
     for (i = 0; i < numclasses; ++i)
c8ca20
@@ -1818,10 +1831,10 @@ _PyObject_DebugMallocStats(void)
c8ca20
     }
c8ca20
     assert(narenas == narenas_currently_allocated);
c8ca20
 
c8ca20
-    fputc('\n', stderr);
c8ca20
+    fputc('\n', out);
c8ca20
     fputs("class   size   num pools   blocks in use  avail blocks\n"
c8ca20
           "-----   ----   ---------   -------------  ------------\n",
c8ca20
-          stderr);
c8ca20
+          out);
c8ca20
 
c8ca20
     for (i = 0; i < numclasses; ++i) {
c8ca20
         size_t p = numpools[i];
c8ca20
@@ -1832,7 +1845,7 @@ _PyObject_DebugMallocStats(void)
c8ca20
             assert(b == 0 && f == 0);
c8ca20
             continue;
c8ca20
         }
c8ca20
-        fprintf(stderr, "%5u %6u "
c8ca20
+        fprintf(out, "%5u %6u "
c8ca20
                         "%11" PY_FORMAT_SIZE_T "u "
c8ca20
                         "%15" PY_FORMAT_SIZE_T "u "
c8ca20
                         "%13" PY_FORMAT_SIZE_T "u\n",
c8ca20
@@ -1842,36 +1855,35 @@ _PyObject_DebugMallocStats(void)
c8ca20
         pool_header_bytes += p * POOL_OVERHEAD;
c8ca20
         quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
c8ca20
     }
c8ca20
-    fputc('\n', stderr);
c8ca20
-    (void)printone("# times object malloc called", serialno);
c8ca20
-
c8ca20
-    (void)printone("# arenas allocated total", ntimes_arena_allocated);
c8ca20
-    (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas);
c8ca20
-    (void)printone("# arenas highwater mark", narenas_highwater);
c8ca20
-    (void)printone("# arenas allocated current", narenas);
c8ca20
+    fputc('\n', out);
c8ca20
+#ifdef PYMALLOC_DEBUG
c8ca20
+    (void)printone(out, "# times object malloc called", serialno);
c8ca20
+#endif
c8ca20
+    (void)printone(out, "# arenas allocated total", ntimes_arena_allocated);
c8ca20
+    (void)printone(out, "# arenas reclaimed", ntimes_arena_allocated - narenas);
c8ca20
+    (void)printone(out, "# arenas highwater mark", narenas_highwater);
c8ca20
+    (void)printone(out, "# arenas allocated current", narenas);
c8ca20
 
c8ca20
     PyOS_snprintf(buf, sizeof(buf),
c8ca20
         "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena",
c8ca20
         narenas, ARENA_SIZE);
c8ca20
-    (void)printone(buf, narenas * ARENA_SIZE);
c8ca20
+    (void)printone(out, buf, narenas * ARENA_SIZE);
c8ca20
 
c8ca20
-    fputc('\n', stderr);
c8ca20
+    fputc('\n', out);
c8ca20
 
c8ca20
-    total = printone("# bytes in allocated blocks", allocated_bytes);
c8ca20
-    total += printone("# bytes in available blocks", available_bytes);
c8ca20
+    total = printone(out, "# bytes in allocated blocks", allocated_bytes);
c8ca20
+    total += printone(out, "# bytes in available blocks", available_bytes);
c8ca20
 
c8ca20
     PyOS_snprintf(buf, sizeof(buf),
c8ca20
         "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
c8ca20
-    total += printone(buf, (size_t)numfreepools * POOL_SIZE);
c8ca20
+    total += printone(out, buf, (size_t)numfreepools * POOL_SIZE);
c8ca20
 
c8ca20
-    total += printone("# bytes lost to pool headers", pool_header_bytes);
c8ca20
-    total += printone("# bytes lost to quantization", quantization);
c8ca20
-    total += printone("# bytes lost to arena alignment", arena_alignment);
c8ca20
-    (void)printone("Total", total);
c8ca20
+    total += printone(out, "# bytes lost to pool headers", pool_header_bytes);
c8ca20
+    total += printone(out, "# bytes lost to quantization", quantization);
c8ca20
+    total += printone(out, "# bytes lost to arena alignment", arena_alignment);
c8ca20
+    (void)printone(out, "Total", total);
c8ca20
 }
c8ca20
 
c8ca20
-#endif  /* PYMALLOC_DEBUG */
c8ca20
-
c8ca20
 #ifdef Py_USING_MEMORY_DEBUGGER
c8ca20
 /* Make this function last so gcc won't inline it since the definition is
c8ca20
  * after the reference.
c8ca20
diff -up Python-2.7.2/Objects/setobject.c.add-debug-malloc-stats Python-2.7.2/Objects/setobject.c
c8ca20
--- Python-2.7.2/Objects/setobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/setobject.c	2011-09-16 19:03:25.115821625 -0400
c8ca20
@@ -1088,6 +1088,16 @@ PySet_Fini(void)
c8ca20
     Py_CLEAR(emptyfrozenset);
c8ca20
 }
c8ca20
 
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PySet_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyDebugAllocatorStats(out,
c8ca20
+                           "free PySetObject",
c8ca20
+                           numfree, sizeof(PySetObject));
c8ca20
+}
c8ca20
+
c8ca20
+
c8ca20
 static PyObject *
c8ca20
 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
c8ca20
 {
c8ca20
diff -up Python-2.7.2/Objects/stringobject.c.add-debug-malloc-stats Python-2.7.2/Objects/stringobject.c
c8ca20
--- Python-2.7.2/Objects/stringobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/stringobject.c	2011-09-16 19:03:25.116821625 -0400
c8ca20
@@ -4822,3 +4822,43 @@ void _Py_ReleaseInternedStrings(void)
c8ca20
     PyDict_Clear(interned);
c8ca20
     Py_CLEAR(interned);
c8ca20
 }
c8ca20
+
c8ca20
+void _PyString_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    ssize_t i;
c8ca20
+    int num_immortal = 0, num_mortal = 0;
c8ca20
+    ssize_t immortal_size = 0, mortal_size = 0;
c8ca20
+
c8ca20
+    if (interned == NULL || !PyDict_Check(interned))
c8ca20
+        return;
c8ca20
+
c8ca20
+    for (i = 0; i <= ((PyDictObject*)interned)->ma_mask; i++) {
c8ca20
+        PyDictEntry *ep = ((PyDictObject*)interned)->ma_table + i;
c8ca20
+        PyObject *pvalue = ep->me_value;
c8ca20
+        if (pvalue != NULL) {
c8ca20
+            PyStringObject *s = (PyStringObject *)ep->me_key;
c8ca20
+
c8ca20
+            switch (s->ob_sstate) {
c8ca20
+            case SSTATE_NOT_INTERNED:
c8ca20
+                /* XXX Shouldn't happen */
c8ca20
+                break;
c8ca20
+            case SSTATE_INTERNED_IMMORTAL:
c8ca20
+                num_immortal ++;
c8ca20
+                immortal_size += s->ob_size;
c8ca20
+                break;
c8ca20
+            case SSTATE_INTERNED_MORTAL:
c8ca20
+                num_mortal ++;
c8ca20
+                mortal_size += s->ob_size;
c8ca20
+                break;
c8ca20
+            default:
c8ca20
+                Py_FatalError("Inconsistent interned string state.");
c8ca20
+            }
c8ca20
+        }
c8ca20
+    }
c8ca20
+
c8ca20
+    fprintf(out, "%d mortal interned strings\n", num_mortal);
c8ca20
+    fprintf(out, "%d immortal interned strings\n", num_immortal);
c8ca20
+    fprintf(out, "total size of all interned strings: "
c8ca20
+            "%zi/%zi "
c8ca20
+            "mortal/immortal\n", mortal_size, immortal_size);
c8ca20
+}
c8ca20
diff -up Python-2.7.2/Objects/tupleobject.c.add-debug-malloc-stats Python-2.7.2/Objects/tupleobject.c
c8ca20
--- Python-2.7.2/Objects/tupleobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/tupleobject.c	2011-09-16 19:03:25.116821625 -0400
c8ca20
@@ -44,6 +44,22 @@ show_track(void)
c8ca20
 }
c8ca20
 #endif
c8ca20
 
c8ca20
+/* Print summary info about the state of the optimized allocator */
c8ca20
+void
c8ca20
+_PyTuple_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+#if PyTuple_MAXSAVESIZE > 0
c8ca20
+    int i;
c8ca20
+    char buf[128];
c8ca20
+    for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
c8ca20
+        PyOS_snprintf(buf, sizeof(buf),
c8ca20
+                      "free %d-sized PyTupleObject", i);
c8ca20
+        _PyDebugAllocatorStats(out,
c8ca20
+                               buf,
c8ca20
+                               numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
c8ca20
+    }
c8ca20
+#endif
c8ca20
+}
c8ca20
 
c8ca20
 PyObject *
c8ca20
 PyTuple_New(register Py_ssize_t size)
c8ca20
diff -up Python-2.7.2/Objects/unicodeobject.c.add-debug-malloc-stats Python-2.7.2/Objects/unicodeobject.c
c8ca20
--- Python-2.7.2/Objects/unicodeobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
c8ca20
+++ Python-2.7.2/Objects/unicodeobject.c	2011-09-16 19:03:25.118821625 -0400
c8ca20
@@ -8883,6 +8883,12 @@ _PyUnicode_Fini(void)
c8ca20
     (void)PyUnicode_ClearFreeList();
c8ca20
 }
c8ca20
 
c8ca20
+void _PyUnicode_DebugMallocStats(FILE *out)
c8ca20
+{
c8ca20
+    _PyDebugAllocatorStats(out, "free PyUnicodeObject", numfree,
c8ca20
+                           sizeof(PyUnicodeObject));
c8ca20
+}
c8ca20
+
c8ca20
 #ifdef __cplusplus
c8ca20
 }
c8ca20
 #endif
c8ca20
diff -up Python-2.7.2/Python/pythonrun.c.add-debug-malloc-stats Python-2.7.2/Python/pythonrun.c
c8ca20
--- Python-2.7.2/Python/pythonrun.c.add-debug-malloc-stats	2011-09-16 19:03:25.025821626 -0400
c8ca20
+++ Python-2.7.2/Python/pythonrun.c	2011-09-16 19:03:25.118821625 -0400
c8ca20
@@ -549,7 +549,7 @@ Py_Finalize(void)
c8ca20
 #endif /* Py_TRACE_REFS */
c8ca20
 #ifdef PYMALLOC_DEBUG
c8ca20
     if (Py_GETENV("PYTHONMALLOCSTATS"))
c8ca20
-        _PyObject_DebugMallocStats();
c8ca20
+        _PyObject_DebugMallocStats(stderr);
c8ca20
 #endif
c8ca20
 
c8ca20
     call_ll_exitfuncs();
c8ca20
diff -up Python-2.7.2/Python/sysmodule.c.add-debug-malloc-stats Python-2.7.2/Python/sysmodule.c
c8ca20
--- Python-2.7.2/Python/sysmodule.c.add-debug-malloc-stats	2011-09-16 19:03:25.007821626 -0400
c8ca20
+++ Python-2.7.2/Python/sysmodule.c	2011-09-16 19:03:25.119821625 -0400
c8ca20
@@ -872,6 +872,57 @@ a 11-tuple where the entries in the tupl
c8ca20
 extern "C" {
c8ca20
 #endif
c8ca20
 
c8ca20
+static PyObject *
c8ca20
+sys_debugmallocstats(PyObject *self, PyObject *args)
c8ca20
+{
c8ca20
+    PyObject *file = NULL;
c8ca20
+    FILE *fp;
c8ca20
+
c8ca20
+    if (!PyArg_ParseTuple(args, "|O!",
c8ca20
+                          &PyFile_Type, &file)) {
c8ca20
+      return NULL;
c8ca20
+    }
c8ca20
+    if (!file) {
c8ca20
+        /* Default to sys.stderr: */
c8ca20
+      file = PySys_GetObject("stderr");
c8ca20
+      if (!file) {
c8ca20
+          PyErr_SetString(PyExc_ValueError, "sys.stderr not set");
c8ca20
+          return NULL;
c8ca20
+      }
c8ca20
+      if (!PyFile_Check(file)) {
c8ca20
+          PyErr_SetString(PyExc_TypeError, "sys.stderr is not a file");
c8ca20
+          return NULL;
c8ca20
+      }
c8ca20
+    }
c8ca20
+
c8ca20
+    Py_INCREF(file);
c8ca20
+    /* OK, we now own a ref on non-NULL "file" */
c8ca20
+
c8ca20
+    fp = PyFile_AsFile(file);
c8ca20
+    if (!fp) {
c8ca20
+        PyErr_SetString(PyExc_ValueError, "file is closed");
c8ca20
+        Py_DECREF(file);
c8ca20
+        return NULL;	
c8ca20
+    }
c8ca20
+
c8ca20
+    _PyObject_DebugMallocStats(fp);
c8ca20
+    fputc('\n', fp);
c8ca20
+    _PyObject_DebugTypeStats(fp);
c8ca20
+
c8ca20
+    Py_DECREF(file);
c8ca20
+
c8ca20
+    Py_RETURN_NONE;
c8ca20
+}
c8ca20
+PyDoc_STRVAR(debugmallocstats_doc,
c8ca20
+"_debugmallocstats([file])\n\
c8ca20
+\n\
c8ca20
+Print summary info to the given file (or sys.stderr) about the state of\n\
c8ca20
+pymalloc's structures.\n\
c8ca20
+\n\
c8ca20
+In Py_DEBUG mode, also perform some expensive internal consistency\n\
c8ca20
+checks.\n\
c8ca20
+");
c8ca20
+
c8ca20
 #ifdef Py_TRACE_REFS
c8ca20
 /* Defined in objects.c because it uses static globals if that file */
c8ca20
 extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
c8ca20
@@ -970,6 +1021,8 @@ static PyMethodDef sys_methods[] = {
c8ca20
     {"settrace",        sys_settrace, METH_O, settrace_doc},
c8ca20
     {"gettrace",        sys_gettrace, METH_NOARGS, gettrace_doc},
c8ca20
     {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
c8ca20
+    {"_debugmallocstats", sys_debugmallocstats, METH_VARARGS,
c8ca20
+     debugmallocstats_doc},
c8ca20
     {NULL,              NULL}           /* sentinel */
c8ca20
 };
c8ca20