]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Place moving average function is generic utilities
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Jul 2018 18:48:08 +0000 (19:48 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Jul 2018 18:48:08 +0000 (19:48 +0100)
src/libserver/symbols_cache.c
src/libutil/util.c
src/libutil/util.h

index 9431252871c761e46830f96d47879a1f6b3dc360..6076d1743e42039310d388c038b58a4f7884d379 100644 (file)
@@ -85,19 +85,13 @@ struct symbols_cache {
        gint peak_cb;
 };
 
-struct counter_data {
-       gdouble mean;
-       gdouble stddev;
-       guint64 number;
-};
-
 struct item_stat {
-       struct counter_data time_counter;
+       struct rspamd_counter_data time_counter;
        gdouble avg_time;
        gdouble weight;
        guint hits;
        guint64 total_hits;
-       struct counter_data frequency_counter;
+       struct rspamd_counter_data frequency_counter;
        gdouble avg_frequency;
        gdouble stddev_frequency;
 };
@@ -109,7 +103,7 @@ struct cache_item {
        guint64 last_count;
 
        /* Per process counter */
-       struct counter_data *cd;
+       struct rspamd_counter_data *cd;
        gchar *symbol;
        enum rspamd_symbol_type type;
 
@@ -338,50 +332,6 @@ cache_logic_cmp (const void *p1, const void *p2, gpointer ud)
        return 0;
 }
 
-/**
- * Set counter for a symbol using moving average
- */
-static double
-rspamd_set_counter (struct counter_data *cd, gdouble value)
-{
-       gdouble cerr;
-
-       /* Cumulative moving average using per-process counter data */
-       if (cd->number == 0) {
-               cd->mean = 0;
-               cd->stddev = 0;
-       }
-
-       cd->mean += (value - cd->mean) / (gdouble)(++cd->number);
-       cerr = (value - cd->mean) * (value - cd->mean);
-       cd->stddev += (cerr - cd->stddev) / (gdouble)(cd->number);
-
-       return cd->mean;
-}
-
-/**
- * Set counter for a symbol using exponential moving average
- */
-static double
-rspamd_set_counter_ema (struct counter_data *cd, gdouble value, gdouble alpha)
-{
-       gdouble diff, incr;
-
-       /* Cumulative moving average using per-process counter data */
-       if (cd->number == 0) {
-               cd->mean = 0;
-               cd->stddev = 0;
-       }
-
-       diff = value - cd->mean;
-       incr = diff * alpha;
-       cd->mean += incr;
-       cd->stddev = (1 - alpha) * (cd->stddev + diff * incr);
-       cd->number ++;
-
-       return cd->mean;
-}
-
 static void
 rspamd_symbols_cache_tsort_visit (struct symbols_cache *cache,
                                                                  struct cache_item *it,
@@ -874,7 +824,7 @@ rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,
         * save or accumulate
         */
        item->cd = rspamd_mempool_alloc0 (cache->static_pool,
-                       sizeof (struct counter_data));
+                       sizeof (struct rspamd_counter_data));
        item->func = func;
        item->user_data = user_data;
        item->priority = priority;
index 93f4497910d8065e432f13ca42f9133ef175aae4..6554da63b259174bf7bce3f082c94d429ec04f05 100644 (file)
@@ -2973,4 +2973,44 @@ rspamd_glob_path (const gchar *dir,
        }
 
        return res;
-}
\ No newline at end of file
+}
+
+double
+rspamd_set_counter (struct rspamd_counter_data *cd, gdouble value)
+{
+       gdouble cerr;
+
+       /* Cumulative moving average using per-process counter data */
+       if (cd->number == 0) {
+               cd->mean = 0;
+               cd->stddev = 0;
+       }
+
+       cd->mean += (value - cd->mean) / (gdouble)(++cd->number);
+       cerr = (value - cd->mean) * (value - cd->mean);
+       cd->stddev += (cerr - cd->stddev) / (gdouble)(cd->number);
+
+       return cd->mean;
+}
+
+double
+rspamd_set_counter_ema (struct rspamd_counter_data *cd,
+               gdouble value,
+               gdouble alpha)
+{
+       gdouble diff, incr;
+
+       /* Cumulative moving average using per-process counter data */
+       if (cd->number == 0) {
+               cd->mean = 0;
+               cd->stddev = 0;
+       }
+
+       diff = value - cd->mean;
+       incr = diff * alpha;
+       cd->mean += incr;
+       cd->stddev = (1 - alpha) * (cd->stddev + diff * incr);
+       cd->number ++;
+
+       return cd->mean;
+}
index 5968094a9c66dfffe5d7b55a8ab0f4be222dc41c..4338d55572d1c6246c46fb615400bd2d8819094e 100644 (file)
@@ -511,4 +511,30 @@ GPtrArray *rspamd_glob_path (const gchar *dir,
                                                         gboolean recursive,
                                                         GError **err);
 
+struct rspamd_counter_data {
+       gdouble mean;
+       gdouble stddev;
+       guint64 number;
+};
+
+/**
+ * Sets counter's data using exponential moving average
+ * @param cd counter
+ * @param value new counter value
+ * @param alpha decay coefficient (0..1)
+ * @return new counter value
+ */
+double rspamd_set_counter_ema (struct rspamd_counter_data *cd,
+                                                          gdouble value,
+                                                          gdouble alpha);
+
+/**
+ * Sets counter's data using flat moving average
+ * @param cd counter
+ * @param value new counter value
+ * @return new counter value
+ */
+double rspamd_set_counter (struct rspamd_counter_data *cd,
+                                                  gdouble value);
+
 #endif