|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::fs::{self, File}; |
| 5 | +use std::io::Write; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use std::sync::atomic::{AtomicU64, Ordering}; |
| 8 | +use std::sync::Arc; |
| 9 | +use std::thread; |
| 10 | +use std::time::{Duration, SystemTime}; |
| 11 | + |
| 12 | +use pprof::protos::Message; |
| 13 | + |
| 14 | +mod error; |
| 15 | +pub use error::ProfilerError; |
| 16 | + |
| 17 | +/// Save a flamegraph to the specified path |
| 18 | +fn save_flamegraph( |
| 19 | + report: &pprof::Report, |
| 20 | + path: &Path, |
| 21 | + options: &mut pprof::flamegraph::Options, |
| 22 | +) -> Result<(), ProfilerError> { |
| 23 | + let file = File::create(path)?; |
| 24 | + |
| 25 | + report |
| 26 | + .flamegraph_with_options(file, options) |
| 27 | + .map_err(|e| ProfilerError::FlamegraphCreationError(e.to_string()))?; |
| 28 | + |
| 29 | + tracing::info!("✅ Generated flamegraph: {:?}", path); |
| 30 | + Ok(()) |
| 31 | +} |
| 32 | + |
| 33 | +/// Save a protobuf profile to the specified path |
| 34 | +fn save_protobuf(profile: &pprof::protos::Profile, path: &Path) -> Result<(), ProfilerError> { |
| 35 | + // Try write_to_bytes first |
| 36 | + match profile.write_to_bytes() { |
| 37 | + Ok(bytes) => { |
| 38 | + let mut file = File::create(path)?; |
| 39 | + file.write_all(&bytes)?; |
| 40 | + } |
| 41 | + Err(e) => { |
| 42 | + // Alternative approach: try direct file writing |
| 43 | + tracing::info!( |
| 44 | + "⚠️ Failed to serialize profile: {}, trying direct writer", |
| 45 | + e |
| 46 | + ); |
| 47 | + |
| 48 | + let mut file = File::create(path)?; |
| 49 | + profile |
| 50 | + .write_to_writer(&mut file) |
| 51 | + .map_err(|e| ProfilerError::SerializationError(e.to_string()))?; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + tracing::info!("✅ Generated protobuf profile: {:?}", path); |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | + |
| 59 | +/// Generate a unique filename with timestamp and counter |
| 60 | +fn generate_filename( |
| 61 | + base_path: &str, |
| 62 | + prefix: &str, |
| 63 | + extension: &str, |
| 64 | + counter: u64, |
| 65 | +) -> Result<PathBuf, ProfilerError> { |
| 66 | + let timestamp = SystemTime::now() |
| 67 | + .duration_since(SystemTime::UNIX_EPOCH)? |
| 68 | + .as_secs() |
| 69 | + .to_string(); |
| 70 | + |
| 71 | + let filename = format!("{}-{}-{}.{}", prefix, timestamp, counter, extension); |
| 72 | + Ok(Path::new(base_path).join(filename)) |
| 73 | +} |
| 74 | + |
| 75 | +/// Process a single profiling report |
| 76 | +fn process_profiling_report( |
| 77 | + guard: &pprof::ProfilerGuard<'_>, |
| 78 | + path: &str, |
| 79 | + counter: u64, |
| 80 | + options: &mut pprof::flamegraph::Options, |
| 81 | +) -> Result<(), ProfilerError> { |
| 82 | + let report = guard |
| 83 | + .report() |
| 84 | + .build() |
| 85 | + .map_err(|e| ProfilerError::ReportError(e.to_string()))?; |
| 86 | + |
| 87 | + // Generate flamegraph |
| 88 | + let flamegraph_path = generate_filename(path, "flamegraph", "svg", counter)?; |
| 89 | + if let Err(e) = save_flamegraph(&report, &flamegraph_path, options) { |
| 90 | + tracing::error!("Failed to save flamegraph: {}", e); |
| 91 | + // Continue execution to try saving protobuf |
| 92 | + } |
| 93 | + |
| 94 | + // Generate protobuf profile |
| 95 | + match report.pprof() { |
| 96 | + Ok(profile) => { |
| 97 | + let proto_path = generate_filename(path, "profile", "pb", counter)?; |
| 98 | + if let Err(e) = save_protobuf(&profile, &proto_path) { |
| 99 | + tracing::error!("Failed to save protobuf: {}", e); |
| 100 | + } |
| 101 | + } |
| 102 | + Err(e) => { |
| 103 | + tracing::error!("Failed to generate pprof profile: {}", e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
| 109 | + |
| 110 | +fn setup(path: String, frequency: i32, interval: u64, name: String) -> Result<(), ProfilerError> { |
| 111 | + // Ensure the profiling directory exists |
| 112 | + let profile_dir = Path::new(&path); |
| 113 | + if !profile_dir.exists() { |
| 114 | + fs::create_dir_all(profile_dir)?; |
| 115 | + } |
| 116 | + |
| 117 | + // Create a background thread for continuous profiling |
| 118 | + let path_clone = path.clone(); |
| 119 | + thread::spawn(move || { |
| 120 | + // Wait a bit for the application to start |
| 121 | + thread::sleep(Duration::from_secs(10)); |
| 122 | + tracing::info!("🔍 Starting continuous profiling..."); |
| 123 | + |
| 124 | + // Counter for tracking report generation |
| 125 | + let counter = Arc::new(AtomicU64::new(0)); |
| 126 | + |
| 127 | + // Create a separate thread for continuous data collection |
| 128 | + thread::spawn(move || { |
| 129 | + // Start continuous profiling |
| 130 | + let guard = match pprof::ProfilerGuardBuilder::default() |
| 131 | + .frequency(frequency) |
| 132 | + .blocklist(&["libc", "libgcc", "pthread", "vdso"]) |
| 133 | + .build() |
| 134 | + { |
| 135 | + Ok(guard) => guard, |
| 136 | + Err(e) => { |
| 137 | + tracing::error!("Failed to initialize profiler: {}", e); |
| 138 | + return; |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + tracing::info!("📊 Continuous profiling active"); |
| 143 | + let mut options = pprof::flamegraph::Options::default(); |
| 144 | + options.title = name; |
| 145 | + |
| 146 | + // Create a timer thread to periodically save reports |
| 147 | + thread::spawn(move || { |
| 148 | + loop { |
| 149 | + // Sleep for `interval` seconds before saving reports |
| 150 | + thread::sleep(Duration::from_secs(interval)); |
| 151 | + |
| 152 | + let current_counter = counter.fetch_add(1, Ordering::Relaxed); |
| 153 | + |
| 154 | + tracing::info!("💾 Saving profiling snapshot #{}...", current_counter); |
| 155 | + |
| 156 | + if let Err(e) = |
| 157 | + process_profiling_report(&guard, &path_clone, current_counter, &mut options) |
| 158 | + { |
| 159 | + tracing::error!("Error processing profiling report: {}", e); |
| 160 | + } |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + // Keep profiling thread alive |
| 165 | + loop { |
| 166 | + thread::sleep(Duration::from_secs(3600)); |
| 167 | + } |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + Ok(()) |
| 172 | +} |
| 173 | + |
| 174 | +/// Sets up continuous CPU profiling with flamegraph and protobuf output. |
| 175 | +/// |
| 176 | +/// # Arguments |
| 177 | +/// |
| 178 | +/// * `path` - Directory where profiling data will be stored |
| 179 | +/// * `frequency` - Sampling frequency in Hz |
| 180 | +/// * `interval` - Time between saving reports in seconds |
| 181 | +/// * `name` - Optional service name for labeling profiles |
| 182 | +/// |
| 183 | +/// # Errors |
| 184 | +/// |
| 185 | +/// Returns `ProfilerError` if directory creation fails |
| 186 | +/// |
| 187 | +/// # Examples |
| 188 | +/// |
| 189 | +/// ``` |
| 190 | +/// profiler::setup_profiling( |
| 191 | +/// "/opt/profiling/my-service".to_string(), |
| 192 | +/// 150, |
| 193 | +/// 120, |
| 194 | +/// Some("My Service".to_string()) |
| 195 | +/// )?; |
| 196 | +/// ``` |
| 197 | +pub fn setup_profiling( |
| 198 | + path: String, |
| 199 | + frequency: i32, |
| 200 | + interval: u64, |
| 201 | + name: Option<String>, |
| 202 | +) -> Result<(), ProfilerError> { |
| 203 | + tracing::info!("🔍 Setting up profiling..."); |
| 204 | + setup( |
| 205 | + path, |
| 206 | + frequency, |
| 207 | + interval, |
| 208 | + name.unwrap_or("Service".to_string()), |
| 209 | + ) |
| 210 | +} |
0 commit comments