Skip to content

Commit 7f0f2a1

Browse files
committed
refactor: Remove old profiler regex
1 parent afc260f commit 7f0f2a1

File tree

11 files changed

+23
-210
lines changed

11 files changed

+23
-210
lines changed

agent/src/ebpf/mod.rs

-43
Original file line numberDiff line numberDiff line change
@@ -615,45 +615,6 @@ extern "C" {
615615
*/
616616
pub fn continuous_profiler_running() -> bool;
617617

618-
/*
619-
* To set the regex matching for the profiler.
620-
*
621-
* Perform regular expression matching on process names.
622-
* Processes that successfully match the regular expression are
623-
* aggregated using the key:
624-
* `{pid + stime + u_stack_id + k_stack_id + tid + cpu}`.
625-
*
626-
* For processes that do not match, they are aggregated using the
627-
* key:
628-
* `<process name + u_stack_id + k_stack_id + cpu>`.
629-
*
630-
* Using regular expressions, we match process names to establish
631-
* symbol table caching for specific processes, rather than enabling
632-
* symbol caching for all processes. This approach aims to reduce
633-
* memory usage.
634-
*
635-
* For example:
636-
*
637-
* "^(java|nginx|profiler|telegraf|mysqld|socket_tracer|.*deepflow.*)$"
638-
*
639-
* This regex pattern matches the process names: "java", "nginx", "profiler",
640-
* "telegraf", "mysqld", "socket_tracer", and any process containing the
641-
* substring "deepflow".
642-
*
643-
* By using this interface, you can customize the regular expression to
644-
* match specific process names that you want to establish symbol table
645-
* caching for, providing flexibility and control over which processes will
646-
* have symbol caching enabled. This allows you to finetune the memory usage
647-
* and profiling behavior of the profiler according to your requirements.
648-
*
649-
* The default expression is empty (''), indicating that no profile data will
650-
* be generated.
651-
*
652-
* @pattern : Regular expression pattern. e.g. "^(java|nginx|.*ser.*)$"
653-
* @returns 0 on success, < 0 on error
654-
*/
655-
pub fn set_profiler_regex(pattern: *const c_char) -> c_int;
656-
657618
/*
658619
* This interface is used to set whether CPUID should be included in the
659620
* aggregation of stack trace data.
@@ -744,8 +705,6 @@ extern "C" {
744705

745706
cfg_if::cfg_if! {
746707
if #[cfg(feature = "extended_profile")] {
747-
pub fn set_offcpu_profiler_regex(pattern: *const c_char) -> c_int;
748-
749708
pub fn enable_offcpu_profiler() -> c_int;
750709

751710
pub fn disable_offcpu_profiler() -> c_int;
@@ -756,8 +715,6 @@ extern "C" {
756715
block_time: c_uint,
757716
) -> c_int;
758717

759-
pub fn set_memory_profiler_regex(pattern: *const c_char) -> c_int;
760-
761718
pub fn enable_memory_profiler() -> c_int;
762719

763720
pub fn disable_memory_profiler() -> c_int;

agent/src/ebpf/samples/rust/socket-tracer/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ fn main() {
637637
// ::std::process::exit(1);
638638
//}
639639

640-
//set_profiler_regex(
640+
//set_feature_regex(
641+
// ebpf::FEATURE_PROFILE_ONCPU,
641642
// CString::new(
642643
// "^(java|nginx|profiler|telegraf|mysqld|.*deepflow.*|socket_tracer)$".as_bytes(),
643644
// )

agent/src/ebpf/user/extended/extended.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void __attribute__ ((weak)) extended_process_exit(int pid) {}
5959

6060
void __attribute__ ((weak)) extended_match_pid_handle(int feat, int pid, enum match_pids_act act) {}
6161

62-
bool __attribute__ ((weak)) extended_require_dwarf(int pid, const char *name)
62+
bool __attribute__ ((weak)) extended_require_dwarf(int pid, const char *path)
6363
{
6464
return false;
6565
}

agent/src/ebpf/user/extended/extended.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ void extended_match_pid_handle(int feat, int pid, enum match_pids_act act);
8383
/**
8484
* @brief **extended_requires_dwarf()** whether extended profilers require DWARF unwinding
8585
* @param pid Process ID
86-
* @param name Process executable name
86+
* @param name Process executable path
8787
*/
88-
bool extended_require_dwarf(int pid, const char *name);
88+
bool extended_require_dwarf(int pid, const char *path);
8989

9090
#endif /* DF_EXTENDED_H */

agent/src/ebpf/user/profile/perf_profiler.c

-55
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,6 @@ int stop_continuous_profiler(void *cb_ctx[PROFILER_CTX_NUM])
523523

524524
u64 alloc_b, free_b;
525525
get_mem_stat(&alloc_b, &free_b);
526-
if (oncpu_ctx.regex_existed) {
527-
regfree(&oncpu_ctx.profiler_regex);
528-
oncpu_ctx.regex_existed = false;
529-
}
530526

531527
ebpf_info(LOG_CP_TAG "== alloc_b %lu bytes, free_b %lu bytes, "
532528
"use %lu bytes ==\n", alloc_b, free_b, alloc_b - free_b);
@@ -876,48 +872,6 @@ void release_flame_graph_hash(void)
876872
flame_graph_start_time, flame_graph_end_time);
877873
}
878874

