Skip to content

Commit 1ddfa59

Browse files
authored
Merge pull request #12463 from kjbracey-arm/sockstats
Clean up and optimise socket statistics
2 parents 761b546 + 6f45e96 commit 1ddfa59

File tree

3 files changed

+50
-46
lines changed

3 files changed

+50
-46
lines changed

UNITTESTS/stubs/SocketStats_Stub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ int SocketStats::get_entry_position(const Socket *const reference_id)
2222
{
2323
return 0;
2424
}
25-
#endif
2625

2726
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
2827
{
@@ -62,3 +61,4 @@ void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size
6261
{
6362
return;
6463
}
64+
#endif

features/netsocket/SocketStats.cpp

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "rtos/Kernel.h"
2222
#endif
2323

24-
#include <string.h>
2524
#include <stdlib.h>
2625

2726
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
@@ -38,40 +37,28 @@ int SocketStats::get_entry_position(const Socket *const reference_id)
3837
}
3938
return -1;
4039
}
41-
#endif
4240

4341
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
4442
{
4543
MBED_ASSERT(stats != NULL);
46-
size_t i = 0;
47-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
48-
memset(stats, 0, count * sizeof(mbed_stats_socket_t));
44+
size_t j;
4945
_mutex->lock();
50-
for (uint32_t j = 0; j < count; j++) {
51-
if (_stats[j].reference_id) {
52-
memcpy(&stats[i], &_stats[j], sizeof(mbed_stats_socket_t));
53-
i++;
54-
}
46+
for (j = 0; j < count && j < _size; j++) {
47+
stats[j] = _stats[j];
5548
}
5649
_mutex->unlock();
57-
#endif
58-
return i;
59-
}
60-
61-
SocketStats::SocketStats()
62-
{
50+
return j;
6351
}
6452

65-
void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
53+
void SocketStats::stats_new_socket_entry(Socket *const reference_id)
6654
{
67-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
6855
_mutex->lock();
6956
if (get_entry_position(reference_id) >= 0) {
7057
// Duplicate entry
7158
MBED_WARNING1(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_INVALID_INDEX), "Duplicate socket Reference ID ", reference_id);
7259
} else if (_size < MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT) {
7360
// Add new entry
74-
_stats[_size].reference_id = (Socket *)reference_id;
61+
_stats[_size].reference_id = reference_id;
7562
_size++;
7663
} else {
7764
int position = -1;
@@ -88,17 +75,14 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
8875
if (-1 == position) {
8976
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_OUT_OF_RESOURCES), "List full with all open sockets");
9077
}
91-
memset(&_stats[position], 0, sizeof(mbed_stats_socket_t));
92-
_stats[position].reference_id = (Socket *)reference_id;
78+
_stats[position] = {};
79+
_stats[position].reference_id = reference_id;
9380
}
9481
_mutex->unlock();
95-
#endif
96-
return;
9782
}
9883

9984
void SocketStats::stats_update_socket_state(const Socket *const reference_id, socket_state state)
10085
{
101-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
10286
_mutex->lock();
10387
int position = get_entry_position(reference_id);
10488
if (position >= 0) {
@@ -108,53 +92,45 @@ void SocketStats::stats_update_socket_state(const Socket *const reference_id, so
10892
#endif
10993
}
11094
_mutex->unlock();
111-
#endif
11295
}
11396

11497
void SocketStats::stats_update_peer(const Socket *const reference_id, const SocketAddress &peer)
11598
{
116-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
11799
_mutex->lock();
118100
int position = get_entry_position(reference_id);
119101
if ((position >= 0) && (!_stats[position].peer)) {
120102
_stats[position].peer = peer;
121103
}
122104
_mutex->unlock();
123-
#endif
124105
}
125106

126107
void SocketStats::stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto)
127108
{
128-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
129109
_mutex->lock();
130110
int position = get_entry_position(reference_id);
131111
if (position >= 0) {
132112
_stats[position].proto = proto;
133113
}
134114
_mutex->unlock();
135-
#endif
136115
}
137116

138117
void SocketStats::stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes)
139118
{
140-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
141119
_mutex->lock();
142120
int position = get_entry_position(reference_id);
143121
if ((position >= 0) && ((int32_t)sent_bytes > 0)) {
144122
_stats[position].sent_bytes += sent_bytes;
145123
}
146124
_mutex->unlock();
147-
#endif
148125
}
149126

