Skip to content

Commit 2009f02

Browse files
authored
Merge pull request #8592 from deepikabhavnani/nw_stats_ver2
Network Socket Statistics
2 parents 1c16383 + 09f4d0b commit 2009f02

34 files changed

+565
-6
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ NetworkInterface *net;
3838
Timer tc_bucket; // Timer to limit a test cases run time
3939
}
4040

41+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
42+
mbed_stats_socket_t tcp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
43+
#endif
44+
4145
char tcp_global::rx_buffer[RX_BUFF_SIZE];
4246
char tcp_global::tx_buffer[TX_BUFF_SIZE];
4347

@@ -115,6 +119,13 @@ int split2half_rmng_tcp_test_time()
115119
return (tcp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2;
116120
}
117121

122+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
123+
int fetch_stats()
124+
{
125+
return SocketStats::mbed_stats_socket_get_each(&tcp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
126+
}
127+
#endif
128+
118129
// Test setup
119130
utest::v1::status_t greentea_setup(const size_t number_of_cases)
120131
{

TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ void fill_tx_buffer_ascii(char *buff, size_t len);
2424
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket &sock);
2525
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket &sock);
2626

27+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
28+
extern mbed_stats_socket_t tcp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
29+
int fetch_stats(void);
30+
#endif
31+
2732
/**
2833
* Single testcase might take only half of the remaining execution time
2934
*/

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
114114

115115
void TCPSOCKET_ECHOTEST_NONBLOCK()
116116
{
117+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
118+
int j = 0;
119+
int count = fetch_stats();
120+
for (; j < count; j++) {
121+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
122+
}
123+
#endif
117124
tc_exec_time.start();
118125
time_allotted = split2half_rmng_tcp_test_time(); // [s]
119126

@@ -160,6 +167,15 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
160167
bytes2send -= sent;
161168
}
162169
printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s);
170+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
171+
count = fetch_stats();
172+
for (j = 0; j < count; j++) {
173+
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
174+
break;
175+
}
176+
}
177+
TEST_ASSERT_EQUAL(bytes2send, tcp_stats[j].sent_bytes);
178+
#endif
163179
tx_sem.wait(split2half_rmng_tcp_test_time());
164180
thread->join();
165181
delete thread;

TESTS/netsocket/tcp/tcpsocket_open_close_repeat.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ using namespace utest::v1;
2626

2727
void TCPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
30+
int count = fetch_stats();
31+
for (int j = 0; j < count; j++) {
32+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33+
}
34+
#endif
2935
TCPSocket *sock = new TCPSocket;
3036
if (!sock) {
3137
TEST_FAIL();
@@ -36,4 +42,10 @@ void TCPSOCKET_OPEN_CLOSE_REPEAT()
3642
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
3743
}
3844
delete sock;
45+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
46+
count = fetch_stats();
47+
for (int j = 0; j < count; j++) {
48+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
49+
}
50+
#endif
3951
}

TESTS/netsocket/tcp/tcpsocket_open_limit.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ void TCPSOCKET_OPEN_LIMIT()
7070
break;
7171
}
7272

73+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
74+
int count = fetch_stats();
75+
int open_count = 0;
76+
for (int j = 0; j < count; j++) {
77+
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
78+
open_count++;
79+
}
80+
}
81+
TEST_ASSERT(open_count >= 4);
82+
#endif
83+
7384
TCPSocketItem *tmp;
7485
for (TCPSocketItem *it = socket_list_head; it;) {
7586
++open_sockets[i];

TESTS/netsocket/udp/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ namespace {
3737
NetworkInterface *net;
3838
}
3939

40+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
41+
mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
42+
#endif
43+
4044
NetworkInterface *get_interface()
4145
{
4246
return net;
@@ -76,6 +80,13 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
7680
}
7781
}
7882

