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

98d66e
diff --git a/Include/dictobject.h b/Include/dictobject.h
98d66e
index 5a1e9fe..da89cec 100644
98d66e
--- a/Include/dictobject.h
98d66e
+++ b/Include/dictobject.h
98d66e
@@ -154,6 +154,8 @@ PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
98d66e
 PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
98d66e
 PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Include/floatobject.h b/Include/floatobject.h
98d66e
index 54e8825..33c6ac0 100644
98d66e
--- a/Include/floatobject.h
98d66e
+++ b/Include/floatobject.h
98d66e
@@ -132,6 +132,7 @@ PyAPI_FUNC(PyObject *) _PyFloat_FormatAdvanced(PyObject *obj,
98d66e
    failure.  Used in builtin_round in bltinmodule.c. */
98d66e
 PyAPI_FUNC(PyObject *) _Py_double_round(double x, int ndigits);
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);
98d66e
 
98d66e
 
98d66e
 #ifdef __cplusplus
98d66e
diff --git a/Include/frameobject.h b/Include/frameobject.h
98d66e
index 3460379..db89a4a 100644
98d66e
--- a/Include/frameobject.h
98d66e
+++ b/Include/frameobject.h
98d66e
@@ -80,6 +80,8 @@ PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
98d66e
 
98d66e
 PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);
98d66e
+
98d66e
 /* Return the line of code the frame is currently executing. */
98d66e
 PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
98d66e
 
98d66e
diff --git a/Include/intobject.h b/Include/intobject.h
98d66e
index d198574..60cb9e0 100644
98d66e
--- a/Include/intobject.h
98d66e
+++ b/Include/intobject.h
98d66e
@@ -78,6 +78,8 @@ PyAPI_FUNC(PyObject *) _PyInt_FormatAdvanced(PyObject *obj,
98d66e
 					     char *format_spec,
98d66e
 					     Py_ssize_t format_spec_len);
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyInt_DebugMallocStats(FILE *out);
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Include/listobject.h b/Include/listobject.h
98d66e
index f19b1c5..7fccb47 100644
98d66e
--- a/Include/listobject.h
98d66e
+++ b/Include/listobject.h
98d66e
@@ -62,6 +62,8 @@ PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
98d66e
 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
98d66e
 #define PyList_GET_SIZE(op)    Py_SIZE(op)
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Include/methodobject.h b/Include/methodobject.h
98d66e
index 6e160b6..1944517 100644
98d66e
--- a/Include/methodobject.h
98d66e
+++ b/Include/methodobject.h
98d66e
@@ -87,6 +87,10 @@ typedef struct {
98d66e
 
98d66e
 PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyCFunction_DebugMallocStats(FILE *out);
98d66e
+PyAPI_FUNC(void) _PyMethod_DebugMallocStats(FILE *out);
98d66e
+
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Include/object.h b/Include/object.h
98d66e
index 807b241..a9d2079 100644
98d66e
--- a/Include/object.h
98d66e
+++ b/Include/object.h
98d66e
@@ -1040,6 +1040,13 @@ PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
98d66e
             _PyTrash_thread_deposit_object((PyObject*)op); \
98d66e
     } while (0);
98d66e
 
98d66e
+PyAPI_FUNC(void)
98d66e
+_PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
98d66e
+		       size_t sizeof_block);
98d66e
+
98d66e
+PyAPI_FUNC(void)
98d66e
+_PyObject_DebugTypeStats(FILE *out);
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Include/objimpl.h b/Include/objimpl.h
98d66e
index cbf6bc3..8c14ab8 100644
98d66e
--- a/Include/objimpl.h
98d66e
+++ b/Include/objimpl.h
98d66e
@@ -101,13 +101,13 @@ PyAPI_FUNC(void) PyObject_Free(void *);
98d66e
 
98d66e
 /* Macros */
98d66e
 #ifdef WITH_PYMALLOC
98d66e
+PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out);
98d66e
 #ifdef PYMALLOC_DEBUG   /* WITH_PYMALLOC && PYMALLOC_DEBUG */
98d66e
 PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
98d66e
 PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
98d66e
 PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
98d66e
 PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
