Exemple #1
0
static void test_zmq (flux_reactor_t *reactor)
{
    zctx_t *zctx;
    void *zs[2];
    flux_watcher_t *r, *w;

    ok ((zctx = zctx_new ()) != NULL,
        "zmq: created zmq context");
    zs[0] = zsocket_new (zctx, ZMQ_PAIR);
    zs[1] = zsocket_new (zctx, ZMQ_PAIR);
    ok (zs[0] && zs[1]
        && zsocket_bind (zs[0], "inproc://test_zmq") == 0
        && zsocket_connect (zs[1], "inproc://test_zmq") == 0,
        "zmq: connected ZMQ_PAIR sockets over inproc");
    r = flux_zmq_watcher_create (reactor, zs[0], FLUX_POLLIN, zmqreader, NULL);
    w = flux_zmq_watcher_create (reactor, zs[1], FLUX_POLLOUT, zmqwriter, NULL);
    ok (r != NULL && w != NULL,
        "zmq: nonblocking reader and writer created");
    flux_watcher_start (r);
    flux_watcher_start (w);
    ok (flux_reactor_run  (reactor, 0) == 0,
        "zmq: reactor ran to completion after %d messages", zmqwriter_msgcount);
    flux_watcher_stop (r);
    flux_watcher_stop (w);
    flux_watcher_destroy (r);
    flux_watcher_destroy (w);

    zsocket_destroy (zctx, zs[0]);
    zsocket_destroy (zctx, zs[1]);
    zctx_destroy (&zctx);
}
void prep (flux_reactor_t *r, flux_watcher_t *w, int revents, void *arg)
{
    if (txcount == totcount) {
        flux_watcher_stop (w_prep);
        flux_watcher_stop (w_check);
    }
    else if ((txcount - rxcount) < max_queue_depth)
        flux_watcher_start (w_idle); // keeps loop from blocking
}
Exemple #3
0
static void fdreader (flux_reactor_t *r, flux_watcher_t *w,
                      int revents, void *arg)
{
    int fd = flux_fd_watcher_get_fd (w);
    static char *buf = NULL;
    static int count = 0;
    int n;

    if (!buf)
        buf = xzmalloc (fdwriter_bufsize);
    if (revents & FLUX_POLLERR) {
        fprintf (stderr, "%s: FLUX_POLLERR is set\n", __FUNCTION__);
        goto error;
    }
    if (revents & FLUX_POLLIN) {
        if ((n = read (fd, buf + count, fdwriter_bufsize - count)) < 0
                            && errno != EWOULDBLOCK && errno != EAGAIN) {
            fprintf (stderr, "%s: read failed: %s\n",
                     __FUNCTION__, strerror (errno));
            goto error;
        }
        if (n > 0) {
            count += n;
            if (count == fdwriter_bufsize) {
                flux_watcher_stop (w);
                free (buf);
            }
        }
    }
    return;
error:
    flux_reactor_stop_error (r);
}
Exemple #4
0
/* See POSIX 2008 Volume 3 Shell and Utilities, Issue 7
 * Section 2.8.2 Exit status for shell commands (page 2315)
 */
static void completion_cb (flux_subprocess_t *p)
{
    runlevel_t *r = flux_subprocess_aux_get (p, "runlevel");
    const char *exit_string = NULL;
    int rc;

    if ((rc = flux_subprocess_exit_code (p)) < 0) {
        /* bash standard, signals + 128 */
        if ((rc = flux_subprocess_signaled (p)) >= 0) {
            rc += 128;
            exit_string = strsignal (rc);
        }
    }
    else {
        if (rc)
            exit_string = "Exited with non-zero status";
        else
            exit_string = "Exited";
    }

    assert (r->rc[r->level].p == p);
    r->rc[r->level].p = NULL;

    flux_watcher_stop (r->rc[r->level].timer);

    if (r->cb) {
        double elapsed = monotime_since (r->rc[r->level].start) / 1000;
        r->cb (r, r->level, rc, elapsed, exit_string, r->cb_arg);
    }
    flux_subprocess_destroy (p);
}
Exemple #5
0
static void repeat (flux_reactor_t *r, flux_watcher_t *w,
                    int revents, void *arg)
{
    repeat_countdown--;
    if (repeat_countdown == 0)
        flux_watcher_stop (w);
}
Exemple #6
0
/* check:
 * Runs right after reactor calls poll(2).
 * Stop idle watcher, and send next alloc request, if available.
 */
