cpython: 60655e543d8a (original) (raw)

Mercurial > cpython

changeset 100654:60655e543d8a

Add C functions _PyTraceMalloc_Track() Issue #26530: * Add C functions _PyTraceMalloc_Track() and _PyTraceMalloc_Untrack() to track memory blocks using the tracemalloc module. * Add _PyTraceMalloc_GetTraceback() to get the traceback of an object. [#26530]

Victor Stinner victor.stinner@gmail.com
date Tue, 22 Mar 2016 13:39:05 +0100
parents b86cdebe0e97
children 0da4532a8933
files Include/pymem.h Lib/test/test_tracemalloc.py Misc/NEWS Modules/_testcapimodule.c Modules/_tracemalloc.c
diffstat 5 files changed, 300 insertions(+), 11 deletions(-)[+] [-] Include/pymem.h 34 Lib/test/test_tracemalloc.py 115 Misc/NEWS 5 Modules/_testcapimodule.c 75 Modules/_tracemalloc.c 82

line wrap: on

line diff

--- a/Include/pymem.h +++ b/Include/pymem.h @@ -25,6 +25,40 @@ PyAPI_FUNC(int) _PyMem_SetupAllocators(c PyAPI_FUNC(int) _PyMem_PymallocEnabled(void); #endif +/* Identifier of an address space (domain) in tracemalloc / +typedef unsigned int _PyTraceMalloc_domain_t; + +/ Track an allocated memory block in the tracemalloc module.

+ +/* Untrack an allocated memory block in the tracemalloc module.

+ +/* Get the traceback where a memory block was allocated. +

#endif /* !Py_LIMITED_API */

--- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -11,9 +11,15 @@ try: import threading except ImportError: threading = None +try:

+except ImportError:

+ EMPTY_STRING_SIZE = sys.getsizeof(b'') + def get_frames(nframe, lineno_delta): frames = [] frame = sys._getframe(1) @@ -866,12 +872,121 @@ class TestCommandLine(unittest.TestCase) assert_python_ok('-X', 'tracemalloc', '-c', code) +@unittest.skipIf(_testcapi is None, 'need _testcapi') +class TestCAPI(unittest.TestCase):

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+

+ + def test_main(): support.run_unittest( TestTracemallocEnabled, TestSnapshot, TestFilters, TestCommandLine,

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -232,6 +232,11 @@ Core and Builtins Library ------- +- Issue #26530: Add C functions :c:func:_PyTraceMalloc_Track and

--- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3675,6 +3675,78 @@ pyobject_malloc_without_gil(PyObject *se Py_RETURN_NONE; } +static PyObject * +tracemalloc_track(PyObject *self, PyObject *args) +{

+

+

+

+

+} + +static PyObject * +tracemalloc_untrack(PyObject *self, PyObject *args) +{

+

+

+

+} + +static PyObject * +tracemalloc_get_traceback(PyObject *self, PyObject *args) +{

+

+

+} + static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, @@ -3861,6 +3933,9 @@ static PyMethodDef TestMethods[] = { {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},

--- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -61,8 +61,6 @@ static PyThread_type_lock tables_lock; #define DEFAULT_DOMAIN 0 -typedef unsigned int domain_t; - /* Pack the frame_t structure to reduce the memory footprint. */ typedef struct #ifdef GNUC @@ -70,7 +68,7 @@ typedef struct #endif { Py_uintptr_t ptr;

} pointer_t; /* Pack the frame_t structure to reduce the memory footprint on 64-bit @@ -519,11 +517,13 @@ traceback_new(void) static void -tracemalloc_remove_trace(domain_t domain, Py_uintptr_t ptr) +tracemalloc_remove_trace(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr) { trace_t trace; int removed;

+ if (tracemalloc_config.use_domain) { pointer_t key = {ptr, domain}; removed = _Py_HASHTABLE_POP(tracemalloc_traces, key, trace); @@ -544,12 +544,15 @@ tracemalloc_remove_trace(domain_t domain static int -tracemalloc_add_trace(domain_t domain, Py_uintptr_t ptr, size_t size) +tracemalloc_add_trace(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr,

{ traceback_t *traceback; trace_t trace; int res;

+ /* first, remove the previous trace (if any) */ tracemalloc_remove_trace(domain, ptr); @@ -1183,7 +1186,7 @@ traceback_to_pyobject(traceback_t trace static PyObject -trace_to_pyobject(domain_t domain, trace_t *trace, +trace_to_pyobject(_PyTraceMalloc_domain_t domain, trace_t *trace, _Py_hashtable_t *intern_tracebacks) { PyObject *trace_obj = NULL; @@ -1229,7 +1232,7 @@ tracemalloc_get_traces_fill(_Py_hashtabl void *user_data) { get_traces_t *get_traces = user_data;

@@ -1415,7 +1418,7 @@ void traceback_t *traceback; int i;

@@ -1686,3 +1689,60 @@ void #endif tracemalloc_deinit(); } + +int +_PyTraceMalloc_Track(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr,

+{

+#ifdef WITH_THREAD

+#endif +

+ +#ifdef WITH_THREAD

+#endif +

+ +#ifdef WITH_THREAD

+#endif

+} + + +int +_PyTraceMalloc_Untrack(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr) +{

+

+

+} + + +PyObject* +_PyTraceMalloc_GetTraceback(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr) +{

+

+

+}