Пример #1
0
size_t zmq_msg_size (zmq_msg_t *msg)
{
    return xs_msg_size ((xs_msg_t*) msg->content);
}
Пример #2
0
int main (int argc, char *argv [])
{
    const char *bind_to;
    int message_count;
    size_t message_size;
    void *ctx;
    void *s;
    int rc;
    int i;
    xs_msg_t msg;
    void *watch;
    unsigned long elapsed;
    unsigned long throughput;
    double megabits;

    if (argc != 4) {
        printf ("usage: local_thr <bind-to> <message-size> <message-count>\n");
        return 1;
    }
    bind_to = argv [1];
    message_size = atoi (argv [2]);
    message_count = atoi (argv [3]);

    ctx = xs_init ();
    if (!ctx) {
        printf ("error in xs_init: %s\n", xs_strerror (errno));
        return -1;
    }

    s = xs_socket (ctx, XS_PULL);
    if (!s) {
        printf ("error in xs_socket: %s\n", xs_strerror (errno));
        return -1;
    }

    //  Add your socket options here.
    //  For example XS_RATE, XS_RECOVERY_IVL and XS_MCAST_LOOP for PGM.

    rc = xs_bind (s, bind_to);
    if (rc == -1) {
        printf ("error in xs_bind: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_msg_init (&msg);
    if (rc != 0) {
        printf ("error in xs_msg_init: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_recvmsg (s, &msg, 0);
    if (rc < 0) {
        printf ("error in xs_recvmsg: %s\n", xs_strerror (errno));
        return -1;
    }
    if (xs_msg_size (&msg) != message_size) {
        printf ("message of incorrect size received\n");
        return -1;
    }

    watch = xs_stopwatch_start ();

    for (i = 0; i != message_count - 1; i++) {
        rc = xs_recvmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in xs_recvmsg: %s\n", xs_strerror (errno));
            return -1;
        }
        if (xs_msg_size (&msg) != message_size) {
            printf ("message of incorrect size received\n");
            return -1;
        }
    }

    elapsed = xs_stopwatch_stop (watch);
    if (elapsed == 0)
        elapsed = 1;

    rc = xs_msg_close (&msg);
    if (rc != 0) {
        printf ("error in xs_msg_close: %s\n", xs_strerror (errno));
        return -1;
    }

    throughput = (unsigned long)
        ((double) message_count / (double) elapsed * 1000000);
    megabits = (double) (throughput * message_size * 8) / 1000000;

    printf ("message size: %d [B]\n", (int) message_size);
    printf ("message count: %d\n", (int) message_count);
    printf ("mean throughput: %d [msg/s]\n", (int) throughput);
    printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits);

    rc = xs_close (s);
    if (rc != 0) {
        printf ("error in xs_close: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_term (ctx);
    if (rc != 0) {
        printf ("error in xs_term: %s\n", xs_strerror (errno));
        return -1;
    }

    return 0;
}
Пример #3
0
nmsg_res
_input_nmsg_read_container_xs(nmsg_input_t input, Nmsg__Nmsg **nmsg) {
	int ret;
	nmsg_res res;
	uint8_t *buf;
	size_t buf_len;
	ssize_t msgsize = 0;
	xs_msg_t xmsg;
	xs_pollitem_t xitems[1];

	/* poll */
	xitems[0].socket = input->stream->xs;
	xitems[0].events = XS_POLLIN;
	ret = xs_poll(xitems, 1, NMSG_RBUF_TIMEOUT);
	if (ret == 0 || (ret == -1 && errno == EINTR))
		return (nmsg_res_again);
	else if (ret == -1)
		return (nmsg_res_read_failure);

	/* initialize XS message object */
	if (xs_msg_init(&xmsg))
		return (nmsg_res_failure);

	/* read the NMSG container */
	if (xs_recvmsg(input->stream->xs, &xmsg, 0) == -1) {
		res = nmsg_res_failure;
		goto out;
	}
	nmsg_timespec_get(&input->stream->now);

	/* get buffer from the XS message */
	buf = xs_msg_data(&xmsg);
	buf_len = xs_msg_size(&xmsg);
	if (buf_len < NMSG_HDRLSZ_V2) {
		res = nmsg_res_failure;
		goto out;
	}

	/* deserialize the NMSG header */
	res = _input_nmsg_deserialize_header(buf, buf_len, &msgsize, &input->stream->flags);
	if (res != nmsg_res_success)
		goto out;
	buf += NMSG_HDRLSZ_V2;

	/* the entire message must have been read by xs_recvmsg() */
	assert((size_t) msgsize == buf_len - NMSG_HDRLSZ_V2);

	/* unpack message */
	res = _input_nmsg_unpack_container(input, nmsg, buf, msgsize);

	/* update seqsrc counts */
	if (input->stream->verify_seqsrc && *nmsg != NULL) {
		struct nmsg_seqsrc *seqsrc = _input_seqsrc_get(input, *nmsg);
		if (seqsrc != NULL)
			_input_seqsrc_update(input, seqsrc, *nmsg);
	}

	/* expire old outstanding fragments */
	_input_frag_gc(input->stream);

out:
	xs_msg_close(&xmsg);
	return (res);
}
Пример #4
0
int main (int argc, char *argv [])
{
#if defined XS_HAVE_WINDOWS
    HANDLE local_thread;
#else
    pthread_t local_thread;
#endif
    void *ctx;
    void *s;
    int rc;
    int i;
    xs_msg_t msg;
    void *watch;
    unsigned long elapsed;
    double latency;

    if (argc != 3) {
        printf ("usage: inproc_lat <message-size> <roundtrip-count>\n");
        return 1;
    }

    message_size = atoi (argv [1]);
    roundtrip_count = atoi (argv [2]);

    ctx = xs_init ();
    if (!ctx) {
        printf ("error in xs_init: %s\n", xs_strerror (errno));
        return -1;
    }

    s = xs_socket (ctx, XS_REQ);
    if (!s) {
        printf ("error in xs_socket: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_bind (s, "inproc://lat_test");
    if (rc == -1) {
        printf ("error in xs_bind: %s\n", xs_strerror (errno));
        return -1;
    }

#if defined XS_HAVE_WINDOWS
    local_thread = (HANDLE) _beginthreadex (NULL, 0,
        worker, ctx, 0 , NULL);
    if (local_thread == 0) {
        printf ("error in _beginthreadex\n");
        return -1;
    }
#else
    rc = pthread_create (&local_thread, NULL, worker, ctx);
    if (rc != 0) {
        printf ("error in pthread_create: %s\n", xs_strerror (rc));
        return -1;
    }
#endif

    rc = xs_msg_init_size (&msg, message_size);
    if (rc != 0) {
        printf ("error in xs_msg_init_size: %s\n", xs_strerror (errno));
        return -1;
    }
    memset (xs_msg_data (&msg), 0, message_size);

    printf ("message size: %d [B]\n", (int) message_size);
    printf ("roundtrip count: %d\n", (int) roundtrip_count);

    watch = xs_stopwatch_start ();

    for (i = 0; i != roundtrip_count; i++) {
        rc = xs_sendmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in xs_sendmsg: %s\n", xs_strerror (errno));
            return -1;
        }
        rc = xs_recvmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in xs_recvmsg: %s\n", xs_strerror (errno));
            return -1;
        }
        if (xs_msg_size (&msg) != message_size) {
            printf ("message of incorrect size received\n");
            return -1;
        }
    }

    elapsed = xs_stopwatch_stop (watch);

    rc = xs_msg_close (&msg);
    if (rc != 0) {
        printf ("error in xs_msg_close: %s\n", xs_strerror (errno));
        return -1;
    }

    latency = (double) elapsed / (roundtrip_count * 2);

#if defined XS_HAVE_WINDOWS
    DWORD rc2 = WaitForSingleObject (local_thread, INFINITE);
    if (rc2 == WAIT_FAILED) {
        printf ("error in WaitForSingleObject\n");
        return -1;
    }
    BOOL rc3 = CloseHandle (local_thread);
    if (rc3 == 0) {
        printf ("error in CloseHandle\n");
        return -1;
    }
#else
    rc = pthread_join (local_thread, NULL);
    if (rc != 0) {
        printf ("error in pthread_join: %s\n", xs_strerror (rc));
        return -1;
    }
#endif

    printf ("average latency: %.3f [us]\n", (double) latency);

    rc = xs_close (s);
    if (rc != 0) {
        printf ("error in xs_close: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_term (ctx);
    if (rc != 0) {
        printf ("error in xs_term: %s\n", xs_strerror (errno));
        return -1;
    }

    return 0;
}
Пример #5
0
int main (int argc, char *argv [])
{
#if defined XS_HAVE_WINDOWS
    HANDLE local_thread;
#else
    pthread_t local_thread;
#endif
    void *ctx;
    void *s;
    int rc;
    int i;
    xs_msg_t msg;
    void *watch;
    unsigned long elapsed;
    unsigned long throughput;
    double megabits;

    if (argc != 3) {
        printf ("usage: thread_thr <message-size> <message-count>\n");
        return 1;
    }

    message_size = atoi (argv [1]);
    message_count = atoi (argv [2]);

    ctx = xs_init ();
    if (!ctx) {
        printf ("error in xs_init: %s\n", xs_strerror (errno));
        return -1;
    }

    s = xs_socket (ctx, XS_PULL);
    if (!s) {
        printf ("error in xs_socket: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_bind (s, "inproc://thr_test");
    if (rc == -1) {
        printf ("error in xs_bind: %s\n", xs_strerror (errno));
        return -1;
    }

#if defined XS_HAVE_WINDOWS
    local_thread = (HANDLE) _beginthreadex (NULL, 0,
        worker, ctx, 0 , NULL);
    if (local_thread == 0) {
        printf ("error in _beginthreadex\n");
        return -1;
    }
#else
    rc = pthread_create (&local_thread, NULL, worker, ctx);
    if (rc != 0) {
        printf ("error in pthread_create: %s\n", xs_strerror (rc));
        return -1;
    }
#endif

    rc = xs_msg_init (&msg);
    if (rc != 0) {
        printf ("error in xs_msg_init: %s\n", xs_strerror (errno));
        return -1;
    }

    printf ("message size: %d [B]\n", (int) message_size);
    printf ("message count: %d\n", (int) message_count);

    rc = xs_recvmsg (s, &msg, 0);
    if (rc < 0) {
        printf ("error in xs_recvmsg: %s\n", xs_strerror (errno));
        return -1;
    }
    if (xs_msg_size (&msg) != message_size) {
        printf ("message of incorrect size received\n");
        return -1;
    }

    watch = xs_stopwatch_start ();

    for (i = 0; i != message_count - 1; i++) {
        rc = xs_recvmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in xs_recvmsg: %s\n", xs_strerror (errno));
            return -1;
        }
        if (xs_msg_size (&msg) != message_size) {
            printf ("message of incorrect size received\n");
            return -1;
        }
    }

    elapsed = xs_stopwatch_stop (watch);
    if (elapsed == 0)
        elapsed = 1;

    rc = xs_msg_close (&msg);
    if (rc != 0) {
        printf ("error in xs_msg_close: %s\n", xs_strerror (errno));
        return -1;
    }

#if defined XS_HAVE_WINDOWS
    DWORD rc2 = WaitForSingleObject (local_thread, INFINITE);
    if (rc2 == WAIT_FAILED) {
        printf ("error in WaitForSingleObject\n");
        return -1;
    }
    BOOL rc3 = CloseHandle (local_thread);
    if (rc3 == 0) {
        printf ("error in CloseHandle\n");
        return -1;
    }
#else
    rc = pthread_join (local_thread, NULL);
    if (rc != 0) {
        printf ("error in pthread_join: %s\n", xs_strerror (rc));
        return -1;
    }
#endif

    rc = xs_close (s);
    if (rc != 0) {
        printf ("error in xs_close: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_term (ctx);
    if (rc != 0) {
        printf ("error in xs_term: %s\n", xs_strerror (errno));
        return -1;
    }

    throughput = (unsigned long)
        ((double) message_count / (double) elapsed * 1000000);
    megabits = (double) (throughput * message_size * 8) / 1000000;

    printf ("mean throughput: %d [msg/s]\n", (int) throughput);
    printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits);

    return 0;
}
Пример #6
0
int xs_instream_recv (xs_instream *self, xs_msg *msg)
{
    int rc;
    ssize_t nbytes;

    /*  If there's a message available, return is straight away. */
    if (self->state == 2) {
        xs_msg_move (msg, &self->msg);
        self->state = 0;
        return 0;
    }

    /*  If message is being asynchronously received at the moment, there's
        no message to return yet. */
    if (self->state == 1)
        return -EINPROGRESS;

    /* Try to read the message in a synchronous way. */
    nbytes = recv (self->fd, self->sizebuf, 8, MSG_DONTWAIT);

    /*  Sanitation of the result. */
    if (unlikely (nbytes == 0)) {
        errno = ECONNREFUSED;
        nbytes = -1;
    }
    else if (unlikely (nbytes == -1 &&
          (errno == EAGAIN || errno == EWOULDBLOCK)))
        nbytes = 0;

    errno_assert (nbytes >= 0);

    if (nbytes < 8) {
        self->recvd = nbytes;
        /*  TODO: xs_instream_launch_async (self); */
        return -EINPROGRESS;
    }

    rc = xs_msg_init (&self->msg, xs_getll (self->sizebuf));
    err_assert (rc);

    nbytes = recv (self->fd, xs_msg_data (&self->msg), xs_msg_size (&self->msg),
        MSG_DONTWAIT);

    /*  Sanitation of the result. */
    if (unlikely (nbytes == 0)) {
        errno = ECONNREFUSED;
        nbytes = -1;
    }
    else if (unlikely (nbytes == -1 &&
          (errno == EAGAIN || errno == EWOULDBLOCK)))
        nbytes = 0;

    if (nbytes < xs_msg_size (&self->msg)) {
        self->recvd = nbytes + 8;
        /*  TODO: xs_instream_launch_async (self); */
        return -EINPROGRESS;
    }

    /*  Synchronous receive was successful. */
    xs_msg_move (msg, &self->msg);
    return 0;
}
Пример #7
0
int main (int argc, char *argv [])
{
    const char *connect_to;
    int roundtrip_count;
    size_t message_size;
    void *ctx;
    void *s;
    int rc;
    int i;
    xs_msg_t msg;
    void *watch;
    unsigned long elapsed;
    double latency;

    if (argc != 4) {
        printf ("usage: remote_lat <connect-to> <message-size> "
            "<roundtrip-count>\n");
        return 1;
    }
    connect_to = argv [1];
    message_size = atoi (argv [2]);
    roundtrip_count = atoi (argv [3]);

    ctx = xs_init ();
    if (!ctx) {
        printf ("error in xs_init: %s\n", xs_strerror (errno));
        return -1;
    }

    s = xs_socket (ctx, XS_REQ);
    if (!s) {
        printf ("error in xs_socket: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_connect (s, connect_to);
    if (rc == -1) {
        printf ("error in xs_connect: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_msg_init_size (&msg, message_size);
    if (rc != 0) {
        printf ("error in xs_msg_init_size: %s\n", xs_strerror (errno));
        return -1;
    }
    memset (xs_msg_data (&msg), 0, message_size);

    watch = xs_stopwatch_start ();

    for (i = 0; i != roundtrip_count; i++) {
        rc = xs_sendmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in xs_sendmsg: %s\n", xs_strerror (errno));
            return -1;
        }
        rc = xs_recvmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in xs_recvmsg: %s\n", xs_strerror (errno));
            return -1;
        }
        if (xs_msg_size (&msg) != message_size) {
            printf ("message of incorrect size received\n");
            return -1;
        }
    }

    elapsed = xs_stopwatch_stop (watch);

    rc = xs_msg_close (&msg);
    if (rc != 0) {
        printf ("error in xs_msg_close: %s\n", xs_strerror (errno));
        return -1;
    }

    latency = (double) elapsed / (roundtrip_count * 2);

    printf ("message size: %d [B]\n", (int) message_size);
    printf ("roundtrip count: %d\n", (int) roundtrip_count);
    printf ("average latency: %.3f [us]\n", (double) latency);

    rc = xs_close (s);
    if (rc != 0) {
        printf ("error in xs_close: %s\n", xs_strerror (errno));
        return -1;
    }

    rc = xs_term (ctx);
    if (rc != 0) {
        printf ("error in xs_term: %s\n", xs_strerror (errno));
        return -1;
    }

    return 0;
}