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

ae2451
diff --git a/Include/dictobject.h b/Include/dictobject.h
ae2451
index ece01c6..acc1df0 100644
ae2451
--- a/Include/dictobject.h
ae2451
+++ b/Include/dictobject.h
ae2451
@@ -150,6 +150,8 @@ PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
ae2451
 PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
ae2451
 PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/floatobject.h b/Include/floatobject.h
ae2451
index 54e8825..33c6ac0 100644
ae2451
--- a/Include/floatobject.h
ae2451
+++ b/Include/floatobject.h
ae2451
@@ -132,6 +132,7 @@ PyAPI_FUNC(PyObject *) _PyFloat_FormatAdvanced(PyObject *obj,
ae2451
    failure.  Used in builtin_round in bltinmodule.c. */
ae2451
 PyAPI_FUNC(PyObject *) _Py_double_round(double x, int ndigits);
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);
ae2451
 
ae2451
 
ae2451
 #ifdef __cplusplus
ae2451
diff --git a/Include/frameobject.h b/Include/frameobject.h
ae2451
index 17e7679..66d9d8b 100644
ae2451
--- a/Include/frameobject.h
ae2451
+++ b/Include/frameobject.h
ae2451
@@ -80,6 +80,8 @@ PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
ae2451
 
ae2451
 PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);
ae2451
+
ae2451
 /* Return the line of code the frame is currently executing. */
ae2451
 PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
ae2451
 
ae2451
diff --git a/Include/intobject.h b/Include/intobject.h
ae2451
index 252eea9..4003736 100644
ae2451
--- a/Include/intobject.h
ae2451
+++ b/Include/intobject.h
ae2451
@@ -75,6 +75,8 @@ PyAPI_FUNC(PyObject *) _PyInt_FormatAdvanced(PyObject *obj,
ae2451
 					     char *format_spec,
ae2451
 					     Py_ssize_t format_spec_len);
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyInt_DebugMallocStats(FILE *out);
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/listobject.h b/Include/listobject.h
ae2451
index c445873..04664d7 100644
ae2451
--- a/Include/listobject.h
ae2451
+++ b/Include/listobject.h
ae2451
@@ -62,6 +62,8 @@ PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
ae2451
 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
ae2451
 #define PyList_GET_SIZE(op)    Py_SIZE(op)
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/methodobject.h b/Include/methodobject.h
ae2451
index 6e160b6..1944517 100644
ae2451
--- a/Include/methodobject.h
ae2451
+++ b/Include/methodobject.h
ae2451
@@ -87,6 +87,10 @@ typedef struct {
ae2451
 
ae2451
 PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyCFunction_DebugMallocStats(FILE *out);
ae2451
+PyAPI_FUNC(void) _PyMethod_DebugMallocStats(FILE *out);
ae2451
+
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/object.h b/Include/object.h
ae2451
index afbc68d..ce5febf 100644
ae2451
--- a/Include/object.h
ae2451
+++ b/Include/object.h
ae2451
@@ -1005,6 +1005,13 @@ PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
ae2451
             _PyTrash_thread_deposit_object((PyObject*)op); \
ae2451
     } while (0);
ae2451
 
ae2451
+PyAPI_FUNC(void)
ae2451
+_PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
ae2451
+		       size_t sizeof_block);
ae2451
+
ae2451
+PyAPI_FUNC(void)
ae2451
+_PyObject_DebugTypeStats(FILE *out);
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/objimpl.h b/Include/objimpl.h
ae2451
index 55e83ec..331b456 100644
ae2451
--- a/Include/objimpl.h
ae2451
+++ b/Include/objimpl.h
ae2451
@@ -101,13 +101,13 @@ PyAPI_FUNC(void) PyObject_Free(void *);
ae2451
 
ae2451
 /* Macros */
ae2451
 #ifdef WITH_PYMALLOC
ae2451
+PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out);
ae2451
 #ifdef PYMALLOC_DEBUG   /* WITH_PYMALLOC && PYMALLOC_DEBUG */
ae2451
 PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
ae2451
 PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
ae2451
 PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
ae2451
 PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
ae2451
 PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
