Skip to content

Commit

Permalink
fixes nanomsg#84 Consider using msec for durations
Browse files Browse the repository at this point in the history
There is now a public nng_duration type.  We have also updated the
zerotier work to work with the signed int64_t's that the latst ZeroTier
dev branch is using.
  • Loading branch information
gdamore committed Oct 20, 2017
1 parent d7e39a2 commit 4e668fd
Show file tree
Hide file tree
Showing 42 changed files with 395 additions and 465 deletions.
23 changes: 11 additions & 12 deletions perf/perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ do_inproc_lat(int argc, char **argv)
nni_thr_run(&thr);

// Sleep a bit.
nng_usleep(100000);
nng_msleep(100);

latency_client("inproc://latency_test", ia.msgsize, ia.count);
nni_thr_fini(&thr);
Expand Down Expand Up @@ -297,9 +297,9 @@ latency_client(const char *addr, int msgsize, int trips)
nni_msg_free(msg);
nng_close(s);

total = (float) (end - start);
latency = (total / (trips * 2));
printf("total time: %.3f [s]\n", total / 1000000.0);
total = (float) ((end - start)) / 1000;
latency = ((float) ((total * 1000000)) / (trips * 2));
printf("total time: %.3f [s]\n", total);
printf("message size: %d [B]\n", msgsize);
printf("round trip count: %d\n", trips);
printf("average latency: %.3f [us]\n", latency);
Expand Down Expand Up @@ -339,7 +339,7 @@ latency_server(const char *addr, int msgsize, int trips)

// Wait a bit for things to drain... linger should do this.
// 100ms ought to be enough.
nni_usleep(100000);
nng_msleep(100);
nng_close(s);
}

Expand All @@ -355,7 +355,7 @@ throughput_server(const char *addr, int msgsize, int count)
int rv;
int i;
uint64_t start, end;
double msgpersec, mbps, total;
float msgpersec, mbps, total;

if ((rv = nng_pair_open(&s)) != 0) {
die("nng_socket: %s", nng_strerror(rv));
Expand Down Expand Up @@ -391,11 +391,10 @@ throughput_server(const char *addr, int msgsize, int count)
}
end = nni_clock();
nng_close(s);
total = (end - start) / 1.0;
msgpersec = (count * 1000000.0) / total;
mbps = (count * 8.0 * msgsize);
mbps /= total;
printf("total time: %.3f [s]\n", total / 1000000.0);
total = (float) ((end - start)) / 1000;
msgpersec = (float) (count) / total;
mbps = (float) (msgpersec * 8 * msgsize) / (1024 * 1024);
printf("total time: %.3f [s]\n", total);
printf("message size: %d [B]\n", msgsize);
printf("message count: %d\n", count);
printf("throughput: %.f [msg/s]\n", msgpersec);
Expand Down Expand Up @@ -447,6 +446,6 @@ throughput_client(const char *addr, int msgsize, int count)
}

// Wait 100msec for pipes to drain.
nni_usleep(100000);
nng_msleep(100);
nng_close(s);
}
6 changes: 3 additions & 3 deletions src/core/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ nni_clock(void)
}

void
nni_usleep(nni_duration usec)
nni_msleep(nni_duration msec)
{
nni_plat_usleep(usec);
}
nni_plat_sleep(msec);
}
3 changes: 2 additions & 1 deletion src/core/clock.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright 2017 Garrett D'Amore <[email protected]>
// Copyright 2017 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
Expand All @@ -14,6 +15,6 @@

extern nni_time nni_clock(void);

extern void nni_usleep(nni_duration usec);
extern void nni_msleep(nni_duration);

#endif // CORE_CLOCK_H
6 changes: 3 additions & 3 deletions src/core/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ typedef struct nni_thr nni_thr;
typedef void (*nni_thr_func)(void *);

typedef int nni_signal; // Wakeup channel.
typedef uint64_t nni_time; // Abs. time (usec).
typedef int64_t nni_duration; // Rel. time (usec).
typedef uint64_t nni_time; // Abs. time (ms).
typedef int32_t nni_duration; // Rel. time (ms).

typedef struct nni_aio nni_aio;