83+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
84+
int fetch_stats()
85+
{
86+
return SocketStats::mbed_stats_socket_get_each(&udp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
87+
}
88+
#endif
89+
7990
// Test setup
8091
utest::v1::status_t greentea_setup(const size_t number_of_cases)
8192
{

TESTS/netsocket/udp/udp_tests.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ NetworkInterface *get_interface();
2222
void drop_bad_packets(UDPSocket &sock, int orig_timeout);
2323
void fill_tx_buffer_ascii(char *buff, size_t len);
2424

25+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
26+
extern mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
27+
int fetch_stats(void);
28+
#endif
29+
2530
/*
2631
* Test cases
2732
*/

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
121121

122122
void UDPSOCKET_ECHOTEST_NONBLOCK()
123123
{
124+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
125+
int j = 0;
126+
int count = fetch_stats();
127+
for (; j < count; j++) {
128+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
129+
}
130+
#endif
131+
124132
SocketAddress udp_addr;
125133
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
126134
udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
@@ -174,11 +182,27 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
174182
}
175183
}
176184
free(stack_mem);
185+
177186
// Packet loss up to 30% tolerated
178187
if (packets_sent > 0) {
179188
double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
180189
printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
181190
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
191+
192+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
193+
count = fetch_stats();
194+
for (j = 0; j < count; j++) {
195+
if ((NSAPI_UDP == udp_stats[j].proto) && (SOCK_OPEN == udp_stats[j].state)) {
196+
TEST_ASSERT(udp_stats[j].sent_bytes != 0);
197+
TEST_ASSERT(udp_stats[j].recv_bytes != 0);
198+
break;
199+
}
200+
}
201+
loss_ratio = 1 - ((double)udp_stats[j].recv_bytes / (double)udp_stats[j].sent_bytes);
202+
printf("Bytes sent: %d, bytes received %d, loss ratio %.2lf\r\n", udp_stats[j].sent_bytes, udp_stats[j].recv_bytes, loss_ratio);
203+
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
204+
205+
#endif
182206
}
183207
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
184208
}

TESTS/netsocket/udp/udpsocket_open_close_repeat.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ using namespace utest::v1;
2626

2727
void UDPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
30+
int count = fetch_stats();
31+
for (int j = 0; j < count; j++) {
32+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
33+
}
34+
#endif
2935
UDPSocket *sock = new UDPSocket;
3036
if (!sock) {
3137
TEST_FAIL();
@@ -36,4 +42,10 @@ void UDPSOCKET_OPEN_CLOSE_REPEAT()
3642
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
3743
}
3844
delete sock;
45+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
46+
count = fetch_stats();
47+
for (int j = 0; j < count; j++) {
48+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
49+
}
50+
#endif
3951
}

TESTS/netsocket/udp/udpsocket_open_limit.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "UDPSocket.h"
2222
#include "unity/unity.h"
2323
#include "utest.h"
24+
#include "SocketStats.h"
2425

2526
using namespace utest::v1;
2627

@@ -69,7 +70,16 @@ void UDPSOCKET_OPEN_LIMIT()
6970
if (!socket_list_head) {
7071
break;
7172
}
72-
73+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
74+
int count = fetch_stats();
75+
int open_count = 0;
76+
for (int j = 0; j < count; j++) {
77+
if ((udp_stats[j].state == SOCK_OPEN) && (udp_stats[j].proto == NSAPI_UDP)) {
78+
open_count++;
79+
}
80+
}
81+
TEST_ASSERT(open_count >= 3);
82+
#endif
7383
UDPSocketItem *tmp;
7484
for (UDPSocketItem *it = socket_list_head; it;) {
7585
++open_sockets[i];

UNITTESTS/features/netsocket/DTLSSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(unittest-test-sources
3131
stubs/stoip4_stub.c
3232
stubs/ip4tos_stub.c
3333
stubs/Kernel_stub.cpp
34+
stubs/SocketStats_Stub.cpp
3435
)
3536

3637
set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/DTLSSocket/dtls_test_config.h\"")

UNITTESTS/features/netsocket/DTLSSocketWrapper/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(unittest-test-sources
3030
stubs/stoip4_stub.c
3131
stubs/ip4tos_stub.c
3232
stubs/Kernel_stub.cpp
33+
stubs/SocketStats_Stub.cpp
3334
)
3435

3536
set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/DTLSSocketWrapper/dtls_test_config.h\"")

