Skip to content

Commit

Permalink
Issue python#20186: Converted the tracemalloc module to Argument Clinic.
Browse files Browse the repository at this point in the history
Based on patch by Georg Brandl.
  • Loading branch information
serhiy-storchaka committed Feb 4, 2017
1 parent 5106ad1 commit b451f91
Show file tree
Hide file tree
Showing 2 changed files with 315 additions and 102 deletions.
225 changes: 123 additions & 102 deletions Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include "pythread.h"
#include "osdefs.h"

#include "clinic/_tracemalloc.c.h"
/*[clinic input]
module _tracemalloc
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/

/* Trace memory blocks allocated by PyMem_RawMalloc() */
#define TRACE_RAW_MALLOC

Expand Down Expand Up @@ -1161,27 +1167,31 @@ tracemalloc_stop(void)
tracemalloc_traceback = NULL;
}

PyDoc_STRVAR(tracemalloc_is_tracing_doc,
"is_tracing()->bool\n"
"\n"
"True if the tracemalloc module is tracing Python memory allocations,\n"
"False otherwise.");


static PyObject*
py_tracemalloc_is_tracing(PyObject *self)
/*[clinic input]
_tracemalloc.is_tracing
Return True if the tracemalloc module is tracing Python memory allocations.
[clinic start generated code]*/

static PyObject *
_tracemalloc_is_tracing_impl(PyObject *module)
/*[clinic end generated code: output=2d763b42601cd3ef input=8beb4fb5446813be]*/
{
return PyBool_FromLong(tracemalloc_config.tracing);
}

PyDoc_STRVAR(tracemalloc_clear_traces_doc,
"clear_traces()\n"
"\n"
"Clear traces of memory blocks allocated by Python.");

/*[clinic input]
_tracemalloc.clear_traces
static PyObject*
py_tracemalloc_clear_traces(PyObject *self)
Clear traces of memory blocks allocated by Python.
[clinic start generated code]*/