ae2451
-PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
ae2451
 PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes);
ae2451
 PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes);
ae2451
 PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p);
ae2451
diff --git a/Include/setobject.h b/Include/setobject.h
ae2451
index 52b07d5..143b175 100644
ae2451
--- a/Include/setobject.h
ae2451
+++ b/Include/setobject.h
ae2451
@@ -93,6 +93,7 @@ PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key,
ae2451
 PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
ae2451
 PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
ae2451
 
ae2451
+PyAPI_FUNC(void) _PySet_DebugMallocStats(FILE *out);
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/stringobject.h b/Include/stringobject.h
ae2451
index 18b5b41..de78d76 100644
ae2451
--- a/Include/stringobject.h
ae2451
+++ b/Include/stringobject.h
ae2451
@@ -204,6 +204,8 @@ PyAPI_FUNC(PyObject *) _PyBytes_FormatAdvanced(PyObject *obj,
ae2451
 					       char *format_spec,
ae2451
 					       Py_ssize_t format_spec_len);
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyString_DebugMallocStats(FILE *out);
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/tupleobject.h b/Include/tupleobject.h
ae2451
index a5ab733..e233f47 100644
ae2451
--- a/Include/tupleobject.h
ae2451
+++ b/Include/tupleobject.h
ae2451
@@ -54,7 +54,7 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
ae2451
 #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = v)
ae2451
 
ae2451
 PyAPI_FUNC(int) PyTuple_ClearFreeList(void);
ae2451
-
ae2451
+PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
ae2451
index 9ab724a..b91250a 100644
ae2451
--- a/Include/unicodeobject.h
ae2451
+++ b/Include/unicodeobject.h
ae2451
@@ -1406,6 +1406,8 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
ae2451
     Py_UNICODE ch       /* Unicode character */
ae2451
     );
ae2451
 
ae2451
+PyAPI_FUNC(void) _PyUnicode_DebugMallocStats(FILE *out);
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
ae2451
index 82243f3..8f1e1a0 100644
ae2451
--- a/Lib/test/test_sys.py
ae2451
+++ b/Lib/test/test_sys.py
ae2451
@@ -488,6 +488,32 @@ class SysModuleTest(unittest.TestCase):
ae2451
         p.wait()
ae2451
         self.assertIn(executable, ["''", repr(sys.executable)])
ae2451
 
ae2451
+    def test_debugmallocstats(self):
ae2451
+        # Test sys._debugmallocstats()
ae2451
+
ae2451
+        import subprocess
ae2451
+
ae2451
+        # Verify the default of writing to stderr:
ae2451
+        p = subprocess.Popen([sys.executable,
ae2451
+                              '-c', 'import sys; sys._debugmallocstats()'],
ae2451
+                             stderr=subprocess.PIPE)
ae2451
+        out, err = p.communicate()
ae2451
+        p.wait()
ae2451
+        self.assertIn("arenas allocated current", err)
ae2451
+
ae2451
+        # Verify that we can redirect the output to a file (not a file-like
ae2451
+        # object, though):
ae2451
+        with open('mallocstats.txt', 'w') as out:
ae2451
+            sys._debugmallocstats(out)
ae2451
+        result = open('mallocstats.txt').read()
ae2451
+        self.assertIn("arenas allocated current", result)
ae2451
+        os.unlink('mallocstats.txt')
ae2451
+
ae2451
+        # Verify that the destination must be a file:
ae2451
+        with self.assertRaises(TypeError):
ae2451
+            sys._debugmallocstats(42)
ae2451
+
ae2451
+
ae2451
 class SizeofTest(unittest.TestCase):
ae2451
 
ae2451
     def setUp(self):
ae2451
diff --git a/Objects/classobject.c b/Objects/classobject.c
ae2451
index 2c9c216..2ba7077 100644
ae2451
--- a/Objects/classobject.c
ae2451
+++ b/Objects/classobject.c
ae2451
@@ -2694,3 +2694,12 @@ PyMethod_Fini(void)
ae2451
 {
ae2451
     (void)PyMethod_ClearFreeList();
ae2451
 }
