diff options
| -rw-r--r-- | docs/man/nng_options.5.adoc | 2 | ||||
| -rw-r--r-- | src/core/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/core/socket.c | 18 | ||||
| -rw-r--r-- | src/core/stats.c | 6 | ||||
| -rw-r--r-- | src/core/stats_test.c | 96 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/stats.c | 74 |
7 files changed, 116 insertions, 82 deletions
diff --git a/docs/man/nng_options.5.adoc b/docs/man/nng_options.5.adoc index 1ad61c46..e4e5124d 100644 --- a/docs/man/nng_options.5.adoc +++ b/docs/man/nng_options.5.adoc @@ -282,7 +282,7 @@ return value of `NNG_ETIMEDOUT`. (((name, socket))) (string) This the socket name. -By default this is a string corresponding to the value of the socket. +By default, this is a string corresponding to the value of the socket. The string must fit within 64-bytes, including the terminating `NUL` byte. The value is intended for application use, and is not used for anything diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8ff5a80d..eb511a83 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -82,4 +82,5 @@ nng_test(list_test) nng_test(message_test) nng_test(reconnect_test) nng_test(sock_test) +nng_test(stats_test) nng_test(url_test) diff --git a/src/core/socket.c b/src/core/socket.c index 75c9eedc..7d15f3cb 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1,5 +1,5 @@ // -// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a @@ -237,8 +237,13 @@ sock_get_sockname(void *s, void *buf, size_t *szp, nni_type t) static int sock_set_sockname(void *s, const void *buf, size_t sz, nni_type t) { - return (nni_copyin_str( + int rv; + rv = (nni_copyin_str( SOCK(s)->s_name, buf, sizeof(SOCK(s)->s_name), sz, t)); + if (rv == 0) { + nni_stat_set_string(&SOCK(s)->st_name, SOCK(s)->s_name); + } + return (rv); } static int @@ -646,8 +651,11 @@ nni_sock_open(nni_sock **sockp, const nni_proto *proto) (void) snprintf(s->s_name, sizeof(s->s_name), "%u", s->s_id); #ifdef NNG_ENABLE_STATS - // Set up basic stat values. + // Set up basic stat values. The socket id wasn't + // known at stat creation time, so we set it now. nni_stat_set_id(&s->st_id, (int) s->s_id); + nni_stat_set_id(&s->st_root, (int) s->s_id); + nni_stat_set_string(&s->st_name, s->s_name); // Add our stats chain. nni_stat_register(&s->st_root); @@ -1488,6 +1496,8 @@ nni_dialer_add_pipe(nni_dialer *d, void *tpipe) return; } #ifdef NNG_ENABLE_STATS + nni_stat_set_id(&p->st_root, (int) p->p_id); + nni_stat_set_id(&p->st_id, (int) p->p_id); nni_stat_register(&p->st_root); #endif nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_POST); @@ -1599,6 +1609,8 @@ nni_listener_add_pipe(nni_listener *l, void *tpipe) return; } #ifdef NNG_ENABLE_STATS + nni_stat_set_id(&p->st_root, (int) p->p_id); + nni_stat_set_id(&p->st_id, (int) p->p_id); nni_stat_register(&p->st_root); #endif nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_POST); diff --git a/src/core/stats.c b/src/core/stats.c index f9de055e..50f24e29 100644 --- a/src/core/stats.c +++ b/src/core/stats.c @@ -1,5 +1,5 @@ // -// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a @@ -463,7 +463,7 @@ nng_stat * nng_stat_find_scope(nng_stat *stat, const char *name, int id) { nng_stat *child; - if (stat == NULL) { + if (stat == NULL || stat->s_info->si_type != NNG_STAT_SCOPE) { return (NULL); } if ((stat->s_val.sv_id == id) && @@ -473,7 +473,7 @@ nng_stat_find_scope(nng_stat *stat, const char *name, int id) } NNI_LIST_FOREACH (&stat->s_children, child) { nng_stat *result; - if ((result = nng_stat_find(child, name)) != NULL) { + if ((result = nng_stat_find_scope(child, name, id)) != NULL) { return (result); } } diff --git a/src/core/stats_test.c b/src/core/stats_test.c new file mode 100644 index 00000000..ba4198ab --- /dev/null +++ b/src/core/stats_test.c @@ -0,0 +1,96 @@ +// +// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include <nuts.h> + +#define SECONDS(x) ((x) *1000) + +void +test_stats_socket(void) +{ +#ifdef NNG_ENABLE_STATS + nng_socket s1; + nng_socket s2; + nng_stat *st1; + nng_stat *st2; + nng_stat *item; + nng_stat *stats; + + NUTS_OPEN(s1); + NUTS_OPEN(s2); + nng_socket_set_string(s2, NNG_OPT_SOCKNAME, "second"); + NUTS_MARRY(s1, s2); + NUTS_SEND(s1, "ping"); + NUTS_RECV(s2, "ping"); + + nng_stats_get(&stats); + NUTS_ASSERT(stats != NULL); + st1 = nng_stat_find_socket(stats, s1); + st2 = nng_stat_find_socket(stats, s2); + NUTS_ASSERT(st1 != NULL); + NUTS_ASSERT(st2 != NULL); + NUTS_ASSERT(st1 != st2); + item = nng_stat_find(st1, "name"); + NUTS_ASSERT(item != NULL); + NUTS_ASSERT(nng_stat_string(item) != NULL); + NUTS_MATCH(nng_stat_string(item), "1"); + item = nng_stat_find(st2, "name"); + NUTS_ASSERT(item != NULL); + NUTS_ASSERT(nng_stat_string(item) != NULL); + NUTS_MATCH(nng_stat_string(item), "second"); + item = nng_stat_find(st1, "tx_msgs"); + NUTS_ASSERT(item != NULL); + NUTS_ASSERT(nng_stat_value(item) == 1); + NUTS_ASSERT(nng_stat_unit(item) == NNG_UNIT_MESSAGES); + item = nng_stat_find(st2, "rx_msgs"); + NUTS_ASSERT(item != NULL); + NUTS_ASSERT(nng_stat_value(item) == 1); + NUTS_ASSERT(nng_stat_unit(item) == NNG_UNIT_MESSAGES); + NUTS_CLOSE(s1); + NUTS_CLOSE(s2); + nng_stats_free(stats); +#endif +} + +void +test_stats_dump(void) +{ +#ifdef NNG_ENABLE_STATS + nng_socket s1; + nng_socket s2; + nng_stat *st1; + nng_stat *st2; + nng_stat *stats; + + NUTS_OPEN(s1); + NUTS_OPEN(s2); + nng_socket_set_string(s2, NNG_OPT_SOCKNAME, "second"); + NUTS_MARRY(s1, s2); + NUTS_SEND(s1, "ping"); + NUTS_RECV(s2, "ping"); + nng_stats_get(&stats); + NUTS_ASSERT(stats != NULL); + st1 = nng_stat_find_socket(stats, s1); + st2 = nng_stat_find_socket(stats, s2); + NUTS_ASSERT(st1 != NULL); + NUTS_ASSERT(st2 != NULL); + NUTS_ASSERT(st1 != st2); + nng_stats_dump(stats); + nng_stats_free(stats); + NUTS_CLOSE(s1); + NUTS_CLOSE(s2); +#endif +} + +NUTS_TESTS = { + { "socket stats", test_stats_socket }, + { "dump stats", test_stats_dump }, + { NULL, NULL }, +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bc212c0f..063692cb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -137,7 +137,6 @@ add_nng_test(options 5) add_nng_test(pipe 5) add_nng_test(pollfd 5) add_nng_test(scalability 20 ON) -add_nng_test1(stats 5 NNG_ENABLE_STATS) add_nng_test(synch 5) add_nng_test(tls 60) add_nng_test(tcpsupp 10) diff --git a/tests/stats.c b/tests/stats.c deleted file mode 100644 index c42c129c..00000000 --- a/tests/stats.c +++ /dev/null @@ -1,74 +0,0 @@ -// -// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech> -// Copyright 2018 Capitar IT Group BV <info@capitar.com> -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include <string.h> - -#include <nng/nng.h> -#include <nng/protocol/pair1/pair.h> -#include <nng/protocol/pubsub0/sub.h> -#include <nng/supplemental/util/platform.h> - -#include "convey.h" -#include "stubs.h" -#include "trantest.h" - -#define SECONDS(x) ((x) *1000) - -TestMain("Stats Test", { - - Convey("We are able to open a PAIR socket", { - nng_socket s1; - - So(nng_pair_open(&s1) == 0); - - Reset({ nng_close(s1); }); - - Convey("We can send and receive messages", { - nng_socket s2; - int len; - size_t sz; - nng_duration to = SECONDS(3); - char * buf; - char * a = "inproc://stats"; - nng_stat * stats; - - So(nng_pair_open(&s2) == 0); - Reset({ nng_close(s2); }); - - So(nng_socket_set_int(s1, NNG_OPT_RECVBUF, 1) == 0); - So(nng_socket_get_int(s1, NNG_OPT_RECVBUF, &len) == 0); - So(len == 1); - - So(nng_socket_set_int(s1, NNG_OPT_SENDBUF, 1) == 0); - So(nng_socket_set_int(s2, NNG_OPT_SENDBUF, 1) == 0); - - So(nng_socket_set_ms(s1, NNG_OPT_SENDTIMEO, to) == 0); - So(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, to) == 0); - So(nng_socket_set_ms(s2, NNG_OPT_SENDTIMEO, to) == 0); - So(nng_socket_set_ms(s2, NNG_OPT_RECVTIMEO, to) == 0); - - So(nng_listen(s1, a, NULL, 0) == 0); - So(nng_dial(s2, a, NULL, 0) == 0); - - So(nng_send(s1, "abc", 4, 0) == 0); - So(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC) == 0); - So(buf != NULL); - So(sz == 4); - So(memcmp(buf, "abc", 4) == 0); - nng_free(buf, sz); - - Convey("We can collect stats", { - So(nng_stats_get(&stats) == 0); - nng_stats_dump(stats); - nng_stats_free(stats); - }); - }); - }); -}) |
