]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3697: memory: Added memusage pegs
authorSteven Baigal (sbaigal) <sbaigal@cisco.com>
Thu, 19 Jan 2023 23:33:13 +0000 (23:33 +0000)
committerSteven Baigal (sbaigal) <sbaigal@cisco.com>
Thu, 19 Jan 2023 23:33:13 +0000 (23:33 +0000)
Merge in SNORT/snort3 from ~AKAYAMBU/snort3:memusage to master

Squashed commit of the following:

commit 3a41f9cd67876831ce9c501f9fed17675f2e4718
Author: Arunkumar Kayambu <akayambu@cisco.com>
Date:   Fri Dec 9 06:56:16 2022 -0500

    memory: Added memusage pegs

src/file_api/file_capture.cc
src/network_inspectors/port_scan/ps_detect.cc
src/network_inspectors/port_scan/ps_detect.h
src/network_inspectors/port_scan/ps_module.cc
src/network_inspectors/port_scan/ps_pegs.h
src/service_inspectors/netflow/netflow.cc
src/service_inspectors/netflow/netflow_module.cc
src/service_inspectors/netflow/netflow_module.h

index 5f6997655720cb6012f275519c55458b0aa910a6..7a3f62e2fe81d3ae91713ed79e2c28c2932871c3 100644 (file)
@@ -539,10 +539,14 @@ void FileCapture::print_mem_usage()
 {
     if (file_mempool)
     {
+        int64_t block_size = get_block_size() + sizeof (FileCapture);
+        if (block_size & 7)
+            block_size += (8 - (block_size & 7));
         LogCount("Max buffers can allocate", file_mempool->total_objects());
         LogCount("Buffers in use", file_mempool->allocated());
         LogCount("Buffers in free list", file_mempool->freed());
         LogCount("Buffers in release list", file_mempool->released());
+        LogCount("Memory usage in bytes", file_mempool->allocated() * block_size);
     }
 }
 
index 45c1ce13de73f7a4e0ac56d780ddab24bcd2ac11..aaba4b82a838a9fe1cbcbac13712f148e4cc3895 100644 (file)
@@ -158,6 +158,15 @@ void ps_reset()
         portscan_hash->clear_hash();
 }
 
+void ps_update_memusage_peg()
+{
+    if (portscan_hash)
+        spstats.bytes_in_use = portscan_hash->get_mem_used();
+    else
+        spstats.bytes_in_use = 0;
+}
+
+
 //  Check scanner and scanned ips to see if we can filter them out.
 bool PortScan::ps_ignore_ip(const SfIp* scanner, uint16_t scanner_port,
     const SfIp* scanned, uint16_t scanned_port)
@@ -310,6 +319,7 @@ static PS_TRACKER* ps_tracker_get(PS_HASH_KEY* key)
 
     ht = (PS_TRACKER*)portscan_hash->get_mru_user_data();
 
+
     if ( ht )
         memset(ht, 0x00, sizeof(PS_TRACKER));
 
index dd6db6c0e855522f9d3635dda05788161e96c885..cc3ac55a906e06a71792a1c29973efc9bb32e9e8 100644 (file)
@@ -162,6 +162,7 @@ struct PS_PKT
 
 void ps_cleanup();
 void ps_reset();
+void ps_update_memusage_peg();
 
 unsigned ps_node_size();
 bool ps_init_hash(unsigned long);
index bfb0e1077f38afbcad67d6f2e4aff53454baff7b..e6c6ba5a24e59d80b2cbb1772b57ea9aaf5c38fa 100644 (file)
@@ -204,7 +204,10 @@ const PegInfo* PortScanModule::get_pegs() const
 { return ps_module_pegs; }
 
 PegCount* PortScanModule::get_counts() const
-{ return (PegCount*)&spstats; }
+{
+    ps_update_memusage_peg();
+    return (PegCount*)&spstats;
+}
 
 const RuleMap* PortScanModule::get_rules() const
 { return port_scan_rules; }
