Skip to content

Commit

Permalink
MMappedCircuit skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreSenellart committed Feb 23, 2024
1 parent 2d69c37 commit f8b3f3c
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
INTERNAL = Makefile.internal
ARGS = with_llvm=no
ifdef DEBUG
ARGS+=DEBUG=1
endif
UNAME := $(shell uname)

ifeq ($(UNAME), Darwin)
Expand Down
2 changes: 1 addition & 1 deletion Makefile.internal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
LDFLAGS_SL = -lstdc++ -Wno-lto-type-mismatch

ifdef DEBUG
PG_CPPFLAGS += -Og -g
PG_CPPFLAGS += -O0 -g
endif

# Disable JIT code generation, due to various bugs, see in particular
Expand Down
39 changes: 39 additions & 0 deletions src/MMappedCircuit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "MMappedCircuit.h"

extern "C" {
#include "provsql_mmap.h"
}

static MMappedCircuit *c;

MMappedCircuit::MMappedCircuit(bool read_only)
{
if(read_only) {
gates_file = new bip::managed_mapped_file(bip::open_read_only, GATES_FILENAME);
wires_file = new bip::managed_mapped_file(bip::open_read_only, WIRES_FILENAME);
mapping_file = new bip::managed_mapped_file(bip::open_read_only, MAPPING_FILENAME);
} else {
gates_file = new bip::managed_mapped_file(bip::open_or_create, GATES_FILENAME, INITIAL_FILE_SIZE);
wires_file = new bip::managed_mapped_file(bip::open_or_create, WIRES_FILENAME, INITIAL_FILE_SIZE);
mapping_file = new bip::managed_mapped_file(bip::open_or_create, MAPPING_FILENAME, INITIAL_FILE_SIZE);
}
mapping = mapping_file->find_or_construct<mmapped_unordered_map>("mapping")(mapping_file->get_segment_manager());
// Get a raw pointer to access gates and wires
}

MMappedCircuit::~MMappedCircuit()
{
delete mapping_file;
delete gates_file;
delete wires_file;
}

void initialize_provsql_mmap()
{
c = new MMappedCircuit(false);
}

void destroy_provsql_mmap()
{
delete c;
}
51 changes: 51 additions & 0 deletions src/MMappedCircuit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef MMAPPED_CIRCUIT_H
#define MMAPPED_CIRCUIT_H

#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/unordered_map.hpp>

extern "C" {
#include "provsql_utils.h"
}

namespace bip=boost::interprocess;

typedef struct GateInformation
{
gate_type type;
unsigned nb_children;
unsigned children_idx;
double prob;
unsigned info1;
unsigned info2;
} GateInformation;

class MMappedCircuit {
private:
bip::managed_mapped_file *gates_file;
bip::managed_mapped_file *wires_file;
bip::managed_mapped_file *mapping_file;

using mmapped_unordered_map =
boost::unordered_map<
pg_uuid_t,
std::pair<unsigned, unsigned>,
boost::hash<pg_uuid_t>,
std::equal_to<pg_uuid_t>,
bip::allocator<pg_uuid_t, bip::managed_mapped_file::segment_manager> >;

mmapped_unordered_map *mapping;
GateInformation *gates;
pg_uuid_t *wires;

static constexpr const char *GATES_FILENAME="gates.mmap";
static constexpr const char *WIRES_FILENAME="wires.mmap";
static constexpr const char *MAPPING_FILENAME="mapping.mmap";
static constexpr unsigned INITIAL_FILE_SIZE = 1<<16;

public:
explicit MMappedCircuit(bool read_only);
~MMappedCircuit();
};

#endif /* MMAPPED_CIRCUIT_H */
8 changes: 7 additions & 1 deletion src/provsql_mmap.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "provsql_mmap.h"
#include "provsql_shmem.h"

#include <unistd.h>
#include "postgres.h"
Expand All @@ -7,12 +8,17 @@
void provsql_mmap_worker(Datum ignored)
{
BackgroundWorkerUnblockSignals();

elog(LOG, "%s initializing", MyBgworkerEntry->bgw_name);
initialize_provsql_mmap();
elog(LOG, "%s initialized", MyBgworkerEntry->bgw_name);

provsql_shared_state->mmap_initialized=true;

while(!sleep(1))
{
}

destroy_provsql_mmap();
}

void RegisterProvSQLMMapWorker(void)
Expand Down
2 changes: 2 additions & 0 deletions src/provsql_mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

void provsql_mmap_worker(Datum);
void RegisterProvSQLMMapWorker(void);
void initialize_provsql_mmap(void);
void destroy_provsql_mmap(void);

#endif /* PROVSQL_COLUMN_NAME */
2 changes: 1 addition & 1 deletion src/provsql_shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ void provsql_shmem_startup(void)
provsql_shared_state = NULL;
provsql_hash = NULL;


LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);

provsql_shared_state = ShmemInitStruct(
Expand All @@ -71,6 +70,7 @@ void provsql_shmem_startup(void)
#else
provsql_shared_state->lock =LWLockAssign();
#endif /* PG_VERSION_NUM >= 90600 */
provsql_shared_state->mmap_initialized=false;
provsql_shared_state->nb_wires=0;
}

Expand Down
1 change: 1 addition & 0 deletions src/provsql_shmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void provsql_shmem_request(void);
typedef struct provsqlSharedState
{
LWLock *lock; // protect access to the shared data
bool mmap_initialized;
unsigned nb_wires;
pg_uuid_t wires[FLEXIBLE_ARRAY_MEMBER];
} provsqlSharedState;
Expand Down

0 comments on commit f8b3f3c

Please sign in to comment.