counters: use ptr to name instead of copy

All counters have hardcoded names, so copies are not needed.
pull/1508/head
Victor Julien 11 years ago
parent 7e66c70507
commit 07efec550d

@ -489,9 +489,6 @@ static void *StatsWakeupThread(void *arg)
static void StatsReleaseCounter(StatsCounter *pc) static void StatsReleaseCounter(StatsCounter *pc)
{ {
if (pc != NULL) { if (pc != NULL) {
if (pc->cname != NULL)
SCFree(pc->cname);
SCFree(pc); SCFree(pc);
} }
@ -501,7 +498,7 @@ static void StatsReleaseCounter(StatsCounter *pc)
/** /**
* \brief Registers a counter. * \brief Registers a counter.
* *
* \param cname Name of the counter, to be registered * \param name Name of the counter, to be registered
* \param tm_name Thread module to which this counter belongs * \param tm_name Thread module to which this counter belongs
* \param pctx StatsPublicThreadContext for this tm-tv instance * \param pctx StatsPublicThreadContext for this tm-tv instance
* \param type_q Qualifier describing the type of counter to be registered * \param type_q Qualifier describing the type of counter to be registered
@ -510,7 +507,7 @@ static void StatsReleaseCounter(StatsCounter *pc)
* present counter on success * present counter on success
* \retval 0 on failure * \retval 0 on failure
*/ */
static uint16_t StatsRegisterQualifiedCounter(char *cname, char *tm_name, static uint16_t StatsRegisterQualifiedCounter(char *name, char *tm_name,
StatsPublicThreadContext *pctx, StatsPublicThreadContext *pctx,
int type_q, uint64_t (*Func)(void)) int type_q, uint64_t (*Func)(void))
{ {
@ -519,7 +516,7 @@ static uint16_t StatsRegisterQualifiedCounter(char *cname, char *tm_name,
StatsCounter *prev = NULL; StatsCounter *prev = NULL;
StatsCounter *pc = NULL; StatsCounter *pc = NULL;
if (cname == NULL || pctx == NULL) { if (name == NULL || pctx == NULL) {
SCLogDebug("Counter name, StatsPublicThreadContext NULL"); SCLogDebug("Counter name, StatsPublicThreadContext NULL");
return 0; return 0;
} }
@ -528,7 +525,7 @@ static uint16_t StatsRegisterQualifiedCounter(char *cname, char *tm_name,
while (temp != NULL) { while (temp != NULL) {
prev = temp; prev = temp;
if (strcmp(cname, temp->cname) == 0) { if (strcmp(name, temp->name) == 0) {
break; break;
} }
@ -539,20 +536,15 @@ static uint16_t StatsRegisterQualifiedCounter(char *cname, char *tm_name,
if (temp != NULL) if (temp != NULL)
return(temp->id); return(temp->id);
/* if we reach this point we don't have a counter registered by this cname */ /* if we reach this point we don't have a counter registered by this name */
if ( (pc = SCMalloc(sizeof(StatsCounter))) == NULL) if ( (pc = SCMalloc(sizeof(StatsCounter))) == NULL)
return 0; return 0;
memset(pc, 0, sizeof(StatsCounter)); memset(pc, 0, sizeof(StatsCounter));
if ( (pc->cname = SCStrdup(cname)) == NULL) {
SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory");
exit(EXIT_FAILURE);
}
/* assign a unique id to this StatsCounter. The id is local to this /* assign a unique id to this StatsCounter. The id is local to this
* thread context. Please note that the id start from 1, and not 0 */ * thread context. Please note that the id start from 1, and not 0 */
pc->id = ++(pctx->curr_id); pc->id = ++(pctx->curr_id);
pc->name = name;
pc->type = type_q; pc->type = type_q;
pc->Func = Func; pc->Func = Func;
@ -652,7 +644,7 @@ static int StatsOutput(ThreadVars *tv)
pc = sts->ctx->head; pc = sts->ctx->head;
while (pc != NULL) { while (pc != NULL) {
SCLogDebug("Counter %s (%u:%u) value %"PRIu64, SCLogDebug("Counter %s (%u:%u) value %"PRIu64,
pc->cname, pc->id, pc->gid, pc->value); pc->name, pc->id, pc->gid, pc->value);
thread_table[pc->gid].type = pc->type; thread_table[pc->gid].type = pc->type;
switch (pc->type) { switch (pc->type) {
@ -666,7 +658,7 @@ static int StatsOutput(ThreadVars *tv)
break; break;
} }
thread_table[pc->gid].updates = pc->updates; thread_table[pc->gid].updates = pc->updates;
table[pc->gid].name = pc->cname; table[pc->gid].name = pc->name;
pc = pc->next; pc = pc->next;
} }
@ -844,16 +836,16 @@ void StatsSpawnThreads(void)
/** /**
* \brief Registers a normal, unqualified counter * \brief Registers a normal, unqualified counter
* *
* \param cname Name of the counter, to be registered * \param name Name of the counter, to be registered
* \param tv Pointer to the ThreadVars instance for which the counter would * \param tv Pointer to the ThreadVars instance for which the counter would
* be registered * be registered
* *
* \retval id Counter id for the newly registered counter, or the already * \retval id Counter id for the newly registered counter, or the already
* present counter * present counter
*/ */
uint16_t StatsRegisterCounter(char *cname, struct ThreadVars_ *tv) uint16_t StatsRegisterCounter(char *name, struct ThreadVars_ *tv)
{ {
uint16_t id = StatsRegisterQualifiedCounter(cname, uint16_t id = StatsRegisterQualifiedCounter(name,
(tv->thread_group_name != NULL) ? tv->thread_group_name : tv->name, (tv->thread_group_name != NULL) ? tv->thread_group_name : tv->name,
&tv->perf_public_ctx, &tv->perf_public_ctx,
STATS_TYPE_NORMAL, NULL); STATS_TYPE_NORMAL, NULL);
@ -865,16 +857,16 @@ uint16_t StatsRegisterCounter(char *cname, struct ThreadVars_ *tv)
* \brief Registers a counter, whose value holds the average of all the values * \brief Registers a counter, whose value holds the average of all the values
* assigned to it. * assigned to it.
* *
* \param cname Name of the counter, to be registered * \param name Name of the counter, to be registered
* \param tv Pointer to the ThreadVars instance for which the counter would * \param tv Pointer to the ThreadVars instance for which the counter would
* be registered * be registered
* *
* \retval id Counter id for the newly registered counter, or the already * \retval id Counter id for the newly registered counter, or the already
* present counter * present counter
*/ */
uint16_t StatsRegisterAvgCounter(char *cname, struct ThreadVars_ *tv) uint16_t StatsRegisterAvgCounter(char *name, struct ThreadVars_ *tv)
{ {
uint16_t id = StatsRegisterQualifiedCounter(cname, uint16_t id = StatsRegisterQualifiedCounter(name,
(tv->thread_group_name != NULL) ? tv->thread_group_name : tv->name, (tv->thread_group_name != NULL) ? tv->thread_group_name : tv->name,
&tv->perf_public_ctx, &tv->perf_public_ctx,
STATS_TYPE_AVERAGE, NULL); STATS_TYPE_AVERAGE, NULL);
@ -886,16 +878,16 @@ uint16_t StatsRegisterAvgCounter(char *cname, struct ThreadVars_ *tv)
* \brief Registers a counter, whose value holds the maximum of all the values * \brief Registers a counter, whose value holds the maximum of all the values
* assigned to it. * assigned to it.
* *
* \param cname Name of the counter, to be registered * \param name Name of the counter, to be registered
* \param tv Pointer to the ThreadVars instance for which the counter would * \param tv Pointer to the ThreadVars instance for which the counter would
* be registered * be registered
* *
* \retval the counter id for the newly registered counter, or the already * \retval the counter id for the newly registered counter, or the already
* present counter * present counter
*/ */
uint16_t StatsRegisterMaxCounter(char *cname, struct ThreadVars_ *tv) uint16_t StatsRegisterMaxCounter(char *name, struct ThreadVars_ *tv)
{ {
uint16_t id = StatsRegisterQualifiedCounter(cname, uint16_t id = StatsRegisterQualifiedCounter(name,
(tv->thread_group_name != NULL) ? tv->thread_group_name : tv->name, (tv->thread_group_name != NULL) ? tv->thread_group_name : tv->name,
&tv->perf_public_ctx, &tv->perf_public_ctx,
STATS_TYPE_MAXIMUM, NULL); STATS_TYPE_MAXIMUM, NULL);
@ -906,13 +898,13 @@ uint16_t StatsRegisterMaxCounter(char *cname, struct ThreadVars_ *tv)
/** /**
* \brief Registers a counter, which represents a global value * \brief Registers a counter, which represents a global value
* *
* \param cname Name of the counter, to be registered * \param name Name of the counter, to be registered
* \param Func Function Pointer returning a uint64_t * \param Func Function Pointer returning a uint64_t
* *
* \retval id Counter id for the newly registered counter, or the already * \retval id Counter id for the newly registered counter, or the already
* present counter * present counter
*/ */
uint16_t StatsRegisterGlobalCounter(char *cname, uint64_t (*Func)(void)) uint16_t StatsRegisterGlobalCounter(char *name, uint64_t (*Func)(void))
{ {
#ifdef UNITTESTS #ifdef UNITTESTS
if (stats_ctx == NULL) if (stats_ctx == NULL)
@ -920,7 +912,7 @@ uint16_t StatsRegisterGlobalCounter(char *cname, uint64_t (*Func)(void))
#else #else
BUG_ON(stats_ctx == NULL); BUG_ON(stats_ctx == NULL);
#endif #endif
uint16_t id = StatsRegisterQualifiedCounter(cname, NULL, uint16_t id = StatsRegisterQualifiedCounter(name, NULL,
&(stats_ctx->global_counter_ctx), &(stats_ctx->global_counter_ctx),
STATS_TYPE_FUNC, STATS_TYPE_FUNC,
Func); Func);
@ -1010,13 +1002,13 @@ static int StatsThreadRegister(const char *thread_name, StatsPublicThreadContext
} }
StatsCounter *pc = pctx->head; StatsCounter *pc = pctx->head;
while (pc != NULL) { while (pc != NULL) {
CountersIdType t = { 0, pc->cname }, *id = NULL; CountersIdType t = { 0, pc->name }, *id = NULL;
id = HashTableLookup(stats_ctx->counters_id_hash, &t, sizeof(t)); id = HashTableLookup(stats_ctx->counters_id_hash, &t, sizeof(t));
if (id == NULL) { if (id == NULL) {
id = SCCalloc(1, sizeof(*id)); id = SCCalloc(1, sizeof(*id));
BUG_ON(id == NULL); BUG_ON(id == NULL);
id->id = counters_global_id++; id->id = counters_global_id++;
id->string = pc->cname; id->string = pc->name;
BUG_ON(HashTableAdd(stats_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->gid = id->id;
@ -1232,7 +1224,7 @@ void StatsThreadCleanup(ThreadVars *tv)
/** \internal /** \internal
* \brief Registers a normal, unqualified counter * \brief Registers a normal, unqualified counter
* *
* \param cname Name of the counter, to be registered * \param name Name of the counter, to be registered
* \param tm_name Name of the engine module under which the counter has to be * \param tm_name Name of the engine module under which the counter has to be
* registered * registered
* \param type Datatype of this counter variable * \param type Datatype of this counter variable
@ -1242,10 +1234,10 @@ void StatsThreadCleanup(ThreadVars *tv)
* \retval id Counter id for the newly registered counter, or the already * \retval id Counter id for the newly registered counter, or the already
* present counter * present counter
*/ */
static uint16_t RegisterCounter(char *cname, char *tm_name, static uint16_t RegisterCounter(char *name, char *tm_name,
StatsPublicThreadContext *pctx) StatsPublicThreadContext *pctx)
{ {
uint16_t id = StatsRegisterQualifiedCounter(cname, tm_name, pctx, uint16_t id = StatsRegisterQualifiedCounter(name, tm_name, pctx,
STATS_TYPE_NORMAL, NULL); STATS_TYPE_NORMAL, NULL);
return id; return id;
} }

@ -49,7 +49,7 @@ typedef struct StatsCounter_ {
uint64_t (*Func)(void); uint64_t (*Func)(void);
/* name of the counter */ /* name of the counter */
char *cname; const char *name;
/* the next perfcounter for this tv's tm instance */ /* the next perfcounter for this tv's tm instance */
struct StatsCounter_ *next; struct StatsCounter_ *next;

Loading…
Cancel
Save