Expand All @@ -76,7 +76,7 @@ typedef struct {
// Some default timing things.
#define NNI_TIME_NEVER ((nni_time) -1)
#define NNI_TIME_ZERO ((nni_time) 0)
#define NNI_SECOND (1000000)
#define NNI_SECOND (1000)

// Structure allocation conveniences.
#define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof(*s))
Expand Down
2 changes: 1 addition & 1 deletion src/core/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ nni_device(nni_sock *sock1, nni_sock *sock2)
{
nni_device_pair pair;
int rv;
nni_time never = NNI_TIME_NEVER;
nni_duration never = -1;
size_t sz;

memset(&pair, 0, sizeof(pair));
Expand Down
6 changes: 3 additions & 3 deletions src/core/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string.h>

int
nni_chkopt_usec(const void *v, size_t sz)
nni_chkopt_ms(const void *v, size_t sz)
{
nni_duration val;
if (sz != sizeof(val)) {
Expand Down Expand Up @@ -56,7 +56,7 @@ nni_chkopt_size(const void *v, size_t sz, size_t minv, size_t maxv)
}

int
nni_setopt_usec(nni_duration *dp, const void *v, size_t sz)
nni_setopt_ms(nni_duration *dp, const void *v, size_t sz)
{
nni_duration dur;

Expand Down Expand Up @@ -110,7 +110,7 @@ nni_setopt_size(size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv)
}

int
nni_getopt_usec(nni_duration u, void *val, size_t *sizep)
nni_getopt_ms(nni_duration u, void *val, size_t *sizep)
{
size_t sz = sizeof(u);

Expand Down
6 changes: 3 additions & 3 deletions src/core/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ extern int nni_getopt_buf(nni_msgq *, void *, size_t *);

// nni_setopt_duration sets the duration. Durations must be legal,
// either a positive value, 0, or -1 to indicate forever.
extern int nni_setopt_usec(nni_duration *, const void *, size_t);
extern int nni_setopt_ms(nni_duration *, const void *, size_t);

// nni_getopt_duration gets the duration.
extern int nni_getopt_usec(nni_duration, void *, size_t *);
extern int nni_getopt_ms(nni_duration, void *, size_t *);

// nni_setopt_int sets an integer, which must be between the minimum and
// maximum values (inclusive).
Expand Down Expand Up @@ -61,7 +61,7 @@ extern int nni_getopt_size(size_t, void *, size_t *);
// nni_getopt_fd obtains a notification file descriptor.
extern int nni_getopt_fd(nni_sock *, nni_notifyfd *, int, void *, size_t *);

extern int nni_chkopt_usec(const void *, size_t);
extern int nni_chkopt_ms(const void *, size_t);
extern int nni_chkopt_int(const void *, size_t, int, int);
extern int nni_chkopt_size(const void *, size_t, size_t, size_t);

Expand Down
4 changes: 2 additions & 2 deletions src/core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ extern void nni_plat_thr_fini(nni_plat_thr *);
// of using negative values for other purposes in the future.)
extern nni_time nni_plat_clock(void);

// nni_plat_usleep sleeps for the specified number of microseconds (at least).
extern void nni_plat_usleep(nni_duration);
// nni_plat_sleep sleeps for the specified number of milliseconds (at least).
extern void nni_plat_sleep(nni_duration);

//
// Entropy Support
Expand Down
24 changes: 12 additions & 12 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,49 +95,49 @@ nni_sock_getopt_recvfd(nni_sock *s, void *buf, size_t *szp)
static int
nni_sock_setopt_recvtimeo(nni_sock *s, const void *buf, size_t sz)
{
return (nni_setopt_usec(&s->s_rcvtimeo, buf, sz));
return (nni_setopt_ms(&s->s_rcvtimeo, buf, sz));
}

static int
nni_sock_getopt_recvtimeo(nni_sock *s, void *buf, size_t *szp)
{
return (nni_getopt_usec(s->s_rcvtimeo, buf, szp));
return (nni_getopt_ms(s->s_rcvtimeo, buf, szp));
}

static int
nni_sock_setopt_sendtimeo(nni_sock *s, const void *buf, size_t sz)
{
return (nni_setopt_usec(&s->s_sndtimeo, buf, sz));
return (nni_setopt_ms(&s->s_sndtimeo, buf, sz));
}

static int
nni_sock_getopt_sendtimeo(nni_sock *s, void *buf, size_t *szp)
{
return (nni_getopt_usec(s->s_sndtimeo, buf, szp));
return (nni_getopt_ms(s->s_sndtimeo, buf, szp));
}

static int
nni_sock_setopt_reconnmint(nni_sock *s, const void *buf, size_t sz)
{
return (nni_setopt_usec(&s->s_reconn, buf, sz));
return (nni_setopt_ms(&s->s_reconn, buf, sz));
}

static int
nni_sock_getopt_reconnmint(nni_sock *s, void *buf, size_t *szp)
{
return (nni_getopt_usec(s->s_reconn, buf, szp));
return (nni_getopt_ms(s->s_reconn, buf, szp));
}

static int
nni_sock_setopt_reconnmaxt(nni_sock *s, const void *buf, size_t sz)
{
return (nni_setopt_usec(&s->s_reconnmax, buf, sz));
return (nni_setopt_ms(&s->s_reconnmax, buf, sz));
}

static int
nni_sock_getopt_reconnmaxt(nni_sock *s, void *buf, size_t *szp)
{
return (nni_getopt_usec(s->s_reconnmax, buf, szp));
return (nni_getopt_ms(s->s_reconnmax, buf, szp));
}

static int
Expand Down Expand Up @@ -500,8 +500,8 @@ nni_sock_create(nni_sock **sp, const nni_proto *proto)
return (NNG_ENOMEM);
}
s->s_linger = 0;
s->s_sndtimeo = NNI_TIME_NEVER;
s->s_rcvtimeo = NNI_TIME_NEVER;
s->s_sndtimeo = -1;
s->s_rcvtimeo = -1;
s->s_closing = 0;
s->s_reconn = NNI_SECOND;
s->s_reconnmax = 0;
Expand Down Expand Up @@ -1026,7 +1026,7 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *val, size_t size)
// was found, or even if a transport rejected one of the settings.
if ((rv == NNG_ENOTSUP) || (rv == 0)) {
if ((strcmp(name, NNG_OPT_LINGER) == 0)) {
rv = nni_chkopt_usec(val, size);
rv = nni_chkopt_ms(val, size);
} else if (strcmp(name, NNG_OPT_RECVMAXSZ) == 0) {
// just a sanity test on the size; it also ensures that
// a size can be set even with no transport configured.
Expand Down Expand Up @@ -1090,7 +1090,7 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *val, size_t size)
// will already have had a chance to veto this.

if (strcmp(name, NNG_OPT_LINGER) == 0) {
rv = nni_setopt_usec(&s->s_linger, val, size);
rv = nni_setopt_ms(&s->s_linger, val, size);
}

if (rv == 0) {
Expand Down
30 changes: 17 additions & 13 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ nng_dialer_setopt_size(nng_dialer id, const char *name, size_t val)
}

int
nng_dialer_setopt_usec(nng_dialer id, const char *name, uint64_t val)
nng_dialer_setopt_ms(nng_dialer id, const char *name, nng_duration val)
{
return (nng_dialer_setopt(id, name, &val, sizeof(val)));
}
Expand Down Expand Up @@ -428,9 +428,10 @@ nng_dialer_getopt_uint64(nng_dialer id, const char *name, uint64_t *valp)
}

int
nng_dialer_getopt_usec(nng_dialer id, const char *name, uint64_t *valp)
nng_dialer_getopt_ms(nng_dialer id, const char *name, nng_duration *valp)
{
return (nng_dialer_getopt_uint64(id, name, valp));
size_t sz = sizeof(*valp);
return (nng_dialer_getopt(id, name, valp, &sz));
}

int
Expand All @@ -453,7 +454,7 @@ nng_listener_setopt_size(nng_listener id, const char *name, size_t val)
}

int
nng_listener_setopt_usec(nng_listener id, const char *name, uint64_t val)
nng_listener_setopt_ms(nng_listener id, const char *name, nng_duration val)
{
return (nng_listener_setopt(id, name, &val, sizeof(val)));
}
Expand Down Expand Up @@ -492,9 +493,10 @@ nng_listener_getopt_uint64(nng_listener id, const char *name, uint64_t *valp)
}

