Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filterx performance improvements #400

Merged
merged 13 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/dnscache.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ dns_cache_store(DNSCache *self, gboolean persistent, gint family, void *addr, co
INIT_IV_LIST_HEAD(&entry->list);
if (!persistent)
{
entry->resolved = iv_now.tv_sec;
entry->resolved = get_cached_realtime_sec();
iv_list_add(&entry->list, &self->cache_list);
}
else
Expand Down Expand Up @@ -304,8 +304,7 @@ dns_cache_lookup(DNSCache *self, gint family, void *addr, const gchar **hostname
DNSCacheEntry *entry;
time_t now;

iv_validate_now();
now = iv_now.tv_sec;
now = get_cached_realtime_sec();
dns_cache_check_hosts(self, now);

dns_cache_fill_key(&key, family, addr);
Expand Down
1 change: 0 additions & 1 deletion lib/filterx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ set(FILTERX_SOURCES
filterx/func-vars.c
filterx/object-datetime.c
filterx/object-dict-interface.c
filterx/object-extractor.c
filterx/object-json-array.c
filterx/object-json-object.c
filterx/object-json.c
Expand Down
1 change: 0 additions & 1 deletion lib/filterx/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ filterx_sources = \
lib/filterx/func-vars.c \
lib/filterx/object-datetime.c \
lib/filterx/object-dict-interface.c \
lib/filterx/object-extractor.c \
lib/filterx/object-json-array.c \
lib/filterx/object-json-object.c \
lib/filterx/object-json.c \
Expand Down
12 changes: 8 additions & 4 deletions lib/filterx/expr-compound.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,19 @@ _eval_expr(FilterXExpr *expr, FilterXObject **result)
static gboolean
_eval_exprs(FilterXCompoundExpr *self, FilterXObject **result)
{
FilterXEvalContext *context = filterx_eval_get_context();

*result = NULL;
for (gint i = 0; i < self->exprs->len; i++)
gint len = self->exprs->len;
for (gint i = 0; i < len; i++)
{
filterx_object_unref(*result);
FilterXEvalContext *context = filterx_eval_get_context();

if (G_UNLIKELY(context->eval_control_modifier == FXC_DROP || context->eval_control_modifier == FXC_DONE))
/* code flow modifier detected, short circuiting */
return TRUE;
{
/* code flow modifier detected, short circuiting */
return TRUE;
}

FilterXExpr *expr = g_ptr_array_index(self->exprs, i);
if (!_eval_expr(expr, result))
Expand Down
31 changes: 0 additions & 31 deletions lib/filterx/filterx-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,6 @@ filterx_format_last_error_location(void)
return filterx_error_format_location(&context->error);
}


/*
* This is not a real weakref implementation as we will never get rid off
* weak references until the very end of a scope. If this wasn't the case
* we would have to:
* 1) run a proper GC
* 2) notify weak references once the object is detroyed
*
* None of that exists now and I doubt ever will (but never say never).
* Right now a weak ref is destroyed as a part of the scope finalization
* process at which point circular references will be broken so the rest can
* go too.
*/
void
filterx_eval_store_weak_ref(FilterXObject *object)
{
/* Frozen objects do not need weak refs. */
if (object && filterx_object_is_frozen(object))
return;

FilterXEvalContext *context = filterx_eval_get_context();

if (object && !object->weak_referenced)
{
/* avoid putting object to the list multiple times */
object->weak_referenced = TRUE;
g_assert(context->weak_refs);
g_ptr_array_add(context->weak_refs, filterx_object_ref(object));
}
}

FilterXEvalResult
filterx_eval_exec(FilterXEvalContext *context, FilterXExpr *expr, LogMessage *msg)
{
Expand Down
32 changes: 30 additions & 2 deletions lib/filterx/filterx-eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ EVTTAG *filterx_format_last_error_location(void);
void filterx_eval_clear_errors(void);
EVTTAG *filterx_format_eval_result(FilterXEvalResult result);

void filterx_eval_store_weak_ref(FilterXObject *object);

void filterx_eval_init_context(FilterXEvalContext *context, FilterXEvalContext *previous_context);
void filterx_eval_deinit_context(FilterXEvalContext *context);

Expand All @@ -95,4 +93,34 @@ filterx_eval_prepare_for_fork(FilterXEvalContext *context, LogMessage **pmsg, co
filterx_scope_write_protect(context->scope);
}

/*
* This is not a real weakref implementation as we will never get rid off
* weak references until the very end of a scope. If this wasn't the case
* we would have to:
* 1) run a proper GC
* 2) notify weak references once the object is detroyed
*
* None of that exists now and I doubt ever will (but never say never).
* Right now a weak ref is destroyed as a part of the scope finalization
* process at which point circular references will be broken so the rest can
* go too.
*/
static inline void
filterx_eval_store_weak_ref(FilterXObject *object)
{
/* Frozen objects do not need weak refs. */
if (object && filterx_object_is_frozen(object))
return;

if (object && !object->weak_referenced)
{
FilterXEvalContext *context = filterx_eval_get_context();
/* avoid putting object to the list multiple times */
object->weak_referenced = TRUE;
g_assert(context->weak_refs);
g_ptr_array_add(context->weak_refs, filterx_object_ref(object));
}
}


#endif
14 changes: 10 additions & 4 deletions lib/filterx/filterx-expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@
void
filterx_expr_set_location_with_text(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *lloc, const gchar *text)
{
self->lloc = *lloc;
if (!self->lloc)
self->lloc = g_new0(CFG_LTYPE, 1);
*self->lloc = *lloc;

if (debug_flag)
self->expr_text = g_strdup(text);
}

void
filterx_expr_set_location(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *lloc)
{
self->lloc = *lloc;
if (!self->lloc)
self->lloc = g_new0(CFG_LTYPE, 1);
*self->lloc = *lloc;
if (debug_flag)
{
GString *res = g_string_sized_new(0);
Expand All @@ -52,9 +57,9 @@ filterx_expr_set_location(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *lloc)
EVTTAG *
filterx_expr_format_location_tag(FilterXExpr *self)
{
if (self)
if (self && self->lloc)
return evt_tag_printf("expr", "%s:%d:%d|\t%s",
self->lloc.name, self->lloc.first_line, self->lloc.first_column,
self->lloc->name, self->lloc->first_line, self->lloc->first_column,
self->expr_text ? : "n/a");
else
return evt_tag_str("expr", "n/a");
Expand All @@ -74,6 +79,7 @@ filterx_expr_deinit_method(FilterXExpr *self, GlobalConfig *cfg)
void
filterx_expr_free_method(FilterXExpr *self)
{
g_free(self->lloc);
g_free(self->expr_text);
}

Expand Down
16 changes: 7 additions & 9 deletions lib/filterx/filterx-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@

struct _FilterXExpr
{
/* not thread-safe */
guint32 ref_cnt;
const gchar *type;
guint32 ignore_falsy_result:1, suppress_from_trace:1;

StatsCounterItem *eval_count;

/* evaluate expression */
FilterXObject *(*eval)(FilterXExpr *self);

/* not thread-safe */
guint32 ref_cnt;
guint32 ignore_falsy_result:1, suppress_from_trace:1, inited:1;

/* not to be used except for FilterXMessageRef, replace any cached values
* with the unmarshaled version */
void (*_update_repr)(FilterXExpr *self, FilterXObject *new_repr);
Expand All @@ -56,9 +55,8 @@ struct _FilterXExpr
void (*deinit)(FilterXExpr *self, GlobalConfig *cfg);
void (*free_fn)(FilterXExpr *self);

gboolean inited;

CFG_LTYPE lloc;
const gchar *type;
CFG_LTYPE *lloc;
gchar *expr_text;
};

Expand Down
39 changes: 0 additions & 39 deletions lib/filterx/filterx-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ filterx_type_init(FilterXType *type)
msg_error("Reregistering filterx type", evt_tag_str("name", type->name));
}

#define FILTERX_OBJECT_MAGIC_BIAS G_MAXINT32

void
filterx_object_free_method(FilterXObject *self)
{
Expand Down Expand Up @@ -125,12 +123,6 @@ filterx_object_freeze(FilterXObject *self)
return TRUE;
}

gboolean
filterx_object_is_frozen(FilterXObject *self)
{
return g_atomic_counter_get(&self->ref_cnt) == FILTERX_OBJECT_MAGIC_BIAS;
}

void
filterx_object_unfreeze_and_free(FilterXObject *self)
{
Expand All @@ -139,37 +131,6 @@ filterx_object_unfreeze_and_free(FilterXObject *self)
filterx_object_unref(self);
}

FilterXObject *
filterx_object_ref(FilterXObject *self)
{
if (!self)
return NULL;

if (filterx_object_is_frozen(self))
return self;

g_atomic_counter_inc(&self->ref_cnt);

return self;
}

void
filterx_object_unref(FilterXObject *self)
{
if (!self)
return;

if (filterx_object_is_frozen(self))
return;

g_assert(g_atomic_counter_get(&self->ref_cnt) > 0);
if (g_atomic_counter_dec_and_test(&self->ref_cnt))
{
self->type->free_fn(self);
g_free(self);
}
}

FilterXType FILTERX_TYPE_NAME(object) =
{
.super_type = NULL,
Expand Down
42 changes: 39 additions & 3 deletions lib/filterx/filterx-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ void _filterx_type_init_methods(FilterXType *type);
__VA_ARGS__ \
}

#define FILTERX_OBJECT_MAGIC_BIAS G_MAXINT32


FILTERX_DECLARE_TYPE(object);

Expand All @@ -100,14 +102,48 @@ FilterXObject *filterx_object_getattr_string(FilterXObject *self, const gchar *a
gboolean filterx_object_setattr_string(FilterXObject *self, const gchar *attr_name, FilterXObject **new_value);

FilterXObject *filterx_object_new(FilterXType *type);
FilterXObject *filterx_object_ref(FilterXObject *self);
void filterx_object_unref(FilterXObject *self);
gboolean filterx_object_freeze(FilterXObject *self);
gboolean filterx_object_is_frozen(FilterXObject *self);
void filterx_object_unfreeze_and_free(FilterXObject *self);
void filterx_object_init_instance(FilterXObject *self, FilterXType *type);
void filterx_object_free_method(FilterXObject *self);

static inline gboolean
filterx_object_is_frozen(FilterXObject *self)
{
return g_atomic_counter_get(&self->ref_cnt) == FILTERX_OBJECT_MAGIC_BIAS;
}

static inline FilterXObject *
filterx_object_ref(FilterXObject *self)
{
if (!self)
return NULL;

if (filterx_object_is_frozen(self))
return self;

g_atomic_counter_inc(&self->ref_cnt);

return self;
}

static inline void
filterx_object_unref(FilterXObject *self)
{
if (!self)
return;

if (filterx_object_is_frozen(self))
return;

g_assert(g_atomic_counter_get(&self->ref_cnt) > 0);
if (g_atomic_counter_dec_and_test(&self->ref_cnt))
{
self->type->free_fn(self);
g_free(self);
}
}

static inline void
filterx_object_make_readonly(FilterXObject *self)
{
Expand Down
52 changes: 0 additions & 52 deletions lib/filterx/filterx-variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,6 @@

#include "filterx-variable.h"

#define FILTERX_HANDLE_FLOATING_BIT (1UL << 31)

gboolean
filterx_variable_handle_is_floating(FilterXVariableHandle handle)
{
return !!(handle & FILTERX_HANDLE_FLOATING_BIT);
}

gboolean
filterx_variable_is_floating(FilterXVariable *v)
{
return filterx_variable_handle_is_floating(v->handle);
}

NVHandle
filterx_variable_get_nv_handle(FilterXVariable *v)
{
return v->handle & ~FILTERX_HANDLE_FLOATING_BIT;
}

const gchar *
filterx_variable_get_name(FilterXVariable *v, gssize *len)
{
return log_msg_get_handle_name(filterx_variable_get_nv_handle(v), len);
}

FilterXObject *
filterx_variable_get_value(FilterXVariable *v)
{
return filterx_object_ref(v->value);
}

void
filterx_variable_set_value(FilterXVariable *v, FilterXObject *new_value)
{
filterx_object_unref(v->value);
v->value = filterx_object_ref(new_value);
v->assigned = TRUE;
}

void
filterx_variable_unset_value(FilterXVariable *v)
{
filterx_variable_set_value(v, NULL);
}

gboolean
filterx_variable_is_set(FilterXVariable *v)
{
return v->value != NULL;
}

FilterXVariableHandle
filterx_map_varname_to_handle(const gchar *name, FilterXVariableType type)
{
Expand Down
Loading