static PyObject *
_tracemalloc_clear_traces_impl(PyObject *module)
/*[clinic end generated code: output=a86080ee41b84197 input=0dab5b6c785183a5]*/
{
if (!tracemalloc_config.tracing)
Py_RETURN_NONE;
Expand Down Expand Up @@ -1343,17 +1353,21 @@ tracemalloc_pyobject_decref_cb(_Py_hashtable_t *tracebacks,
}


PyDoc_STRVAR(tracemalloc_get_traces_doc,
"_get_traces() -> list\n"
"\n"
"Get traces of all memory blocks allocated by Python.\n"
"Return a list of (size: int, traceback: tuple) tuples.\n"
"traceback is a tuple of (filename: str, lineno: int) tuples.\n"
"\n"
"Return an empty list if the tracemalloc module is disabled.");

static PyObject*
py_tracemalloc_get_traces(PyObject *self, PyObject *obj)
/*[clinic input]
_tracemalloc._get_traces
Get traces of all memory blocks allocated by Python.
Return a list of (size: int, traceback: tuple) tuples.
traceback is a tuple of (filename: str, lineno: int) tuples.
Return an empty list if the tracemalloc module is disabled.
[clinic start generated code]*/

static PyObject *
_tracemalloc__get_traces_impl(PyObject *module)
/*[clinic end generated code: output=e9929876ced4b5cc input=6c7d2230b24255aa]*/
{
get_traces_t get_traces;
int err;
Expand Down Expand Up @@ -1439,17 +1453,23 @@ tracemalloc_get_traceback(_PyTraceMalloc_domain_t domain, uintptr_t ptr)
}


PyDoc_STRVAR(tracemalloc_get_object_traceback_doc,
"_get_object_traceback(obj)\n"
"\n"
"Get the traceback where the Python object obj was allocated.\n"
"Return a tuple of (filename: str, lineno: int) tuples.\n"
"\n"
"Return None if the tracemalloc module is disabled or did not\n"
"trace the allocation of the object.");

static PyObject*
py_tracemalloc_get_object_traceback(PyObject *self, PyObject *obj)
/*[clinic input]
_tracemalloc._get_object_traceback
obj: object
/
Get the traceback where the Python object obj was allocated.
Return a tuple of (filename: str, lineno: int) tuples.
Return None if the tracemalloc module is disabled or did not
trace the allocation of the object.
[clinic start generated code]*/

static PyObject *
_tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
{
PyTypeObject *type;
void *ptr;
Expand Down Expand Up @@ -1503,25 +1523,29 @@ _PyMem_DumpTraceback(int fd, const void *ptr)
#undef PUTS


PyDoc_STRVAR(tracemalloc_start_doc,
"start(nframe: int=1)\n"
"\n"
"Start tracing Python memory allocations. Set also the maximum number \n"
"of frames stored in the traceback of a trace to nframe.");

static PyObject*
py_tracemalloc_start(PyObject *self, PyObject *args)
/*[clinic input]
_tracemalloc.start
nframe: Py_ssize_t = 1
/
Start tracing Python memory allocations.
Also set the maximum number of frames stored in the traceback of a
trace to nframe.
[clinic start generated code]*/

static PyObject *
_tracemalloc_start_impl(PyObject *module, Py_ssize_t nframe)
/*[clinic end generated code: output=0f558d2079511553 input=997841629cc441cb]*/
{
Py_ssize_t nframe = 1;
int nframe_int;

if (!PyArg_ParseTuple(args, "|n:start", &nframe))
return NULL;

if (nframe < 1 || nframe > MAX_NFRAME) {
PyErr_Format(PyExc_ValueError,
"the number of frames must be in range [1; %i]",
MAX_NFRAME);
(int)MAX_NFRAME);
return NULL;
}
nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
Expand All @@ -1532,48 +1556,55 @@ py_tracemalloc_start(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}

PyDoc_STRVAR(tracemalloc_stop_doc,
"stop()\n"
"\n"
"Stop tracing Python memory allocations and clear traces\n"
"of memory blocks allocated by Python.");

/*[clinic input]
_tracemalloc.stop
static PyObject*
py_tracemalloc_stop(PyObject *self)
Stop tracing Python memory allocations.
Also clear traces of memory blocks allocated by Python.
[clinic start generated code]*/

static PyObject *
_tracemalloc_stop_impl(PyObject *module)
/*[clinic end generated code: output=c3c42ae03e3955cd input=7478f075e51dae18]*/
{
tracemalloc_stop();
Py_RETURN_NONE;
}


PyDoc_STRVAR(tracemalloc_get_traceback_limit_doc,
"get_traceback_limit() -> int\n"
"\n"
"Get the maximum number of frames stored in the traceback\n"
"of a trace.\n"
"\n"
"By default, a trace of an allocated memory block only stores\n"
"the most recent frame: the limit is 1.");
/*[clinic input]
_tracemalloc.get_traceback_limit
static PyObject*
py_tracemalloc_get_traceback_limit(PyObject *self)
Get the maximum number of frames stored in the traceback of a trace.
By default, a trace of an allocated memory block only stores
the most recent frame: the limit is 1.
[clinic start generated code]*/

static PyObject *
_tracemalloc_get_traceback_limit_impl(PyObject *module)
/*[clinic end generated code: output=d556d9306ba95567 input=da3cd977fc68ae3b]*/
{
return PyLong_FromLong(tracemalloc_config.max_nframe);
}


PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc,
"get_tracemalloc_memory() -> int\n"
"\n"
"Get the memory usage in bytes of the tracemalloc module\n"
"used internally to trace memory allocations.");

static PyObject*
tracemalloc_get_tracemalloc_memory(PyObject *self)
/*[clinic input]
_tracemalloc.get_tracemalloc_memory
Get the memory usage in bytes of the tracemalloc module.
This memory is used internally to trace memory allocations.
[clinic start generated code]*/

static PyObject *
_tracemalloc_get_tracemalloc_memory_impl(PyObject *module)
/*[clinic end generated code: output=e3f14e280a55f5aa input=5d919c0f4d5132ad]*/
{
size_t size;
PyObject *size_obj;

size = _Py_hashtable_size(tracemalloc_tracebacks);
size += _Py_hashtable_size(tracemalloc_filenames);
Expand All @@ -1582,22 +1613,24 @@ tracemalloc_get_tracemalloc_memory(PyObject *self)
size += _Py_hashtable_size(tracemalloc_traces);
TABLES_UNLOCK();

size_obj = PyLong_FromSize_t(size);
return Py_BuildValue("N", size_obj);
return PyLong_FromSize_t(size);
}


PyDoc_STRVAR(tracemalloc_get_traced_memory_doc,
"get_traced_memory() -> (int, int)\n"
"\n"
"Get the current size and peak size of memory blocks traced\n"
"by the tracemalloc module as a tuple: (current: int, peak: int).");

static PyObject*
tracemalloc_get_traced_memory(PyObject *self)
/*[clinic input]
_tracemalloc.get_traced_memory
Get the current size and peak size of memory blocks traced by tracemalloc.
Returns a tuple: (current: int, peak: int).
[clinic start generated code]*/

static PyObject *
_tracemalloc_get_traced_memory_impl(PyObject *module)
/*[clinic end generated code: output=5b167189adb9e782 input=61ddb5478400ff66]*/
{
Py_ssize_t size, peak_size;
PyObject *size_obj, *peak_size_obj;

if (!tracemalloc_config.tracing)
return Py_BuildValue("ii", 0, 0);
Expand All @@ -1607,32 +1640,20 @@ tracemalloc_get_traced_memory(PyObject *self)
peak_size = tracemalloc_peak_traced_memory;
TABLES_UNLOCK();

size_obj = PyLong_FromSize_t(size);
peak_size_obj = PyLong_FromSize_t(peak_size);
return Py_BuildValue("NN", size_obj, peak_size_obj);
return Py_BuildValue("nn", size, peak_size);
}


static PyMethodDef module_methods[] = {
{"is_tracing", (PyCFunction)py_tracemalloc_is_tracing,
METH_NOARGS, tracemalloc_is_tracing_doc},
{"clear_traces", (PyCFunction)py_tracemalloc_clear_traces,
METH_NOARGS, tracemalloc_clear_traces_doc},
{"_get_traces", (PyCFunction)py_tracemalloc_get_traces,
METH_NOARGS, tracemalloc_get_traces_doc},
{"_get_object_traceback", (PyCFunction)py_tracemalloc_get_object_traceback,
METH_O, tracemalloc_get_object_traceback_doc},
{"start", (PyCFunction)py_tracemalloc_start,
METH_VARARGS, tracemalloc_start_doc},
{"stop", (PyCFunction)py_tracemalloc_stop,
METH_NOARGS, tracemalloc_stop_doc},
{"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit,
METH_NOARGS, tracemalloc_get_traceback_limit_doc},
{"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory,
METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc},
{"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory,
METH_NOARGS, tracemalloc_get_traced_memory_doc},

_TRACEMALLOC_IS_TRACING_METHODDEF
_TRACEMALLOC_CLEAR_TRACES_METHODDEF
_TRACEMALLOC__GET_TRACES_METHODDEF
_TRACEMALLOC__GET_OBJECT_TRACEBACK_METHODDEF
_TRACEMALLOC_START_METHODDEF
_TRACEMALLOC_STOP_METHODDEF
_TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF
_TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF
_TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF
/* sentinel */
{NULL, NULL}
};
Expand Down
Loading

0 comments on commit b451f91

Please sign in to comment.