int
nng_listener_getopt_usec(nng_listener id, const char *name, uint64_t *valp)
nng_listener_getopt_ms(nng_listener id, const char *name, nng_duration *valp)
{
return (nng_listener_getopt_uint64(id, name, valp));
size_t sz = sizeof(*valp);
return (nng_listener_getopt(id, name, valp, &sz));
}

static int
Expand Down Expand Up @@ -570,7 +572,7 @@ nng_setopt_size(nng_socket sid, const char *name, size_t val)
}

int
nng_setopt_usec(nng_socket sid, const char *name, uint64_t val)
nng_setopt_ms(nng_socket sid, const char *name, nng_duration val)
{
return (nng_setopt(sid, name, &val, sizeof(val)));
}
Expand Down Expand Up @@ -603,9 +605,10 @@ nng_getopt_uint64(nng_socket sid, const char *name, uint64_t *valp)
}

int
nng_getopt_usec(nng_socket sid, const char *name, uint64_t *valp)
nng_getopt_ms(nng_socket sid, const char *name, nng_duration *valp)
{
return (nng_getopt_uint64(sid, name, valp));
size_t sz = sizeof(*valp);
return (nng_getopt(sid, name, valp, &sz));
}

nng_notify *
Expand Down Expand Up @@ -775,9 +778,10 @@ nng_pipe_getopt_uint64(nng_pipe id, const char *name, uint64_t *valp)
}

int
nng_pipe_getopt_usec(nng_pipe id, const char *name, uint64_t *valp)
nng_pipe_getopt_ms(nng_pipe id, const char *name, nng_duration *valp)
{
return (nng_pipe_getopt_uint64(id, name, valp));
size_t sz = sizeof(*valp);
return (nng_pipe_getopt(id, name, valp, &sz));
}

int
Expand Down Expand Up @@ -1042,9 +1046,9 @@ nng_stat_value(nng_stat *stat)
// API, and applications should refrain from their use.

void
nng_usleep(uint64_t usec)
nng_msleep(nng_duration ms)
{
nni_usleep(usec);
nni_msleep(ms);
}

uint64_t
Expand Down
Loading

0 comments on commit 4e668fd

Please sign in to comment.