98d66e
 PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
98d66e
-PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
98d66e
 PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes);
98d66e
 PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes);
98d66e
 PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p);
98d66e
diff --git a/Include/setobject.h b/Include/setobject.h
98d66e
index 52b07d5..73a37b6 100644
98d66e
--- a/Include/setobject.h
98d66e
+++ b/Include/setobject.h
98d66e
@@ -92,6 +92,7 @@ PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key);
98d66e
 PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash);
98d66e
 PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
98d66e
 PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
98d66e
+PyAPI_FUNC(void) _PySet_DebugMallocStats(FILE *out);
98d66e
 
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
diff --git a/Include/stringobject.h b/Include/stringobject.h
98d66e
index 12cc093..0a5fbd1 100644
98d66e
--- a/Include/stringobject.h
98d66e
+++ b/Include/stringobject.h
98d66e
@@ -204,6 +204,8 @@ PyAPI_FUNC(PyObject *) _PyBytes_FormatAdvanced(PyObject *obj,
98d66e
 					       char *format_spec,
98d66e
 					       Py_ssize_t format_spec_len);
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyString_DebugMallocStats(FILE *out);
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Include/tupleobject.h b/Include/tupleobject.h
98d66e
index a5ab733..27e6ca6 100644
98d66e
--- a/Include/tupleobject.h
98d66e
+++ b/Include/tupleobject.h
98d66e
@@ -54,6 +54,7 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
98d66e
 #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = v)
98d66e
 
98d66e
 PyAPI_FUNC(int) PyTuple_ClearFreeList(void);
98d66e
+PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);
98d66e
 
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
98d66e
index 7781f96..321bd20 100644
98d66e
--- a/Include/unicodeobject.h
98d66e
+++ b/Include/unicodeobject.h
98d66e
@@ -1406,6 +1406,8 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
98d66e
     Py_UNICODE ch       /* Unicode character */
98d66e
     );
98d66e
 
98d66e
+PyAPI_FUNC(void) _PyUnicode_DebugMallocStats(FILE *out);
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
98d66e
index 9342716..8eeb5ab 100644
98d66e
--- a/Lib/test/test_sys.py
98d66e
+++ b/Lib/test/test_sys.py
98d66e
@@ -487,6 +487,32 @@ class SysModuleTest(unittest.TestCase):
98d66e
         p.wait()
98d66e
         self.assertIn(executable, ["''", repr(sys.executable)])
98d66e
 
98d66e
+    def test_debugmallocstats(self):
98d66e
+        # Test sys._debugmallocstats()
98d66e
+
98d66e
+        import subprocess
98d66e
+
98d66e
+        # Verify the default of writing to stderr:
98d66e
+        p = subprocess.Popen([sys.executable,
98d66e
+                              '-c', 'import sys; sys._debugmallocstats()'],
98d66e
+                             stderr=subprocess.PIPE)
98d66e
+        out, err = p.communicate()
98d66e
+        p.wait()
98d66e
+        self.assertIn("arenas allocated current", err)
98d66e
+
98d66e
+        # Verify that we can redirect the output to a file (not a file-like
98d66e
+        # object, though):
98d66e
+        with open('mallocstats.txt', 'w') as out:
98d66e
+            sys._debugmallocstats(out)
98d66e
+        result = open('mallocstats.txt').read()
98d66e
+        self.assertIn("arenas allocated current", result)
98d66e
+        os.unlink('mallocstats.txt')
98d66e
+
98d66e
+        # Verify that the destination must be a file:
98d66e
+        with self.assertRaises(TypeError):
98d66e
+            sys._debugmallocstats(42)
98d66e
+
98d66e
+
98d66e
 @test.test_support.cpython_only
98d66e
 class SizeofTest(unittest.TestCase):
98d66e
 
98d66e
diff --git a/Objects/classobject.c b/Objects/classobject.c
98d66e
index 02d7cfd..1c44a47 100644
98d66e
--- a/Objects/classobject.c
98d66e
+++ b/Objects/classobject.c
98d66e
@@ -2691,3 +2691,12 @@ PyMethod_Fini(void)
98d66e
 {
98d66e
     (void)PyMethod_ClearFreeList();
98d66e
 }
