Skip to content

Commit

Permalink
Better debug counting
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Jan 20, 2025
1 parent 3d2e71b commit de86e2e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions c_src/nif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ extern "C" {

static void print(const char *msg);
static ErlNifResourceType *merkletree_type = NULL;
static ErlNifMutex *stats_mutex = NULL;
static volatile int shared_states = 0;
static volatile int resources = 0;
static int locked_states_cnt = 0;


#ifdef DEBUG
#define STAT(cmd) { enif_mutex_lock(stats_mutex); cmd; enif_mutex_unlock(stats_mutex); }
static void print(const char *msg) {
static int ops = 0;

Expand All @@ -21,6 +24,7 @@ static void print(const char *msg) {
}
}
#else
#define STAT(cmd) {}
static void print(const char */*msg*/) {}
#endif

Expand All @@ -32,19 +36,19 @@ class SharedState {
SharedState() : tree() {
mtx = enif_mutex_create((char*)"merkletree_mutex");
has_clone = 0;
shared_states++;
STAT(shared_states++);
print("CREATE");
}

SharedState(SharedState &other) : tree(other.tree) {
mtx = enif_mutex_create((char*)"merkletree_mutex");
has_clone = 0;
shared_states++;
STAT(shared_states++);
print("CLONE");
}

~SharedState() {
shared_states--;
STAT(shared_states--);
print("DESTROY");
enif_mutex_destroy(mtx);
}
Expand Down Expand Up @@ -164,7 +168,7 @@ merkletree_new(ErlNifEnv *env, int argc, const ERL_NIF_TERM[] /*argv[]*/)
{
if (argc != 0) return enif_make_badarg(env);
merkletree *mt = (merkletree*)enif_alloc_resource(merkletree_type, sizeof(merkletree));
resources++;
STAT(resources++);
mt->shared_state = new SharedState();
mt->locked = false;
ERL_NIF_TERM res = enif_make_resource(env, mt);
Expand All @@ -181,7 +185,7 @@ merkletree_clone(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])

Lock lock(mt);
merkletree *clone = (merkletree*)enif_alloc_resource(merkletree_type, sizeof(merkletree));
resources++;
STAT(resources++);
clone->shared_state = mt->shared_state;
clone->locked = false;
clone->shared_state->has_clone += 1;
Expand Down Expand Up @@ -458,7 +462,7 @@ static void
destruct_merkletree_type(ErlNifEnv* /*env*/, void *arg)
{
merkletree *mt = (merkletree *) arg;
resources--;
STAT(resources--);
locked_states->leave_lock(mt);
}

Expand All @@ -474,6 +478,7 @@ on_load(ErlNifEnv* env, void** /*priv*/, ERL_NIF_TERM /*info*/)
merkletree_type = rt;

locked_states = new LockedStates();
stats_mutex = enif_mutex_create((char*)"stats_mutex");
return 0;
}

Expand Down

0 comments on commit de86e2e

Please sign in to comment.