Skip to content

Commit

Permalink
feat: agent - eBPF Profile support RT kernel (#8323)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinjiping authored Oct 17, 2024
1 parent 2e6cec2 commit c18dae4
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 10 deletions.
1 change: 1 addition & 0 deletions agent/src/ebpf/kernel/include/perf_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef enum {
ENABLE_IDX, /* Enable profiler sampling flag.
0: disable sampling; 1: enable sampling. */
MINBLOCK_TIME_IDX, /* The minimum blocking time, applied in the profiler extension.*/
RT_KERN, /* Indicates whether it is a real-time kernel.*/
PROFILER_CNT
} profiler_idx;

Expand Down
4 changes: 4 additions & 0 deletions agent/src/ebpf/user/profile/extended/extended.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ int __attribute__ ((weak)) extended_proc_event_handler(int pid,
{
return 0;
}

void __attribute__ ((weak)) extended_print_cp_tracer_status(void)
{
}
6 changes: 6 additions & 0 deletions agent/src/ebpf/user/profile/extended/extended.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ int extended_maps_set(struct bpf_tracer *tracer);
*/
int extended_proc_event_handler(int pid, const char *name,
enum proc_act_type type);

/**
* @brief **extended_print_cp_tracer_status()** Extended Profile runtime
* status output.
*/
void extended_print_cp_tracer_status(void);
#endif /* DF_PROFILE_EXT_H */
47 changes: 37 additions & 10 deletions agent/src/ebpf/user/profile/perf_profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ static FILE *folded_file;
char *flame_graph_start_time;
static char *flame_graph_end_time;

static void print_cp_tracer_status(struct bpf_tracer *t,
struct profiler_context *ctx);

/* Continuous Profiler debug related settings. */
static pthread_mutex_t cpdbg_mutex;
static bool cpdbg_enable;
Expand Down Expand Up @@ -142,7 +139,7 @@ static int release_profiler(struct bpf_tracer *tracer)
/* free all readers */
free_all_readers(tracer);

print_cp_tracer_status(tracer, &oncpu_ctx);
print_cp_tracer_status();

/* release object */
release_object(tracer->obj);
Expand Down Expand Up @@ -257,7 +254,7 @@ static void oncpu_reader_work(void *arg)
}

exit:
print_cp_tracer_status(t, &oncpu_ctx);
print_cp_tracer_status();

print_hash_stack_str(&oncpu_ctx.stack_str_hash);
/* free stack_str_hash */
Expand Down Expand Up @@ -463,9 +460,10 @@ int stop_continuous_profiler(void)
return (0);
}

static void print_cp_tracer_status(struct bpf_tracer *t,
struct profiler_context *ctx)
void output_profiler_status(struct bpf_tracer *t,
void *context)
{
struct profiler_context *ctx = context;
u64 alloc_b, free_b;
get_mem_stat(&alloc_b, &free_b);

Expand Down Expand Up @@ -497,7 +495,23 @@ static void print_cp_tracer_status(struct bpf_tracer *t,
ctx->state_map_name);
}

ebpf_info("\n\n----------------------------\nrecv envent:\t%lu\n"
u64 is_rt_kern = 0;
if (!bpf_table_get_value(t, ctx->state_map_name, RT_KERN,
(void *)&is_rt_kern)) {
ebpf_warning("Get map '%s' is_rt_kern failed.\n",
ctx->state_map_name);
}

u64 is_enabled = 0;
if (!bpf_table_get_value(t, ctx->state_map_name, ENABLE_IDX,
(void *)&is_enabled)) {
ebpf_warning("Get map '%s' is_enabled failed.\n",
ctx->state_map_name);
}

ebpf_info("\n\n----------------------------\n"
"Profiler Name: %s\nstate_map_name: %s\n"
"enabled: %lu\nrecv envent:\t%lu\n"
"process-cnt:\t%lu\nkern_lost:\t%lu perf_buf_lost_a:\t%lu, "
"perf_buf_lost_b:\t%lu process_lost_count:\t%lu "
"stack_table_data_miss:\t%lu\n"
Expand All @@ -510,18 +524,27 @@ static void print_cp_tracer_status(struct bpf_tracer *t,
" - sample_drop_cnt:\t%lu\n"
" - output_err_cnt:\t%lu\n"
" - iter_max_cnt:\t%lu\n"
" - is_rt_kern:\t%lu\n"
"----------------------------\n\n",
ctx->name, ctx->state_map_name, is_enabled,
atomic64_read(&t->recv), ctx->process_count,
atomic64_read(&t->lost), ctx->perf_buf_lost_a_count,
ctx->perf_buf_lost_b_count, ctx->perf_buf_lost_a_count,
ctx->perf_buf_lost_b_count, get_process_lost_count(ctx),
get_stack_table_data_miss_count(),
ctx->stackmap_clear_failed_count, ctx->stack_trace_err,
ctx->transfer_count,
((double)atomic64_read(&t->recv) /
(double)ctx->transfer_count), alloc_b, free_b,
alloc_b - free_b, output_count, sample_drop_cnt,
output_err_cnt, iter_max_cnt);
output_err_cnt, iter_max_cnt, is_rt_kern);
}

