-
Notifications
You must be signed in to change notification settings - Fork 3
Basic support profiling task #169
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,11 +32,16 @@ use log::log_init; | |
use mimalloc::MiMalloc; | ||
use nix::unistd::geteuid; | ||
use opentelemetry_otlp::ExportConfig; | ||
use psh_proto::HeartbeatReq; | ||
use runtime::{Task, TaskRuntime}; | ||
use psh_proto::{ | ||
HeartbeatReq, PerfDataProto, | ||
export_data_req::{Data, data::DataType}, | ||
}; | ||
use runtime::{TaskRuntime, WasmTask}; | ||
use services::rpc::RpcClient; | ||
use tokio::{runtime::Runtime, try_join}; | ||
|
||
use self::services::{rpc::WhichTask, sampling::Profiler}; | ||
|
||
#[global_allocator] | ||
static GLOBAL: MiMalloc = mimalloc::MiMalloc; | ||
|
||
|
@@ -86,7 +91,7 @@ fn main() -> Result<()> { | |
let task_rt = TaskRuntime::new()?; | ||
|
||
if let Some(args) = wasm_with_args { | ||
let task = Task { | ||
let task = WasmTask { | ||
id: None, | ||
wasm_component: fs::read(&args[0])?, | ||
wasm_component_args: args, | ||
|
@@ -143,14 +148,70 @@ async fn async_tasks(remote_cfg: RemoteConfig, mut task_rt: TaskRuntime) -> Resu | |
loop { | ||
let idle = task_rt.is_idle(); | ||
if idle { | ||
if let Some(mut task) = client.get_task(instance_id.clone()).await? { | ||
let task_id = task | ||
.id | ||
.as_ref() | ||
.map(|it| it.to_string()) | ||
.expect("No task id provided"); | ||
task.wasm_component_args.insert(0, task_id); | ||
task_rt.schedule(task)? | ||
if let Some(task) = client.get_task(instance_id.clone()).await? { | ||
match task { | ||
WhichTask::Wasm(mut task) => { | ||
let task_id = task | ||
.id | ||
.as_ref() | ||
.map(|it| it.to_string()) | ||
.expect("No task id provided"); | ||
task.wasm_component_args.insert(0, task_id); | ||
task_rt.schedule(task)?; | ||
} | ||
WhichTask::Profiling(profiling_task) => { | ||
let mut profiler = Profiler::new( | ||
profiling_task.process, | ||
profiling_task.mmap_pages as _, | ||
profiling_task.overflow_by, | ||
profiling_task.stack_depth, | ||
)?; | ||
let task_time_slice = { | ||
let delta = profiling_task.end_time.timestamp_millis() | ||
- Utc::now().timestamp_millis(); | ||
delta.max(0) as u64 | ||
}; | ||
|
||
let perf_data = tokio::task::spawn_blocking( | ||
move || -> anyhow::Result<PerfDataProto> { | ||
profiler.enable()?; | ||
std::thread::sleep(Duration::from_millis(task_time_slice)); | ||
profiler.disable()?; | ||
Ok(profiler.perf_data_proto()) | ||
}, | ||
) | ||
.await??; | ||
if let Some(task_id) = profiling_task.id { | ||
let mut data = vec![]; | ||
for ele in &perf_data.events { | ||
let Some(event_type) = &ele.event_type else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 读取ELF文件为什么需要event_type? 这个不是强关联吧? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文件名是在MmapEvent里面的,所以需要从里面获取文件名。 |
||
continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这些地方需要打印错误日志,帮助后续的排查。默认静默掉这些错误,排查起来比较麻烦,可能是网络问题,也可能是参数错误。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. event_type 在proto里面是用 oneof定义, |
||
}; | ||
|
||
if let psh_proto::perf_data_proto::perf_event::EventType::MmapEvent(event)= event_type{ | ||
let Some(filename) = &event.filename else { | ||
continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
}; | ||
|
||
let data_type = DataType::ElfFile(psh_proto::ElfFile { | ||
filename: filename.to_owned(), | ||
build_id: event.build_id.clone(), | ||
arch: std::env::consts::ARCH.to_string(), | ||
bytes: tokio::fs::read(filename).await? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 应该是先做一些校验,然后在判断是不是要读取文件内容,而不是直接读取。比如文件不存在怎么处理, build_id不一致怎么处理,用户是否允许读取某个文件/目录等等。 |
||
}); | ||
data.push(Data { data_type: Some(data_type) }); | ||
} | ||
} | ||
let dat = Data { | ||
data_type: Some(DataType::PerfData(perf_data)), | ||
}; | ||
data.push(dat); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 采样和上传ELF文件两个可以分开,放在一起反而不方便。不是所有的ELF文件都需要上传。 |
||
client | ||
.export_data(psh_proto::ExportDataReq { task_id, data }) | ||
.await?; | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ | |
|
||
pub mod host_info; | ||
pub mod rpc; | ||
pub mod sampling; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ele
->event
?