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