98d66e
+
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyMethod_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    _PyDebugAllocatorStats(out,
98d66e
+                           "free PyMethodObject",
98d66e
+                           numfree, sizeof(PyMethodObject));
98d66e
+}
98d66e
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
98d66e
index c544ecd..89ca39c 100644
98d66e
--- a/Objects/dictobject.c
98d66e
+++ b/Objects/dictobject.c
98d66e
@@ -225,6 +225,15 @@ show_track(void)
98d66e
 static PyDictObject *free_list[PyDict_MAXFREELIST];
98d66e
 static int numfree = 0;
98d66e
 
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyDict_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    _PyDebugAllocatorStats(out,
98d66e
+                           "free PyDictObject", numfree, sizeof(PyDictObject));
98d66e
+}
98d66e
+
98d66e
+
98d66e
 void
98d66e
 PyDict_Fini(void)
98d66e
 {
98d66e
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
98d66e
index 5954d39..02acc8c 100644
98d66e
--- a/Objects/floatobject.c
98d66e
+++ b/Objects/floatobject.c
98d66e
@@ -34,6 +34,22 @@ typedef struct _floatblock PyFloatBlock;
98d66e
 static PyFloatBlock *block_list = NULL;
98d66e
 static PyFloatObject *free_list = NULL;
98d66e
 
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyFloat_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+  int num_blocks = 0;
98d66e
+  PyFloatBlock *block;
98d66e
+
98d66e
+  /* Walk the block list, counting */
98d66e
+  for (block = block_list; block ; block = block->next) {
98d66e
+      num_blocks++;
98d66e
+  }
98d66e
+
98d66e
+  _PyDebugAllocatorStats(out,
98d66e
+                         "PyFloatBlock", num_blocks, sizeof(PyFloatBlock));
98d66e
+}
98d66e
+
98d66e
 static PyFloatObject *
98d66e
 fill_free_list(void)
98d66e
 {
98d66e
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
98d66e
index 4c91dd0..03a66dc 100644
98d66e
--- a/Objects/frameobject.c
98d66e
+++ b/Objects/frameobject.c
98d66e
@@ -1019,3 +1019,13 @@ PyFrame_Fini(void)
98d66e
     Py_XDECREF(builtin_object);
98d66e
     builtin_object = NULL;
98d66e
 }
98d66e
+
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyFrame_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    _PyDebugAllocatorStats(out,
98d66e
+                           "free PyFrameObject",
98d66e
+                           numfree, sizeof(PyFrameObject));
98d66e
+}
98d66e
+
98d66e
diff --git a/Objects/intobject.c b/Objects/intobject.c
98d66e
index 9b27c35..703fa5a 100644
98d66e
--- a/Objects/intobject.c
98d66e
+++ b/Objects/intobject.c
98d66e
@@ -44,6 +44,23 @@ typedef struct _intblock PyIntBlock;
98d66e
 static PyIntBlock *block_list = NULL;
98d66e
 static PyIntObject *free_list = NULL;
98d66e
 
98d66e
+
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyInt_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    int num_blocks = 0;
98d66e
+    PyIntBlock *block;
98d66e
+
98d66e
+    /* Walk the block list, counting */
98d66e
+    for (block = block_list; block ; block = block->next) {
98d66e
+        num_blocks++;
98d66e
+    }
98d66e
+
98d66e
+    _PyDebugAllocatorStats(out,
98d66e
+                           "PyIntBlock", num_blocks, sizeof(PyIntBlock));
98d66e
+}
98d66e
+
98d66e
 static PyIntObject *
98d66e
 fill_free_list(void)
98d66e
 {
98d66e
diff --git a/Objects/listobject.c b/Objects/listobject.c
98d66e
index 24eff76..38848bd 100644
98d66e
--- a/Objects/listobject.c
98d66e
+++ b/Objects/listobject.c
98d66e
@@ -109,6 +109,15 @@ PyList_Fini(void)
98d66e
     }
98d66e
 }
98d66e
 
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyList_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    _PyDebugAllocatorStats(out,
98d66e
+                           "free PyListObject",
98d66e
+                           numfree, sizeof(PyListObject));
98d66e
+}
98d66e
+
98d66e
 PyObject *