ae2451
+
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyMethod_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    _PyDebugAllocatorStats(out,
ae2451
+                           "free PyMethodObject",
ae2451
+                           numfree, sizeof(PyMethodObject));
ae2451
+}
ae2451
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
ae2451
index ba36b18..b8a5c7f 100644
ae2451
--- a/Objects/dictobject.c
ae2451
+++ b/Objects/dictobject.c
ae2451
@@ -225,6 +225,15 @@ show_track(void)
ae2451
 static PyDictObject *free_list[PyDict_MAXFREELIST];
ae2451
 static int numfree = 0;
ae2451
 
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyDict_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    _PyDebugAllocatorStats(out,
ae2451
+                           "free PyDictObject", numfree, sizeof(PyDictObject));
ae2451
+}
ae2451
+
ae2451
+
ae2451
 void
ae2451
 PyDict_Fini(void)
ae2451
 {
ae2451
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
ae2451
index ba867ef..533511d 100644
ae2451
--- a/Objects/floatobject.c
ae2451
+++ b/Objects/floatobject.c
ae2451
@@ -35,6 +35,22 @@ typedef struct _floatblock PyFloatBlock;
ae2451
 static PyFloatBlock *block_list = NULL;
ae2451
 static PyFloatObject *free_list = NULL;
ae2451
 
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyFloat_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+  int num_blocks = 0;
ae2451
+  PyFloatBlock *block;
ae2451
+
ae2451
+  /* Walk the block list, counting */
ae2451
+  for (block = block_list; block ; block = block->next) {
ae2451
+      num_blocks++;
ae2451
+  }
ae2451
+
ae2451
+  _PyDebugAllocatorStats(out,
ae2451
+                         "PyFloatBlock", num_blocks, sizeof(PyFloatBlock));
ae2451
+}
ae2451
+
ae2451
 static PyFloatObject *
ae2451
 fill_free_list(void)
ae2451
 {
ae2451
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
ae2451
index f9e4a0e..337fc58 100644
ae2451
--- a/Objects/frameobject.c
ae2451
+++ b/Objects/frameobject.c
ae2451
@@ -982,3 +982,13 @@ PyFrame_Fini(void)
ae2451
     Py_XDECREF(builtin_object);
ae2451
     builtin_object = NULL;
ae2451
 }
ae2451
+
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyFrame_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    _PyDebugAllocatorStats(out,
ae2451
+                           "free PyFrameObject",
ae2451
+                           numfree, sizeof(PyFrameObject));
ae2451
+}
ae2451
+
ae2451
diff --git a/Objects/intobject.c b/Objects/intobject.c
ae2451
index 28182f9..f442ea0 100644
ae2451
--- a/Objects/intobject.c
ae2451
+++ b/Objects/intobject.c
ae2451
@@ -44,6 +44,23 @@ typedef struct _intblock PyIntBlock;
ae2451
 static PyIntBlock *block_list = NULL;
ae2451
 static PyIntObject *free_list = NULL;
ae2451
 
ae2451
+
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyInt_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    int num_blocks = 0;
ae2451
+    PyIntBlock *block;
ae2451
+
ae2451
+    /* Walk the block list, counting */
ae2451
+    for (block = block_list; block ; block = block->next) {
ae2451
+        num_blocks++;
ae2451
+    }
ae2451
+
ae2451
+    _PyDebugAllocatorStats(out,
ae2451
+                           "PyIntBlock", num_blocks, sizeof(PyIntBlock));
ae2451
+}
ae2451
+
ae2451
 static PyIntObject *
ae2451
 fill_free_list(void)
ae2451
 {
ae2451
diff --git a/Objects/listobject.c b/Objects/listobject.c
ae2451
index f753643..e6fa17d 100644
ae2451
--- a/Objects/listobject.c
ae2451
+++ b/Objects/listobject.c
ae2451
@@ -109,6 +109,15 @@ PyList_Fini(void)
ae2451
     }
ae2451
 }
ae2451
 
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyList_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    _PyDebugAllocatorStats(out,
ae2451
+                           "free PyListObject",
ae2451
+                           numfree, sizeof(PyListObject));
ae2451
+}
ae2451
+
ae2451
 PyObject *
