From 7e70f136ec41042c1f323be4d31eec91f39f5006 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Tue, 26 May 2015 15:41:35 +0200 Subject: [PATCH] counters: various renames and cleanups --- src/counters.c | 195 +++++++++++++++++++++++-------------------------- 1 file changed, 93 insertions(+), 102 deletions(-) diff --git a/src/counters.c b/src/counters.c index 3638f2c47f..9a333a7eb7 100644 --- a/src/counters.c +++ b/src/counters.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2013 Open Information Security Foundation +/* Copyright (C) 2007-2015 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -88,14 +88,14 @@ typedef struct StatsGlobalContext_ { } StatsGlobalContext; static void *stats_thread_data = NULL; -static StatsGlobalContext *sc_perf_op_ctx = NULL; -static time_t sc_start_time; +static StatsGlobalContext *stats_ctx = NULL; +static time_t stats_start_time; /** refresh interval in seconds */ -static uint32_t sc_counter_tts = STATS_MGMTT_TTS; +static uint32_t stats_tts = STATS_MGMTT_TTS; /** is the stats counter enabled? */ -static char sc_counter_enabled = TRUE; +static char stats_enabled = TRUE; -static int SCPerfOutputCounterFileIface(ThreadVars *tv); +static int StatsOutput(ThreadVars *tv); static int StatsThreadRegister(const char *thread_name, SCPerfPublicContext *); /** stats table is filled each interval and passed to the @@ -104,14 +104,6 @@ static StatsTable stats_table = { NULL, NULL, 0, 0, 0, {0 , 0}}; static uint16_t counters_global_id = 0; -/** - * \brief The output interface dispatcher for the counter api - */ -void SCPerfOutputCounters(ThreadVars *tv) -{ - SCPerfOutputCounterFileIface(tv); -} - /** * \brief Adds a value of type uint64_t to the local counter. * @@ -214,26 +206,26 @@ static void SCPerfInitOPCtx(void) if (stats != NULL) { const char *enabled = ConfNodeLookupChildValue(stats, "enabled"); if (enabled != NULL && ConfValIsFalse(enabled)) { - sc_counter_enabled = FALSE; + stats_enabled = FALSE; SCLogDebug("Stats module has been disabled"); SCReturn; } const char *interval = ConfNodeLookupChildValue(stats, "interval"); if (interval != NULL) - sc_counter_tts = (uint32_t) atoi(interval); + stats_tts = (uint32_t) atoi(interval); } if (!OutputStatsLoggersRegistered()) { SCLogWarning(SC_WARN_NO_STATS_LOGGERS, "stats are enabled but no loggers are active"); - sc_counter_enabled = FALSE; + stats_enabled = FALSE; SCReturn; } /* Store the engine start time */ - time(&sc_start_time); + time(&stats_start_time); /* init the lock used by StatsThreadStore */ - if (SCMutexInit(&sc_perf_op_ctx->sts_lock, NULL) != 0) { + if (SCMutexInit(&stats_ctx->sts_lock, NULL) != 0) { SCLogError(SC_ERR_INITIALIZATION, "error initializing sts mutex"); exit(EXIT_FAILURE); } @@ -247,14 +239,14 @@ static void SCPerfInitOPCtx(void) */ static void SCPerfReleaseOPCtx() { - if (sc_perf_op_ctx == NULL) { + if (stats_ctx == NULL) { SCLogDebug("Counter module has been disabled"); return; } StatsThreadStore *sts = NULL; StatsThreadStore *temp = NULL; - sts = sc_perf_op_ctx->sts; + sts = stats_ctx->sts; while (sts != NULL) { if (sts->tm_name != NULL) @@ -268,8 +260,8 @@ static void SCPerfReleaseOPCtx() sts = temp; } - SCFree(sc_perf_op_ctx); - sc_perf_op_ctx = NULL; + SCFree(stats_ctx); + stats_ctx = NULL; /* free stats table */ if (stats_table.stats != NULL) { @@ -310,7 +302,7 @@ static void *SCPerfMgmtThread(void *arg) SCDropCaps(tv_local); - if (sc_perf_op_ctx == NULL) { + if (stats_ctx == NULL) { SCLogError(SC_ERR_PERF_STATS_NOT_INIT, "Perf Counter API not init" "SCPerfInitCounterApi() has to be called first"); TmThreadsSetFlag(tv_local, THV_CLOSED | THV_RUNNING_DONE); @@ -336,7 +328,7 @@ static void *SCPerfMgmtThread(void *arg) TmThreadsUnsetFlag(tv_local, THV_PAUSED); } - cond_time.tv_sec = time(NULL) + sc_counter_tts; + cond_time.tv_sec = time(NULL) + stats_tts; cond_time.tv_nsec = 0; /* wait for the set time, or until we are woken up by @@ -345,7 +337,7 @@ static void *SCPerfMgmtThread(void *arg) SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time); SCCtrlMutexUnlock(tv_local->ctrl_mutex); - SCPerfOutputCounters(tv_local); + StatsOutput(tv_local); if (TmThreadsCheckFlag(tv_local, THV_KILL)) { run = 0; @@ -397,7 +389,7 @@ static void *SCPerfWakeupThread(void *arg) SCDropCaps(tv_local); - if (sc_perf_op_ctx == NULL) { + if (stats_ctx == NULL) { SCLogError(SC_ERR_PERF_STATS_NOT_INIT, "Perf Counter API not init" "SCPerfInitCounterApi() has to be called first"); TmThreadsSetFlag(tv_local, THV_CLOSED | THV_RUNNING_DONE); @@ -588,16 +580,16 @@ static uint64_t SCPerfOutputCalculateCounterValue(SCPerfCounter *pc) } /** - * \brief The file output interface for the Perf Counter api + * \brief The output interface for the Stats API */ -static int SCPerfOutputCounterFileIface(ThreadVars *tv) +static int StatsOutput(ThreadVars *tv) { const StatsThreadStore *sts = NULL; const SCPerfCounter *pc = NULL; void *td = stats_thread_data; if (stats_table.nstats == 0) { - StatsThreadRegister("Global", &sc_perf_op_ctx->global_counter_ctx); + StatsThreadRegister("Global", &stats_ctx->global_counter_ctx); uint32_t nstats = counters_global_id; @@ -609,7 +601,7 @@ static int SCPerfOutputCounterFileIface(ThreadVars *tv) return -1; } - stats_table.ntstats = sc_perf_op_ctx->sts_cnt; + stats_table.ntstats = stats_ctx->sts_cnt; uint32_t array_size = stats_table.nstats * sizeof(StatsRecord); stats_table.tstats = SCCalloc(stats_table.ntstats, array_size); if (stats_table.tstats == NULL) { @@ -618,7 +610,7 @@ static int SCPerfOutputCounterFileIface(ThreadVars *tv) return -1; } - stats_table.start_time = sc_start_time; + stats_table.start_time = stats_start_time; } /** temporary local table to merge the per thread counters, @@ -631,12 +623,12 @@ static int SCPerfOutputCounterFileIface(ThreadVars *tv) memset(&merge_table, 0x00, counters_global_id * sizeof(struct CountersMergeTable)); - int thread = sc_perf_op_ctx->sts_cnt - 1; + int thread = stats_ctx->sts_cnt - 1; StatsRecord *table = stats_table.stats; /* Loop through the thread counter stores. The global counters * are in a separate store inside this list. */ - sts = sc_perf_op_ctx->sts; + sts = stats_ctx->sts; SCLogDebug("sts %p", sts); while (sts != NULL) { BUG_ON(thread < 0); @@ -777,7 +769,7 @@ TmEcode SCPerfOutputCounterSocket(json_t *cmd, uint32_t u = 0; int flag = 0; - if (sc_perf_op_ctx == NULL) { + if (stats_ctx == NULL) { json_object_set_new(answer, "message", json_string("No performance counter context")); return TM_ECODE_FAILED; @@ -792,7 +784,7 @@ TmEcode SCPerfOutputCounterSocket(json_t *cmd, return TM_ECODE_FAILED; } - sts = sc_perf_op_ctx->sts; + sts = stats_ctx->sts; while (sts != NULL) { json_t *jdata; int filled = 0; @@ -863,12 +855,12 @@ TmEcode SCPerfOutputCounterSocket(json_t *cmd, */ void StatsInit(void) { - BUG_ON(sc_perf_op_ctx != NULL); - if ( (sc_perf_op_ctx = SCMalloc(sizeof(StatsGlobalContext))) == NULL) { + BUG_ON(stats_ctx != NULL); + if ( (stats_ctx = SCMalloc(sizeof(StatsGlobalContext))) == NULL) { SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCPerfInitOPCtx. Exiting..."); exit(EXIT_FAILURE); } - memset(sc_perf_op_ctx, 0, sizeof(StatsGlobalContext)); + memset(stats_ctx, 0, sizeof(StatsGlobalContext)); } void StatsSetupPostConfig(void) @@ -887,7 +879,7 @@ void SCPerfSpawnThreads(void) { SCEnter(); - if (!sc_counter_enabled) { + if (!stats_enabled) { SCReturn; } @@ -1001,40 +993,18 @@ uint16_t StatsRegisterMaxCounter(char *cname, struct ThreadVars_ *tv) uint16_t StatsRegisterGlobalCounter(char *cname, uint64_t (*Func)(void)) { #ifdef UNITTESTS - if (sc_perf_op_ctx == NULL) + if (stats_ctx == NULL) return 0; #else - BUG_ON(sc_perf_op_ctx == NULL); + BUG_ON(stats_ctx == NULL); #endif uint16_t id = StatsRegisterQualifiedCounter(cname, NULL, - &(sc_perf_op_ctx->global_counter_ctx), + &(stats_ctx->global_counter_ctx), STATS_TYPE_FUNC, Func); return id; } -/** - * \brief Registers a normal, unqualified counter - * - * \param cname Name of the counter, to be registered - * \param tm_name Name of the engine module under which the counter has to be - * registered - * \param type Datatype of this counter variable - * \param pctx SCPerfPublicContext corresponding to the tm_name key under which the - * key has to be registered - * - * \retval id Counter id for the newly registered counter, or the already - * present counter - */ -static uint16_t SCPerfRegisterCounter(char *cname, char *tm_name, - SCPerfPublicContext *pctx) -{ - uint16_t id = StatsRegisterQualifiedCounter(cname, tm_name, pctx, - STATS_TYPE_NORMAL, NULL); - - return id; -} - typedef struct CountersIdType_ { uint16_t id; const char *string; @@ -1097,7 +1067,7 @@ void CountersIdHashFreeFunc(void *data) */ static int StatsThreadRegister(const char *thread_name, SCPerfPublicContext *pctx) { - if (sc_perf_op_ctx == NULL) { + if (stats_ctx == NULL) { SCLogDebug("Counter module has been disabled"); return 0; } @@ -1109,23 +1079,23 @@ static int StatsThreadRegister(const char *thread_name, SCPerfPublicContext *pct return 0; } - SCMutexLock(&sc_perf_op_ctx->sts_lock); - if (sc_perf_op_ctx->counters_id_hash == NULL) { - sc_perf_op_ctx->counters_id_hash = HashTableInit(256, CountersIdHashFunc, + SCMutexLock(&stats_ctx->sts_lock); + if (stats_ctx->counters_id_hash == NULL) { + stats_ctx->counters_id_hash = HashTableInit(256, CountersIdHashFunc, CountersIdHashCompareFunc, CountersIdHashFreeFunc); - BUG_ON(sc_perf_op_ctx->counters_id_hash == NULL); + BUG_ON(stats_ctx->counters_id_hash == NULL); } SCPerfCounter *pc = pctx->head; while (pc != NULL) { CountersIdType t = { 0, pc->cname }, *id = NULL; - id = HashTableLookup(sc_perf_op_ctx->counters_id_hash, &t, sizeof(t)); + id = HashTableLookup(stats_ctx->counters_id_hash, &t, sizeof(t)); if (id == NULL) { id = SCCalloc(1, sizeof(*id)); BUG_ON(id == NULL); id->id = counters_global_id++; id->string = pc->cname; - BUG_ON(HashTableAdd(sc_perf_op_ctx->counters_id_hash, id, sizeof(*id)) < 0); + BUG_ON(HashTableAdd(stats_ctx->counters_id_hash, id, sizeof(*id)) < 0); } pc->gid = id->id; pc = pc->next; @@ -1133,7 +1103,7 @@ static int StatsThreadRegister(const char *thread_name, SCPerfPublicContext *pct if ( (temp = SCMalloc(sizeof(StatsThreadStore))) == NULL) { - SCMutexUnlock(&sc_perf_op_ctx->sts_lock); + SCMutexUnlock(&stats_ctx->sts_lock); return 0; } memset(temp, 0, sizeof(StatsThreadStore)); @@ -1143,16 +1113,16 @@ static int StatsThreadRegister(const char *thread_name, SCPerfPublicContext *pct temp->name = SCStrdup(thread_name); if (unlikely(temp->name == NULL)) { SCFree(temp); - SCMutexUnlock(&sc_perf_op_ctx->sts_lock); + SCMutexUnlock(&stats_ctx->sts_lock); return 0; } - temp->next = sc_perf_op_ctx->sts; - sc_perf_op_ctx->sts = temp; - sc_perf_op_ctx->sts_cnt++; - SCLogDebug("sc_perf_op_ctx->sts %p", sc_perf_op_ctx->sts); + temp->next = stats_ctx->sts; + stats_ctx->sts = temp; + stats_ctx->sts_cnt++; + SCLogDebug("stats_ctx->sts %p", stats_ctx->sts); - SCMutexUnlock(&sc_perf_op_ctx->sts_lock); + SCMutexUnlock(&stats_ctx->sts_lock); return 1; } @@ -1336,13 +1306,34 @@ void SCPerfReleasePCA(SCPerfPrivateContext *pca) /*----------------------------------Unit_Tests--------------------------------*/ #ifdef UNITTESTS +/** \internal + * \brief Registers a normal, unqualified counter + * + * \param cname Name of the counter, to be registered + * \param tm_name Name of the engine module under which the counter has to be + * registered + * \param type Datatype of this counter variable + * \param pctx SCPerfPublicContext corresponding to the tm_name key under which the + * key has to be registered + * + * \retval id Counter id for the newly registered counter, or the already + * present counter + */ +static uint16_t RegisterCounter(char *cname, char *tm_name, + SCPerfPublicContext *pctx) +{ + uint16_t id = StatsRegisterQualifiedCounter(cname, tm_name, pctx, + STATS_TYPE_NORMAL, NULL); + return id; +} + static int SCPerfTestCounterReg02() { SCPerfPublicContext pctx; memset(&pctx, 0, sizeof(SCPerfPublicContext)); - return SCPerfRegisterCounter(NULL, NULL, &pctx); + return RegisterCounter(NULL, NULL, &pctx); } static int SCPerfTestCounterReg03() @@ -1352,7 +1343,7 @@ static int SCPerfTestCounterReg03() memset(&pctx, 0, sizeof(SCPerfPublicContext)); - result = SCPerfRegisterCounter("t1", "c1", &pctx); + result = RegisterCounter("t1", "c1", &pctx); SCPerfReleasePerfCounterS(pctx.head); @@ -1366,11 +1357,11 @@ static int SCPerfTestCounterReg04() memset(&pctx, 0, sizeof(SCPerfPublicContext)); - SCPerfRegisterCounter("t1", "c1", &pctx); - SCPerfRegisterCounter("t2", "c2", &pctx); - SCPerfRegisterCounter("t3", "c3", &pctx); + RegisterCounter("t1", "c1", &pctx); + RegisterCounter("t2", "c2", &pctx); + RegisterCounter("t3", "c3", &pctx); - result = SCPerfRegisterCounter("t1", "c1", &pctx); + result = RegisterCounter("t1", "c1", &pctx); SCPerfReleasePerfCounterS(pctx.head); @@ -1384,7 +1375,7 @@ static int SCPerfTestGetCntArray05() memset(&tv, 0, sizeof(ThreadVars)); - id = SCPerfRegisterCounter("t1", "c1", &tv.perf_public_ctx); + id = RegisterCounter("t1", "c1", &tv.perf_public_ctx); if (id != 1) { printf("id %d: ", id); return 0; @@ -1402,7 +1393,7 @@ static int SCPerfTestGetCntArray06() memset(&tv, 0, sizeof(ThreadVars)); - id = SCPerfRegisterCounter("t1", "c1", &tv.perf_public_ctx); + id = RegisterCounter("t1", "c1", &tv.perf_public_ctx); if (id != 1) return 0; @@ -1426,8 +1417,8 @@ static int SCPerfTestCntArraySize07() //pca = (SCPerfPrivateContext *)&tv.perf_private_ctx; - SCPerfRegisterCounter("t1", "c1", &tv.perf_public_ctx); - SCPerfRegisterCounter("t2", "c2", &tv.perf_public_ctx); + RegisterCounter("t1", "c1", &tv.perf_public_ctx); + RegisterCounter("t2", "c2", &tv.perf_public_ctx); SCPerfGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx); pca = &tv.perf_private_ctx; @@ -1452,7 +1443,7 @@ static int SCPerfTestUpdateCounter08() memset(&tv, 0, sizeof(ThreadVars)); - id = SCPerfRegisterCounter("t1", "c1", &tv.perf_public_ctx); + id = RegisterCounter("t1", "c1", &tv.perf_public_ctx); SCPerfGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx); pca = &tv.perf_private_ctx; @@ -1477,11 +1468,11 @@ static int SCPerfTestUpdateCounter09() memset(&tv, 0, sizeof(ThreadVars)); - id1 = SCPerfRegisterCounter("t1", "c1", &tv.perf_public_ctx); - SCPerfRegisterCounter("t2", "c2", &tv.perf_public_ctx); - SCPerfRegisterCounter("t3", "c3", &tv.perf_public_ctx); - SCPerfRegisterCounter("t4", "c4", &tv.perf_public_ctx); - id2 = SCPerfRegisterCounter("t5", "c5", &tv.perf_public_ctx); + id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx); + RegisterCounter("t2", "c2", &tv.perf_public_ctx); + RegisterCounter("t3", "c3", &tv.perf_public_ctx); + RegisterCounter("t4", "c4", &tv.perf_public_ctx); + id2 = RegisterCounter("t5", "c5", &tv.perf_public_ctx); SCPerfGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx); pca = &tv.perf_private_ctx; @@ -1507,9 +1498,9 @@ static int SCPerfTestUpdateGlobalCounter10() memset(&tv, 0, sizeof(ThreadVars)); - id1 = SCPerfRegisterCounter("t1", "c1", &tv.perf_public_ctx); - id2 = SCPerfRegisterCounter("t2", "c2", &tv.perf_public_ctx); - id3 = SCPerfRegisterCounter("t3", "c3", &tv.perf_public_ctx); + id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx); + id2 = RegisterCounter("t2", "c2", &tv.perf_public_ctx); + id3 = RegisterCounter("t3", "c3", &tv.perf_public_ctx); SCPerfGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx); pca = &tv.perf_private_ctx; @@ -1541,10 +1532,10 @@ static int SCPerfTestCounterValues11() memset(&tv, 0, sizeof(ThreadVars)); - id1 = SCPerfRegisterCounter("t1", "c1", &tv.perf_public_ctx); - id2 = SCPerfRegisterCounter("t2", "c2", &tv.perf_public_ctx); - id3 = SCPerfRegisterCounter("t3", "c3", &tv.perf_public_ctx); - id4 = SCPerfRegisterCounter("t4", "c4", &tv.perf_public_ctx); + id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx); + id2 = RegisterCounter("t2", "c2", &tv.perf_public_ctx); + id3 = RegisterCounter("t3", "c3", &tv.perf_public_ctx); + id4 = RegisterCounter("t4", "c4", &tv.perf_public_ctx); SCPerfGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx); pca = &tv.perf_private_ctx;