]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output: optimize root logging loop
authorVictor Julien <victor@inliniac.net>
Sat, 16 Nov 2019 14:46:13 +0000 (15:46 +0100)
committerVictor Julien <victor@inliniac.net>
Mon, 27 Jan 2020 19:20:08 +0000 (20:20 +0100)
Instead of unconditionally looping all the 'root' loggers, loop only
those that are in use.

Root loggers are: packet, tx, file, filedata, streaming.

src/output-file.c
src/output-filedata.c
src/output-packet.c
src/output-streaming.c
src/output-tx.c
src/output.c
src/output.h
src/runmodes.c

index 2747f2ed187856291b316e2ac02126775e4854d3..97aec6c2a8072cb5030bc1a5f2acfefad13c8f54 100644 (file)
@@ -263,10 +263,20 @@ static void OutputFileLogExitPrintStats(ThreadVars *tv, void *thread_data)
     }
 }
 
+static uint32_t OutputFileLoggerGetActiveCount(void)
+{
+    uint32_t cnt = 0;
+    for (OutputFileLogger *p = list; p != NULL; p = p->next) {
+        cnt++;
+    }
+    return cnt;
+}
+
 void OutputFileLoggerRegister(void)
 {
     OutputRegisterRootLogger(OutputFileLogThreadInit,
-        OutputFileLogThreadDeinit, OutputFileLogExitPrintStats, OutputFileLog);
+        OutputFileLogThreadDeinit, OutputFileLogExitPrintStats,
+        OutputFileLog, OutputFileLoggerGetActiveCount);
 }
 
 void OutputFileShutdown(void)
index 9467365ca5e3876699bb2ac7b2058bc9abc51b45..893818234c6018bf96968f412b4e88702d963961 100644 (file)
@@ -435,11 +435,20 @@ static void OutputFiledataLogExitPrintStats(ThreadVars *tv, void *thread_data)
     }
 }
 
+static uint32_t OutputFiledataLoggerGetActiveCount(void)
+{
+    uint32_t cnt = 0;
+    for (OutputFiledataLogger *p = list; p != NULL; p = p->next) {
+        cnt++;
+    }
+    return cnt;
+}
+
 void OutputFiledataLoggerRegister(void)
 {
     OutputRegisterRootLogger(OutputFiledataLogThreadInit,
         OutputFiledataLogThreadDeinit, OutputFiledataLogExitPrintStats,
-        OutputFiledataLog);
+        OutputFiledataLog, OutputFiledataLoggerGetActiveCount);
     SC_ATOMIC_INIT(g_file_store_id);
 }
 
index 45de33b1a13f69a1eace21195b4559f42f78cc19..e18bc4448bd38f911e8cfae88a7abc7f50e4d743 100644 (file)
@@ -210,11 +210,20 @@ static void OutputPacketLogExitPrintStats(ThreadVars *tv, void *thread_data)
     }
 }
 
+static uint32_t OutputPacketLoggerGetActiveCount(void)
+{
+    uint32_t cnt = 0;
+    for (OutputPacketLogger *p = list; p != NULL; p = p->next) {
+        cnt++;
+    }
+    return cnt;
+}
+
 void OutputPacketLoggerRegister(void)
 {
     OutputRegisterRootLogger(OutputPacketLogThreadInit,
         OutputPacketLogThreadDeinit, OutputPacketLogExitPrintStats,
-        OutputPacketLog);
+        OutputPacketLog, OutputPacketLoggerGetActiveCount);
 }
 
 void OutputPacketShutdown(void)
index 46dfebcf1a5f50af557548db927d063aab1d73f8..d445d888375a754da7dc559f4e771da6503f6ab7 100644 (file)
@@ -448,10 +448,19 @@ static void OutputStreamingLogExitPrintStats(ThreadVars *tv, void *thread_data)
     }
 }
 
+static uint32_t OutputStreamingLoggerGetActiveCount(void)
+{
+    uint32_t cnt = 0;
+    for (OutputStreamingLogger *p = list; p != NULL; p = p->next) {
+        cnt++;
+    }
+    return cnt;
+}
+
 void OutputStreamingLoggerRegister(void) {
     OutputRegisterRootLogger(OutputStreamingLogThreadInit,
         OutputStreamingLogThreadDeinit, OutputStreamingLogExitPrintStats,
-        OutputStreamingLog);
+        OutputStreamingLog, OutputStreamingLoggerGetActiveCount);
 }
 
 void OutputStreamingShutdown(void)
index edb198ad9754343c78517bb8ac2903bb04d5259f..cfc555fc4e4da513de6d214658bfb74b6340aa92 100644 (file)
@@ -371,10 +371,20 @@ static void OutputTxLogExitPrintStats(ThreadVars *tv, void *thread_data)
     }
 }
 
