]> arthur.barton.de Git - netdata.git/commitdiff
Merge pull request #472 from ktsaou/master
authorCosta Tsaousis <costa@tsaousis.gr>
Tue, 31 May 2016 22:54:49 +0000 (01:54 +0300)
committerCosta Tsaousis <costa@tsaousis.gr>
Tue, 31 May 2016 22:54:49 +0000 (01:54 +0300)
TCP_CORK when available; gzip compression configuration options

src/global_statistics.c
src/global_statistics.h
src/main.c
src/plugin_proc.c
src/registry.c
src/web_client.c
src/web_client.h

index d813f66c9b9d4d31838a6d7fb44f664cdd412b9a..40d3c3e3da83a6b38e9ea66a3da31a0fc8ae7425 100644 (file)
@@ -5,7 +5,7 @@
 
 #include "global_statistics.h"
 
-struct global_statistics global_statistics = { 0, 0ULL, 0ULL, 0ULL, 0ULL};
+struct global_statistics global_statistics = { 0, 0ULL, 0ULL, 0ULL, 0ULL, 0ULL, 0ULL};
 
 pthread_mutex_t global_statistics_mutex = PTHREAD_MUTEX_INITIALIZER;
 
index b618191656fc3d9537002ded68e4e812da8836f1..56a8118c727fb27f3aea58f7a0ee7f2d6547baa5 100644 (file)
@@ -5,11 +5,13 @@
 // global statistics
 
 struct global_statistics {
-       unsigned long volatile connected_clients;
-       unsigned long long volatile web_requests;
-       unsigned long long volatile web_usec;
-       unsigned long long volatile bytes_received;
-       unsigned long long volatile bytes_sent;
+       volatile unsigned long volatile connected_clients;
+       volatile unsigned long long volatile web_requests;
+       volatile unsigned long long volatile web_usec;
+       volatile unsigned long long volatile bytes_received;
+       volatile unsigned long long volatile bytes_sent;
+       volatile unsigned long long volatile content_size;
+       volatile unsigned long long volatile compressed_content_size;
 };
 
 extern struct global_statistics global_statistics;
index 945728b90fd42e0cc251e9125e11d4a46dd41101..f11460b5518a587e897afce2621271f75254e4aa 100644 (file)
@@ -101,7 +101,36 @@ void web_server_threading_selection(void) {
        }
 
        web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
+
+#ifdef NETDATA_WITH_ZLIB
        web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
+
+       char *s = config_get("global", "web compression strategy", "default");
+       if(!strcmp(s, "default"))
+               web_gzip_strategy = Z_DEFAULT_STRATEGY;
+       else if(!strcmp(s, "filtered"))
+               web_gzip_strategy = Z_FILTERED;
+       else if(!strcmp(s, "huffman only"))
+               web_gzip_strategy = Z_HUFFMAN_ONLY;
+       else if(!strcmp(s, "rle"))
+               web_gzip_strategy = Z_RLE;
+       else if(!strcmp(s, "fixed"))
+               web_gzip_strategy = Z_FIXED;
+       else {
+               error("Invalid compression strategy '%s'. Valid strategies are 'default', 'filtered', 'huffman only', 'rle' and 'fixed'. Proceeding with 'default'.");
+               web_gzip_strategy = Z_DEFAULT_STRATEGY;
+       }
+
+       web_gzip_level = (int)config_get_number("global", "web compression level", 3);
+       if(web_gzip_level < 1) {
+               error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 1 (fastest compression).");
+               web_gzip_level = 1;
+       }
+       else if(web_gzip_level > 9) {
+               error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 9 (best compression).");
+               web_gzip_level = 9;
+       }
+#endif /* NETDATA_WITH_ZLIB */
 }
 
 
@@ -481,7 +510,7 @@ int main(int argc, char **argv)
                error_log_throttle_period = config_get_number("global", "errors flood protection period", error_log_throttle_period);
                setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get("global", "errors flood protection period"    , ""), 1);
 
-               error_log_errors_per_period = config_get_number("global", "errors to trigger flood protection", error_log_errors_per_period);
+               error_log_errors_per_period = (unsigned long)config_get_number("global", "errors to trigger flood protection", error_log_errors_per_period);
                setenv("NETDATA_ERRORS_PER_PERIOD"     , config_get("global", "errors to trigger flood protection", ""), 1);
 
                // --------------------------------------------------------------------