879-
/*
880-
* To set the regex matching for the profiler.
881-
*
882-
* @pattern : Regular expression pattern. e.g. "^(java|nginx|.*ser.*)$"
883-
* @returns 0 on success, < 0 on error
884-
*/
885-
int set_profiler_regex(const char *pattern)
886-
{
887-
if (profiler_tracer == NULL) {
888-
ebpf_warning(LOG_CP_TAG
889-
"The 'profiler_tracer' has not been created yet."
890-
" Please use start_continuous_profiler() to create it first.\n");
891-
return (-1);
892-
}
893-
894-
if (!g_enable_oncpu) {
895-
ebpf_warning(LOG_CP_TAG
896-
"'profiler_regex' cannot be set while on-CPU is currently disabled.\n");
897-
return (-1);
898-
}
899-
900-
/*
901-
* During the data processing, the thread responsible for matching reads the
902-
* regular expression, while the thread handling the regular expression upd-
903-
* ates is different. Synchronization is implemented to ensure protection and
904-
* coordination between these two threads.
905-
*/
906-
profile_regex_lock(&oncpu_ctx);
907-
do_profiler_regex_config(pattern, &oncpu_ctx);
908-
profile_regex_unlock(&oncpu_ctx);
909-
910-
unwind_process_reload();
911-
912-
ebpf_info(LOG_CP_TAG "Set 'profiler_regex' successful, pattern : '%s'",
913-
pattern);
914-
return (0);
915-
}
916-
917-
bool check_oncpu_profiler_regex(const char *name) {
918-
return check_profiler_regex(&oncpu_ctx, name);
919-
}
920-
921875
int set_profiler_cpu_aggregation(int flag)
922876
{
923877
if (flag != 0 && flag != 1) {
@@ -1026,20 +980,11 @@ void release_flame_graph_hash(void)
1026980
return;
1027981
}
1028982

1029-
int set_profiler_regex(const char *pattern)
1030-
{
1031-
return (-1);
1032-
}
1033-
1034983
int set_profiler_cpu_aggregation(int flag)
1035984
{
1036985
return (-1);
1037986
}
1038987

1039-
bool check_oncpu_profiler_regex(const char *name) {
1040-
return false;
1041-
}
1042-
1043988
struct bpf_tracer *get_profiler_tracer(void)
1044989
{
1045990
return NULL;

agent/src/ebpf/user/profile/perf_profiler.h

-2
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ int start_continuous_profiler(int freq, int java_syms_update_delay,
200200
tracer_callback_t callback, void *cb_ctx[PROFILER_CTX_NUM]);
201201
void process_stack_trace_data_for_flame_graph(stack_trace_msg_t * val);
202202
void release_flame_graph_hash(void);
203-
int set_profiler_regex(const char *pattern);
204203
int set_profiler_cpu_aggregation(int flag);
205-
bool check_oncpu_profiler_regex(const char *name);
206204
struct bpf_tracer *get_profiler_tracer(void);
207205
void set_enable_perf_sample(struct bpf_tracer *t, u64 enable_flag);
208206
void cpdbg_process(stack_trace_msg_t * msg);

agent/src/ebpf/user/profile/profile_common.c

-60
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ int profiler_context_init(struct profiler_context *ctx,
131131
snprintf(ctx->custom_stack_map_b.name, sizeof(ctx->custom_stack_map_b.name), "%s",
132132
custom_stack_map_name_b);
133133

134-
ctx->regex_existed = false;
135134
ctx->only_matched_data = only_matched;
136135
ctx->use_delta_time = use_delta_time;
137136
ctx->type = type;
@@ -162,37 +161,6 @@ void set_bpf_run_enabled(struct bpf_tracer *t, struct profiler_context *ctx,
162161
enable_flag);
163162
}
164163

165-
int do_profiler_regex_config(const char *pattern, struct profiler_context *ctx)
166-
{
167-
if (*pattern == '\0') {
168-
ctx->regex_existed = false;
169-
ebpf_warning("%sSet 'profiler_regex' pattern : '', an empty"
170-
" regular expression will not generate any stack data."
171-
"Please configure the regular expression for profiler.\n",
172-
ctx->tag);
173-
return (0);
174-
}
175-
176-
if (ctx->regex_existed) {
177-
regfree(&ctx->profiler_regex);
178-
}
179-
180-
int ret = regcomp(&ctx->profiler_regex, pattern, REG_EXTENDED);
181-
if (ret != 0) {
182-
char error_buffer[100];
183-
regerror(ret, &ctx->profiler_regex, error_buffer,
184-
sizeof(error_buffer));
185-
ebpf_warning("%sPattern %s failed to compile the regular "
186-
"expression: %s\n", ctx->tag, pattern,
187-
error_buffer);
188-
ctx->regex_existed = false;
189-
return (-1);
190-
}
191-
192-
ctx->regex_existed = true;
193-
return 0;
194-
}
195-
196164
static bool check_kallsyms_addr_is_zero(void)
197165
{
198166
const int check_num = 100;
@@ -1268,34 +1236,6 @@ void process_bpf_stacktraces(struct profiler_context *ctx, struct bpf_tracer *t)
12681236
push_and_release_stack_trace_msg(ctx, &ctx->msg_hash, false);
12691237
}
12701238

1271-
bool check_profiler_regex(struct profiler_context *ctx, const char *name)
1272-
{
1273-
bool matched = false;
1274-
for (int i = 0; i < ARRAY_SIZE(g_ctx_array); i++) {
1275-
if (g_ctx_array[i] == NULL)
1276-
continue;
1277-
1278-
if (ctx != NULL && ctx->name != NULL) {
1279-
if (strcmp(g_ctx_array[i]->name, ctx->name))
1280-
continue;
1281-
}
1282-
1283-
if (g_ctx_array[i]->regex_existed) {
1284-
profile_regex_lock(g_ctx_array[i]);
1285-
matched =
1286-
(regexec
1287-
(&g_ctx_array[i]->profiler_regex, name, 0, NULL, 0)
1288-
== 0);
1289-
profile_regex_unlock(g_ctx_array[i]);
1290-
if (matched) {
1291-
return true;
1292-
}
1293-
}
1294-
}
1295-
1296-
return false;
1297-
}
1298-
12991239
bool profiler_is_running(void)
13001240
{
13011241
for (int i = 0; i < ARRAY_SIZE(g_ctx_array); i++) {

agent/src/ebpf/user/profile/profile_common.h

-37
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,6 @@ struct profiler_context {
6666
*/
6767
volatile u64 cpu_aggregation_flag;
6868

69-
/*
70-
* To perform regular expression matching on process names,
71-
* the 'profiler_regex' is set using the 'set_profiler_regex()'
72-
* interface. Processes that successfully match the regular
73-
* expression are aggregated using the key:
74-
* `{pid + stime + u_stack_id + k_stack_id + tid + cpu}`.
75-
*
76-
* For processes that do not match, they are aggregated using the
77-
* key:
78-
* `<process name + u_stack_id + k_stack_id + cpu>`.
79-
*/
80-
regex_t profiler_regex;
81-
bool regex_existed;
82-
volatile u32 regex_lock;
83-
8469
/*
8570
* The profiler stop flag, with 1 indicating stop and
8671
* 0 indicating running status.
@@ -147,20 +132,8 @@ struct profiler_context {
147132
void *callback_ctx;
148133
};
149134

150-
static inline void profile_regex_lock(struct profiler_context *c)
151-
{
152-
while (__atomic_test_and_set(&c->regex_lock, __ATOMIC_ACQUIRE))
153-
CLIB_PAUSE();
154-
}
155-
156-
static inline void profile_regex_unlock(struct profiler_context *c)
157-
{
158-
__atomic_clear(&c->regex_lock, __ATOMIC_RELEASE);
159-
}
160-
161135
void process_bpf_stacktraces(struct profiler_context *ctx,
162136
struct bpf_tracer *t);
163-
int do_profiler_regex_config(const char *pattern, struct profiler_context *ctx);
164137
void set_bpf_run_enabled(struct bpf_tracer *t, struct profiler_context *ctx,
165138
u64 enable_flag);
166139
int profiler_context_init(struct profiler_context *ctx, const char *name,
@@ -177,16 +150,6 @@ int java_libs_and_tools_install(void);
177150
void push_and_release_stack_trace_msg(struct profiler_context *ctx,
178151
stack_trace_msg_hash_t * h,
179152
bool is_force);
180-
/**
181-
* @brief **check_profiler_regex()** check if the process name matches
182-
* the regular expression.
183-
*
184-
* @param ctx Profiler context(runtime environment status & configuration)
185-
* address
186-
* @param name Process name to be checked
187-
* @return true successfully passed the check, false otherwise
188-
*/
189-
bool check_profiler_regex(struct profiler_context *ctx, const char *name);
190153
// Check if the profiler is currently running.
191154
bool profiler_is_running(void);
192155
#endif /*DF_USER_PROFILE_COMMON_H */

agent/src/ebpf/user/tracer.c

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "elf.h"
3939
#include "load.h"
4040
#include "mem.h"
41+
#include "unwind_tracer.h"
4142
#include "extended/extended.h"
4243

4344
uint32_t k_version;
@@ -1707,6 +1708,10 @@ int set_feature_regex(int feature, const char *pattern)
17071708
}
17081709

17091710
cfg_feature_regex_array[feature].ok = true;
1711+
1712+
if (feature == FEATURE_PROFILE_ONCPU || feature == FEATURE_PROFILE_OFFCPU || feature == FEATURE_PROFILE_MEMORY) {
1713+
unwind_process_reload();
1714+
}
17101715
return 0;
17111716
}
17121717

agent/src/ebpf/user/unwind_tracer.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,17 @@ static bool requires_dwarf_unwind_table(int pid) {
174174
if (path == NULL) {
175175
return false;
176176
}
177-
char *exe_name = basename(path);
178-
// Java has JIT compiled code without DWARF info, not supported at the moment
179-
if (strcmp(exe_name, "java") == 0) {
177+
178+
// TBD: offcpu
179+
bool need_unwind = is_feature_matched(FEATURE_PROFILE_ONCPU, pid, path) || extended_require_dwarf(pid, path);
180+
if (!need_unwind) {
180181
free(path);
181182
return false;
182183
}
183184

184-
// TBD: offcpu
185-
bool need_unwind = (oncpu_profiler_enabled() && check_oncpu_profiler_regex(exe_name)) || extended_require_dwarf(pid, exe_name);
186-
if (!need_unwind) {
185+
char *exe_name = basename(path);
186+
// Java has JIT compiled code without DWARF info, not supported at the moment
187+
if (strcmp(exe_name, "java") == 0) {
187188
free(path);
188189
return false;
189190
}

agent/src/ebpf_dispatcher.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,8 @@ impl EbpfCollector {
849849
}
850850

851851
if !on_cpu.disabled {
852-
ebpf::set_profiler_regex(
852+
ebpf::set_feature_regex(
853+
ebpf::FEATURE_PROFILE_ONCPU,
853854
CString::new(on_cpu.regex.as_bytes())
854855
.unwrap()
855856
.as_c_str()
@@ -863,7 +864,8 @@ impl EbpfCollector {
863864
#[cfg(feature = "extended_profile")]
864865
{
865866
if !off_cpu.disabled {
866-
ebpf::set_offcpu_profiler_regex(
867+
ebpf::set_feature_regex(
868+
ebpf::FEATURE_PROFILE_OFFCPU,
867869
CString::new(off_cpu.regex.as_bytes())
868870
.unwrap()
869871
.as_c_str()
@@ -875,7 +877,8 @@ impl EbpfCollector {
875877
}
876878

877879
if !memory.disabled {
878-
ebpf::set_memory_profiler_regex(
880+
ebpf::set_feature_regex(
881+
ebpf::FEATURE_PROFILE_MEMORY,
879882
CString::new(memory.regex.as_bytes())
880883
.unwrap()
881884
.as_c_str()

0 commit comments

Comments
 (0)