+static uint32_t OutputTxLoggerGetActiveCount(void)
+{
+    uint32_t cnt = 0;
+    for (OutputTxLogger *p = list; p != NULL; p = p->next) {
+        cnt++;
+    }
+    return cnt;
+}
+
+
 void OutputTxLoggerRegister (void)
 {
     OutputRegisterRootLogger(OutputTxLogThreadInit, OutputTxLogThreadDeinit,
-        OutputTxLogExitPrintStats, OutputTxLog);
+        OutputTxLogExitPrintStats, OutputTxLog, OutputTxLoggerGetActiveCount);
 }
 
 void OutputTxShutdown(void)
index 400b8f47ff18566770fc89c70f6ff223021b6533..1f234115ab592dac3153588e44be36a8815ca72e 100644 (file)
 #include "output-filestore.h"
 
 typedef struct RootLogger_ {
+    OutputLogFunc LogFunc;
     ThreadInitFunc ThreadInit;
     ThreadDeinitFunc ThreadDeinit;
     ThreadExitPrintStatsFunc ThreadExitPrintStats;
-    OutputLogFunc LogFunc;
+    OutputGetActiveCountFunc ActiveCntFunc;
 
     TAILQ_ENTRY(RootLogger_) entries;
 } RootLogger;
 
-/* List of registered root loggers. */
+/* List of registered root loggers. These are registered at start up and
+ * are independent of configuration. Later we will build a list of active
+ * loggers based on configuration. */
 static TAILQ_HEAD(, RootLogger_) registered_loggers =
     TAILQ_HEAD_INITIALIZER(registered_loggers);
 
