]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4353: appid: updating appid cpu profiler cli for ims
authorUmang Sharma (umasharm) <umasharm@cisco.com>
Mon, 24 Jun 2024 13:12:41 +0000 (13:12 +0000)
committerChris Sherwin (chsherwi) <chsherwi@cisco.com>
Mon, 24 Jun 2024 13:12:41 +0000 (13:12 +0000)
Merge in SNORT/snort3 from ~UMASHARM/snort3:appid_profiler_cli to master

Squashed commit of the following:

commit 6ffb9e1039fff58491e408cf2513e40a98143ec8
Author: Umang Sharma <umasharm@cisco.com>
Date:   Fri Jun 14 15:00:43 2024 -0400

    appid: updating appid cpu profiler cli for ims

src/network_inspectors/appid/appid_cpu_profile_table.cc
src/network_inspectors/appid/appid_cpu_profile_table.h
src/network_inspectors/appid/appid_module.cc

index 44d25f66051b7a59e1f9e99b3f64f7f8e67c77ff..6e0d154e02ba81948a32bd6823af069c3f63f286 100644 (file)
 #include <sstream>
 #include <queue>
 #include <algorithm>
+#include <cstdarg>
 
 #include "appid_session.h"
 #include "appid_cpu_profile_table.h"
+#include "control/control.h"
 
 using namespace snort;
 
+
 #define TABLE_HEADER(num_rows) "AppId Performance Statistics (top %d appids)\n===================================================================================================================================================\n", num_rows
 static const char* columns = " AppId   App Name                   Usecs       Pkts     AvgUsecs/Pkt     Sessions     AvgUsecs/Sess     MaxPkts/Sess     MaxUsecs/Sess     %%/Total\n";
 static const char* partition = "---------------------------------------------------------------------------------------------------------------------------------------------------\n";
@@ -50,6 +53,33 @@ static std::string FormatWithCommas(uint64_t value)
     return numStr;
 }
 
+static void print_log(ControlConn* ctrlcon, AppidCPUProfilerOutputType output_type, int level, const char* format, ...)
+{
+    static std::vector<char> buffer(STD_BUF);
+
+    va_list args;
+    va_start(args, format);
+
+    int response_len = std::vsnprintf(buffer.data(), buffer.size(), format, args);
+
+    va_end(args);
+
+    if (response_len < 0 || static_cast<size_t>(response_len) >= buffer.size())
+        return;
+
+    switch (output_type)
+    {
+        case OUTPUT_CONSOLE:
+            LogRespond(ctrlcon, "%s", buffer.data());
+            break;
+        case OUPUT_LOGFILE:
+            appid_log(nullptr, level, "%s", buffer.data());
+            break;
+        default:
+            break;
+    }
+}
+
 // Comparator for priority queue based on avg_processing_time/session
 struct CompareByAvgProcessingTime {
     bool operator()(const std::pair<AppId, AppidCPUProfilerStats>& a, const std::pair<AppId, AppidCPUProfilerStats>& b) const {
@@ -60,36 +90,43 @@ struct CompareByAvgProcessingTime {
     }
 };
 
-AppidCpuTableDisplayStatus AppidCPUProfilingManager::display_appid_cpu_profiler_table(AppId appid, OdpContext& odp_ctxt)
+AppidCpuTableDisplayStatus AppidCPUProfilingManager::display_appid_cpu_profiler_table(AppId appid, OdpContext& odp_ctxt, ControlConn* ctrlcon)
 {
+    AppidCPUProfilerOutputType output_type = OUPUT_LOGFILE;
+
     if (odp_ctxt.is_appid_cpu_profiler_running())
         return DISPLAY_ERROR_APPID_PROFILER_RUNNING;
     else if (appid_cpu_profiling_table.empty())
         return DISPLAY_ERROR_TABLE_EMPTY;
 
+    if (ctrlcon)
+        output_type = OUTPUT_CONSOLE;
+
     auto bucket = appid_cpu_profiling_table.find(appid);
 
     if (bucket != appid_cpu_profiling_table.end())
     {
-        appid_log(nullptr, TRACE_INFO_LEVEL, TABLE_HEADER(1));
-        appid_log(nullptr, TRACE_INFO_LEVEL, columns);
-        appid_log(nullptr, TRACE_INFO_LEVEL, partition);
+        print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, TABLE_HEADER(1));
+        print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, columns);
+        print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, partition);
 
