]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3783: memory: add shell command to dump heap stats
authorSteven Baigal (sbaigal) <sbaigal@cisco.com>
Mon, 20 Mar 2023 14:45:59 +0000 (14:45 +0000)
committerSteven Baigal (sbaigal) <sbaigal@cisco.com>
Mon, 20 Mar 2023 14:45:59 +0000 (14:45 +0000)
Merge in SNORT/snort3 from ~SBAIGAL/snort3:memstats to master

Squashed commit of the following:

commit ebe8554f4f5e95c464f08e57393d4fc204b531a0
Author: Steven Baigal <sbaigal@cisco.com>
Date:   Wed Mar 15 17:19:35 2023 -0400

    memory: add shell command to dump heap stats

src/main.cc
src/main.h
src/main/snort_module.cc
src/memory/heap_interface.cc
src/memory/heap_interface.h
src/memory/memory_cap.cc
src/memory/memory_cap.h

index a13211d954f1c079e7ed9b84eb0f6f88712b18c9..9049b4ca53bc592066bd3d3912ade371d632d75b 100644 (file)
@@ -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);
index ccdf74b5e0337cbaed1074d45f69def0f30ea90e..c9c66213e0b519c1cc7ff5daf4d2ce88a096b970 100644 (file)
@@ -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);
index 6fed652617c59327bdd453f9761239b2e3d4c693..0bc0126542e67c9836bca9a95c5ddc620e7dadb0 100644 (file)
@@ -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" },
index 8fd5cb2c9735e528edf36cf3c4990076c7c9d589..ef88702ae780fea916e17aebe67708f6d36c012f 100644 (file)
 #include "heap_interface.h"
 
 #include <cassert>
+#include <cstring>
 
 #ifdef HAVE_JEMALLOC
 #include <jemalloc/jemalloc.h>
 #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
 //--------------------------------------------------------------------------
index 9665202481262aea46c563f38604ca8a37339542..070c28647774d04ba02c8a5930ce6036690e926e 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <cstdint>
 
+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:
index 3a90ebf42278abf1a200775c9554c3c40a89516f..88f2fc9661bac301162bba5926e978e5b99ca7fc 100644 (file)
@@ -296,5 +296,9 @@ void MemoryCap::print(bool verbose, bool init)
     }
 }
 
+void MemoryCap::dump_mem_stats(ControlConn* ctrlcon)
+{
+    heap->print_stats(ctrlcon);
+}
 } // namespace memory
 
index 231598f5d0baa45b9a55cc22d8841e2cc0dd4e11..cef9963ce6fed530b131c71bad19abe3aaf17b29 100644 (file)
@@ -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