98d66e
 PyList_New(Py_ssize_t size)
98d66e
 {
98d66e
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
98d66e
index c1a99ab..ea5df77 100644
98d66e
--- a/Objects/methodobject.c
98d66e
+++ b/Objects/methodobject.c
98d66e
@@ -412,6 +412,15 @@ PyCFunction_Fini(void)
98d66e
     (void)PyCFunction_ClearFreeList();
98d66e
 }
98d66e
 
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyCFunction_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    _PyDebugAllocatorStats(out,
98d66e
+                           "free PyCFunction",
98d66e
+                           numfree, sizeof(PyCFunction));
98d66e
+}
98d66e
+
98d66e
 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
98d66e
    but it's part of the API so we need to keep a function around that
98d66e
    existing C extensions can call.
98d66e
diff --git a/Objects/object.c b/Objects/object.c
98d66e
index 65366b0..acef3ce 100644
98d66e
--- a/Objects/object.c
98d66e
+++ b/Objects/object.c
98d66e
@@ -2360,6 +2360,23 @@ PyMem_Free(void *p)
98d66e
     PyMem_FREE(p);
98d66e
 }
98d66e
 
98d66e
+void
98d66e
+_PyObject_DebugTypeStats(FILE *out)
98d66e
+{
98d66e
+    _PyString_DebugMallocStats(out);
98d66e
+    _PyCFunction_DebugMallocStats(out);
98d66e
+    _PyDict_DebugMallocStats(out);
98d66e
+    _PyFloat_DebugMallocStats(out);
98d66e
+    _PyFrame_DebugMallocStats(out);
98d66e
+    _PyInt_DebugMallocStats(out);
98d66e
+    _PyList_DebugMallocStats(out);
98d66e
+    _PyMethod_DebugMallocStats(out);
98d66e
+    _PySet_DebugMallocStats(out);
98d66e
+    _PyTuple_DebugMallocStats(out);
98d66e
+#if Py_USING_UNICODE
98d66e
+    _PyUnicode_DebugMallocStats(out);
98d66e
+#endif
98d66e
+}
98d66e
 
98d66e
 /* These methods are used to control infinite recursion in repr, str, print,
98d66e
    etc.  Container objects that may recursively contain themselves,
98d66e
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
98d66e
index 0778c85..f049f5c 100644
98d66e
--- a/Objects/obmalloc.c
98d66e
+++ b/Objects/obmalloc.c
98d66e
@@ -541,12 +541,10 @@ static struct arena_object* usable_arenas = NULL;
98d66e
 /* Number of arenas allocated that haven't been free()'d. */
98d66e
 static size_t narenas_currently_allocated = 0;
98d66e
 
98d66e
-#ifdef PYMALLOC_DEBUG
98d66e
 /* Total number of times malloc() called to allocate an arena. */
98d66e
 static size_t ntimes_arena_allocated = 0;
98d66e
 /* High water mark (max value ever seen) for narenas_currently_allocated. */
98d66e
 static size_t narenas_highwater = 0;
98d66e
-#endif
98d66e
 
98d66e
 /* Allocate a new arena.  If we run out of memory, return NULL.  Else
98d66e
  * allocate a new arena, and return the address of an arena_object
98d66e
@@ -563,7 +561,7 @@ new_arena(void)
98d66e
 
98d66e
 #ifdef PYMALLOC_DEBUG
98d66e
     if (Py_GETENV("PYTHONMALLOCSTATS"))
98d66e
-        _PyObject_DebugMallocStats();
98d66e
+        _PyObject_DebugMallocStats(stderr);
98d66e
 #endif
98d66e
     if (unused_arena_objects == NULL) {
98d66e
         uint i;
98d66e
@@ -631,11 +629,9 @@ new_arena(void)
98d66e
     arenaobj->address = (uptr)address;
98d66e
 
98d66e
     ++narenas_currently_allocated;
98d66e
-#ifdef PYMALLOC_DEBUG
98d66e
     ++ntimes_arena_allocated;
98d66e
     if (narenas_currently_allocated > narenas_highwater)
98d66e
         narenas_highwater = narenas_currently_allocated;
98d66e
-#endif
98d66e
     arenaobj->freepools = NULL;
98d66e
     /* pool_address <- first pool-aligned address in the arena
98d66e
        nfreepools <- number of whole pools that fit after alignment */
