From 53955465043c8ace9a7616e1478b34d8888b77da Mon Sep 17 00:00:00 2001 From: Jiping Yin Date: Wed, 7 Aug 2024 23:12:16 +0800 Subject: [PATCH] feat: agent - eBPF Java agent uses caching to reduce send frequency --- agent/src/ebpf/user/profile/java/config.h | 2 +- .../user/profile/java/jvm_symbol_collect.h | 1 - .../user/profile/java/symbol_collect_agent.c | 30 ++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/agent/src/ebpf/user/profile/java/config.h b/agent/src/ebpf/user/profile/java/config.h index 85747a3bea0..324c6c37daf 100644 --- a/agent/src/ebpf/user/profile/java/config.h +++ b/agent/src/ebpf/user/profile/java/config.h @@ -18,7 +18,7 @@ #define DF_JAVA_CONFIG_H // Maximum length of Java symbol information string -#define STRING_BUFFER_SIZE 2000 +#define STRING_BUFFER_SIZE 2048 /* * In Unix domain sockets, the maximum length of the path is defined by * the macro UNIX_PATH_MAX. For most systems (e.g., Linux), this maximum diff --git a/agent/src/ebpf/user/profile/java/jvm_symbol_collect.h b/agent/src/ebpf/user/profile/java/jvm_symbol_collect.h index d82fe5f0ced..3ffa01d12bd 100644 --- a/agent/src/ebpf/user/profile/java/jvm_symbol_collect.h +++ b/agent/src/ebpf/user/profile/java/jvm_symbol_collect.h @@ -19,7 +19,6 @@ #include "config.h" -#define STRING_BUFFER_SIZE 2000 #define UNIX_PATH_MAX 108 #define JAVA_ADDR_STR_SIZE 13 diff --git a/agent/src/ebpf/user/profile/java/symbol_collect_agent.c b/agent/src/ebpf/user/profile/java/symbol_collect_agent.c index dad28981a6b..7b91e117e82 100644 --- a/agent/src/ebpf/user/profile/java/symbol_collect_agent.c +++ b/agent/src/ebpf/user/profile/java/symbol_collect_agent.c @@ -63,6 +63,10 @@ char perf_log_socket_path[128]; int perf_map_socket_fd = -1; int perf_map_log_socket_fd = -1; + +// Cache symbols for batch sending +char g_symbol_buffer[STRING_BUFFER_SIZE * 4]; +int g_cached_bytes; jint close_files(void); #define _(e) \ @@ -301,6 +305,7 @@ void df_send_symbol(enum event_type type, const void *code_addr, return; } + int send_bytes; struct symbol_metadata *meta; char symbol_str[STRING_BUFFER_SIZE]; if (type == METHOD_UNLOAD) { @@ -315,8 +320,30 @@ void df_send_symbol(enum event_type type, const void *code_addr, meta = (struct symbol_metadata *)symbol_str; meta->len = strlen(symbol_str + sizeof(*meta)); meta->type = type; + send_bytes = meta->len + sizeof(*meta); pthread_mutex_lock(&g_df_lock); - send_msg(perf_map_socket_fd, symbol_str, meta->len + sizeof(*meta)); + if (replay_finish) { + if (g_cached_bytes > 0) { + send_msg(perf_map_socket_fd, g_symbol_buffer, + g_cached_bytes); + g_cached_bytes = 0; + } + send_msg(perf_map_socket_fd, symbol_str, send_bytes); + } else { + int buff_remain_bytes = + sizeof(g_symbol_buffer) - g_cached_bytes; + if (buff_remain_bytes >= send_bytes) { + memcpy(g_symbol_buffer + g_cached_bytes, symbol_str, + send_bytes); + g_cached_bytes += send_bytes; + } else { + send_msg(perf_map_socket_fd, g_symbol_buffer, + g_cached_bytes); + memcpy(g_symbol_buffer, symbol_str, send_bytes); + g_cached_bytes = send_bytes; + } + } + if (!replay_finish) replay_count++; pthread_mutex_unlock(&g_df_lock); @@ -505,6 +532,7 @@ Agent_OnAttach(JavaVM * vm, char *options, void *reserved) _(get_jvmti_env(vm, &jvmti)); enable_replay: + g_cached_bytes = 0; _(df_agent_config(options)); _(open_perf_map_log_file(getpid())); _(open_perf_map_file(getpid()));