]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4523: analyzer: add logging for resource tuning progress
authorAdrian Mamolea (admamole) <admamole@cisco.com>
Tue, 26 Nov 2024 12:09:54 +0000 (12:09 +0000)
committerMaya Dagon (mdagon) <mdagon@cisco.com>
Tue, 26 Nov 2024 12:09:54 +0000 (12:09 +0000)
Merge in SNORT/snort3 from ~ADMAMOLE/snort3:reload_tuner2 to master

Squashed commit of the following:

commit 8d93eee846ae6a585cf86db11b5316f35ca5fe27
Author: Adrian Mamolea <admamole@cisco.com>
Date:   Tue Nov 19 11:15:03 2024 -0500

    analyzer: add logging for resource tuning progress

14 files changed:
src/host_tracker/host_cache_module.h
src/latency/latency_module.cc
src/main/analyzer_command.cc
src/main/reload_tuner.h
src/network_inspectors/appid/appid_module.h
src/network_inspectors/kaizen/kaizen_engine.cc
src/network_inspectors/perf_monitor/perf_reload_tuner.h
src/network_inspectors/port_scan/ps_module.h
src/network_inspectors/rna/rna_module.cc
src/network_inspectors/rna/rna_module.h
src/profiler/profiler_module.cc
src/stream/base/stream_module.cc
src/stream/base/stream_module.h
src/target_based/host_attributes.cc

