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/net.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/server/net.c') 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; -- cgit v1.2.3-55-g7522