]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Implement runtime creation
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 23 Apr 2022 11:39:40 +0000 (12:39 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 23 Apr 2022 11:39:40 +0000 (12:39 +0100)
src/libserver/rspamd_symcache.cxx
src/libserver/symcache/symcache_impl.cxx
src/libserver/symcache/symcache_internal.hxx
src/libserver/symcache/symcache_runtime.cxx
src/libserver/symcache/symcache_runtime.hxx

index e1e9ade4ebf0b107300646fa61ffbc5030ada2df..35fa0588f886f99b4ea2d7a6f0cee3270f646678 100644 (file)
@@ -55,13 +55,6 @@ INIT_LOG_MODULE(symcache)
 
 
 
-/* At least once per minute */
-#define PROFILE_MAX_TIME (60.0)
-/* For messages larger than 2Mb enable profiling */
-#define PROFILE_MESSAGE_SIZE_THRESHOLD (1024 * 1024 * 2)
-/* Enable profile at least once per this amount of messages processed */
-#define PROFILE_PROBABILITY (0.01)
-
 /* weight, frequency, time */
 #define TIME_ALPHA (1.0)
 #define WEIGHT_ALPHA (0.1)
index e2032c063f9a9b02cd2ed9830f72857fe41ad808..535fe57b9e9f6033e79fff47838ec69fda8e4c51 100644 (file)
@@ -879,4 +879,21 @@ symcache::~symcache()
        }
 }
 
+auto symcache::maybe_resort() -> bool
+{
+       if (items_by_order->generation_id != cur_order_gen) {
+               /*
+                * Cache has been modified, need to resort it
+                */
+               msg_info_cache("symbols cache has been modified since last check:"
+                                               " old id: %ud, new id: %ud",
+                               items_by_order->generation_id, cur_order_gen);
+               resort();
+
+               return true;
+       }
+
+       return false;
+}
+
 }
\ No newline at end of file
index a45ae88b9e0654f0c84a78c0eae8454a1f671438..0862476f68990170a9ed04a5886edca58c68d750 100644 (file)
@@ -144,6 +144,8 @@ private:
        lua_State *L;
        double reload_time;
        double last_profile;
+
+private:
        int peak_cb;
        int cache_id;
 
@@ -331,6 +333,45 @@ public:
                        f(sym_it.second.get());
                }
        }
+
+       /**
+        * Resort cache if anything has been changed since last time
+        * @return
+        */
+       auto maybe_resort() -> bool;
+
+       /**
+        * Returns number of items with ids
+        * @return
+        */
+       auto get_items_count()  const -> auto {
+               return items_by_id.size();
+       }
+
+       /**
+        * Returns current set of items ordered for sharing ownership
+        * @return
+        */
+       auto get_cache_order() const -> auto {
+               return items_by_order;
+       }
+
+       /**
+        * Get last profile timestamp
+        * @return
+        */
+       auto get_last_profile() const -> auto {
+               return last_profile;
+       }
+
+       /**
+        * Sets last profile timestamp
+        * @param last_profile
+        * @return
+        */
+       auto set_last_profile(double last_profile){
+               symcache::last_profile = last_profile;
+       }
 };
 
 
index a87dad2598aa834a07ea779c2c0d72341f817812..b1b5f5b99f3cb5d3d6750d13e8742ad7fa370c11 100644 (file)
 
 namespace rspamd::symcache {
 
-auto cache_savepoint::create_savepoint(struct rspamd_task *task, const symcache &cache) -> cache_savepoint *
+/* At least once per minute */
+constexpr static const auto PROFILE_MAX_TIME = 60.0;
+/* For messages larger than 2Mb enable profiling */
+constexpr static const auto PROFILE_MESSAGE_SIZE_THRESHOLD = 1024ul * 1024 * 2;
+/* Enable profile at least once per this amount of messages processed */
+constexpr static const auto PROFILE_PROBABILITY = 0.01;
+
+auto
+cache_savepoint::create_savepoint(struct rspamd_task *task, symcache &cache) -> cache_savepoint *
 {
-       return nullptr;
+       struct cache_savepoint *checkpoint;
+
+       cache.maybe_resort();
+
+       checkpoint = (struct cache_savepoint *) rspamd_mempool_alloc0 (task->task_pool,
+                       sizeof(*checkpoint) +
+                       sizeof(struct cache_dynamic_item) * cache.get_items_count());
+
+       checkpoint->order = cache.get_cache_order();
+       rspamd_mempool_add_destructor(task->task_pool,
+                       cache_savepoint::savepoint_dtor, checkpoint);
+
+       /* Calculate profile probability */
+       ev_now_update_if_cheap(task->event_loop);
+       ev_tstamp now = ev_now(task->event_loop);
+       checkpoint->profile_start = now;
+
+       if ((cache.get_last_profile() == 0.0 || now > cache.get_last_profile() + PROFILE_MAX_TIME) ||
+               (task->msg.len >= PROFILE_MESSAGE_SIZE_THRESHOLD) ||
+               (rspamd_random_double_fast() >= (1 - PROFILE_PROBABILITY))) {
+               msg_debug_cache_task("enable profiling of symbols for task");
+               checkpoint->profile = true;
+               cache.set_last_profile(now);
+       }
+
+       task->checkpoint = (void *)checkpoint;
+
+       return checkpoint;
 }
 
 }
index 3a7da615d8340822ca4609e5b5390705c9b295bf..4d43a7015cdc5a77d9ad14bb5e13e8b348211367 100644 (file)
@@ -44,8 +44,7 @@ struct cache_dynamic_item {
 static_assert(sizeof(cache_dynamic_item) == sizeof(std::uint64_t));
 static_assert(std::is_trivial_v<cache_dynamic_item>);
 
-struct cache_savepoint {
-       unsigned order_gen;
+class cache_savepoint {
        unsigned items_inflight;
        bool profile;
        bool has_slow;
@@ -59,9 +58,17 @@ struct cache_savepoint {
        order_generation_ptr order;
        /* Dynamically expanded as needed */
        struct cache_dynamic_item dynamic_items[];
+       /* We allocate this structure merely in memory pool, so destructor is absent */
+       ~cache_savepoint() = delete;
+       /* Dropper for a shared ownership */
+       static auto savepoint_dtor(void *ptr) -> void {
+               auto *real_savepoint = (cache_savepoint *)ptr;
 
+               /* Drop shared ownership */
+               real_savepoint->order.reset();
+       }
 public:
-       static auto create_savepoint(struct rspamd_task *task, const symcache &cache) -> cache_savepoint *;
+       static auto create_savepoint(struct rspamd_task *task, symcache &cache) -> cache_savepoint *;
 };