ae2451
 PyList_New(Py_ssize_t size)
ae2451
 {
ae2451
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
ae2451
index 0b60ca3..3193135 100644
ae2451
--- a/Objects/methodobject.c
ae2451
+++ b/Objects/methodobject.c
ae2451
@@ -412,6 +412,15 @@ PyCFunction_Fini(void)
ae2451
     (void)PyCFunction_ClearFreeList();
ae2451
 }
ae2451
 
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyCFunction_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    _PyDebugAllocatorStats(out,
ae2451
+                           "free PyCFunction",
ae2451
+                           numfree, sizeof(PyCFunction));
ae2451
+}
ae2451
+
ae2451
 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
ae2451
    but it's part of the API so we need to keep a function around that
ae2451
    existing C extensions can call.
ae2451
diff --git a/Objects/object.c b/Objects/object.c
ae2451
index 14f4e9f..68aedcd 100644
ae2451
--- a/Objects/object.c
ae2451
+++ b/Objects/object.c
ae2451
@@ -2355,6 +2355,23 @@ PyMem_Free(void *p)
ae2451
     PyMem_FREE(p);
ae2451
 }
ae2451
 
ae2451
+void
ae2451
+_PyObject_DebugTypeStats(FILE *out)
ae2451
+{
ae2451
+    _PyString_DebugMallocStats(out);
ae2451
+    _PyCFunction_DebugMallocStats(out);
ae2451
+    _PyDict_DebugMallocStats(out);
ae2451
+    _PyFloat_DebugMallocStats(out);
ae2451
+    _PyFrame_DebugMallocStats(out);
ae2451
+    _PyInt_DebugMallocStats(out);
ae2451
+    _PyList_DebugMallocStats(out);
ae2451
+    _PyMethod_DebugMallocStats(out);
ae2451
+    _PySet_DebugMallocStats(out);
ae2451
+    _PyTuple_DebugMallocStats(out);
ae2451
+#if Py_USING_UNICODE
ae2451
+    _PyUnicode_DebugMallocStats(out);
ae2451
+#endif
ae2451
+}
ae2451
 
ae2451
 /* These methods are used to control infinite recursion in repr, str, print,
ae2451
    etc.  Container objects that may recursively contain themselves,
ae2451
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
ae2451
index 38ebc37..2c05359 100644
ae2451
--- a/Objects/obmalloc.c
ae2451
+++ b/Objects/obmalloc.c
ae2451
@@ -508,12 +508,10 @@ static struct arena_object* usable_arenas = NULL;
ae2451
 /* Number of arenas allocated that haven't been free()'d. */
ae2451
 static size_t narenas_currently_allocated = 0;
ae2451
 
ae2451
-#ifdef PYMALLOC_DEBUG
ae2451
 /* Total number of times malloc() called to allocate an arena. */
ae2451
 static size_t ntimes_arena_allocated = 0;
ae2451
 /* High water mark (max value ever seen) for narenas_currently_allocated. */
ae2451
 static size_t narenas_highwater = 0;
ae2451
-#endif
ae2451
 
ae2451
 /* Allocate a new arena.  If we run out of memory, return NULL.  Else
ae2451
  * allocate a new arena, and return the address of an arena_object
ae2451
@@ -528,7 +526,7 @@ new_arena(void)
ae2451
 
ae2451
 #ifdef PYMALLOC_DEBUG
ae2451
     if (Py_GETENV("PYTHONMALLOCSTATS"))
ae2451
-        _PyObject_DebugMallocStats();
ae2451
+        _PyObject_DebugMallocStats(stderr);
ae2451
 #endif
ae2451
     if (unused_arena_objects == NULL) {
ae2451
         uint i;
ae2451
@@ -588,11 +586,9 @@ new_arena(void)
ae2451
     }
ae2451
 
ae2451
     ++narenas_currently_allocated;
ae2451
-#ifdef PYMALLOC_DEBUG
ae2451
     ++ntimes_arena_allocated;
ae2451
     if (narenas_currently_allocated > narenas_highwater)
ae2451
         narenas_highwater = narenas_currently_allocated;
ae2451
-#endif
ae2451
     arenaobj->freepools = NULL;
ae2451
     /* pool_address <- first pool-aligned address in the arena
ae2451
        nfreepools <- number of whole pools that fit after alignment */
