aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/pipe.c78
-rw-r--r--src/core/socket.c10
-rw-r--r--src/core/sockimpl.h5
-rw-r--r--src/core/stats.c22
-rw-r--r--src/core/stats.h8
5 files changed, 69 insertions, 54 deletions
diff --git a/src/core/pipe.c b/src/core/pipe.c
index d623e158..96fb6c7d 100644
--- a/src/core/pipe.c
+++ b/src/core/pipe.c
@@ -179,35 +179,14 @@ nni_pipe_peer(nni_pipe *p)
return (p->p_tran_ops.p_peer(p->p_tran_data));
}
-void
-nni_pipe_stats_init(nni_pipe *p)
-{
-#ifdef NNG_ENABLE_STATS
- nni_pipe_stats *st = &p->p_stats;
-
- if (p->p_listener != NULL) {
- st->s_ep_id.si_name = "listener";
- st->s_ep_id.si_desc = "listener for pipe";
- st->s_ep_id.si_value = nni_listener_id(p->p_listener);
- } else {
- st->s_ep_id.si_name = "dialer";
- st->s_ep_id.si_desc = "dialer for pipe";
- st->s_ep_id.si_value = nni_dialer_id(p->p_dialer);
- }
-
- nni_stat_append(NULL, &st->s_root);
-#else
- NNI_ARG_UNUSED(p);
-#endif
-}
-
-int
-nni_pipe_create(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata)
+static int
+pipe_create(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata)
{
nni_pipe * p;
int rv;
void * sdata = nni_sock_proto_data(sock);
nni_proto_pipe_ops *pops = nni_sock_proto_pipe_ops(sock);
+ nni_pipe_stats * st;
if ((p = NNI_ALLOC_STRUCT(p)) == NULL) {
// In this case we just toss the pipe...
@@ -215,7 +194,6 @@ nni_pipe_create(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata)
return (NNG_ENOMEM);
}
- // Make a private copy of the transport ops.
p->p_tran_ops = *tran->tran_pipe;
p->p_tran_data = tdata;
p->p_proto_ops = *pops;
@@ -224,6 +202,7 @@ nni_pipe_create(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata)
p->p_closed = false;
p->p_cbs = false;
p->p_refcnt = 0;
+ st = &p->p_stats;
nni_atomic_flag_reset(&p->p_stop);
NNI_LIST_NODE_INIT(&p->p_sock_node);
@@ -238,7 +217,6 @@ nni_pipe_create(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata)
}
nni_mtx_unlock(&nni_pipe_lk);
- nni_pipe_stats *st = &p->p_stats;
snprintf(st->s_scope, sizeof(st->s_scope), "pipe%u", p->p_id);
nni_stat_init_scope(&st->s_root, st->s_scope, "pipe statistics");
@@ -246,15 +224,11 @@ nni_pipe_create(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata)
nni_stat_init_id(&st->s_id, "id", "pipe id", p->p_id);
nni_stat_append(&st->s_root, &st->s_id);
- // name and description fleshed out later.
- nni_stat_init_id(&st->s_ep_id, "", "", 0);
- nni_stat_append(&st->s_root, &st->s_ep_id);
-
nni_stat_init_id(&st->s_sock_id, "socket", "socket for pipe",
nni_sock_id(p->p_sock));
nni_stat_append(&st->s_root, &st->s_sock_id);
- if ((rv != 0) || ((rv = tran->tran_pipe->p_init(tdata, p)) != 0) ||
+ if ((rv != 0) || ((rv = p->p_tran_ops.p_init(tdata, p)) != 0) ||
((rv = pops->pipe_init(&p->p_proto_data, p, sdata)) != 0)) {
nni_pipe_close(p);
nni_pipe_rele(p);
@@ -266,6 +240,48 @@ nni_pipe_create(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata)
}
int
+nni_pipe_create_dialer(nni_pipe **pp, nni_dialer *d, void *tdata)
+{
+ int rv;
+ nni_tran * tran = d->d_tran;
+ uint64_t id = nni_dialer_id(d);
+ nni_pipe * p;
+ nni_stat_item *st;
+
+ if ((rv = pipe_create(&p, d->d_sock, tran, tdata)) != 0) {
+ return (rv);
+ }
+ st = &p->p_stats.s_ep_id;
+ p->p_dialer = d;
+ nni_stat_init_id(st, "dialer", "dialer for pipe", id);
+ nni_pipe_add_stat(p, st);
+ nni_stat_append(NULL, &p->p_stats.s_root);
+ *pp = p;
+ return (0);
+}
+
+int
+nni_pipe_create_listener(nni_pipe **pp, nni_listener *l, void *tdata)
+{
+ int rv;
+ nni_tran * tran = l->l_tran;
+ uint64_t id = nni_listener_id(l);
+ nni_pipe * p;
+ nni_stat_item *st;
+
+ if ((rv = pipe_create(&p, l->l_sock, tran, tdata)) != 0) {
+ return (rv);
+ }
+ st = &p->p_stats.s_ep_id;
+ p->p_listener = l;
+ nni_stat_init_id(st, "listener", "listener for pipe", id);
+ nni_pipe_add_stat(p, st);
+ nni_stat_append(NULL, &p->p_stats.s_root);
+ *pp = p;
+ return (0);
+}
+
+int
nni_pipe_getopt(
nni_pipe *p, const char *name, void *val, size_t *szp, nni_opt_type t)
{
diff --git a/src/core/socket.c b/src/core/socket.c
index 63696b36..22fa5c07 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -1472,12 +1472,11 @@ nni_dialer_add_pipe(nni_dialer *d, void *tpipe)
nni_mtx_lock(&s->s_mx);
if (s->s_closed || d->d_closing ||
- (nni_pipe_create(&p, s, d->d_tran, tpipe) != 0)) {
+ (nni_pipe_create_dialer(&p, d, tpipe) != 0)) {
nni_mtx_unlock(&s->s_mx);
return;
}
- p->p_dialer = d;
nni_list_append(&d->d_pipes, p);
nni_list_append(&s->s_pipes, p);
d->d_pipe = p;
@@ -1486,8 +1485,6 @@ nni_dialer_add_pipe(nni_dialer *d, void *tpipe)
nni_stat_inc_atomic(&s->s_stats.s_npipes, 1);
nni_stat_inc_atomic(&d->d_stats.s_npipes, 1);
- nni_pipe_stats_init(p);
-
nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_PRE);
nni_mtx_lock(&s->s_mx);
@@ -1586,20 +1583,17 @@ nni_listener_add_pipe(nni_listener *l, void *tpipe)
nni_mtx_lock(&s->s_mx);
if (s->s_closed || l->l_closing ||
- (nni_pipe_create(&p, s, l->l_tran, tpipe) != 0)) {
+ (nni_pipe_create_listener(&p, l, tpipe) != 0)) {
nni_mtx_unlock(&s->s_mx);
return;
}
- p->p_listener = l;
nni_list_append(&l->l_pipes, p);
nni_list_append(&s->s_pipes, p);
nni_mtx_unlock(&s->s_mx);
nni_stat_inc_atomic(&l->l_stats.s_npipes, 1);
nni_stat_inc_atomic(&s->s_stats.s_npipes, 1);
- nni_pipe_stats_init(p);
-
nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_PRE);
nni_mtx_lock(&s->s_mx);
diff --git a/src/core/sockimpl.h b/src/core/sockimpl.h
index ae1fd92e..5a9a2589 100644
--- a/src/core/sockimpl.h
+++ b/src/core/sockimpl.h
@@ -139,8 +139,9 @@ extern void nni_listener_close_rele(nni_listener *);
extern void nni_pipe_remove(nni_pipe *);
extern void nni_pipe_run_cb(nni_pipe *, nng_pipe_ev);
-extern int nni_pipe_create(nni_pipe **, nni_sock *, nni_tran *, void *);
+extern int nni_pipe_create_dialer(nni_pipe **, nni_dialer *, void *);
+extern int nni_pipe_create_listener(nni_pipe **, nni_listener *, void *);
+
extern void nni_pipe_start(nni_pipe *);
-extern void nni_pipe_stats_init(nni_pipe *);
#endif // CORE_SOCKIMPL_H
diff --git a/src/core/stats.c b/src/core/stats.c
index 832bb1da..ffe6659c 100644
--- a/src/core/stats.c
+++ b/src/core/stats.c
@@ -88,7 +88,7 @@ nni_stat_init(nni_stat_item *stat, const char *name, const char *desc)
stat->si_update = NULL;
stat->si_private = NULL;
stat->si_string = NULL;
- stat->si_value = 0;
+ stat->si_number = 0;
stat->si_type = NNG_STAT_COUNTER;
stat->si_unit = NNG_UNIT_NONE;
}
@@ -116,9 +116,9 @@ nni_stat_init_id(
nni_stat_item *stat, const char *name, const char *desc, uint64_t id)
{
nni_stat_init(stat, name, desc);
- stat->si_value = id;
- stat->si_type = NNG_STAT_ID;
- stat->si_unit = NNG_UNIT_NONE;
+ stat->si_number = id;
+ stat->si_type = NNG_STAT_ID;
+ stat->si_unit = NNG_UNIT_NONE;
}
void
@@ -126,16 +126,16 @@ nni_stat_init_bool(
nni_stat_item *stat, const char *name, const char *desc, bool v)
{
nni_stat_init(stat, name, desc);
- stat->si_value = v ? 1 : 0;
- stat->si_type = NNG_STAT_BOOLEAN;
- stat->si_unit = NNG_UNIT_NONE;
+ stat->si_number = v ? 1 : 0;
+ stat->si_type = NNG_STAT_BOOLEAN;
+ stat->si_unit = NNG_UNIT_NONE;
}
static void
stat_atomic_update(nni_stat_item *stat, void *notused)
{
NNI_ARG_UNUSED(notused);
- stat->si_value = nni_atomic_get64(&stat->si_atomic);
+ stat->si_number = nni_atomic_get64(&stat->si_atomic);
}
void
@@ -143,7 +143,7 @@ nni_stat_init_atomic(nni_stat_item *stat, const char *name, const char *desc)
{
nni_stat_init(stat, name, desc);
- stat->si_value = 0;
+ stat->si_number = 0;
stat->si_private = NULL;
stat->si_update = stat_atomic_update;
nni_atomic_init64(&stat->si_atomic);
@@ -166,7 +166,7 @@ void
nni_stat_set_value(nni_stat_item *stat, uint64_t v)
{
#ifdef NNG_ENABLE_STATS
- stat->si_value = v;
+ stat->si_number = v;
#else
NNI_ARG_UNUSED(stat);
NNI_ARG_UNUSED(v);
@@ -292,7 +292,7 @@ stat_update(nni_stat *stat)
if (item->si_update != NULL) {
item->si_update(item, item->si_private);
}
- stat->s_value = item->si_value;
+ stat->s_value = item->si_number;
stat->s_string = item->si_string;
stat->s_time = nni_clock();
}
diff --git a/src/core/stats.h b/src/core/stats.h
index 152ba01f..87bd90a7 100644
--- a/src/core/stats.h
+++ b/src/core/stats.h
@@ -29,7 +29,11 @@ typedef void (*nni_stat_update)(nni_stat_item *, void *);
typedef enum nng_stat_type_enum nni_stat_type;
typedef enum nng_unit_enum nni_stat_unit;
-// nni_stat_item is used by providers
+// nni_stat_item is used by providers. Providers should avoid accessing
+// this directly, but use accessors below. It is important that we offer
+// this structure so that providers can declare them inline, in order to
+// avoid having to spend dereference costs or (worse) to have to include
+// extra conditionals on hot code paths.
struct nni_stat_item {
#ifdef NNG_ENABLE_STATS
nni_list_node si_node; // list node, framework use only
@@ -43,7 +47,7 @@ struct nni_stat_item {
nni_stat_unit si_unit; // units, e.g. NNG_UNIT_MILLIS
nni_stat_update si_update; // update function (can be NULL)
const char * si_string; // string value (NULL for numerics)
- uint64_t si_value; // numeric value
+ uint64_t si_number; // numeric value
nni_atomic_u64 si_atomic; // atomic value
#endif
};