[Profiler] Add per-session profiling controls to Python and Rust frontends (#57875)

Signed-off-by: czhu <conway.zhu@cohere.com>
Signed-off-by: Conway Zhu <conway.zhu@cohere.com>
Co-authored-by: aoshen02 <aoshen@inferact.ai>
This commit is contained in:
czhu-cohere
2026-10-06 05:21:19 +00:00
committed by GitHub
co-authored by aoshen02
parent 9a1f6fd4cb
commit 6fddc9b21e
25 changed files with 1497 additions and 164 deletions
+31 -3
View File
@@ -9,7 +9,7 @@ use futures::future::join_all;
use itertools::Itertools;
use serde::Serialize;
use serde_json::Value as JsonValue;
use tokio::sync::mpsc;
use tokio::sync::{Mutex, mpsc};
use tokio_util::task::AbortOnDropHandle;
use tracing::{debug, info, trace};
@@ -283,6 +283,9 @@ pub struct EngineCoreClient {
inner: Arc<ClientInner>,
coordinator: Option<CoordinatorHandle>,
abort_tx: mpsc::UnboundedSender<AbortRequest>,
/// Whether a profiling session is awaiting an explicit stop. Held across each
/// start/stop utility call so a stale stop reply cannot clear a newer session.
profile_active: Mutex<bool>,
/// Runtime used to send messages to the engine and drive all background tasks.
runtime: BackgroundShutdownRuntime,
@@ -431,6 +434,7 @@ impl EngineCoreClient {
inner,
coordinator,
abort_tx,
profile_active: Mutex::new(false),
runtime,
output_task,
dispatcher_task,
@@ -963,14 +967,38 @@ impl EngineCoreClient {
}
/// Start profiling the engine.
pub async fn start_profile(&self, profile_prefix: Option<&str>) -> Result<()> {
self.call_utility::<(), _>("profile", (true, profile_prefix)).await?;
pub async fn start_profile(
&self,
profile_prefix: Option<&str>,
delay_iterations: Option<u64>,
max_iterations: Option<u64>,
) -> Result<()> {
let mut active = self.profile_active.lock().await;
if *active {
return Err(Error::ProfileAlreadyActive);
}
// A cancelled start may still reach the engine, so only a reported
// failure clears the session; otherwise a stop is needed.
*active = true;
if let Err(error) = self
.call_utility::<(), _>(
"profile",
(true, profile_prefix, delay_iterations, max_iterations),
)
.await
{
*active = false;
return Err(error);
}
Ok(())
}
/// Stop profiling the engine.
pub async fn stop_profile(&self, profile_prefix: Option<&str>) -> Result<()> {
let mut active = self.profile_active.lock().await;
self.call_utility::<(), _>("profile", (false, profile_prefix)).await?;
*active = false;
Ok(())
}
+2
View File
@@ -83,6 +83,8 @@ pub enum Error {
ClientClosed { message: String },
#[error("request output stream for `{request_id}` closed unexpectedly")]
RequestStreamClosed { request_id: String },
#[error("profiling session is already active")]
ProfileAlreadyActive,
#[error("utility call `{method}` failed (call_id={call_id}): {message}")]
UtilityCallFailed {
method: String,