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

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