Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.

Commit ac61285

Browse files
committed
API to run trace monitor on circuit events only.
This is the first step towards refactoring the profiling infra to make it easier to use and reduce overheads. The goal is not (yet) to build a production-quality profiler, but to expose existing facilities in a better way. This commit adds an option to run the trace monitor on circuit events only. This way it can be used to record circuit topology without incurring the overhead of monitoring all scheduler events.
1 parent a379379 commit ac61285

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/monitor/mod.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,17 @@ impl TraceMonitor {
135135
/// Attach trace monitor to a circuit. The monitor will register for
136136
/// both `CircuitEvent`s and `SchedulerEvent`s.
137137
pub fn attach(&self, circuit: &Circuit<()>, handler_name: &str) {
138-
TraceMonitorInternal::attach(self.0.clone(), circuit, handler_name);
138+
TraceMonitorInternal::attach(self.0.clone(), circuit, true, handler_name);
139+
}
140+
141+
/// Attach trace monitor to a circuit. The monitor will register for
142+
/// `CircuitEvent`s only. It will validate the circuit construction process
143+
/// and can be used to visualize the circuit (see
144+
/// [`Self::visualize_circuit`] and [`Self::visualize_circuit_annotate`],
145+
/// but it will not validate scheduler events, nor incur the associated
146+
/// overheads.
147+
pub fn attach_circuit_events(&self, circuit: &Circuit<()>, handler_name: &str) {
148+
TraceMonitorInternal::attach(self.0.clone(), circuit, false, handler_name);
139149
}
140150

141151
pub fn new<CE, SE>(circuit_error_handler: CE, scheduler_error_handler: SE) -> Self
@@ -186,7 +196,12 @@ pub struct TraceMonitorInternal {
186196
}
187197

188198
impl TraceMonitorInternal {
189-
fn attach(this: Arc<Mutex<Self>>, circuit: &Circuit<()>, handler_name: &str) {
199+
fn attach(
200+
this: Arc<Mutex<Self>>,
201+
circuit: &Circuit<()>,
202+
monitor_eval: bool,
203+
handler_name: &str,
204+
) {
190205
let this_clone = this.clone();
191206

192207
circuit.register_circuit_event_handler(handler_name, move |event| {
@@ -195,16 +210,18 @@ impl TraceMonitorInternal {
195210
(this.circuit_error_handler)(event, &e);
196211
});
197212
});
198-
circuit.register_scheduler_event_handler(handler_name, move |event| {
199-
// The `ClockEnd` event can be triggered by `CircuitHandle::drop()` while the
200-
// thread is being terminated, at which point the lock may be
201-
// poisoned. Just ignore the event in that case.
202-
if let Ok(mut this) = this_clone.lock() {
203-
let _ = this.scheduler_event(event).map_err(|e| {
204-
(this.scheduler_error_handler)(event, &e);
205-
});
206-
};
207-
});
213+
if monitor_eval {
214+
circuit.register_scheduler_event_handler(handler_name, move |event| {
215+
// The `ClockEnd` event can be triggered by `CircuitHandle::drop()` while the
216+
// thread is being terminated, at which point the lock may be
217+
// poisoned. Just ignore the event in that case.
218+
if let Ok(mut this) = this_clone.lock() {
219+
let _ = this.scheduler_event(event).map_err(|e| {
220+
(this.scheduler_error_handler)(event, &e);
221+
});
222+
};
223+
});
224+
}
208225
}
209226

210227
/// Create a new trace monitor with user-provided event handlers.

0 commit comments

Comments
 (0)