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

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