index 5ecd7b9e459a3e7e1daa4114bb63e3f626930061..ffcc32c44a0875b928edf0118bf090026bd112b5 100644 (file)
 
 void *proc_main(void *ptr)
 {
-       static unsigned long long old_web_requests = 0, old_web_usec = 0;
        (void)ptr;
 
+       unsigned long long old_web_requests = 0, old_web_usec = 0,
+                       old_content_size = 0, old_compressed_content_size = 0;
+
+       collected_number compression_ratio = -1, average_response_time = -1;
+
        info("PROC Plugin thread created with task id %d", gettid());
 
        if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
@@ -78,7 +82,8 @@ void *proc_main(void *ptr)
        unsigned long long sunext = (time(NULL) - (time(NULL) % rrd_update_every) + rrd_update_every) * 1000000ULL;
        unsigned long long sunow;
 
-       RRDSET *stcpu = NULL, *stcpu_thread = NULL, *stclients = NULL, *streqs = NULL, *stbytes = NULL, *stduration = NULL;
+       RRDSET *stcpu = NULL, *stcpu_thread = NULL, *stclients = NULL, *streqs = NULL, *stbytes = NULL, *stduration = NULL,
+                       *stcompression = NULL;
 
        for(;1;) {
                if(unlikely(netdata_exit)) break;
@@ -328,13 +333,43 @@ void *proc_main(void *ptr)
                        old_web_usec     = gweb_usec;
                        old_web_requests = gweb_requests;
 
-                       if(!web_requests) web_requests = 1;
+                       if(web_requests)
+                               average_response_time =  web_usec / web_requests;
+
+                       if(average_response_time != -1)
+                               rrddim_set(stduration, "response_time", average_response_time);
 
-                       rrddim_set(stduration, "response_time", web_usec / web_requests);
                        rrdset_done(stduration);
 
                        // ----------------------------------------------------------------
 
+                       if(!stcompression) stcompression = rrdset_find("netdata.compression_ratio");
+                       if(!stcompression) {
+                               stcompression = rrdset_create("netdata", "compression_ratio", NULL, "netdata", NULL, "NetData API Responses Compression Savings Ratio", "percentage", 130500, rrd_update_every, RRDSET_TYPE_LINE);
+
+                               rrddim_add(stcompression, "savings", NULL,  1, 1000, RRDDIM_ABSOLUTE);
+                       }
+                       else rrdset_next(stcompression);
+
+                       unsigned long long gcontent_size            = global_statistics.content_size;
+                       unsigned long long gcompressed_content_size = global_statistics.compressed_content_size;
+
+                       unsigned long long content_size             = gcontent_size            - old_content_size;
+                       unsigned long long compressed_content_size  = gcompressed_content_size - old_compressed_content_size;
+
+                       old_content_size            = gcontent_size;
+                       old_compressed_content_size = gcompressed_content_size;
+
+                       if(content_size)
+                               compression_ratio = (content_size - compressed_content_size) * 100 * 1000 / content_size;
+
+                       if(compression_ratio != -1)
+                               rrddim_set(stcompression, "savings", compression_ratio);
+
+                       rrdset_done(stcompression);
+
+                       // ----------------------------------------------------------------
+
                        registry_statistics();
                }
        }