ae2451
@@ -1694,17 +1690,19 @@ _PyObject_DebugDumpAddress(const void *p)
ae2451
     }
ae2451
 }
ae2451
 
ae2451
+#endif  /* PYMALLOC_DEBUG */
ae2451
+
ae2451
 static size_t
ae2451
-printone(const char* msg, size_t value)
ae2451
+printone(FILE *out, const char* msg, size_t value)
ae2451
 {
ae2451
     int i, k;
ae2451
     char buf[100];
ae2451
     size_t origvalue = value;
ae2451
 
ae2451
-    fputs(msg, stderr);
ae2451
+    fputs(msg, out);
ae2451
     for (i = (int)strlen(msg); i < 35; ++i)
ae2451
-        fputc(' ', stderr);
ae2451
-    fputc('=', stderr);
ae2451
+        fputc(' ', out);
ae2451
+    fputc('=', out);
ae2451
 
ae2451
     /* Write the value with commas. */
ae2451
     i = 22;
ae2451
@@ -1725,17 +1723,32 @@ printone(const char* msg, size_t value)
ae2451
 
ae2451
     while (i >= 0)
ae2451
         buf[i--] = ' ';
ae2451
-    fputs(buf, stderr);
ae2451
+    fputs(buf, out);
ae2451
 
ae2451
     return origvalue;
ae2451
 }
ae2451
 
ae2451
-/* Print summary info to stderr about the state of pymalloc's structures.
ae2451
+void
ae2451
+_PyDebugAllocatorStats(FILE *out,
ae2451
+                       const char *block_name, int num_blocks, size_t sizeof_block)
ae2451
+{
ae2451
+       char buf1[128];
ae2451
+       char buf2[128];
ae2451
+       PyOS_snprintf(buf1, sizeof(buf1),
ae2451
+                     "%d %ss * %zd bytes each",
ae2451
+                     num_blocks, block_name, sizeof_block);
ae2451
+       PyOS_snprintf(buf2, sizeof(buf2),
ae2451
+                     "%48s ", buf1);
ae2451
+      (void)printone(out, buf2, num_blocks * sizeof_block);
ae2451
+}
ae2451
+
ae2451
+
ae2451
+/* Print summary info to "out" about the state of pymalloc's structures.
ae2451
  * In Py_DEBUG mode, also perform some expensive internal consistency
ae2451
  * checks.
ae2451
  */
ae2451
 void
ae2451
-_PyObject_DebugMallocStats(void)
ae2451
+_PyObject_DebugMallocStats(FILE *out)
ae2451
 {
ae2451
     uint i;
ae2451
     const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
ae2451
@@ -1764,7 +1777,7 @@ _PyObject_DebugMallocStats(void)
ae2451
     size_t total;
ae2451
     char buf[128];
ae2451
 
ae2451
-    fprintf(stderr, "Small block threshold = %d, in %u size classes.\n",
ae2451
+    fprintf(out, "Small block threshold = %d, in %u size classes.\n",
ae2451
             SMALL_REQUEST_THRESHOLD, numclasses);
ae2451
 
ae2451
     for (i = 0; i < numclasses; ++i)
ae2451
@@ -1818,10 +1831,10 @@ _PyObject_DebugMallocStats(void)
ae2451
     }
ae2451
     assert(narenas == narenas_currently_allocated);
ae2451
 
ae2451
-    fputc('\n', stderr);
ae2451
+    fputc('\n', out);
ae2451
     fputs("class   size   num pools   blocks in use  avail blocks\n"
ae2451
           "-----   ----   ---------   -------------  ------------\n",
ae2451
-          stderr);
ae2451
+          out);
ae2451
 
ae2451
     for (i = 0; i < numclasses; ++i) {
ae2451
         size_t p = numpools[i];
ae2451
@@ -1832,7 +1845,7 @@ _PyObject_DebugMallocStats(void)
ae2451
             assert(b == 0 && f == 0);
ae2451
             continue;
ae2451
         }
