Issue #26588: remove debug traces from _tracemalloc.

This commit is contained in:
Victor Stinner 2016-03-22 23:54:42 +01:00
parent 92c21d7a7c
commit f9a71153e9
5 changed files with 5 additions and 91 deletions

View File

@ -59,8 +59,6 @@ PyAPI_FUNC(int) _PyTraceMalloc_Untrack(
PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback( PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
_PyTraceMalloc_domain_t domain, _PyTraceMalloc_domain_t domain,
Py_uintptr_t ptr); Py_uintptr_t ptr);
PyAPI_DATA(int) tracemalloc_debug;
#endif /* !Py_LIMITED_API */ #endif /* !Py_LIMITED_API */

View File

@ -88,9 +88,6 @@ def traceback_filename(filename):
class TestTracemallocEnabled(unittest.TestCase): class TestTracemallocEnabled(unittest.TestCase):
def setUp(self): def setUp(self):
if _testcapi:
_testcapi.tracemalloc_set_debug(True)
if tracemalloc.is_tracing(): if tracemalloc.is_tracing():
self.skipTest("tracemalloc must be stopped before the test") self.skipTest("tracemalloc must be stopped before the test")
@ -98,8 +95,6 @@ class TestTracemallocEnabled(unittest.TestCase):
def tearDown(self): def tearDown(self):
tracemalloc.stop() tracemalloc.stop()
if _testcapi:
_testcapi.tracemalloc_set_debug(False)
def test_get_tracemalloc_memory(self): def test_get_tracemalloc_memory(self):
data = [allocate_bytes(123) for count in range(1000)] data = [allocate_bytes(123) for count in range(1000)]
@ -882,9 +877,6 @@ class TestCAPI(unittest.TestCase):
maxDiff = 80 * 20 maxDiff = 80 * 20
def setUp(self): def setUp(self):
if _testcapi:
_testcapi.tracemalloc_set_debug(True)
if tracemalloc.is_tracing(): if tracemalloc.is_tracing():
self.skipTest("tracemalloc must be stopped before the test") self.skipTest("tracemalloc must be stopped before the test")
@ -898,8 +890,6 @@ class TestCAPI(unittest.TestCase):
def tearDown(self): def tearDown(self):
tracemalloc.stop() tracemalloc.stop()
if _testcapi:
_testcapi.tracemalloc_set_debug(False)
def get_traceback(self): def get_traceback(self):
frames = _testcapi.tracemalloc_get_traceback(self.domain, self.ptr) frames = _testcapi.tracemalloc_get_traceback(self.domain, self.ptr)

View File

@ -772,7 +772,6 @@ class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase): class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
module = py_warnings module = py_warnings
@unittest.skipIf(True, "FIXME: Issue #26588")
def test_tracemalloc(self): def test_tracemalloc(self):
self.addCleanup(support.unlink, support.TESTFN) self.addCleanup(support.unlink, support.TESTFN)

View File