+/* List of active root loggers. This means that at least one logger is enabled
+ * for each root logger type in the config. */
+static TAILQ_HEAD(, RootLogger_) active_loggers =
+    TAILQ_HEAD_INITIALIZER(active_loggers);
+
 typedef struct LoggerThreadStoreNode_ {
     void *thread_data;
     TAILQ_ENTRY(LoggerThreadStoreNode_) entries;
@@ -918,7 +926,7 @@ void OutputNotifyFileRotation(void) {
 TmEcode OutputLoggerLog(ThreadVars *tv, Packet *p, void *thread_data)
 {
     LoggerThreadStore *thread_store = (LoggerThreadStore *)thread_data;
-    RootLogger *logger = TAILQ_FIRST(&registered_loggers);
+    RootLogger *logger = TAILQ_FIRST(&active_loggers);
     LoggerThreadStoreNode *thread_store_node = TAILQ_FIRST(thread_store);
     while (logger && thread_store_node) {
         if (logger->LogFunc != NULL) {
@@ -941,7 +949,7 @@ TmEcode OutputLoggerThreadInit(ThreadVars *tv, const void *initdata, void **data
     *data = (void *)thread_store;
 
     RootLogger *logger;
-    TAILQ_FOREACH(logger, &registered_loggers, entries) {
+    TAILQ_FOREACH(logger, &active_loggers, entries) {
 
         void *child_thread_data = NULL;
         if (logger->ThreadInit != NULL) {
@@ -968,7 +976,7 @@ TmEcode OutputLoggerThreadDeinit(ThreadVars *tv, void *thread_data)
         return TM_ECODE_FAILED;
 
     LoggerThreadStore *thread_store = (LoggerThreadStore *)thread_data;
-    RootLogger *logger = TAILQ_FIRST(&registered_loggers);
+    RootLogger *logger = TAILQ_FIRST(&active_loggers);
     LoggerThreadStoreNode *thread_store_node = TAILQ_FIRST(thread_store);
     while (logger && thread_store_node) {
         if (logger->ThreadDeinit != NULL) {
@@ -991,7 +999,7 @@ TmEcode OutputLoggerThreadDeinit(ThreadVars *tv, void *thread_data)
 void OutputLoggerExitPrintStats(ThreadVars *tv, void *thread_data)
 {
     LoggerThreadStore *thread_store = (LoggerThreadStore *)thread_data;
-    RootLogger *logger = TAILQ_FIRST(&registered_loggers);
+    RootLogger *logger = TAILQ_FIRST(&active_loggers);
     LoggerThreadStoreNode *thread_store_node = TAILQ_FIRST(thread_store);
     while (logger && thread_store_node) {
         if (logger->ThreadExitPrintStats != NULL) {
@@ -1005,7 +1013,7 @@ void OutputLoggerExitPrintStats(ThreadVars *tv, void *thread_data)
 void OutputRegisterRootLogger(ThreadInitFunc ThreadInit,
     ThreadDeinitFunc ThreadDeinit,
     ThreadExitPrintStatsFunc ThreadExitPrintStats,
-    OutputLogFunc LogFunc)
+    OutputLogFunc LogFunc, OutputGetActiveCountFunc ActiveCntFunc)
 {
     RootLogger *logger = SCCalloc(1, sizeof(*logger));
     if (logger == NULL) {
@@ -1015,9 +1023,46 @@ void OutputRegisterRootLogger(ThreadInitFunc ThreadInit,
     logger->ThreadDeinit = ThreadDeinit;
     logger->ThreadExitPrintStats = ThreadExitPrintStats;
     logger->LogFunc = LogFunc;
+    logger->ActiveCntFunc = ActiveCntFunc;
     TAILQ_INSERT_TAIL(&registered_loggers, logger, entries);
 }
 
+static void OutputRegisterActiveLogger(RootLogger *reg)
+{
+    RootLogger *logger = SCCalloc(1, sizeof(*logger));
+    if (logger == NULL) {
+        FatalError(SC_ERR_MEM_ALLOC, "failed to alloc root logger");
+    }
+    logger->ThreadInit = reg->ThreadInit;
+    logger->ThreadDeinit = reg->ThreadDeinit;
+    logger->ThreadExitPrintStats = reg->ThreadExitPrintStats;
+    logger->LogFunc = reg->LogFunc;
+    logger->ActiveCntFunc = reg->ActiveCntFunc;
+    TAILQ_INSERT_TAIL(&active_loggers, logger, entries);
+}
+
+void OutputSetupActiveLoggers(void)
+{
+    RootLogger *logger = TAILQ_FIRST(&registered_loggers);
+    while (logger) {
+        uint32_t cnt = logger->ActiveCntFunc();
+        if (cnt) {
+            OutputRegisterActiveLogger(logger);
+        }
+
+        logger = TAILQ_NEXT(logger, entries);
+    }
+}
+
+void OutputClearActiveLoggers(void)
+{
+    RootLogger *logger;
+    while ((logger = TAILQ_FIRST(&active_loggers)) != NULL) {
+        TAILQ_REMOVE(&active_loggers, logger, entries);
+        SCFree(logger);
+    }
+}
+
 void TmModuleLoggerRegister(void)
 {
     OutputRegisterRootLoggers();
index d6873d076f4772840a61480b5c093187bd676b01..14bf4d61f97607661dcdc527e8f0cfd5f57330e2 100644 (file)
@@ -46,6 +46,7 @@ typedef struct OutputInitResult_ {
 typedef OutputInitResult (*OutputInitFunc)(ConfNode *);
 typedef OutputInitResult (*OutputInitSubFunc)(ConfNode *, OutputCtx *);
 typedef TmEcode (*OutputLogFunc)(ThreadVars *, Packet *, void *);
+typedef uint32_t (*OutputGetActiveCountFunc)(void);
 
 typedef struct OutputModule_ {
     LoggerId logger_id;
@@ -196,7 +197,7 @@ void OutputNotifyFileRotation(void);
 void OutputRegisterRootLogger(ThreadInitFunc ThreadInit,
     ThreadDeinitFunc ThreadDeinit,
     ThreadExitPrintStatsFunc ThreadExitPrintStats,
-    OutputLogFunc LogFunc);
+    OutputLogFunc LogFunc, OutputGetActiveCountFunc ActiveCntFunc);
 void TmModuleLoggerRegister(void);
 
 TmEcode OutputLoggerLog(ThreadVars *, Packet *, void *);
@@ -204,4 +205,7 @@ TmEcode OutputLoggerThreadInit(ThreadVars *, const void *, void **);
 TmEcode OutputLoggerThreadDeinit(ThreadVars *, void *);
 void OutputLoggerExitPrintStats(ThreadVars *, void *);
 
+void OutputSetupActiveLoggers(void);
+void OutputClearActiveLoggers(void);
+
 #endif /* ! __OUTPUT_H__ */
index 47f774493d82b4528b5d29eade106cfacec3b2d3..fbea455bbd7b92300fc70fed8524521b615b3d87 100644 (file)
@@ -531,6 +531,8 @@ void RunModeShutDown(void)
     OutputStatsShutdown();
     OutputFlowShutdown();
 
+    OutputClearActiveLoggers();
+
     /* Reset logger counts. */
     file_logger_count = 0;
     filedata_logger_count = 0;
@@ -890,6 +892,7 @@ void RunModeInitializeOutputs(void)
             AppLayerParserRegisterLoggerBits(IPPROTO_UDP, a, logger_bits[a]);
 
     }
+    OutputSetupActiveLoggers();
 }
 
 float threading_detect_ratio = 1;