ae2451
-        fprintf(stderr, "%5u %6u "
ae2451
+        fprintf(out, "%5u %6u "
ae2451
                         "%11" PY_FORMAT_SIZE_T "u "
ae2451
                         "%15" PY_FORMAT_SIZE_T "u "
ae2451
                         "%13" PY_FORMAT_SIZE_T "u\n",
ae2451
@@ -1842,36 +1855,35 @@ _PyObject_DebugMallocStats(void)
ae2451
         pool_header_bytes += p * POOL_OVERHEAD;
ae2451
         quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
ae2451
     }
ae2451
-    fputc('\n', stderr);
ae2451
-    (void)printone("# times object malloc called", serialno);
ae2451
-
ae2451
-    (void)printone("# arenas allocated total", ntimes_arena_allocated);
ae2451
-    (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas);
ae2451
-    (void)printone("# arenas highwater mark", narenas_highwater);
ae2451
-    (void)printone("# arenas allocated current", narenas);
ae2451
+    fputc('\n', out);
ae2451
+#ifdef PYMALLOC_DEBUG
ae2451
+    (void)printone(out, "# times object malloc called", serialno);
ae2451
+#endif
ae2451
+    (void)printone(out, "# arenas allocated total", ntimes_arena_allocated);
ae2451
+    (void)printone(out, "# arenas reclaimed", ntimes_arena_allocated - narenas);
ae2451
+    (void)printone(out, "# arenas highwater mark", narenas_highwater);
ae2451
+    (void)printone(out, "# arenas allocated current", narenas);
ae2451
 
ae2451
     PyOS_snprintf(buf, sizeof(buf),
ae2451
         "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena",
ae2451
         narenas, ARENA_SIZE);
ae2451
-    (void)printone(buf, narenas * ARENA_SIZE);
ae2451
+    (void)printone(out, buf, narenas * ARENA_SIZE);
ae2451
 
ae2451
-    fputc('\n', stderr);
ae2451
+    fputc('\n', out);
ae2451
 
ae2451
-    total = printone("# bytes in allocated blocks", allocated_bytes);
ae2451
-    total += printone("# bytes in available blocks", available_bytes);
ae2451
+    total = printone(out, "# bytes in allocated blocks", allocated_bytes);
ae2451
+    total += printone(out, "# bytes in available blocks", available_bytes);
ae2451
 
ae2451
     PyOS_snprintf(buf, sizeof(buf),
ae2451
         "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
ae2451
-    total += printone(buf, (size_t)numfreepools * POOL_SIZE);
ae2451
+    total += printone(out, buf, (size_t)numfreepools * POOL_SIZE);
ae2451
 
ae2451
-    total += printone("# bytes lost to pool headers", pool_header_bytes);
ae2451
-    total += printone("# bytes lost to quantization", quantization);
ae2451
-    total += printone("# bytes lost to arena alignment", arena_alignment);
ae2451
-    (void)printone("Total", total);
ae2451
+    total += printone(out, "# bytes lost to pool headers", pool_header_bytes);
ae2451
+    total += printone(out, "# bytes lost to quantization", quantization);
ae2451
+    total += printone(out, "# bytes lost to arena alignment", arena_alignment);
ae2451
+    (void)printone(out, "Total", total);
ae2451
 }
ae2451
 
ae2451
-#endif  /* PYMALLOC_DEBUG */
ae2451
-
ae2451
 #ifdef Py_USING_MEMORY_DEBUGGER
ae2451
 /* Make this function last so gcc won't inline it since the definition is
ae2451
  * after the reference.
ae2451
diff --git a/Objects/setobject.c b/Objects/setobject.c
ae2451
index af1ce16..3439b7c 100644
ae2451
--- a/Objects/setobject.c
ae2451
+++ b/Objects/setobject.c
ae2451
@@ -1088,6 +1088,16 @@ PySet_Fini(void)
ae2451
     Py_CLEAR(emptyfrozenset);
ae2451
 }
ae2451
 
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PySet_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    _PyDebugAllocatorStats(out,
ae2451
+                           "free PySetObject",
ae2451
+                           numfree, sizeof(PySetObject));
ae2451
+}
ae2451
+
ae2451
+
ae2451
 static PyObject *
ae2451
 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ae2451
 {
ae2451
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
ae2451
index 1209197..b8646dd 100644
ae2451
--- a/Objects/stringobject.c
ae2451
+++ b/Objects/stringobject.c
ae2451
@@ -4843,3 +4843,43 @@ void _Py_ReleaseInternedStrings(void)
ae2451
     PyDict_Clear(interned);
ae2451
     Py_CLEAR(interned);
ae2451
 }
ae2451
+
ae2451
+void _PyString_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    ssize_t i;
ae2451
+    int num_immortal = 0, num_mortal = 0;
ae2451
+    ssize_t immortal_size = 0, mortal_size = 0;
ae2451
+
ae2451
+    if (interned == NULL || !PyDict_Check(interned))
ae2451
+        return;
ae2451
+
ae2451
+    for (i = 0; i <= ((PyDictObject*)interned)->ma_mask; i++) {
ae2451
+        PyDictEntry *ep = ((PyDictObject*)interned)->ma_table + i;
ae2451
+        PyObject *pvalue = ep->me_value;
ae2451
+        if (pvalue != NULL) {
ae2451
+            PyStringObject *s = (PyStringObject *)ep->me_key;
ae2451
+
ae2451
+            switch (s->ob_sstate) {
ae2451
+            case SSTATE_NOT_INTERNED:
ae2451
+                /* XXX Shouldn't happen */
ae2451
+                break;
ae2451
+            case SSTATE_INTERNED_IMMORTAL:
ae2451
+                num_immortal ++;
ae2451
+                immortal_size += s->ob_size;
ae2451
+                break;
ae2451
+            case SSTATE_INTERNED_MORTAL:
ae2451
+                num_mortal ++;
ae2451
+                mortal_size += s->ob_size;
ae2451
+                break;
ae2451
+            default:
ae2451
+                Py_FatalError("Inconsistent interned string state.");
ae2451
+            }
ae2451
+        }
ae2451
+    }
ae2451
+
ae2451
+    fprintf(out, "%d mortal interned strings\n", num_mortal);
ae2451
+    fprintf(out, "%d immortal interned strings\n", num_immortal);
ae2451
+    fprintf(out, "total size of all interned strings: "
ae2451
+            "%zi/%zi "
ae2451
+            "mortal/immortal\n", mortal_size, immortal_size);
ae2451
+}
ae2451
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
ae2451
index 00f2e47..7682d81 100644
ae2451
--- a/Objects/tupleobject.c
ae2451
+++ b/Objects/tupleobject.c
ae2451
@@ -44,6 +44,22 @@ show_track(void)
ae2451
 }
