profiling: let 'ruleset-profile' send message

Let's send the profile output as an answer on the Unix socket.
pull/8879/head
Eric Leblond 4 years ago committed by Victor Julien
parent 75b46edd79
commit df88ef0249

@ -784,12 +784,15 @@ static TmEcode UnixManagerRulesetStatsCommand(json_t *cmd,
static TmEcode UnixManagerRulesetProfileCommand(json_t *cmd, json_t *server_msg, void *data) static TmEcode UnixManagerRulesetProfileCommand(json_t *cmd, json_t *server_msg, void *data)
{ {
SCEnter(); SCEnter();
TmEcode retval;
DetectEngineCtx *de_ctx = DetectEngineGetCurrent(); DetectEngineCtx *de_ctx = DetectEngineGetCurrent();
retval = SCProfileRuleTriggerDump(de_ctx); json_t *js = SCProfileRuleTriggerDump(de_ctx);
json_object_set_new(server_msg, "message", json_string("OK")); if (js == NULL) {
SCReturnInt(retval); json_object_set_new(server_msg, "message", json_string("NOK"));
SCReturnInt(TM_ECODE_FAILED);
}
json_object_set_new(server_msg, "message", js);
SCReturnInt(TM_ECODE_OK);
} }
#endif #endif

@ -261,21 +261,21 @@ SCProfileSummarySortByMaxTicks(const void *a, const void *b)
return s0->max > s1->max ? -1 : 1; return s0->max > s1->max ? -1 : 1;
} }
static void DumpJson(FILE *fp, SCProfileSummary *summary, static json_t *BuildJson(
uint32_t count, uint64_t total_ticks, SCProfileSummary *summary, uint32_t count, uint64_t total_ticks, const char *sort_desc)
const char *sort_desc)
{ {
char timebuf[64]; char timebuf[64];
uint32_t i; uint32_t i;
struct timeval tval; struct timeval tval;
json_t *js = json_object(); json_t *js = json_object();
if (js == NULL) if (js == NULL)
return; return js;
json_t *jsa = json_array(); json_t *jsa = json_array();
if (jsa == NULL) { if (jsa == NULL) {
json_decref(js); json_decref(js);
return; return js;
} }
gettimeofday(&tval, NULL); gettimeofday(&tval, NULL);
@ -312,7 +312,15 @@ static void DumpJson(FILE *fp, SCProfileSummary *summary,
} }
} }
json_object_set_new(js, "rules", jsa); json_object_set_new(js, "rules", jsa);
return js;
}
static void DumpJson(FILE *fp, SCProfileSummary *summary, uint32_t count, uint64_t total_ticks,
const char *sort_desc)
{
json_t *js = BuildJson(summary, count, total_ticks, sort_desc);
if (unlikely(js == NULL))
return;
char *js_s = json_dumps(js, char *js_s = json_dumps(js,
JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII| JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII|
JSON_ESCAPE_SLASH); JSON_ESCAPE_SLASH);
@ -391,31 +399,32 @@ static void DumpText(FILE *fp, SCProfileSummary *summary,
* *
* \param de_ctx The active DetectEngineCtx, used to get at the loaded rules. * \param de_ctx The active DetectEngineCtx, used to get at the loaded rules.
*/ */
static void static void *SCProfilingRuleDump(SCProfileDetectCtx *rules_ctx, int file_output)
SCProfilingRuleDump(SCProfileDetectCtx *rules_ctx)
{ {
uint32_t i; uint32_t i;
FILE *fp; FILE *fp = NULL;
if (rules_ctx == NULL) if (rules_ctx == NULL)
return; return NULL;
if (profiling_output_to_file == 1) { if (file_output != 0) {
fp = fopen(profiling_file_name, profiling_file_mode); if (profiling_output_to_file == 1) {
fp = fopen(profiling_file_name, profiling_file_mode);
if (fp == NULL) { if (fp == NULL) {
SCLogError("failed to open %s: %s", profiling_file_name, strerror(errno)); SCLogError("failed to open %s: %s", profiling_file_name, strerror(errno));
return; return NULL;
}
} else {
fp = stdout;
} }
} else {
fp = stdout;
} }
int summary_size = sizeof(SCProfileSummary) * rules_ctx->size; int summary_size = sizeof(SCProfileSummary) * rules_ctx->size;
SCProfileSummary *summary = SCMalloc(summary_size); SCProfileSummary *summary = SCMalloc(summary_size);
if (unlikely(summary == NULL)) { if (unlikely(summary == NULL)) {
SCLogError("Error allocating memory for profiling summary"); SCLogError("Error allocating memory for profiling summary");
return; return NULL;
} }
uint32_t count = rules_ctx->size; uint32_t count = rules_ctx->size;
@ -493,17 +502,26 @@ SCProfilingRuleDump(SCProfileDetectCtx *rules_ctx)
break; break;
} }
if (profiling_rule_json) { if (profiling_rule_json) {
DumpJson(fp, summary, count, total_ticks, sort_desc); if (file_output != 1) {
json_t *js = BuildJson(summary, count, total_ticks, sort_desc);
SCFree(summary);
return js;
} else {
DumpJson(fp, summary, count, total_ticks, sort_desc);
}
} else { } else {
DumpText(fp, summary, count, total_ticks, sort_desc); DumpText(fp, summary, count, total_ticks, sort_desc);
} }
order++; order++;
} }
if (fp != stdout) if (file_output != 0) {
fclose(fp); if (fp != stdout)
fclose(fp);
}
SCFree(summary); SCFree(summary);
SCLogPerf("Done dumping profiling data."); SCLogPerf("Done dumping profiling data.");
return NULL;
} }
/** /**
@ -559,7 +577,7 @@ static SCProfileDetectCtx *SCProfilingRuleInitCtx(void)
void SCProfilingRuleDestroyCtx(SCProfileDetectCtx *ctx) void SCProfilingRuleDestroyCtx(SCProfileDetectCtx *ctx)
{ {
if (ctx != NULL) { if (ctx != NULL) {
SCProfilingRuleDump(ctx); SCProfilingRuleDump(ctx, 1);
if (ctx->data != NULL) if (ctx->data != NULL)
SCFree(ctx->data); SCFree(ctx->data);
pthread_mutex_destroy(&ctx->data_m); pthread_mutex_destroy(&ctx->data_m);
@ -668,10 +686,9 @@ SCProfilingRuleInitCounters(DetectEngineCtx *de_ctx)
SCLogPerf("Registered %"PRIu32" rule profiling counters.", count); SCLogPerf("Registered %"PRIu32" rule profiling counters.", count);
} }
int SCProfileRuleTriggerDump(DetectEngineCtx *de_ctx) json_t *SCProfileRuleTriggerDump(DetectEngineCtx *de_ctx)
{ {
SCProfilingRuleDump(de_ctx->profile_ctx); return SCProfilingRuleDump(de_ctx->profile_ctx, 0);
return TM_ECODE_OK;
} }
#endif /* PROFILING */ #endif /* PROFILING */

@ -400,7 +400,7 @@ void SCProfilingRuleUpdateCounter(DetectEngineThreadCtx *, uint16_t, uint64_t, i
void SCProfilingRuleThreadSetup(struct SCProfileDetectCtx_ *, DetectEngineThreadCtx *); void SCProfilingRuleThreadSetup(struct SCProfileDetectCtx_ *, DetectEngineThreadCtx *);
void SCProfilingRuleThreadCleanup(DetectEngineThreadCtx *); void SCProfilingRuleThreadCleanup(DetectEngineThreadCtx *);
int SCProfileRuleStart(Packet *p); int SCProfileRuleStart(Packet *p);
int SCProfileRuleTriggerDump(DetectEngineCtx *de_ctx); json_t *SCProfileRuleTriggerDump(DetectEngineCtx *de_ctx);
void SCProfilingRuleThreatAggregate(DetectEngineThreadCtx *det_ctx); void SCProfilingRuleThreatAggregate(DetectEngineThreadCtx *det_ctx);
#define RULE_PROFILING_START(p) \ #define RULE_PROFILING_START(p) \

Loading…
Cancel
Save