From 92c7914bfe07ea98c45565261dc5ff708fa6ddda Mon Sep 17 00:00:00 2001 From: "Vitalii Serhiiovych Horbatov -X (vhorbato - SOFTSERVE INC at Cisco)" Date: Mon, 26 Jun 2023 09:04:01 +0000 Subject: [PATCH] Pull request #3885: profiler: change date output format in rule profiler from microseconds to seconds Merge in SNORT/snort3 from ~VHORBATO/snort3:rule_prof_date to master Squashed commit of the following: commit cbcd8133dd4f0d2d61460719414507319fa82c5d Author: vhorbato Date: Fri Jun 16 18:55:11 2023 +0300 profiler: fix date related problems in rule_profiling json output --- src/profiler/json_view.cc | 16 +++++----------- src/profiler/profiler_module.cc | 8 ++++++-- src/profiler/table_view.cc | 2 -- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/profiler/json_view.cc b/src/profiler/json_view.cc index bd2a90bcd..ec3f62d58 100644 --- a/src/profiler/json_view.cc +++ b/src/profiler/json_view.cc @@ -30,7 +30,6 @@ #include "control/control.h" #include "helpers/json_stream.h" #include "main/snort_config.h" -#include "utils/stats.h" #include "profiler_printer.h" #include "rule_profiler.h" @@ -80,19 +79,11 @@ void print_json_entries(ControlConn* ctrlcon, std::vector& ent std::ostringstream ss; JsonStream json(ss); - RuleContext::set_end_time(get_time_curr()); RuleContext::count_total_time(); - double start_time_usec = - RuleContext::get_start_time()->tv_sec * 1000000.0 + RuleContext::get_start_time()->tv_usec; - double end_time_usec = - RuleContext::get_end_time()->tv_sec * 1000000.0 + RuleContext::get_end_time()->tv_usec; - double total_time_usec = - RuleContext::get_total_time()->tv_sec * 1000000.0 + RuleContext::get_total_time()->tv_usec; - json.open(); - json.put("startTime", start_time_usec); - json.put("endTime", end_time_usec); + json.put("startTime", RuleContext::get_start_time()->tv_sec); + json.put("endTime", RuleContext::get_end_time()->tv_sec); json.open_array("rules"); json.put_eol(); @@ -104,6 +95,9 @@ void print_json_entries(ControlConn* ctrlcon, std::vector& ent if ( sort ) std::partial_sort(entries.begin(), entries.begin() + count, entries.end(), sort); + double total_time_usec = + RuleContext::get_total_time()->tv_sec * 1000000.0 + RuleContext::get_total_time()->tv_usec; + for ( unsigned i = 0; i < count; ++i ) print_single_entry(ctrlcon, entries[i], i + 1, count, total_time_usec); diff --git a/src/profiler/profiler_module.cc b/src/profiler/profiler_module.cc index 66c6a4b5e..4cb82f845 100644 --- a/src/profiler/profiler_module.cc +++ b/src/profiler/profiler_module.cc @@ -33,6 +33,7 @@ #include "main/snort.h" #include "main/snort_config.h" #include "managers/module_manager.h" +#include "utils/stats.h" #include "rule_profiler.h" #include "rule_profiler_defs.h" @@ -109,6 +110,7 @@ public: const auto* config = SnortConfig::get_conf()->get_profiler(); assert(config); + RuleContext::set_end_time(get_time_curr()); print_rule_profiler_stats(config->rule, stats, ctrlcon, out_type); } @@ -170,6 +172,7 @@ static int rule_profiling_start(lua_State* L) } RuleContext::set_enabled(true); + RuleContext::set_start_time(get_time_curr()); main_broadcast_command(new ProfilerControl(ProfilerControl::CommandType::ENABLE), ctrlcon); main_broadcast_command(new ProfilerRuleReset(), ctrlcon); LogRespond(ctrlcon, "Rule profiler started\n"); @@ -189,6 +192,7 @@ static int rule_profiling_stop(lua_State* L) } RuleContext::set_enabled(false); + RuleContext::set_end_time(get_time_curr()); main_broadcast_command(new ProfilerControl(ProfilerControl::CommandType::DISABLE), ctrlcon); LogRespond(ctrlcon, "Rule profiler stopped\n"); @@ -246,7 +250,7 @@ static int rule_profiling_dump(lua_State* L) static const Parameter profiler_dump_params[] = { { "output", Parameter::PT_ENUM, "table | json", - "table", "print rule statistics in table or json format" }, + "table", "output format for rule statistics" }, { nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr } }; @@ -263,7 +267,7 @@ static const Command profiler_cmds[] = nullptr, "print rule profiler status" }, { "rule_dump", rule_profiling_dump, - profiler_dump_params, "print rule statistics" }, + profiler_dump_params, "print rule statistics in table or json format (json format prints dates as Unix epoch)" }, { nullptr, nullptr, nullptr, nullptr } }; diff --git a/src/profiler/table_view.cc b/src/profiler/table_view.cc index 675a03423..90d98b90b 100644 --- a/src/profiler/table_view.cc +++ b/src/profiler/table_view.cc @@ -29,7 +29,6 @@ #include "control/control.h" #include "detection/treenodes.h" -#include "utils/stats.h" #include "profiler_printer.h" #include "profiler_stats_table.h" @@ -100,7 +99,6 @@ void print_entries(ControlConn* ctrlcon, std::vector& entries, ProfilerSorter& sort, unsigned count) { std::ostringstream ss; - RuleContext::set_end_time(get_time_curr()); RuleContext::count_total_time(); double total_time_usec = -- 2.47.3