Skip to content

Commit

Permalink
Free the memory/reference when module is freed (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored Nov 16, 2024
1 parent 397c222 commit f1e3590
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/viztracer/modules/quicktime.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ mach_timebase_info_data_t timebase_info;
double ts_to_ns_factor = 1.0;
int64_t system_base_time = 0;
int64_t system_base_ts = 0;
int64_t start_ts[CALIBRATE_SIZE] = {0};
int64_t start_ns[CALIBRATE_SIZE] = {0};
int64_t* start_ts = NULL;
int64_t* start_ns = NULL;
int64_t t0_ts = 0;
int64_t t0_ns = 0;
bool calibrated = false;
Expand Down Expand Up @@ -105,13 +105,23 @@ int64_t dur_ts_to_ns(int64_t dur)
return dur * ts_to_ns_factor;
}

void quicktime_free()
{
free(start_ts);
free(start_ns);
}

void quicktime_init()
{
#if _WIN32
QueryPerformanceFrequency(&qpc_freq);
#elif defined(__APPLE__)
mach_timebase_info(&timebase_info);
#endif

start_ts = (int64_t*)malloc(sizeof(int64_t) * CALIBRATE_SIZE);
start_ns = (int64_t*)malloc(sizeof(int64_t) * CALIBRATE_SIZE);

int64_t diff_ns[CALIBRATE_SIZE] = {0};

t0_ts = 0;
Expand Down
1 change: 1 addition & 0 deletions src/viztracer/modules/quicktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern double ts_to_ns_factor;
extern int64_t system_base_time;

void quicktime_init();
void quicktime_free();
double system_ts_to_us(int64_t ts);
int64_t system_ts_to_ns(int64_t ts);
double dur_ts_to_us(int64_t dur);
Expand Down
26 changes: 17 additions & 9 deletions src/viztracer/modules/snaptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static PyObject* snaptrace_setcurrstack(TracerObject* self, PyObject* stack_dept
static PyObject* snaptrace_setignorestackcounter(TracerObject* self, PyObject* value);
static void snaptrace_flush_unfinished(TracerObject* self, int flush_as_finish);
static void snaptrace_threaddestructor(void* key);
static void snaptrace_free(void*);
static struct ThreadInfo* snaptrace_createthreadinfo(TracerObject* self);
static void log_func_args(struct FunctionNode* node, PyFrameObject* frame, PyObject* log_func_repr);
static int64_t get_ts(struct ThreadInfo*);
Expand Down Expand Up @@ -238,20 +239,15 @@ static PyMethodDef Tracer_methods[] = {
{NULL, NULL, 0, NULL}
};

static PyMethodDef Snaptrace_methods[] = {
{NULL, NULL, 0, NULL}
};

// ================================================================
// Python interface
// ================================================================

static struct PyModuleDef snaptracemodule = {
PyModuleDef_HEAD_INIT,
"viztracer.snaptrace",
NULL,
-1,
Snaptrace_methods
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "viztracer.snaptrace",
.m_size = -1,
.m_free = snaptrace_free,
};

// =============================================================================
Expand Down Expand Up @@ -1768,6 +1764,18 @@ static PyTypeObject TracerType = {
.tp_getset = Tracer_getsetters,
};

void snaptrace_free(void* Py_UNUSED(unused)) {
quicktime_free();
Py_CLEAR(threading_module);
Py_CLEAR(multiprocessing_module);
Py_CLEAR(asyncio_module);
Py_CLEAR(asyncio_tasks_module);
Py_CLEAR(curr_task_getters[0]);
Py_CLEAR(trio_lowlevel_module);
Py_CLEAR(curr_task_getters[1]);
Py_CLEAR(json_module);
}

PyMODINIT_FUNC
PyInit_snaptrace(void)
{
Expand Down

0 comments on commit f1e3590

Please sign in to comment.