@ -3747,19 +3747,6 @@ tracemalloc_get_traceback(PyObject *self, PyObject *args)
return _PyTraceMalloc_GetTraceback(domain, (Py_uintptr_t)ptr); return _PyTraceMalloc_GetTraceback(domain, (Py_uintptr_t)ptr);
} }
PyObject*
tracemalloc_set_debug(PyObject *self, PyObject *args)
{
int debug;
extern int tracemalloc_debug;
if (!PyArg_ParseTuple(args, "i", &debug))
return NULL;
tracemalloc_debug = debug;
Py_RETURN_NONE;
}
static PyMethodDef TestMethods[] = { static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS}, {"raise_exception", raise_exception, METH_VARARGS},
@ -3949,7 +3936,6 @@ static PyMethodDef TestMethods[] = {
{"tracemalloc_track", tracemalloc_track, METH_VARARGS}, {"tracemalloc_track", tracemalloc_track, METH_VARARGS},
{"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
{"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
{"tracemalloc_set_debug", tracemalloc_set_debug, METH_VARARGS},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };

View File

@ -45,8 +45,6 @@ static struct {
int use_domain; int use_domain;
} tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1, 1}; } tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1, 1};
int tracemalloc_debug = 0;
#if defined(TRACE_RAW_MALLOC) && defined(WITH_THREAD) #if defined(TRACE_RAW_MALLOC) && defined(WITH_THREAD)
/* This lock is needed because tracemalloc_free() is called without /* This lock is needed because tracemalloc_free() is called without
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
@ -898,35 +896,23 @@ tracemalloc_clear_traces(void)
_Py_hashtable_clear(tracemalloc_filenames); _Py_hashtable_clear(tracemalloc_filenames);
} }
#define DEBUG(MSG) \
if (tracemalloc_debug) { fprintf(stderr, "[pid %li, tid %li, reentrant key %i] " MSG "\n", (long)getpid(), PyThread_get_thread_ident(), tracemalloc_reentrant_key); fflush(stderr); }
static int static int
tracemalloc_init(void) tracemalloc_init(void)
{ {
DEBUG("tracemalloc_init()");
#ifdef WITH_THREAD
assert(PyGILState_Check());
#endif
if (tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) { if (tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) {
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"the tracemalloc module has been unloaded"); "the tracemalloc module has been unloaded");
return -1; return -1;
} }
if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED) { if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED)
DEBUG("tracemalloc_init(): exit (already initialized)");
return 0; return 0;
}
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
#ifdef REENTRANT_THREADLOCAL #ifdef REENTRANT_THREADLOCAL
tracemalloc_reentrant_key = PyThread_create_key(); tracemalloc_reentrant_key = PyThread_create_key();
fprintf(stderr, "[pid %li, tid %li] PyThread_create_key() -> %i\n", (long)getpid(), PyThread_get_thread_ident(), tracemalloc_reentrant_key); fflush(stderr);
if (tracemalloc_reentrant_key == -1) { if (tracemalloc_reentrant_key == -1) {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
PyErr_SetFromWindowsErr(0); PyErr_SetFromWindowsErr(0);
@ -988,12 +974,9 @@ fprintf(stderr, "[pid %li, tid %li] PyThread_create_key() -> %i\n", (long)getpid
/* Disable tracing allocations until hooks are installed. Set /* Disable tracing allocations until hooks are installed. Set
also the reentrant flag to detect bugs: fail with an assertion error also the reentrant flag to detect bugs: fail with an assertion error
if set_reentrant(1) is called while tracing is disabled. */ if set_reentrant(1) is called while tracing is disabled. */
DEBUG("tracemalloc_init(): set_reentrant(1)");
set_reentrant(1); set_reentrant(1);
tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED; tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED;
DEBUG("tracemalloc_init(): done");
assert(get_reentrant());
return 0; return 0;
} }
@ -1001,11 +984,8 @@ assert(get_reentrant());
static void static void
tracemalloc_deinit(void) tracemalloc_deinit(void)
{ {
DEBUG("tracemalloc_deinit()"); if (tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED)
if (tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED) {
DEBUG("tracemalloc_deinit(): exit (not initialized)");
return; return;
}
tracemalloc_config.initialized = TRACEMALLOC_FINALIZED; tracemalloc_config.initialized = TRACEMALLOC_FINALIZED;
tracemalloc_stop(); tracemalloc_stop();
@ -1022,15 +1002,12 @@ DEBUG("tracemalloc_deinit(): exit (not initialized)");
} }
#endif #endif
DEBUG("tracemalloc_deinit(): delete reentrant key");
#ifdef REENTRANT_THREADLOCAL #ifdef REENTRANT_THREADLOCAL
fprintf(stderr, "[pid %li, tid %li] PyThread_delete_key(%i)\n", (long)getpid(), PyThread_get_thread_ident(), tracemalloc_reentrant_key); fflush(stderr);
PyThread_delete_key(tracemalloc_reentrant_key); PyThread_delete_key(tracemalloc_reentrant_key);
tracemalloc_reentrant_key = -1; tracemalloc_reentrant_key = -1;
#endif #endif
Py_XDECREF(unknown_filename); Py_XDECREF(unknown_filename);
DEBUG("tracemalloc_deinit(): done");
} }
@ -1040,24 +1017,13 @@ tracemalloc_start(int max_nframe)
PyMemAllocatorEx alloc; PyMemAllocatorEx alloc;
size_t size; size_t size;
DEBUG("tracemalloc_start()"); if (tracemalloc_init() < 0)
#ifdef WITH_THREAD
assert(PyGILState_Check());
#endif
if (tracemalloc_init() < 0) {
DEBUG("tracemalloc_start(): ERROR! init failed!");
return -1; return -1;
}
if (tracemalloc_config.tracing) { if (tracemalloc_config.tracing) {
/* hook already installed: do nothing */ /* hook already installed: do nothing */
DEBUG("tracemalloc_start(): exit (already tracing)");
assert(!get_reentrant());
return 0; return 0;
} }
assert(get_reentrant());
assert(1 <= max_nframe && max_nframe <= MAX_NFRAME); assert(1 <= max_nframe && max_nframe <= MAX_NFRAME);
tracemalloc_config.max_nframe = max_nframe; tracemalloc_config.max_nframe = max_nframe;
@ -1097,12 +1063,8 @@ assert(get_reentrant());
/* everything is ready: start tracing Python memory allocations */ /* everything is ready: start tracing Python memory allocations */
tracemalloc_config.tracing = 1; tracemalloc_config.tracing = 1;
DEBUG("tracemalloc_start(): set_reentrant(0)");
set_reentrant(0); set_reentrant(0);
DEBUG("tracemalloc_start(): done");
assert(!get_reentrant());
return 0; return 0;
} }
@ -1110,25 +1072,14 @@ assert(!get_reentrant());
static void static void
tracemalloc_stop(void) tracemalloc_stop(void)
{ {
DEBUG("tracemalloc_stop()"); if (!tracemalloc_config.tracing)
#ifdef WITH_THREAD
assert(PyGILState_Check());
#endif
if (!tracemalloc_config.tracing) {
DEBUG("tracemalloc_stop(): exit (not tracing)");
assert(get_reentrant());
return; return;
}
assert(!get_reentrant());
/* stop tracing Python memory allocations */ /* stop tracing Python memory allocations */
tracemalloc_config.tracing = 0; tracemalloc_config.tracing = 0;
/* set the reentrant flag to detect bugs: fail with an assertion error if /* set the reentrant flag to detect bugs: fail with an assertion error if
set_reentrant(1) is called while tracing is disabled. */ set_reentrant(1) is called while tracing is disabled. */
DEBUG("tracemalloc_stop(): set_reentrant(1)");
set_reentrant(1); set_reentrant(1);
/* unregister the hook on memory allocators */ /* unregister the hook on memory allocators */
@ -1143,8 +1094,6 @@ DEBUG("tracemalloc_stop(): set_reentrant(1)");
/* release memory */ /* release memory */
raw_free(tracemalloc_traceback); raw_free(tracemalloc_traceback);
tracemalloc_traceback = NULL; tracemalloc_traceback = NULL;
DEBUG("tracemalloc_stop(): done");
assert(get_reentrant());
} }
PyDoc_STRVAR(tracemalloc_is_tracing_doc, PyDoc_STRVAR(tracemalloc_is_tracing_doc,
@ -1512,11 +1461,8 @@ py_tracemalloc_start(PyObject *self, PyObject *args)
} }
nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int); nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
if (tracemalloc_start(nframe_int) < 0) { if (tracemalloc_start(nframe_int) < 0)
DEBUG("start(): ERROR!");
return NULL; return NULL;
}
DEBUG("start(): done");
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -1642,17 +1588,12 @@ PyMODINIT_FUNC
PyInit__tracemalloc(void) PyInit__tracemalloc(void)
{ {
PyObject *m; PyObject *m;
fprintf(stderr, "[pid %li, tid %li] PyInit__tracemalloc\n", (long)getpid(), PyThread_get_thread_ident()); fflush(stderr);
m = PyModule_Create(&module_def); m = PyModule_Create(&module_def);
if (m == NULL) if (m == NULL)
return NULL; return NULL;
#if 0
if (tracemalloc_init() < 0) if (tracemalloc_init() < 0)
return NULL; return NULL;
#endif
return m; return m;
} }