From: Steven Baigal (sbaigal) Date: Mon, 20 Mar 2023 14:45:59 +0000 (+0000) Subject: Pull request #3783: memory: add shell command to dump heap stats X-Git-Tag: 3.1.58.0~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13bd527d59bc1e6b89e19dbc30ad9352c516a5a5;p=thirdparty%2Fsnort3.git Pull request #3783: memory: add shell command to dump heap stats Merge in SNORT/snort3 from ~SBAIGAL/snort3:memstats to master Squashed commit of the following: commit ebe8554f4f5e95c464f08e57393d4fc204b531a0 Author: Steven Baigal Date: Wed Mar 15 17:19:35 2023 -0400 memory: add shell command to dump heap stats --- diff --git a/src/main.cc b/src/main.cc index a13211d95..9049b4ca5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -44,6 +44,7 @@ #include "managers/inspector_manager.h" #include "managers/module_manager.h" #include "managers/plugin_manager.h" +#include "memory/memory_cap.h" #include "packet_io/sfdaq.h" #include "packet_io/sfdaq_config.h" #include "packet_io/sfdaq_instance.h" @@ -336,6 +337,14 @@ int main_dump_stats(lua_State* L) return 0; } + +int main_dump_heap_stats(lua_State* L) +{ + ControlConn* ctrlcon = ControlConn::query_from_lua(L); + memory::MemoryCap::dump_mem_stats(ctrlcon); + return 0; +} + int main_reset_stats(lua_State* L) { ControlConn* ctrlcon = ControlConn::query_from_lua(L); diff --git a/src/main.h b/src/main.h index ccdf74b5e..c9c66213e 100644 --- a/src/main.h +++ b/src/main.h @@ -28,6 +28,7 @@ const char* get_prompt(); // commands provided by the snort module int main_delete_inspector(lua_State* = nullptr); int main_dump_stats(lua_State* = nullptr); +int main_dump_heap_stats(lua_State* = nullptr); int main_reset_stats(lua_State* = nullptr); int main_set_watchdog_params(lua_State* = nullptr); int main_rotate_stats(lua_State* = nullptr); diff --git a/src/main/snort_module.cc b/src/main/snort_module.cc index 6fed65261..0bc012654 100644 --- a/src/main/snort_module.cc +++ b/src/main/snort_module.cc @@ -120,6 +120,7 @@ static const Command snort_cmds[] = "delete an inspector from the default policy" }, { "dump_stats", main_dump_stats, nullptr, "show summary statistics" }, + { "dump_heap_stats", main_dump_heap_stats, nullptr, "show heap statistics" }, { "reset_stats", main_reset_stats, nullptr, "clear summary statistics" }, { "rotate_stats", main_rotate_stats, nullptr, "roll perfmonitor log files" }, { "reload_config", main_reload_config, s_reload_w_path, "load new configuration" }, diff --git a/src/memory/heap_interface.cc b/src/memory/heap_interface.cc index 8fd5cb2c9..ef88702ae 100644 --- a/src/memory/heap_interface.cc +++ b/src/memory/heap_interface.cc @@ -25,11 +25,14 @@ #include "heap_interface.h" #include +#include #ifdef HAVE_JEMALLOC #include #endif +#include "control/control.h" +#include "log/messages.h" #include "main/thread.h" namespace memory @@ -46,6 +49,9 @@ class JemallocInterface : public HeapInterface void get_process_total(uint64_t&, uint64_t&) override; void get_thread_allocs(uint64_t&, uint64_t&) override; + + void print_stats(ControlConn*) override; + }; static size_t stats_mib[2], mib_len = 2; @@ -53,6 +59,24 @@ static size_t stats_mib[2], mib_len = 2; static THREAD_LOCAL uint64_t* alloc_ptr = nullptr; static THREAD_LOCAL uint64_t* dealloc_ptr = nullptr; +static ControlConn* s_ctrlconn = nullptr; +static void log_jem_stats(void *,const char *buf) +{ + if (s_ctrlconn) + { + char tmp[STD_BUF]; + const char* end = buf + strlen(buf); + for(const char* p = buf; p < end ;) + { + int n = (end - p > (STD_BUF - 1)) ? (STD_BUF - 1) : (end - p); + std::memcpy(tmp, p, n); + tmp[n] = '\0'; + s_ctrlconn->respond("%s", tmp); + p += n; + } + } +} + void JemallocInterface::main_init() { mallctlnametomib("stats.allocated", stats_mib, &mib_len); @@ -90,6 +114,12 @@ void JemallocInterface::get_thread_allocs(uint64_t& alloc, uint64_t& dealloc) dealloc = *dealloc_ptr; } +void JemallocInterface::print_stats(ControlConn* ctrlcon) +{ + s_ctrlconn = ctrlcon; + malloc_stats_print(log_jem_stats, nullptr, nullptr); +} + //-------------------------------------------------------------------------- #else // disabled interface //-------------------------------------------------------------------------- diff --git a/src/memory/heap_interface.h b/src/memory/heap_interface.h index 966520248..070c28647 100644 --- a/src/memory/heap_interface.h +++ b/src/memory/heap_interface.h @@ -23,6 +23,7 @@ #include +class ControlConn; namespace memory { @@ -37,6 +38,7 @@ public: virtual void get_process_total(uint64_t& epoch, uint64_t& total) = 0; virtual void get_thread_allocs(uint64_t& alloc, uint64_t& dealloc) = 0; + virtual void print_stats(ControlConn*) { } static HeapInterface* get_instance(); protected: diff --git a/src/memory/memory_cap.cc b/src/memory/memory_cap.cc index 3a90ebf42..88f2fc966 100644 --- a/src/memory/memory_cap.cc +++ b/src/memory/memory_cap.cc @@ -296,5 +296,9 @@ void MemoryCap::print(bool verbose, bool init) } } +void MemoryCap::dump_mem_stats(ControlConn* ctrlcon) +{ + heap->print_stats(ctrlcon); +} } // namespace memory diff --git a/src/memory/memory_cap.h b/src/memory/memory_cap.h index 231598f5d..cef9963ce 100644 --- a/src/memory/memory_cap.h +++ b/src/memory/memory_cap.h @@ -29,6 +29,7 @@ #include "main/snort_types.h" struct MemoryConfig; +class ControlConn; namespace memory { @@ -76,6 +77,7 @@ public: // main thread - shutdown static void update_pegs(PegCount*); + static void dump_mem_stats(ControlConn*); #if defined(REG_TEST) || defined(UNIT_TEST) static void test_main_check(); #endif