index d004650e839fee2974b1bcfacf23edd3b2bad943..e9286884f8f2a61e18b22924766e1891c8eea4a2 100644 (file)
@@ -40,6 +40,10 @@ class HostCacheReloadTuner : public snort::ReloadResourceTuner
 {
 public:
     explicit HostCacheReloadTuner(size_t memcap) : memcap(memcap) { }
+
+    const char* name() const override
+    { return "HostCacheReloadTuner"; }
+
     bool tinit() override
     { return host_cache.reload_resize(memcap); }
 
index 15d6c83c6db3b0136e874e7623f21b14a57f4b3a..d7d26d1741207e626fc8e0e683b3b72350fb1644 100644 (file)
@@ -182,6 +182,9 @@ public:
     {}
     ~LatencyTuner() override = default;
 
+    const char* name() const override
+    { return "LatencyTuner"; }
+
     bool tinit() override
     {
         packet_latency::set_force_enable(enable_packet);
@@ -260,3 +263,18 @@ const PegInfo* LatencyModule::get_pegs() const
 
 PegCount* LatencyModule::get_counts() const
 { return reinterpret_cast<PegCount*>(&latency_stats); }
+
+#ifdef UNIT_TEST
+
+#include "catch/snort_catch.h"
+
+#include <memory.h>
+
+TEST_CASE("Latency tuner name", "[latency_module]")
+{
+    LatencyTuner tuner(true, false);
+
+    REQUIRE(strcmp(tuner.name(), "LatencyTuner") == 0);
+}
+
+#endif
index daa5ff03b3d99e46e34945da896ad289ef4c4487..b595291990d4c71a54aaa7287eff981b469b565b 100644 (file)
 #include "analyzer_command.h"
 
 #include <cassert>
+#include <sys/time.h>
 
 #include "control/control.h"
 #include "framework/module.h"
 #include "log/messages.h"
 #include "managers/module_manager.h"
+#include "packet_io/sfdaq_instance.h"
 #include "protocols/packet_manager.h"
 #include "target_based/host_attributes.h"
+#include "time/packet_time.h"
 #include "utils/stats.h"
-#include "packet_io/sfdaq_instance.h"
 
 #include "analyzer.h"
 #include "reload_tracker.h"
 
 using namespace snort;
 
+static THREAD_LOCAL timeval report_time{};
+static const timeval report_period = {10, 0};
+
+static void tuner_next(ReloadResourceTuner* tuner)
+{
+    LogMessage("ReloadResourceTuner[%u] running %s\n", get_instance_id(), tuner->name());
+
+    timeval now;
+    packet_gettimeofday(&now);
+    timeradd(&now, &report_period, &report_time);
+}
+
+static void tuner_update(ReloadResourceTuner* tuner)
+{
+    timeval now;
+    packet_gettimeofday(&now);
+    if (timercmp(&now, &report_time, >))
+    {
+        timeradd(&now, &report_period, &report_time);
+        tuner->report_progress();
+    }
+}
+
 void AnalyzerCommand::log_message(ControlConn* ctrlcon, const char* format, va_list& ap)
 {
     if (ctrlcon && !ctrlcon->is_local())
@@ -168,6 +193,7 @@ bool ACSwap::execute(Analyzer& analyzer, void** ac_state)
 
             if ( !*ac_state )
             {
+                LogMessage("ReloadResourceTuner[%u] start\n", get_instance_id());
                 reload_tuners = new std::list<ReloadResourceTuner*>(sc->get_reload_resource_tuners());
                 std::list<ReloadResourceTuner*>::iterator rtt = reload_tuners->begin();
                 while ( rtt != reload_tuners->end() )
@@ -178,6 +204,8 @@ bool ACSwap::execute(Analyzer& analyzer, void** ac_state)
                         rtt = reload_tuners->erase(rtt);
                 }
                 *ac_state = reload_tuners;
+                if (!reload_tuners->empty())
+                    tuner_next(reload_tuners->front());
             }
             else
                 reload_tuners = (std::list<ReloadResourceTuner*>*)*ac_state;
@@ -185,21 +213,21 @@ bool ACSwap::execute(Analyzer& analyzer, void** ac_state)
             if ( !reload_tuners->empty() )
             {
                 auto rrt = reload_tuners->front();
-                if ( analyzer.is_idling() )
+                bool tuning_complete = analyzer.is_idling() ? rrt->tune_idle_context() : rrt->tune_packet_context();
+                if (tuning_complete)
                 {
-                    if ( rrt->tune_idle_context() )
-                        reload_tuners->pop_front();
+                    reload_tuners->pop_front();
+                    if (!reload_tuners->empty())
+                        tuner_next(reload_tuners->front());
                 }
                 else
-                {
-                    if ( rrt->tune_packet_context() )
-                        reload_tuners->pop_front();
-                }
+                    tuner_update(rrt);
             }
 
             // check for empty again and free list instance if we are done
             if ( reload_tuners->empty() )
             {
+                LogMessage("ReloadResourceTuner[%d] complete\n", get_instance_id());
                 delete reload_tuners;
                 ps->finish(analyzer);
                 return true;
index ebae9a8e08be2e97287ad79611cecddaf6ae7442..35301c2668b523cdd975d6a84eb623eb3b04c580 100644 (file)
@@ -31,6 +31,9 @@ public:
 
     virtual ~ReloadResourceTuner() = default;
 
+    // returns name of the tuner
+    virtual const char* name() const = 0;
+
     // returns true if resource tuning required, false otherwise
     virtual bool tinit() = 0;
 
@@ -38,6 +41,9 @@ public:
     virtual bool tune_packet_context() = 0;
     virtual bool tune_idle_context() = 0;
 
+    // report progress and/or work left for the tuner
+    virtual void report_progress() {}
+
 protected:
     ReloadResourceTuner() = default;
 
@@ -50,6 +56,10 @@ class ReloadSwapper : public ReloadResourceTuner
 public:
     virtual ~ReloadSwapper() override = default;
 
+    // returns name of the tuner
+    const char* name() const override
+    { return "ReloadSwapper"; }
+
     // each of these returns true if resource tuning is complete, false otherwise
     bool tune_packet_context() override
     { return true; }
index 186985538cdf44679f8683a0d4e936ac80c170cc..6fcf5bc2046607face4368c243fd4adff4d434b3 100644 (file)
@@ -53,6 +53,10 @@ public:
     explicit AppIdReloadTuner(size_t memcap) : memcap(memcap) { }
     ~AppIdReloadTuner() override = default;
 
+    const char* name() const override
+    {
+        return "AppIdReloadTuner";
+    }
     bool tinit() override;
     bool tune_packet_context() override
     {
index 38c45106de6b7840a7ad8fba029a2587e2954e1b..ca2e5b824e12ba2671169bc349cbfaa9f521f75d 100644 (file)
@@ -80,6 +80,9 @@ public:
     explicit KaizenReloadTuner(const string& http_param_model) : http_param_model(http_param_model) {}
     ~KaizenReloadTuner() override = default;
 
+    const char* name() const override
+    { return "KaizenReloadTuner"; }
+
     bool tinit() override
     {
         delete classifier;
@@ -233,3 +236,19 @@ const BaseApi* nin_kaizen_engine[] =
     &kaizen_engine_api.base,
     nullptr
 };
+
+#ifdef UNIT_TEST
+
+#include "catch/snort_catch.h"
+
+#include <memory.h>
+
+TEST_CASE("Kaizen tuner name", "[kaizen_module]")
+{
+    const string http_param_model("model");
+    KaizenReloadTuner tuner(http_param_model);
+
+    REQUIRE(strcmp(tuner.name(), "KaizenReloadTuner") == 0);
+}
+
+#endif
index 280dcdbfa1bdf31446d6309b2f20102aaa2da6ad..4731ce00745d0ac7433ddfcaeb30823874cc59d3 100644 (file)
@@ -29,6 +29,9 @@ public:
     explicit PerfMonReloadTuner(size_t memcap) : memcap(memcap) { }
     ~PerfMonReloadTuner() override = default;
 
+    const char* name() const override
+        { return "PerfMonReloadTuner"; }
+
     bool tinit() override;
 
     bool tune_idle_context() override
index 5fdc94acb810b0a2b8a859f46f2056b3743a49c6..f7c3bcc8ff07021c70dfea4e377ddf2619256df6 100644 (file)
@@ -145,6 +145,9 @@ public:
     explicit PortScanReloadTuner(size_t memcap) : memcap(memcap) { }
     ~PortScanReloadTuner() override = default;
 
+    const char* name() const override
+    { return "PortScanReloadTuner"; }
+
     bool tinit() override
     { return ps_init_hash(memcap); }
 
index 0ed4d22f9927fb21f26af3cfbd352e1a8e7df301..a82fcd37c856e4ea2d8f73be7e4d898a01880d10 100644 (file)
@@ -51,6 +51,7 @@
 
 #ifdef UNIT_TEST
 #include "catch/snort_catch.h"
+#include <memory.h>
 #endif
 
 using namespace snort;
@@ -710,5 +711,13 @@ TEST_CASE("RNA module", "[rna_module]")
     }
 }
 
+TEST_CASE("Fingerprint processors tuner name", "[rna_module]")
+{
+    RnaModuleConfig mod_conf;
+    FpProcReloadTuner tuner(mod_conf);
+
+    REQUIRE(strcmp(tuner.name(), "FpProcReloadTuner") == 0);
+}
+
 #endif
 
index e025f104c75a2f3b5a1bc5216244ca86ed5e2faf..aef417d4b154ce245bd217d56318ac635be189a2 100644 (file)
@@ -73,6 +73,9 @@ public:
         : mod_conf(mod_conf) { }
     ~FpProcReloadTuner() override = default;
 
+    const char* name() const override
+    { return "FpProcReloadTuner"; }
+
     bool tinit() override;
 
     bool tune_packet_context() override
index 526f29b5d05ad391de70bfaf4cfa0dd17a0afe1a..685a2662fd18796fa4edcbf4774a6574df426e89 100644 (file)
@@ -462,6 +462,9 @@ public:
     {}
     ~ProfilerReloadTuner() override = default;
 
+    const char* name() const override
+    { return "ProfilerReloadTuner"; }
+
     bool tinit() override
     {
         RuleContext::set_enabled(enable_rule);
@@ -578,3 +581,18 @@ ProfileStats* ProfilerModule::get_profile(
     }
     return nullptr;
 }
+
+#ifdef UNIT_TEST
+
+#include "catch/snort_catch.h"
+
+#include <memory.h>
+
+TEST_CASE("Profiler reload tuner name", "[profiler_module]")
+{
+    ProfilerReloadTuner tuner(true, true);
+
+    REQUIRE(strcmp(tuner.name(), "ProfilerReloadTuner") == 0);
+}
+
+#endif
index af8bf25068efc7bdf6c8d024b6fd395193c2fff4..6a28b957e6be5dc5eefb6d94092690a1123252ea 100644 (file)
@@ -452,6 +452,14 @@ bool StreamReloadResourceManager::tune_resources(unsigned work_limit)
     return ( flows_to_delete ) ? false : true;
 }
 
+void StreamReloadResourceManager::report_progress()
+{
+    LogMessage("StreamReloadResourceManager[%u] flows allocated %u, max flows %u,"
+        " tuning packets %" PRIu64", tuning idle %" PRIu64"\n",
+        get_instance_id(), flow_con->get_flows_allocated(), config.flow_cache_cfg.max_flows,
+        stream_base_stats.reload_tuning_packets, stream_base_stats.reload_tuning_idle);
+}
+
 bool StreamUnloadReloadResourceManager::tinit()
 {
     unsigned max_flows = flow_con->get_flow_cache_config().max_flows;
@@ -490,6 +498,14 @@ bool StreamUnloadReloadResourceManager::tune_resources(unsigned work_limit)
     return (flows_to_delete) ? false : true;
 }
 
+void StreamUnloadReloadResourceManager::report_progress()
+{
+    LogMessage("StreamUnloadReloadResourceManager[%u] flows allocated %u,"
+        " tuning packets %" PRIu64", tuning idle %" PRIu64"\n",
+        get_instance_id(), flow_con->get_flows_allocated(),
+        stream_base_stats.reload_tuning_packets, stream_base_stats.reload_tuning_idle);
+}
+
 void StreamModuleConfig::show() const
 {
     ConfigLogger::log_value("max_flows", flow_cache_cfg.max_flows);
index ab7c4d6eea15e671c79121a702b17bfb6f24d502..2678f5394077ff912582e2a4d2eee6ebd2ff9d66 100644 (file)
@@ -134,10 +134,14 @@ class StreamReloadResourceManager : public snort::ReloadResourceTuner
 public:
     StreamReloadResourceManager() = default;
 
+    const char* name() const override
+    { return "StreamReloadResourceManager"; }
     bool tinit() override;
     bool tune_packet_context() override;
     bool tune_idle_context() override;
 
+    void report_progress() override;
+
     bool initialize(const StreamModuleConfig&);
 
 private:
@@ -153,6 +157,8 @@ public:
     explicit HPQReloadTuner(uint32_t packet_timeout) : held_packet_timeout(packet_timeout) { }
     ~HPQReloadTuner() override = default;
 
+    const char* name() const override
+    { return "HPQReloadTuner"; }
     bool tinit() override;
     bool tune_packet_context() override;
     bool tune_idle_context() override;
@@ -166,9 +172,12 @@ class StreamUnloadReloadResourceManager : public snort::ReloadResourceTuner
 public:
     StreamUnloadReloadResourceManager() = default;
 
+    const char* name() const override
+    { return "StreamUnloadReloadResourceManager"; }
     bool tinit() override;
     bool tune_packet_context() override;
     bool tune_idle_context() override;
+    void report_progress() override;
 
 private:
     bool tune_resources(unsigned work_limit);
index ca2777752b89b62e75641cd38251c3cbb9d17a8f..e854a5cbe3f456f52a5be807e2f55cf3c2a1f437 100644 (file)
@@ -61,6 +61,9 @@ class HostAttributesReloadTuner : public snort::ReloadResourceTuner
 public:
     HostAttributesReloadTuner() = default;
 
+    const char* name() const override
+    { return "HostAttributesReloadTuner"; }
+
     bool tinit() override
     {
         HostAttributesManager::initialize();