UNITTESTS/features/netsocket/EthernetInterface/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ set(unittest-test-sources
3333
stubs/stoip4_stub.c
3434
stubs/ip4tos_stub.c
3535
stubs/NetworkStack_stub.cpp
36+
stubs/SocketStats_Stub.cpp
3637
)

UNITTESTS/features/netsocket/InternetSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ set(unittest-test-sources
2525
stubs/EventFlags_stub.cpp
2626
stubs/stoip4_stub.c
2727
stubs/ip4tos_stub.c
28+
stubs/SocketStats_Stub.cpp
2829
)

UNITTESTS/features/netsocket/NetworkInterface/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ set(unittest-test-sources
2525
stubs/nsapi_dns_stub.cpp
2626
stubs/EventFlags_stub.cpp
2727
features/netsocket/NetworkInterface/test_NetworkInterface.cpp
28+
stubs/SocketStats_Stub.cpp
2829
)

UNITTESTS/features/netsocket/NetworkStack/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ set(unittest-test-sources
2828
stubs/nsapi_dns_stub.cpp
2929
stubs/EventFlags_stub.cpp
3030
features/netsocket/NetworkStack/test_NetworkStack.cpp
31+
stubs/SocketStats_Stub.cpp
3132
)

UNITTESTS/features/netsocket/TCPServer/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ set(unittest-test-sources
2828
stubs/nsapi_dns_stub.cpp
2929
stubs/EventFlags_stub.cpp
3030
features/netsocket/TCPServer/test_TCPServer.cpp
31+
stubs/SocketStats_Stub.cpp
3132
)

UNITTESTS/features/netsocket/TCPSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ set(unittest-test-sources
2626
stubs/EventFlags_stub.cpp
2727
stubs/stoip4_stub.c
2828
stubs/ip4tos_stub.c
29+
stubs/SocketStats_Stub.cpp
2930
)

UNITTESTS/features/netsocket/TLSSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(unittest-test-sources
2929
stubs/EventFlags_stub.cpp
3030
stubs/stoip4_stub.c
3131
stubs/ip4tos_stub.c
32+
stubs/SocketStats_Stub.cpp
3233
)
3334

3435
set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/TLSSocket/tls_test_config.h\"")

UNITTESTS/features/netsocket/TLSSocketWrapper/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(unittest-test-sources
2828
stubs/EventFlags_stub.cpp
2929
stubs/stoip4_stub.c
3030
stubs/ip4tos_stub.c
31+
stubs/SocketStats_Stub.cpp
3132
)
3233

3334
set(MBEDTLS_USER_CONFIG_FILE_PATH "\"../UNITTESTS/features/netsocket/TLSSocketWrapper/tls_test_config.h\"")

UNITTESTS/features/netsocket/UDPSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ set(unittest-test-sources
2626
stubs/nsapi_dns_stub.cpp
2727
stubs/stoip4_stub.c
2828
stubs/ip4tos_stub.c
29+
stubs/SocketStats_Stub.cpp
2930
)

UNITTESTS/stubs/SocketStats_Stub.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "SocketStats.h"
18+
19+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
20+
int SocketStats::get_entry_position(const Socket *const reference_id)
21+
{
22+
return 0;
23+
}
24+
#endif
25+
26+
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
27+
{
28+
return 0;
29+
}
30+
31+
SocketStats::SocketStats()
32+
{
33+
}
34+
35+
void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
36+
{
37+
return;
38+
}
39+
40+
void SocketStats::stats_update_socket_state(const Socket *const reference_id, socket_state state)
41+
{
42+
return;
43+
}
44+
45+
void SocketStats::stats_update_peer(const Socket *const reference_id, const SocketAddress &peer)
46+
{
47+
return;
48+
}
49+
50+
void SocketStats::stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto)
51+
{
52+
return;
53+
}
54+
55+
void SocketStats::stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes)
56+
{
57+
return;
58+
}
59+
60+
void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes)
61+
{
62+
return;
63+
}

features/lwipstack/LWIPStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
450450
* @param func Callback to be called
451451
* @return 0 on success, negative error code on failure
452452
*/
453-
nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
453+
virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
454454

455455
struct mbed_lwip_socket {
456456
bool in_use;

0 commit comments

Comments
 (0)