Skip to content

Network Socket Statistics #8592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions TESTS/netsocket/tcp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ NetworkInterface *net;
Timer tc_bucket; // Timer to limit a test cases run time
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
mbed_stats_socket_t tcp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
#endif

char tcp_global::rx_buffer[RX_BUFF_SIZE];
char tcp_global::tx_buffer[TX_BUFF_SIZE];

Expand Down Expand Up @@ -115,6 +119,13 @@ int split2half_rmng_tcp_test_time()
return (tcp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2;
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int fetch_stats()
{
return SocketStats::mbed_stats_socket_get_each(&tcp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
}
#endif

// Test setup
utest::v1::status_t greentea_setup(const size_t number_of_cases)
{
Expand Down
5 changes: 5 additions & 0 deletions TESTS/netsocket/tcp/tcp_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ void fill_tx_buffer_ascii(char *buff, size_t len);
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket &sock);
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket &sock);

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
extern mbed_stats_socket_t tcp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
int fetch_stats(void);
#endif

/**
* Single testcase might take only half of the remaining execution time
*/
Expand Down
16 changes: 16 additions & 0 deletions TESTS/netsocket/tcp/tcpsocket_echotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)

void TCPSOCKET_ECHOTEST_NONBLOCK()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int j = 0;
int count = fetch_stats();
for (; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
tc_exec_time.start();
time_allotted = split2half_rmng_tcp_test_time(); // [s]

Expand Down Expand Up @@ -160,6 +167,15 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
bytes2send -= sent;
}
printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s);
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
count = fetch_stats();
for (j = 0; j < count; j++) {
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
break;
}
}
TEST_ASSERT_EQUAL(bytes2send, tcp_stats[j].sent_bytes);
#endif
tx_sem.wait(split2half_rmng_tcp_test_time());
thread->join();
delete thread;
Expand Down
12 changes: 12 additions & 0 deletions TESTS/netsocket/tcp/tcpsocket_open_close_repeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ using namespace utest::v1;

void TCPSOCKET_OPEN_CLOSE_REPEAT()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -36,4 +42,10 @@ void TCPSOCKET_OPEN_CLOSE_REPEAT()
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
}
delete sock;
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
11 changes: 11 additions & 0 deletions TESTS/netsocket/tcp/tcpsocket_open_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ void TCPSOCKET_OPEN_LIMIT()
break;
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int count = fetch_stats();
int open_count = 0;
for (int j = 0; j < count; j++) {
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
open_count++;
}
}
TEST_ASSERT(open_count >= 4);
#endif

TCPSocketItem *tmp;
for (TCPSocketItem *it = socket_list_head; it;) {
++open_sockets[i];
Expand Down
11 changes: 11 additions & 0 deletions TESTS/netsocket/udp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace {
NetworkInterface *net;
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
#endif

NetworkInterface *get_interface()
{
return net;
Expand Down Expand Up @@ -76,6 +80,13 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
}
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int fetch_stats()
{
return SocketStats::mbed_stats_socket_get_each(&udp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
}
#endif

// Test setup
utest::v1::status_t greentea_setup(const size_t number_of_cases)
{
Expand Down
5 changes: 5 additions & 0 deletions TESTS/netsocket/udp/udp_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ NetworkInterface *get_interface();
void drop_bad_packets(UDPSocket &sock, int orig_timeout);
void fill_tx_buffer_ascii(char *buff, size_t len);

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
extern mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
int fetch_stats(void);
#endif

/*
* Test cases
*/
Expand Down
24 changes: 24 additions & 0 deletions TESTS/netsocket/udp/udpsocket_echotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ void udpsocket_echotest_nonblock_receiver(void *receive_bytes)

void UDPSOCKET_ECHOTEST_NONBLOCK()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int j = 0;
int count = fetch_stats();
for (; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
}
#endif

SocketAddress udp_addr;
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
Expand Down Expand Up @@ -174,11 +182,27 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
}
}
free(stack_mem);

// Packet loss up to 30% tolerated
if (packets_sent > 0) {
double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
count = fetch_stats();
for (j = 0; j < count; j++) {
if ((NSAPI_UDP == udp_stats[j].proto) && (SOCK_OPEN == udp_stats[j].state)) {
TEST_ASSERT(udp_stats[j].sent_bytes != 0);
TEST_ASSERT(udp_stats[j].recv_bytes != 0);
break;
}
}
loss_ratio = 1 - ((double)udp_stats[j].recv_bytes / (double)udp_stats[j].sent_bytes);
printf("Bytes sent: %d, bytes received %d, loss ratio %.2lf\r\n", udp_stats[j].sent_bytes, udp_stats[j].recv_bytes, loss_ratio);
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);

#endif
}
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}
12 changes: 12 additions & 0 deletions TESTS/netsocket/udp/udpsocket_open_close_repeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ using namespace utest::v1;

