-
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 3 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 { | ||
|
||
| continue; | ||
|
||
| }; | ||
|
|
||
| if let psh_proto::perf_data_proto::perf_event::EventType::MmapEvent(event)= event_type{ | ||
| let Some(filename) = &event.filename else { | ||
| continue; | ||
|
||
| }; | ||
|
|
||
| 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? | ||
|
||
| }); | ||
| data.push(Data { data_type: Some(data_type) }); | ||
| } | ||
| } | ||
| let dat = Data { | ||
| data_type: Some(DataType::PerfData(perf_data)), | ||
| }; | ||
| data.push(dat); | ||
|
Member
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?