From 81c42f49163bc01c441f0f3e9703b0393fb64860 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 3 Dec 2014 13:22:46 +0100 Subject: [PATCH] log-stats: expand membuffer if necessary Many threads could lead to a membuffer size requirement bigger than 64k. So use the expansion call to grow the buffer as needed. --- src/log-stats.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/log-stats.c b/src/log-stats.c index fafd4c2852..1ae4246208 100644 --- a/src/log-stats.c +++ b/src/log-stats.c @@ -47,7 +47,7 @@ #define DEFAULT_LOG_FILENAME "stats.log" #define MODULE_NAME "LogStatsLog" -#define OUTPUT_BUFFER_SIZE 65535 +#define OUTPUT_BUFFER_SIZE 16384 TmEcode LogStatsLogThreadInit(ThreadVars *, void *, void **); TmEcode LogStatsLogThreadDeinit(ThreadVars *, void *); @@ -102,8 +102,18 @@ int LogStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable *st) for (u = 0; u < st->nstats; u++) { if (st->stats[u].name == NULL) break; - MemBufferWriteString(aft->buffer, "%-25s | %-25s | %-" PRIu64 "\n", - st->stats[u].name, st->stats[u].tm_name, st->stats[u].value); + + char line[1024]; + size_t len = snprintf(line, sizeof(line), "%-25s | %-25s | %-" PRIu64 "\n", + st->stats[u].name, st->stats[u].tm_name, st->stats[u].value); + + /* since we can have many threads, the buffer might not be big enough. + * Expand if necessary. */ + if (MEMBUFFER_OFFSET(aft->buffer) + len > MEMBUFFER_SIZE(aft->buffer)) { + MemBufferExpand(&aft->buffer, OUTPUT_BUFFER_SIZE); + } + + MemBufferWriteString(aft->buffer, "%s", line); } SCMutexLock(&aft->statslog_ctx->file_ctx->fp_mutex);