Skip to content

Commit

Permalink
Merge pull request #66 from xian-network/devnet
Browse files Browse the repository at this point in the history
process id check
  • Loading branch information
crosschainer authored Sep 30, 2024
2 parents 9eeeace + 072eeed commit 09f9410
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/contracting/execution/metering/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/* Conditional inclusion of sys/resource.h for Unix-like systems */
#ifndef _WIN32
#include <sys/resource.h>
#include <unistd.h> // For Unix-like systems
#endif

#ifdef _WIN32
Expand Down Expand Up @@ -82,12 +83,24 @@ typedef struct {
long total_mem_usage;
int started;
char * cu_cost_fname;
unsigned long long process_id;
unsigned long long call_count; // Add this line to track call counts
}
Tracer;

static int get_process_id() {
#ifdef _WIN32
DWORD pid = GetCurrentProcessId();
return pid;
#else
pid_t pid = getpid();
return pid;
#endif
}

static int
Tracer_init(Tracer * self, PyObject * args, PyObject * kwds) {

//char *fname = getenv("CU_COST_FNAME");

//read_cu_costs(fname, self->cu_costs); // Read cu cu_costs from ones interpreted in Python
Expand All @@ -96,6 +109,7 @@ Tracer_init(Tracer * self, PyObject * args, PyObject * kwds) {
self -> cost = 0;
self -> last_frame_mem_usage = 0;
self -> total_mem_usage = 0;
self -> process_id = get_process_id();

return RET_OK;
}
Expand Down Expand Up @@ -128,6 +142,7 @@ static long get_memory_usage() {

static int
Tracer_trace(Tracer * self, PyFrameObject * frame, int what, PyObject * arg) {

self->call_count++;

if (self->call_count > 800000) {
Expand All @@ -142,6 +157,10 @@ Tracer_trace(Tracer * self, PyFrameObject * frame, int what, PyObject * arg) {
if (code == NULL) {
return RET_OK;
}
if (get_process_id() != self->process_id) {
PyDECREF(code);
return RET_OK;
}
const char *current_function_name = PyUnicode_AsUTF8(code->co_name);
if (current_function_name == NULL) {
Py_DECREF(code);
Expand Down

0 comments on commit 09f9410

Please sign in to comment.