Merge pull request #12463 from kjbracey-arm/sockstats

Clean up and optimise socket statistics
pull/12896/head
Martin Kojtal 2020-04-29 22:16:54 +02:00 committed by GitHub
commit 1ddfa59ea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 46 deletions

View File

@ -22,7 +22,6 @@ 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)
{
@ -62,3 +61,4 @@ void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size
{
return;
}
#endif

View File

@ -21,7 +21,6 @@
#include "rtos/Kernel.h"
#endif
#include <string.h>
#include <stdlib.h>
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
@ -38,40 +37,28 @@ int SocketStats::get_entry_position(const Socket *const reference_id)
}
return -1;
}
#endif
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
{
MBED_ASSERT(stats != NULL);
size_t i = 0;
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
memset(stats, 0, count * sizeof(mbed_stats_socket_t));
size_t j;
_mutex->lock();
for (uint32_t j = 0; j < count; j++) {
if (_stats[j].reference_id) {
memcpy(&stats[i], &_stats[j], sizeof(mbed_stats_socket_t));
i++;
}
for (j = 0; j < count && j < _size; j++) {
stats[j] = _stats[j];
}
_mutex->unlock();
#endif
return i;
return j;
}
SocketStats::SocketStats()
void SocketStats::stats_new_socket_entry(Socket *const reference_id)
{
}
void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
_mutex->lock();
if (get_entry_position(reference_id) >= 0) {
// Duplicate entry
MBED_WARNING1(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_INVALID_INDEX), "Duplicate socket Reference ID ", reference_id);
} else if (_size < MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT) {
// Add new entry
_stats[_size].reference_id = (Socket *)reference_id;
_stats[_size].reference_id = reference_id;
_size++;
} else {
int position = -1;
@ -88,17 +75,14 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
if (-1 == position) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_OUT_OF_RESOURCES), "List full with all open sockets");
}
memset(&_stats[position], 0, sizeof(mbed_stats_socket_t));
_stats[position].reference_id = (Socket *)reference_id;
_stats[position] = {};
_stats[position].reference_id = reference_id;
}
_mutex->unlock();
#endif
return;
}
void SocketStats::stats_update_socket_state(const Socket *const reference_id, socket_state state)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
_mutex->lock();
int position = get_entry_position(reference_id);
if (position >= 0) {
@ -108,53 +92,45 @@ void SocketStats::stats_update_socket_state(const Socket *const reference_id, so
#endif
}
_mutex->unlock();
#endif
}
void SocketStats::stats_update_peer(const Socket *const reference_id, const SocketAddress &peer)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
_mutex->lock();
int position = get_entry_position(reference_id);
if ((position >= 0) && (!_stats[position].peer)) {
_stats[position].peer = peer;
}
_mutex->unlock();
#endif
}
void SocketStats::stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
_mutex->lock();
int position = get_entry_position(reference_id);
if (position >= 0) {
_stats[position].proto = proto;
}
_mutex->unlock();
#endif
}
void SocketStats::stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
_mutex->lock();
int position = get_entry_position(reference_id);
if ((position >= 0) && ((int32_t)sent_bytes > 0)) {
_stats[position].sent_bytes += sent_bytes;
}
_mutex->unlock();
#endif
}
void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
_mutex->lock();
int position = get_entry_position(reference_id);
if ((position >= 0) && ((int32_t)recv_bytes > 0)) {
_stats[position].recv_bytes += recv_bytes;
}
_mutex->unlock();
#endif
}
#endif // MBED_CONF_NSAPI_SOCKET_STATS_ENABLED

View File

@ -60,17 +60,14 @@ class SocketStats {
public:
#if !defined(DOXYGEN_ONLY)
/** Create an socket statictics object
/** Create a socket statistics object
*
* Application users must not create class objects.
* Entities reporting network statistics create the class object.
* Application can fetch network statistics using static `mbed_stats_socket_get_each` API
* without creating an object.
*/
SocketStats();
virtual ~SocketStats()
{
}
constexpr SocketStats() = default;
#endif
/**
* Fill the passed array of structures with the socket statistics for each created socket.
@ -96,7 +93,7 @@ public:
* the socket creation count exceeds `MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT`.
*
*/
void stats_new_socket_entry(const Socket *const reference_id);
void stats_new_socket_entry(Socket *reference_id);
/** Updates the state of the socket and records `tick_last_change`.
* API used by socket (TCP or UDP) layers only, not to be used by application.
@ -105,7 +102,7 @@ public:
* @param state Parameter to update the current state of the socket.
*
*/
void stats_update_socket_state(const Socket *const reference_id, socket_state state);
void stats_update_socket_state(const Socket *reference_id, socket_state state);
/** Update the peer information of the socket.
* API used by socket (TCP or UDP) layers only, not to be used by application.
@ -114,7 +111,7 @@ public:
* @param peer Parameter to update destination peer information.
*
*/
void stats_update_peer(const Socket *const reference_id, const SocketAddress &peer);
void stats_update_peer(const Socket *reference_id, const SocketAddress &peer);
/** Update socket protocol.
* API used by socket (TCP or UDP) layers only, not to be used by application.
@ -123,7 +120,7 @@ public:
* @param proto Parameter to update the protocol type of socket.
*
*/
void stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto);
void stats_update_proto(const Socket *reference_id, nsapi_protocol_t proto);
/** Update bytes sent on socket, which is cumulative count per socket.
* API used by socket (TCP or UDP) layers only, not to be used by application.
@ -132,7 +129,7 @@ public:
* @param sent_bytes Parameter to append bytes sent over the socket.
*
*/
void stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes);
void stats_update_sent_bytes(const Socket *reference_id, size_t sent_bytes);
/** Update bytes received on socket, which is cumulative count per socket
* API used by socket (TCP or UDP) layers only, not to be used by application.
@ -141,7 +138,7 @@ public:
* @param recv_bytes Parameter to append bytes the socket receives.
*
*/
void stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes);
void stats_update_recv_bytes(const Socket *reference_id, size_t recv_bytes);
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
private:
@ -154,9 +151,40 @@ private:
* @param reference_id ID to identify the socket in the data array.
*
*/
int get_entry_position(const Socket *const reference_id);
int get_entry_position(const Socket *reference_id);
#endif
#endif
};
#if !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
inline size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *, size_t)
{
return 0;
}
inline void SocketStats::stats_new_socket_entry(Socket *)
{
}
inline void SocketStats::stats_update_socket_state(const Socket *, socket_state)
{
}
inline void SocketStats::stats_update_peer(const Socket *, const SocketAddress &)
{
}
inline void SocketStats::stats_update_proto(const Socket *, nsapi_protocol_t)
{
}
inline void SocketStats::stats_update_sent_bytes(const Socket *, size_t)
{
}
inline void SocketStats::stats_update_recv_bytes(const Socket *, size_t)
{
}
#endif // !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
#endif