Skip to content

Commit

Permalink
More logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Sep 12, 2024
1 parent 5c48471 commit 95a85f2
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions c_src/nif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ extern "C" {

#include "merkletree.hpp"

static void print(const char *msg, int value) {
if (value % 10000 == 0) {
fprintf(stderr, "%s [%d]\n", msg, value); fflush(stderr);
static ErlNifResourceType *merkletree_type = NULL;
static volatile int ops = 0;
static volatile int shared_states = 0;
static volatile int resources = 0;

static void print(const char *msg) {
if (ops++ % 10000 == 0) {
fprintf(stderr, "%s [shared_states=%d] [resources=%d]\n", msg, shared_states, resources); fflush(stderr);
}
}

static ErlNifResourceType *merkletree_type = NULL;
static int alive = 0;

class SharedState {
public:
ErlNifMutex *mtx;
Expand All @@ -22,14 +24,20 @@ class SharedState {
SharedState() : tree() {
mtx = enif_mutex_create((char*)"merkletree_mutex");
has_clone = 0;
shared_states++;
print("CREATE");
}

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

~SharedState() {
shared_states--;
print("DESTROY");
enif_mutex_destroy(mtx);
}
};
Expand Down Expand Up @@ -85,9 +93,9 @@ 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++;
mt->shared_state = new SharedState();
mt->locked = false;
print("CREATING", alive++);
ERL_NIF_TERM res = enif_make_resource(env, mt);
enif_release_resource(mt);
return res;
Expand All @@ -100,7 +108,7 @@ bool make_writeable(merkletree *mt)
if (mt->shared_state->has_clone > 0) {
mt->shared_state->has_clone -= 1;
mt->shared_state = new SharedState(*mt->shared_state);
print("CLONING", alive++);
print("CREATING (UNCLONING)");
}
return true;
}
Expand All @@ -114,6 +122,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++;
clone->shared_state = mt->shared_state;
clone->locked = false;
clone->shared_state->has_clone += 1;
Expand Down Expand Up @@ -368,9 +377,9 @@ static void
destruct_merkletree_type(ErlNifEnv* /*env*/, void *arg)
{
merkletree *mt = (merkletree *) arg;
resources--;
Lock lock(mt);
if (mt->shared_state->has_clone == 0) {
print("DESTROYING", alive--);
lock.unlock();
delete(mt->shared_state);
} else {
Expand Down

0 comments on commit 95a85f2

Please sign in to comment.