From a812c8ea51834482fcf1bc58a147a37886f58285 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 25 Oct 2017 15:32:02 +0200 Subject: [SERVER] Improve handling of byte stats counters Less writes to variables, more up-to-date values for uplinks. --- src/server/globals.h | 7 ++++--- src/server/net.c | 24 ++++++++++++------------ src/server/uplink.c | 17 +++++++++++++---- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/server/globals.h b/src/server/globals.h index 798f4ee..2dbabd5 100644 --- a/src/server/globals.h +++ b/src/server/globals.h @@ -68,6 +68,7 @@ struct _dnbd3_connection int nextReplicationIndex; // Which index in the cache map we should start looking for incomplete blocks at uint64_t replicationHandle; // Handle of pending replication request uint64_t bytesReceived; // Number of bytes received by the connection. + uint64_t lastBytesReceived; // Number of bytes received last time we updated the global counter. int queueLen; // length of queue dnbd3_queued_request_t queue[SERVER_MAX_UPLINK_QUEUE]; }; @@ -123,11 +124,11 @@ struct _dnbd3_image struct _dnbd3_client { #define HOSTNAMELEN (48) - uint64_t bytesSent; // Byte counter for this client. Use statsLock when accessing + uint64_t bytesSent; // Byte counter for this client. Use statsLock when accessing. + uint64_t lastBytesSent; // Byte counter from when we last added to global counter. Use statsLock when accessing. dnbd3_image_t *image; - size_t tmpBytesSent; // Temporary byte counter that gets added to the global counter periodically. Use statsLock when accessing int sock; - bool isServer; // true if a server in proxy mode, false if real client + bool isServer; // true if a server in proxy mode, false if real client dnbd3_host_t host; char hostName[HOSTNAMELEN]; pthread_mutex_t sendMutex; diff --git a/src/server/net.c b/src/server/net.c index 09121c3..79d6cfa 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -61,9 +61,9 @@ static bool dnbd3_addClient(dnbd3_client_t *client); void net_updateGlobalSentStatsFromClient(dnbd3_client_t * const client) { spin_lock( &statisticsSentLock ); - totalBytesSent += client->tmpBytesSent; + totalBytesSent += ( client->bytesSent - client->lastBytesSent ); spin_unlock( &statisticsSentLock ); - client->tmpBytesSent = 0; + client->lastBytesSent = client->bytesSent; } static inline bool recv_request_header(int sock, dnbd3_request_t *request) @@ -197,6 +197,16 @@ void* net_handleNewConnection(void *clientPtr) spin_init( &client->lock, PTHREAD_PROCESS_PRIVATE ); spin_init( &client->statsLock, PTHREAD_PROCESS_PRIVATE ); pthread_mutex_init( &client->sendMutex, NULL ); + + spin_lock( &client->lock ); + host_to_string( &client->host, client->hostName, HOSTNAMELEN ); + client->hostName[HOSTNAMELEN-1] = '\0'; + spin_unlock( &client->lock ); + spin_lock( &client->statsLock ); + client->bytesSent = 0; + client->lastBytesSent = 0; + spin_unlock( &client->statsLock ); + if ( !dnbd3_addClient( client ) ) { dnbd3_freeClient( client ); logadd( LOG_WARNING, "Could not add new client to list when connecting" ); @@ -223,11 +233,6 @@ void* net_handleNewConnection(void *clientPtr) memset( &payload, 0, sizeof(payload) ); reply.magic = dnbd3_packet_magic; - spin_lock( &client->lock ); - host_to_string( &client->host, client->hostName, HOSTNAMELEN ); - client->hostName[HOSTNAMELEN-1] = '\0'; - spin_unlock( &client->lock ); - // Receive first packet's payload if ( recv_request_payload( client->sock, request.size, &payload ) ) { char *image_name; @@ -461,11 +466,6 @@ void* net_handleNewConnection(void *clientPtr) spin_lock( &client->statsLock ); // Global per-client counter client->bytesSent += request.size; // Increase counter for statistics. - // Local counter that gets added to the global total bytes sent counter periodically - client->tmpBytesSent += request.size; - if ( client->tmpBytesSent > 100000000 ) { - net_updateGlobalSentStatsFromClient( client ); - } spin_unlock( &client->statsLock ); break; diff --git a/src/server/uplink.c b/src/server/uplink.c index e899ed9..2f71a4c 100644 --- a/src/server/uplink.c +++ b/src/server/uplink.c @@ -23,6 +23,7 @@ static void uplink_handleReceive(dnbd3_connection_t *link); static int uplink_sendKeepalive(const int fd); static void uplink_addCrc32(dnbd3_connection_t *uplink); static void uplink_sendReplicationRequest(dnbd3_connection_t *link); +static void uplink_updateGlobalReceivedCounter(dnbd3_connection_t *link); // ############ uplink connection handling @@ -64,6 +65,7 @@ bool uplink_init(dnbd3_image_t *image, int sock, dnbd3_host_t *host, int version spin_init( &link->rttLock, PTHREAD_PROCESS_PRIVATE ); link->image = image; link->bytesReceived = 0; + link->lastBytesReceived = 0; link->queueLen = 0; link->fd = -1; link->signal = NULL; @@ -378,6 +380,7 @@ static void* uplink_mainloop(void *data) link->fd = -1; close( fd ); } + uplink_updateGlobalReceivedCounter( link ); } // See if we should trigger an RTT measurement spin_lock( &link->rttLock ); @@ -457,9 +460,7 @@ static void* uplink_mainloop(void *data) spin_destroy( &link->rttLock ); free( link->recvBuffer ); link->recvBuffer = NULL; - spin_lock( &statisticsReceivedLock ); - totalBytesReceived += link->bytesReceived; - spin_unlock( &statisticsReceivedLock ); + uplink_updateGlobalReceivedCounter( link ); free( link ); return NULL ; } @@ -648,7 +649,6 @@ static void uplink_handleReceive(dnbd3_connection_t *link) pthread_mutex_unlock( &client->sendMutex ); if ( bytesSent != 0 ) { client->bytesSent += bytesSent; - client->tmpBytesSent += bytesSent; } spin_unlock( &client->statsLock ); spin_lock( &link->queueLock ); @@ -722,3 +722,12 @@ static void uplink_addCrc32(dnbd3_connection_t *uplink) close( fd ); } } + +static void uplink_updateGlobalReceivedCounter(dnbd3_connection_t *link) +{ + spin_lock( &statisticsReceivedLock ); + totalBytesReceived += ( link->bytesReceived - link->lastBytesReceived ); + link->lastBytesReceived = 0; + spin_unlock( &statisticsReceivedLock ); +} + -- cgit v1.2.3-55-g7522