index 9b14682d0b28ff81f3b53e1c7612b10bd3fcbde4..d37180f0075b1269650e67d08ec4c7e35d998929 100644 (file)
@@ -30,6 +30,7 @@ static const PegInfo ps_module_pegs[] =
     { CountType::SUM, "trackers", "number of trackers allocated by port scan" },
     { CountType::SUM, "alloc_prunes", "number of trackers pruned on allocation of new tracking" },
     { CountType::SUM, "reload_prunes", "number of trackers pruned on reload due to reduced memcap" },
+    { CountType::NOW, "bytes_in_use", "number of bytes currently used by portscan" },
     { CountType::END, nullptr, nullptr },
 };
 
@@ -39,6 +40,7 @@ struct PsPegStats
     PegCount trackers;
     PegCount alloc_prunes;
     PegCount reload_prunes;
+    PegCount bytes_in_use;
 };
 
 #endif
index ddfacdf47db0aacca08689df009e546a92039354..d3b69a25f82cdad2bbf6a347cb79139003147aa2 100644 (file)
@@ -1043,6 +1043,8 @@ void NetFlowInspector::tterm()
     }
     delete netflow_cache;
     delete template_cache;
+    netflow_cache = nullptr;
+    template_cache = nullptr;
 }
 
 void NetFlowInspector::install_reload_handler(SnortConfig* sc)
index 785b5d148f2b36b31fb55f2eabe5b24dd69d0ec6..55357d5b68cac540b0c30a53f96c2261d15fbbe9 100644 (file)
 #include <sstream>
 #include <string>
 
+#include "netflow_cache.h"
 #include "netflow_module.h"
 
 #include "utils/util.h"
 
 using namespace snort;
 
+extern THREAD_LOCAL NetFlowCache* netflow_cache;
+extern THREAD_LOCAL TemplateFieldCache* template_cache;
 // -----------------------------------------------------------------------------
 // static variables
 // -----------------------------------------------------------------------------
@@ -94,6 +97,8 @@ static const PegInfo netflow_pegs[] =
     { CountType::SUM, "v9_templates", "count of total version 9 templates" },
     { CountType::SUM, "version_5", "count of netflow version 5 packets received" },
     { CountType::SUM, "version_9", "count of netflow version 9 packets received" },
+    { CountType::NOW, "netflow_cache_bytes_in_use", "number of bytes used in netflow cache" },
+    { CountType::NOW, "template_cache_bytes_in_use", "number of bytes used in template cache" },
     { CountType::END, nullptr, nullptr},
 };
 
@@ -251,7 +256,19 @@ void NetFlowModule::parse_service_id_file(const std::string& serv_id_file_path)
 }
 
 PegCount* NetFlowModule::get_counts() const
-{ return (PegCount*)&netflow_stats; }
+{
+    if (netflow_cache && template_cache)
+    {
+        netflow_stats.netflow_cache_bytes_in_use = netflow_cache->current_size;
+        netflow_stats.template_cache_bytes_in_use = template_cache->current_size;
+    }
+    else
+    {
+        netflow_stats.netflow_cache_bytes_in_use = 0;
+        netflow_stats.template_cache_bytes_in_use = 0;
+    }
+    return (PegCount*)&netflow_stats;
+}
 
 const PegInfo* NetFlowModule::get_pegs() const
 { return netflow_pegs; }
index 3cadfc0f0f10145d2772013efd4e4ffa9e5face6..73f8c2ba0514d9901bdb9c87c51e6ea497c2b266 100644 (file)
@@ -137,6 +137,8 @@ struct NetFlowStats : public LruCacheLocalStats
     PegCount v9_templates;
     PegCount version_5;
     PegCount version_9;
+    PegCount netflow_cache_bytes_in_use;
+    PegCount template_cache_bytes_in_use;
 };
 
 extern THREAD_LOCAL NetFlowStats netflow_stats;