From 0ab1cfacc95abecc2d4894bdd4a1fdc5f0efe28c Mon Sep 17 00:00:00 2001 From: Unbit Date: Tue, 26 Nov 2013 07:13:07 +0100 Subject: [PATCH] added read errors counters and --log-ioerror --- core/logging.c | 3 +++ core/master_utils.c | 3 +++ core/metrics.c | 3 +++ core/reader.c | 22 ++++++++++++++++++++++ core/utils.c | 1 + core/uwsgi.c | 1 + uwsgi.h | 3 +++ 7 files changed, 36 insertions(+) diff --git a/core/logging.c b/core/logging.c index c08a68c2..8efdf8d7 100644 --- a/core/logging.c +++ b/core/logging.c @@ -575,6 +575,9 @@ void log_request(struct wsgi_request *wsgi_req) { if (uwsgi.shared->options[UWSGI_OPTION_LOG_SENDFILE] && wsgi_req->via == UWSGI_VIA_SENDFILE) { goto logit; } + if (uwsgi.shared->options[UWSGI_OPTION_LOG_IOERROR] && wsgi_req->read_errors > 0 && wsgi_req->write_errors > 0) { + goto logit; + } if (!log_it) return; diff --git a/core/master_utils.c b/core/master_utils.c index 3125bc27..b046d81d 100644 --- a/core/master_utils.c +++ b/core/master_utils.c @@ -1133,6 +1133,9 @@ struct uwsgi_stats *uwsgi_master_generate_stats() { if (uwsgi_stats_keylong_comma(us, "write_errors", (unsigned long long) uc->write_errors)) goto end; + if (uwsgi_stats_keylong_comma(us, "read_errors", (unsigned long long) uc->read_errors)) + goto end; + if (uwsgi_stats_keylong_comma(us, "in_request", (unsigned long long) uc->in_request)) goto end; diff --git a/core/metrics.c b/core/metrics.c index f83f8c7c..0c8741cd 100644 --- a/core/metrics.c +++ b/core/metrics.c @@ -784,6 +784,9 @@ void uwsgi_setup_metrics() { uwsgi_metric_name2("worker.%d.core.%d.exceptions", i, j) ; uwsgi_metric_oid2("3.%d.2.%d.7", i, j); uwsgi_register_metric(buf, buf2, UWSGI_METRIC_COUNTER, "ptr", &uwsgi.workers[i].cores[j].exceptions, 0, NULL); + uwsgi_metric_name2("worker.%d.core.%d.read_errors", i, j) ; uwsgi_metric_oid2("3.%d.2.%d.8", i, j); + uwsgi_register_metric(buf, buf2, UWSGI_METRIC_COUNTER, "ptr", &uwsgi.workers[i].cores[j].read_errors, 0, NULL); + } } diff --git a/core/reader.c b/core/reader.c index 1d402113..6cec6540 100644 --- a/core/reader.c +++ b/core/reader.c @@ -33,6 +33,7 @@ void uwsgi_request_body_seek(struct wsgi_request *wsgi_req, off_t pos) { if (pos < 0) { if (fseek(wsgi_req->post_file, pos, SEEK_CUR)) { uwsgi_error("uwsgi_request_body_seek()/fseek()"); + wsgi_req->read_errors++; } wsgi_req->post_pos = ftell(wsgi_req->post_file); return; @@ -40,6 +41,7 @@ void uwsgi_request_body_seek(struct wsgi_request *wsgi_req, off_t pos) { if (fseek(wsgi_req->post_file, pos, SEEK_SET)) { uwsgi_error("uwsgi_request_body_seek()/fseek()"); + wsgi_req->read_errors++; } wsgi_req->post_pos = ftell(wsgi_req->post_file); return; @@ -140,6 +142,7 @@ static int consume_body_for_readline(struct wsgi_request *wsgi_req) { } if (len == 0) { uwsgi_read_error(remains); + wsgi_req->read_errors++; return -1; } if (len < 0) { @@ -147,6 +150,7 @@ static int consume_body_for_readline(struct wsgi_request *wsgi_req) { goto wait; } uwsgi_read_error(remains); + wsgi_req->read_errors++; return -1; } wait: @@ -159,6 +163,7 @@ wait: return 0; } uwsgi_read_error(remains); + wsgi_req->read_errors++; return -1; } // 0 means timeout @@ -167,6 +172,7 @@ wait: return -1; } uwsgi_read_error(remains); + wsgi_req->read_errors++; return -1; } @@ -205,6 +211,7 @@ char *uwsgi_request_body_readline(struct wsgi_request *wsgi_req, ssize_t hint, s wsgi_req->post_readline_buf = malloc(amount); if (!wsgi_req->post_readline_buf) { uwsgi_error("uwsgi_request_body_readline()/malloc()"); + wsgi_req->read_errors++; *rlen = -1; return NULL; } @@ -217,6 +224,7 @@ char *uwsgi_request_body_readline(struct wsgi_request *wsgi_req, ssize_t hint, s if (wsgi_req->post_pos >= wsgi_req->post_cl) break; if (consume_body_for_readline(wsgi_req)) { + wsgi_req->read_errors++; *rlen = -1; return NULL; } @@ -274,6 +282,7 @@ char *uwsgi_request_body_read(struct wsgi_request *wsgi_req, ssize_t hint, ssize if (!tmp_buf) { uwsgi_error("uwsgi_request_body_read()/realloc()"); *rlen = -1; + wsgi_req->read_errors++; return NULL; } wsgi_req->post_read_buf = tmp_buf; @@ -321,6 +330,7 @@ char *uwsgi_request_body_read(struct wsgi_request *wsgi_req, ssize_t hint, ssize wsgi_req->post_read_buf = malloc(remains); if (!wsgi_req->post_read_buf) { uwsgi_error("uwsgi_request_body_read()/malloc()"); + wsgi_req->read_errors++; *rlen = -1; return NULL; } @@ -332,6 +342,7 @@ char *uwsgi_request_body_read(struct wsgi_request *wsgi_req, ssize_t hint, ssize char *tmp_buf = realloc(wsgi_req->post_read_buf, (remains+*rlen)); if (!tmp_buf) { uwsgi_error("uwsgi_request_body_read()/realloc()"); + wsgi_req->read_errors++; *rlen = -1; return NULL; } @@ -349,6 +360,7 @@ char *uwsgi_request_body_read(struct wsgi_request *wsgi_req, ssize_t hint, ssize if (fread(wsgi_req->post_read_buf + *rlen, remains, 1, wsgi_req->post_file) != 1) { *rlen = -1; uwsgi_error("uwsgi_request_body_read()/fread()"); + wsgi_req->read_errors++; return NULL; } *rlen += remains; @@ -378,6 +390,7 @@ char *uwsgi_request_body_read(struct wsgi_request *wsgi_req, ssize_t hint, ssize } *rlen = -1; uwsgi_read_error(remains); + wsgi_req->read_errors++; return NULL; } wait: @@ -398,6 +411,7 @@ wait: } else { uwsgi_read_error(remains); + wsgi_req->read_errors++; } return NULL; } @@ -409,6 +423,7 @@ wait: } *rlen = -1; uwsgi_read_error(remains); + wsgi_req->read_errors++; return NULL; } @@ -447,6 +462,7 @@ int uwsgi_postbuffer_do_in_mem(struct wsgi_request *wsgi_req) { goto wait; } uwsgi_read_error(remains); + wsgi_req->read_errors++; return -1; } @@ -462,6 +478,7 @@ wait: } if (ret < 0) { uwsgi_read_error(remains); + wsgi_req->read_errors++; return -1; } uwsgi_read_timeout(remains); @@ -483,6 +500,7 @@ int uwsgi_postbuffer_do_in_disk(struct wsgi_request *wsgi_req) { wsgi_req->post_file = uwsgi_tmpfile(); if (!wsgi_req->post_file) { uwsgi_error("uwsgi_postbuffer_do_in_disk()/uwsgi_tmpfile()"); + wsgi_req->read_errors++; return -1; } @@ -518,6 +536,7 @@ int uwsgi_postbuffer_do_in_disk(struct wsgi_request *wsgi_req) { goto wait; } uwsgi_read_error(remains); + wsgi_req->read_errors++; goto end; } @@ -531,11 +550,13 @@ wait: } else { uwsgi_read_error(remains); + wsgi_req->read_errors++; } goto end; } if (ret < 0) { uwsgi_read_error(remains); + wsgi_req->read_errors++; goto end; } uwsgi_read_timeout(remains); @@ -544,6 +565,7 @@ wait: write: if (fwrite(wsgi_req->post_buffering_buf, rlen, 1, wsgi_req->post_file) != 1) { uwsgi_error("uwsgi_postbuffer_do_in_disk()/fwrite()"); + wsgi_req->read_errors++; goto end; } diff --git a/core/utils.c b/core/utils.c index 38e918af..ea94ebf8 100644 --- a/core/utils.c +++ b/core/utils.c @@ -1038,6 +1038,7 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) { uwsgi.workers[uwsgi.mywid].requests++; uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].requests++; uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].write_errors += wsgi_req->write_errors; + uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].read_errors += wsgi_req->read_errors; // this is used for MAX_REQUESTS uwsgi.workers[uwsgi.mywid].delta_requests++; } diff --git a/core/uwsgi.c b/core/uwsgi.c index ee890913..a80de4e3 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -666,6 +666,7 @@ static struct uwsgi_option uwsgi_base_options[] = { {"log-5xx", no_argument, 0, "log requests with a 5xx response", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_5xx, 0}, {"log-big", required_argument, 0, "log requestes bigger than the specified size", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_LOG_BIG, 0}, {"log-sendfile", required_argument, 0, "log sendfile requests", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_SENDFILE, 0}, + {"log-ioerror", required_argument, 0, "log requests with io errors", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_IOERROR, 0}, {"log-micros", no_argument, 0, "report response time in microseconds instead of milliseconds", uwsgi_opt_true, &uwsgi.log_micros, 0}, {"log-x-forwarded-for", no_argument, 0, "use the ip from X-Forwarded-For header instead of REMOTE_ADDR", uwsgi_opt_true, &uwsgi.log_x_forwarded_for, 0}, {"master-as-root", no_argument, 0, "leave master process running as root", uwsgi_opt_true, &uwsgi.master_as_root, 0}, diff --git a/uwsgi.h b/uwsgi.h index ecf86f4a..99862804 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -846,6 +846,7 @@ struct uwsgi_opt { #define UWSGI_OPTION_MULE_HARAKIRI 18 #define UWSGI_OPTION_MAX_WORKER_LIFETIME 19 #define UWSGI_OPTION_MIN_WORKER_LIFETIME 20 +#define UWSGI_OPTION_LOG_IOERROR 21 #define UWSGI_SPOOLER_EXTERNAL 1 @@ -1436,6 +1437,7 @@ struct wsgi_request { int suspended; uint64_t write_errors; + uint64_t read_errors; int *ovector; size_t post_cl; @@ -2725,6 +2727,7 @@ struct uwsgi_core { uint64_t offloaded_requests; uint64_t write_errors; + uint64_t read_errors; uint64_t exceptions; pthread_t thread_id;