void print_cp_tracer_status(void)
{
if (profiler_tracer == NULL)
return;
output_profiler_status(profiler_tracer, (void *)&oncpu_ctx);
extended_print_cp_tracer_status();
}

static int cpdbg_sockopt_get(sockoptid_t opt, const void *conf, size_t size,
Expand Down Expand Up @@ -967,4 +990,8 @@ int disable_oncpu_profiler(void)
return 0;
}

void print_cp_tracer_status(void)
{
}

#endif /* AARCH64_MUSL */
2 changes: 2 additions & 0 deletions agent/src/ebpf/user/profile/perf_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,6 @@ void cpdbg_process(stack_trace_msg_t * msg);
int check_profiler_running_pid(int pid);
int check_profiler_is_running(void);
int write_profiler_running_pid(void);
void print_cp_tracer_status(void);
void output_profiler_status(struct bpf_tracer *t, void *context);
#endif /* DF_USER_PERF_PROFILER_H */
21 changes: 21 additions & 0 deletions agent/src/ebpf/user/profile/profile_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ void set_bpf_run_enabled(struct bpf_tracer *t, struct profiler_context *ctx,
enable_flag);
}

void set_bpf_rt_kern(struct bpf_tracer *t, struct profiler_context *ctx)
{
if (ctx->profiler_stop == 1)
return;

u64 rt_flag = 1;
if (bpf_table_set_value(t, ctx->state_map_name,
RT_KERN, &rt_flag) == false) {
ebpf_warning("%sprofiler state map update error."
"(%s rt_flag %lu) - %s\n",
ctx->tag, ctx->state_map_name, rt_flag,
strerror(errno));
return;
}

ebpf_info("%s%s() success, rt_flag:%d\n", ctx->tag, __func__, rt_flag);
}

int do_profiler_regex_config(const char *pattern, struct profiler_context *ctx)
{
if (*pattern == '\0') {
Expand Down Expand Up @@ -383,9 +401,12 @@ static void cleanup_stackmap(struct profiler_context *ctx, struct bpf_tracer *t,
}
}

/* *INDENT-OFF* */
static void __attribute__ ((__unused__))
print_profiler_status(struct profiler_context *ctx,
struct bpf_tracer *t, u64 iter_count)
/* *INDENT-ON* */

{
u64 alloc_b, free_b;
get_mem_stat(&alloc_b, &free_b);
Expand Down
1 change: 1 addition & 0 deletions agent/src/ebpf/user/profile/profile_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,5 @@ void push_and_release_stack_trace_msg(struct profiler_context *ctx,
bool check_profiler_regex(struct profiler_context *ctx, const char *name);
// Check if the profiler is currently running.
bool profiler_is_running(void);
void set_bpf_rt_kern(struct bpf_tracer *t, struct profiler_context *ctx);
#endif /*DF_USER_PROFILE_COMMON_H */
3 changes: 3 additions & 0 deletions agent/src/ebpf/user/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "elf.h"
#include "load.h"
#include "mem.h"
#include "profile/perf_profiler.h"

uint32_t k_version;
// Linux kernel major version, minor version, revision version, and revision number.
Expand Down Expand Up @@ -1618,6 +1619,8 @@ static int tracer_sockopt_get(sockoptid_t opt, const void *conf, size_t size,
}
}

print_cp_tracer_status();

return ETR_OK;
}

Expand Down

0 comments on commit c18dae4

Please sign in to comment.