-        appid_log(nullptr, TRACE_INFO_LEVEL, " %5d   %-15.15s   %14.14s %10.10s  %15.14s  %11.11s  %16.14s  %15.14s   %15.14s  %10.2f\n",
+        print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, " %5d   %-15.15s   %14.14s %10.10s  %15.14s  %11.11s  %16.14s  %15.14s   %15.14s  %10.2f\n",
                 appid, bucket->second.app_name.c_str(), FormatWithCommas(bucket->second.processing_time).c_str(), FormatWithCommas(bucket->second.processed_packets).c_str(), 
                 FormatWithCommas(bucket->second.processing_time/bucket->second.processed_packets).c_str(), FormatWithCommas(bucket->second.per_appid_sessions).c_str(), 
                 FormatWithCommas(bucket->second.processing_time/bucket->second.per_appid_sessions).c_str(), FormatWithCommas(bucket->second.max_processed_pkts_per_session).c_str(), 
-                FormatWithCommas(bucket->second.max_processing_time_per_session).c_str(), (static_cast<float>(bucket->second.processing_time)/total_processing_time)*100);
+                FormatWithCommas(bucket->second.max_processing_time_per_session).c_str(), static_cast<float>(bucket->second.processing_time)/static_cast<float>(total_processing_time)*100);
     }
     else
     {
-        appid_log(nullptr, TRACE_INFO_LEVEL,"Appid %d not found in the table\n", appid);
+        print_log(ctrlcon, output_type, TRACE_INFO_LEVEL,"Appid %d not found in the table\n", appid);
     }
     return DISPLAY_SUCCESS;
 }
 
-AppidCpuTableDisplayStatus AppidCPUProfilingManager::display_appid_cpu_profiler_table(OdpContext& odp_ctxt, uint32_t display_rows_limit, bool override_running_flag)
+AppidCpuTableDisplayStatus AppidCPUProfilingManager::display_appid_cpu_profiler_table(OdpContext& odp_ctxt, uint32_t display_rows_limit, bool override_running_flag, ControlConn* ctrlcon)
 {
+    AppidCPUProfilerOutputType output_type = OUPUT_LOGFILE;
+
     if (odp_ctxt.is_appid_cpu_profiler_running() and !override_running_flag)
         return DISPLAY_ERROR_APPID_PROFILER_RUNNING;
     else if (appid_cpu_profiling_table.empty())
@@ -100,10 +137,13 @@ AppidCpuTableDisplayStatus AppidCPUProfilingManager::display_appid_cpu_profiler_
     for (const auto& entry : appid_cpu_profiling_table) 
         sorted_appid_cpu_profiler_table.push(entry);
 
+    if (ctrlcon)
+        output_type = OUTPUT_CONSOLE;
+
     display_rows_limit = static_cast<uint32_t>(std::min({static_cast<size_t>(display_rows_limit), sorted_appid_cpu_profiler_table.size(), static_cast<size_t>(APPID_CPU_PROFILER_MAX_DISPLAY_ROWS)}));
-    appid_log(nullptr, TRACE_INFO_LEVEL, TABLE_HEADER(display_rows_limit));
-    appid_log(nullptr, TRACE_INFO_LEVEL, columns);
-    appid_log(nullptr, TRACE_INFO_LEVEL, partition);
+    print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, TABLE_HEADER(display_rows_limit));
+    print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, columns);
+    print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, partition);
     
     uint32_t rows_displayed = 0;
 
@@ -114,18 +154,18 @@ AppidCpuTableDisplayStatus AppidCPUProfilingManager::display_appid_cpu_profiler_
         if (!entry.second.processed_packets or !entry.second.per_appid_sessions)
             continue;
             
-        appid_log(nullptr, TRACE_INFO_LEVEL, " %5d   %-15.15s   %14.14s %10.10s  %15.14s  %11.11s  %16.14s  %15.14s   %15.14s  %10.2f\n",
+        print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, " %5d   %-15.15s   %14.14s %10.10s  %15.14s  %11.11s  %16.14s  %15.14s   %15.14s  %10.2f\n",
                 entry.first, entry.second.app_name.c_str(), FormatWithCommas(entry.second.processing_time).c_str(), FormatWithCommas(entry.second.processed_packets).c_str(), 
                 FormatWithCommas(entry.second.processing_time/entry.second.processed_packets).c_str(), FormatWithCommas(entry.second.per_appid_sessions).c_str(), 
                 FormatWithCommas(entry.second.processing_time/entry.second.per_appid_sessions).c_str(), FormatWithCommas(entry.second.max_processed_pkts_per_session).c_str(),
-                FormatWithCommas(entry.second.max_processing_time_per_session).c_str(), (static_cast<float>(entry.second.processing_time)/total_processing_time)*100);
+                FormatWithCommas(entry.second.max_processing_time_per_session).c_str(), static_cast<float>(entry.second.processing_time)/static_cast<float>(total_processing_time)*100);
 
         rows_displayed += 1;
     } 
 