98d66e
@@ -1796,17 +1792,19 @@ _PyObject_DebugDumpAddress(const void *p)
98d66e
     }
98d66e
 }
98d66e
 
98d66e
+#endif  /* PYMALLOC_DEBUG */
98d66e
+
98d66e
 static size_t
98d66e
-printone(const char* msg, size_t value)
98d66e
+printone(FILE *out, const char* msg, size_t value)
98d66e
 {
98d66e
     int i, k;
98d66e
     char buf[100];
98d66e
     size_t origvalue = value;
98d66e
 
98d66e
-    fputs(msg, stderr);
98d66e
+    fputs(msg, out);
98d66e
     for (i = (int)strlen(msg); i < 35; ++i)
98d66e
-        fputc(' ', stderr);
98d66e
-    fputc('=', stderr);
98d66e
+        fputc(' ', out);
98d66e
+    fputc('=', out);
98d66e
 
98d66e
     /* Write the value with commas. */
98d66e
     i = 22;
98d66e
@@ -1827,17 +1825,32 @@ printone(const char* msg, size_t value)
98d66e
 
98d66e
     while (i >= 0)
98d66e
         buf[i--] = ' ';
98d66e
-    fputs(buf, stderr);
98d66e
+    fputs(buf, out);
98d66e
 
98d66e
     return origvalue;
98d66e
 }
98d66e
 
98d66e
-/* Print summary info to stderr about the state of pymalloc's structures.
98d66e
+void
98d66e
+_PyDebugAllocatorStats(FILE *out,
98d66e
+                       const char *block_name, int num_blocks, size_t sizeof_block)
98d66e
+{
98d66e
+       char buf1[128];
98d66e
+       char buf2[128];
98d66e
+       PyOS_snprintf(buf1, sizeof(buf1),
98d66e
+                     "%d %ss * %zd bytes each",
98d66e
+                     num_blocks, block_name, sizeof_block);
98d66e
+       PyOS_snprintf(buf2, sizeof(buf2),
98d66e
+                     "%48s ", buf1);
98d66e
+      (void)printone(out, buf2, num_blocks * sizeof_block);
98d66e
+}
98d66e
+
98d66e
+
98d66e
+/* Print summary info to "out" about the state of pymalloc's structures.
98d66e
  * In Py_DEBUG mode, also perform some expensive internal consistency
98d66e
  * checks.
98d66e
  */
98d66e
 void
98d66e
-_PyObject_DebugMallocStats(void)
98d66e
+_PyObject_DebugMallocStats(FILE *out)
98d66e
 {
98d66e
     uint i;
98d66e
     const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
98d66e
@@ -1866,7 +1879,7 @@ _PyObject_DebugMallocStats(void)
98d66e
     size_t total;
98d66e
     char buf[128];
98d66e
 
98d66e
-    fprintf(stderr, "Small block threshold = %d, in %u size classes.\n",
98d66e
+    fprintf(out, "Small block threshold = %d, in %u size classes.\n",
98d66e
             SMALL_REQUEST_THRESHOLD, numclasses);
98d66e
 
98d66e
     for (i = 0; i < numclasses; ++i)
98d66e
@@ -1920,10 +1933,10 @@ _PyObject_DebugMallocStats(void)
98d66e
     }
98d66e
     assert(narenas == narenas_currently_allocated);
98d66e
 
98d66e
-    fputc('\n', stderr);
98d66e
+    fputc('\n', out);
98d66e
     fputs("class   size   num pools   blocks in use  avail blocks\n"
98d66e
           "-----   ----   ---------   -------------  ------------\n",
98d66e
-          stderr);
98d66e
+          out);
98d66e
 
98d66e
     for (i = 0; i < numclasses; ++i) {
98d66e
         size_t p = numpools[i];
98d66e
@@ -1934,7 +1947,7 @@ _PyObject_DebugMallocStats(void)
98d66e
             assert(b == 0 && f == 0);
98d66e
             continue;
98d66e
         }
