]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Reduce number of timers queries
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 19 Jul 2016 10:48:57 +0000 (11:48 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 19 Jul 2016 10:48:57 +0000 (11:48 +0100)
src/libserver/re_cache.c
src/libserver/symbols_cache.c
src/libserver/worker_util.c

index 4cffe675c0a21703a21e86e24b194ff70076f3a9..59070d3daf257f7109e1229a967e282d277ba7b1 100644 (file)
@@ -463,7 +463,7 @@ rspamd_re_cache_process_pcre (struct rspamd_re_runtime *rt,
        const gchar *start = NULL, *end = NULL;
        guint max_hits = rspamd_regexp_get_maxhits (re);
        guint64 id = rspamd_regexp_get_cache_id (re);
-       gdouble t1, t2;
+       gdouble t1, t2, pr;
        const gdouble slow_time = 0.1;
 
        if (in == NULL) {
@@ -481,7 +481,11 @@ rspamd_re_cache_process_pcre (struct rspamd_re_runtime *rt,
        r = rt->results[id];
 
        if (max_hits == 0 || r < max_hits) {
-               t1 = rspamd_get_ticks ();
+               pr = rspamd_random_double_fast ();
+
+               if (pr > 0.9) {
+                       t1 = rspamd_get_ticks ();
+               }
 
                while (rspamd_regexp_search (re,
                                in,
@@ -505,11 +509,13 @@ rspamd_re_cache_process_pcre (struct rspamd_re_runtime *rt,
                        rt->stat.regexp_matched += r;
                }
 
-               t2 = rspamd_get_ticks ();
+               if (pr > 0.9) {
+                       t2 = rspamd_get_ticks ();
 
-               if (t2 - t1 > slow_time) {
-                       msg_info_pool ("regexp '%16s' took %.2f seconds to execute",
-                                       rspamd_regexp_get_pattern (re), t2 - t1);
+                       if (t2 - t1 > slow_time) {
+                               msg_info_pool ("regexp '%16s' took %.2f seconds to execute",
+                                               rspamd_regexp_get_pattern (re), t2 - t1);
+                       }
                }
        }
 
index 2642b607ad164cbaf6f873a03e116070d25744b9..b4a3275d8ea3b9e701e7ad49233d34741276a774 100644 (file)
@@ -162,7 +162,8 @@ static gboolean rspamd_symbols_cache_check_symbol (struct rspamd_task *task,
                struct symbols_cache *cache,
                struct cache_item *item,
                struct cache_savepoint *checkpoint,
-               gdouble *total_diff);
+               gdouble *total_diff,
+               gdouble pr);
 static gboolean rspamd_symbols_cache_check_deps (struct rspamd_task *task,
                struct symbols_cache *cache,
                struct cache_item *item,
@@ -1083,6 +1084,7 @@ rspamd_symbols_cache_watcher_cb (gpointer sessiond, gpointer ud)
        struct cache_savepoint *checkpoint;
        struct symbols_cache *cache;
        gint i, remain = 0;
+       gdouble pr = rspamd_random_double_fast ();
 
        checkpoint = task->checkpoint;
        cache = task->cfg->cache;
@@ -1102,7 +1104,7 @@ rspamd_symbols_cache_watcher_cb (gpointer sessiond, gpointer ud)
                                }
 
                                rspamd_symbols_cache_check_symbol (task, cache, it, checkpoint,
-                                               NULL);
+                                               NULL, pr);
                        }
                }
        }
@@ -1115,7 +1117,8 @@ rspamd_symbols_cache_check_symbol (struct rspamd_task *task,
                struct symbols_cache *cache,
                struct cache_item *item,
                struct cache_savepoint *checkpoint,
-               gdouble *total_diff)
+               gdouble *total_diff,
+               gdouble pr)
 {
        guint pending_before, pending_after;
        double t1, t2;
@@ -1154,7 +1157,10 @@ rspamd_symbols_cache_check_symbol (struct rspamd_task *task,
                }
 
                if (check) {
-                       t1 = rspamd_get_ticks ();
+                       if (pr > 0.9) {
+                               t1 = rspamd_get_ticks ();
+                       }
+
                        pending_before = rspamd_session_events_pending (task->s);
                        /* Watch for events appeared */
                        rspamd_session_watch_start (task->s, rspamd_symbols_cache_watcher_cb,
@@ -1163,19 +1169,21 @@ rspamd_symbols_cache_check_symbol (struct rspamd_task *task,
                        msg_debug_task ("execute %s, %d", item->symbol, item->id);
                        item->func (task, item->user_data);
 
-                       t2 = rspamd_get_ticks ();
-                       diff = (t2 - t1) * 1e6;
+                       if (pr > 0.9) {
+                               t2 = rspamd_get_ticks ();
+                               diff = (t2 - t1) * 1e6;
 
-                       if (total_diff) {
-                               *total_diff += diff;
-                       }
+                               if (total_diff) {
+                                       *total_diff += diff;
+                               }
 
-                       if (diff > slow_diff_limit) {
-                               msg_info_task ("slow rule: %s: %d ms", item->symbol,
-                                               (gint)(diff / 1000.));
-                       }
+                               if (diff > slow_diff_limit) {
+                                       msg_info_task ("slow rule: %s: %d ms", item->symbol,
+                                                       (gint)(diff / 1000.));
+                               }
 
-                       rspamd_set_counter (item, diff);
+                               rspamd_set_counter (item, diff);
+                       }
                        rspamd_session_watch_stop (task->s);
                        pending_after = rspamd_session_events_pending (task->s);
 
@@ -1213,6 +1221,7 @@ rspamd_symbols_cache_check_deps (struct rspamd_task *task,
        struct cache_dependency *dep;
        guint i;
        gboolean ret = TRUE;
+       gdouble pr = rspamd_random_double_fast ();
 
        if (item->deps != NULL && item->deps->len > 0) {
                for (i = 0; i < item->deps->len; i ++) {
@@ -1246,7 +1255,8 @@ rspamd_symbols_cache_check_deps (struct rspamd_task *task,
                                        else if (!rspamd_symbols_cache_check_symbol (task, cache,
                                                        dep->item,
                                                        checkpoint,
-                                                       NULL)) {
+                                                       NULL,
+                                                       pr)) {
                                                /* Now started, but has events pending */
                                                ret = FALSE;
                                                msg_debug_task ("started check of %d symbol as dep for "
@@ -1437,6 +1447,7 @@ rspamd_symbols_cache_process_symbols (struct rspamd_task * task,
        gboolean all_done;
        const gdouble max_microseconds = 3e5;
        guint start_events_pending;
+       gdouble pr = rspamd_random_double_fast ();
 
        g_assert (cache != NULL);
 
@@ -1464,7 +1475,7 @@ rspamd_symbols_cache_process_symbols (struct rspamd_task * task,
 
                        if (!isset (checkpoint->processed_bits, item->id * 2)) {
                                rspamd_symbols_cache_check_symbol (task, cache, item,
-                                               checkpoint, &total_microseconds);
+                                               checkpoint, &total_microseconds, pr);
                        }
                }
                checkpoint->pass = RSPAMD_CACHE_PASS_WAIT_PREFILTERS;
@@ -1517,7 +1528,7 @@ rspamd_symbols_cache_process_symbols (struct rspamd_task * task,
                                }
 
                                rspamd_symbols_cache_check_symbol (task, cache, item,
-                                               checkpoint, &total_microseconds);
+                                               checkpoint, &total_microseconds, pr);
                        }
 
                        if (total_microseconds > max_microseconds) {
@@ -1567,7 +1578,7 @@ rspamd_symbols_cache_process_symbols (struct rspamd_task * task,
                                }
 
                                rspamd_symbols_cache_check_symbol (task, cache, item,
-                                               checkpoint, &total_microseconds);
+                                               checkpoint, &total_microseconds, pr);
                        }
 
                        if (total_microseconds > max_microseconds) {
@@ -1597,7 +1608,7 @@ rspamd_symbols_cache_process_symbols (struct rspamd_task * task,
 
                        if (!isset (checkpoint->processed_bits, item->id * 2)) {
                                rspamd_symbols_cache_check_symbol (task, cache, item,
-                                               checkpoint, &total_microseconds);
+                                               checkpoint, &total_microseconds, pr);
                        }
                }
                checkpoint->pass = RSPAMD_CACHE_PASS_WAIT_POSTFILTERS;
index 9924671cf50871e877a7af5ad78da54c89e8424e..b7aa5035f7085e481297ba8961d24fe97f892ef5 100644 (file)
@@ -538,6 +538,7 @@ rspamd_fork_worker (struct rspamd_main *rspamd_main,
                /* Lock statfile pool if possible XXX */
                /* Init PRNG after fork */
                rc = ottery_init (rspamd_main->cfg->libs_ctx->ottery_cfg);
+               rspamd_random_seed_fast ();
                if (rc != OTTERY_ERR_NONE) {
                        msg_err_main ("cannot initialize PRNG: %d", rc);
                        g_assert (0);