From 05027a457a5ce2788fdb9838d999f9470ed645be Mon Sep 17 00:00:00 2001 From: Stephan Schwaer Date: Mon, 27 Apr 2015 18:39:47 +0200 Subject: [SERVER] Added counters for received and sent bytes. --- LOCKS | 2 ++ src/server/globals.h | 2 ++ src/server/net.c | 12 ++++++++++++ src/server/net.h | 2 ++ src/server/server.c | 4 +++- src/server/uplink.c | 19 ++++++++++++++++++- src/server/uplink.h | 2 ++ 7 files changed, 41 insertions(+), 2 deletions(-) diff --git a/LOCKS b/LOCKS index a108f1d..e23f4c5 100644 --- a/LOCKS +++ b/LOCKS @@ -21,6 +21,8 @@ pendingLockProduce pendingLockConsume altServersLock client.sendMutex +statisticsSentLock +statisticsReceivedLock If you need to lock multiple clients/images/... at once, lock the client with the lowest array index first. diff --git a/src/server/globals.h b/src/server/globals.h index 3c800aa..9f6a46d 100644 --- a/src/server/globals.h +++ b/src/server/globals.h @@ -57,6 +57,7 @@ struct _dnbd3_connection volatile bool shutdown; // signal this thread to stop, must only be set from uplink_shutdown() or cleanup in uplink_mainloop() int replicatedLastBlock; // bool telling if the last block has been replicated yet uint64_t replicationHandle; // Handle of pending replication request + uint64_t bytesReceived; // Number of bytes received by the connection. }; typedef struct @@ -117,6 +118,7 @@ struct _dnbd3_client int sock; dnbd3_host_t host; dnbd3_image_t *image; + uint64_t bytesSent; pthread_spinlock_t lock; pthread_mutex_t sendMutex; bool isServer; // true if a server in proxy mode, false if real client diff --git a/src/server/net.c b/src/server/net.c index 3046795..9836043 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -44,6 +44,9 @@ #include "../types.h" #include "locks.h" +static uint64_t totalBytesSent = 0; +static pthread_spinlock_t statisticsSentLock; + static inline bool recv_request_header(int sock, dnbd3_request_t *request) { int ret, fails = 0; @@ -112,6 +115,11 @@ static inline bool send_reply(int sock, dnbd3_reply_t *reply, void *payload) return true; } +void net_init() +{ + spin_init( &statisticsSentLock, PTHREAD_PROCESS_PRIVATE ); +} + void *net_client_handler(void *dnbd3_client) { dnbd3_client_t *client = (dnbd3_client_t *)dnbd3_client; @@ -307,6 +315,7 @@ void *net_client_handler(void *dnbd3_client) } done += ret; } + client->bytesSent += request.size; // Increase counter for statistics. } if ( lock ) pthread_mutex_unlock( &client->sendMutex ); break; @@ -359,6 +368,9 @@ set_name: ; } exit_client_cleanup: ; dnbd3_removeClient( client ); + spin_lock( &statisticsSentLock ); + totalBytesSent += client->bytesSent; // Add the amount of bytes send to statistics of the server. + spin_unlock( &statisticsSentLock ); client = dnbd3_freeClient( client ); return NULL ; } diff --git a/src/server/net.h b/src/server/net.h index 2c4115d..c677ae7 100644 --- a/src/server/net.h +++ b/src/server/net.h @@ -21,6 +21,8 @@ #ifndef NET_H_ #define NET_H_ +void net_init(); + void *net_client_handler(void *client_socket); #endif /* NET_H_ */ diff --git a/src/server/server.c b/src/server/server.c index 0547a87..b11b085 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -29,7 +29,6 @@ #include #include #include - #include "../types.h" #include "../version.h" @@ -263,6 +262,8 @@ int main(int argc, char *argv[]) spin_init( &_images_lock, PTHREAD_PROCESS_PRIVATE ); altservers_init(); integrity_init(); + net_init(); + uplink_globalsInit(); logadd( LOG_INFO, "DNBD3 server starting.... Machine type: " ENDIAN_MODE ); if ( altservers_load() < 0 ) { @@ -395,6 +396,7 @@ dnbd3_client_t* dnbd3_initClient(struct sockaddr_storage *client, int fd) return NULL ; } dnbd3_client->sock = fd; + dnbd3_client->bytesSent = 0; spin_init( &dnbd3_client->lock, PTHREAD_PROCESS_PRIVATE ); pthread_mutex_init( &dnbd3_client->sendMutex, NULL ); return dnbd3_client; diff --git a/src/server/uplink.c b/src/server/uplink.c index 469ac4a..b5bd981 100644 --- a/src/server/uplink.c +++ b/src/server/uplink.c @@ -22,6 +22,10 @@ #include #include + +static uint64_t totalBytesReceived = 0; +static pthread_spinlock_t statisticsReceivedLock; + static void* uplink_mainloop(void *data); static void uplink_sendRequests(dnbd3_connection_t *link, bool newOnly); static void uplink_handleReceive(dnbd3_connection_t *link); @@ -31,6 +35,11 @@ static void uplink_sendReplicationRequest(dnbd3_connection_t *link); // ############ uplink connection handling +void uplink_globalsInit() +{ + spin_init( &statisticsReceivedLock, PTHREAD_PROCESS_PRIVATE ); +} + /** * Create and initialize an uplink instance for the given * image. Uplinks run in their own thread. @@ -53,6 +62,7 @@ bool uplink_init(dnbd3_image_t *image, int sock, dnbd3_host_t *host) } link = image->uplink = calloc( 1, sizeof(dnbd3_connection_t) ); link->image = image; + link->bytesReceived = 0; link->queueLen = 0; link->fd = -1; link->signal = -1; @@ -426,6 +436,9 @@ static void* uplink_mainloop(void *data) spin_destroy( &link->queueLock ); free( link->recvBuffer ); link->recvBuffer = NULL; + spin_lock( &statisticsReceivedLock ); + totalBytesReceived += link->bytesReceived; + spin_unlock( &statisticsReceivedLock ); free( link ); return NULL ; } @@ -549,6 +562,7 @@ static void uplink_handleReceive(dnbd3_connection_t *link) struct iovec iov[2]; const uint64_t start = inReply.handle; const uint64_t end = inReply.handle + inReply.size; + link->bytesReceived += inReply.size; // 1) Write to cache file assert( link->image->cacheFd != -1 ); ret = (int)pwrite( link->image->cacheFd, link->recvBuffer, inReply.size, start ); @@ -588,7 +602,10 @@ static void uplink_handleReceive(dnbd3_connection_t *link) served = true; pthread_mutex_lock( &client->sendMutex ); spin_unlock( &link->queueLock ); - if ( client->sock != -1 ) writev( client->sock, iov, 2 ); + if ( client->sock != -1 ) { + ssize_t sent = writev( client->sock, iov, 2 ); + if ( sent > (ssize_t) sizeof outReply ) client->bytesSent += (uint64_t) sent - sizeof outReply; + } pthread_mutex_unlock( &client->sendMutex ); spin_lock( &link->queueLock ); } diff --git a/src/server/uplink.h b/src/server/uplink.h index e77c591..3513022 100644 --- a/src/server/uplink.h +++ b/src/server/uplink.h @@ -4,6 +4,8 @@ #include "../types.h" #include "globals.h" +void uplink_globalsInit(); + bool uplink_init(dnbd3_image_t *image, int sock, dnbd3_host_t *host); void uplink_removeClient(dnbd3_connection_t *uplink, dnbd3_client_t *client); -- cgit v1.2.3-55-g7522