static void check_cb (flux_reactor_t *r, flux_watcher_t *w,
                      int revents, void *arg)
{
    struct alloc_ctx *ctx = arg;
    struct job *job;

    flux_watcher_stop (ctx->idle);
    if (!ctx->ready)
        return;
    if (ctx->mode == SCHED_SINGLE && ctx->active_alloc_count > 0)
        return;
    if ((job = queue_first (ctx->inqueue))) {
        if (alloc_request (ctx, job) < 0) {
            flux_log_error (ctx->h, "alloc_request fatal error");
            flux_reactor_stop_error (flux_get_reactor (ctx->h));
            return;
        }
        queue_delete (ctx->inqueue, job, job->aux_queue_handle);
        job->aux_queue_handle = NULL;
        job->alloc_pending = 1;
        job->alloc_queued = 0;
        ctx->active_alloc_count++;
        if ((job->flags & FLUX_JOB_DEBUG))
            (void)event_job_post_pack (ctx->event_ctx, job,
                                       "debug.alloc-request", NULL);

    }
}
Exemple #7
0
static void zmqreader (flux_reactor_t *r, flux_watcher_t *w,
                       int revents, void *arg)
{
    void *sock = flux_zmq_watcher_get_zsock (w);
    static int count = 0;
    if (revents & FLUX_POLLERR) {
        fprintf (stderr, "%s: FLUX_POLLERR is set\n", __FUNCTION__);
        goto error;
    }
    if (revents & FLUX_POLLIN) {
        zmsg_t *zmsg = zmsg_recv (sock);
        if (!zmsg) {
            fprintf (stderr, "%s: zmsg_recv: %s\n",
                     __FUNCTION__, strerror (errno));
            goto error;
        }
        zmsg_destroy (&zmsg);
        count++;
        if (count == zmqwriter_msgcount)
            flux_watcher_stop (w);
    }
    return;
error:
    flux_reactor_stop_error (r);
}
Exemple #8
0
static void zmqwriter (flux_reactor_t *r, flux_watcher_t *w,
                       int revents, void *arg)
{
    void *sock = flux_zmq_watcher_get_zsock (w);
    static int count = 0;
    if (revents & FLUX_POLLERR) {
        fprintf (stderr, "%s: FLUX_POLLERR is set\n", __FUNCTION__);
        goto error;
    }
    if (revents & FLUX_POLLOUT) {
        uint8_t blob[64];
        zmsg_t *zmsg = zmsg_new ();
        if (!zmsg || zmsg_addmem (zmsg, blob, sizeof (blob)) < 0) {
            fprintf (stderr, "%s: failed to create message: %s\n",
                     __FUNCTION__, strerror (errno));
            goto error;
        }
        if (zmsg_send (&zmsg, sock) < 0) {
            fprintf (stderr, "%s: zmsg_send: %s\n",
                     __FUNCTION__, strerror (errno));
            goto error;
        }
        count++;
        if (count == zmqwriter_msgcount)
            flux_watcher_stop (w);
    }
    return;
error:
    flux_reactor_stop_error (r);
}
Exemple #9
0
static void s_io_cb (flux_reactor_t *r, flux_watcher_t *w,
                     int revents, void *arg)
{
    struct context *ctx = arg;
    int *rfd, fd = flux_fd_watcher_get_fd (w);
    char *resp;
    int rc;

    if (dgetline (fd, ctx->buf, ctx->buflen) < 0) {
        diag ("dgetline: %s", strerror (errno));
        flux_reactor_stop_error (r);
        return;
    }
    rc = pmi_simple_server_request (ctx->pmi, ctx->buf, &ctx->fds[1]);
    if (rc < 0) {
        diag ("pmi_simple_server_request: %s", strerror (errno));
        flux_reactor_stop_error (r);
        return;
    }
    while (pmi_simple_server_response (ctx->pmi, &resp, &rfd) == 0) {
        if (dputline (*rfd, resp) < 0) {
            diag ("dputline: %s", strerror (errno));
            flux_reactor_stop_error (r);
            return;
        }
        free (resp);
    }
    if (rc == 1) {
        close (fd);
        flux_watcher_stop (w);
    }
}
Exemple #10
0
void heartbeat_stop (heartbeat_t *hb)
{
    if (hb->timer)
        flux_watcher_stop (hb->timer);
    if (hb->mh)
        flux_msg_handler_stop (hb->mh);
}
Exemple #11
0
void shutdown_disarm (shutdown_t *s)
{
    if (s->timer) {
        flux_watcher_stop (s->timer);
        flux_watcher_destroy (s->timer);
        s->timer = NULL;
    }
}
Exemple #12
0
void flux_msg_handler_stop (flux_msg_handler_t *w)
{
    struct dispatch *d = w->d;

    assert (w->magic == HANDLER_MAGIC);
    zlist_remove (d->waiters, w);
    zlist_remove (d->handlers, w);
    if (zlist_size (d->handlers) == 0)
        flux_watcher_stop (d->w);
}
Exemple #13
0
static void test_timer (flux_reactor_t *reactor)
{
    flux_watcher_t *w;

    errno = 0;
    ok (!flux_timer_watcher_create (reactor, -1, 0, oneshot, NULL)
        && errno == EINVAL,
        "timer: creating negative timeout fails with EINVAL");
    ok (!flux_timer_watcher_create (reactor, 0, -1, oneshot, NULL)
        && errno == EINVAL,
        "timer: creating negative repeat fails with EINVAL");
    ok ((w = flux_timer_watcher_create (reactor, 0, 0, oneshot, NULL)) != NULL,
        "timer: creating zero timeout works");
    flux_watcher_start (w);
    ok (flux_reactor_run (reactor, 0) == 0,
        "timer: reactor ran to completion (single oneshot)");
    ok (oneshot_ran == true,
        "timer: oneshot was executed");
    oneshot_ran = false;
    ok (flux_reactor_run (reactor, 0) == 0,
        "timer: reactor ran to completion (expired oneshot)");
    ok (oneshot_ran == false,
        "timer: expired oneshot was not re-executed");

    errno = 0;
    oneshot_errno = ESRCH;
    flux_watcher_start (w);
    ok (flux_reactor_run (reactor, 0) < 0 && errno == ESRCH,
        "general: reactor stop_error worked with errno passthru");
    flux_watcher_stop (w);
    flux_watcher_destroy (w);

    ok ((w = flux_timer_watcher_create (reactor, 0.01, 0.01, repeat, NULL))
        != NULL,
        "timer: creating 1ms timeout with 1ms repeat works");
    flux_watcher_start (w);
    ok (flux_reactor_run (reactor, 0) == 0,
        "timer: reactor ran to completion (single repeat)");
    ok (repeat_countdown == 0,
        "timer: repeat timer stopped itself after countdown");
    flux_watcher_stop (w);
    flux_watcher_destroy (w);
}
Exemple #14
0
static void ev_timer_cb (flux_reactor_t *r, flux_watcher_t *w,
                         int revents, void *arg)
{
    cron_entry_t *e = arg;
    struct cron_event *ev = cron_entry_type_data (e);
    cron_entry_schedule_task (e);
    flux_watcher_stop (w);
    flux_watcher_destroy (w);
    ev->paused = 0;
}
Exemple #15
0
/* Send a request each time the timer fires.
 * After 'ctx->count' requests have been sent, stop the watcher.
 */
