From 7fe138f98d96dfc647d9044425afc8c33503acfa Mon Sep 17 00:00:00 2001 From: Balazs Scheidler Date: Sat, 28 Dec 2024 20:43:57 +0100 Subject: [PATCH] WIP: temporarily enable the extraction of source text --- lib/cfg-grammar.y | 4 ++-- lib/cfg-tree.c | 10 ++++++++++ lib/cfg-tree.h | 1 + lib/filterx/filterx-expr.c | 23 ++++++++++++++++++++--- lib/logmpx.c | 5 +++++ lib/logpipe.c | 17 ++++++++++++++++- 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/cfg-grammar.y b/lib/cfg-grammar.y index 59decde075..87891bce3e 100644 --- a/lib/cfg-grammar.y +++ b/lib/cfg-grammar.y @@ -567,14 +567,14 @@ expr_stmt source_stmt : KW_SOURCE string '{' source_content '}' { - $$ = log_expr_node_new_source($2, $4, &@1); + $$ = log_expr_node_new_source($2, $4, &@$); free($2); } ; dest_stmt : KW_DESTINATION string '{' dest_content '}' { - $$ = log_expr_node_new_destination($2, $4, &@1); + $$ = log_expr_node_new_destination($2, $4, &@$); free($2); } ; diff --git a/lib/cfg-tree.c b/lib/cfg-tree.c index d86b2ac635..748993c044 100644 --- a/lib/cfg-tree.c +++ b/lib/cfg-tree.c @@ -26,6 +26,7 @@ #include "logmpx.h" #include "logpipe.h" #include "metrics-pipe.h" +#include "cfg-source.h" #include @@ -282,6 +283,14 @@ log_expr_node_new(gint layout, gint content, const gchar *name, LogExprNode *chi self->line = yylloc->first_line; self->column = yylloc->first_column; } + if (yylloc && (debug_flag || 1)) + { + CFG_LTYPE lloc = *yylloc; + lloc.last_column = 255; + GString *text = g_string_new(""); + cfg_source_extract_source_text(configuration->lexer, &lloc, text); + self->expr_text = g_string_free(text, FALSE); + } return self; } @@ -308,6 +317,7 @@ _log_expr_node_free(LogExprNode *self) self->aux_destroy(self->aux); g_free(self->name); g_free(self->filename); + g_free(self->expr_text); g_free(self); } diff --git a/lib/cfg-tree.h b/lib/cfg-tree.h index 4e446881d4..06117b5e14 100644 --- a/lib/cfg-tree.h +++ b/lib/cfg-tree.h @@ -124,6 +124,7 @@ struct _LogExprNode gchar *filename; gint line, column; gint child_id; + gchar *expr_text; }; gint log_expr_node_lookup_flag(const gchar *flag); diff --git a/lib/filterx/filterx-expr.c b/lib/filterx/filterx-expr.c index 1e039982ab..8530df7b4f 100644 --- a/lib/filterx/filterx-expr.c +++ b/lib/filterx/filterx-expr.c @@ -28,6 +28,7 @@ #include "mainloop.h" #include "stats/stats-registry.h" #include "stats/stats-cluster-single.h" +#include "perf/perf.h" void filterx_expr_set_location_with_text(FilterXExpr *self, CFG_LTYPE *lloc, const gchar *text) @@ -36,7 +37,7 @@ filterx_expr_set_location_with_text(FilterXExpr *self, CFG_LTYPE *lloc, const gc self->lloc = g_new0(CFG_LTYPE, 1); *self->lloc = *lloc; - if (debug_flag && text) + if ((debug_flag || 1) && text) self->expr_text = g_strdup(text); } @@ -46,7 +47,7 @@ filterx_expr_set_location(FilterXExpr *self, CfgLexer *lexer, CFG_LTYPE *lloc) if (!self->lloc) self->lloc = g_new0(CFG_LTYPE, 1); *self->lloc = *lloc; - if (debug_flag) + if (debug_flag || 1) { GString *res = g_string_sized_new(0); cfg_source_extract_source_text(lexer, lloc, res); @@ -110,7 +111,7 @@ _init_sc_key_name(FilterXExpr *self, gchar *buf, gsize buf_len) gboolean filterx_expr_init_method(FilterXExpr *self, GlobalConfig *cfg) { - gchar buf[64]; + gchar buf[256]; _init_sc_key_name(self, buf, sizeof(buf)); stats_lock(); @@ -123,6 +124,22 @@ filterx_expr_init_method(FilterXExpr *self, GlobalConfig *cfg) stats_cluster_single_key_set(&sc_key, buf, labels, labels_len); stats_register_counter(STATS_LEVEL3, &sc_key, SC_TYPE_SINGLE_VALUE, &self->eval_count); stats_unlock(); + + if (!perf_is_trampoline_address(self->eval)) + { + //g_snprintf(buf, sizeof(buf), "filterx_%s_eval(%s)", self->type, self->name ? : "anon"); + if (self->lloc) +// g_snprintf(buf, sizeof(buf), "filterx::%s:%d:%d|\t%s", +// self->lloc->name, self->lloc->first_line, self->lloc->first_column, +// self->expr_text ? : "n/a"); + g_snprintf(buf, sizeof(buf), "filterx::%s", self->expr_text ? : "n/a"); + else + g_snprintf(buf, sizeof(buf), "filterx::%s(%s)", self->type, self->name ? : "anon"); +// msg_error("installing trampoline on expr", +// evt_tag_str("expr", buf)); + self->eval = perf_generate_trampoline(self->eval, buf); + } + return TRUE; } diff --git a/lib/logmpx.c b/lib/logmpx.c index 6cd119756b..49a63b02a3 100644 --- a/lib/logmpx.c +++ b/lib/logmpx.c @@ -93,6 +93,11 @@ log_multiplexer_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_op * write protected so that changes in those branches don't overwrite * data we still need */ + msg_trace("logmpx: making message readonly", + evt_tag_int("next_hops", self->next_hops->len), + evt_tag_int("pipe_next", !!self->super.pipe_next), + log_expr_node_location_tag(self->super.expr_node), + evt_tag_str("expr_text", self->super.expr_node && self->super.expr_node->expr_text ? self->super.expr_node->expr_text : "n/a")); filterx_eval_prepare_for_fork(path_options->filterx_context, &msg, path_options); log_msg_write_protect(msg); } diff --git a/lib/logpipe.c b/lib/logpipe.c index 2a3fbdec69..1f0b470970 100644 --- a/lib/logpipe.c +++ b/lib/logpipe.c @@ -25,6 +25,7 @@ #include "logpipe.h" #include "cfg-tree.h" #include "cfg-walker.h" +#include "perf/perf.h" gboolean (*pipe_single_step_hook)(LogPipe *pipe, LogMessage *msg, const LogPathOptions *path_options); @@ -146,6 +147,20 @@ log_pipe_clone_method(LogPipe *dst, const LogPipe *src) log_pipe_set_options(dst, &src->options); } +static gboolean +log_pipe_post_config_init_method(LogPipe *self) +{ +// GlobalConfig *cfg = log_pipe_get_config(self); + if (self->flags & PIF_CONFIG_RELATED) + { + gchar buf[256]; + /* FIXME: check that perf profiling is enabled */ + if (!perf_is_trampoline_address(self->queue)) + self->queue = perf_generate_trampoline(self->queue, log_expr_node_format_location(self->expr_node, buf, sizeof(buf))); + } + return TRUE; +} + void log_pipe_init_instance(LogPipe *self, GlobalConfig *cfg) { @@ -154,7 +169,7 @@ log_pipe_init_instance(LogPipe *self, GlobalConfig *cfg) self->pipe_next = NULL; self->persist_name = NULL; self->plugin_name = NULL; - + self->post_config_init = log_pipe_post_config_init_method; self->queue = log_pipe_forward_msg; self->free_fn = log_pipe_free_method; self->arcs = _arcs;