void UDPSOCKET_OPEN_CLOSE_REPEAT()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
}
#endif
UDPSocket *sock = new UDPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -36,4 +42,10 @@ void UDPSOCKET_OPEN_CLOSE_REPEAT()
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
}
delete sock;
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
}
#endif
}
12 changes: 11 additions & 1 deletion TESTS/netsocket/udp/udpsocket_open_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "UDPSocket.h"
#include "unity/unity.h"
#include "utest.h"
#include "SocketStats.h"

using namespace utest::v1;

Expand Down Expand Up @@ -69,7 +70,16 @@ void UDPSOCKET_OPEN_LIMIT()
if (!socket_list_head) {
break;
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int count = fetch_stats();
int open_count = 0;
for (int j = 0; j < count; j++) {
if ((udp_stats[j].state == SOCK_OPEN) && (udp_stats[j].proto == NSAPI_UDP)) {
open_count++;
}
}
TEST_ASSERT(open_count >= 3);
#endif
UDPSocketItem *tmp;
for (UDPSocketItem *it = socket_list_head; it;) {
++open_sockets[i];
Expand Down
1 change: 1 addition & 0 deletions UNITTESTS/features/netsocket/DTLSSocket/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(unittest-test-sources
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/Kernel_stub.cpp
stubs/SocketStats_Stub.cpp
)

set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/DTLSSocket/dtls_test_config.h\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(unittest-test-sources
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/Kernel_stub.cpp
stubs/SocketStats_Stub.cpp
)

set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/DTLSSocketWrapper/dtls_test_config.h\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ set(unittest-test-sources
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/NetworkStack_stub.cpp
stubs/SocketStats_Stub.cpp
)
1 change: 1 addition & 0 deletions UNITTESTS/features/netsocket/InternetSocket/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ set(unittest-test-sources
stubs/EventFlags_stub.cpp
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/SocketStats_Stub.cpp
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ set(unittest-test-sources
stubs/nsapi_dns_stub.cpp
stubs/EventFlags_stub.cpp
features/netsocket/NetworkInterface/test_NetworkInterface.cpp
stubs/SocketStats_Stub.cpp
)
1 change: 1 addition & 0 deletions UNITTESTS/features/netsocket/NetworkStack/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ set(unittest-test-sources
stubs/nsapi_dns_stub.cpp
stubs/EventFlags_stub.cpp
features/netsocket/NetworkStack/test_NetworkStack.cpp
stubs/SocketStats_Stub.cpp
)
1 change: 1 addition & 0 deletions UNITTESTS/features/netsocket/TCPServer/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ set(unittest-test-sources
stubs/nsapi_dns_stub.cpp
stubs/EventFlags_stub.cpp
features/netsocket/TCPServer/test_TCPServer.cpp
stubs/SocketStats_Stub.cpp
)
1 change: 1 addition & 0 deletions UNITTESTS/features/netsocket/TCPSocket/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ set(unittest-test-sources
stubs/EventFlags_stub.cpp
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/SocketStats_Stub.cpp
)
1 change: 1 addition & 0 deletions UNITTESTS/features/netsocket/TLSSocket/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set(unittest-test-sources
stubs/EventFlags_stub.cpp
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/SocketStats_Stub.cpp
)

set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/TLSSocket/tls_test_config.h\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(unittest-test-sources
stubs/EventFlags_stub.cpp
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/SocketStats_Stub.cpp
)

set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/TLSSocketWrapper/tls_test_config.h\"")
Expand Down
1 change: 1 addition & 0 deletions UNITTESTS/features/netsocket/UDPSocket/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ set(unittest-test-sources
stubs/nsapi_dns_stub.cpp
stubs/stoip4_stub.c
stubs/ip4tos_stub.c
stubs/SocketStats_Stub.cpp
)
63 changes: 63 additions & 0 deletions UNITTESTS/stubs/SocketStats_Stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "SocketStats.h"

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int SocketStats::get_entry_position(const Socket *const reference_id)
{
return 0;
}
#endif

size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
{
return 0;
}

SocketStats::SocketStats()
{
}

void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
{
return;
}

void SocketStats::stats_update_socket_state(const Socket *const reference_id, socket_state state)
{
return;
}

void SocketStats::stats_update_peer(const Socket *const reference_id, const SocketAddress &peer)
{
return;
}

void SocketStats::stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto)
{
return;
}

void SocketStats::stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes)
{
return;
}

void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes)
{
return;
}
2 changes: 1 addition & 1 deletion features/lwipstack/LWIPStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
* @param func Callback to be called
* @return 0 on success, negative error code on failure
*/
nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func);

struct mbed_lwip_socket {
bool in_use;
Expand Down
Loading