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

[FR] 针对deepflow-agent开放profile的能力的咨询 #9114

Open
2 of 3 tasks
zcl1115 opened this issue Feb 17, 2025 · 4 comments
Open
2 of 3 tasks

[FR] 针对deepflow-agent开放profile的能力的咨询 #9114

zcl1115 opened this issue Feb 17, 2025 · 4 comments
Assignees
Labels

Comments

@zcl1115
Copy link

zcl1115 commented Feb 17, 2025

Search before asking

  • I had searched in the issues and found no similar feature requirement.

Description

我这边阅读了deepflow-agent的代码,发现deepflow-agent的ebpf相关的profile能力是在Trident->AgentComponents->EbpfCollector中定义并执行的,在EbpfCollector中会去检测on_cpu_profile是否启动,在启动的情况下会调用start_continuous_profiler()开始持续采样并通过ebpf_on_cpu_callback()回调函数进行处理,然后在stop_continuous_profiler()函数中关闭持续采样,并没有对外提供能力的接口;
我们想要改造deepflow-agent,希望能够实现deepflow-server跟agent建连的rpc通道去下发命令,在保证调用deepflow-agent串行的情况下,能够实现动态的调用profile的能力,类似如下的代码所示,想问下是否具有可行性,以及这种由server触发的代码应该放在agent的哪里被执行

fn profile_pid_for_duration(pid: i32, duration_secs: u64) -> Result<(), &'static str> {
    // 动态设置正则表达式,仅采样指定 PID 的数据
    let regex = format!("^{}", pid); // 匹配目标 PID
    unsafe {
        set_profiler_regex(
            CString::new(regex.as_bytes())
                .map_err(|_| "Failed to create CString")?
                .as_c_str()
                .as_ptr(),
        );
    }
    // 启动 eBPF profiler
    let frequency = 99; // 设置采样频率(可根据需求调整)
    let max_java_file_limit = 1024 * 1024 * 10; // Java 符号文件限制
    let java_refresh_interval = 10; // 刷新间隔(秒)
    unsafe {
        if start_continuous_profiler(
            frequency,
            max_java_file_limit,
            java_refresh_interval,
            EbpfCollector::ebpf_on_cpu_callback,
        ) != 0
        {
            return Err("Failed to start continuous profiler");
        }
    }
    // 定时结束采样
    thread::spawn(move || {
        thread::sleep(Duration::from_secs(duration_secs));
        // 停止 eBPF profiler
        unsafe {
            ebpf::stop_continuous_profiler(); // 停止内核态采样
        }
        println!("Stopped profiling for PID {}", pid);
    });
    Ok(())
}

Use case

No response

Related issues

#8550

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@rvql
Copy link
Contributor

rvql commented Feb 18, 2025

这里有单独调用 profiler 的方法供你参考
没太理解你所说“动态调用 profile” 的含义;如果希望对某些进程开启持续剖析,配置process_matcher就可以了

@zcl1115
Copy link
Author

zcl1115 commented Feb 18, 2025

这里有单独调用 profiler 的方法供你参考 没太理解你所说“动态调用 profile” 的含义;如果希望对某些进程开启持续剖析,配置process_matcher就可以了

嗯,有看过这部分代码,
我们是想要提供一种方式来让agent能够开放出profile的能力,比如在特定的时刻(比如告警后)针对某些进程触发profile这样的能力,
如果配置process_matcher的话,就会是一直持续性的profile,跟我们设想的不符,
如果是频繁修改agent-config的话,也会比较麻烦,
所以希望能够提供一种方式来触发profile的调用,然后通过原有的数据上报通道来上报信息这样子,所以是想看看server控制agent执行命令是大体上是怎么实现的,能够控制agent开启一段时间(比如1、2分钟这样子)的profiler
最后就是想问下profile能否针对特定pid触发?

@rvql
Copy link
Contributor

rvql commented Feb 25, 2025

这里有单独调用 profiler 的方法供你参考 没太理解你所说“动态调用 profile” 的含义;如果希望对某些进程开启持续剖析,配置process_matcher就可以了

嗯,有看过这部分代码, 我们是想要提供一种方式来让agent能够开放出profile的能力,比如在特定的时刻(比如告警后)针对某些进程触发profile这样的能力, 如果配置process_matcher的话,就会是一直持续性的profile,跟我们设想的不符, 如果是频繁修改agent-config的话,也会比较麻烦, 所以希望能够提供一种方式来触发profile的调用,然后通过原有的数据上报通道来上报信息这样子,所以是想看看server控制agent执行命令是大体上是怎么实现的,能够控制agent开启一段时间(比如1、2分钟这样子)的profiler

目前采集器只支持通过 process_matcher 一种方法进行配置,由 process-listener 线程根据匹配的结果触发预先配置的回调函数开启对应进程的剖析

最后就是想问下profile能否针对特定pid触发?

不支持直接配置 pid,可支持的配置参考

# enum_options: [process_name, cmdline, cmdline_with_args, parent_process_name, tag]

@zcl1115
Copy link
Author

zcl1115 commented Mar 4, 2025

这里有单独调用 profiler 的方法供你参考 没太理解你所说“动态调用 profile” 的含义;如果希望对某些进程开启持续剖析,配置process_matcher就可以了

你好,想问下我直接执行了这个单独调佣profiler的main方法,然后修改了set_profiler_regex只严格匹配一个set_profiler_regex(
CString::new("^deepflow-agent$".as_bytes())
.unwrap()
.as_c_str()
.as_ptr(),
);,但是还是在callback的回调中看到有很多额外的采样数据,这是正常的吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants