2013-11-23 07:27:24 -04:00
|
|
|
#include "Python.h"
|
2020-04-14 13:30:41 -03:00
|
|
|
#include "pycore_gc.h" // PyGC_Head
|
|
|
|
#include "pycore_pymem.h" // _Py_tracemalloc_config
|
2019-05-19 19:14:57 -03:00
|
|
|
#include "pycore_traceback.h"
|
2020-05-11 21:42:19 -03:00
|
|
|
#include "pycore_hashtable.h"
|
2020-04-28 22:28:46 -03:00
|
|
|
#include "frameobject.h" // PyFrame_GetBack()
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
#include "clinic/_tracemalloc.c.h"
|
|
|
|
/*[clinic input]
|
|
|
|
module _tracemalloc
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
/* Trace memory blocks allocated by PyMem_RawMalloc() */
|
|
|
|
#define TRACE_RAW_MALLOC
|
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
static void tracemalloc_stop(void);
|
|
|
|
static void* raw_malloc(size_t size);
|
|
|
|
static void raw_free(void *ptr);
|
|
|
|
|
|
|
|
#ifdef Py_DEBUG
|
|
|
|
# define TRACE_DEBUG
|
|
|
|
#endif
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
#define TO_PTR(key) ((const void *)(uintptr_t)(key))
|
|
|
|
#define FROM_PTR(key) ((uintptr_t)(key))
|
2020-05-12 21:26:02 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
/* Protected by the GIL */
|
|
|
|
static struct {
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx mem;
|
|
|
|
PyMemAllocatorEx raw;
|
|
|
|
PyMemAllocatorEx obj;
|
2013-11-23 07:27:24 -04:00
|
|
|
} allocators;
|
|
|
|
|
|
|
|
|
2017-09-07 13:56:24 -03:00
|
|
|
#if defined(TRACE_RAW_MALLOC)
|
2013-11-23 07:27:24 -04:00
|
|
|
/* This lock is needed because tracemalloc_free() is called without
|
|
|
|
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
|
2019-09-05 13:06:49 -03:00
|
|
|
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
|
2013-11-23 07:27:24 -04:00
|
|
|
static PyThread_type_lock tables_lock;
|
|
|
|
# define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
|
|
|
|
# define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
|
|
|
|
#else
|
|
|
|
/* variables are protected by the GIL */
|
|
|
|
# define TABLES_LOCK()
|
|
|
|
# define TABLES_UNLOCK()
|
|
|
|
#endif
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
|
|
|
#define DEFAULT_DOMAIN 0
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
/* Pack the frame_t structure to reduce the memory footprint on 64-bit
|
2016-03-22 08:58:23 -03:00
|
|
|
architectures: 12 bytes instead of 16. */
|
2013-11-23 07:27:24 -04:00
|
|
|
typedef struct
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__attribute__((packed))
|
2014-01-31 22:43:58 -04:00
|
|
|
#elif defined(_MSC_VER)
|
2017-07-25 05:47:43 -03:00
|
|
|
#pragma pack(push, 4)
|
2013-11-23 07:27:24 -04:00
|
|
|
#endif
|
|
|
|
{
|
2016-03-15 10:28:04 -03:00
|
|
|
/* filename cannot be NULL: "<unknown>" is used if the Python frame
|
|
|
|
filename is NULL */
|
2013-11-23 07:27:24 -04:00
|
|
|
PyObject *filename;
|
2016-03-15 17:57:02 -03:00
|
|
|
unsigned int lineno;
|
2013-11-23 07:27:24 -04:00
|
|
|
} frame_t;
|
2017-07-25 05:47:43 -03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(pop)
|
|
|
|
#endif
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
typedef struct {
|
|
|
|
Py_uhash_t hash;
|
2019-10-15 09:00:16 -03:00
|
|
|
/* Number of frames stored */
|
|
|
|
uint16_t nframe;
|
|
|
|
/* Total number of frames the traceback had */
|
|
|
|
uint16_t total_nframe;
|
2013-11-23 07:27:24 -04:00
|
|
|
frame_t frames[1];
|
|
|
|
} traceback_t;
|
|
|
|
|
|
|
|
#define TRACEBACK_SIZE(NFRAME) \
|
|
|
|
(sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1))
|
2013-11-27 17:27:13 -04:00
|
|
|
|
2019-10-15 09:00:16 -03:00
|
|
|
/* The maximum number of frames is either:
|
|
|
|
- The maximum number of frames we can store in `traceback_t.nframe`
|
|
|
|
- The maximum memory size_t we can allocate */
|
|
|
|
static const unsigned long MAX_NFRAME = Py_MIN(UINT16_MAX, ((SIZE_MAX - sizeof(traceback_t)) / sizeof(frame_t) + 1));
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static PyObject *unknown_filename = NULL;
|
|
|
|
static traceback_t tracemalloc_empty_traceback;
|
|
|
|
|
2013-11-25 20:06:02 -04:00
|
|
|
/* Trace of a memory block */
|
2013-11-23 07:27:24 -04:00
|
|
|
typedef struct {
|
2013-11-25 20:06:02 -04:00
|
|
|
/* Size of the memory block in bytes */
|
2013-11-23 07:27:24 -04:00
|
|
|
size_t size;
|
2013-11-25 20:06:02 -04:00
|
|
|
|
|
|
|
/* Traceback where the memory block was allocated */
|
2013-11-23 07:27:24 -04:00
|
|
|
traceback_t *traceback;
|
|
|
|
} trace_t;
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
/* Size in bytes of currently traced memory.
|
|
|
|
Protected by TABLES_LOCK(). */
|
|
|
|
static size_t tracemalloc_traced_memory = 0;
|
|
|
|
|
2013-11-27 16:39:49 -04:00
|
|
|
/* Peak size in bytes of traced memory.
|
2013-11-23 07:27:24 -04:00
|
|
|
Protected by TABLES_LOCK(). */
|
2013-11-27 16:39:49 -04:00
|
|
|
static size_t tracemalloc_peak_traced_memory = 0;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2014-12-01 12:28:43 -04:00
|
|
|
/* Hash table used as a set to intern filenames:
|
2013-11-23 07:27:24 -04:00
|
|
|
PyObject* => PyObject*.
|
|
|
|
Protected by the GIL */
|
|
|
|
static _Py_hashtable_t *tracemalloc_filenames = NULL;
|
|
|
|
|
2013-11-27 17:27:13 -04:00
|
|
|
/* Buffer to store a new traceback in traceback_new().
|
|
|
|
Protected by the GIL. */
|
|
|
|
static traceback_t *tracemalloc_traceback = NULL;
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
/* Hash table used as a set to intern tracebacks:
|
|
|
|
traceback_t* => traceback_t*
|
|
|
|
Protected by the GIL */
|
|
|
|
static _Py_hashtable_t *tracemalloc_tracebacks = NULL;
|
|
|
|
|
2020-05-12 22:52:11 -03:00
|
|
|
/* pointer (void*) => trace (trace_t*).
|
2013-11-23 07:27:24 -04:00
|
|
|
Protected by TABLES_LOCK(). */
|
|
|
|
static _Py_hashtable_t *tracemalloc_traces = NULL;
|
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
/* domain (unsigned int) => traces (_Py_hashtable_t).
|
|
|
|
Protected by TABLES_LOCK(). */
|
|
|
|
static _Py_hashtable_t *tracemalloc_domains = NULL;
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
#ifdef TRACE_DEBUG
|
|
|
|
static void
|
|
|
|
tracemalloc_error(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
fprintf(stderr, "tracemalloc: ");
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2017-09-07 13:56:24 -03:00
|
|
|
#if defined(TRACE_RAW_MALLOC)
|
2013-11-23 07:27:24 -04:00
|
|
|
#define REENTRANT_THREADLOCAL
|
|
|
|
|
2017-10-06 07:41:34 -03:00
|
|
|
static Py_tss_t tracemalloc_reentrant_key = Py_tss_NEEDS_INIT;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
/* Any non-NULL pointer can be used */
|
|
|
|
#define REENTRANT Py_True
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_reentrant(void)
|
|
|
|
{
|
2016-03-22 13:45:09 -03:00
|
|
|
void *ptr;
|
|
|
|
|
2017-10-06 07:41:34 -03:00
|
|
|
assert(PyThread_tss_is_created(&tracemalloc_reentrant_key));
|
|
|
|
ptr = PyThread_tss_get(&tracemalloc_reentrant_key);
|
2013-11-23 07:27:24 -04:00
|
|
|
if (ptr != NULL) {
|
|
|
|
assert(ptr == REENTRANT);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_reentrant(int reentrant)
|
|
|
|
{
|
2013-11-25 20:08:53 -04:00
|
|
|
assert(reentrant == 0 || reentrant == 1);
|
2017-10-06 07:41:34 -03:00
|
|
|
assert(PyThread_tss_is_created(&tracemalloc_reentrant_key));
|
2016-03-22 13:45:09 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
if (reentrant) {
|
2016-03-22 13:40:07 -03:00
|
|
|
assert(!get_reentrant());
|
2017-10-06 07:41:34 -03:00
|
|
|
PyThread_tss_set(&tracemalloc_reentrant_key, REENTRANT);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-03-22 13:40:07 -03:00
|
|
|
assert(get_reentrant());
|
2017-10-06 07:41:34 -03:00
|
|
|
PyThread_tss_set(&tracemalloc_reentrant_key, NULL);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-09-07 13:56:24 -03:00
|
|
|
/* TRACE_RAW_MALLOC not defined: variable protected by the GIL */
|
2013-11-23 07:27:24 -04:00
|
|
|
static int tracemalloc_reentrant = 0;
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_reentrant(void)
|
|
|
|
{
|
|
|
|
return tracemalloc_reentrant;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_reentrant(int reentrant)
|
|
|
|
{
|
2016-03-22 20:17:04 -03:00
|
|
|
assert(reentrant != tracemalloc_reentrant);
|
2013-11-23 07:27:24 -04:00
|
|
|
tracemalloc_reentrant = reentrant;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2016-03-21 18:00:58 -03:00
|
|
|
static Py_uhash_t
|
2020-05-12 21:26:02 -03:00
|
|
|
hashtable_hash_pyobject(const void *key)
|
2016-03-21 18:00:58 -03:00
|
|
|
{
|
2020-05-12 21:26:02 -03:00
|
|
|
PyObject *obj = (PyObject *)key;
|
2016-03-21 18:00:58 -03:00
|
|
|
return PyObject_Hash(obj);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static int
|
2020-05-12 21:26:02 -03:00
|
|
|
hashtable_compare_unicode(const void *key1, const void *key2)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2020-05-12 21:26:02 -03:00
|
|
|
PyObject *obj1 = (PyObject *)key1;
|
|
|
|
PyObject *obj2 = (PyObject *)key2;
|
|
|
|
if (obj1 != NULL && obj2 != NULL) {
|
|
|
|
return (PyUnicode_Compare(obj1, obj2) == 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return obj1 == obj2;
|
|
|
|
}
|
2016-03-22 08:58:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Py_uhash_t
|
2020-05-12 21:26:02 -03:00
|
|
|
hashtable_hash_uint(const void *key_raw)
|
2016-03-22 08:58:23 -03:00
|
|
|
{
|
2020-05-12 21:26:02 -03:00
|
|
|
unsigned int key = (unsigned int)FROM_PTR(key_raw);
|
2020-05-12 20:36:47 -03:00
|
|
|
return (Py_uhash_t)key;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static _Py_hashtable_t *
|
2020-05-12 23:40:30 -03:00
|
|
|
hashtable_new(_Py_hashtable_hash_func hash_func,
|
2020-05-12 21:50:18 -03:00
|
|
|
_Py_hashtable_compare_func compare_func,
|
2020-05-12 23:40:30 -03:00
|
|
|
_Py_hashtable_destroy_func key_destroy_func,
|
|
|
|
_Py_hashtable_destroy_func value_destroy_func)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2016-03-22 08:13:01 -03:00
|
|
|
_Py_hashtable_allocator_t hashtable_alloc = {malloc, free};
|
2020-05-12 23:40:30 -03:00
|
|
|
return _Py_hashtable_new_full(hash_func, compare_func,
|
|
|
|
key_destroy_func, value_destroy_func,
|
|
|
|
&hashtable_alloc);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void*
|
|
|
|
raw_malloc(size_t size)
|
|
|
|
{
|
|
|
|
return allocators.raw.malloc(allocators.raw.ctx, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
raw_free(void *ptr)
|
|
|
|
{
|
|
|
|
allocators.raw.free(allocators.raw.ctx, ptr);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static Py_uhash_t
|
2020-05-12 21:26:02 -03:00
|
|
|
hashtable_hash_traceback(const void *key)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2020-05-12 21:26:02 -03:00
|
|
|
const traceback_t *traceback = (const traceback_t *)key;
|
2013-11-23 07:27:24 -04:00
|
|
|
return traceback->hash;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static int
|
2020-05-12 21:26:02 -03:00
|
|
|
hashtable_compare_traceback(const void *key1, const void *key2)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2020-05-12 21:26:02 -03:00
|
|
|
const traceback_t *traceback1 = (const traceback_t *)key1;
|
|
|
|
const traceback_t *traceback2 = (const traceback_t *)key2;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-05-12 21:26:02 -03:00
|
|
|
if (traceback1->nframe != traceback2->nframe) {
|
2013-11-23 07:27:24 -04:00
|
|
|
return 0;
|
2020-05-12 21:26:02 -03:00
|
|
|
}
|
|
|
|
if (traceback1->total_nframe != traceback2->total_nframe) {
|
2019-10-15 09:00:16 -03:00
|
|
|
return 0;
|
2020-05-12 21:26:02 -03:00
|
|
|
}
|
2019-10-15 09:00:16 -03:00
|
|
|
|
2020-05-12 21:26:02 -03:00
|
|
|
for (int i=0; i < traceback1->nframe; i++) {
|
|
|
|
const frame_t *frame1 = &traceback1->frames[i];
|
|
|
|
const frame_t *frame2 = &traceback2->frames[i];
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-05-12 21:26:02 -03:00
|
|
|
if (frame1->lineno != frame2->lineno) {
|
2013-11-23 07:27:24 -04:00
|
|
|
return 0;
|
2020-05-12 21:26:02 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
if (frame1->filename != frame2->filename) {
|
|
|
|
assert(PyUnicode_Compare(frame1->filename, frame2->filename) != 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void
|
|
|
|
tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame)
|
|
|
|
{
|
|
|
|
frame->filename = unknown_filename;
|
2020-04-28 20:28:13 -03:00
|
|
|
int lineno = PyFrame_GetLineNumber(pyframe);
|
|
|
|
if (lineno < 0) {
|
2016-03-15 17:57:02 -03:00
|
|
|
lineno = 0;
|
2020-04-28 20:28:13 -03:00
|
|
|
}
|
2016-03-15 17:57:02 -03:00
|
|
|
frame->lineno = (unsigned int)lineno;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-04-28 20:28:13 -03:00
|
|
|
PyCodeObject *code = PyFrame_GetCode(pyframe);
|
|
|
|
PyObject *filename = code->co_filename;
|
|
|
|
Py_DECREF(code);
|
|
|
|
|
|
|
|
if (filename == NULL) {
|
2013-11-23 07:27:24 -04:00
|
|
|
#ifdef TRACE_DEBUG
|
|
|
|
tracemalloc_error("failed to get the filename of the code object");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(filename)) {
|
|
|
|
#ifdef TRACE_DEBUG
|
2016-04-14 23:14:19 -03:00
|
|
|
tracemalloc_error("filename is not a unicode string");
|
2013-11-23 07:27:24 -04:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!PyUnicode_IS_READY(filename)) {
|
|
|
|
/* Don't make a Unicode string ready to avoid reentrant calls
|
|
|
|
to tracemalloc_malloc() or tracemalloc_realloc() */
|
|
|
|
#ifdef TRACE_DEBUG
|
|
|
|
tracemalloc_error("filename is not a ready unicode string");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* intern the filename */
|
2020-04-28 20:28:13 -03:00
|
|
|
_Py_hashtable_entry_t *entry;
|
2020-05-12 23:40:30 -03:00
|
|
|
entry = _Py_hashtable_get_entry(tracemalloc_filenames, filename);
|
2013-11-23 07:27:24 -04:00
|
|
|
if (entry != NULL) {
|
2020-05-12 21:26:02 -03:00
|
|
|
filename = (PyObject *)entry->key;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* tracemalloc_filenames is responsible to keep a reference
|
|
|
|
to the filename */
|
|
|
|
Py_INCREF(filename);
|
2020-05-12 23:40:30 -03:00
|
|
|
if (_Py_hashtable_set(tracemalloc_filenames, filename, NULL) < 0) {
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_DECREF(filename);
|
|
|
|
#ifdef TRACE_DEBUG
|
|
|
|
tracemalloc_error("failed to intern the filename");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the tracemalloc_filenames table keeps a reference to the filename */
|
|
|
|
frame->filename = filename;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static Py_uhash_t
|
|
|
|
traceback_hash(traceback_t *traceback)
|
|
|
|
{
|
|
|
|
/* code based on tuplehash() of Objects/tupleobject.c */
|
2013-12-16 18:05:13 -04:00
|
|
|
Py_uhash_t x, y; /* Unsigned for defined overflow behavior. */
|
2013-11-23 07:27:24 -04:00
|
|
|
int len = traceback->nframe;
|
|
|
|
Py_uhash_t mult = _PyHASH_MULTIPLIER;
|
|
|
|
frame_t *frame;
|
|
|
|
|
|
|
|
x = 0x345678UL;
|
|
|
|
frame = traceback->frames;
|
|
|
|
while (--len >= 0) {
|
2013-12-16 18:05:13 -04:00
|
|
|
y = (Py_uhash_t)PyObject_Hash(frame->filename);
|
|
|
|
y ^= (Py_uhash_t)frame->lineno;
|
2013-11-23 07:27:24 -04:00
|
|
|
frame++;
|
|
|
|
|
|
|
|
x = (x ^ y) * mult;
|
|
|
|
/* the cast might truncate len; that doesn't change hash stability */
|
2013-12-16 18:05:13 -04:00
|
|
|
mult += (Py_uhash_t)(82520UL + len + len);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
2019-10-15 09:00:16 -03:00
|
|
|
x ^= traceback->total_nframe;
|
2013-11-23 07:27:24 -04:00
|
|
|
x += 97531UL;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void
|
|
|
|
traceback_get_frames(traceback_t *traceback)
|
|
|
|
{
|
2020-04-28 22:01:43 -03:00
|
|
|
PyThreadState *tstate = PyGILState_GetThisThreadState();
|
2013-11-23 07:27:24 -04:00
|
|
|
if (tstate == NULL) {
|
|
|
|
#ifdef TRACE_DEBUG
|
|
|
|
tracemalloc_error("failed to get the current thread state");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 22:01:43 -03:00
|
|
|
PyFrameObject *pyframe = PyThreadState_GetFrame(tstate);
|
2020-04-28 22:28:46 -03:00
|
|
|
for (; pyframe != NULL;) {
|
2019-10-15 09:00:16 -03:00
|
|
|
if (traceback->nframe < _Py_tracemalloc_config.max_nframe) {
|
|
|
|
tracemalloc_get_frame(pyframe, &traceback->frames[traceback->nframe]);
|
|
|
|
assert(traceback->frames[traceback->nframe].filename != NULL);
|
|
|
|
traceback->nframe++;
|
|
|
|
}
|
2020-04-28 22:28:46 -03:00
|
|
|
if (traceback->total_nframe < UINT16_MAX) {
|
2019-10-15 09:00:16 -03:00
|
|
|
traceback->total_nframe++;
|
2020-04-28 22:28:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyFrameObject *back = PyFrame_GetBack(pyframe);
|
|
|
|
Py_DECREF(pyframe);
|
|
|
|
pyframe = back;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static traceback_t *
|
|
|
|
traceback_new(void)
|
|
|
|
{
|
2013-11-27 17:27:13 -04:00
|
|
|
traceback_t *traceback;
|
2013-11-23 07:27:24 -04:00
|
|
|
_Py_hashtable_entry_t *entry;
|
|
|
|
|
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
|
|
|
/* get frames */
|
2013-11-27 17:27:13 -04:00
|
|
|
traceback = tracemalloc_traceback;
|
2013-11-23 07:27:24 -04:00
|
|
|
traceback->nframe = 0;
|
2019-10-15 09:00:16 -03:00
|
|
|
traceback->total_nframe = 0;
|
2013-11-23 07:27:24 -04:00
|
|
|
traceback_get_frames(traceback);
|
|
|
|
if (traceback->nframe == 0)
|
|
|
|
return &tracemalloc_empty_traceback;
|
|
|
|
traceback->hash = traceback_hash(traceback);
|
|
|
|
|
|
|
|
/* intern the traceback */
|
2020-05-12 23:40:30 -03:00
|
|
|
entry = _Py_hashtable_get_entry(tracemalloc_tracebacks, traceback);
|
2013-11-23 07:27:24 -04:00
|
|
|
if (entry != NULL) {
|
2020-05-12 21:26:02 -03:00
|
|
|
traceback = (traceback_t *)entry->key;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
traceback_t *copy;
|
|
|
|
size_t traceback_size;
|
|
|
|
|
|
|
|
traceback_size = TRACEBACK_SIZE(traceback->nframe);
|
|
|
|
|
|
|
|
copy = raw_malloc(traceback_size);
|
|
|
|
if (copy == NULL) {
|
|
|
|
#ifdef TRACE_DEBUG
|
|
|
|
tracemalloc_error("failed to intern the traceback: malloc failed");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(copy, traceback, traceback_size);
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
if (_Py_hashtable_set(tracemalloc_tracebacks, copy, NULL) < 0) {
|
2013-11-23 07:27:24 -04:00
|
|
|
raw_free(copy);
|
|
|
|
#ifdef TRACE_DEBUG
|
|
|
|
tracemalloc_error("failed to intern the traceback: putdata failed");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
traceback = copy;
|
|
|
|
}
|
|
|
|
return traceback;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
static _Py_hashtable_t*
|
|
|
|
tracemalloc_create_traces_table(void)
|
2016-03-23 18:03:55 -03:00
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
return hashtable_new(_Py_hashtable_hash_ptr,
|
2020-05-12 21:50:18 -03:00
|
|
|
_Py_hashtable_compare_direct,
|
2020-05-12 23:40:30 -03:00
|
|
|
NULL, raw_free);
|
2016-03-23 18:03:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 21:50:18 -03:00
|
|
|
static _Py_hashtable_t*
|
|
|
|
tracemalloc_create_domains_table(void)
|
2016-03-23 18:03:55 -03:00
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
return hashtable_new(hashtable_hash_uint,
|
2020-05-12 21:50:18 -03:00
|
|
|
_Py_hashtable_compare_direct,
|
2020-05-12 23:40:30 -03:00
|
|
|
NULL,
|
|
|
|
(_Py_hashtable_destroy_func)_Py_hashtable_destroy);
|
2020-05-12 20:36:47 -03:00
|
|
|
}
|
2016-03-23 18:03:55 -03:00
|
|
|
|
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
static _Py_hashtable_t*
|
|
|
|
tracemalloc_get_traces_table(unsigned int domain)
|
|
|
|
{
|
|
|
|
if (domain == DEFAULT_DOMAIN) {
|
|
|
|
return tracemalloc_traces;
|
|
|
|
}
|
|
|
|
else {
|
2020-05-12 23:40:30 -03:00
|
|
|
return _Py_hashtable_get(tracemalloc_domains, TO_PTR(domain));
|
2020-05-12 20:36:47 -03:00
|
|
|
}
|
2016-03-23 18:03:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
static void
|
2017-06-20 12:46:36 -03:00
|
|
|
tracemalloc_remove_trace(unsigned int domain, uintptr_t ptr)
|
2016-03-22 08:58:23 -03:00
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
assert(_Py_tracemalloc_config.tracing);
|
2016-03-22 09:39:05 -03:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_t *traces = tracemalloc_get_traces_table(domain);
|
|
|
|
if (!traces) {
|
2016-03-22 08:58:23 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
trace_t *trace = _Py_hashtable_steal(traces, TO_PTR(ptr));
|
|
|
|
if (!trace) {
|
2020-05-12 20:36:47 -03:00
|
|
|
return;
|
|
|
|
}
|
2020-05-12 22:52:11 -03:00
|
|
|
assert(tracemalloc_traced_memory >= trace->size);
|
|
|
|
tracemalloc_traced_memory -= trace->size;
|
|
|
|
raw_free(trace);
|
2016-03-22 08:58:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define REMOVE_TRACE(ptr) \
|
2016-09-06 17:47:26 -03:00
|
|
|
tracemalloc_remove_trace(DEFAULT_DOMAIN, (uintptr_t)(ptr))
|
2016-03-22 08:58:23 -03:00
|
|
|
|
|
|
|
|
2013-11-24 06:37:15 -04:00
|
|
|
static int
|
2017-06-20 12:46:36 -03:00
|
|
|
tracemalloc_add_trace(unsigned int domain, uintptr_t ptr,
|
2016-03-22 09:39:05 -03:00
|
|
|
size_t size)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
assert(_Py_tracemalloc_config.tracing);
|
2016-03-22 09:39:05 -03:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
traceback_t *traceback = traceback_new();
|
2016-03-22 08:58:23 -03:00
|
|
|
if (traceback == NULL) {
|
2013-11-24 06:37:15 -04:00
|
|
|
return -1;
|
2016-03-22 08:58:23 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_t *traces = tracemalloc_get_traces_table(domain);
|
|
|
|
if (traces == NULL) {
|
|
|
|
traces = tracemalloc_create_traces_table();
|
|
|
|
if (traces == NULL) {
|
2016-03-23 18:03:55 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
if (_Py_hashtable_set(tracemalloc_domains, TO_PTR(domain), traces) < 0) {
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_destroy(traces);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-23 05:38:54 -03:00
|
|
|
}
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
trace_t *trace = _Py_hashtable_get(traces, TO_PTR(ptr));
|
|
|
|
if (trace != NULL) {
|
2016-03-23 05:38:54 -03:00
|
|
|
/* the memory block is already tracked */
|
2020-05-12 22:52:11 -03:00
|
|
|
assert(tracemalloc_traced_memory >= trace->size);
|
|
|
|
tracemalloc_traced_memory -= trace->size;
|
2016-03-23 05:38:54 -03:00
|
|
|
|
2020-05-12 22:52:11 -03:00
|
|
|
trace->size = size;
|
|
|
|
trace->traceback = traceback;
|
2016-03-22 08:58:23 -03:00
|
|
|
}
|
2016-03-23 05:38:54 -03:00
|
|
|
else {
|
2020-05-12 23:40:30 -03:00
|
|
|
trace = raw_malloc(sizeof(trace_t));
|
2020-05-12 22:52:11 -03:00
|
|
|
if (trace == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
trace->size = size;
|
|
|
|
trace->traceback = traceback;
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
int res = _Py_hashtable_set(traces, TO_PTR(ptr), trace);
|
2016-03-23 05:38:54 -03:00
|
|
|
if (res != 0) {
|
2020-05-12 22:52:11 -03:00
|
|
|
raw_free(trace);
|
2016-03-23 05:38:54 -03:00
|
|
|
return res;
|
|
|
|
}
|
2013-11-24 06:28:20 -04:00
|
|
|
}
|
2013-11-24 06:37:15 -04:00
|
|
|
|
2016-09-07 13:26:18 -03:00
|
|
|
assert(tracemalloc_traced_memory <= SIZE_MAX - size);
|
2016-03-22 08:58:23 -03:00
|
|
|
tracemalloc_traced_memory += size;
|
2020-05-12 20:36:47 -03:00
|
|
|
if (tracemalloc_traced_memory > tracemalloc_peak_traced_memory) {
|
2016-03-22 08:58:23 -03:00
|
|
|
tracemalloc_peak_traced_memory = tracemalloc_traced_memory;
|
2020-05-12 20:36:47 -03:00
|
|
|
}
|
2016-03-22 08:58:23 -03:00
|
|
|
return 0;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
#define ADD_TRACE(ptr, size) \
|
2016-09-06 17:47:26 -03:00
|
|
|
tracemalloc_add_trace(DEFAULT_DOMAIN, (uintptr_t)(ptr), size)
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
static void*
|
2014-05-02 17:31:14 -03:00
|
|
|
tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
|
2013-11-23 07:27:24 -04:00
|
|
|
void *ptr;
|
|
|
|
|
2016-09-07 13:26:18 -03:00
|
|
|
assert(elsize == 0 || nelem <= SIZE_MAX / elsize);
|
2014-05-02 17:31:14 -03:00
|
|
|
|
|
|
|
if (use_calloc)
|
|
|
|
ptr = alloc->calloc(alloc->ctx, nelem, elsize);
|
|
|
|
else
|
|
|
|
ptr = alloc->malloc(alloc->ctx, nelem * elsize);
|
2013-12-03 20:29:35 -04:00
|
|
|
if (ptr == NULL)
|
|
|
|
return NULL;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_LOCK();
|
2016-03-22 08:58:23 -03:00
|
|
|
if (ADD_TRACE(ptr, nelem * elsize) < 0) {
|
2013-12-03 20:29:35 -04:00
|
|
|
/* Failed to allocate a trace for the new memory block */
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
2013-12-03 20:29:35 -04:00
|
|
|
alloc->free(alloc->ctx, ptr);
|
|
|
|
return NULL;
|
2013-11-24 06:37:15 -04:00
|
|
|
}
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
2013-11-23 07:27:24 -04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void*
|
2013-12-03 20:29:35 -04:00
|
|
|
tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
|
2013-11-23 07:27:24 -04:00
|
|
|
void *ptr2;
|
|
|
|
|
|
|
|
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
|
2013-12-03 20:29:35 -04:00
|
|
|
if (ptr2 == NULL)
|
|
|
|
return NULL;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2013-12-03 20:29:35 -04:00
|
|
|
if (ptr != NULL) {
|
|
|
|
/* an existing memory block has been resized */
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_LOCK();
|
2016-03-23 05:08:08 -03:00
|
|
|
|
|
|
|
/* tracemalloc_add_trace() updates the trace if there is already
|
2020-05-12 20:36:47 -03:00
|
|
|
a trace at address ptr2 */
|
2016-03-23 05:08:08 -03:00
|
|
|
if (ptr2 != ptr) {
|
|
|
|
REMOVE_TRACE(ptr);
|
|
|
|
}
|
2013-11-24 07:27:59 -04:00
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
if (ADD_TRACE(ptr2, new_size) < 0) {
|
2013-12-03 20:29:35 -04:00
|
|
|
/* Memory allocation failed. The error cannot be reported to
|
2016-08-30 14:47:49 -03:00
|
|
|
the caller, because realloc() may already have shrunk the
|
2013-12-03 20:29:35 -04:00
|
|
|
memory block and so removed bytes.
|
|
|
|
|
2016-04-17 02:32:47 -03:00
|
|
|
This case is very unlikely: a hash entry has just been
|
2013-12-03 20:47:46 -04:00
|
|
|
released, so the hash table should have at least one free entry.
|
|
|
|
|
|
|
|
The GIL and the table lock ensures that only one thread is
|
|
|
|
allocating memory. */
|
2020-03-09 15:49:52 -03:00
|
|
|
Py_FatalError("tracemalloc_realloc() failed to allocate a trace");
|
2013-11-24 06:37:15 -04:00
|
|
|
}
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
2013-12-03 20:29:35 -04:00
|
|
|
else {
|
|
|
|
/* new allocation */
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_LOCK();
|
2016-03-22 08:58:23 -03:00
|
|
|
if (ADD_TRACE(ptr2, new_size) < 0) {
|
2013-12-03 20:29:35 -04:00
|
|
|
/* Failed to allocate a trace for the new memory block */
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
2013-12-03 20:29:35 -04:00
|
|
|
alloc->free(alloc->ctx, ptr2);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
2013-12-03 20:29:35 -04:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
return ptr2;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void
|
|
|
|
tracemalloc_free(void *ctx, void *ptr)
|
|
|
|
{
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
if (ptr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* GIL cannot be locked in PyMem_RawFree() because it would introduce
|
2019-09-05 13:06:49 -03:00
|
|
|
a deadlock in _PyThreadState_DeleteCurrent(). */
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
alloc->free(alloc->ctx, ptr);
|
2013-12-03 20:47:46 -04:00
|
|
|
|
|
|
|
TABLES_LOCK();
|
2016-03-22 08:58:23 -03:00
|
|
|
REMOVE_TRACE(ptr);
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void*
|
2014-05-02 17:31:14 -03:00
|
|
|
tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2013-12-03 20:29:35 -04:00
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if (get_reentrant()) {
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
|
2014-05-02 17:31:14 -03:00
|
|
|
if (use_calloc)
|
|
|
|
return alloc->calloc(alloc->ctx, nelem, elsize);
|
|
|
|
else
|
|
|
|
return alloc->malloc(alloc->ctx, nelem * elsize);
|
2013-12-03 20:29:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore reentrant call. PyObjet_Malloc() calls PyMem_Malloc() for
|
|
|
|
allocations larger than 512 bytes, don't trace the same memory
|
|
|
|
allocation twice. */
|
|
|
|
set_reentrant(1);
|
|
|
|
|
2014-05-02 17:31:14 -03:00
|
|
|
ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize);
|
2013-12-03 20:29:35 -04:00
|
|
|
|
|
|
|
set_reentrant(0);
|
|
|
|
return ptr;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2014-05-02 17:31:14 -03:00
|
|
|
static void*
|
|
|
|
tracemalloc_malloc_gil(void *ctx, size_t size)
|
|
|
|
{
|
|
|
|
return tracemalloc_alloc_gil(0, ctx, 1, size);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2014-05-02 17:31:14 -03:00
|
|
|
static void*
|
|
|
|
tracemalloc_calloc_gil(void *ctx, size_t nelem, size_t elsize)
|
|
|
|
{
|
|
|
|
return tracemalloc_alloc_gil(1, ctx, nelem, elsize);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void*
|
|
|
|
tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
|
|
|
|
{
|
2013-12-03 20:29:35 -04:00
|
|
|
void *ptr2;
|
|
|
|
|
|
|
|
if (get_reentrant()) {
|
|
|
|
/* Reentrant call to PyMem_Realloc() and PyMem_RawRealloc().
|
|
|
|
Example: PyMem_RawRealloc() is called internally by pymalloc
|
|
|
|
(_PyObject_Malloc() and _PyObject_Realloc()) to allocate a new
|
|
|
|
arena (new_arena()). */
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
|
2013-12-03 20:29:35 -04:00
|
|
|
|
|
|
|
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
|
2013-12-03 20:47:46 -04:00
|
|
|
if (ptr2 != NULL && ptr != NULL) {
|
|
|
|
TABLES_LOCK();
|
2016-03-22 08:58:23 -03:00
|
|
|
REMOVE_TRACE(ptr);
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
|
|
|
}
|
2013-12-03 20:29:35 -04:00
|
|
|
return ptr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore reentrant call. PyObjet_Realloc() calls PyMem_Realloc() for
|
|
|
|
allocations larger than 512 bytes. Don't trace the same memory
|
|
|
|
allocation twice. */
|
|
|
|
set_reentrant(1);
|
|
|
|
|
|
|
|
ptr2 = tracemalloc_realloc(ctx, ptr, new_size);
|
|
|
|
|
|
|
|
set_reentrant(0);
|
|
|
|
return ptr2;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
#ifdef TRACE_RAW_MALLOC
|
|
|
|
static void*
|
2014-05-02 17:31:14 -03:00
|
|
|
tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2013-12-03 20:29:35 -04:00
|
|
|
PyGILState_STATE gil_state;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if (get_reentrant()) {
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
|
2014-05-02 17:31:14 -03:00
|
|
|
if (use_calloc)
|
|
|
|
return alloc->calloc(alloc->ctx, nelem, elsize);
|
|
|
|
else
|
|
|
|
return alloc->malloc(alloc->ctx, nelem * elsize);
|
2013-12-03 20:29:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc()
|
|
|
|
indirectly which would call PyGILState_Ensure() if reentrant are not
|
|
|
|
disabled. */
|
|
|
|
set_reentrant(1);
|
|
|
|
|
|
|
|
gil_state = PyGILState_Ensure();
|
2014-05-02 17:31:14 -03:00
|
|
|
ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize);
|
2013-12-03 20:29:35 -04:00
|
|
|
PyGILState_Release(gil_state);
|
|
|
|
|
|
|
|
set_reentrant(0);
|
|
|
|
return ptr;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2014-05-02 17:31:14 -03:00
|
|
|
static void*
|
|
|
|
tracemalloc_raw_malloc(void *ctx, size_t size)
|
|
|
|
{
|
|
|
|
return tracemalloc_raw_alloc(0, ctx, 1, size);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2014-05-02 17:31:14 -03:00
|
|
|
static void*
|
|
|
|
tracemalloc_raw_calloc(void *ctx, size_t nelem, size_t elsize)
|
|
|
|
{
|
|
|
|
return tracemalloc_raw_alloc(1, ctx, nelem, elsize);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void*
|
|
|
|
tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
|
|
|
|
{
|
2013-12-03 20:29:35 -04:00
|
|
|
PyGILState_STATE gil_state;
|
|
|
|
void *ptr2;
|
|
|
|
|
|
|
|
if (get_reentrant()) {
|
|
|
|
/* Reentrant call to PyMem_RawRealloc(). */
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
|
2013-12-03 20:29:35 -04:00
|
|
|
|
|
|
|
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
|
|
|
|
|
2013-12-03 20:47:46 -04:00
|
|
|
if (ptr2 != NULL && ptr != NULL) {
|
|
|
|
TABLES_LOCK();
|
2016-03-22 08:58:23 -03:00
|
|
|
REMOVE_TRACE(ptr);
|
2013-12-03 20:47:46 -04:00
|
|
|
TABLES_UNLOCK();
|
|
|
|
}
|
2013-12-03 20:29:35 -04:00
|
|
|
return ptr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc()
|
|
|
|
indirectly which would call PyGILState_Ensure() if reentrant calls are
|
|
|
|
not disabled. */
|
|
|
|
set_reentrant(1);
|
|
|
|
|
|
|
|
gil_state = PyGILState_Ensure();
|
|
|
|
ptr2 = tracemalloc_realloc(ctx, ptr, new_size);
|
|
|
|
PyGILState_Release(gil_state);
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2013-12-03 20:29:35 -04:00
|
|
|
set_reentrant(0);
|
|
|
|
return ptr2;
|
|
|
|
}
|
|
|
|
#endif /* TRACE_RAW_MALLOC */
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
static void
|
|
|
|
tracemalloc_clear_filename(void *value)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
PyObject *filename = (PyObject *)value;
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_DECREF(filename);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
/* reentrant flag must be set to call this function and GIL must be held */
|
|
|
|
static void
|
|
|
|
tracemalloc_clear_traces(void)
|
|
|
|
{
|
|
|
|
/* The GIL protects variables againt concurrent access */
|
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
|
|
|
TABLES_LOCK();
|
|
|
|
_Py_hashtable_clear(tracemalloc_traces);
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_clear(tracemalloc_domains);
|
2013-11-23 07:27:24 -04:00
|
|
|
tracemalloc_traced_memory = 0;
|
2013-11-27 16:39:49 -04:00
|
|
|
tracemalloc_peak_traced_memory = 0;
|
2013-11-23 07:27:24 -04:00
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
|
|
|
_Py_hashtable_clear(tracemalloc_tracebacks);
|
|
|
|
|
|
|
|
_Py_hashtable_clear(tracemalloc_filenames);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static int
|
|
|
|
tracemalloc_init(void)
|
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
if (_Py_tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) {
|
2013-11-23 07:27:24 -04:00
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"the tracemalloc module has been unloaded");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
if (_Py_tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED)
|
2013-11-23 07:27:24 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
|
|
|
|
|
|
|
#ifdef REENTRANT_THREADLOCAL
|
2017-10-06 07:41:34 -03:00
|
|
|
if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
|
2013-11-23 07:27:24 -04:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
PyErr_SetFromWindowsErr(0);
|
|
|
|
#else
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-07 13:56:24 -03:00
|
|
|
#if defined(TRACE_RAW_MALLOC)
|
2013-11-23 07:27:24 -04:00
|
|
|
if (tables_lock == NULL) {
|
|
|
|
tables_lock = PyThread_allocate_lock();
|
|
|
|
if (tables_lock == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "cannot allocate lock");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
tracemalloc_filenames = hashtable_new(hashtable_hash_pyobject,
|
2020-05-12 21:50:18 -03:00
|
|
|
hashtable_compare_unicode,
|
2020-05-12 23:40:30 -03:00
|
|
|
tracemalloc_clear_filename, NULL);
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
tracemalloc_tracebacks = hashtable_new(hashtable_hash_traceback,
|
2020-05-12 21:50:18 -03:00
|
|
|
hashtable_compare_traceback,
|
2020-05-12 23:40:30 -03:00
|
|
|
NULL, raw_free);
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
tracemalloc_traces = tracemalloc_create_traces_table();
|
|
|
|
tracemalloc_domains = tracemalloc_create_domains_table();
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
if (tracemalloc_filenames == NULL || tracemalloc_tracebacks == NULL
|
2020-05-12 20:36:47 -03:00
|
|
|
|| tracemalloc_traces == NULL || tracemalloc_domains == NULL) {
|
2013-11-23 07:27:24 -04:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unknown_filename = PyUnicode_FromString("<unknown>");
|
|
|
|
if (unknown_filename == NULL)
|
|
|
|
return -1;
|
|
|
|
PyUnicode_InternInPlace(&unknown_filename);
|
|
|
|
|
|
|
|
tracemalloc_empty_traceback.nframe = 1;
|
2019-10-15 09:00:16 -03:00
|
|
|
tracemalloc_empty_traceback.total_nframe = 1;
|
2013-11-23 07:27:24 -04:00
|
|
|
/* borrowed reference */
|
|
|
|
tracemalloc_empty_traceback.frames[0].filename = unknown_filename;
|
|
|
|
tracemalloc_empty_traceback.frames[0].lineno = 0;
|
|
|
|
tracemalloc_empty_traceback.hash = traceback_hash(&tracemalloc_empty_traceback);
|
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
_Py_tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED;
|
2013-11-23 07:27:24 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void
|
|
|
|
tracemalloc_deinit(void)
|
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
if (_Py_tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED)
|
2013-11-23 07:27:24 -04:00
|
|
|
return;
|
2018-10-25 08:31:16 -03:00
|
|
|
_Py_tracemalloc_config.initialized = TRACEMALLOC_FINALIZED;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
tracemalloc_stop();
|
|
|
|
|
|
|
|
/* destroy hash tables */
|
2020-05-12 23:40:30 -03:00
|
|
|
_Py_hashtable_destroy(tracemalloc_domains);
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_destroy(tracemalloc_traces);
|
2013-11-23 07:27:24 -04:00
|
|
|
_Py_hashtable_destroy(tracemalloc_tracebacks);
|
|
|
|
_Py_hashtable_destroy(tracemalloc_filenames);
|
|
|
|
|
2017-09-07 13:56:24 -03:00
|
|
|
#if defined(TRACE_RAW_MALLOC)
|
2013-11-23 07:27:24 -04:00
|
|
|
if (tables_lock != NULL) {
|
|
|
|
PyThread_free_lock(tables_lock);
|
|
|
|
tables_lock = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef REENTRANT_THREADLOCAL
|
2017-10-06 07:41:34 -03:00
|
|
|
PyThread_tss_delete(&tracemalloc_reentrant_key);
|
2013-11-23 07:27:24 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Py_XDECREF(unknown_filename);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static int
|
2013-11-27 17:27:13 -04:00
|
|
|
tracemalloc_start(int max_nframe)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2014-06-02 16:57:10 -03:00
|
|
|
PyMemAllocatorEx alloc;
|
2013-11-27 17:27:13 -04:00
|
|
|
size_t size;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2019-10-15 09:00:16 -03:00
|
|
|
if (max_nframe < 1 || (unsigned long) max_nframe > MAX_NFRAME) {
|
2017-11-15 22:11:45 -04:00
|
|
|
PyErr_Format(PyExc_ValueError,
|
2019-10-15 09:00:16 -03:00
|
|
|
"the number of frames must be in range [1; %lu]",
|
|
|
|
MAX_NFRAME);
|
2013-11-23 07:27:24 -04:00
|
|
|
return -1;
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tracemalloc_init() < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
if (_Py_tracemalloc_config.tracing) {
|
2013-11-23 07:27:24 -04:00
|
|
|
/* hook already installed: do nothing */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
_Py_tracemalloc_config.max_nframe = max_nframe;
|
2013-11-27 17:27:13 -04:00
|
|
|
|
|
|
|
/* allocate a buffer to store a new traceback */
|
|
|
|
size = TRACEBACK_SIZE(max_nframe);
|
|
|
|
assert(tracemalloc_traceback == NULL);
|
|
|
|
tracemalloc_traceback = raw_malloc(size);
|
|
|
|
if (tracemalloc_traceback == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
#ifdef TRACE_RAW_MALLOC
|
|
|
|
alloc.malloc = tracemalloc_raw_malloc;
|
2014-05-02 17:31:14 -03:00
|
|
|
alloc.calloc = tracemalloc_raw_calloc;
|
2013-11-23 07:27:24 -04:00
|
|
|
alloc.realloc = tracemalloc_raw_realloc;
|
|
|
|
alloc.free = tracemalloc_free;
|
|
|
|
|
|
|
|
alloc.ctx = &allocators.raw;
|
|
|
|
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
alloc.malloc = tracemalloc_malloc_gil;
|
2014-05-02 17:31:14 -03:00
|
|
|
alloc.calloc = tracemalloc_calloc_gil;
|
2013-11-23 07:27:24 -04:00
|
|
|
alloc.realloc = tracemalloc_realloc_gil;
|
|
|
|
alloc.free = tracemalloc_free;
|
|
|
|
|
|
|
|
alloc.ctx = &allocators.mem;
|
|
|
|
PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
|
|
|
|
|
|
|
|
alloc.ctx = &allocators.obj;
|
|
|
|
PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
|
|
|
|
|
|
|
|
/* everything is ready: start tracing Python memory allocations */
|
2018-10-25 08:31:16 -03:00
|
|
|
_Py_tracemalloc_config.tracing = 1;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static void
|
|
|
|
tracemalloc_stop(void)
|
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
if (!_Py_tracemalloc_config.tracing)
|
2013-11-23 07:27:24 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* stop tracing Python memory allocations */
|
2018-10-25 08:31:16 -03:00
|
|
|
_Py_tracemalloc_config.tracing = 0;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
/* unregister the hook on memory allocators */
|
|
|
|
#ifdef TRACE_RAW_MALLOC
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
|
|
|
#endif
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);
|
|
|
|
|
|
|
|
tracemalloc_clear_traces();
|
2016-03-21 18:00:58 -03:00
|
|
|
|
|
|
|
/* release memory */
|
2013-11-27 17:27:13 -04:00
|
|
|
raw_free(tracemalloc_traceback);
|
|
|
|
tracemalloc_traceback = NULL;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[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)
|
2017-02-05 16:58:46 -04:00
|
|
|
/*[clinic end generated code: output=2d763b42601cd3ef input=af104b0a00192f63]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
return PyBool_FromLong(_Py_tracemalloc_config.tracing);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_tracemalloc.clear_traces
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
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]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
if (!_Py_tracemalloc_config.tracing)
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
|
|
|
set_reentrant(1);
|
|
|
|
tracemalloc_clear_traces();
|
|
|
|
set_reentrant(0);
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static PyObject*
|
|
|
|
frame_to_pyobject(frame_t *frame)
|
|
|
|
{
|
|
|
|
PyObject *frame_obj, *lineno_obj;
|
|
|
|
|
|
|
|
frame_obj = PyTuple_New(2);
|
|
|
|
if (frame_obj == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(frame->filename);
|
|
|
|
PyTuple_SET_ITEM(frame_obj, 0, frame->filename);
|
|
|
|
|
2016-03-15 17:57:02 -03:00
|
|
|
lineno_obj = PyLong_FromUnsignedLong(frame->lineno);
|
2013-11-23 07:27:24 -04:00
|
|
|
if (lineno_obj == NULL) {
|
|
|
|
Py_DECREF(frame_obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(frame_obj, 1, lineno_obj);
|
|
|
|
|
|
|
|
return frame_obj;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static PyObject*
|
|
|
|
traceback_to_pyobject(traceback_t *traceback, _Py_hashtable_t *intern_table)
|
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
PyObject *frames;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
if (intern_table != NULL) {
|
2020-05-12 23:40:30 -03:00
|
|
|
frames = _Py_hashtable_get(intern_table, (const void *)traceback);
|
|
|
|
if (frames) {
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_INCREF(frames);
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
frames = PyTuple_New(traceback->nframe);
|
|
|
|
if (frames == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
for (int i=0; i < traceback->nframe; i++) {
|
|
|
|
PyObject *frame = frame_to_pyobject(&traceback->frames[i]);
|
2013-11-23 07:27:24 -04:00
|
|
|
if (frame == NULL) {
|
|
|
|
Py_DECREF(frames);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(frames, i, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intern_table != NULL) {
|
2020-05-12 23:40:30 -03:00
|
|
|
if (_Py_hashtable_set(intern_table, traceback, frames) < 0) {
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_DECREF(frames);
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* intern_table keeps a new reference to frames */
|
|
|
|
Py_INCREF(frames);
|
|
|
|
}
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static PyObject*
|
2020-05-12 23:40:30 -03:00
|
|
|
trace_to_pyobject(unsigned int domain, const trace_t *trace,
|
2016-03-22 08:58:23 -03:00
|
|
|
_Py_hashtable_t *intern_tracebacks)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
|
|
|
PyObject *trace_obj = NULL;
|
2016-03-22 08:58:23 -03:00
|
|
|
PyObject *obj;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2019-10-15 09:00:16 -03:00
|
|
|
trace_obj = PyTuple_New(4);
|
2013-11-23 07:27:24 -04:00
|
|
|
if (trace_obj == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
obj = PyLong_FromSize_t(domain);
|
|
|
|
if (obj == NULL) {
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_DECREF(trace_obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-22 08:58:23 -03:00
|
|
|
PyTuple_SET_ITEM(trace_obj, 0, obj);
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
obj = PyLong_FromSize_t(trace->size);
|
|
|
|
if (obj == NULL) {
|
|
|
|
Py_DECREF(trace_obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(trace_obj, 1, obj);
|
|
|
|
|
|
|
|
obj = traceback_to_pyobject(trace->traceback, intern_tracebacks);
|
|
|
|
if (obj == NULL) {
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_DECREF(trace_obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-22 08:58:23 -03:00
|
|
|
PyTuple_SET_ITEM(trace_obj, 2, obj);
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2019-10-15 09:00:16 -03:00
|
|
|
obj = PyLong_FromUnsignedLong(trace->traceback->total_nframe);
|
|
|
|
if (obj == NULL) {
|
|
|
|
Py_DECREF(trace_obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(trace_obj, 3, obj);
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
return trace_obj;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
typedef struct {
|
|
|
|
_Py_hashtable_t *traces;
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_t *domains;
|
2013-11-23 07:27:24 -04:00
|
|
|
_Py_hashtable_t *tracebacks;
|
|
|
|
PyObject *list;
|
2020-05-12 20:36:47 -03:00
|
|
|
unsigned int domain;
|
2013-11-23 07:27:24 -04:00
|
|
|
} get_traces_t;
|
|
|
|
|
2020-05-12 22:52:11 -03:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
static int
|
2020-05-12 22:52:11 -03:00
|
|
|
tracemalloc_copy_trace(_Py_hashtable_t *traces,
|
2020-05-12 23:40:30 -03:00
|
|
|
const void *key, const void *value,
|
|
|
|
void *user_data)
|
2020-05-12 20:36:47 -03:00
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
_Py_hashtable_t *traces2 = (_Py_hashtable_t *)user_data;
|
2020-05-12 22:52:11 -03:00
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
trace_t *trace = (trace_t *)value;
|
2020-05-12 22:52:11 -03:00
|
|
|
|
|
|
|
trace_t *trace2 = raw_malloc(sizeof(trace_t));
|
2020-10-13 03:46:31 -03:00
|
|
|
if (trace2 == NULL) {
|
2020-05-12 22:52:11 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*trace2 = *trace;
|
2020-05-12 23:40:30 -03:00
|
|
|
if (_Py_hashtable_set(traces2, key, trace2) < 0) {
|
2020-05-12 22:52:11 -03:00
|
|
|
raw_free(trace2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _Py_hashtable_t*
|
|
|
|
tracemalloc_copy_traces(_Py_hashtable_t *traces)
|
|
|
|
{
|
|
|
|
_Py_hashtable_t *traces2 = tracemalloc_create_traces_table();
|
|
|
|
if (traces2 == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int err = _Py_hashtable_foreach(traces,
|
|
|
|
tracemalloc_copy_trace,
|
|
|
|
traces2);
|
|
|
|
if (err) {
|
|
|
|
_Py_hashtable_destroy(traces2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return traces2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
tracemalloc_copy_domain(_Py_hashtable_t *domains,
|
2020-05-12 23:40:30 -03:00
|
|
|
const void *key, const void *value,
|
|
|
|
void *user_data)
|
2020-05-12 22:52:11 -03:00
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
_Py_hashtable_t *domains2 = (_Py_hashtable_t *)user_data;
|
2020-05-12 20:36:47 -03:00
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
unsigned int domain = (unsigned int)FROM_PTR(key);
|
|
|
|
_Py_hashtable_t *traces = (_Py_hashtable_t *)value;
|
2020-05-12 20:36:47 -03:00
|
|
|
|
2020-05-12 22:52:11 -03:00
|
|
|
_Py_hashtable_t *traces2 = tracemalloc_copy_traces(traces);
|
2020-05-12 23:40:30 -03:00
|
|
|
if (_Py_hashtable_set(domains2, TO_PTR(domain), traces2) < 0) {
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_destroy(traces2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 22:52:11 -03:00
|
|
|
static _Py_hashtable_t*
|
|
|
|
tracemalloc_copy_domains(_Py_hashtable_t *domains)
|
|
|
|
{
|
|
|
|
_Py_hashtable_t *domains2 = tracemalloc_create_domains_table();
|
|
|
|
if (domains2 == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int err = _Py_hashtable_foreach(domains,
|
|
|
|
tracemalloc_copy_domain,
|
|
|
|
domains2);
|
|
|
|
if (err) {
|
|
|
|
_Py_hashtable_destroy(domains2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return domains2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static int
|
2020-05-12 23:40:30 -03:00
|
|
|
tracemalloc_get_traces_fill(_Py_hashtable_t *traces,
|
|
|
|
const void *key, const void *value,
|
2016-03-21 18:00:58 -03:00
|
|
|
void *user_data)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
|
|
|
get_traces_t *get_traces = user_data;
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
const trace_t *trace = (const trace_t *)value;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
PyObject *tuple = trace_to_pyobject(get_traces->domain, trace,
|
|
|
|
get_traces->tracebacks);
|
|
|
|
if (tuple == NULL) {
|
2013-11-23 07:27:24 -04:00
|
|
|
return 1;
|
2020-05-12 23:40:30 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
int res = PyList_Append(get_traces->list, tuple);
|
|
|
|
Py_DECREF(tuple);
|
|
|
|
if (res < 0) {
|
2013-11-23 07:27:24 -04:00
|
|
|
return 1;
|
2020-05-12 23:40:30 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
static int
|
|
|
|
tracemalloc_get_traces_domain(_Py_hashtable_t *domains,
|
2020-05-12 23:40:30 -03:00
|
|
|
const void *key, const void *value,
|
2020-05-12 20:36:47 -03:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
get_traces_t *get_traces = user_data;
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
unsigned int domain = (unsigned int)FROM_PTR(key);
|
|
|
|
_Py_hashtable_t *traces = (_Py_hashtable_t *)value;
|
2020-05-12 20:36:47 -03:00
|
|
|
|
|
|
|
get_traces->domain = domain;
|
|
|
|
return _Py_hashtable_foreach(traces,
|
|
|
|
tracemalloc_get_traces_fill,
|
|
|
|
get_traces);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 21:50:18 -03:00
|
|
|
static void
|
2020-05-12 23:40:30 -03:00
|
|
|
tracemalloc_pyobject_decref(void *value)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
PyObject *obj = (PyObject *)value;
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_DECREF(obj);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[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]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
|
|
|
get_traces_t get_traces;
|
2020-05-12 20:36:47 -03:00
|
|
|
get_traces.domain = DEFAULT_DOMAIN;
|
2013-11-23 07:27:24 -04:00
|
|
|
get_traces.traces = NULL;
|
2020-05-12 20:36:47 -03:00
|
|
|
get_traces.domains = NULL;
|
2013-11-23 07:27:24 -04:00
|
|
|
get_traces.tracebacks = NULL;
|
|
|
|
get_traces.list = PyList_New(0);
|
|
|
|
if (get_traces.list == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
if (!_Py_tracemalloc_config.tracing)
|
2013-11-23 07:27:24 -04:00
|
|
|
return get_traces.list;
|
|
|
|
|
2013-11-25 19:26:23 -04:00
|
|
|
/* the traceback hash table is used temporarily to intern traceback tuple
|
|
|
|
of (filename, lineno) tuples */
|
2020-05-12 23:40:30 -03:00
|
|
|
get_traces.tracebacks = hashtable_new(_Py_hashtable_hash_ptr,
|
2020-05-12 21:50:18 -03:00
|
|
|
_Py_hashtable_compare_direct,
|
2020-05-12 23:40:30 -03:00
|
|
|
NULL, tracemalloc_pyobject_decref);
|
2013-11-23 07:27:24 -04:00
|
|
|
if (get_traces.tracebacks == NULL) {
|
2020-05-12 20:36:47 -03:00
|
|
|
goto no_memory;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
// Copy all traces so tracemalloc_get_traces_fill() doesn't have to disable
|
|
|
|
// temporarily tracemalloc which would impact other threads and so would
|
|
|
|
// miss allocations while get_traces() is called.
|
2013-11-23 07:27:24 -04:00
|
|
|
TABLES_LOCK();
|
2020-05-12 22:52:11 -03:00
|
|
|
get_traces.traces = tracemalloc_copy_traces(tracemalloc_traces);
|
2013-11-23 07:27:24 -04:00
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
|
|
|
if (get_traces.traces == NULL) {
|
2020-05-12 20:36:47 -03:00
|
|
|
goto no_memory;
|
|
|
|
}
|
2020-05-12 22:52:11 -03:00
|
|
|
|
|
|
|
TABLES_LOCK();
|
|
|
|
get_traces.domains = tracemalloc_copy_domains(tracemalloc_domains);
|
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
|
|
|
if (get_traces.domains == NULL) {
|
2020-05-12 20:36:47 -03:00
|
|
|
goto no_memory;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
// Convert traces to a list of tuples
|
2013-11-23 07:27:24 -04:00
|
|
|
set_reentrant(1);
|
2020-05-12 22:52:11 -03:00
|
|
|
int err = _Py_hashtable_foreach(get_traces.traces,
|
|
|
|
tracemalloc_get_traces_fill,
|
|
|
|
&get_traces);
|
2020-05-12 20:36:47 -03:00
|
|
|
if (!err) {
|
|
|
|
err = _Py_hashtable_foreach(get_traces.domains,
|
2020-05-12 22:52:11 -03:00
|
|
|
tracemalloc_get_traces_domain,
|
|
|
|
&get_traces);
|
2020-05-12 20:36:47 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
set_reentrant(0);
|
2020-05-12 20:36:47 -03:00
|
|
|
if (err) {
|
2013-11-23 07:27:24 -04:00
|
|
|
goto error;
|
2020-05-12 20:36:47 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
goto finally;
|
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
no_memory:
|
|
|
|
PyErr_NoMemory();
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
error:
|
|
|
|
Py_CLEAR(get_traces.list);
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if (get_traces.tracebacks != NULL) {
|
|
|
|
_Py_hashtable_destroy(get_traces.tracebacks);
|
|
|
|
}
|
2016-03-22 08:13:01 -03:00
|
|
|
if (get_traces.traces != NULL) {
|
2013-11-23 07:27:24 -04:00
|
|
|
_Py_hashtable_destroy(get_traces.traces);
|
2016-03-22 08:13:01 -03:00
|
|
|
}
|
2020-05-12 20:36:47 -03:00
|
|
|
if (get_traces.domains != NULL) {
|
2020-05-12 23:40:30 -03:00
|
|
|
_Py_hashtable_destroy(get_traces.domains);
|
2020-05-12 20:36:47 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
return get_traces.list;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2016-03-15 18:22:13 -03:00
|
|
|
static traceback_t*
|
2017-06-20 12:46:36 -03:00
|
|
|
tracemalloc_get_traceback(unsigned int domain, uintptr_t ptr)
|
2016-03-15 18:22:13 -03:00
|
|
|
{
|
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
if (!_Py_tracemalloc_config.tracing)
|
2016-03-15 18:22:13 -03:00
|
|
|
return NULL;
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
trace_t *trace;
|
2016-03-15 18:22:13 -03:00
|
|
|
TABLES_LOCK();
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_t *traces = tracemalloc_get_traces_table(domain);
|
|
|
|
if (traces) {
|
2020-05-12 23:40:30 -03:00
|
|
|
trace = _Py_hashtable_get(traces, TO_PTR(ptr));
|
2016-03-22 08:58:23 -03:00
|
|
|
}
|
|
|
|
else {
|
2020-05-12 23:40:30 -03:00
|
|
|
trace = NULL;
|
2016-03-22 08:58:23 -03:00
|
|
|
}
|
2016-03-15 18:22:13 -03:00
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
2020-05-12 23:40:30 -03:00
|
|
|
if (!trace) {
|
2016-03-15 18:22:13 -03:00
|
|
|
return NULL;
|
2020-05-12 22:52:11 -03:00
|
|
|
}
|
2016-03-15 18:22:13 -03:00
|
|
|
|
2020-05-12 22:52:11 -03:00
|
|
|
return trace->traceback;
|
2016-03-15 18:22:13 -03:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[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]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
|
|
|
PyTypeObject *type;
|
|
|
|
void *ptr;
|
2016-03-15 18:22:13 -03:00
|
|
|
traceback_t *traceback;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
type = Py_TYPE(obj);
|
2018-10-25 12:31:10 -03:00
|
|
|
if (PyType_IS_GC(type)) {
|
2013-11-23 07:27:24 -04:00
|
|
|
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
|
2018-10-25 12:31:10 -03:00
|
|
|
}
|
|
|
|
else {
|
2013-11-23 07:27:24 -04:00
|
|
|
ptr = (void *)obj;
|
2018-10-25 12:31:10 -03:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2016-09-06 17:47:26 -03:00
|
|
|
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr);
|
2016-03-15 18:22:13 -03:00
|
|
|
if (traceback == NULL)
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
2016-03-15 18:22:13 -03:00
|
|
|
return traceback_to_pyobject(traceback, NULL);
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2016-03-15 18:22:13 -03:00
|
|
|
#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
|
|
|
|
|
|
|
|
static void
|
|
|
|
_PyMem_DumpFrame(int fd, frame_t * frame)
|
|
|
|
{
|
|
|
|
PUTS(fd, " File \"");
|
|
|
|
_Py_DumpASCII(fd, frame->filename);
|
|
|
|
PUTS(fd, "\", line ");
|
|
|
|
_Py_DumpDecimal(fd, frame->lineno);
|
|
|
|
PUTS(fd, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump the traceback where a memory block was allocated into file descriptor
|
|
|
|
fd. The function may block on TABLES_LOCK() but it is unlikely. */
|
|
|
|
void
|
|
|
|
_PyMem_DumpTraceback(int fd, const void *ptr)
|
|
|
|
{
|
|
|
|
traceback_t *traceback;
|
|
|
|
int i;
|
|
|
|
|
2018-11-13 10:14:58 -04:00
|
|
|
if (!_Py_tracemalloc_config.tracing) {
|
|
|
|
PUTS(fd, "Enable tracemalloc to get the memory block "
|
|
|
|
"allocation traceback\n\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-06 17:47:26 -03:00
|
|
|
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr);
|
2016-03-15 18:22:13 -03:00
|
|
|
if (traceback == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PUTS(fd, "Memory block allocated at (most recent call first):\n");
|
|
|
|
for (i=0; i < traceback->nframe; i++) {
|
|
|
|
_PyMem_DumpFrame(fd, &traceback->frames[i]);
|
|
|
|
}
|
|
|
|
PUTS(fd, "\n");
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-15 18:22:13 -03:00
|
|
|
#undef PUTS
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_tracemalloc.start
|
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
nframe: int = 1
|
2017-02-04 06:18:38 -04:00
|
|
|
/
|
|
|
|
|
|
|
|
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 *
|
2017-11-15 22:11:45 -04:00
|
|
|
_tracemalloc_start_impl(PyObject *module, int nframe)
|
|
|
|
/*[clinic end generated code: output=caae05c23c159d3c input=40d849b5b29d1933]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2017-11-15 22:11:45 -04:00
|
|
|
if (tracemalloc_start(nframe) < 0) {
|
2013-11-23 07:37:20 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_tracemalloc.stop
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
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]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
|
|
|
tracemalloc_stop();
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_tracemalloc.get_traceback_limit
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
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]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
return PyLong_FromLong(_Py_tracemalloc_config.max_nframe);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2020-05-12 20:36:47 -03:00
|
|
|
static int
|
|
|
|
tracemalloc_get_tracemalloc_memory_cb(_Py_hashtable_t *domains,
|
2020-05-12 23:40:30 -03:00
|
|
|
const void *key, const void *value,
|
2020-05-12 20:36:47 -03:00
|
|
|
void *user_data)
|
|
|
|
{
|
2020-05-12 23:40:30 -03:00
|
|
|
const _Py_hashtable_t *traces = value;
|
2020-05-12 20:36:47 -03:00
|
|
|
size_t *size = (size_t*)user_data;
|
|
|
|
*size += _Py_hashtable_size(traces);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[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]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
size = _Py_hashtable_size(tracemalloc_tracebacks);
|
|
|
|
size += _Py_hashtable_size(tracemalloc_filenames);
|
|
|
|
|
|
|
|
TABLES_LOCK();
|
|
|
|
size += _Py_hashtable_size(tracemalloc_traces);
|
2020-05-12 20:36:47 -03:00
|
|
|
_Py_hashtable_foreach(tracemalloc_domains,
|
|
|
|
tracemalloc_get_tracemalloc_memory_cb, &size);
|
2013-11-23 07:27:24 -04:00
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
return PyLong_FromSize_t(size);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
/*[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]*/
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
2013-11-27 16:39:49 -04:00
|
|
|
Py_ssize_t size, peak_size;
|
2013-11-23 07:27:24 -04:00
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
if (!_Py_tracemalloc_config.tracing)
|
2013-11-23 07:27:24 -04:00
|
|
|
return Py_BuildValue("ii", 0, 0);
|
|
|
|
|
|
|
|
TABLES_LOCK();
|
|
|
|
size = tracemalloc_traced_memory;
|
2013-11-27 16:39:49 -04:00
|
|
|
peak_size = tracemalloc_peak_traced_memory;
|
2013-11-23 07:27:24 -04:00
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
2017-02-04 06:18:38 -04:00
|
|
|
return Py_BuildValue("nn", size, peak_size);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2020-05-22 11:18:51 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_tracemalloc.reset_peak
|
|
|
|
|
|
|
|
Set the peak size of memory blocks traced by tracemalloc to the current size.
|
|
|
|
|
|
|
|
Do nothing if the tracemalloc module is not tracing memory allocations.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_tracemalloc_reset_peak_impl(PyObject *module)
|
|
|
|
/*[clinic end generated code: output=140c2870f691dbb2 input=18afd0635066e9ce]*/
|
|
|
|
{
|
|
|
|
if (!_Py_tracemalloc_config.tracing) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
TABLES_LOCK();
|
|
|
|
tracemalloc_peak_traced_memory = tracemalloc_traced_memory;
|
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
static PyMethodDef module_methods[] = {
|
2017-02-04 06:18:38 -04:00
|
|
|
_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
|
2020-05-22 11:18:51 -03:00
|
|
|
_TRACEMALLOC_RESET_PEAK_METHODDEF
|
2013-11-23 07:27:24 -04:00
|
|
|
/* sentinel */
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(module_doc,
|
|
|
|
"Debug module to trace memory blocks allocated by Python.");
|
|
|
|
|
|
|
|
static struct PyModuleDef module_def = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"_tracemalloc",
|
|
|
|
module_doc,
|
|
|
|
0, /* non-negative size to be able to unload the module */
|
|
|
|
module_methods,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__tracemalloc(void)
|
|
|
|
{
|
|
|
|
PyObject *m;
|
|
|
|
m = PyModule_Create(&module_def);
|
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-20 06:00:31 -04:00
|
|
|
if (tracemalloc_init() < 0) {
|
|
|
|
Py_DECREF(m);
|
2013-11-23 07:27:24 -04:00
|
|
|
return NULL;
|
2019-11-20 06:00:31 -04:00
|
|
|
}
|
2013-11-23 07:27:24 -04:00
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-11-23 07:27:24 -04:00
|
|
|
int
|
2017-11-15 22:11:45 -04:00
|
|
|
_PyTraceMalloc_Init(int nframe)
|
2013-11-23 07:27:24 -04:00
|
|
|
{
|
|
|
|
assert(PyGILState_Check());
|
2017-11-15 22:11:45 -04:00
|
|
|
if (nframe == 0) {
|
|
|
|
return 0;
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
2013-11-27 17:27:13 -04:00
|
|
|
return tracemalloc_start(nframe);
|
2013-11-23 07:27:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:58:23 -03:00
|
|
|
|
2013-12-01 05:03:26 -04:00
|
|
|
void
|
|
|
|
_PyTraceMalloc_Fini(void)
|
|
|
|
{
|
|
|
|
assert(PyGILState_Check());
|
|
|
|
tracemalloc_deinit();
|
|
|
|
}
|
2016-03-22 09:39:05 -03:00
|
|
|
|
|
|
|
int
|
2017-06-20 12:46:36 -03:00
|
|
|
PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
|
|
|
size_t size)
|
2016-03-22 09:39:05 -03:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
PyGILState_STATE gil_state;
|
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
if (!_Py_tracemalloc_config.tracing) {
|
2016-03-22 09:39:05 -03:00
|
|
|
/* tracemalloc is not tracing: do nothing */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
gil_state = PyGILState_Ensure();
|
|
|
|
|
|
|
|
TABLES_LOCK();
|
|
|
|
res = tracemalloc_add_trace(domain, ptr, size);
|
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
|
|
|
PyGILState_Release(gil_state);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-06-20 12:46:36 -03:00
|
|
|
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
|
2016-03-22 09:39:05 -03:00
|
|
|
{
|
2018-10-25 08:31:16 -03:00
|
|
|
if (!_Py_tracemalloc_config.tracing) {
|
2016-03-22 09:39:05 -03:00
|
|
|
/* tracemalloc is not tracing: do nothing */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
TABLES_LOCK();
|
|
|
|
tracemalloc_remove_trace(domain, ptr);
|
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-25 08:31:16 -03:00
|
|
|
/* If the object memory block is already traced, update its trace
|
|
|
|
with the current Python traceback.
|
|
|
|
|
|
|
|
Do nothing if tracemalloc is not tracing memory allocations
|
|
|
|
or if the object memory block is not already traced. */
|
|
|
|
int
|
|
|
|
_PyTraceMalloc_NewReference(PyObject *op)
|
|
|
|
{
|
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
|
|
|
if (!_Py_tracemalloc_config.tracing) {
|
|
|
|
/* tracemalloc is not tracing: do nothing */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uintptr_t ptr;
|
|
|
|
PyTypeObject *type = Py_TYPE(op);
|
|
|
|
if (PyType_IS_GC(type)) {
|
|
|
|
ptr = (uintptr_t)((char *)op - sizeof(PyGC_Head));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ptr = (uintptr_t)op;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
TABLES_LOCK();
|
2020-05-12 23:40:30 -03:00
|
|
|
trace_t *trace = _Py_hashtable_get(tracemalloc_traces, TO_PTR(ptr));
|
|
|
|
if (trace != NULL) {
|
2018-10-25 08:31:16 -03:00
|
|
|
/* update the traceback of the memory block */
|
|
|
|
traceback_t *traceback = traceback_new();
|
|
|
|
if (traceback != NULL) {
|
2020-05-12 22:52:11 -03:00
|
|
|
trace->traceback = traceback;
|
2018-10-25 08:31:16 -03:00
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* else: cannot track the object, its memory block size is unknown */
|
|
|
|
TABLES_UNLOCK();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-22 09:39:05 -03:00
|
|
|
PyObject*
|
2017-06-20 12:46:36 -03:00
|
|
|
_PyTraceMalloc_GetTraceback(unsigned int domain, uintptr_t ptr)
|
2016-03-22 09:39:05 -03:00
|
|
|
{
|
|
|
|
traceback_t *traceback;
|
|
|
|
|
|
|
|
traceback = tracemalloc_get_traceback(domain, ptr);
|
|
|
|
if (traceback == NULL)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
|
|
|
return traceback_to_pyobject(traceback, NULL);
|
|
|
|
}
|