From: Fabian Keil Date: Tue, 16 Mar 2021 18:34:52 +0000 (+0100) Subject: FEATURE_STATISTICS: Include all requests in the statistics X-Git-Tag: v_3_0_34~132 X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=commitdiff_plain;h=7c3ed8b407e462da82d2a25a8760c7d32ea4e899 FEATURE_STATISTICS: Include all requests in the statistics ... if mutexes are available. Previously in case of reused connections only the last request got counted. The statistics still aren't perfect but it's an improvement. --- diff --git a/cgisimple.c b/cgisimple.c index 96506273..142a4910 100644 --- a/cgisimple.c +++ b/cgisimple.c @@ -1262,9 +1262,15 @@ jb_err cgi_show_status(struct client_state *csp, char buf[BUFFER_SIZE]; #ifdef FEATURE_STATISTICS +#ifdef MUTEX_LOCKS_AVAILABLE + float percentage_blocked; + unsigned long long local_number_of_requests_received; + unsigned long long local_number_of_requests_blocked; +#else float perc_rej; /* Percentage of http requests rejected */ int local_urls_read; int local_urls_rejected; +#endif #endif /* ndef FEATURE_STATISTICS */ jb_err err = JB_ERR_OK; @@ -1302,9 +1308,15 @@ jb_err cgi_show_status(struct client_state *csp, } #ifdef FEATURE_STATISTICS +#ifdef MUTEX_LOCKS_AVAILABLE + privoxy_mutex_lock(&block_statistics_mutex); + local_number_of_requests_received = number_of_requests_received; + local_number_of_requests_blocked = number_of_requests_blocked; + privoxy_mutex_unlock(&block_statistics_mutex); +#else local_urls_read = urls_read; local_urls_rejected = urls_rejected; - +#endif /* * Need to alter the stats not to include the fetch of this * page. @@ -1315,7 +1327,11 @@ jb_err cgi_show_status(struct client_state *csp, * urls_rejected--; * This will be incremented subsequently * */ +#ifdef MUTEX_LOCKS_AVAILABLE + if (local_number_of_requests_received == 0) +#else if (local_urls_read == 0) +#endif { if (!err) err = map_block_killer(exports, "have-stats"); } @@ -1323,6 +1339,19 @@ jb_err cgi_show_status(struct client_state *csp, { if (!err) err = map_block_killer(exports, "have-no-stats"); +#ifdef MUTEX_LOCKS_AVAILABLE + percentage_blocked = (float)local_number_of_requests_blocked * 100.0F / + (float)local_number_of_requests_received; + + snprintf(buf, sizeof(buf), "%llu", local_number_of_requests_received); + if (!err) err = map(exports, "requests-received", 1, buf, 1); + + snprintf(buf, sizeof(buf), "%llu", local_number_of_requests_blocked); + if (!err) err = map(exports, "requests-blocked", 1, buf, 1); + + snprintf(buf, sizeof(buf), "%6.2f", percentage_blocked); + if (!err) err = map(exports, "percent-blocked", 1, buf, 1); +#else perc_rej = (float)local_urls_rejected * 100.0F / (float)local_urls_read; @@ -1334,6 +1363,7 @@ jb_err cgi_show_status(struct client_state *csp, snprintf(buf, sizeof(buf), "%6.2f", perc_rej); if (!err) err = map(exports, "percent-blocked", 1, buf, 1); +#endif } #else /* ndef FEATURE_STATISTICS */ diff --git a/jcc.c b/jcc.c index ef79d606..563c6153 100644 --- a/jcc.c +++ b/jcc.c @@ -127,6 +127,10 @@ struct file_list files[1]; #ifdef FEATURE_STATISTICS int urls_read = 0; /* total nr of urls read inc rejected */ int urls_rejected = 0; /* total nr of urls rejected */ +#ifdef MUTEX_LOCKS_AVAILABLE +unsigned long long number_of_requests_received = 0; +unsigned long long number_of_requests_blocked = 0; +#endif #endif /* def FEATURE_STATISTICS */ #ifdef FEATURE_GRACEFUL_TERMINATION @@ -191,6 +195,9 @@ privoxy_mutex_t external_filter_mutex; #ifdef FEATURE_CLIENT_TAGS privoxy_mutex_t client_tags_mutex; #endif +#ifdef FEATURE_STATISTICS +privoxy_mutex_t block_statistics_mutex; +#endif #ifdef FEATURE_EXTENDED_STATISTICS privoxy_mutex_t filter_statistics_mutex; privoxy_mutex_t block_reason_statistics_mutex; @@ -945,6 +952,11 @@ static int crunch_response_triggered(struct client_state *csp, const struct crun #ifdef FEATURE_STATISTICS if (c->flags & CF_COUNT_AS_REJECT) { +#ifdef MUTEX_LOCKS_AVAILABLE + privoxy_mutex_lock(&block_statistics_mutex); + number_of_requests_blocked++; + privoxy_mutex_unlock(&block_statistics_mutex); +#endif csp->flags |= CSP_FLAG_REJECTED; } #endif /* def FEATURE_STATISTICS */ @@ -2966,6 +2978,12 @@ static void continue_https_chat(struct client_state *csp) return; } +#if defined(FEATURE_STATISTICS) && defined(MUTEX_LOCKS_AVAILABLE) + privoxy_mutex_lock(&block_statistics_mutex); + number_of_requests_received++; + privoxy_mutex_unlock(&block_statistics_mutex); +#endif + csp->requests_received_total++; /* @@ -4207,6 +4225,13 @@ static void chat(struct client_state *csp) { return; } + +#if defined(FEATURE_STATISTICS) && defined(MUTEX_LOCKS_AVAILABLE) + privoxy_mutex_lock(&block_statistics_mutex); + number_of_requests_received++; + privoxy_mutex_unlock(&block_statistics_mutex); +#endif + if (parse_client_request(csp) != JB_ERR_OK) { return; @@ -5281,6 +5306,9 @@ static void initialize_mutexes(void) #ifdef FEATURE_CLIENT_TAGS privoxy_mutex_init(&client_tags_mutex); #endif +#ifdef FEATURE_STATISTICS + privoxy_mutex_init(&block_statistics_mutex); +#endif #ifdef FEATURE_EXTENDED_STATISTICS privoxy_mutex_init(&filter_statistics_mutex); privoxy_mutex_init(&block_reason_statistics_mutex); diff --git a/jcc.h b/jcc.h index df513b83..dbe05b7b 100644 --- a/jcc.h +++ b/jcc.h @@ -41,8 +41,13 @@ struct file_list; /* Global variables */ #ifdef FEATURE_STATISTICS +#if defined(FEATURE_PTHREAD) || defined(_WIN32) +extern unsigned long long number_of_requests_received; +extern unsigned long long number_of_requests_blocked; +#else extern int urls_read; extern int urls_rejected; +#endif #endif /*def FEATURE_STATISTICS*/ extern struct client_states clients[1]; @@ -86,6 +91,9 @@ extern privoxy_mutex_t external_filter_mutex; extern privoxy_mutex_t client_tags_mutex; #endif +#ifdef FEATURE_STATISTICS +extern privoxy_mutex_t block_statistics_mutex; +#endif #ifdef FEATURE_EXTENDED_STATISTICS extern privoxy_mutex_t filter_statistics_mutex; extern privoxy_mutex_t block_reason_statistics_mutex; diff --git a/loaders.c b/loaders.c index 61d27637..5f21fd0b 100644 --- a/loaders.c +++ b/loaders.c @@ -222,13 +222,13 @@ unsigned int sweep(void) { last_active->next = client_list->next; -#ifdef FEATURE_STATISTICS +#if defined(FEATURE_STATISTICS) && !defined(MUTEX_LOCKS_AVAILABLE) urls_read++; if (csp->flags & CSP_FLAG_REJECTED) { urls_rejected++; } -#endif /* def FEATURE_STATISTICS */ +#endif /* defined(FEATURE_STATISTICS) && !defined(MUTEX_LOCKS_AVAILABLE) */ freez(client_list); diff --git a/templates/show-status b/templates/show-status index 2336c069..e8708c6b 100644 --- a/templates/show-status +++ b/templates/show-status @@ -200,10 +200,7 @@ which equals a block rate of @percent-blocked@%.

- Note that the statistics currently don't work properly for - reused connections where only the last request gets counted. - You may want to look into Privoxy-Log-Parser's --statistics option, - which doesn't have this limitation. + For additional statistics you may want to look into Privoxy-Log-Parser's --statistics option. There haven't been any requests so far.