98d66e
-        fprintf(stderr, "%5u %6u "
98d66e
+        fprintf(out, "%5u %6u "
98d66e
                         "%11" PY_FORMAT_SIZE_T "u "
98d66e
                         "%15" PY_FORMAT_SIZE_T "u "
98d66e
                         "%13" PY_FORMAT_SIZE_T "u\n",
98d66e
@@ -1944,36 +1957,35 @@ _PyObject_DebugMallocStats(void)
98d66e
         pool_header_bytes += p * POOL_OVERHEAD;
98d66e
         quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
98d66e
     }
98d66e
-    fputc('\n', stderr);
98d66e
-    (void)printone("# times object malloc called", serialno);
98d66e
-
98d66e
-    (void)printone("# arenas allocated total", ntimes_arena_allocated);
98d66e
-    (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas);
98d66e
-    (void)printone("# arenas highwater mark", narenas_highwater);
98d66e
-    (void)printone("# arenas allocated current", narenas);
98d66e
+    fputc('\n', out);
98d66e
+#ifdef PYMALLOC_DEBUG
98d66e
+    (void)printone(out, "# times object malloc called", serialno);
98d66e
+#endif
98d66e
+    (void)printone(out, "# arenas allocated total", ntimes_arena_allocated);
98d66e
+    (void)printone(out, "# arenas reclaimed", ntimes_arena_allocated - narenas);
98d66e
+    (void)printone(out, "# arenas highwater mark", narenas_highwater);
98d66e
+    (void)printone(out, "# arenas allocated current", narenas);
98d66e
 
98d66e
     PyOS_snprintf(buf, sizeof(buf),
98d66e
         "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena",
98d66e
         narenas, ARENA_SIZE);
98d66e
-    (void)printone(buf, narenas * ARENA_SIZE);
98d66e
+    (void)printone(out, buf, narenas * ARENA_SIZE);
98d66e
 
98d66e
-    fputc('\n', stderr);
98d66e
+    fputc('\n', out);
98d66e
 
98d66e
-    total = printone("# bytes in allocated blocks", allocated_bytes);
98d66e
-    total += printone("# bytes in available blocks", available_bytes);
98d66e
+    total = printone(out, "# bytes in allocated blocks", allocated_bytes);
98d66e
+    total += printone(out, "# bytes in available blocks", available_bytes);
98d66e
 
98d66e
     PyOS_snprintf(buf, sizeof(buf),
98d66e
         "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
98d66e
-    total += printone(buf, (size_t)numfreepools * POOL_SIZE);
98d66e
+    total += printone(out, buf, (size_t)numfreepools * POOL_SIZE);
98d66e
 
98d66e
-    total += printone("# bytes lost to pool headers", pool_header_bytes);
98d66e
-    total += printone("# bytes lost to quantization", quantization);
98d66e
-    total += printone("# bytes lost to arena alignment", arena_alignment);
98d66e
-    (void)printone("Total", total);
98d66e
+    total += printone(out, "# bytes lost to pool headers", pool_header_bytes);
98d66e
+    total += printone(out, "# bytes lost to quantization", quantization);
98d66e
+    total += printone(out, "# bytes lost to arena alignment", arena_alignment);
98d66e
+    (void)printone(out, "Total", total);
98d66e
 }
98d66e
 
98d66e
-#endif  /* PYMALLOC_DEBUG */
98d66e
-
98d66e
 #ifdef Py_USING_MEMORY_DEBUGGER
98d66e
 /* Make this function last so gcc won't inline it since the definition is
98d66e
  * after the reference.
98d66e
diff --git a/Objects/setobject.c b/Objects/setobject.c
98d66e
index 31da3db..da086ab 100644
98d66e
--- a/Objects/setobject.c
98d66e
+++ b/Objects/setobject.c
98d66e
@@ -1087,6 +1087,16 @@ PySet_Fini(void)
98d66e
     Py_CLEAR(emptyfrozenset);
98d66e
 }
98d66e
 
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PySet_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    _PyDebugAllocatorStats(out,
98d66e
+                           "free PySetObject",
98d66e
+                           numfree, sizeof(PySetObject));
98d66e
+}
98d66e
+
98d66e
+
98d66e
 static PyObject *
98d66e
 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
98d66e
 {
98d66e
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
98d66e
index c47d32f..b1ffa24 100644
98d66e
--- a/Objects/stringobject.c
98d66e
+++ b/Objects/stringobject.c
98d66e
@@ -4880,3 +4880,43 @@ void _Py_ReleaseInternedStrings(void)
98d66e
     PyDict_Clear(interned);
98d66e
     Py_CLEAR(interned);
98d66e
 }
98d66e
+
98d66e
+void _PyString_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    ssize_t i;
98d66e
+    int num_immortal = 0, num_mortal = 0;
98d66e
+    ssize_t immortal_size = 0, mortal_size = 0;
98d66e
+
98d66e
+    if (interned == NULL || !PyDict_Check(interned))
98d66e
+        return;
98d66e
+
98d66e
+    for (i = 0; i <= ((PyDictObject*)interned)->ma_mask; i++) {
98d66e
+        PyDictEntry *ep = ((PyDictObject*)interned)->ma_table + i;
98d66e
+        PyObject *pvalue = ep->me_value;
98d66e
+        if (pvalue != NULL) {
98d66e
+            PyStringObject *s = (PyStringObject *)ep->me_key;
98d66e
+
98d66e
+            switch (s->ob_sstate) {
98d66e
+            case SSTATE_NOT_INTERNED:
98d66e
+                /* XXX Shouldn't happen */
98d66e
+                break;
98d66e
+            case SSTATE_INTERNED_IMMORTAL:
98d66e
+                num_immortal ++;
98d66e
+                immortal_size += s->ob_size;
98d66e
+                break;
98d66e
+            case SSTATE_INTERNED_MORTAL:
98d66e
+                num_mortal ++;
98d66e
+                mortal_size += s->ob_size;
98d66e
+                break;
98d66e
+            default:
98d66e
+                Py_FatalError("Inconsistent interned string state.");
98d66e
+            }
98d66e
+        }
98d66e
+    }
98d66e
+
98d66e
+    fprintf(out, "%d mortal interned strings\n", num_mortal);
98d66e
+    fprintf(out, "%d immortal interned strings\n", num_immortal);
98d66e
+    fprintf(out, "total size of all interned strings: "
98d66e
+            "%zi/%zi "
98d66e
+            "mortal/immortal\n", mortal_size, immortal_size);
98d66e
+}
98d66e
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
98d66e
index 6f4b18c..e8e4490 100644
98d66e
--- a/Objects/tupleobject.c
98d66e
+++ b/Objects/tupleobject.c
98d66e
@@ -44,6 +44,22 @@ show_track(void)
98d66e
 }
