void * zthread_fork (zctx_t *ctx, zthread_attached_fn *thread_fn, void *args) { shim_t *shim = NULL; // Create our end of the pipe void *pipe = zsocket_new (ctx, ZMQ_PAIR); if (pipe) { zsocket_set_hwm (pipe, zctx_hwm (ctx)); zsocket_bind (pipe, "inproc://zctx-pipe-%p", pipe); } else return NULL; // Prepare argument shim for child thread shim = (shim_t *) zmalloc (sizeof (shim_t)); if (shim) { shim->attached = thread_fn; shim->args = args; shim->ctx = zctx_shadow (ctx); if (!shim->ctx) return NULL; } else return NULL; // Connect child pipe to our pipe shim->pipe = zsocket_new (shim->ctx, ZMQ_PAIR); if (!shim->pipe) return NULL; zsocket_set_hwm (shim->pipe, 1); zsocket_connect (shim->pipe, "inproc://zctx-pipe-%p", pipe); s_thread_start (shim); return pipe; }
void * zctx__socket_new (zctx_t *self, int type) { // Initialize context now if necessary assert (self); zmutex_lock (self->mutex); if (!self->context) self->context = zmq_init (self->iothreads); zmutex_unlock (self->mutex); if (!self->context) return NULL; // Create and register socket void *zocket = zmq_socket (self->context, type); if (!zocket) return NULL; #if (ZMQ_VERSION_MAJOR == 2) // For ZeroMQ/2.x we use sndhwm for both send and receive zsocket_set_hwm (zocket, self->sndhwm); #else // For later versions we use separate SNDHWM and RCVHWM zsocket_set_sndhwm (zocket, self->sndhwm); zsocket_set_rcvhwm (zocket, self->rcvhwm); #endif zmutex_lock (self->mutex); if (zlist_push (self->sockets, zocket)) { zmutex_unlock (self->mutex); zmq_close (zocket); return NULL; } zmutex_unlock (self->mutex); return zocket; }
int main (void) { zctx_t *ctx = zctx_new (); zctx_set_linger (ctx, 1000); void *pub = zsocket_new (ctx, ZMQ_XPUB); zsocket_set_hwm (pub, 0); zsocket_connect (pub, "tcp://127.0.0.1:9000"); // Wait for subscriber to subscribe zframe_t *frame = zframe_recv (pub); zframe_destroy (&frame); // Send HELLOs for five seconds size_t total = 0; int64_t finish_at = zclock_time () + 5000; while (zclock_time () < finish_at) { // Send 100K and then check time again int count = 0; for (count = 0; count < 100000; count++) zstr_send (pub, "HELLO"); total++; } printf ("%zd00000 messages sent\n", total); zstr_send (pub, "WORLD"); zctx_destroy (&ctx); return 0; }
int main (int argc, char *argv []) { // Socket to talk to server printf ("Collecting updates from weather server...\n"); zctx_t *context = zctx_new (); void *subscriber = zsocket_new (context, ZMQ_PULL); zsocket_set_hwm(subscriber, 100000); int rc = zsocket_connect (subscriber, RECEIVE_SOCKET); assert (rc == 0); void *worker = zsocket_new(context, ZMQ_PUSH); rc = zsocket_bind(worker, WORKER_SOCKET); assert(rc == 0); int nthreads = 0; for (nthreads=0; nthreads < 50; nthreads++) { zthread_fork(context, parser_thread, NULL); } // Subscribe to zipcode, default is NYC, 10001 while(1) { int size; char *string = safe_recv_from_server (subscriber, &size); parse_notifications(string, size, worker); free (string); } printf("Ending \n"); zctx_destroy (&context); return 0; }
int main (void) { // Prepare our context and publisher socket zctx_t *ctx = zctx_new (); void *publisher = zsocket_new (ctx, ZMQ_PUB); zsocket_set_hwm (publisher, 0); zsocket_bind (publisher, "tcp://*:5556"); zclock_sleep (200); zhash_t *kvmap = zhash_new (); int64_t sequence = 0; srandom ((unsigned) time (NULL)); while (!zctx_interrupted) { // Distribute as key-value message kvmsg_t *kvmsg = kvmsg_new (++sequence); kvmsg_fmt_key (kvmsg, "%d", randof (10000)); kvmsg_fmt_body (kvmsg, "%d", randof (1000000)); kvmsg_send (kvmsg, publisher); kvmsg_store (&kvmsg, kvmap); } printf (" Interrupted\n%d messages out\n", (int) sequence); zhash_destroy (&kvmap); zctx_destroy (&ctx); return 0; }
void * zctx__socket_pipe (zctx_t *self) { assert (self); void *pipe = zctx__socket_new (self, ZMQ_PAIR); if (pipe) zsocket_set_hwm (pipe, self->pipehwm); return pipe; }
/* We have to defer connection until the zctx is available to us. * This function is idempotent. */ static int connect_socket (ctx_t *ctx) { if (!ctx->sock) { if (!ctx->zctx) { errno = EINVAL; return -1; } if (!(ctx->sock = zsocket_new (ctx->zctx, ZMQ_PAIR))) return -1; zsocket_set_hwm (ctx->sock, 0); if (zsocket_connect (ctx->sock, "%s", ctx->uri) < 0) { zsocket_destroy (ctx->zctx, ctx->sock); ctx->sock = NULL; return -1; } } return 0; }
void * zsys_socket (int type, const char *filename, size_t line_nbr) { // First time initialization; if the application is mixing // its own threading calls with zsock, this may fail if two // threads try to create sockets at the same time. In such // apps, they MUST create a socket in the main program before // starting any threads. If the app uses zactor for its threads // then we can guarantee this to always be safe. zsys_init (); ZMUTEX_LOCK (s_mutex); void *handle = zmq_socket (s_process_ctx, type); // Configure socket with process defaults zsocket_set_linger (handle, (int) s_linger); #if (ZMQ_VERSION_MAJOR == 2) // For ZeroMQ/2.x we use sndhwm for both send and receive zsocket_set_hwm (handle, s_sndhwm); #else // For later versions we use separate SNDHWM and RCVHWM zsocket_set_sndhwm (handle, (int) s_sndhwm); zsocket_set_rcvhwm (handle, (int) s_rcvhwm); # if defined (ZMQ_IPV6) zsocket_set_ipv6 (handle, s_ipv6); # else zsocket_set_ipv4only (handle, s_ipv6 ? 0 : 1); # endif #endif // Add socket to reference tracker so we can report leaks; this is // done only when the caller passes a filename/line_nbr if (filename) { s_sockref_t *sockref = (s_sockref_t *) malloc (sizeof (s_sockref_t)); sockref->handle = handle; sockref->type = type; sockref->filename = filename; sockref->line_nbr = line_nbr; zlist_append (s_sockref_list, sockref); } s_open_sockets++; ZMUTEX_UNLOCK (s_mutex); return handle; }
void * zctx__socket_new (zctx_t *self, int type) { // Initialize context now if necessary assert (self); if (!self->context) self->context = zmq_init (self->iothreads); if (!self->context) return NULL; // Create and register socket void *zocket = zmq_socket (self->context, type); if (!zocket) return NULL; zsocket_set_hwm (zocket, self->hwm); if (zlist_push (self->sockets, zocket)) { zmq_close (zocket); return NULL; } return zocket; }
static void server_thread (void *args, zctx_t *ctx, void *pipe) { FILE *file = fopen ("testdata", "r"); assert (file); void *router = zsocket_new (ctx, ZMQ_ROUTER); // Default HWM is 1000, which will drop messages here // since we send more than 1,000 chunks of test data, // so set an infinite HWM as a simple, stupid solution: zsocket_set_hwm (router, 0); zsocket_bind (router, "tcp://*:6000"); while (true) { // First frame in each message is the sender identity zframe_t *identity = zframe_recv (router); if (!identity) break; // Shutting down, quit // Second frame is "fetch" command char *command = zstr_recv (router); assert (streq (command, "fetch")); free (command); while (true) { byte *data = malloc (CHUNK_SIZE); assert (data); size_t size = fread (data, 1, CHUNK_SIZE, file); zframe_t *chunk = zframe_new_zero_copy ( data, size, free_chunk, NULL); zframe_send (&identity, router, ZFRAME_REUSE + ZFRAME_MORE); zframe_send (&chunk, router, 0); if (size == 0) break; // Always end with a zero-size frame } zframe_destroy (&identity); } fclose (file); }
int zsockopt_test (bool verbose) { printf (" * zsockopt: "); // @selftest zctx_t *ctx = zctx_new (); assert (ctx); void *zocket; #if (ZMQ_VERSION_MAJOR == 2) # if defined (ZMQ_HWM) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_hwm (zocket, 1); assert (zsocket_hwm (zocket) == 1); zsocket_hwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SWAP) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_swap (zocket, 1); assert (zsocket_swap (zocket) == 1); zsocket_swap (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_AFFINITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_affinity (zocket, 1); assert (zsocket_affinity (zocket) == 1); zsocket_affinity (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IDENTITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_identity (zocket, "test"); char *identity = zsocket_identity (zocket); assert (identity); free (identity); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RATE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rate (zocket, 1); assert (zsocket_rate (zocket) == 1); zsocket_rate (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl (zocket, 1); assert (zsocket_recovery_ivl (zocket) == 1); zsocket_recovery_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL_MSEC) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl_msec (zocket, 1); assert (zsocket_recovery_ivl_msec (zocket) == 1); zsocket_recovery_ivl_msec (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MCAST_LOOP) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_mcast_loop (zocket, 1); assert (zsocket_mcast_loop (zocket) == 1); zsocket_mcast_loop (zocket); zsocket_destroy (ctx, zocket); # endif # if (ZMQ_VERSION_MINOR == 2) # if defined (ZMQ_RCVTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvtimeo (zocket, 1); assert (zsocket_rcvtimeo (zocket) == 1); zsocket_rcvtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # endif # if (ZMQ_VERSION_MINOR == 2) # if defined (ZMQ_SNDTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndtimeo (zocket, 1); assert (zsocket_sndtimeo (zocket) == 1); zsocket_sndtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # endif # if defined (ZMQ_SNDBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndbuf (zocket, 1); assert (zsocket_sndbuf (zocket) == 1); zsocket_sndbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvbuf (zocket, 1); assert (zsocket_rcvbuf (zocket) == 1); zsocket_rcvbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LINGER) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_linger (zocket, 1); assert (zsocket_linger (zocket) == 1); zsocket_linger (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl (zocket, 1); assert (zsocket_reconnect_ivl (zocket) == 1); zsocket_reconnect_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL_MAX) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl_max (zocket, 1); assert (zsocket_reconnect_ivl_max (zocket) == 1); zsocket_reconnect_ivl_max (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_BACKLOG) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_backlog (zocket, 1); assert (zsocket_backlog (zocket) == 1); zsocket_backlog (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_subscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_UNSUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_unsubscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_TYPE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_type (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVMORE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_rcvmore (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_FD) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_fd (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_EVENTS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_events (zocket); zsocket_destroy (ctx, zocket); # endif #endif #if (ZMQ_VERSION_MAJOR == 3) # if defined (ZMQ_TYPE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_type (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDHWM) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_sndhwm (zocket, 1); assert (zsocket_sndhwm (zocket) == 1); zsocket_sndhwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVHWM) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvhwm (zocket, 1); assert (zsocket_rcvhwm (zocket) == 1); zsocket_rcvhwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_AFFINITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_affinity (zocket, 1); assert (zsocket_affinity (zocket) == 1); zsocket_affinity (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_subscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_UNSUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_unsubscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IDENTITY) zocket = zsocket_new (ctx, ZMQ_DEALER); assert (zocket); zsocket_set_identity (zocket, "test"); char *identity = zsocket_identity (zocket); assert (identity); free (identity); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RATE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rate (zocket, 1); assert (zsocket_rate (zocket) == 1); zsocket_rate (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl (zocket, 1); assert (zsocket_recovery_ivl (zocket) == 1); zsocket_recovery_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDBUF) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_sndbuf (zocket, 1); assert (zsocket_sndbuf (zocket) == 1); zsocket_sndbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvbuf (zocket, 1); assert (zsocket_rcvbuf (zocket) == 1); zsocket_rcvbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LINGER) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_linger (zocket, 1); assert (zsocket_linger (zocket) == 1); zsocket_linger (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl (zocket, 1); assert (zsocket_reconnect_ivl (zocket) == 1); zsocket_reconnect_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL_MAX) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl_max (zocket, 1); assert (zsocket_reconnect_ivl_max (zocket) == 1); zsocket_reconnect_ivl_max (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_BACKLOG) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_backlog (zocket, 1); assert (zsocket_backlog (zocket) == 1); zsocket_backlog (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MAXMSGSIZE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_maxmsgsize (zocket, 1); assert (zsocket_maxmsgsize (zocket) == 1); zsocket_maxmsgsize (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MULTICAST_HOPS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_multicast_hops (zocket, 1); assert (zsocket_multicast_hops (zocket) == 1); zsocket_multicast_hops (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvtimeo (zocket, 1); assert (zsocket_rcvtimeo (zocket) == 1); zsocket_rcvtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndtimeo (zocket, 1); assert (zsocket_sndtimeo (zocket) == 1); zsocket_sndtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IPV4ONLY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_ipv4only (zocket, 1); assert (zsocket_ipv4only (zocket) == 1); zsocket_ipv4only (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_DELAY_ATTACH_ON_CONNECT) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_delay_attach_on_connect (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_ROUTER_MANDATORY) zocket = zsocket_new (ctx, ZMQ_ROUTER); assert (zocket); zsocket_set_router_mandatory (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_ROUTER_RAW) zocket = zsocket_new (ctx, ZMQ_ROUTER); assert (zocket); zsocket_set_router_raw (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_XPUB_VERBOSE) zocket = zsocket_new (ctx, ZMQ_XPUB); assert (zocket); zsocket_set_xpub_verbose (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVMORE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_rcvmore (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_FD) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_fd (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_EVENTS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_events (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LAST_ENDPOINT) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); char *last_endpoint = zsocket_last_endpoint (zocket); assert (last_endpoint); free (last_endpoint); zsocket_destroy (ctx, zocket); # endif zocket = zsocket_new (ctx, ZMQ_SUB); zsocket_set_hwm (zocket, 1); #endif zctx_destroy (&ctx); // @end printf ("OK\n"); return 0; }
int main () { int64_t diff; unsigned int N_KEYS; unsigned int TN_KEYS; int bool_bench; int N_THREADS; printf ("\n number of keys to retrieve:"); scanf ("%u", &N_KEYS); printf ("\n total number of keys:"); scanf ("%u", &TN_KEYS); printf("\n benchmark randomly inserted database or sequentially inserted database:(1->rand or 0->seq)"); scanf ("%u", &bool_bench); printf ("\n number of threads:"); scanf ("%u", &N_THREADS); //zeromq context zctx_t *ctx = zctx_new (); //initialize worker args worker_args_t worker_args; //initialize rb_tree; struct dbkey_rb_t dbkey_rb; RB_INIT (&dbkey_rb); //initialize keys unsigned int factor = (unsigned int) N_KEYS / TN_KEYS; tinymt32_t tinymt32; /*initializing random generator with the same seed */ tinymt32_init (&tinymt32, 0); unsigned int *keys = (unsigned int *) malloc (sizeof (unsigned int) * N_KEYS); unsigned int sec_iter; unsigned int iter; iter = 0; while (iter < N_KEYS) { keys[iter] = tinymt32_generate_uint32 (&tinymt32); tinymt32_generate_uint32 (&tinymt32); for (sec_iter = 1; sec_iter < factor; sec_iter++) { tinymt32_generate_uint32 (&tinymt32); tinymt32_generate_uint32 (&tinymt32); } iter++; } //initialize database char *errptr = NULL; leveldb_options_t *options = leveldb_options_create (); worker_args.options = &options; /* initialize Options */ leveldb_options_set_create_if_missing (options, 1); leveldb_options_set_write_buffer_size (options, 62914560); leveldb_options_set_max_open_files (options, 800000); //bloom filter leveldb_filterpolicy_t *bloom = leveldb_filterpolicy_create_bloom (10); leveldb_options_set_filter_policy (options, bloom); leveldb_readoptions_t *readoptions = leveldb_readoptions_create (); worker_args.readoptions = readoptions; leveldb_writeoptions_t *writeoptions = leveldb_writeoptions_create (); worker_args.writeoptions = writeoptions; int rc; void *push = zsocket_new (ctx, ZMQ_PUSH); int water = 2000000000; zsocket_set_hwm (push, water); rc = zsocket_bind (push, "inproc://bind_point"); //connect void *pub = zsocket_new (ctx, ZMQ_PUB); rc = zsocket_bind (pub, "inproc://pub_point"); //connect void *router = zsocket_new (ctx, ZMQ_ROUTER); rc = zsocket_bind (router, "inproc://response_point"); //sleep a while zclock_sleep (1000); //this assumes some synchronization at the start and end of each new bench leveldb_t *db_pointer; worker_args.db = &db_pointer; //initialize the threads void *pipe[6]; unsigned char i; for (i = 0; i < N_THREADS; i++) { pipe[i] = zthread_fork (ctx, &worker_fn, (void *) &(worker_args)); } //sleep a while zclock_sleep (1000); if(bool_bench==0){ db_pointer = leveldb_open (options, "/mnt/database/database", &errptr); benchmark_notree (push, pub, router, N_KEYS, keys,N_THREADS); printf ("\n benchmark with tree 1st pass"); leveldb_close (db_pointer); db_pointer = leveldb_open (options, "/mnt/database/database", &errptr); benchmark_tree (push, pub, router, N_KEYS, keys, dbkey_rb ,N_THREADS); leveldb_close (db_pointer); }else{if(bool_bench==1){ db_pointer = leveldb_open (options, "/mnt/database/database_helper", &errptr); printf ("\n starting random read without a rb_btree on random inserted database"); benchmark_notree (push, pub, router, N_KEYS, keys,N_THREADS); leveldb_close (db_pointer); db_pointer = leveldb_open (options, "/mnt/database/database_helper", &errptr); printf ("\n with a tree"); benchmark_tree (push, pub, router, N_KEYS, keys, dbkey_rb,N_THREADS); leveldb_close (db_pointer); } } }
module_t *module_add (modhash_t *mh, const char *path) { module_t *p; void *dso; const char **mod_namep; mod_main_f *mod_main; zfile_t *zf; int rc; dlerror (); if (!(dso = dlopen (path, RTLD_NOW | RTLD_LOCAL))) { msg ("%s", dlerror ()); errno = ENOENT; return NULL; } mod_main = dlsym (dso, "mod_main"); mod_namep = dlsym (dso, "mod_name"); if (!mod_main || !mod_namep || !*mod_namep) { dlclose (dso); errno = ENOENT; return NULL; } p = xzmalloc (sizeof (*p)); p->name = xstrdup (*mod_namep); p->magic = MODULE_MAGIC; p->main = mod_main; p->dso = dso; zf = zfile_new (NULL, path); p->digest = xstrdup (zfile_digest (zf)); p->size = (int)zfile_cursize (zf); zfile_destroy (&zf); if (!(p->uuid = zuuid_new ())) oom (); if (!(p->rmmod = zlist_new ())) oom (); if (!(p->subs = zlist_new ())) oom (); p->rank = mh->rank; p->zctx = mh->zctx; p->broker_h = mh->broker_h; p->heartbeat = mh->heartbeat; /* Broker end of PAIR socket is opened here. */ if (!(p->sock = zsocket_new (p->zctx, ZMQ_PAIR))) err_exit ("zsocket_new"); zsocket_set_hwm (p->sock, 0); if (zsocket_bind (p->sock, "inproc://%s", module_get_uuid (p)) < 0) err_exit ("zsock_bind inproc://%s", module_get_uuid (p)); if (!(p->broker_w = flux_zmq_watcher_create (flux_get_reactor (p->broker_h), p->sock, FLUX_POLLIN, module_cb, p))) err_exit ("flux_zmq_watcher_create"); /* Update the modhash. */ rc = zhash_insert (mh->zh_byuuid, module_get_uuid (p), p); assert (rc == 0); /* uuids are by definition unique */ zhash_freefn (mh->zh_byuuid, module_get_uuid (p), (zhash_free_fn *)module_destroy); return p; }
static void parser_thread(void *args, zctx_t* context, void *pipe){ void *dispatch_socket = zsocket_new (context, ZMQ_PUB); zsocket_set_hwm(dispatch_socket, 10000); int rc = zsocket_connect(dispatch_socket, DISPATCH_SOCKET); assert(rc == 0); void *work_receiver = zsocket_new(context, ZMQ_PULL); rc = zsocket_connect(work_receiver, WORKER_SOCKET); assert(rc == 0); while(1) { zmq_msg_t message; zmq_msg_init (&message); int size = zmq_msg_recv (&message, work_receiver, 0); assert (size != -1); char *buff = malloc (size + 1); memcpy (buff, zmq_msg_data (&message), size); zmq_msg_close (&message); buff [size] = 0; ssize_t i = 0; char action[81+FILENAME_MAX] = {0}; while (i < size) { struct inotify_event *pevent = (struct inotify_event *)&buff[i]; if (pevent->len){ int serial_length = sizeof(struct inotify_event) + pevent->len; dispatch(&buff[i], serial_length, dispatch_socket); strcpy (action, pevent->name); }else{ strcpy (action, "some random directory"); } if (pevent->mask & IN_ACCESS) strcat(action, " was read"); if (pevent->mask & IN_ATTRIB) strcat(action, " Metadata changed"); if (pevent->mask & IN_CLOSE_WRITE) strcat(action, " opened for writing was closed"); if (pevent->mask & IN_CLOSE_NOWRITE) strcat(action, " not opened for writing was closed"); if (pevent->mask & IN_CREATE) strcat(action, " created in watched directory"); if (pevent->mask & IN_DELETE) strcat(action, " deleted from watched directory"); if (pevent->mask & IN_DELETE_SELF) strcat(action, "Watched file/directory was itself deleted"); if (pevent->mask & IN_MODIFY) strcat(action, " was modified"); if (pevent->mask & IN_MOVE_SELF) strcat(action, "Watched file/directory was itself moved"); if (pevent->mask & IN_MOVED_FROM) strcat(action, " moved out of watched directory"); if (pevent->mask & IN_MOVED_TO) strcat(action, " moved into watched directory"); if (pevent->mask & IN_OPEN) strcat(action, " was opened"); //printf ("wd=%d mask=%d cookie=%d len=%d dir=%s\n",pevent->wd, pevent->mask, pevent->cookie, pevent->len, (pevent->mask & IN_ISDIR)?"yes":"no"); if (pevent->len) printf ("name=%s\n", pevent->name); //printf ("%s\n", action); i += sizeof(struct inotify_event) + pevent->len; } free (buff); } //zmq_close(dispatch_socket); //zmq_close(work_receiver); }