150127
void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes)
151128
{
152-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
153129
_mutex->lock();
154130
int position = get_entry_position(reference_id);
155131
if ((position >= 0) && ((int32_t)recv_bytes > 0)) {
156132
_stats[position].recv_bytes += recv_bytes;
157133
}
158134
_mutex->unlock();
159-
#endif
160135
}
136+
#endif // MBED_CONF_NSAPI_SOCKET_STATS_ENABLED

features/netsocket/SocketStats.h

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ class SocketStats {
6060
public:
6161

6262
#if !defined(DOXYGEN_ONLY)
63-
/** Create an socket statictics object
63+
/** Create a socket statistics object
6464
*
6565
* Application users must not create class objects.
6666
* Entities reporting network statistics create the class object.
6767
* Application can fetch network statistics using static `mbed_stats_socket_get_each` API
6868
* without creating an object.
6969
*/
70-
SocketStats();
71-
virtual ~SocketStats()
72-
{
73-
}
70+
constexpr SocketStats() = default;
7471
#endif
7572
/**
7673
* Fill the passed array of structures with the socket statistics for each created socket.
@@ -96,7 +93,7 @@ class SocketStats {
9693
* the socket creation count exceeds `MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT`.
9794
*
9895
*/
99-
void stats_new_socket_entry(const Socket *const reference_id);
96+
void stats_new_socket_entry(Socket *reference_id);
10097

10198
/** Updates the state of the socket and records `tick_last_change`.
10299
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -105,7 +102,7 @@ class SocketStats {
105102
* @param state Parameter to update the current state of the socket.
106103
*
107104
*/
108-
void stats_update_socket_state(const Socket *const reference_id, socket_state state);
105+
void stats_update_socket_state(const Socket *reference_id, socket_state state);
109106

110107
/** Update the peer information of the socket.
111108
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -114,7 +111,7 @@ class SocketStats {
114111
* @param peer Parameter to update destination peer information.
115112
*
116113
*/
117-
void stats_update_peer(const Socket *const reference_id, const SocketAddress &peer);
114+
void stats_update_peer(const Socket *reference_id, const SocketAddress &peer);
118115

119116
/** Update socket protocol.
120117
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -123,7 +120,7 @@ class SocketStats {
123120
* @param proto Parameter to update the protocol type of socket.
124121
*
125122
*/
126-
void stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto);
123+
void stats_update_proto(const Socket *reference_id, nsapi_protocol_t proto);
127124

128125
/** Update bytes sent on socket, which is cumulative count per socket.
129126
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -132,7 +129,7 @@ class SocketStats {
132129
* @param sent_bytes Parameter to append bytes sent over the socket.
133130
*
134131
*/
135-
void stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes);
132+
void stats_update_sent_bytes(const Socket *reference_id, size_t sent_bytes);
136133

137134
/** Update bytes received on socket, which is cumulative count per socket
138135
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -141,7 +138,7 @@ class SocketStats {
141138
* @param recv_bytes Parameter to append bytes the socket receives.
142139
*
143140
*/
144-
void stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes);
141+
void stats_update_recv_bytes(const Socket *reference_id, size_t recv_bytes);
145142

146143
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
147144
private:
@@ -154,9 +151,40 @@ class SocketStats {
154151
* @param reference_id ID to identify the socket in the data array.
155152
*
156153
*/
157-
int get_entry_position(const Socket *const reference_id);
154+
int get_entry_position(const Socket *reference_id);
158155
#endif
159156
#endif
160157
};
161158

159+
#if !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
160+
inline size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *, size_t)
161+
{
162+
return 0;
163+
}
164+
165+
inline void SocketStats::stats_new_socket_entry(Socket *)
166+
{
167+
}
168+
169+
inline void SocketStats::stats_update_socket_state(const Socket *, socket_state)
170+
{
171+
}
172+
173+
inline void SocketStats::stats_update_peer(const Socket *, const SocketAddress &)
174+
{
175+
}
176+
177+
inline void SocketStats::stats_update_proto(const Socket *, nsapi_protocol_t)
178+
{
179+
}
180+
181+
inline void SocketStats::stats_update_sent_bytes(const Socket *, size_t)
182+
{
183+
}
184+
185+
inline void SocketStats::stats_update_recv_bytes(const Socket *, size_t)
186+
{
187+
}
188+
#endif // !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
189+
162190
#endif

0 commit comments

Comments
 (0)