98d66e
 #endif
98d66e
 
98d66e
+/* Print summary info about the state of the optimized allocator */
98d66e
+void
98d66e
+_PyTuple_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+#if PyTuple_MAXSAVESIZE > 0
98d66e
+    int i;
98d66e
+    char buf[128];
98d66e
+    for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
98d66e
+        PyOS_snprintf(buf, sizeof(buf),
98d66e
+                      "free %d-sized PyTupleObject", i);
98d66e
+        _PyDebugAllocatorStats(out,
98d66e
+                               buf,
98d66e
+                               numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
98d66e
+    }
98d66e
+#endif
98d66e
+}
98d66e
 
98d66e
 PyObject *
98d66e
 PyTuple_New(register Py_ssize_t size)
98d66e
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
98d66e
index a859fa0..b6ff83e 100644
98d66e
--- a/Objects/unicodeobject.c
98d66e
+++ b/Objects/unicodeobject.c
98d66e
@@ -9018,6 +9018,12 @@ _PyUnicode_Fini(void)
98d66e
     (void)PyUnicode_ClearFreeList();
98d66e
 }
98d66e
 
98d66e
+void _PyUnicode_DebugMallocStats(FILE *out)
98d66e
+{
98d66e
+    _PyDebugAllocatorStats(out, "free PyUnicodeObject", numfree,
98d66e
+                           sizeof(PyUnicodeObject));
98d66e
+}
98d66e
+
98d66e
 #ifdef __cplusplus
