#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;
// 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;
}
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 */
}
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);
// --------------------------------------------------------------------
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)
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;
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();
}
}
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);
}
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);
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);
}
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);
}
#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;
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;
}
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);
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();
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) {
// 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;
}
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);
// }
// 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;
}
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
, 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)
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;
{
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) {
#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
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 */
};
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;