perf_task_bugs_fixex_v1

remotes/origin/master-1.0.x
Anoop Saldanha 16 years ago committed by Victor Julien
parent a5fb240a4a
commit fc8bb6c934

@ -1,15 +1,19 @@
#include "stdio.h" #include <stdio.h>
#include "stdlib.h" #include <stdlib.h>
#include "string.h" #include <string.h>
#include "pthread.h" #include <pthread.h>
#include <sys/time.h>
#include "time.h"
#include "counters.h" #include "counters.h"
#include "eidps.h" #include "eidps.h"
#include "threadvars.h" #include "threadvars.h"
#include "tm-modules.h" #include "tm-modules.h"
#include "tm-threads.h" #include "tm-threads.h"
#include "util-unittest.h" #include "util-unittest.h"
#include <sys/time.h>
#include "time.h" /** \todo config api */
#define LOGPATH "/var/log/eidps/stats.log"
static PerfThreadContext *perf_tc = NULL; static PerfThreadContext *perf_tc = NULL;
static PerfOPIfaceContext *perf_op_ctx = NULL; static PerfOPIfaceContext *perf_op_ctx = NULL;
@ -38,21 +42,24 @@ void PerfInitOPCtx()
perf_op_ctx->iface = IFACE_FILE; perf_op_ctx->iface = IFACE_FILE;
if ( (perf_op_ctx->file = strdup("/root/log.txt")) == NULL) { if ( (perf_op_ctx->file = strdup(LOGPATH)) == NULL) {
printf("error allocating memory\n"); printf("error allocating memory\n");
exit(0); exit(0);
} }
if ( (perf_op_ctx->fp = fopen(perf_op_ctx->file, "w+")) == NULL) { if ( (perf_op_ctx->fp = fopen(perf_op_ctx->file, "w+")) == NULL) {
printf("fopen error opening file /root/log.txt\n"); printf("fopen error opening file %s\n", perf_op_ctx->file);
exit(0); exit(0);
} }
// club the counter from multiple instances of the tm before o/p /* club the counter from multiple instances of the tm before o/p */
perf_op_ctx->club_tm = 1; perf_op_ctx->club_tm = 1;
// init the lock used by PerfClubTMInst /* init the lock used by PerfClubTMInst */
pthread_mutex_init(&perf_op_ctx->pctmi_lock, NULL); if (pthread_mutex_init(&perf_op_ctx->pctmi_lock, NULL) != 0) {
printf("error initializing the pctmi mutex\n");
exit(0);
}
return; return;
} }
@ -75,12 +82,27 @@ void PerfSpawnThreads()
perf_tc->flags = PT_RUN; perf_tc->flags = PT_RUN;
if (pthread_create(&perf_tc->wakeup_t, &attr, PerfWakeupThread, NULL)) { if (pthread_mutex_init(&perf_tc->wakeup_m, NULL) != 0) {
printf("Error initializing the perf_tc->wakeup_m mutex\n");
exit(0);
}
if (pthread_mutex_init(&perf_tc->mgmt_m, NULL) != 0) {
printf("Error initializing the perf_tc->mgmt_m mutex\n");
exit(0);
}
if (pthread_cond_init(&perf_tc->tc_cond, NULL) != 0) {
printf("Error initializing the perf_tc->tc_cond condition variable\n");
exit(0);
}
if (pthread_create(&perf_tc->wakeup_t, &attr, PerfWakeupThread, NULL) != 0) {
printf("Error creating PerfWakeupFunc thread\n"); printf("Error creating PerfWakeupFunc thread\n");
exit(0); exit(0);
} }
if (pthread_create(&perf_tc->mgmt_t, &attr, PerfMgmtThread, NULL)) { if (pthread_create(&perf_tc->mgmt_t, &attr, PerfMgmtThread, NULL) != 0) {
printf("Error creating PerfWakeupFunc thread\n"); printf("Error creating PerfWakeupFunc thread\n");
exit(0); exit(0);
} }
@ -95,9 +117,26 @@ void PerfDestroyThreads()
{ {
perf_tc->flags |= PT_KILL; perf_tc->flags |= PT_KILL;
/* prematurely wakeup, the mgmt and wakeup threads */
pthread_cond_broadcast(&perf_tc->tc_cond);
pthread_join(perf_tc->wakeup_t, NULL); pthread_join(perf_tc->wakeup_t, NULL);
pthread_join(perf_tc->mgmt_t, NULL); pthread_join(perf_tc->mgmt_t, NULL);
if (pthread_mutex_destroy(&perf_tc->wakeup_m) != 0) {
printf("Error destroying the mutex perf_tc->wakeup_m\n");
}
if (pthread_mutex_destroy(&perf_tc->mgmt_m) != 0) {
printf("Error destroying the mutex perf_tc->mgmt_m\n");
}
if (pthread_cond_destroy(&perf_tc->tc_cond) != 0) {
printf("Error destroying the condition variable perf_tc->tc_cond\n");
}
if (perf_tc != NULL) free(perf_tc);
return; return;
} }
@ -111,6 +150,7 @@ void PerfDestroyThreads()
void * PerfMgmtThread(void *arg) void * PerfMgmtThread(void *arg)
{ {
u_int8_t run = 1; u_int8_t run = 1;
struct timespec cond_time;
if (perf_op_ctx == NULL) { if (perf_op_ctx == NULL) {
printf("error: PerfInitCounterApi() has to be called first\n"); printf("error: PerfInitCounterApi() has to be called first\n");
@ -118,7 +158,15 @@ void * PerfMgmtThread(void *arg)
} }
while (run) { while (run) {
sleep(MGMTT_TTS); cond_time.tv_sec = time(NULL) + MGMTT_TTS;
cond_time.tv_nsec = 0;
pthread_mutex_lock(&perf_tc->mgmt_m);
pthread_cond_timedwait(&perf_tc->tc_cond, &perf_tc->mgmt_m,
&cond_time);
pthread_mutex_unlock(&perf_tc->mgmt_m);
// sleep(MGMTT_TTS);
PerfOutputCounters(); PerfOutputCounters();
@ -138,24 +186,33 @@ void * PerfMgmtThread(void *arg)
void * PerfWakeupThread(void *arg) void * PerfWakeupThread(void *arg)
{ {
u_int8_t run = 1; u_int8_t run = 1;
ThreadVars *tv; ThreadVars *tv = NULL;
PacketQueue *q; PacketQueue *q = NULL;
struct timespec cond_time;
while (run) { while (run) {
sleep(WUT_TTS); cond_time.tv_sec = time(NULL) + WUT_TTS;
cond_time.tv_nsec = 0;
pthread_mutex_lock(&perf_tc->wakeup_m);
pthread_cond_timedwait(&perf_tc->tc_cond, &perf_tc->wakeup_m,
&cond_time);
pthread_mutex_unlock(&perf_tc->wakeup_m);
// sleep(WUT_TTS);
tv = tv_root; tv = tv_root;
while (tv) { while (tv != NULL) {
if (!tv->inq || !tv->pctx.head) { if (tv->inq == NULL || tv->pctx.head == NULL) {
tv = tv->next; tv = tv->next;
continue; continue;
} }
q = &trans_q[tv->inq->id]; q = &trans_q[tv->inq->id];
// assuming the assignment of an int to be atomic, and even if it's /* assuming the assignment of an int to be atomic, and even if it's
// not, it should be okay not, it should be okay */
tv->pctx.perf_flag = 1; tv->pctx.perf_flag = 1;
pthread_cond_signal(&q->cond_q); pthread_cond_signal(&q->cond_q);
@ -181,36 +238,37 @@ void * PerfWakeupThread(void *arg)
* *
* @returns the counter id * @returns the counter id
*/ */
u_int32_t PerfRegisterCounter(char *cname, char *tm_name, pthread_t tid, int type, u_int32_t PerfRegisterCounter(char *cname, char *tm_name, int type,
char *desc, PerfContext *pctx) char *desc, PerfContext *pctx)
{ {
PerfCounter **head = &pctx->head; PerfCounter **head = &pctx->head;
PerfCounter *temp, *prev; PerfCounter *temp = NULL;
PerfCounter *pc; PerfCounter *prev = NULL;
PerfCounter *pc = NULL;
if (cname == NULL || tm_name == NULL || pctx == NULL) { if (cname == NULL || tm_name == NULL || pctx == NULL) {
printf("counter name, tm name null or PerfContext NULL\n"); printf("counter name, tm name null or PerfContext NULL\n");
return 0; return 0;
} }
// (TYPE_MAX - 1) because we still haven't implemented TYPE_STR /* (TYPE_MAX - 1) because we still haven't implemented TYPE_STR */
if ((type >= (TYPE_MAX - 1)) || (type < 0)) { if ((type >= (TYPE_MAX - 1)) || (type < 0)) {
printf("Error:Counters of this type can't be registered\n"); printf("Error: Counters of type %d can't be registered\n", type);
return(0); return 0;
} }
temp = prev = *head; temp = prev = *head;
while (temp != NULL) { while (temp != NULL) {
prev = temp; prev = temp;
if (!strcmp(cname, temp->name->cname) && if (strcmp(cname, temp->name->cname) == 0 &&
!strcmp(tm_name, temp->name->tm_name)) strcmp(tm_name, temp->name->tm_name) == 0)
break; break;
temp = temp->next; temp = temp->next;
} }
// We already have a counter registered by this name /* We already have a counter registered by this name */
if (temp != NULL) if (temp != NULL)
return(temp->id); return(temp->id);
@ -243,16 +301,16 @@ u_int32_t PerfRegisterCounter(char *cname, char *tm_name, pthread_t tid, int typ
pc->name->cname = strdup(cname); pc->name->cname = strdup(cname);
pc->name->tm_name = strdup(tm_name); pc->name->tm_name = strdup(tm_name);
pc->name->tid = tid; pc->name->tid = pthread_self();
pc->value->type = type; pc->value->type = type;
switch(pc->value->type) { switch(pc->value->type) {
case TYPE_UINT64: case TYPE_UINT64:
pc->value->size = sizeof(u_int64_t); pc->value->size = sizeof(u_int64_t);
break; break;
case TYPE_DOUBLE: case TYPE_DOUBLE:
pc->value->size = sizeof(double); pc->value->size = sizeof(double);
break; break;
} }
if ( (pc->value->cvalue = malloc(pc->value->size)) == NULL) { if ( (pc->value->cvalue = malloc(pc->value->size)) == NULL) {
printf("error allocating memory\n"); printf("error allocating memory\n");
@ -260,19 +318,28 @@ u_int32_t PerfRegisterCounter(char *cname, char *tm_name, pthread_t tid, int typ
} }
memset(pc->value->cvalue, 0, pc->value->size); memset(pc->value->cvalue, 0, pc->value->size);
// assign a unique id to this PerfCounter. The id is local to this tv. /* assign a unique id to this PerfCounter. The id is local to this tv.
// please note that the ids start from 1 and not 0 please note that the ids start from 1 and not 0 */
pc->id = ++(pctx->curr_id); pc->id = ++(pctx->curr_id);
if (desc != NULL) if (desc != NULL)
pc->desc = strdup(desc); pc->desc = strdup(desc);
return(pc->id); return pc->id;
} }
/**
* Adds a TM to the clubbed TM table. Multiple instances of the same TM are
* stacked together in a PCTMI container
*
* @param tm_name is the name of the tm to be added
* @param pctx holds the PerfContext associated with the TM tm_name
*/
void PerfAddToClubbedTMTable(char *tm_name, PerfContext *pctx) void PerfAddToClubbedTMTable(char *tm_name, PerfContext *pctx)
{ {
PerfClubTMInst *pctmi, *prev, *temp; PerfClubTMInst *pctmi = NULL;
PerfClubTMInst *prev = NULL;
PerfClubTMInst *temp = NULL;
PerfContext **hpctx; PerfContext **hpctx;
int i = 0; int i = 0;
@ -283,7 +350,7 @@ void PerfAddToClubbedTMTable(char *tm_name, PerfContext *pctx)
while (pctmi != NULL) { while (pctmi != NULL) {
prev = pctmi; prev = pctmi;
if (strcmp(tm_name, pctmi->tm_name)) { if (strcmp(tm_name, pctmi->tm_name) != 0) {
pctmi = pctmi->next; pctmi = pctmi->next;
continue; continue;
} }
@ -291,7 +358,10 @@ void PerfAddToClubbedTMTable(char *tm_name, PerfContext *pctx)
} }
if (pctmi == NULL) { if (pctmi == NULL) {
temp = malloc(sizeof(PerfClubTMInst)); if ( (temp = malloc(sizeof(PerfClubTMInst))) == NULL) {
printf("error allocating memory\n");
exit(0);
}
memset(temp, 0, sizeof(PerfClubTMInst)); memset(temp, 0, sizeof(PerfClubTMInst));
temp->size++; temp->size++;
@ -349,8 +419,8 @@ void PerfAddToClubbedTMTable(char *tm_name, PerfContext *pctx)
PerfCounterArray * PerfGetCounterArrayRange(u_int32_t s_id, u_int32_t e_id, PerfCounterArray * PerfGetCounterArrayRange(u_int32_t s_id, u_int32_t e_id,
PerfContext *pctx) PerfContext *pctx)
{ {
PerfCounterArray *pca; PerfCounterArray *pca = NULL;
u_int8_t i; u_int32_t i = 0;
if (pctx == NULL) { if (pctx == NULL) {
printf("pctx is NULL\n"); printf("pctx is NULL\n");
@ -419,7 +489,7 @@ PerfCounterArray * PerfGetAllCountersArray(PerfContext *pctx)
int PerfUpdateCounter(char *cname, char *tm_name, u_int32_t id, void *value, int PerfUpdateCounter(char *cname, char *tm_name, u_int32_t id, void *value,
PerfContext *pctx) PerfContext *pctx)
{ {
PerfCounter *pc = pctx->head; PerfCounter *pc = NULL;
if (pctx == NULL) { if (pctx == NULL) {
printf("pctx null inside PerfUpdateCounter\n"); printf("pctx null inside PerfUpdateCounter\n");
@ -432,6 +502,12 @@ int PerfUpdateCounter(char *cname, char *tm_name, u_int32_t id, void *value,
return 0; return 0;
} }
if (value == NULL) {
printf("value is NULL\n");
exit(0);
}
pc = pctx->head;
while(pc != NULL) { while(pc != NULL) {
if (pc->id != id) { if (pc->id != id) {
pc = pc->next; pc = pc->next;
@ -461,15 +537,18 @@ int PerfUpdateCounter(char *cname, char *tm_name, u_int32_t id, void *value,
*/ */
int PerfUpdateCounterArray(PerfCounterArray *pca, PerfContext *pctx, int reset_lc) int PerfUpdateCounterArray(PerfCounterArray *pca, PerfContext *pctx, int reset_lc)
{ {
u_int32_t i; PerfCounter *pc = NULL;
PerfCounter *pc = pctx->head; PCAElem *pcae = NULL;
PCAElem *pcae = pca->head; u_int32_t i = 0;
if (pca == NULL || pctx == NULL) { if (pca == NULL || pctx == NULL) {
printf("pca or pctx is NULL inside PerfUpdateCounterArray\n"); printf("pca or pctx is NULL inside PerfUpdateCounterArray\n");
return -1; return -1;
} }
pc = pctx->head;
pcae = pca->head;
pthread_mutex_lock(&pctx->m); pthread_mutex_lock(&pctx->m);
for (i = 1; i <= pca->size; i++) { for (i = 1; i <= pca->size; i++) {
while (pc != NULL) { while (pc != NULL) {
@ -502,18 +581,18 @@ int PerfUpdateCounterArray(PerfCounterArray *pca, PerfContext *pctx, int reset_l
void PerfOutputCounters() void PerfOutputCounters()
{ {
switch (perf_op_ctx->iface) { switch (perf_op_ctx->iface) {
case IFACE_FILE: case IFACE_FILE:
PerfOutputCounterFileIface(); PerfOutputCounterFileIface();
break; break;
case IFACE_CONSOLE: case IFACE_CONSOLE:
// yet to be implemented // yet to be implemented
break; break;
case IFACE_NETWORK: case IFACE_NETWORK:
// yet to be implemented // yet to be implemented
break; break;
case IFACE_SYSLOG: case IFACE_SYSLOG:
// yet to be implemented // yet to be implemented
break; break;
} }
return; return;
@ -525,12 +604,12 @@ void PerfOutputCounters()
int PerfOutputCounterFileIface() int PerfOutputCounterFileIface()
{ {
ThreadVars *tv = tv_root; ThreadVars *tv = tv_root;
PerfClubTMInst *pctmi; PerfClubTMInst *pctmi = NULL;
PerfCounter *pc; PerfCounter *pc = NULL;
PerfCounter **pc_heads; PerfCounter **pc_heads;
u_int64_t *ui64_cvalue; u_int64_t *ui64_cvalue = NULL;
u_int64_t result; u_int64_t result = 0;
struct timeval tval; struct timeval tval;
struct tm *tms; struct tm *tms;
@ -560,15 +639,15 @@ int PerfOutputCounterFileIface()
"------------------\n"); "------------------\n");
if (perf_op_ctx->club_tm == 0) { if (perf_op_ctx->club_tm == 0) {
while (tv) { while (tv != NULL) {
pthread_mutex_lock(&tv->pctx.m); pthread_mutex_lock(&tv->pctx.m);
pc = tv->pctx.head; pc = tv->pctx.head;
while (pc) { while (pc != NULL) {
ui64_cvalue = (u_int64_t *)pc->value->cvalue; ui64_cvalue = (u_int64_t *)pc->value->cvalue;
fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-u\n", pc->name->cname, fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-lu\n",
pc->name->tm_name, *ui64_cvalue); pc->name->cname, pc->name->tm_name, *ui64_cvalue);
//printf("**** %-10d %-10d %-10s %-10u\n", pc->name->tid, pc->id, //printf("%-10d %-10d %-10s %-llu\n", pc->name->tid, pc->id,
// pc->name->cname, *ui64_cvalue); // pc->name->cname, *ui64_cvalue);
pc = pc->next; pc = pc->next;
} }
@ -583,7 +662,7 @@ int PerfOutputCounterFileIface()
} }
pctmi = perf_op_ctx->pctmi; pctmi = perf_op_ctx->pctmi;
while (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"); printf("error allocating memory\n");
exit(0); exit(0);
@ -613,7 +692,7 @@ int PerfOutputCounterFileIface()
strcmp(pctmi->tm_name, pc_heads[0]->name->tm_name)) strcmp(pctmi->tm_name, pc_heads[0]->name->tm_name))
flag = 0; flag = 0;
} }
fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-u\n", fprintf(perf_op_ctx->fp, "%-25s | %-25s | %-lu\n",
pc->name->cname, pctmi->tm_name, result); pc->name->cname, pctmi->tm_name, result);
//printf("%-25s | %-25s | %-u\n", pc->name->cname, //printf("%-25s | %-25s | %-u\n", pc->name->cname,
// pctmi->tm_name, result); // pctmi->tm_name, result);
@ -643,17 +722,17 @@ void PerfReleaseResources()
void PerfReleaseOPCtx() void PerfReleaseOPCtx()
{ {
if (perf_op_ctx) { if (perf_op_ctx != NULL) {
if (perf_op_ctx->fp) if (perf_op_ctx->fp != NULL)
fclose(perf_op_ctx->fp); fclose(perf_op_ctx->fp);
if (perf_op_ctx->file) if (perf_op_ctx->file != NULL)
free(perf_op_ctx->file); free(perf_op_ctx->file);
if (perf_op_ctx->pctmi) { if (perf_op_ctx->pctmi != NULL) {
if (perf_op_ctx->pctmi->tm_name) if (perf_op_ctx->pctmi->tm_name != NULL)
free(perf_op_ctx->pctmi->tm_name); free(perf_op_ctx->pctmi->tm_name);
if (perf_op_ctx->pctmi->head) if (perf_op_ctx->pctmi->head != NULL)
free(perf_op_ctx->pctmi->head); free(perf_op_ctx->pctmi->head);
free(perf_op_ctx->pctmi); free(perf_op_ctx->pctmi);
} }
@ -666,9 +745,9 @@ void PerfReleaseOPCtx()
void PerfReleasePerfCounterS(PerfCounter *head) void PerfReleasePerfCounterS(PerfCounter *head)
{ {
PerfCounter *pc; PerfCounter *pc = NULL;
while (head) { while (head != NULL) {
pc = head; pc = head;
head = head->next; head = head->next;
PerfReleaseCounter(pc); PerfReleaseCounter(pc);
@ -679,17 +758,17 @@ void PerfReleasePerfCounterS(PerfCounter *head)
void PerfReleaseCounter(PerfCounter *pc) void PerfReleaseCounter(PerfCounter *pc)
{ {
if (pc) { if (pc != NULL) {
if (pc->name) { if (pc->name != NULL) {
if (pc->name->cname) free(pc->name->cname); if (pc->name->cname != NULL) free(pc->name->cname);
if (pc->name->tm_name) free(pc->name->tm_name); if (pc->name->tm_name != NULL) free(pc->name->tm_name);
free(pc->name); free(pc->name);
} }
if (pc->value) { if (pc->value != NULL) {
if (pc->value->cvalue) free(pc->value->cvalue); if (pc->value->cvalue != NULL) free(pc->value->cvalue);
free(pc->value); free(pc->value);
} }
if (pc->desc) free(pc->desc); if (pc->desc != NULL) free(pc->desc);
free(pc); free(pc);
} }
@ -698,8 +777,8 @@ void PerfReleaseCounter(PerfCounter *pc)
void PerfReleasePCA(PerfCounterArray *pca) void PerfReleasePCA(PerfCounterArray *pca)
{ {
if (pca) { if (pca != NULL) {
if (pca->head) if (pca->head != NULL)
free(pca->head); free(pca->head);
free(pca); free(pca);
} }
@ -707,91 +786,75 @@ void PerfReleasePCA(PerfCounterArray *pca)
return; return;
} }
void PerfRegisterTests()
{
UtRegisterTest("PerfTestCounterReg01", PerfTestCounterReg01, 0);
UtRegisterTest("PerfTestCounterReg02", PerfTestCounterReg02, 0);
UtRegisterTest("PerfTestCounterReg03", PerfTestCounterReg03, 1);
UtRegisterTest("PerfTestCounterReg04", PerfTestCounterReg04, 1);
UtRegisterTest("PerfTestGetCntArray05", PerfTestGetCntArray05, 1);
UtRegisterTest("PerfTestGetCntArray06", PerfTestGetCntArray06, 1);
UtRegisterTest("PerfTestCntArraySize07", PerfTestCntArraySize07, 2);
UtRegisterTest("PerfTestUpdateCounter08", PerfTestUpdateCounter08, 101);
UtRegisterTest("PerfTestUpdateCounter09", PerfTestUpdateCounter09, 1);
UtRegisterTest("PerfTestUpdateGlobalCounter10", PerfTestUpdateGlobalCounter10, 1);
return;
}
//------------------------------------Unit_Tests------------------------------------ //------------------------------------Unit_Tests------------------------------------
int PerfTestCounterReg01() static int PerfTestCounterReg01()
{ {
PerfContext pctx; PerfContext pctx;
memset(&pctx, 0, sizeof(PerfContext)); memset(&pctx, 0, sizeof(PerfContext));
return PerfRegisterCounter("t1", "c1", 100, 5, NULL, &pctx); return PerfRegisterCounter("t1", "c1", 5, NULL, &pctx);
} }
int PerfTestCounterReg02() static int PerfTestCounterReg02()
{ {
PerfContext pctx; PerfContext pctx;
memset(&pctx, 0, sizeof(PerfContext)); memset(&pctx, 0, sizeof(PerfContext));
return PerfRegisterCounter(NULL, NULL, 100, TYPE_UINT64, NULL, &pctx); return PerfRegisterCounter(NULL, NULL, TYPE_UINT64, NULL, &pctx);
} }
int PerfTestCounterReg03() static int PerfTestCounterReg03()
{ {
PerfContext pctx; PerfContext pctx;
int result; int result;
memset(&pctx, 0, sizeof(PerfContext)); memset(&pctx, 0, sizeof(PerfContext));
result = PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &pctx); result = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx);
PerfReleasePerfCounterS(pctx.head); PerfReleasePerfCounterS(pctx.head);
return result; return result;
} }
int PerfTestCounterReg04() static int PerfTestCounterReg04()
{ {
PerfContext pctx; PerfContext pctx;
int result; int result;
memset(&pctx, 0, sizeof(PerfContext)); memset(&pctx, 0, sizeof(PerfContext));
PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &pctx); PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx);
PerfRegisterCounter("t2", "c2", 100, TYPE_UINT64, NULL, &pctx); PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &pctx);
PerfRegisterCounter("t3", "c3", 100, TYPE_UINT64, NULL, &pctx); PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &pctx);
result = PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &pctx); result = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &pctx);
PerfReleasePerfCounterS(pctx.head); PerfReleasePerfCounterS(pctx.head);
return result; return result;
} }
int PerfTestGetCntArray05() static int PerfTestGetCntArray05()
{ {
ThreadVars tv; ThreadVars tv;
int id; int id;
memset(&tv, 0, sizeof(ThreadVars)); memset(&tv, 0, sizeof(ThreadVars));
id = PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &tv.pctx); id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx);
tv.pca = PerfGetAllCountersArray(NULL); tv.pca = PerfGetAllCountersArray(NULL);
return (!tv.pca)?1:0; return (!tv.pca)?1:0;
} }
int PerfTestGetCntArray06() static int PerfTestGetCntArray06()
{ {
ThreadVars tv; ThreadVars tv;
int id; int id;
@ -799,7 +862,7 @@ int PerfTestGetCntArray06()
memset(&tv, 0, sizeof(ThreadVars)); memset(&tv, 0, sizeof(ThreadVars));
id = PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &tv.pctx); id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx);
tv.pca = PerfGetAllCountersArray(&tv.pctx); tv.pca = PerfGetAllCountersArray(&tv.pctx);
@ -811,18 +874,18 @@ int PerfTestGetCntArray06()
return result; return result;
} }
int PerfTestCntArraySize07() static int PerfTestCntArraySize07()
{ {
ThreadVars tv; ThreadVars tv;
PerfCounterArray *pca; PerfCounterArray *pca = NULL;
int result; int result;
memset(&tv, 0, sizeof(ThreadVars)); memset(&tv, 0, sizeof(ThreadVars));
pca = (PerfCounterArray *)&tv.pca; pca = (PerfCounterArray *)&tv.pca;
PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &tv.pctx); PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx);
PerfRegisterCounter("t2", "c2", 100, TYPE_UINT64, NULL, &tv.pctx); PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx);
pca = PerfGetAllCountersArray(&tv.pctx); pca = PerfGetAllCountersArray(&tv.pctx);
@ -837,16 +900,16 @@ int PerfTestCntArraySize07()
return result; return result;
} }
int PerfTestUpdateCounter08() static int PerfTestUpdateCounter08()
{ {
ThreadVars tv; ThreadVars tv;
PerfCounterArray *pca; PerfCounterArray *pca = NULL;
int id; int id;
int result; int result;
memset(&tv, 0, sizeof(ThreadVars)); memset(&tv, 0, sizeof(ThreadVars));
id = PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &tv.pctx); id = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx);
pca = PerfGetAllCountersArray(&tv.pctx); pca = PerfGetAllCountersArray(&tv.pctx);
@ -861,20 +924,20 @@ int PerfTestUpdateCounter08()
return result; return result;
} }
int PerfTestUpdateCounter09() static int PerfTestUpdateCounter09()
{ {
ThreadVars tv; ThreadVars tv;
PerfCounterArray *pca; PerfCounterArray *pca = NULL;
int id1, id2; int id1, id2;
int result; int result;
memset(&tv, 0, sizeof(ThreadVars)); memset(&tv, 0, sizeof(ThreadVars));
id1 = PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &tv.pctx); id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx);
PerfRegisterCounter("t2", "c2", 100, TYPE_UINT64, NULL, &tv.pctx); PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx);
PerfRegisterCounter("t3", "c3", 100, TYPE_UINT64, NULL, &tv.pctx); PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx);
PerfRegisterCounter("t4", "c4", 100, TYPE_UINT64, NULL, &tv.pctx); PerfRegisterCounter("t4", "c4", TYPE_UINT64, NULL, &tv.pctx);
id2 = PerfRegisterCounter("t5", "c5", 100, TYPE_UINT64, NULL, &tv.pctx); id2 = PerfRegisterCounter("t5", "c5", TYPE_UINT64, NULL, &tv.pctx);
pca = PerfGetAllCountersArray(&tv.pctx); pca = PerfGetAllCountersArray(&tv.pctx);
@ -889,21 +952,21 @@ int PerfTestUpdateCounter09()
return result; return result;
} }
int PerfTestUpdateGlobalCounter10() static int PerfTestUpdateGlobalCounter10()
{ {
ThreadVars tv; ThreadVars tv;
PerfCounterArray *pca; PerfCounterArray *pca = NULL;
int result = 1; int result = 1;
int id1, id2, id3; int id1, id2, id3;
u_int64_t *p; u_int64_t *p = NULL;
u_int64_t m; u_int64_t m;
memset(&tv, 0, sizeof(ThreadVars)); memset(&tv, 0, sizeof(ThreadVars));
id1 = PerfRegisterCounter("t1", "c1", 100, TYPE_UINT64, NULL, &tv.pctx); id1 = PerfRegisterCounter("t1", "c1", TYPE_UINT64, NULL, &tv.pctx);
id2 = PerfRegisterCounter("t2", "c2", 100, TYPE_UINT64, NULL, &tv.pctx); id2 = PerfRegisterCounter("t2", "c2", TYPE_UINT64, NULL, &tv.pctx);
id3 = PerfRegisterCounter("t3", "c3", 100, TYPE_UINT64, NULL, &tv.pctx); id3 = PerfRegisterCounter("t3", "c3", TYPE_UINT64, NULL, &tv.pctx);
pca = PerfGetAllCountersArray(&tv.pctx); pca = PerfGetAllCountersArray(&tv.pctx);
PerfCounterIncr(id1, pca); PerfCounterIncr(id1, pca);
@ -913,7 +976,6 @@ int PerfTestUpdateGlobalCounter10()
PerfUpdateCounterArray(pca, &tv.pctx, 0); PerfUpdateCounterArray(pca, &tv.pctx, 0);
printf("%d\n", result);
p = (u_int64_t *)tv.pctx.head->value->cvalue; p = (u_int64_t *)tv.pctx.head->value->cvalue;
m = *p; m = *p;
result = (m == 1); result = (m == 1);
@ -926,3 +988,20 @@ int PerfTestUpdateGlobalCounter10()
return result; return result;
} }
void PerfRegisterTests()
{
UtRegisterTest("PerfTestCounterReg01", PerfTestCounterReg01, 0);
UtRegisterTest("PerfTestCounterReg02", PerfTestCounterReg02, 0);
UtRegisterTest("PerfTestCounterReg03", PerfTestCounterReg03, 1);
UtRegisterTest("PerfTestCounterReg04", PerfTestCounterReg04, 1);
UtRegisterTest("PerfTestGetCntArray05", PerfTestGetCntArray05, 1);
UtRegisterTest("PerfTestGetCntArray06", PerfTestGetCntArray06, 1);
UtRegisterTest("PerfTestCntArraySize07", PerfTestCntArraySize07, 2);
UtRegisterTest("PerfTestUpdateCounter08", PerfTestUpdateCounter08, 101);
UtRegisterTest("PerfTestUpdateCounter09", PerfTestUpdateCounter09, 1);
UtRegisterTest("PerfTestUpdateGlobalCounter10",
PerfTestUpdateGlobalCounter10, 1);
return;
}

@ -1,13 +1,15 @@
/* Anoop Saldanha <poonaatsoc@gmail.com> */ /** Copyright (c) Open Information Security Foundation.
* \author Anoop Saldanha <poonaatsoc@gmail.com>
*/
#ifndef __COUNTERS_H__ #ifndef __COUNTERS_H__
#define __COUNTERS_H__ #define __COUNTERS_H__
// Time interval for syncing the local counters with the global ones /** Time interval for syncing the local counters with the global ones */
#define WUT_TTS 3 #define WUT_TTS 3
// Time interval at which the mgmt thread o/p the stats /** Time interval at which the mgmt thread o/p the stats */
#define MGMTT_TTS 10 #define MGMTT_TTS 8
#define PT_RUN 0x01 #define PT_RUN 0x01
#define PT_KILL 0x02 #define PT_KILL 0x02
@ -59,6 +61,12 @@ typedef struct _PerfThreadContext {
/* state of the 2 threads, determined by PT_RUN AND PT_KILL */ /* state of the 2 threads, determined by PT_RUN AND PT_KILL */
u_int32_t flags; u_int32_t flags;
/* need these mutexes just for calling pthread_cond_timewait() on tc_cond */
pthread_mutex_t wakeup_m;
pthread_mutex_t mgmt_m;
pthread_cond_t tc_cond;
} PerfThreadContext; } PerfThreadContext;
typedef struct _PerfCounterName { typedef struct _PerfCounterName {
@ -105,7 +113,7 @@ typedef struct _PerfContext {
/* PerfCounterArray(PCA) Node*/ /* PerfCounterArray(PCA) Node*/
typedef struct _PCAElem { typedef struct _PCAElem {
u_int32_t id; u_int32_t id;
u_int32_t cnt; u_int64_t cnt;
} PCAElem; } PCAElem;
/* The PerfCounterArray */ /* The PerfCounterArray */
@ -154,8 +162,7 @@ void * PerfMgmtThread(void *);
void * PerfWakeupThread(void *); void * PerfWakeupThread(void *);
u_int32_t PerfRegisterCounter(char *, char *, pthread_t, int, char *, u_int32_t PerfRegisterCounter(char *, char *, int, char *, PerfContext *);
PerfContext *);
void PerfAddToClubbedTMTable(char *, PerfContext *); void PerfAddToClubbedTMTable(char *, PerfContext *);
@ -186,24 +193,4 @@ void PerfReleasePCA(PerfCounterArray *);
void PerfRegisterTests(void); void PerfRegisterTests(void);
int PerfTestCounterReg01(void);
int PerfTestCounterReg02(void);
int PerfTestCounterReg03(void);
int PerfTestCounterReg04(void);
int PerfTestGetCntArray05(void);
int PerfTestGetCntArray06(void);
int PerfTestCntArraySize07(void);
int PerfTestUpdateCounter08(void);
int PerfTestUpdateCounter09(void);
int PerfTestUpdateGlobalCounter10(void);
#endif /* __COUNTERS_H__ */ #endif /* __COUNTERS_H__ */

@ -21,11 +21,11 @@ void DecodeEthernet(ThreadVars *t, Packet *p, u_int8_t *pkt, u_int16_t len, Pack
if (ntohs(ethh->eth_type) == ETHERNET_TYPE_IP) { if (ntohs(ethh->eth_type) == ETHERNET_TYPE_IP) {
//printf("DecodeEthernet ip4\n"); //printf("DecodeEthernet ip4\n");
PerfCounterIncr(DECODER_IPV4, t->pca); PerfCounterIncr(COUNTER_DECODER_IPV4, t->pca);
DecodeIPV4(t, p, pkt + ETHERNET_HEADER_LEN, len - ETHERNET_HEADER_LEN, pq); DecodeIPV4(t, p, pkt + ETHERNET_HEADER_LEN, len - ETHERNET_HEADER_LEN, pq);
} else if(ntohs(ethh->eth_type) == ETHERNET_TYPE_IPV6) { } else if(ntohs(ethh->eth_type) == ETHERNET_TYPE_IPV6) {
//printf("DecodeEthernet ip6\n"); //printf("DecodeEthernet ip6\n");
PerfCounterIncr(DECODER_IPV6, t->pca); PerfCounterIncr(COUNTER_DECODER_IPV6, t->pca);
DecodeIPV6(t, p, pkt + ETHERNET_HEADER_LEN, len - ETHERNET_HEADER_LEN); DecodeIPV6(t, p, pkt + ETHERNET_HEADER_LEN, len - ETHERNET_HEADER_LEN);
} }

@ -91,22 +91,22 @@ void DecodeIPV4(ThreadVars *t, Packet *p, u_int8_t *pkt, u_int16_t len, PacketQu
} }
break; break;
case IPPROTO_TCP: case IPPROTO_TCP:
PerfCounterIncr(DECODER_TCP, t->pca); PerfCounterIncr(COUNTER_DECODER_TCP, t->pca);
return(DecodeTCP(t, p, pkt + IPV4_GET_HLEN(p), IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p))); return(DecodeTCP(t, p, pkt + IPV4_GET_HLEN(p), IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p)));
break; break;
case IPPROTO_UDP: case IPPROTO_UDP:
//printf("DecodeIPV4: next layer is UDP\n"); //printf("DecodeIPV4: next layer is UDP\n");
PerfCounterIncr(DECODER_UDP, t->pca); PerfCounterIncr(COUNTER_DECODER_UDP, t->pca);
return(DecodeUDP(t, p, pkt + IPV4_GET_HLEN(p), IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p))); return(DecodeUDP(t, p, pkt + IPV4_GET_HLEN(p), IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p)));
break; break;
case IPPROTO_ICMP: case IPPROTO_ICMP:
//printf("DecodeIPV4: next layer is ICMP\n"); //printf("DecodeIPV4: next layer is ICMP\n");
PerfCounterIncr(DECODER_ICMPV4, t->pca); PerfCounterIncr(COUNTER_DECODER_ICMPV4, t->pca);
return(DecodeICMPV4(t, p, pkt + IPV4_GET_HLEN(p), IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p))); return(DecodeICMPV4(t, p, pkt + IPV4_GET_HLEN(p), IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p)));
break; break;
case IPPROTO_IPV6: case IPPROTO_IPV6:
{ {
PerfCounterIncr(DECODER_ICMPV6, t->pca); PerfCounterIncr(COUNTER_DECODER_ICMPV6, t->pca);
if (pq != NULL) { if (pq != NULL) {
//printf("DecodeIPV4: next layer is IPV6\n"); //printf("DecodeIPV4: next layer is IPV6\n");
//printf("DecodeIPV4: we are p %p\n", p); //printf("DecodeIPV4: we are p %p\n", p);

@ -285,21 +285,21 @@ int ReceivePcapThreadDeinit(ThreadVars *tv, void *data) {
*/ */
int DecodePcap(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) { int DecodePcap(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) {
PerfCounterIncr(DECODER_PKTS, t->pca); PerfCounterIncr(COUNTER_DECODER_PKTS, t->pca);
PerfCounterAdd(DECODER_BYTES, t->pca, p->pktlen); PerfCounterAdd(COUNTER_DECODER_BYTES, t->pca, p->pktlen);
/* call the decoder */ /* call the decoder */
switch(p->pcap_v.datalink) { switch(p->pcap_v.datalink) {
case LINKTYPE_LINUX_SLL: case LINKTYPE_LINUX_SLL:
PerfCounterIncr(DECODER_SLL, t->pca); PerfCounterIncr(COUNTER_DECODER_SLL, t->pca);
DecodeSll(t,p,p->pkt,p->pktlen,pq); DecodeSll(t,p,p->pkt,p->pktlen,pq);
break; break;
case LINKTYPE_ETHERNET: case LINKTYPE_ETHERNET:
PerfCounterIncr(DECODER_ETH, t->pca); PerfCounterIncr(COUNTER_DECODER_ETH, t->pca);
DecodeEthernet(t,p,p->pkt,p->pktlen,pq); DecodeEthernet(t,p,p->pkt,p->pktlen,pq);
break; break;
case LINKTYPE_PPP: case LINKTYPE_PPP:
PerfCounterIncr(DECODER_PPP, t->pca); PerfCounterIncr(COUNTER_DECODER_PPP, t->pca);
DecodePPP(t,p,p->pkt,p->pktlen,pq); DecodePPP(t,p,p->pkt,p->pktlen,pq);
break; break;
default: default:
@ -312,30 +312,28 @@ int DecodePcap(ThreadVars *t, Packet *p, void *data, PacketQueue *pq) {
int DecodePcapThreadInit(ThreadVars *tv, void *initdata, void **data) int DecodePcapThreadInit(ThreadVars *tv, void *initdata, void **data)
{ {
pthread_t self_tid = pthread_self(); PerfRegisterCounter("decoder.pkts", "DecodePcap", TYPE_UINT64, "NULL",
&tv->pctx);
PerfRegisterCounter("decoder.pkts", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.bytes", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.bytes", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.ipv4", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.ipv4", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.ipv6", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.ipv6", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.ethernet", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.ethernet", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.sll", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.sll", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.tcp", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.tcp", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.udp", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.udp", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.icmpv4", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.icmpv4", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.icmpv6", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.icmpv6", "DecodePcap", self_tid, TYPE_UINT64, PerfRegisterCounter("decoder.ppp", "DecodePcap", TYPE_UINT64, "NULL",
"NULL", &tv->pctx); &tv->pctx);
PerfRegisterCounter("decoder.ppp", "DecodePcap", self_tid, TYPE_UINT64,
"NULL", &tv->pctx);
tv->pca = PerfGetAllCountersArray(&tv->pctx); tv->pca = PerfGetAllCountersArray(&tv->pctx);

@ -12,17 +12,17 @@ void TmModuleDecodePcapRegister (void);
#define LIBPCAP_PROMISC 1 #define LIBPCAP_PROMISC 1
// The counter ids. In case you can't recollect the ids, use the counter name // The counter ids. In case you can't recollect the ids, use the counter name
#define DECODER_PKTS 1 #define COUNTER_DECODER_PKTS 1
#define DECODER_BYTES 2 #define COUNTER_DECODER_BYTES 2
#define DECODER_IPV4 3 #define COUNTER_DECODER_IPV4 3
#define DECODER_IPV6 4 #define COUNTER_DECODER_IPV6 4
#define DECODER_ETH 5 #define COUNTER_DECODER_ETH 5
#define DECODER_SLL 6 #define COUNTER_DECODER_SLL 6
#define DECODER_TCP 7 #define COUNTER_DECODER_TCP 7
#define DECODER_UDP 8 #define COUNTER_DECODER_UDP 8
#define DECODER_ICMPV4 9 #define COUNTER_DECODER_ICMPV4 9
#define DECODER_ICMPV6 10 #define COUNTER_DECODER_ICMPV6 10
#define DECODER_PPP 11 #define COUNTER_DECODER_PPP 11
/* per packet Pcap vars */ /* per packet Pcap vars */
typedef struct PcapPacketVars_ typedef struct PcapPacketVars_

Loading…
Cancel
Save