]> source.dussan.org Git - rspamd.git/commitdiff
Rework counters.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 24 Aug 2014 16:03:53 +0000 (17:03 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 24 Aug 2014 16:03:53 +0000 (17:03 +0100)
src/libserver/symbols_cache.c
src/libserver/symbols_cache.h
src/libserver/worker_util.c
src/libserver/worker_util.h

index 823af39f53e7057a0860ce00cc69dc1ce460dd3b..01cb63cc0fbae4e1246835d093b8265b3dd07732 100644 (file)
@@ -83,6 +83,27 @@ cache_logic_cmp (const void *p1, const void *p2)
        return (gint)w2 - w1;
 }
 
+/**
+ * Set counter for a symbol
+ */
+static double
+rspamd_set_counter (struct cache_item *item, guint32 value)
+{
+       struct counter_data *cd;
+       double alpha;
+       cd = item->cd;
+
+       /* Calculate new value */
+       rspamd_mempool_lock_mutex (item->mtx);
+
+       alpha = 2. / (++cd->number + 1);
+       cd->value = cd->value * (1. - alpha) + value * alpha;
+
+       rspamd_mempool_unlock_mutex (item->mtx);
+
+       return cd->value;
+}
+
 static GChecksum *
 get_mem_cksum (struct symbols_cache *cache)
 {
@@ -309,8 +330,13 @@ register_symbol_common (struct symbols_cache **cache,
        item = rspamd_mempool_alloc0 (pcache->static_pool,
                        sizeof (struct cache_item));
        item->s =
-               rspamd_mempool_alloc0 (pcache->static_pool,
+               rspamd_mempool_alloc0_shared (pcache->static_pool,
                        sizeof (struct saved_cache_item));
+       item->cd = rspamd_mempool_alloc0_shared (pcache->static_pool,
+                       sizeof (struct counter_data));
+
+       item->mtx = rspamd_mempool_get_mutex (pcache->static_pool);
+
        rspamd_strlcpy (item->s->symbol, name, sizeof (item->s->symbol));
        item->func = func;
        item->user_data = user_data;
@@ -398,7 +424,7 @@ register_symbol_common (struct symbols_cache **cache,
        pcache->used_items++;
        g_hash_table_insert (pcache->items_by_symbol, item->s->symbol, item);
        msg_debug ("used items: %d, added symbol: %s", (*cache)->used_items, name);
-       rspamd_set_counter (item->s->symbol, 0);
+       rspamd_set_counter (item, 0);
 
        *target = g_list_prepend (*target, item);
 }
@@ -509,7 +535,7 @@ register_dynamic_symbol (rspamd_mempool_t *dynamic_pool,
 
        pcache->used_items++;
        msg_debug ("used items: %d, added symbol: %s", (*cache)->used_items, name);
-       rspamd_set_counter (item->s->symbol, 0);
+       rspamd_set_counter (item, 0);
 
        g_hash_table_insert (pcache->items_by_symbol, item->s->symbol, item);
 
@@ -677,9 +703,6 @@ init_symbols_cache (rspamd_mempool_t * pool,
                return FALSE;
        }
 
-       /* Init locking */
-       cache->lock = rspamd_mempool_get_rwlock (pool);
-
        cache->cfg = cfg;
 
        /* Just in-memory cache */
@@ -1006,11 +1029,9 @@ call_symbol_callback (struct rspamd_task * task,
                }
                if (cache->uses++ >= MAX_USES) {
                        msg_info ("resort symbols cache");
-                       rspamd_mempool_wlock_rwlock (cache->lock);
                        cache->uses = 0;
                        /* Resort while having write lock */
                        post_cache_init (cache);
-                       rspamd_mempool_wunlock_rwlock (cache->lock);
                }
                s =
                        rspamd_mempool_alloc0 (task->task_pool,
@@ -1203,7 +1224,7 @@ call_symbol_callback (struct rspamd_task * task,
                diff =
                        (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
 #endif
-               item->s->avg_time = rspamd_set_counter (item->s->symbol, diff);
+               item->s->avg_time = rspamd_set_counter (item, diff);
        }
 
        s->saved_item = item;
index fd9da2ef73b864c8cbd82793da7f1431c48eb924..676430913d8c3c16c75eb0792a9e78661bbc300a 100644 (file)
@@ -24,9 +24,17 @@ struct dynamic_map_item {
        gboolean negative;
 };
 
+struct counter_data {
+       gdouble value;
+       gint number;
+};
+
 struct cache_item {
        /* Static item's data */
        struct saved_cache_item *s;
+       struct counter_data *cd;
+
+       rspamd_mempool_mutex_t *mtx;
 
        /* For dynamic rules */
        struct dynamic_map_item *networks;
@@ -43,7 +51,6 @@ struct cache_item {
        gboolean is_virtual;
        gboolean is_callback;
 
-
        /* Priority */
        gint priority;
        gdouble metric_weight;
@@ -78,7 +85,6 @@ struct symbols_cache {
        guint used_items;
        guint uses;
        gpointer map;
-       rspamd_mempool_rwlock_t *lock;
        struct rspamd_config *cfg;
 };
 
index f2f04b397a0dcf673a1ac144312a916f449129ea..20ef1fc15dbacd78463be3c9d644a3f4ea73240c 100644 (file)
@@ -48,39 +48,6 @@ rspamd_get_worker_by_type (GQuark type)
 
        return NULL;
 }
-
-double
-rspamd_set_counter (const gchar *name, guint32 value)
-{
-       struct counter_data *cd;
-       double alpha;
-       gchar *key;
-
-       cd = rspamd_hash_lookup (rspamd_main->counters, (gpointer) name);
-
-       if (cd == NULL) {
-               cd =
-                       rspamd_mempool_alloc_shared (rspamd_main->counters->pool,
-                               sizeof (struct counter_data));
-               cd->value = value;
-               cd->number = 0;
-               key = rspamd_mempool_strdup_shared (rspamd_main->counters->pool, name);
-               rspamd_hash_insert (rspamd_main->counters, (gpointer) key,
-                       (gpointer) cd);
-       }
-       else {
-               /* Calculate new value */
-               rspamd_mempool_wlock_rwlock (rspamd_main->counters->lock);
-
-               alpha = 2. / (++cd->number + 1);
-               cd->value = cd->value * (1. - alpha) + value * alpha;
-
-               rspamd_mempool_wunlock_rwlock (rspamd_main->counters->lock);
-       }
-
-       return cd->value;
-}
-
 sig_atomic_t wanna_die = 0;
 
 #ifndef HAVE_SA_SIGINFO
index 54926e5f82a6bfbf3d6f9f64eb8913e627083f5e..d1aa1e862daf37a1d75bc17ca39a9d526d3b9e62 100644 (file)
  */
 worker_t * rspamd_get_worker_by_type (GQuark type);
 
-/**
- * Set counter for a symbol
- */
-double rspamd_set_counter (const gchar *name, guint32 value);
-
 #ifndef HAVE_SA_SIGINFO
 typedef void (*rspamd_sig_handler_t) (gint);
 #else