-/* 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
} 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
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.
*
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);
}
*/
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)
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) {
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);
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
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;
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);
}
/**
- * \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;
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) {
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,
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);
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;
return TM_ECODE_FAILED;
}
- sts = sc_perf_op_ctx->sts;
+ sts = stats_ctx->sts;
while (sts != NULL) {
json_t *jdata;
int filled = 0;
*/
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)
{
SCEnter();
- if (!sc_counter_enabled) {
+ if (!stats_enabled) {
SCReturn;
}
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;
*/
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;
}
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;
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));
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;
}
/*----------------------------------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()
memset(&pctx, 0, sizeof(SCPerfPublicContext));
- result = SCPerfRegisterCounter("t1", "c1", &pctx);
+ result = RegisterCounter("t1", "c1", &pctx);
SCPerfReleasePerfCounterS(pctx.head);
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);
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;
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;
//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;
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;
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;
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;
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;