98d66e
 }
98d66e
 #endif
98d66e
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
98d66e
index b686317..c3b3e17 100644
98d66e
--- a/Python/pythonrun.c
98d66e
+++ b/Python/pythonrun.c
98d66e
@@ -605,7 +605,7 @@ Py_Finalize(void)
98d66e
 #endif /* Py_TRACE_REFS */
98d66e
 #ifdef PYMALLOC_DEBUG
98d66e
     if (Py_GETENV("PYTHONMALLOCSTATS"))
98d66e
-        _PyObject_DebugMallocStats();
98d66e
+        _PyObject_DebugMallocStats(stderr);
98d66e
 #endif
98d66e
 
98d66e
     call_ll_exitfuncs();
98d66e
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
98d66e
index 22238ba..60624f2 100644
98d66e
--- a/Python/sysmodule.c
98d66e
+++ b/Python/sysmodule.c
98d66e
@@ -890,6 +890,57 @@ a 11-tuple where the entries in the tuple are counts of:\n\
98d66e
 extern "C" {
98d66e
 #endif
98d66e
 
98d66e
+static PyObject *
98d66e
+sys_debugmallocstats(PyObject *self, PyObject *args)
98d66e
+{
98d66e
+    PyObject *file = NULL;
98d66e
+    FILE *fp;
98d66e
+
98d66e
+    if (!PyArg_ParseTuple(args, "|O!",
98d66e
+                          &PyFile_Type, &file)) {
98d66e
+      return NULL;
98d66e
+    }
98d66e
+    if (!file) {
98d66e
+        /* Default to sys.stderr: */
98d66e
+      file = PySys_GetObject("stderr");
98d66e
+      if (!file) {
98d66e
+          PyErr_SetString(PyExc_ValueError, "sys.stderr not set");
98d66e
+          return NULL;
98d66e
+      }
98d66e
+      if (!PyFile_Check(file)) {
98d66e
+          PyErr_SetString(PyExc_TypeError, "sys.stderr is not a file");
98d66e
+          return NULL;
98d66e
+      }
98d66e
+    }
98d66e
+
98d66e
+    Py_INCREF(file);
98d66e
+    /* OK, we now own a ref on non-NULL "file" */
98d66e
+
98d66e
+    fp = PyFile_AsFile(file);
98d66e
+    if (!fp) {
98d66e
+        PyErr_SetString(PyExc_ValueError, "file is closed");
98d66e
+        Py_DECREF(file);
98d66e
+        return NULL;
98d66e
+    }
98d66e
+
98d66e
+    _PyObject_DebugMallocStats(fp);
98d66e
+    fputc('\n', fp);
98d66e
+    _PyObject_DebugTypeStats(fp);
98d66e
+
98d66e
+    Py_DECREF(file);
98d66e
+
98d66e
+    Py_RETURN_NONE;
98d66e
+}
98d66e
+PyDoc_STRVAR(debugmallocstats_doc,
98d66e
+"_debugmallocstats([file])\n\
98d66e
+\n\
98d66e
+Print summary info to the given file (or sys.stderr) about the state of\n\
98d66e
+pymalloc's structures.\n\
98d66e
+\n\
98d66e
+In Py_DEBUG mode, also perform some expensive internal consistency\n\
98d66e
+checks.\n\
98d66e
+");
98d66e
+
98d66e
 #ifdef Py_TRACE_REFS
98d66e
 /* Defined in objects.c because it uses static globals if that file */
98d66e
 extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
98d66e
@@ -988,6 +1039,8 @@ static PyMethodDef sys_methods[] = {
98d66e
     {"settrace",        sys_settrace, METH_O, settrace_doc},
98d66e
     {"gettrace",        sys_gettrace, METH_NOARGS, gettrace_doc},
98d66e
     {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
98d66e
+    {"_debugmallocstats", sys_debugmallocstats, METH_VARARGS,
98d66e
+     debugmallocstats_doc},
98d66e
     {NULL,              NULL}           /* sentinel */
98d66e
 };
98d66e