From: Joel Cornett Date: Wed, 30 Mar 2016 18:36:18 +0000 (-0400) Subject: added threshold config and hooks X-Git-Tag: 3.0.0-233~474^2~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=dcdfbf3cafaee231183c515977f4d87604c0b2aa;p=thirdparty%2Fsnort3.git added threshold config and hooks --- diff --git a/src/memory/memory_cap.cc b/src/memory/memory_cap.cc index 3c95900e4..1591f69bc 100644 --- a/src/memory/memory_cap.cc +++ b/src/memory/memory_cap.cc @@ -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 diff --git a/src/memory/memory_cap.h b/src/memory/memory_cap.h index d3586100c..b2111cae4 100644 --- a/src/memory/memory_cap.h +++ b/src/memory/memory_cap.h @@ -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 diff --git a/src/memory/memory_config.h b/src/memory/memory_config.h index 2f289630c..992e248eb 100644 --- a/src/memory/memory_config.h +++ b/src/memory/memory_config.h @@ -27,6 +27,7 @@ struct MemoryConfig { size_t cap = 0; bool soft = false; + size_t threshold = 0; constexpr MemoryConfig() = default; }; diff --git a/src/memory/memory_module.cc b/src/memory/memory_module.cc index bbf62dd01..696423af2 100644 --- a/src/memory/memory_module.cc +++ b/src/memory/memory_module.cc @@ -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;