]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
added threshold config and hooks
authorJoel Cornett <joel.cornett@gmail.com>
Wed, 30 Mar 2016 18:36:18 +0000 (14:36 -0400)
committerJoel Cornett <joel.cornett@gmail.com>
Tue, 5 Apr 2016 19:26:04 +0000 (15:26 -0400)
src/memory/memory_cap.cc
src/memory/memory_cap.h
src/memory/memory_config.h
src/memory/memory_module.cc

index 3c95900e4e298dc608c25e8c40df5fe2eefa3b70..1591f69bc30ec1b3094aa8c1ddec99fb127679db 100644 (file)
@@ -96,6 +96,7 @@ inline bool free_space(size_t requested, size_t cap, Tracker& trk, Handler& hand
 // -----------------------------------------------------------------------------
 
 size_t MemoryCap::thread_cap = 0;
+size_t MemoryCap::preemptive_threshold = 0;
 
 // -----------------------------------------------------------------------------
 // public interface
@@ -125,6 +126,14 @@ void MemoryCap::update_deallocations(size_t n)
     mp_active_context.update_deallocs(n);
 }
 
+bool MemoryCap::over_threshold()
+{
+    if ( !preemptive_threshold )
+        return false;
+
+    return s_tracker.used() >= preemptive_threshold;
+}
+
 void MemoryCap::calculate(unsigned num_threads)
 {
     const MemoryConfig& config = *snort_conf->memory;
@@ -142,6 +151,13 @@ void MemoryCap::calculate(unsigned num_threads)
         FatalError("per-thread memory cap is 0");
 
     DebugFormat(DEBUG_MEMORY, "per-thread memory cap set to %zu", thread_cap);
+
+    if ( config.threshold )
+    {
+        preemptive_threshold = memory::calculate_threshold(thread_cap, config.threshold);
+        DebugFormat(DEBUG_MEMORY,
+            "per-thread pre-emptive action threshold set to %zu", preemptive_threshold);
+    }
 }
 
 } // namespace memory
index d3586100c4d7bb8cf8a1d3a159b9d5f3ae606956..b2111cae4fc8cac33a51512eb24d2ac594ac9bcd 100644 (file)
@@ -33,11 +33,14 @@ public:
     static void update_allocations(size_t);
     static void update_deallocations(size_t);
 
+    bool over_threshold();
+
     // call from main thread before thread spawn
     static void calculate(unsigned num_threads);
 
 private:
     static size_t thread_cap;
+    static size_t preemptive_threshold;
 };
 
 } // namespace memory
index 2f289630c8f45c4719a59682a98470fe7d184c05..992e248eb721c26cc2747b422103df43f0235d08 100644 (file)
@@ -27,6 +27,7 @@ struct MemoryConfig
 {
     size_t cap = 0;
     bool soft = false;
+    size_t threshold = 0;
 
     constexpr MemoryConfig() = default;
 };
index bbf62dd01d0a444310342bad6272639312f65739..696423af21df575a4d398eac4d3be50b8473c528 100644 (file)
@@ -39,6 +39,10 @@ static const Parameter s_params[] =
     { "soft", Parameter::PT_BOOL, nullptr, "false",
         "always succeed in allocating memory, even if above the cap" },
 
+    { "threshold", Parameter::PT_INT, "0:", "0",
+        "set the per-packet-thread threshold for preemptive cleanup actions "
+        "(percent, 0 to disable)" },
+
     { nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr }
 };
 
@@ -58,6 +62,9 @@ bool MemoryModule::set(const char*, Value& v, SnortConfig* sc)
     else if ( v.is("soft") )
         sc->memory->soft = v.get_bool();
 
+    else if ( v.is("threshold") )
+        sc->memory->threshold = v.get_long();
+
     else
         return false;