void timer_cb (flux_reactor_t *r, flux_watcher_t *w, int revents, void *arg)
{
    struct ping_ctx *ctx = arg;

    send_ping (ctx);
    if (ctx->send_count == ctx->count)
        flux_watcher_stop (w);
    else if (ctx->period == 0.) { /* needs rearm if repeat is 0. */
        flux_timer_watcher_reset (w, ctx->period, ctx->period);
        flux_watcher_start (w);
    }
}
Exemple #16
0
static void zio_flux_writer_cb (flux_reactor_t *r, flux_watcher_t *w,
                                int revents, void *arg)
{
    zio_t *zio = arg;
    int rc;
    zio_handler_start (zio);
    rc = zio_writer_cb (zio);
    if (!zio_write_pending (zio))
        flux_watcher_stop (w);
    zio_handler_end (zio);
    if (rc < 0)
        flux_reactor_stop_error (r);
}
void check (flux_reactor_t *r, flux_watcher_t *w, int revents, void *arg)
{
    flux_t *h = arg;

    flux_watcher_stop (w_idle);

    if (txcount < totcount && (txcount - rxcount) < max_queue_depth) {
        flux_future_t *f;

        f = commit_int (h, key, txcount++);
        if (flux_future_then (f, -1.0, commit_continuation, NULL) < 0)
            log_err_exit ("flux_future_then");
    }
}
Exemple #18
0
static void test_fd (flux_reactor_t *reactor)
{
    int fd[2];
    flux_watcher_t *r, *w;

    ok (socketpair (PF_LOCAL, SOCK_STREAM, 0, fd) == 0
        && set_nonblock (fd[0]) == 0 && set_nonblock (fd[1]) == 0,
        "fd: successfully created non-blocking socketpair");
    r = flux_fd_watcher_create (reactor, fd[0], FLUX_POLLIN, fdreader, NULL);
    w = flux_fd_watcher_create (reactor, fd[1], FLUX_POLLOUT, fdwriter, NULL);
    ok (r != NULL && w != NULL,
        "fd: reader and writer created");
    flux_watcher_start (r);
    flux_watcher_start (w);
    ok (flux_reactor_run (reactor, 0) == 0,
        "fd: reactor ran to completion after %lu bytes", fdwriter_bufsize);
    flux_watcher_stop (r);
    flux_watcher_stop (w);
    flux_watcher_destroy (r);
    flux_watcher_destroy (w);
    close (fd[0]);
    close (fd[1]);
}
Exemple #19
0
static void zio_flux_read_cb (flux_reactor_t *r, flux_watcher_t *w,
                              int revents, void *arg)
{
    zio_t *zio = arg;
    int rc;
    zio_handler_start (zio);
    rc = zio_read_cb_common (zio);
    if (rc >= 0 && zio_eof_sent (zio)) {
        zio_debug (zio, "reader detaching from flux reactor\n");
        flux_watcher_stop (w);
        rc = zio_close (zio);
    }
    zio_handler_end (zio);
    if (rc < 0)
        flux_reactor_stop_error (r);
}
Exemple #20
0
static void module_destroy (module_t *p)
{
    assert (p->magic == MODULE_MAGIC);
    int errnum;

    if (p->t) {
        errnum = pthread_join (p->t, NULL);
        if (errnum)
            errn_exit (errnum, "pthread_join");
    }

    assert (p->h == NULL);

    flux_watcher_stop (p->broker_w);
    flux_watcher_destroy (p->broker_w);
    zsocket_destroy (p->zctx, p->sock);

    dlclose (p->dso);
    zuuid_destroy (&p->uuid);
    free (p->digest);
    if (p->argz)
        free (p->argz);
    if (p->name)
        free (p->name);
    if (p->rmmod_cb)
        p->rmmod_cb (p, p->rmmod_arg);
    if (p->rmmod) {
        flux_msg_t *msg;
        while ((msg = zlist_pop (p->rmmod)))
            flux_msg_destroy (msg);
    }
    if (p->subs) {
        char *s;
        while ((s = zlist_pop (p->subs)))
            free (s);
        zlist_destroy (&p->subs);
    }
    zlist_destroy (&p->rmmod);
    p->magic = ~MODULE_MAGIC;
    free (p);
}
Exemple #21
0
void fd_read (flux_reactor_t *r, flux_watcher_t *w, int revents, void *arg)
{
    struct counts *c = arg;
    char buf[64];
    int fd = flux_fd_watcher_get_fd (w);
    int n = -1;

    if ((revents & FLUX_POLLIN)) {
        n = read (fd, buf, sizeof (buf));
        if (n < 0)
            c->fd_read_errors++;
        else if (n == 0) {
            c->fd_read_eof++;
            flux_watcher_stop (w);
        } else if (n > 0)
            c->fd_read_data += n;
    }
    if ((revents & FLUX_POLLERR))
        c->fd_read_errors++;
    diag ("%s: %d", __FUNCTION__, n);
}
Exemple #22
0
static int hello_add_rank (hello_t *hello, uint32_t rank)
{
    uint32_t size;

    if (flux_get_size (hello->h, &size) < 0)
        return -1;
    if (!hello->nodeset)
        hello->nodeset = nodeset_create_size (size);
    if (!nodeset_add_rank (hello->nodeset, rank)) {
        errno = EPROTO;
        return -1;
    }
    hello->count++;
    if (hello->count == size) {
        if (hello->cb)
            hello->cb (hello, hello->cb_arg);
        if (hello->timer)
            flux_watcher_stop (hello->timer);
    }
    return 0;
}
Exemple #23
0
static void s_io_cb (flux_reactor_t *r, flux_watcher_t *w,
                     int revents, void *arg)
{
    struct context *ctx = arg;
    int fd = flux_fd_watcher_get_fd (w);
    int rc;

    if (dgetline (fd, ctx->buf, sizeof (ctx->buf)) < 0) {
        diag ("dgetline: %s", strerror (errno));
        flux_reactor_stop_error (r);
        return;
    }
    rc = pmi_simple_server_request (ctx->pmi, ctx->buf, &ctx->fds[1]);
    if (rc < 0) {
        diag ("pmi_simple_server_request: %s", strerror (errno));
        flux_reactor_stop_error (r);
        return;
    }
    if (rc == 1) {
        close (fd);
        flux_watcher_stop (w);
    }
}
Exemple #24
0
static void safe_stop_cb (struct ev_loop *loop, ev_prepare *pw, int revents)
{
    flux_watcher_stop ((flux_watcher_t *)pw->data);
    ev_prepare_stop (loop, pw);
    free (pw);
}