ae2451
 #endif
ae2451
 
ae2451
+/* Print summary info about the state of the optimized allocator */
ae2451
+void
ae2451
+_PyTuple_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+#if PyTuple_MAXSAVESIZE > 0
ae2451
+    int i;
ae2451
+    char buf[128];
ae2451
+    for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
ae2451
+        PyOS_snprintf(buf, sizeof(buf),
ae2451
+                      "free %d-sized PyTupleObject", i);
ae2451
+        _PyDebugAllocatorStats(out,
ae2451
+                               buf,
ae2451
+                               numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
ae2451
+    }
ae2451
+#endif
ae2451
+}
ae2451
 
ae2451
 PyObject *
ae2451
 PyTuple_New(register Py_ssize_t size)
ae2451
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
ae2451
index 6bea370..ced9acf 100644
ae2451
--- a/Objects/unicodeobject.c
ae2451
+++ b/Objects/unicodeobject.c
ae2451
@@ -8920,6 +8920,12 @@ _PyUnicode_Fini(void)
ae2451
     (void)PyUnicode_ClearFreeList();
ae2451
 }
ae2451
 
ae2451
+void _PyUnicode_DebugMallocStats(FILE *out)
ae2451
+{
ae2451
+    _PyDebugAllocatorStats(out, "free PyUnicodeObject", numfree,
ae2451
+                           sizeof(PyUnicodeObject));
ae2451
+}
ae2451
+
ae2451
 #ifdef __cplusplus
