-
Notifications
You must be signed in to change notification settings - Fork 24
add cli options to generate perfetto trace #246
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 2 commits
031fde1
751e25d
7bb60a2
f29d6a0
fc5bd46
bb0b7b2
745e3e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use anyhow::{Result, anyhow}; | ||
| use backtrace::Backtrace; | ||
| use chrono::{DateTime, Local}; | ||
| use flexi_logger::{FileSpec, LogSpecification, Logger}; | ||
| use std::ffi::OsString; | ||
| use std::panic::{self, PanicHookInfo}; | ||
|
|
@@ -8,7 +9,10 @@ use std::sync::Arc; | |
| use cursive::view::Resizable; | ||
|
|
||
| use crate::{ | ||
| interpreter::{ClickHouse, Context, ContextArc, options}, | ||
| interpreter::{ | ||
| ClickHouse, Context, ContextArc, Query, fetch_and_populate_perfetto_trace, | ||
| fetch_server_perfetto_sources, options, perfetto::PerfettoTraceBuilder, | ||
| }, | ||
| view::Navigation, | ||
| }; | ||
|
|
||
|
|
@@ -39,6 +43,104 @@ fn panic_hook(info: &PanicHookInfo<'_>) { | |
| ); | ||
| } | ||
|
|
||
| fn write_perfetto_trace(path: &str, data: Vec<u8>) -> Result<()> { | ||
| std::fs::write(path, &data)?; | ||
| println!("Perfetto trace exported to {}", path); | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn maybe_run_cli_perfetto_export( | ||
| options: &options::ChDigOptions, | ||
| clickhouse: &Arc<ClickHouse>, | ||
| ) -> Result<bool> { | ||
| let Some(cmd) = options.perfetto_command() else { | ||
| return Ok(false); | ||
| }; | ||
|
|
||
| let output = cmd | ||
| .output | ||
| .clone() | ||
| .unwrap_or_else(|| "/tmp/chdig_perfetto.pftrace".to_string()); | ||
|
Owner
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. Why this is not the default for cli option?
Author
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. It's "generic" option now, so hardcoding perfetto naming seems wrong. If it |
||
|
|
||
| let mut perfetto_options = options.perfetto.clone(); | ||
|
|
||
| perfetto_options.aggregated_zookeeper_log = true; | ||
| perfetto_options.query_metric_log = true; | ||
| perfetto_options.asynchronous_metric_log = true; | ||
|
|
||
| if let Some(query_id) = cmd.query_id.as_deref() { | ||
| let scope = clickhouse.get_perfetto_query_scope(query_id).await?; | ||
| let end_time = scope.end + chrono::TimeDelta::seconds(1); | ||
| let query_block = clickhouse | ||
| .get_queries_for_perfetto(scope.start, end_time) | ||
| .await?; | ||
| let query_ids = scope.query_ids; | ||
| let query_ids_set = std::collections::HashSet::<&String>::from_iter(query_ids.iter()); | ||
| let mut queries = Vec::new(); | ||
|
|
||
| for i in 0..query_block.row_count() { | ||
| match Query::from_clickhouse_block(&query_block, i, false) { | ||
| Ok(q) if query_ids_set.contains(&q.query_id) => queries.push(q), | ||
| Ok(_) => {} | ||
| Err(e) => log::warn!("Perfetto: failed to parse query row {}: {}", i, e), | ||
| } | ||
| } | ||
|
|
||
| let mut builder = PerfettoTraceBuilder::new( | ||
| perfetto_options.per_server, | ||
| perfetto_options.text_log_android, | ||
| ); | ||
|
Owner
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. This should be passed from command line
Author
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. I'm not sure about those two. IMO, android style logs should be enabled/used by default. Per server track split also make sense, compared to not have it. |
||
| builder.add_queries(&queries); | ||
| fetch_and_populate_perfetto_trace( | ||
| clickhouse, | ||
| &mut builder, | ||
| &perfetto_options, | ||
| Some(&query_ids), | ||
| scope.start, | ||
| end_time, | ||
| ) | ||
| .await; | ||
| write_perfetto_trace(&output, builder.build())?; | ||
| return Ok(true); | ||
| } | ||
|
|
||
| if cmd.server { | ||
| let start: DateTime<Local> = cmd.start.clone().into(); | ||
| let end: DateTime<Local> = cmd.end.clone().into(); | ||
| let query_block = clickhouse.get_queries_for_perfetto(start, end).await?; | ||
| let mut queries = Vec::new(); | ||
|
|
||
| for i in 0..query_block.row_count() { | ||
| match Query::from_clickhouse_block(&query_block, i, false) { | ||
| Ok(q) => queries.push(q), | ||
| Err(e) => log::warn!("Perfetto: failed to parse query row {}: {}", i, e), | ||
| } | ||
| } | ||
|
|
||
| let end_time = end + chrono::TimeDelta::seconds(1); | ||
| let mut builder = PerfettoTraceBuilder::new( | ||
| perfetto_options.per_server, | ||
| perfetto_options.text_log_android, | ||
| ); | ||
| builder.add_queries(&queries); | ||
| fetch_and_populate_perfetto_trace( | ||
| clickhouse, | ||
| &mut builder, | ||
| &perfetto_options, | ||
| None, | ||
| start, | ||
| end_time, | ||
| ) | ||
| .await; | ||
| fetch_server_perfetto_sources(clickhouse, &mut builder, &perfetto_options, start, end_time) | ||
| .await; | ||
| write_perfetto_trace(&output, builder.build())?; | ||
| return Ok(true); | ||
| } | ||
|
|
||
| Ok(false) | ||
| } | ||
|
|
||
| pub async fn chdig_main_async<I, T>(itr: I) -> Result<()> | ||
| where | ||
| I: IntoIterator<Item = T>, | ||
|
|
@@ -61,6 +163,10 @@ where | |
| // panic hook will clear the screen). | ||
| let clickhouse = Arc::new(ClickHouse::new(options.clickhouse.clone()).await?); | ||
|
|
||
| if maybe_run_cli_perfetto_export(&options, &clickhouse).await? { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let server_warnings = match clickhouse.get_warnings().await { | ||
| Ok(w) => w, | ||
| Err(e) => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.