FEATURE_STATISTICS: Include all requests in the statistics
authorFabian Keil <fk@fabiankeil.de>
Tue, 16 Mar 2021 18:34:52 +0000 (19:34 +0100)
committerFabian Keil <fk@fabiankeil.de>
Sun, 17 Apr 2022 08:12:32 +0000 (10:12 +0200)
... 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.

cgisimple.c
jcc.c
jcc.h
loaders.c
templates/show-status

index 9650627..142a491 100644 (file)
@@ -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 ef79d60..563c615 100644 (file)
--- 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 df513b8..dbe05b7 100644 (file)
--- 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;
index 61d2763..5f21fd0 100644 (file)
--- 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);
 
index 2336c06..e8708c6 100644 (file)
           which equals a block rate of @percent-blocked@%.
         </p>
         <p>
-          <strong>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.</strong>
+         For additional statistics you may want to look into Privoxy-Log-Parser's --statistics option.
 <!-- if-have-stats-end@ -->
 <!-- @if-have-no-stats-start -->
           There haven't been any requests so far.