profiling: fix rule profiling output sometimes missing sid,rev,gid. Bug #576.

pull/129/merge
Victor Julien 14 years ago
parent 10a11b750d
commit 72782e5a6a

@ -42,11 +42,6 @@
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif #endif
typedef struct SCProfileDetectCtx_ {
uint32_t size;
uint32_t id;
} SCProfileDetectCtx;
/** /**
* Extra data for rule profiling. * Extra data for rule profiling.
*/ */
@ -61,6 +56,13 @@ typedef struct SCProfileData_ {
uint64_t ticks_no_match; uint64_t ticks_no_match;
} SCProfileData; } SCProfileData;
typedef struct SCProfileDetectCtx_ {
uint32_t size;
uint32_t id;
SCProfileData *data;
pthread_mutex_t data_m;
} SCProfileDetectCtx;
/** /**
* Used for generating the summary data to print. * Used for generating the summary data to print.
*/ */
@ -80,8 +82,6 @@ typedef struct SCProfileSummary_ {
} SCProfileSummary; } SCProfileSummary;
extern int profiling_output_to_file; extern int profiling_output_to_file;
static SCProfileData rules_profile_data[0xffff];
static pthread_mutex_t rules_profile_data_m;
int profiling_rules_enabled = 0; int profiling_rules_enabled = 0;
static char *profiling_file_name = ""; static char *profiling_file_name = "";
static const char *profiling_file_mode = "a"; static const char *profiling_file_mode = "a";
@ -112,7 +112,6 @@ void SCProfilingRulesGlobalInit(void) {
conf = ConfGetNode("profiling.rules"); conf = ConfGetNode("profiling.rules");
if (conf != NULL) { if (conf != NULL) {
if (ConfNodeChildValueIsTrue(conf, "enabled")) { if (ConfNodeChildValueIsTrue(conf, "enabled")) {
memset(rules_profile_data, 0, sizeof(rules_profile_data));
profiling_rules_enabled = 1; profiling_rules_enabled = 1;
val = ConfNodeLookupChildValue(conf, "sort"); val = ConfNodeLookupChildValue(conf, "sort");
@ -301,29 +300,29 @@ SCProfilingRuleDump(SCProfileDetectCtx *rules_ctx)
memset(summary, 0, summary_size); memset(summary, 0, summary_size);
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
summary[i].sid = rules_profile_data[i].sid; summary[i].sid = rules_ctx->data[i].sid;
summary[i].rev = rules_profile_data[i].rev; summary[i].rev = rules_ctx->data[i].rev;
summary[i].gid = rules_profile_data[i].gid; summary[i].gid = rules_ctx->data[i].gid;
summary[i].ticks = rules_profile_data[i].ticks_match + rules_profile_data[i].ticks_no_match; summary[i].ticks = rules_ctx->data[i].ticks_match + rules_ctx->data[i].ticks_no_match;
summary[i].checks = rules_profile_data[i].checks; summary[i].checks = rules_ctx->data[i].checks;
if (summary[i].ticks > 0) { if (summary[i].ticks > 0) {
summary[i].avgticks = (long double)summary[i].ticks / (long double)rules_profile_data[i].checks; summary[i].avgticks = (long double)summary[i].ticks / (long double)summary[i].checks;
} }
summary[i].matches = rules_profile_data[i].matches; summary[i].matches = rules_ctx->data[i].matches;
summary[i].max = rules_profile_data[i].max; summary[i].max = rules_ctx->data[i].max;
summary[i].ticks_match = rules_profile_data[i].ticks_match; summary[i].ticks_match = rules_ctx->data[i].ticks_match;
summary[i].ticks_no_match = rules_profile_data[i].ticks_no_match; summary[i].ticks_no_match = rules_ctx->data[i].ticks_no_match;
if (rules_profile_data[i].ticks_match > 0) { if (summary[i].ticks_match > 0) {
summary[i].avgticks_match = (long double)rules_profile_data[i].ticks_match / summary[i].avgticks_match = (long double)summary[i].ticks_match /
(long double)rules_profile_data[i].matches; (long double)summary[i].matches;
} }
if (rules_profile_data[i].ticks_no_match > 0) { if (summary[i].ticks_no_match > 0) {
summary[i].avgticks_no_match = (long double)rules_profile_data[i].ticks_no_match / summary[i].avgticks_no_match = (long double)summary[i].ticks_no_match /
((long double)rules_profile_data[i].checks - (long double)rules_profile_data[i].matches); ((long double)summary[i].checks - (long double)summary[i].matches);
} }
total_ticks += summary[i].ticks; total_ticks += summary[i].ticks;
} }
@ -415,7 +414,6 @@ SCProfilingRuleDump(SCProfileDetectCtx *rules_ctx)
fclose(fp); fclose(fp);
SCFree(summary); SCFree(summary);
SCLogInfo("Done dumping profiling data."); SCLogInfo("Done dumping profiling data.");
memset(rules_profile_data, 0x00, sizeof(rules_profile_data));
} }
/** /**
@ -459,7 +457,7 @@ SCProfileDetectCtx *SCProfilingRuleInitCtx(void) {
if (ctx != NULL) { if (ctx != NULL) {
memset(ctx, 0x00, sizeof(SCProfileDetectCtx)); memset(ctx, 0x00, sizeof(SCProfileDetectCtx));
if (pthread_mutex_init(&rules_profile_data_m, NULL) != 0) { if (pthread_mutex_init(&ctx->data_m, NULL) != 0) {
SCLogError(SC_ERR_MUTEX, SCLogError(SC_ERR_MUTEX,
"Failed to initialize hash table mutex."); "Failed to initialize hash table mutex.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -472,7 +470,9 @@ SCProfileDetectCtx *SCProfilingRuleInitCtx(void) {
void SCProfilingRuleDestroyCtx(SCProfileDetectCtx *ctx) { void SCProfilingRuleDestroyCtx(SCProfileDetectCtx *ctx) {
if (ctx != NULL) { if (ctx != NULL) {
SCProfilingRuleDump(ctx); SCProfilingRuleDump(ctx);
pthread_mutex_destroy(&rules_profile_data_m); if (ctx->data != NULL)
SCFree(ctx->data);
pthread_mutex_destroy(&ctx->data_m);
SCFree(ctx); SCFree(ctx);
} }
} }
@ -490,28 +490,29 @@ void SCProfilingRuleThreadSetup(SCProfileDetectCtx *ctx, DetectEngineThreadCtx *
} }
} }
static void SCProfilingRuleThreadMerge(DetectEngineThreadCtx *det_ctx) { static void SCProfilingRuleThreadMerge(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx) {
if (det_ctx == NULL || det_ctx->rule_perf_data == NULL) if (de_ctx == NULL || de_ctx->profile_ctx == NULL || de_ctx->profile_ctx->data == NULL ||
det_ctx == NULL || det_ctx->rule_perf_data == NULL)
return; return;
int i; int i;
for (i = 0; i < det_ctx->rule_perf_data_size; i++) { for (i = 0; i < det_ctx->rule_perf_data_size; i++) {
rules_profile_data[i].checks += det_ctx->rule_perf_data[i].checks; de_ctx->profile_ctx->data[i].checks += det_ctx->rule_perf_data[i].checks;
rules_profile_data[i].matches += det_ctx->rule_perf_data[i].matches; de_ctx->profile_ctx->data[i].matches += det_ctx->rule_perf_data[i].matches;
rules_profile_data[i].ticks_match += det_ctx->rule_perf_data[i].ticks_match; de_ctx->profile_ctx->data[i].ticks_match += det_ctx->rule_perf_data[i].ticks_match;
rules_profile_data[i].ticks_no_match += det_ctx->rule_perf_data[i].ticks_no_match; de_ctx->profile_ctx->data[i].ticks_no_match += det_ctx->rule_perf_data[i].ticks_no_match;
if (det_ctx->rule_perf_data[i].max > rules_profile_data[i].max) if (det_ctx->rule_perf_data[i].max > de_ctx->profile_ctx->data[i].max)
rules_profile_data[i].max = det_ctx->rule_perf_data[i].max; de_ctx->profile_ctx->data[i].max = det_ctx->rule_perf_data[i].max;
} }
} }
void SCProfilingRuleThreadCleanup(DetectEngineThreadCtx *det_ctx) { void SCProfilingRuleThreadCleanup(DetectEngineThreadCtx *det_ctx) {
if (det_ctx == NULL || det_ctx->rule_perf_data == NULL) if (det_ctx == NULL || det_ctx->de_ctx == NULL || det_ctx->rule_perf_data == NULL)
return; return;
pthread_mutex_lock(&rules_profile_data_m); pthread_mutex_lock(&det_ctx->de_ctx->profile_ctx->data_m);
SCProfilingRuleThreadMerge(det_ctx); SCProfilingRuleThreadMerge(det_ctx->de_ctx, det_ctx);
pthread_mutex_unlock(&rules_profile_data_m); pthread_mutex_unlock(&det_ctx->de_ctx->profile_ctx->data_m);
} }
/** /**
@ -529,12 +530,24 @@ SCProfilingRuleInitCounters(DetectEngineCtx *de_ctx)
uint32_t count = 0; uint32_t count = 0;
while (sig != NULL) { while (sig != NULL) {
sig->profiling_id = SCProfilingRegisterRuleCounter(de_ctx->profile_ctx); sig->profiling_id = SCProfilingRegisterRuleCounter(de_ctx->profile_ctx);
rules_profile_data[sig->profiling_id].sid = sig->id;
rules_profile_data[sig->profiling_id].gid = sig->gid;
rules_profile_data[sig->profiling_id].rev = sig->rev;
sig = sig->next; sig = sig->next;
count++; count++;
} }
if (count > 0) {
de_ctx->profile_ctx->data = SCMalloc(sizeof(SCProfileData) * de_ctx->profile_ctx->size);
BUG_ON(de_ctx->profile_ctx->data == NULL);
memset(de_ctx->profile_ctx->data, 0x00, sizeof(SCProfileData) * de_ctx->profile_ctx->size);
sig = de_ctx->sig_list;
while (sig != NULL) {
de_ctx->profile_ctx->data[sig->profiling_id].sid = sig->id;
de_ctx->profile_ctx->data[sig->profiling_id].gid = sig->gid;
de_ctx->profile_ctx->data[sig->profiling_id].rev = sig->rev;
sig = sig->next;
}
}
SCLogInfo("Registered %"PRIu32" rule profiling counters.", count); SCLogInfo("Registered %"PRIu32" rule profiling counters.", count);
} }

Loading…
Cancel
Save