int main (int argc, char *argv[]) { int rc; zsock_t *zs; pthread_t tid; pthread_attr_t attr; flux_msg_t *msg; flux_sec_t *sec; int n; log_init (basename (argv[0])); if (argc != 1 && argc != 2) { fprintf (stderr, "Usage: tmunge [--fake]\n"); exit (1); } if (argc == 2) { if (!strcmp (argv[1], "--fake")) sec_typemask |= FLUX_SEC_FAKEMUNGE; else log_msg_exit ("unknown option %s", argv[1]); } if (!(sec = flux_sec_create (sec_typemask, NULL))) log_err_exit ("flux_sec_create"); if (flux_sec_comms_init (sec) < 0) log_err_exit ("flux_sec_comms_init: %s", flux_sec_errstr (sec)); if (!(zs = zsock_new_sub (uri, ""))) log_err_exit ("S: zsock_new_sub"); if (!(cs = zsock_new_pub (uri))) log_err_exit ("S: zsock_new_pub"); if ((rc = pthread_attr_init (&attr))) log_errn (rc, "S: pthread_attr_init"); if ((rc = pthread_create (&tid, &attr, thread, NULL))) log_errn (rc, "S: pthread_create"); /* Handle one client message. */ if (!(msg = flux_msg_recvzsock_munge (zs, sec))) log_err_exit ("S: flux_msg_recvzsock_munge: %s", flux_sec_errstr (sec)); //zmsg_dump (zmsg); if ((n = flux_msg_frames (msg)) != 4) log_err_exit ("S: expected 4 frames, got %d", n); flux_msg_destroy (msg); /* Wait for thread to terminate, then clean up. */ if ((rc = pthread_join (tid, NULL))) log_errn (rc, "S: pthread_join"); zsock_destroy (&zs); zsock_destroy (&cs); flux_sec_destroy (sec); log_fini (); return 0; }
/* Watch a key that does not exist throughout the test. */ static int mt_watchnil_cb (const char *k, int val, void *arg, int errnum) { thd_t *t = arg; if (errnum != ENOENT) { log_errn (errnum, "%d: %s", t->n, __FUNCTION__); return -1; } t->nil_count++; return 0; }
/* expect val: {-1,0,1,...,(changes - 1)} * count will therefore run 0...changes. */ static int mt_watch_cb (const char *k, int val, void *arg, int errnum) { thd_t *t = arg; if (errnum != 0) { log_errn (errnum, "%d: %s", t->n, __FUNCTION__); return -1; } if (val == t->last_val) { log_msg ("%d: %s: called with same value as last time: %d", t->n, __FUNCTION__, val); return -1; } t->last_val = val; /* normal stop */ if (val + 1 == changes) flux_reactor_stop (flux_get_reactor (t->h)); t->change_count++; return 0; }
void test_mt (int argc, char **argv) { thd_t *thd; int i, rc; flux_t *h; int errors = 0; if (argc != 3) { fprintf (stderr, "Usage: mt nthreads changes key\n"); exit (1); } nthreads = strtoul (argv[0], NULL, 10); changes = strtoul (argv[1], NULL, 10); key = argv[2]; thd = xzmalloc (sizeof (*thd) * nthreads); if (!(h = flux_open (NULL, 0))) log_err_exit ("flux_open"); /* Set initial value of 'key' to -1 */ if (kvs_put_int (h, key, -1) < 0) log_err_exit ("kvs_put_int %s", key); key_stable = xasprintf ("%s-stable", key); if (kvs_put_int (h, key_stable, 0) < 0) log_err_exit ("kvs_put_int %s", key); if (kvs_commit (h) < 0) log_err_exit ("kvs_commit"); for (i = 0; i < nthreads; i++) { thd[i].n = i; thd[i].last_val = -42; if ((rc = pthread_attr_init (&thd[i].attr))) log_errn (rc, "pthread_attr_init"); if ((rc = pthread_create (&thd[i].tid, &thd[i].attr, thread, &thd[i]))) log_errn (rc, "pthread_create"); } wait_ready (); for (i = 0; i < changes; i++) { if (kvs_put_int (h, key, i) < 0) log_err_exit ("kvs_put_int %s", key); if (kvs_commit (h) < 0) log_err_exit ("kvs_commit"); } /* Verify that callbacks were called the correct number of times. * The nil and stable callbacks will be called exactly once before the * reactor is started, then should never be called again. * Due to commit merging on the master, the changing callback may * miss intervening values but it shouldn't be called extra times. */ for (i = 0; i < nthreads; i++) { if ((rc = pthread_join (thd[i].tid, NULL))) log_errn (rc, "pthread_join"); if (thd[i].nil_count != 1) { log_msg ("%d: nil callback called %d times (expected one)", i, thd[i].nil_count); errors++; } if (thd[i].stable_count != 1) { log_msg ("%d: stable callback called %d times (expected one)", i, thd[i].stable_count); errors++; } if (thd[i].change_count > changes + 1) { log_msg ("%d: changing callback called %d times (expected <= %d)", i, thd[i].change_count, changes + 1); errors++; } } if (errors > 0) exit (1); free (thd); free (key_stable); flux_close (h); }
int main (int argc, char *argv[]) { thd_t *thd; int i, rc, ch; log_init (basename (argv[0])); while ((ch = getopt_long (argc, argv, OPTIONS, longopts, NULL)) != -1) { switch (ch) { case 'n': nopt = true; break; default: usage (); } } if (argc - optind != 2) usage (); threadcount = strtoul (argv[optind++], NULL, 10); if (!threadcount) log_msg_exit ("thread count must be > 0"); prefix = argv[optind++]; key = xasprintf ("%s.%s", prefix, KEYSUFFIX); /* +1 for watch thread */ thd = xzmalloc (sizeof (*thd) * (threadcount + 1)); /* start watch thread */ thd[threadcount].n = threadcount; if ((rc = pthread_attr_init (&thd[threadcount].attr))) log_errn (rc, "pthread_attr_init"); if ((rc = pthread_create (&thd[threadcount].t, &thd[threadcount].attr, watchthread, &thd[threadcount]))) log_errn (rc, "pthread_create"); /* Wait for watch thread to setup */ pthread_mutex_lock (&watch_init_lock); while (!watch_init) pthread_cond_wait (&watch_init_cond, &watch_init_lock); pthread_mutex_unlock (&watch_init_lock); /* start commit threads */ for (i = 0; i < threadcount; i++) { thd[i].n = i; if ((rc = pthread_attr_init (&thd[i].attr))) log_errn (rc, "pthread_attr_init"); if ((rc = pthread_create (&thd[i].t, &thd[i].attr, committhread, &thd[i]))) log_errn (rc, "pthread_create"); } /* +1 for watch thread */ for (i = 0; i < (threadcount + 1); i++) { if ((rc = pthread_join (thd[i].t, NULL))) log_errn (rc, "pthread_join"); } printf("%d\n", changecount); free (thd); free (key); log_fini (); return 0; }
int main (int argc, char *argv[]) { thd_t *thd; int i, rc; int ch; tstat_t ts; struct timespec t0; log_init (basename (argv[0])); while ((ch = getopt_long (argc, argv, OPTIONS, longopts, NULL)) != -1) { switch (ch) { case 'f': fopt = true; fence_nprocs = strtoul (optarg, NULL, 10); if (!fence_nprocs) log_msg_exit ("fence value must be > 0"); break; case 's': sopt = true; break; case 'n': nopt = true; nopt_divisor = strtoul (optarg, NULL, 10); if (!nopt_divisor) log_msg_exit ("nopt value must be > 0"); break; default: usage (); } } if (argc - optind != 3) usage (); nthreads = strtoul (argv[optind++], NULL, 10); if (!nthreads) log_msg_exit ("thread count must be > 0"); count = strtoul (argv[optind++], NULL, 10); if (!count) log_msg_exit ("commit count must be > 0"); prefix = argv[optind++]; memset (&ts, 0, sizeof (ts)); thd = xzmalloc (sizeof (*thd) * nthreads); if (sopt) monotime (&t0); for (i = 0; i < nthreads; i++) { thd[i].n = i; if (!(thd[i].perf = zlist_new ())) oom (); if ((rc = pthread_attr_init (&thd[i].attr))) log_errn (rc, "pthread_attr_init"); if ((rc = pthread_create (&thd[i].t, &thd[i].attr, thread, &thd[i]))) log_errn (rc, "pthread_create"); } for (i = 0; i < nthreads; i++) { if ((rc = pthread_join (thd[i].t, NULL))) log_errn (rc, "pthread_join"); if (sopt) { double *e; while ((e = zlist_pop (thd[i].perf))) { tstat_push (&ts, *e); free (e); } } zlist_destroy (&thd[i].perf); } if (sopt) { json_t *o; char *s; if (!(o = json_pack ("{s:{s:i s:f s:f s:f s:f} s:f}", "put+commit times (sec)", "count", tstat_count (&ts), "min", tstat_min (&ts)*1E-3, "mean", tstat_mean (&ts)*1E-3, "stddev", tstat_stddev (&ts)*1E-3, "max", tstat_max (&ts)*1E-3, "put+commit throughput (#/sec)", (double)(count*nthreads) / (monotime_since (t0)*1E-3)))) log_err_exit ("json_pack"); if (!(s = json_dumps (o, JSON_INDENT(2)))) log_err_exit ("json_dumps"); printf ("%s\n", s); json_decref (o); free (s); } free (thd); log_fini (); return 0; }
int main (int argc, char *argv[]) { int rc; zctx_t *zctx; void *zs; pthread_t tid; pthread_attr_t attr; zmsg_t *zmsg; int i, ch; const char *uritmpl = "ipc://*"; int timeout_sec = 1; log_init (basename (argv[0])); while ((ch = getopt_long (argc, argv, OPTIONS, longopts, NULL)) != -1) { switch (ch) { case 'r': raw = true; break; case 'l': lopt = true; linger = strtol (optarg, NULL, 10); break; case 'i': iopt = true; imm = strtol (optarg, NULL, 10); break; case 't': uritmpl = "tcp://*:*"; break; case 'T': timeout_sec = strtoul (optarg, NULL, 10); break; case 's': bufsize = strtoul (optarg, NULL, 10); break; case 'v': vopt = true; break; case 'S': sleep_usec = strtoul (optarg, NULL, 10);; break; default: usage (); /*NOTREACHED*/ } } if (optind != argc - 1) usage (); iter = strtoul (argv[optind++], NULL, 10); /* Create socket and bind to it. * Store uri in global variable. */ if (!(zctx = zctx_new ())) log_err_exit ("S: zctx_new"); if (!(zs = zsocket_new (zctx, ZMQ_ROUTER))) log_err_exit ("S: zsocket_new"); zsocket_set_rcvhwm (zs, 0); /* unlimited */ if (zsocket_bind (zs, "%s", uritmpl) < 0) log_err_exit ("S: zsocket_bind"); uri = zsocket_last_endpoint (zs); /* Spawn thread which will be our client. */ if ((rc = pthread_attr_init (&attr))) log_errn (rc, "S: pthread_attr_init"); if ((rc = pthread_create (&tid, &attr, thread, NULL))) log_errn (rc, "S: pthread_create"); /* Handle 'iter' client messages with timeout */ for (i = 0; i < iter; i++) { alarm (timeout_sec); if (!(zmsg = zmsg_recv (zs))) log_err_exit ("S: zmsg_recv"); zmsg_destroy (&zmsg); alarm (0); if (vopt) log_msg ("received message %d of %d", i + 1, iter); } /* Wait for thread to terminate, then clean up. */ if ((rc = pthread_join (tid, NULL))) log_errn (rc, "S: pthread_join"); zctx_destroy (&zctx); /* destroys sockets too */ if (strstr (uri, "ipc://")) (void)unlink (uri); log_fini (); return 0; }