ae2451
 }
ae2451
 #endif
ae2451
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
ae2451
index f0fbd74..0b73f3a 100644
ae2451
--- a/Python/pythonrun.c
ae2451
+++ b/Python/pythonrun.c
ae2451
@@ -557,7 +557,7 @@ Py_Finalize(void)
ae2451
 #endif /* Py_TRACE_REFS */
ae2451
 #ifdef PYMALLOC_DEBUG
ae2451
     if (Py_GETENV("PYTHONMALLOCSTATS"))
ae2451
-        _PyObject_DebugMallocStats();
ae2451
+        _PyObject_DebugMallocStats(stderr);
ae2451
 #endif
ae2451
 
ae2451
     call_ll_exitfuncs();
ae2451
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
ae2451
index 2a7c207..fbb637b 100644
ae2451
--- a/Python/sysmodule.c
ae2451
+++ b/Python/sysmodule.c
ae2451
@@ -873,6 +873,57 @@ a 11-tuple where the entries in the tuple are counts of:\n\
ae2451
 extern "C" {
ae2451
 #endif
ae2451
 
ae2451
+static PyObject *
ae2451
+sys_debugmallocstats(PyObject *self, PyObject *args)
ae2451
+{
ae2451
+    PyObject *file = NULL;
ae2451
+    FILE *fp;
ae2451
+
ae2451
+    if (!PyArg_ParseTuple(args, "|O!",
ae2451
+                          &PyFile_Type, &file)) {
ae2451
+      return NULL;
ae2451
+    }
ae2451
+    if (!file) {
ae2451
+        /* Default to sys.stderr: */
ae2451
+      file = PySys_GetObject("stderr");
ae2451
+      if (!file) {
ae2451
+          PyErr_SetString(PyExc_ValueError, "sys.stderr not set");
ae2451
+          return NULL;
ae2451
+      }
ae2451
+      if (!PyFile_Check(file)) {
ae2451
+          PyErr_SetString(PyExc_TypeError, "sys.stderr is not a file");
ae2451
+          return NULL;
ae2451
+      }
ae2451
+    }
ae2451
+
ae2451
+    Py_INCREF(file);
ae2451
+    /* OK, we now own a ref on non-NULL "file" */
ae2451
+
ae2451
+    fp = PyFile_AsFile(file);
ae2451
+    if (!fp) {
ae2451
+        PyErr_SetString(PyExc_ValueError, "file is closed");
ae2451
+        Py_DECREF(file);
ae2451
+        return NULL;
ae2451
+    }
ae2451
+
ae2451
+    _PyObject_DebugMallocStats(fp);
ae2451
+    fputc('\n', fp);
ae2451
+    _PyObject_DebugTypeStats(fp);
ae2451
+
ae2451
+    Py_DECREF(file);
ae2451
+
ae2451
+    Py_RETURN_NONE;
ae2451
+}
ae2451
+PyDoc_STRVAR(debugmallocstats_doc,
ae2451
+"_debugmallocstats([file])\n\
ae2451
+\n\
ae2451
+Print summary info to the given file (or sys.stderr) about the state of\n\
ae2451
+pymalloc's structures.\n\
ae2451
+\n\
ae2451
+In Py_DEBUG mode, also perform some expensive internal consistency\n\
ae2451
+checks.\n\
ae2451
+");
ae2451
+
ae2451
 #ifdef Py_TRACE_REFS
ae2451
 /* Defined in objects.c because it uses static globals if that file */
ae2451
 extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
ae2451
@@ -971,6 +1022,8 @@ static PyMethodDef sys_methods[] = {
ae2451
     {"settrace",        sys_settrace, METH_O, settrace_doc},
ae2451
     {"gettrace",        sys_gettrace, METH_NOARGS, gettrace_doc},
ae2451
     {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
ae2451
+    {"_debugmallocstats", sys_debugmallocstats, METH_VARARGS,
ae2451
+     debugmallocstats_doc},
ae2451
     {NULL,              NULL}           /* sentinel */
ae2451
 };
ae2451