-    appid_log(nullptr, TRACE_INFO_LEVEL, partition);
+    print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, partition);
 
-    appid_log(nullptr, TRACE_INFO_LEVEL, "Totals(all_sessions)    : %15.15s %10.10s  %15.14s   %10.10s   %15.15s  %15.14s  %16.15s   %9d\n",
+    print_log(ctrlcon, output_type, TRACE_INFO_LEVEL, "Totals(all_sessions)    : %15.15s %10.10s  %15.14s   %10.10s   %15.15s  %15.14s  %16.15s   %9d\n",
             FormatWithCommas(total_processing_time).c_str(), FormatWithCommas(total_processed_packets).c_str(),  FormatWithCommas(total_processing_time/total_processed_packets).c_str(),
             FormatWithCommas(total_per_appid_sessions).c_str(), FormatWithCommas(total_processing_time/total_per_appid_sessions).c_str(),
             FormatWithCommas(max_processed_pkts_per_session).c_str(), FormatWithCommas(max_processing_time_per_session).c_str(), 100);
index 32a6d2764746b1e7dbb5fea7bd88f0a021fef959..ab0f3aa632db1041f2507ea5a4d21f3f88164de9 100644 (file)
 
 class AppIdSession;
 class OdpContext;
+class ControlConn;
+
+enum AppidCPUProfilerOutputType
+{
+    OUPUT_LOGFILE = 0,
+    OUTPUT_CONSOLE
+};
 
 #define APPID_CPU_PROFILER_DEFAULT_DISPLAY_ROWS 100
 #define APPID_CPU_PROFILER_MAX_DISPLAY_ROWS 2000
@@ -77,8 +84,9 @@ public:
     void check_appid_cpu_profiler_table_entry(const AppIdSession* asd, AppId payload_id);
     void update_totals(const AppidCPUProfilerStats& stats);
 
-    AppidCpuTableDisplayStatus display_appid_cpu_profiler_table(OdpContext&, uint32_t display_rows_limit = APPID_CPU_PROFILER_DEFAULT_DISPLAY_ROWS, bool override_running_flag = false);
-    AppidCpuTableDisplayStatus display_appid_cpu_profiler_table(AppId, OdpContext&);
+    AppidCpuTableDisplayStatus display_appid_cpu_profiler_table(OdpContext&, uint32_t display_rows_limit = APPID_CPU_PROFILER_DEFAULT_DISPLAY_ROWS,
+                                                                bool override_running_flag = false, ControlConn* control_conn = nullptr);
+    AppidCpuTableDisplayStatus display_appid_cpu_profiler_table(AppId, OdpContext&, ControlConn* control_conn = nullptr);
     
     void cleanup_appid_cpu_profiler_table();
 };
index 344c4492d2f486c68123ebc817ea2fdb27e23ce8..3fc1249f071423dfe1c8920990aaa0784580548a 100644 (file)
@@ -417,10 +417,10 @@ static int show_cpu_profiler_stats(lua_State* L)
             if (display_rows_limit > APPID_CPU_PROFILER_MAX_DISPLAY_ROWS)
                 ctrlcon->respond("given number of rows exceeds maximum limit of %d, limiting to %d\n",
                                                    APPID_CPU_PROFILER_MAX_DISPLAY_ROWS, APPID_CPU_PROFILER_MAX_DISPLAY_ROWS);
-            displayed = odp_ctxt.get_appid_cpu_profiler_mgr().display_appid_cpu_profiler_table(odp_ctxt, display_rows_limit);
+            displayed = odp_ctxt.get_appid_cpu_profiler_mgr().display_appid_cpu_profiler_table(odp_ctxt, display_rows_limit, false, ctrlcon);
         }
         else
-            displayed = odp_ctxt.get_appid_cpu_profiler_mgr().display_appid_cpu_profiler_table(appid, odp_ctxt);
+            displayed = odp_ctxt.get_appid_cpu_profiler_mgr().display_appid_cpu_profiler_table(appid, odp_ctxt, ctrlcon);
 
         switch (displayed){
             case DISPLAY_ERROR_TABLE_EMPTY: