diff --git a/src/counters.c b/src/counters.c index f4fca5c0d4..7c2cf8fb1e 100644 --- a/src/counters.c +++ b/src/counters.c @@ -208,7 +208,8 @@ void * PerfWakeupThread(void *arg) * \retval the counter id */ u_int32_t PerfRegisterCounter(char *cname, char *tm_name, int type, - char *desc, PerfContext *pctx) + char *desc, PerfContext *pctx, int type_q, + int disp) { PerfCounter **head = &pctx->head; PerfCounter *temp = NULL; @@ -216,13 +217,17 @@ u_int32_t PerfRegisterCounter(char *cname, char *tm_name, int type, PerfCounter *pc = NULL; if (cname == NULL || tm_name == NULL || pctx == NULL) { +#ifdef DEBUG printf("counter name, tm name null or PerfContext NULL\n"); +#endif return 0; } /* (TYPE_MAX - 1) because we still haven't implemented TYPE_STR */ if ((type >= (TYPE_MAX - 1)) || (type < 0)) { +#ifdef DEBUG printf("Error: Counters of type %d can't be registered\n", type); +#endif return 0; } @@ -294,9 +299,234 @@ u_int32_t PerfRegisterCounter(char *cname, char *tm_name, int type, if (desc != NULL) pc->desc = strdup(desc); + pc->type_q = type_q; + + pc->disp = disp; + return pc->id; } +/** + * \brief Increments the local counter + * + * \param id Index of the counter in the counter array + * \param pca Counter array that holds the local counters for this TM + */ +inline void PerfCounterIncr(u_int64_t id, PerfCounterArray *pca) +{ + if (!pca) { +#ifdef DEBUG + printf("counterarray is NULL\n"); +#endif + return; + } + if ((id < 1) || (id > pca->size)) { +#ifdef DEBUG + printf("counter doesn't exist\n"); +#endif + return; + } + + switch (pca->head[id].pc->value->type) { + case TYPE_UINT64: + pca->head[id].ui64_cnt++; + break; + case TYPE_DOUBLE: + pca->head[id].d_cnt++; + break; + } + + if (pca->head[id].syncs == ULONG_MAX) { + pca->head[id].syncs = 0; + pca->head[id].wrapped_syncs++; + } + pca->head[id].syncs++; + + return; +} + +/** + * \brief Adds a value of type u_int64_t to the local counter. + * + * \param id ID of the counter as set by the API + * \param pca Counter array that holds the local counter for this TM + * \param x Value to add to this local counter + */ +inline void PerfCounterAddUI64(u_int64_t id, PerfCounterArray *pca, u_int64_t x) +{ + if (!pca) { +#ifdef DEBUG + printf("counterarray is NULL\n"); +#endif + return; + } + if ((id < 1) || (id > pca->size)) { +#ifdef DEBUG + printf("counter doesn't exist\n"); +#endif + return; + } + + switch (pca->head[id].pc->value->type) { + case TYPE_UINT64: + pca->head[id].ui64_cnt += x; + break; + case TYPE_DOUBLE: + pca->head[id].d_cnt += x; + break; + } + + if (pca->head[id].syncs == ULONG_MAX) { + pca->head[id].syncs = 0; + pca->head[id].wrapped_syncs++; + } + pca->head[id].syncs++; + + return; +} + +/** + * \brief Adds a value of type double to the local counter + * + * \param id ID of the counter as set by the API + * \param pca Counter array that holds the local counter for this TM + * \param x Value to add to this local counter + */ +inline void PerfCounterAddDouble(u_int64_t id, PerfCounterArray *pca, double x) +{ + if (!pca) { +#ifdef DEBUG + printf("counterarray is NULL\n"); +#endif + return; + } + if ((id < 1) || (id > pca->size)) { +#ifdef DEBUG + printf("counter doesn't exist\n"); +#endif + return; + } + + /* incase you are trying to add a double to a counter of type TYPE_UINT64 + it will be truncated */ + switch (pca->head[id].pc->value->type) { + case TYPE_UINT64: + pca->head[id].ui64_cnt += x; + break; + case TYPE_DOUBLE: + pca->head[id].d_cnt += x; + break; + } + + if (pca->head[id].syncs == ULONG_MAX) { + pca->head[id].syncs = 0; + pca->head[id].wrapped_syncs++; + } + pca->head[id].syncs++; + + return; +} + +/** + * \brief Sets a local counter to an arg of type double + * + * \param id Index of the local counter in the counter array + * \param pca Pointer to the PerfCounterArray + * \param x The value to set for the counter + */ +inline void PerfCounterSetUI64(u_int64_t id, PerfCounterArray *pca, + u_int64_t x) +{ + if (!pca) { +#ifdef DEBUG + printf("counterarray is NULL\n"); +#endif + return; + } + + if ((id < 1) || (id > pca->size)) { +#ifdef DEBUG + printf("counter doesn't exist\n"); +#endif + return; + } + + switch (pca->head[id].pc->value->type) { + case TYPE_UINT64: + if (pca->head[id].pc->type_q & TYPE_Q_MAXIMUM) { + if (x > pca->head[id].ui64_cnt) + pca->head[id].ui64_cnt = x; + } else if (pca->head[id].pc->type_q & TYPE_Q_NONE) + pca->head[id].ui64_cnt = x; + break; + case TYPE_DOUBLE: + if (pca->head[id].pc->type_q & TYPE_Q_MAXIMUM) { + if (x > pca->head[id].d_cnt) + pca->head[id].d_cnt = x; + } else if (pca->head[id].pc->type_q & TYPE_Q_NONE) + pca->head[id].d_cnt = x; + break; + } + + if (pca->head[id].syncs == ULONG_MAX) { + pca->head[id].syncs = 0; + pca->head[id].wrapped_syncs++; + } + pca->head[id].syncs++; + + return; +} + +/** + * \brief Sets a local counter to an arg of type double + * + * \param id Index of the local counter in the counter array + * \param pca Pointer to the PerfCounterArray + * \param x The value to set for the counter + */ +inline void PerfCounterSetDouble(u_int64_t id, PerfCounterArray *pca, + double x) +{ + if (!pca) { +#ifdef DEBUG + printf("counterarray is NULL\n"); +#endif + return; + } + + if ((id < 1) || (id > pca->size)) { +#ifdef DEBUG + printf("counter doesn't exist\n"); +#endif + return; + } + + switch (pca->head[id].pc->value->type) { + case TYPE_UINT64: + if (pca->head[id].pc->type_q & TYPE_Q_MAXIMUM) { + if (x > pca->head[id].ui64_cnt) + pca->head[id].ui64_cnt = x; + } else if (pca->head[id].pc->type_q & TYPE_Q_NONE) + pca->head[id].ui64_cnt = x; + break; + case TYPE_DOUBLE: + if (pca->head[id].pc->type_q & TYPE_Q_MAXIMUM) { + if (x > pca->head[id].d_cnt) + pca->head[id].d_cnt = x; + } else if (pca->head[id].pc->type_q & TYPE_Q_NONE) + pca->head[id].d_cnt = x; + break; + } + + if (pca->head[id].syncs == ULONG_MAX) { + pca->head[id].syncs = 0; + pca->head[id].wrapped_syncs++; + } + pca->head[id].syncs++; + + return; +} + /** * \brief Adds a TM to the clubbed TM table. Multiple instances of the same TM * are stacked together in a PCTMI container @@ -356,7 +586,8 @@ void PerfAddToClubbedTMTable(char *tm_name, PerfContext *pctx) return; } - pctmi->head = realloc(pctmi->head, (pctmi->size + 1) * sizeof(PerfContext **)); + pctmi->head = realloc(pctmi->head, (pctmi->size + 1) * + sizeof(PerfContext **)); hpctx = pctmi->head; hpctx[pctmi->size] = pctx; @@ -388,26 +619,35 @@ void PerfAddToClubbedTMTable(char *tm_name, PerfContext *pctx) PerfCounterArray * PerfGetCounterArrayRange(u_int32_t s_id, u_int32_t e_id, PerfContext *pctx) { + PerfCounter *pc = NULL; PerfCounterArray *pca = NULL; u_int32_t i = 0; if (pctx == NULL) { +#ifdef DEBUG printf("pctx is NULL\n"); +#endif return NULL; } if (s_id < 1 || e_id < 1 || s_id > e_id) { +#ifdef DEBUG printf("error with the counter ids\n"); +#endif return NULL; } if (e_id > pctx->curr_id) { +#ifdef DEBUG printf("end id is greater than the max id for this tv\n"); +#endif return NULL; } if (pctx == NULL) { +#ifdef DEBUG printf("perfcontext is NULL\n"); +#endif return NULL; } @@ -423,9 +663,15 @@ PerfCounterArray * PerfGetCounterArrayRange(u_int32_t s_id, u_int32_t e_id, } memset(pca->head, 0, sizeof(PCAElem) * (e_id - s_id + 2)); + pc = pctx->head; + while (pc->id != s_id) + pc = pc->next; + i = 1; - while (s_id <= e_id) { - pca->head[i].id = s_id++; + while ((pc != NULL) && (pc->id <= e_id)) { + pca->head[i].pc = pc; + pca->head[i].id = pc->id; + pc = pc->next; i++; } pca->size = i - 1; @@ -434,7 +680,8 @@ PerfCounterArray * PerfGetCounterArrayRange(u_int32_t s_id, u_int32_t e_id, } /** - * \brief Returns a counter array for all counters registered for this tm instance + * \brief Returns a counter array for all counters registered for this tm + * instance * * \param pctx Pointer to the tv's PerfContext * @@ -463,18 +710,25 @@ int PerfUpdateCounter(char *cname, char *tm_name, u_int32_t id, void *value, PerfCounter *pc = NULL; if (pctx == NULL) { +#ifdef DEBUG printf("pctx null inside PerfUpdateCounter\n"); +#endif return 0; } if ((cname == NULL || tm_name == NULL) && (id > pctx->curr_id || id < 1)) { +#ifdef DEBUG printf("id supplied doesn't exist. Please supply cname and " "tm_name instead\n"); +#endif return 0; } if (value == NULL) { - printf("Pointer to counter(value) supplied to PerfUpdateCounter is NULL\n"); +#ifdef DEBUG + printf("Pointer to counter(value) supplied to PerfUpdateCounter is " + "NULL\n"); +#endif return 0; } @@ -492,13 +746,59 @@ int PerfUpdateCounter(char *cname, char *tm_name, u_int32_t id, void *value, } if (pc == NULL) { +#ifdef DEBUG printf("this counter isn't registered in this tm\n"); +#endif return 0; } return 1; } +static void PerfCopyCounterValue(PCAElem *pcae, int reset_lc) +{ + PerfCounter *pc = NULL; + double d_temp = 0; + u_int64_t ui64_temp = 0; + int i = 0; + + pc = pcae->pc; + switch (pc->value->type) { + case TYPE_UINT64: + ui64_temp = pcae->ui64_cnt; + if (pc->type_q & TYPE_Q_AVERAGE) { + for (i = 0; i < pcae->wrapped_syncs; i++) + ui64_temp /= ULONG_MAX; + + if (pcae->syncs != 0) + ui64_temp /= pcae->syncs; + } + memcpy(pc->value->cvalue, &ui64_temp, pc->value->size); + + if (reset_lc) + pcae->ui64_cnt = 0; + + break; + case TYPE_DOUBLE: + d_temp = pcae->d_cnt; + if (pc->type_q & TYPE_Q_AVERAGE) { + for (i = 0; i < pcae->wrapped_syncs; i++) + d_temp /= ULONG_MAX; + + if (pcae->syncs != 0) + d_temp /= pcae->syncs; + } + memcpy(pc->value->cvalue, &d_temp, pc->value->size); + + if (reset_lc) + pcae->d_cnt = 0; + + break; + } + + return; +} + /** * \brief Syncs the counter array with the global counter variables * @@ -506,14 +806,17 @@ int PerfUpdateCounter(char *cname, char *tm_name, u_int32_t id, void *value, * \param pctx Pointer the the tv's PerfContext * \param reset_lc Indicates whether the local counter has to be reset or not */ -int PerfUpdateCounterArray(PerfCounterArray *pca, PerfContext *pctx, int reset_lc) +int PerfUpdateCounterArray(PerfCounterArray *pca, PerfContext *pctx, + int reset_lc) { PerfCounter *pc = NULL; PCAElem *pcae = NULL; u_int32_t i = 0; if (pca == NULL || pctx == NULL) { +#ifdef DEBUG printf("pca or pctx is NULL inside PerfUpdateCounterArray\n"); +#endif return -1; } @@ -528,13 +831,10 @@ int PerfUpdateCounterArray(PerfCounterArray *pca, PerfContext *pctx, int reset_l continue; } - memcpy(pc->value->cvalue, &(pcae[i].cnt), pc->value->size); + PerfCopyCounterValue(&pcae[i], reset_lc); pc->updated++; - if (reset_lc) - pcae[i].cnt = 0; - pc = pc->next; break; } @@ -580,7 +880,11 @@ int PerfOutputCounterFileIface() PerfCounter **pc_heads; u_int64_t *ui64_cvalue = NULL; - u_int64_t result = 0; + u_int64_t ui64_result = 0; + + double *double_cvalue = NULL; + double double_result = 0; + struct timeval tval; struct tm *tms; @@ -589,7 +893,9 @@ int PerfOutputCounterFileIface() int flag; if (perf_op_ctx->fp == NULL) { +#ifdef DEBUG printf("perf_op_ctx->fp is NULL"); +#endif return 0; } @@ -601,7 +907,8 @@ int PerfOutputCounterFileIface() fprintf(perf_op_ctx->fp, "-------------------------------------------------" "------------------\n"); fprintf(perf_op_ctx->fp, "%d/%d/%04d -- %02d:%02d:%02d\n", tms->tm_mday, - tms->tm_mon, tms->tm_year + 1900, tms->tm_hour, tms->tm_min, tms->tm_sec); + tms->tm_mon, tms->tm_year + 1900, tms->tm_hour, tms->tm_min, + tms->tm_sec); fprintf(perf_op_ctx->fp, "-------------------------------------------------" "------------------\n"); fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-s\n", "Counter", "TM Name", @@ -618,11 +925,18 @@ int PerfOutputCounterFileIface() pc = tv->pctx.head; while (pc != NULL) { + if (pc->disp == 0) { + pc = pc->next; + continue; + } + ui64_cvalue = (u_int64_t *)pc->value->cvalue; fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-llu\n", pc->name->cname, pc->name->tm_name, *ui64_cvalue); - //printf("%-10d %-10d %-10s %-llu\n", pc->name->tid, pc->id, - // pc->name->cname, *ui64_cvalue); +#ifdef DEBUG + printf("%-10d %-10d %-10s %-llu\n", pc->name->tid, pc->id, + pc->name->cname, *ui64_cvalue); +#endif pc = pc->next; } @@ -637,7 +951,7 @@ int PerfOutputCounterFileIface() pctmi = perf_op_ctx->pctmi; while (pctmi != NULL) { - if ( (pc_heads = malloc(pctmi->size * sizeof(PerfCounter **))) == NULL) { + if ( (pc_heads = malloc(pctmi->size * sizeof(PerfCounter *))) == NULL) { printf("error allocating memory\n"); exit(0); } @@ -654,11 +968,20 @@ int PerfOutputCounterFileIface() flag = 1; while(flag) { - result = 0; + ui64_result = 0; + double_result = 0; pc = pc_heads[0]; for (i = 0; i < pctmi->size; i++) { - ui64_cvalue = pc_heads[i]->value->cvalue; - result += *ui64_cvalue; + switch (pc->value->type) { + case TYPE_UINT64: + ui64_cvalue = pc_heads[i]->value->cvalue; + ui64_result += *ui64_cvalue; + break; + case TYPE_DOUBLE: + double_cvalue = pc_heads[i]->value->cvalue; + double_result += *double_cvalue; + break; + } pc_heads[i] = pc_heads[i]->next; @@ -666,11 +989,24 @@ int PerfOutputCounterFileIface() strcmp(pctmi->tm_name, pc_heads[0]->name->tm_name)) flag = 0; } - fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-llu\n", - pc->name->cname, pctmi->tm_name, result); - //printf("%-25s | %-25s | %-llu\n", pc->name->cname, - // pctmi->tm_name, result); + if (pc->disp == 0) + continue; + + switch (pc->value->type) { + case TYPE_UINT64: + fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-llu\n", + pc->name->cname, pctmi->tm_name, ui64_result); + break; + case TYPE_DOUBLE: + fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-lf\n", + pc->name->cname, pctmi->tm_name, double_result); + break; + } +#ifdef DEBUG + printf("%-25s | %-25s | %-llu\n", pc->name->cname, + pctmi->tm_name, result); +#endif } for (i = 0; i < pctmi->size; i++) pthread_mutex_unlock(&pctmi->head[i]->m); @@ -770,7 +1106,7 @@ static int PerfTestCounterReg01() memset(&pctx, 0, sizeof(PerfContext)); - return PerfRegisterCounter("t1", "c1", 5, NULL, &pctx); + return PerfRegisterCounter("t1", "c1", 5, NULL, &pctx, TYPE_Q_NONE, 1); } static int PerfTestCounterReg02() @@ -779,7 +1115,8 @@ static int PerfTestCounterReg02() memset(&pctx, 0, sizeof(PerfContext)); - return PerfRegisterCounter(NULL, NULL, TYPE_UINT64, NULL, &pctx); + return PerfRegisterCounter(NULL, NULL, TYPE_UINT64, NULL, &pctx, + TYPE_Q_NONE, 1); } static int PerfTestCounterReg03() @@ -789,7 +1126,8 @@ static int PerfTestCounterReg03() memset(&pctx, 0, sizeof(PerfContext)); - result = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx); + result = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx, + TYPE_Q_NONE, 1); PerfReleasePerfCounterS(pctx.head); @@ -803,11 +1141,12 @@ static int PerfTestCounterReg04() memset(&pctx, 0, sizeof(PerfContext)); - PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx); - PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &pctx); - PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &pctx); + PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &pctx, TYPE_Q_NONE, 1); - result = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx); + result = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx, + TYPE_Q_NONE, 1); PerfReleasePerfCounterS(pctx.head); @@ -821,7 +1160,8 @@ static int PerfTestGetCntArray05() memset(&tv, 0, sizeof(ThreadVars)); - id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx); + id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); tv.pca = PerfGetAllCountersArray(NULL); @@ -836,7 +1176,8 @@ static int PerfTestGetCntArray06() memset(&tv, 0, sizeof(ThreadVars)); - id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx); + id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); tv.pca = PerfGetAllCountersArray(&tv.pctx); @@ -858,8 +1199,10 @@ static int PerfTestCntArraySize07() pca = (PerfCounterArray *)&tv.pca; - PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx); - PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx); + PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx, TYPE_Q_NONE, + 1); + PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx, TYPE_Q_NONE, + 1); pca = PerfGetAllCountersArray(&tv.pctx); @@ -883,14 +1226,15 @@ static int PerfTestUpdateCounter08() memset(&tv, 0, sizeof(ThreadVars)); - id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx); + id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); pca = PerfGetAllCountersArray(&tv.pctx); PerfCounterIncr(id, pca); - PerfCounterAdd(id, pca, 100); + PerfCounterAddUI64(id, pca, 100); - result = pca->head[id].cnt; + result = pca->head[id].ui64_cnt; PerfReleasePerfCounterS(tv.pctx.head); PerfReleasePCA(pca); @@ -907,18 +1251,23 @@ static int PerfTestUpdateCounter09() memset(&tv, 0, sizeof(ThreadVars)); - id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx); - PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx); - PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx); - PerfRegisterCounter("t4", "c4", TYPE_UINT64, NULL, &tv.pctx); - id2 = PerfRegisterCounter("t5", "c5", TYPE_UINT64, NULL, &tv.pctx); + id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + PerfRegisterCounter("t4", "c4", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + id2 = PerfRegisterCounter("t5", "c5", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); pca = PerfGetAllCountersArray(&tv.pctx); PerfCounterIncr(id2, pca); - PerfCounterAdd(id2, pca, 100); + PerfCounterAddUI64(id2, pca, 100); - result = (pca->head[id1].cnt == 0) && (pca->head[id2].cnt == 101); + result = (pca->head[id1].ui64_cnt == 0) && (pca->head[id2].ui64_cnt == 101); PerfReleasePerfCounterS(tv.pctx.head); PerfReleasePCA(pca); @@ -937,15 +1286,18 @@ static int PerfTestUpdateGlobalCounter10() memset(&tv, 0, sizeof(ThreadVars)); - id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx); - id2 = PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx); - id3 = PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx); + id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + id2 = PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + id3 = PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); pca = PerfGetAllCountersArray(&tv.pctx); PerfCounterIncr(id1, pca); - PerfCounterAdd(id2, pca, 100); + PerfCounterAddUI64(id2, pca, 100); PerfCounterIncr(id3, pca); - PerfCounterAdd(id3, pca, 100); + PerfCounterAddUI64(id3, pca, 100); PerfUpdateCounterArray(pca, &tv.pctx, 0); @@ -975,17 +1327,21 @@ static int PerfTestCounterValues11() memset(&tv, 0, sizeof(ThreadVars)); - id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx); - id2 = PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx); - id3 = PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx); - id4 = PerfRegisterCounter("t4", "c4", TYPE_UINT64, NULL, &tv.pctx); + id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + id2 = PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + id3 = PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); + id4 = PerfRegisterCounter("t4", "c4", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_NONE, 1); pca = PerfGetAllCountersArray(&tv.pctx); PerfCounterIncr(id1, pca); - PerfCounterAdd(id2, pca, 256); - PerfCounterAdd(id3, pca, 257); - PerfCounterAdd(id4, pca, 16843024); + PerfCounterAddUI64(id2, pca, 256); + PerfCounterAddUI64(id3, pca, 257); + PerfCounterAddUI64(id4, pca, 16843024); PerfUpdateCounterArray(pca, &tv.pctx, 0); @@ -1019,6 +1375,101 @@ static int PerfTestCounterValues11() return result; } +static int PerfTestAverageQual12() +{ + ThreadVars tv; + PerfCounterArray *pca = NULL; + u_int64_t *ui64_temp = NULL; + double *d_temp = NULL; + + int result = 1; + int id1, id2; + + memset(&tv, 0, sizeof(ThreadVars)); + + id1 = PerfRegisterCounter("t1", "c1", TYPE_DOUBLE, NULL, &tv.pctx, + TYPE_Q_AVERAGE, 1); + id2 = PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx, + TYPE_Q_AVERAGE, 1); + + pca = PerfGetAllCountersArray(&tv.pctx); + + PerfCounterAddDouble(id1, pca, 1); + PerfCounterAddDouble(id1, pca, 2); + PerfCounterAddDouble(id1, pca, 3); + PerfCounterAddDouble(id1, pca, 4); + PerfCounterAddDouble(id1, pca, 5); + PerfCounterAddDouble(id1, pca, 6); + + PerfUpdateCounterArray(pca, &tv.pctx, 0); + + result &= (21 == pca->head[1].d_cnt); + result &= (6 == pca->head[1].syncs); + result &= (0 == pca->head[1].wrapped_syncs); + d_temp = tv.pctx.head->value->cvalue; + result &= (3.5 == *d_temp); + + PerfCounterAddUI64(id2, pca, 1.635); + PerfCounterAddUI64(id2, pca, 2.12); + PerfCounterAddUI64(id2, pca, 3.74); + PerfCounterAddUI64(id2, pca, 4.23); + PerfCounterAddUI64(id2, pca, 5.76); + PerfCounterAddDouble(id2, pca, 6.99999); + + PerfUpdateCounterArray(pca, &tv.pctx, 0); + + result &= (21 == pca->head[2].ui64_cnt); + result &= (6 == pca->head[2].syncs); + result &= (0 == pca->head[2].wrapped_syncs); + ui64_temp = tv.pctx.head->next->value->cvalue; + result &= (3 == *ui64_temp); + + return result; +} + +static int PerfTestMaxQual13() +{ + ThreadVars tv; + PerfCounterArray *pca = NULL; + double *p; + + int result = 1; + int id1; + + memset(&tv, 0, sizeof(ThreadVars)); + + id1 = PerfRegisterCounter("t1", "c1", TYPE_DOUBLE, NULL, &tv.pctx, + TYPE_Q_MAXIMUM, 1); + + p = tv.pctx.head->value->cvalue; + + pca = PerfGetAllCountersArray(&tv.pctx); + + PerfCounterSetDouble(id1, pca, 1.352); + PerfCounterSetDouble(id1, pca, 5.12412); + PerfCounterSetDouble(id1, pca, 4.1234); + PerfCounterSetDouble(id1, pca, 5.13562); + PerfCounterSetDouble(id1, pca, 1.2342); + + PerfUpdateCounterArray(pca, &tv.pctx, 0); + result &= (5.13562 == *p); + + PerfCounterSetDouble(id1, pca, 8); + PerfCounterSetDouble(id1, pca, 7); + + PerfUpdateCounterArray(pca, &tv.pctx, 0); + result &= (8 == *p); + + PerfCounterSetDouble(id1, pca, 6); + PerfCounterSetUI64(id1, pca, 10); + PerfCounterSetDouble(id1, pca, 9.562); + + PerfUpdateCounterArray(pca, &tv.pctx, 0); + result &= (10 == *p); + + return result; +} + void PerfRegisterTests() { UtRegisterTest("PerfTestCounterReg01", PerfTestCounterReg01, 0); @@ -1033,6 +1484,8 @@ void PerfRegisterTests() UtRegisterTest("PerfTestUpdateGlobalCounter10", PerfTestUpdateGlobalCounter10, 1); UtRegisterTest("PerfTestCounterValues11", PerfTestCounterValues11, 1); + UtRegisterTest("PerfTestAverageQual12", PerfTestAverageQual12, 1); + UtRegisterTest("PerfTestMaxQual13", PerfTestMaxQual13, 1); return; } diff --git a/src/counters.h b/src/counters.h index acaa8c9d1f..26a7e2cf31 100644 --- a/src/counters.h +++ b/src/counters.h @@ -11,32 +11,7 @@ /* Time interval at which the mgmt thread o/p the stats */ #define MGMTT_TTS 8 -/* These 2 macros can only be used when all the registered counters for the tm, - * are in the counter array */ -#define PerfCounterIncr(id, pca) do { \ - if (!pca) { \ - printf("counterarray is NULL\n"); \ - break; \ - } \ - if ((id < 1) || (id > pca->size)) { \ - printf("counter doesn't exist\n"); \ - break; \ - } \ - pca->head[id].cnt++; \ - } while(0) - -#define PerfCounterAdd(id, pca, x) do { \ - if (!pca) { \ - printf("counterarray is NULL\n"); \ - break; \ - } \ - if ((id < 1) || (id > pca->size)) { \ - printf("counter doesn't exist\n"); \ - break; \ - } \ - pca->head[id].cnt += x; \ - } while(0) - +/* Type of counter */ enum { TYPE_UINT64, TYPE_DOUBLE, @@ -44,6 +19,16 @@ enum { TYPE_MAX, }; +/* Qualifier for the counter */ +enum { + TYPE_Q_NONE = 0x01, + TYPE_Q_AVERAGE = 0x02, + TYPE_Q_MAXIMUM = 0x04, + TYPE_Q_TIMEBASED = 0x08, + TYPE_Q_MAX = 0x10, +}; + +/* Output interfaces */ enum { IFACE_FILE, IFACE_CONSOLE, @@ -54,7 +39,7 @@ enum { typedef struct _PerfCounterName { char *cname; char *tm_name; - int tid; + pthread_t tid; } PerfCounterName; typedef struct _PerfCounterValue { @@ -69,13 +54,19 @@ typedef struct _PerfCounter { PerfCounterValue *value; /* local id for this counter in this tm*/ - pthread_t id; + u_int64_t id; char *desc; /* no of times the local counter has been synced with this counter */ u_int64_t updated; + /* flag that indicates if this counter should be displayed or not */ + int disp; + + /* counter qualifier */ + int type_q; + /* the next perfcounter for this tv's tm instance */ struct _PerfCounter *next; } PerfCounter; @@ -94,8 +85,18 @@ typedef struct _PerfContext { /* PerfCounterArray(PCA) Node*/ typedef struct _PCAElem { - u_int32_t id; - u_int64_t cnt; + PerfCounter *pc; + u_int64_t id; + union { + u_int64_t ui64_cnt; + double d_cnt; + }; + + /* no of times the local counter has been updated */ + u_int64_t syncs; + + /* indicates the times syncs has overflowed */ + u_int64_t wrapped_syncs; } PCAElem; /* The PerfCounterArray */ @@ -142,7 +143,18 @@ void * PerfMgmtThread(void *); void * PerfWakeupThread(void *); -u_int32_t PerfRegisterCounter(char *, char *, int, char *, PerfContext *); +u_int32_t PerfRegisterCounter(char *, char *, int, char *, PerfContext *, int, + int); + +inline void PerfCounterIncr(u_int64_t, PerfCounterArray *); + +inline void PerfCounterAddUI64(u_int64_t, PerfCounterArray *, u_int64_t); + +inline void PerfCounterAddDouble(u_int64_t, PerfCounterArray *, double); + +inline void PerfCounterSetUI64(u_int64_t, PerfCounterArray *, u_int64_t); + +inline void PerfCounterSetDouble(u_int64_t, PerfCounterArray *, double); void PerfAddToClubbedTMTable(char *, PerfContext *); diff --git a/src/detect.c b/src/detect.c index b8559a9f0c..8455fe268a 100644 --- a/src/detect.c +++ b/src/detect.c @@ -578,10 +578,21 @@ int Detect(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) { if (r >= 0) { return 0; } + + // PerfCounterIncr(COUNTER_DETECT_ALERTS, t->pca); + return 1; } -int DetectThreadInit(ThreadVars *t, void *initdata, void **data) { +int DetectThreadInit(ThreadVars *t, void *initdata, void **data) +{ + PerfRegisterCounter("detect.alert", "Detect", TYPE_UINT64, "NULL", + &t->pctx, TYPE_Q_NONE, 1); + + t->pca = PerfGetAllCountersArray(&t->pctx); + + PerfAddToClubbedTMTable("Detect", &t->pctx); + return PatternMatcherThreadInit(t,initdata,data); } diff --git a/src/detect.h b/src/detect.h index 35d43904ba..8dbf159b34 100644 --- a/src/detect.h +++ b/src/detect.h @@ -6,6 +6,8 @@ #include "util-hash.h" #include "util-hashlist.h" +#define COUNTER_DETECT_ALERTS 1 + /* * DETECT ADDRESS */ diff --git a/src/source-nfq.c b/src/source-nfq.c index 57dd10d98f..ad43832249 100644 --- a/src/source-nfq.c +++ b/src/source-nfq.c @@ -108,7 +108,7 @@ void TmModuleVerdictNFQRegister (void) { void TmModuleDecodeNFQRegister (void) { tmm_modules[TMM_DECODENFQ].name = "DecodeNFQ"; - tmm_modules[TMM_DECODENFQ].Init = NULL; + tmm_modules[TMM_DECODENFQ].Init = DecodeNFQThreadInit; tmm_modules[TMM_DECODENFQ].Func = DecodeNFQ; tmm_modules[TMM_DECODENFQ].ExitPrintStats = NULL; tmm_modules[TMM_DECODENFQ].Deinit = NULL; @@ -460,6 +460,9 @@ int DecodeNFQ(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) printf("DecodeNFQ\n"); #endif + PerfCounterIncr(COUNTER_DECODER_PKTS, t->pca); + PerfCounterAddUI64(COUNTER_DECODER_BYTES, t->pca, p->pktlen); + if (IPV4_GET_RAW_VER(ip4h) == 4) { printf("DecodeNFQ ip4\n"); DecodeIPV4(t, p, p->pkt, p->pktlen, pq); @@ -473,5 +476,37 @@ int DecodeNFQ(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) return 0; } +int DecodeNFQThreadInit(ThreadVars *tv, void *initdata, void **data) +{ + PerfRegisterCounter("decoder.pkts", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.bytes", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.ipv4", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.ipv6", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.ethernet", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.sll", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.tcp", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.udp", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.icmpv4", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.icmpv6", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + PerfRegisterCounter("decoder.ppp", "DecodeNFQ", TYPE_UINT64, "NULL", + &tv->pctx, TYPE_Q_NONE, 1); + + tv->pca = PerfGetAllCountersArray(&tv->pctx); + + PerfAddToClubbedTMTable("DecodeNFQ", &tv->pctx); + + return 0; +} + #endif /* NFQ */ diff --git a/src/source-pcap-file.c b/src/source-pcap-file.c index c9d1e16c3f..af31d981c8 100644 --- a/src/source-pcap-file.c +++ b/src/source-pcap-file.c @@ -166,7 +166,7 @@ int ReceivePcapFileThreadDeinit(ThreadVars *tv, void *data) { int DecodePcapFile(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) { PerfCounterIncr(COUNTER_DECODER_PKTS, t->pca); - PerfCounterAdd(COUNTER_DECODER_BYTES, t->pca, p->pktlen); + PerfCounterAddUI64(COUNTER_DECODER_BYTES, t->pca, p->pktlen); /* call the decoder */ pcap_g.Decoder(t,p,p->pkt,p->pktlen,pq); @@ -176,27 +176,27 @@ int DecodePcapFile(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) int DecodePcapFileThreadInit(ThreadVars *tv, void *initdata, void **data) { PerfRegisterCounter("decoder.pkts", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.bytes", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ipv4", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ipv6", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ethernet", "DecodePcapFile", TYPE_UINT64, - "NULL", &tv->pctx); + "NULL", &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.sll", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.tcp", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.udp", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.icmpv4", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.icmpv6", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ppp", "DecodePcapFile", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); tv->pca = PerfGetAllCountersArray(&tv->pctx); diff --git a/src/source-pcap.c b/src/source-pcap.c index e8c027cec7..32b7f8be7f 100644 --- a/src/source-pcap.c +++ b/src/source-pcap.c @@ -286,7 +286,7 @@ int ReceivePcapThreadDeinit(ThreadVars *tv, void *data) { int DecodePcap(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) { PerfCounterIncr(COUNTER_DECODER_PKTS, t->pca); - PerfCounterAdd(COUNTER_DECODER_BYTES, t->pca, p->pktlen); + PerfCounterAddUI64(COUNTER_DECODER_BYTES, t->pca, p->pktlen); /* call the decoder */ switch(p->pcap_v.datalink) { @@ -310,27 +310,27 @@ int DecodePcap(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) int DecodePcapThreadInit(ThreadVars *tv, void *initdata, void **data) { PerfRegisterCounter("decoder.pkts", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.bytes", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ipv4", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ipv6", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ethernet", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.sll", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.tcp", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.udp", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.icmpv4", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.icmpv6", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); PerfRegisterCounter("decoder.ppp", "DecodePcap", TYPE_UINT64, "NULL", - &tv->pctx); + &tv->pctx, TYPE_Q_NONE, 1); tv->pca = PerfGetAllCountersArray(&tv->pctx); diff --git a/src/stream-tcp.c b/src/stream-tcp.c index 1f11af1ee0..0f6a5728cb 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -31,6 +31,7 @@ #include "stream-tcp-reassemble.h" #include "stream.h" +#include "stream-tcp.h" int StreamTcp (ThreadVars *, Packet *, void *, PacketQueue *); int StreamTcpThreadInit(ThreadVars *, void *, void **); @@ -980,6 +981,8 @@ int StreamTcp (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) { StreamTcpThread *stt = (StreamTcpThread *)data; + //PerfCounterAddUI64(COUNTER_STREAMTCP_STREAMS, tv->pca, a); + if (!(PKT_IS_TCP(p))) return 0; @@ -1017,6 +1020,14 @@ int StreamTcpThreadInit(ThreadVars *t, void *initdata, void **data) /* XXX */ *data = (void *)stt; + + PerfRegisterCounter("streamTcp.tcp_streams", "StreamTcp", TYPE_DOUBLE, "NULL", + &t->pctx, TYPE_Q_AVERAGE, 1); + + t->pca = PerfGetAllCountersArray(&t->pctx); + + PerfAddToClubbedTMTable("StreamTcp", &t->pctx); + return 0; } diff --git a/src/stream-tcp.h b/src/stream-tcp.h index b8c7792fc2..7cb081d904 100644 --- a/src/stream-tcp.h +++ b/src/stream-tcp.h @@ -3,6 +3,8 @@ #ifndef __STREAM_TCP_H__ #define __STREAM_TCP_H__ +#define COUNTER_STREAMTCP_STREAMS 1 + void TmModuleStreamTcpRegister (void); #endif /* __STREAM_TCP_H__ */