|
| 1 | +use crate::config::Config; |
| 2 | +use crate::error::{CliError, Result}; |
| 3 | +use crate::executor; |
| 4 | +use crate::tools::{ExecutionResult, Tool}; |
| 5 | +use crate::ui; |
| 6 | +use dialoguer::Input; |
| 7 | +use std::env; |
| 8 | +use std::process::Command; |
| 9 | + |
| 10 | +pub struct FeProfilerTool; |
| 11 | + |
| 12 | +impl FeProfilerTool { |
| 13 | + /// Prompt user for profile duration and return the duration value |
| 14 | + /// This method can be called before tool execution to get user input |
| 15 | + pub fn prompt_duration() -> Result<u32> { |
| 16 | + let input: String = Input::with_theme(&dialoguer::theme::ColorfulTheme::default()) |
| 17 | + .with_prompt("Enter collection duration in seconds") |
| 18 | + .with_initial_text("10") |
| 19 | + .interact_text() |
| 20 | + .map_err(|e| CliError::InvalidInput(format!("Duration input failed: {e}")))?; |
| 21 | + |
| 22 | + let duration_str = if input.trim().is_empty() { |
| 23 | + "10" |
| 24 | + } else { |
| 25 | + input.trim() |
| 26 | + }; |
| 27 | + |
| 28 | + match duration_str.parse::<u32>() { |
| 29 | + Ok(val) if val > 0 && val <= 300 => Ok(val), |
| 30 | + Ok(_) => { |
| 31 | + ui::print_warning("Duration must be between 1 and 300 seconds!"); |
| 32 | + ui::print_info("Hint: Enter a number between 1-300, e.g., 25"); |
| 33 | + Err(CliError::GracefulExit) |
| 34 | + } |
| 35 | + Err(_) => { |
| 36 | + ui::print_warning("Please enter a valid number!"); |
| 37 | + ui::print_info("Hint: Enter a number between 1-300, e.g., 25"); |
| 38 | + Err(CliError::GracefulExit) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Execute the profiler with a specific duration |
| 44 | + pub fn execute_with_duration(&self, config: &Config, duration: u32) -> Result<ExecutionResult> { |
| 45 | + let doris_config = crate::config_loader::load_config()?; |
| 46 | + |
| 47 | + let fe_install_dir = doris_config |
| 48 | + .fe_install_dir |
| 49 | + .as_ref() |
| 50 | + .or(Some(&doris_config.install_dir)) |
| 51 | + .ok_or_else(|| CliError::ConfigError("FE install directory not found".to_string()))?; |
| 52 | + |
| 53 | + let profile_script = fe_install_dir.join("bin").join("profile_fe.sh"); |
| 54 | + |
| 55 | + if !profile_script.exists() { |
| 56 | + return Err(CliError::ConfigError(format!( |
| 57 | + "profile_fe.sh not found at {}. Please ensure Doris version is 2.1.4+", |
| 58 | + profile_script.display() |
| 59 | + ))); |
| 60 | + } |
| 61 | + |
| 62 | + let mut command = Command::new("bash"); |
| 63 | + command.arg(&profile_script); |
| 64 | + command.env("PROFILE_SECONDS", duration.to_string()); |
| 65 | + |
| 66 | + executor::execute_command_with_timeout(&mut command, self.name(), config)?; |
| 67 | + |
| 68 | + let message = format!("Flame graph generated successfully (duration: {duration}s)."); |
| 69 | + |
| 70 | + Ok(ExecutionResult { |
| 71 | + output_path: std::path::PathBuf::new(), |
| 72 | + message, |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl Tool for FeProfilerTool { |
| 78 | + fn name(&self) -> &str { |
| 79 | + "fe-profiler" |
| 80 | + } |
| 81 | + |
| 82 | + fn description(&self) -> &str { |
| 83 | + "Generate flame graph for FE performance analysis using async-profiler" |
| 84 | + } |
| 85 | + |
| 86 | + fn execute(&self, config: &Config, _pid: u32) -> Result<ExecutionResult> { |
| 87 | + let profile_seconds = if env::var("PROFILE_SECONDS").is_ok() { |
| 88 | + env::var("PROFILE_SECONDS") |
| 89 | + .unwrap() |
| 90 | + .parse::<u32>() |
| 91 | + .unwrap_or(10) |
| 92 | + } else { |
| 93 | + Self::prompt_duration()? |
| 94 | + }; |
| 95 | + |
| 96 | + self.execute_with_duration(config, profile_seconds) |
| 97 | + } |
| 98 | + |
| 99 | + fn requires_pid(&self) -> bool { |
| 100 | + false // FE profiler doesn't need PID as it uses profile_fe.sh script |
| 101 | + } |
| 102 | +} |
0 commit comments