Skip to content

Commit

Permalink
Merge pull request #468 from bazsi/filterx-scope-optimizations
Browse files Browse the repository at this point in the history
Filterx scope optimizations
  • Loading branch information
MrAnno authored Feb 11, 2025
2 parents d743d48 + 097238e commit 89dd4a1
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 171 deletions.
11 changes: 2 additions & 9 deletions lib/filterx/filterx-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,9 @@ filterx_eval_exec(FilterXEvalContext *context, FilterXExpr *expr)
}

void
filterx_eval_init_context(FilterXEvalContext *context, FilterXEvalContext *previous_context, LogMessage *msg)
filterx_eval_init_context(FilterXEvalContext *context, FilterXEvalContext *previous_context, FilterXScope *scope,
LogMessage *msg)
{
FilterXScope *scope;

if (previous_context)
scope = filterx_scope_ref(previous_context->scope);
else
scope = filterx_scope_new();
filterx_scope_make_writable(&scope);
filterx_scope_set_message(scope, msg);

memset(context, 0, sizeof(*context));
Expand All @@ -176,7 +170,6 @@ filterx_eval_deinit_context(FilterXEvalContext *context)
{
if (!context->previous_context)
g_ptr_array_free(context->weak_refs, TRUE);
filterx_scope_unref(context->scope);
filterx_eval_set_context(context->previous_context);
}

Expand Down
28 changes: 27 additions & 1 deletion lib/filterx/filterx-eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ EVTTAG *filterx_format_last_error_location(void);
void filterx_eval_clear_errors(void);
EVTTAG *filterx_format_eval_result(FilterXEvalResult result);

void filterx_eval_init_context(FilterXEvalContext *context, FilterXEvalContext *previous_context, LogMessage *msg);
void filterx_eval_init_context(FilterXEvalContext *context, FilterXEvalContext *previous_context,
FilterXScope *scope_storage, LogMessage *msg);
void filterx_eval_deinit_context(FilterXEvalContext *context);

static inline void
Expand Down Expand Up @@ -126,5 +127,30 @@ filterx_eval_store_weak_ref(FilterXObject *object)
}
}

#define FILTERX_EVAL_BEGIN_CONTEXT(eval_context, previous_context) \
do { \
FilterXScope *scope = NULL; \
gboolean local_scope = FALSE; \
\
if (previous_context) \
scope = filterx_scope_reuse(previous_context->scope); \
\
if (!scope) \
{ \
gsize alloc_size = filterx_scope_get_alloc_size(); \
scope = g_alloca(alloc_size); \
filterx_scope_init_instance(scope, alloc_size, path_options->filterx_context ? path_options->filterx_context->scope : NULL); \
local_scope = TRUE; \
} \
filterx_eval_init_context(&eval_context, path_options->filterx_context, scope, msg); \
do


#define FILTERX_EVAL_END_CONTEXT(eval_context) \
while(0); \
filterx_eval_deinit_context(&eval_context); \
if (local_scope) \
filterx_scope_clear(scope); \
} while(0)

#endif
83 changes: 43 additions & 40 deletions lib/filterx/filterx-pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,48 +64,51 @@ log_filterx_pipe_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_o
{
LogFilterXPipe *self = (LogFilterXPipe *) s;
FilterXEvalContext eval_context;
LogPathOptions local_path_options;
FilterXEvalResult eval_res;

path_options = log_path_options_chain(&local_path_options, path_options);
filterx_eval_init_context(&eval_context, path_options->filterx_context, msg);

msg_trace(">>>>>> filterx rule evaluation begin",
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_msg_reference(msg));
FilterXEvalContext *previous_context = path_options->filterx_context;

NVTable *payload = nv_table_ref(msg->payload);
eval_res = filterx_eval_exec(&eval_context, self->block);

msg_trace("<<<<<< filterx rule evaluation result",
filterx_format_eval_result(eval_res),
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_int("dirty", filterx_scope_is_dirty(eval_context.scope)),
evt_tag_msg_reference(msg));

local_path_options.filterx_context = &eval_context;
switch (eval_res)
{
case FXE_SUCCESS:
log_pipe_forward_msg(s, msg, path_options);
break;

case FXE_FAILURE:
if (path_options->matched)
(*path_options->matched) = FALSE;
/* FALLTHROUGH */
case FXE_DROP:
log_msg_drop(msg, path_options, AT_PROCESSED);
break;

default:
g_assert_not_reached();
break;
}

filterx_eval_deinit_context(&eval_context);
FILTERX_EVAL_BEGIN_CONTEXT(eval_context, previous_context)
{
FilterXEvalResult eval_res;
LogPathOptions local_path_options;

path_options = log_path_options_chain(&local_path_options, path_options);

msg_trace(">>>>>> filterx rule evaluation begin",
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_msg_reference(msg));

eval_res = filterx_eval_exec(&eval_context, self->block);

msg_trace("<<<<<< filterx rule evaluation result",
filterx_format_eval_result(eval_res),
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_int("dirty", filterx_scope_is_dirty(eval_context.scope)),
evt_tag_msg_reference(msg));

local_path_options.filterx_context = &eval_context;
switch (eval_res)
{
case FXE_SUCCESS:
log_pipe_forward_msg(s, msg, path_options);
break;

case FXE_FAILURE:
if (path_options->matched)
(*path_options->matched) = FALSE;
/* FALLTHROUGH */
case FXE_DROP:
log_msg_drop(msg, path_options, AT_PROCESSED);
break;

default:
g_assert_not_reached();
break;
}
}
FILTERX_EVAL_END_CONTEXT(eval_context);
nv_table_unref(payload);
}

Expand Down
Loading

0 comments on commit 89dd4a1

Please sign in to comment.