From fa900b4658b21294682760bdf84071cbb2b79aa4 Mon Sep 17 00:00:00 2001 From: Stephan Schwaer Date: Mon, 4 May 2015 17:23:42 +0200 Subject: Removed dnbd3_printClients, added clients to statistics json in rpc. --- src/server/net.c | 20 ++++++++++++++++--- src/server/net.h | 4 ++++ src/server/rpc.c | 57 +++++++++++++++++++++++++++++++++++++++++++---------- src/server/rpc.h | 1 - src/server/server.c | 15 -------------- src/server/uplink.c | 20 ++++++++++++++++--- src/server/uplink.h | 4 ++++ 7 files changed, 89 insertions(+), 32 deletions(-) diff --git a/src/server/net.c b/src/server/net.c index 2edb398..dc53701 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -116,6 +116,22 @@ static inline bool send_reply(int sock, dnbd3_reply_t *reply, void *payload) return true; } +uint64_t net_getTotalBytesSent() +{ + spin_lock( &statisticsSentLock ); + uint64_t tmp = totalBytesSent; + spin_unlock( &statisticsSentLock ); + return tmp; +} + +void net_addTotalBytesSent(uint64_t sentBytes) +{ + spin_lock( &statisticsSentLock ); + totalBytesSent += sentBytes; + spin_unlock( &statisticsSentLock ); + return; +} + void net_init() { spin_init( &statisticsSentLock, PTHREAD_PROCESS_PRIVATE ); @@ -372,9 +388,7 @@ 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 ); + net_addTotalBytesSent( client->bytesSent ); // Add the amount of bytes sent by the client to the statistics of the server. client = dnbd3_freeClient( client ); return NULL ; } diff --git a/src/server/net.h b/src/server/net.h index c677ae7..e8bef34 100644 --- a/src/server/net.h +++ b/src/server/net.h @@ -23,6 +23,10 @@ void net_init(); +uint64_t net_getTotalBytesSent(); + +void net_addTotalBytesSent(uint64_t sentBytes); + void *net_client_handler(void *client_socket); #endif /* NET_H_ */ diff --git a/src/server/rpc.c b/src/server/rpc.c index b2dc088..ae01a0e 100644 --- a/src/server/rpc.c +++ b/src/server/rpc.c @@ -1,18 +1,55 @@ #include +#include #include #include "rpc.h" +#include "server.h" +#include "net.h" +#include "uplink.h" +#include "log.h" +#include "locks.h" +#include "helper.h" + +static void clientsToJson(json_t *jsonClients); void rpc_sendStatsJson(int sock) { - int receivedBytes = 0; - int sentBytes = 1; - json_t *statisticsJson = json_pack( "{sisi}", "receivedBytes", receivedBytes, "sentBytes", sentBytes ); - char* jsonString = json_dumps(statisticsJson, 0); - char bla[500]; - snprintf(bla, sizeof bla, "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n", (int)strlen(jsonString)); - write( sock, bla, strlen(bla) ); - int n = write( sock, jsonString, strlen(jsonString)); - json_decref(statisticsJson); - free(jsonString); + uint64_t receivedBytes = uplink_getTotalBytesReceived(); + uint64_t sentBytes = net_getTotalBytesSent(); + + json_t *jsonClients = json_array(); + clientsToJson( jsonClients ); + + json_t *statisticsJson = json_pack( "{sisi}", "receivedBytes", (json_int_t) receivedBytes, "sentBytes", (json_int_t) sentBytes ); + json_object_set( statisticsJson, "clients", jsonClients ); + char *jsonString = json_dumps( statisticsJson, 0 ); + + char buffer[500]; + snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n", + (int) strlen( jsonString ) ); + write( sock, buffer, strlen( buffer ) ); + write( sock, jsonString, strlen( jsonString ) ); + json_decref( statisticsJson ); + json_decref( jsonClients ); + free( jsonString ); +} + +static void clientsToJson(json_t *jsonClients) +{ + json_t *clientStats; + int i; + char clientName[100]; + const char *imageName; + spin_lock( &_clients_lock ); + for (i = 0; i < _num_clients; ++i) { + if ( _clients[i] == NULL ) continue; + spin_lock( &_clients[i]->lock ); + host_to_string( &_clients[i]->host, clientName, sizeof(clientName) ); + imageName =_clients[i]->image != NULL ? _clients[i]->image->lower_name : "NULL"; + clientStats = json_pack( "{sssssi}", "client", clientName, "image", imageName , "bytesSent", (json_int_t) _clients[i]->bytesSent ); + json_array_append( jsonClients, clientStats ); + json_decref( clientStats ); + spin_unlock( &_clients[i]->lock ); + } + spin_unlock( &_clients_lock ); } diff --git a/src/server/rpc.h b/src/server/rpc.h index 78585cf..ec69762 100644 --- a/src/server/rpc.h +++ b/src/server/rpc.h @@ -1,7 +1,6 @@ #ifndef _RPC_H_ #define _RPC_H_ - void rpc_sendStatsJson(int sock); #endif diff --git a/src/server/server.c b/src/server/server.c index b11b085..a5bfc3e 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -488,18 +488,3 @@ int dnbd3_serverUptime() return (int)(time( NULL ) - startupTime); } -static void dnbd3_printClients() -{ - int i; - char buffer[100]; - spin_lock( &_clients_lock ); - for (i = 0; i < _num_clients; ++i) { - if ( _clients[i] == NULL ) continue; - spin_lock( &_clients[i]->lock ); - host_to_string( &_clients[i]->host, buffer, sizeof(buffer) ); - logadd( LOG_DEBUG1, "Client %s", buffer ); - if ( _clients[i]->image != NULL ) logadd( LOG_DEBUG1, " `- Image: %s\n", _clients[i]->image->lower_name ); - spin_unlock( &_clients[i]->lock ); - } - spin_unlock( &_clients_lock ); -} diff --git a/src/server/uplink.c b/src/server/uplink.c index b5bd981..d76f030 100644 --- a/src/server/uplink.c +++ b/src/server/uplink.c @@ -40,6 +40,22 @@ void uplink_globalsInit() spin_init( &statisticsReceivedLock, PTHREAD_PROCESS_PRIVATE ); } +uint64_t uplink_getTotalBytesReceived() +{ + spin_lock( &statisticsReceivedLock ); + int tmp = totalBytesReceived; + spin_unlock( &statisticsReceivedLock ); + return tmp; +} + +void uplink_addTotalBytesReceived(int receivedBytes) +{ + spin_lock( &statisticsReceivedLock ); + totalBytesReceived += receivedBytes; + spin_unlock( &statisticsReceivedLock ); + return; +} + /** * Create and initialize an uplink instance for the given * image. Uplinks run in their own thread. @@ -436,9 +452,7 @@ 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 ); + uplink_addTotalBytesReceived( link->bytesReceived ); free( link ); return NULL ; } diff --git a/src/server/uplink.h b/src/server/uplink.h index 3513022..c0a68de 100644 --- a/src/server/uplink.h +++ b/src/server/uplink.h @@ -6,6 +6,10 @@ void uplink_globalsInit(); +uint64_t uplink_getTotalBytesReceived(); + +void uplink_addTotalBytesReceived(int receivedBytes); + 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