index f39ce3e2edc2c4350e12173b274a018ffc169b77..a81ab955eca43e95ef915c95529ec8534c78e63c 100644 (file)
@@ -612,7 +612,7 @@ static inline PERSON_URL *registry_person_link_to_url(PERSON *p, MACHINE *m, URL
        else {
                debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
                pu->usages++;
-               if(likely(pu->last_t < when)) pu->last_t = when;
+               if(likely(pu->last_t < (uint32_t)when)) pu->last_t = when;
 
                if(pu->machine != m) {
                        MACHINE_URL *mu = dictionary_get(pu->machine->urls, u->url);
@@ -637,7 +637,7 @@ static inline PERSON_URL *registry_person_link_to_url(PERSON *p, MACHINE *m, URL
        }
 
        p->usages++;
-       if(likely(p->last_t < when)) p->last_t = when;
+       if(likely(p->last_t < (uint32_t)when)) p->last_t = when;
 
        if(pu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
                info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL. Re-enabling URL.", p->guid, m->guid, u->url);
@@ -663,14 +663,14 @@ static inline MACHINE_URL *registry_machine_link_to_url(PERSON *p, MACHINE *m, U
        else {
                debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
                mu->usages++;
-               if(likely(mu->last_t < when)) mu->last_t = when;
+               if(likely(mu->last_t < (uint32_t)when)) mu->last_t = when;
        }
 
        //debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): indexing person in machine", p->guid, m->guid, u->url);
        //dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
 
        m->usages++;
-       if(likely(m->last_t < when)) m->last_t = when;
+       if(likely(m->last_t < (uint32_t)when)) m->last_t = when;
 
        if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
                info("registry_machine_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
@@ -1024,8 +1024,8 @@ static inline void registry_set_person_cookie(struct web_client *w, PERSON *p) {
 }
 
 static inline void registry_json_header(struct web_client *w, const char *action, const char *status) {
-       w->response.data->contenttype = CT_APPLICATION_JSON;
        buffer_flush(w->response.data);
+       w->response.data->contenttype = CT_APPLICATION_JSON;
        buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
                                   action, status, registry.hostname, registry.machine_guid);
 }
index 660d7c656ae595e3689e30fa9e31510c70cc3bfa..bc1071ea0e7344e23fca5e4a7605ae050d3f9f8e 100644 (file)
 #include <pthread.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <netinet/tcp.h>
 #include <malloc.h>
 #include <pwd.h>
 #include <grp.h>
 #include <ctype.h>
 #include <poll.h>
 
+// TCP_CORK
+#include <netinet/tcp.h>
+
 #include "common.h"
 #include "log.h"
 #include "appconfig.h"
 #include "registry.h"
 
 #include "web_client.h"
-#include "../config.h"
 
 #define INITIAL_WEB_DATA_LENGTH 16384
 #define WEB_REQUEST_LENGTH 16384
 #define TOO_BIG_REQUEST 16384
 
 int web_client_timeout = DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS;
-int web_enable_gzip = 1;
+
+#ifdef NETDATA_WITH_ZLIB
+int web_enable_gzip = 1, web_gzip_level = 3, web_gzip_strategy = Z_DEFAULT_STRATEGY;
+#endif /* NETDATA_WITH_ZLIB */
 
 extern int netdata_exit;
 
 struct web_client *web_clients = NULL;
 unsigned long long web_clients_count = 0;
 
+inline int web_client_crock_socket(struct web_client *w) {
+#ifdef TCP_CORK
+       if(likely(!w->tcp_cork && w->ofd != -1)) {
+               w->tcp_cork = 1;
+               if(unlikely(setsockopt(w->ofd, IPPROTO_TCP, TCP_CORK, (char *) &w->tcp_cork, sizeof(int)) != 0)) {
+                       error("%llu: failed to enable TCP_CORK on socket.", w->id);
+                       w->tcp_cork = 0;
+                       return -1;
+               }
+       }
+#endif /* TCP_CORK */
+
+       return 0;
+}
+
+inline int web_client_uncrock_socket(struct web_client *w) {
+#ifdef TCP_CORK
+       if(likely(w->tcp_cork && w->ofd != -1)) {
+               w->tcp_cork = 0;
+               if(unlikely(setsockopt(w->ofd, IPPROTO_TCP, TCP_CORK, (char *) &w->tcp_cork, sizeof(int)) != 0)) {
+                       error("%llu: failed to disable TCP_CORK on socket.", w->id);
+                       w->tcp_cork = 1;
+                       return -1;
+               }
+       }
+#endif /* TCP_CORK */
+
+       return 0;
+}
+
 struct web_client *web_client_create(int listener)
 {
        struct web_client *w;
@@ -81,7 +115,6 @@ struct web_client *web_client_create(int listener)
                w->client_port[NI_MAXSERV] = '\0';
 
                switch(sadr->sa_family) {
-
                case AF_INET:
                        debug(D_WEB_CLIENT_ACCESS, "%llu: New IPv4 web client from %s port %s on socket %d.", w->id, w->client_ip, w->client_port, w->ifd);
                        break;
@@ -101,8 +134,14 @@ struct web_client *web_client_create(int listener)
                }
 
                int flag = 1;
+               if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0)
+                       error("%llu: failed to enable TCP_NODELAY on socket.", w->id);
+
+               flag = 1;
                if(setsockopt(w->ifd, SOL_SOCKET, SO_KEEPALIVE, (char *) &flag, sizeof(int)) != 0)
                        error("%llu: Cannot set SO_KEEPALIVE on socket.", w->id);
+
+
        }
 
        w->response.data = buffer_create(INITIAL_WEB_DATA_LENGTH);
@@ -144,14 +183,24 @@ struct web_client *web_client_create(int listener)
        return(w);
 }
 
-void web_client_reset(struct web_client *w)
-{
-       struct timeval tv;
-       gettimeofday(&tv, NULL);
+void web_client_reset(struct web_client *w) {
+       web_client_uncrock_socket(w);
 
        debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);
 
-       if(w->stats_received_bytes || w->stats_sent_bytes) {
+       if(likely(w->last_url[0])) {
+               struct timeval tv;
+               gettimeofday(&tv, NULL);
+
+               size_t size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               size_t sent = size;
+#ifdef NETDATA_WITH_ZLIB
+               if(likely(w->response.zoutput)) sent = (size_t)w->response.zstream.total_out;
+#endif
+
+               // --------------------------------------------------------------------
+               // global statistics
+
                if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
                        global_statistics_lock();
 
@@ -159,32 +208,31 @@ void web_client_reset(struct web_client *w)
                global_statistics.web_usec += usecdiff(&tv, &w->tv_in);
                global_statistics.bytes_received += w->stats_received_bytes;
                global_statistics.bytes_sent += w->stats_sent_bytes;
+               global_statistics.content_size += size;
+               global_statistics.compressed_content_size += sent;
 
                if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
                        global_statistics_unlock();
-       }
-       w->stats_received_bytes = 0;
-       w->stats_sent_bytes = 0;
 
-       size_t sent = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               w->stats_received_bytes = 0;
+               w->stats_sent_bytes = 0;
 
-#ifdef NETDATA_WITH_ZLIB
-       if(likely(w->response.zoutput)) sent = (size_t)w->response.zstream.total_out;
-#endif
 
-       size_t size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               // --------------------------------------------------------------------
+               // access log
 
-       if(likely(w->last_url[0]))
                log_access("%llu: (sent/all = %zu/%zu bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %s: %d '%s'",
-                       w->id,
-                       sent, size, -((size>0)?((float)(size-sent)/(float)size * 100.0):0.0),
-                       (float)usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
-                       (float)usecdiff(&tv, &w->tv_ready) / 1000.0,
-                       (float)usecdiff(&tv, &w->tv_in) / 1000.0,
-                       (w->mode == WEB_CLIENT_MODE_FILECOPY)?"filecopy":((w->mode == WEB_CLIENT_MODE_OPTIONS)?"options":"data"),
-                       w->response.code,
-                       w->last_url
+                                  w->id,
+                                  sent, size, -((size > 0) ? ((float) (size - sent) / (float) size * 100.0) : 0.0),
+                                  (float) usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
+                                  (float) usecdiff(&tv, &w->tv_ready) / 1000.0,
+                                  (float) usecdiff(&tv, &w->tv_in) / 1000.0,
+                                  (w->mode == WEB_CLIENT_MODE_FILECOPY) ? "filecopy" : ((w->mode == WEB_CLIENT_MODE_OPTIONS)
+                                                                                                                                                ? "options" : "data"),
+                                  w->response.code,
+                                  w->last_url
                );
+       }
 
        if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY)) {
                if(w->ifd != w->ofd) {
@@ -220,7 +268,7 @@ void web_client_reset(struct web_client *w)
        // if we had enabled compression, release it
 #ifdef NETDATA_WITH_ZLIB
        if(w->response.zinitialized) {
-               debug(D_DEFLATE, "%llu: Reseting compression.", w->id);
+               debug(D_DEFLATE, "%llu: Freeing compression resources.", w->id);
                deflateEnd(&w->response.zstream);
                w->response.zsent = 0;
                w->response.zhave = 0;
@@ -234,17 +282,13 @@ void web_client_reset(struct web_client *w)
 }
 
 struct web_client *web_client_free(struct web_client *w) {
+       web_client_reset(w);
+
        struct web_client *n = w->next;
        if(w == web_clients) web_clients = n;
 
        debug(D_WEB_CLIENT_ACCESS, "%llu: Closing web client from %s port %s.", w->id, w->client_ip, w->client_port);
 
-#ifdef NETDATA_WITH_ZLIB
-       if(w->response.zinitialized) {
-               deflateEnd(&w->response.zstream);
-       }
-#endif // NETDATA_WITH_ZLIB
-
        if(w->prev)     w->prev->next = w->next;
        if(w->next) w->next->prev = w->prev;
        if(w->response.header_output) buffer_free(w->response.header_output);
@@ -468,7 +512,7 @@ void web_client_enable_deflate(struct web_client *w, int gzip) {
 //     }
 
        // Select GZIP compression: windowbits = 15 + 16 = 31
-       if(deflateInit2(&w->response.zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + ((gzip)?16:0), 8, Z_DEFAULT_STRATEGY) != Z_OK) {
+       if(deflateInit2(&w->response.zstream, web_gzip_level, Z_DEFLATED, 15 + ((gzip)?16:0), 8, web_gzip_strategy) != Z_OK) {
                error("%llu: Failed to initialize zlib. Proceeding without compression.", w->id);
                return;
        }
@@ -1726,11 +1770,6 @@ void web_client_process(struct web_client *w) {
 
        buffer_strcat(w->response.header_output, "\r\n");
 
-/*     // disable TCP_NODELAY, to buffer the header
-       int flag = 0;
-       if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0)
-               error("%llu: failed to disable TCP_NODELAY on socket.", w->id);
-*/
        // sent the HTTP header
        debug(D_WEB_DATA, "%llu: Sending response HTTP header of size %d: '%s'"
                        , w->id
@@ -1738,6 +1777,8 @@ void web_client_process(struct web_client *w) {
                        , buffer_tostring(w->response.header_output)
                        );
 
+       web_client_crock_socket(w);
+
        bytes = send(w->ofd, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output), 0);
        if(bytes != (ssize_t) buffer_strlen(w->response.header_output)) {
                if(bytes > 0)
@@ -1752,11 +1793,6 @@ void web_client_process(struct web_client *w) {
        else 
                w->stats_sent_bytes += bytes;
 
-/*     // enable TCP_NODELAY, to send all data immediately at the next send()
-       flag = 1;
-       if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0)
-               error("%llu: failed to enable TCP_NODELAY on socket.", w->id);
-*/
        // enable sending immediately if we have data
        if(w->response.data->len) w->wait_send = 1;
        else w->wait_send = 0;
@@ -1803,7 +1839,7 @@ ssize_t web_client_send_chunk_header(struct web_client *w, size_t len)
 {
        debug(D_DEFLATE, "%llu: OPEN CHUNK of %d bytes (hex: %x).", w->id, len, len);
        char buf[1024];
-       sprintf(buf, "%lX\r\n", len);
+       sprintf(buf, "%zX\r\n", len);
        
        ssize_t bytes = send(w->ofd, buf, strlen(buf), 0);
        if(bytes > 0) {
index f2dc554a3ecf72ab6e928f6490248e4c97a69b94..70fb8a0b8980cba19c27401100bb21e7cf0c7f94 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <sys/time.h>
 #include <string.h>
+#include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 #define DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS 60
 extern int web_client_timeout;
-extern int web_enable_gzip;
+
+#ifdef NETDATA_WITH_ZLIB
+extern int web_enable_gzip, web_gzip_level, web_gzip_strategy;
+#endif /* NETDATA_WITH_ZLIB */
 
 #ifndef NETDATA_WEB_CLIENT_H
 #define NETDATA_WEB_CLIENT_H 1
@@ -47,7 +51,7 @@ struct response {
        size_t zsent;                                   // the compressed bytes we have sent to the client
        size_t zhave;                                   // the compressed bytes that we have received from zlib
        int zinitialized:1;
-#endif
+#endif /* NETDATA_WITH_ZLIB */
 
 };
 
@@ -61,13 +65,14 @@ struct web_client {
        uint8_t dead:1;                                         // if set to 1, this client is dead
 
        uint8_t keepalive:1;                            // if set to 1, the web client will be re-used
-       uint8_t enable_gzip:1;                          // if set to 1, the response will be compressed
 
        uint8_t mode:3;                                         // the operational mode of the client
 
        uint8_t wait_receive:1;                         // 1 = we are waiting more input data
        uint8_t wait_send:1;                            // 1 = we have data to send to the client
 
+       int tcp_cork;                                           // 1 = we have a cork on the socket
+
        int ifd;
        int ofd;