diff --git a/Cargo.lock b/Cargo.lock index edf87d21b..0af80a080 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -259,6 +259,8 @@ dependencies = [ "plurality 0.2.4", "scopeguard", "seismograph", + "seismograph_protocol", + "seismograph_runtime", "static_assertions", "testing_aids", "thread_aware", diff --git a/crates/anyspawn/tests/arty.rs b/crates/anyspawn/tests/arty.rs index b4a27d458..6210702cf 100644 --- a/crates/anyspawn/tests/arty.rs +++ b/crates/anyspawn/tests/arty.rs @@ -13,7 +13,7 @@ use std::time::Duration; use anyspawn::{JoinHandle, Spawner}; use arty::runtime::hook::Panic; -use arty::runtime::{Runtime, WorkersPolicy}; +use arty::runtime::{MonitorPolicy, Runtime, WorkersPolicy}; use futures::future::join_all; use thread_aware::{Thread, ThreadAware}; @@ -31,7 +31,11 @@ impl ThreadAware for RelocationProbe { } fn runtime_and_spawner() -> (Runtime, Spawner) { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); let spawner = Spawner::new_arty(scheduler); (runtime, spawner) @@ -83,7 +87,11 @@ fn arty_relocation_round_trip_rebinds_to_destination_worker() { if many_cpus::SystemHardware::current().processors().len() < 2 { return; } - let runtime_a = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime_a = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); let (mut spawner, source, destination) = runtime_a .scheduler() .block_on(async |cx| { @@ -97,7 +105,11 @@ fn arty_relocation_round_trip_rebinds_to_destination_worker() { .unwrap(); let original = spawner.clone(); - let runtime_b = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime_b = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let foreign = runtime_b.scheduler().block_on(async |cx| cx.thread().clone()).unwrap(); spawner.relocate(Some(&source), &foreign); @@ -112,7 +124,11 @@ fn arty_relocation_round_trip_rebinds_to_destination_worker() { #[test] fn arty_spawn_anywhere_relocates_payload_before_factory() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); let spawner = Spawner::new_arty(scheduler); @@ -138,6 +154,7 @@ fn arty_spawn_blocking_returns_output() { #[test] fn arty_task_panics_return_join_errors() { let runtime = Runtime::builder() + .monitor(MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .on(Panic::IGNORE) .build() @@ -209,7 +226,11 @@ fn arty_spawner_debug_names_the_runtime() { #[test] fn arty_builder_preserves_the_worker_association() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let (scheduler, expected) = runtime .scheduler() .block_on(async |cx| (cx.scheduler().clone(), thread::current().id())) diff --git a/crates/anyspawn/tests/observed.rs b/crates/anyspawn/tests/observed.rs index 01aa27f80..f03ea986f 100644 --- a/crates/anyspawn/tests/observed.rs +++ b/crates/anyspawn/tests/observed.rs @@ -200,6 +200,7 @@ fn pending_task_does_not_leak_context_on_the_same_thread() { #[test] fn arty_transfers_context_for_all_spawn_methods() { let runtime = arty::runtime::Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -229,6 +230,7 @@ fn arty_relocates_data_before_the_observed_factory() { } let runtime = arty::runtime::Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); diff --git a/crates/arty/Cargo.toml b/crates/arty/Cargo.toml index 1138bf021..31b121238 100644 --- a/crates/arty/Cargo.toml +++ b/crates/arty/Cargo.toml @@ -36,7 +36,13 @@ all-features = true [features] default = ["rt", "macros", "monitor"] macros = ["rt", "dep:arty_macros"] -monitor = ["dep:seismograph", "seismograph/monitor", "performables?/seismograph"] +monitor = [ + "dep:seismograph", + "dep:seismograph_runtime", + "arty_executor?/inspection", + "seismograph/monitor", + "performables?/seismograph", +] rt = [ "dep:arty_executor", "dep:arty_io_host", @@ -73,12 +79,13 @@ pin-project = { workspace = true, optional = true } plurality = { workspace = true, features = ["std"], optional = true } scopeguard = { workspace = true } seismograph = { workspace = true, optional = true } +seismograph_runtime = { workspace = true, optional = true } thread_aware = { workspace = true, features = ["std", "derive"], optional = true } thread_aware_core = { workspace = true, features = ["std"] } tick = { workspace = true } [dev-dependencies] -arty_executor = { path = "../arty_executor" } +arty_executor = { path = "../arty_executor", features = ["inspection"] } arty_io_host = { path = "../arty_io_host" } arty_macros = { path = "../arty_macros" } bolero = { workspace = true, features = ["std"] } @@ -98,6 +105,8 @@ performables = { path = "../performables", features = ["seismograph"] } pin-project = { workspace = true } plurality = { path = "../plurality", features = ["std"] } seismograph = { path = "../seismograph", features = ["monitor"] } +seismograph_protocol = { path = "../seismograph_protocol", features = ["test-util"] } +seismograph_runtime = { path = "../seismograph_runtime", features = ["test-util"] } static_assertions = { workspace = true } testing_aids = { path = "../testing_aids" } thread_aware = { path = "../thread_aware", features = ["std", "derive", "test-utils"] } @@ -122,6 +131,16 @@ name = "arty_contention" harness = false required-features = ["rt"] +[[bench]] +name = "arty_inspection" +harness = false +required-features = ["rt"] + +[[bench]] +name = "arty_spawn_allocations" +harness = false +required-features = ["rt"] + [[example]] name = "arty_advanced" required-features = ["rt"] diff --git a/crates/arty/README.md b/crates/arty/README.md index cdeefda51..2eb773982 100644 --- a/crates/arty/README.md +++ b/crates/arty/README.md @@ -65,11 +65,13 @@ The default feature set enables `rt`, `macros`, and `monitor`. * `rt`: Enables the runtime, task, and I/O APIs. * `macros`: Enables [`main`][__link13] and [`test`][__link14]. It also enables `rt`. -* `monitor`: Enables the Seismograph monitor dependency. It forwards - monitoring support for `performables` when `performables` is enabled. It does - not start monitoring or emit new telemetry. +* `monitor`: Connects runtime workers and tasks to Seismograph. An enabled + [`MonitorPolicy`][__link15] starts a local connection for + the Seismograph CLI. Task collection runs on request. Event recording stays + off until an application or the CLI enables it. This feature also forwards + monitoring support for `performables` when `performables` is enabled. * `test-util`: Enables testing utilities, including - [`ClockControl`][__link15]. + [`ClockControl`][__link16]. Enable it in dev-dependencies, not production dependencies. @@ -78,7 +80,7 @@ The default feature set enables `rt`, `macros`, and `monitor`. This crate was developed as part of The Oxidizer Project. Browse this crate's source code. - [__cargo_doc2readme_dependencies_info]: ggGmYW0CYXZlMC43LjNhdIQborR2_k_xJd4bTcf2krrNPIcbP72Pw1UdRjkbim_eMDe2BBthYvRhcoQbQL336xbftBwb3WEJKMJ3SngbyR3bGBAhBb0b0RYBQT1d6EhhZIKCZGFydHllMC42LjGCa2FydHlfbWFjcm9zZTAuNi4x + [__cargo_doc2readme_dependencies_info]: ggGmYW0CYXZlMC43LjNhdIQborR2_k_xJd4bTcf2krrNPIcbP72Pw1UdRjkbim_eMDe2BBthYvRhcoQbpjTzkOovhJgb4j8Xd8b5dycb1Kf_wRXWtfYbr0QtKZVggRRhZIKCZGFydHllMC42LjGCa2FydHlfbWFjcm9zZTAuNi4x [__link0]: https://doc.rust-lang.org/stable/std/marker/trait.Send.html [__link1]: https://docs.rs/arty/0.6.1/arty/?search=core::ThreadAware [__link10]: https://docs.rs/arty/0.6.1/arty/core/index.html @@ -86,7 +88,8 @@ This crate was developed as part of &'static str { + match self { + Self::Disabled => "disabled", + #[cfg(feature = "monitor")] + Self::Idle => "idle", + #[cfg(feature = "monitor")] + Self::Collecting => "collecting", + #[cfg(feature = "monitor")] + Self::Recording => "recording", + } + } +} + +#[derive(Clone, Copy)] +enum Workload { + LocalSpawn, + RepeatedPoll, + RemoteBatch, + BlockingBatch, +} + +impl Workload { + const ALL: [Self; 4] = [Self::LocalSpawn, Self::RepeatedPoll, Self::RemoteBatch, Self::BlockingBatch]; + + fn name(self) -> &'static str { + match self { + Self::LocalSpawn => "local_spawn_w1", + Self::RepeatedPoll => "repeated_poll_w1", + Self::RemoteBatch => "remote_batch_w4", + Self::BlockingBatch => "blocking_batch_w4", + } + } +} + +struct Case { + runtime: Runtime, + handles: Vec>, + collector: Option>, + stop: Arc, + captures: Arc, +} + +impl Case { + fn new(mode: Mode, workload: Workload) -> Self { + #[cfg(feature = "monitor")] + seismograph::recorder(Configuration::default()); + let workers = if matches!(workload, Workload::RemoteBatch | Workload::BlockingBatch) { + 4 + } else { + 1 + }; + let policy = if matches!(mode, Mode::Disabled) { + MonitorPolicy::disabled() + } else { + MonitorPolicy::enabled() + }; + let runtime = Runtime::builder() + .workers(WorkersPolicy::exactly(workers)) + .blocking_pool(BlockingPoolPolicy::shared().max(4)) + .monitor(policy) + .build() + .expect("benchmark runtime configuration is valid"); + let stop = Arc::new(AtomicBool::new(false)); + let captures = Arc::new(AtomicU64::new(0)); + #[cfg(feature = "monitor")] + let collector = matches!(mode, Mode::Collecting).then(|| { + let stop = Arc::clone(&stop); + let captures = Arc::clone(&captures); + let (ready, started) = mpsc::sync_channel(1); + let collector = thread::spawn(move || { + black_box(seismograph::snapshot(SnapshotOptions::default()).expect("benchmark collection succeeds")); + captures.fetch_add(1, Ordering::Relaxed); + ready.send(()).expect("benchmark caller waits for collection"); + while !stop.load(Ordering::Acquire) { + thread::park_timeout(Duration::from_millis(10)); + black_box(seismograph::snapshot(SnapshotOptions::default()).expect("benchmark collection succeeds")); + captures.fetch_add(1, Ordering::Relaxed); + } + }); + started + .recv_timeout(Duration::from_secs(5)) + .expect("benchmark collector must complete its first snapshot before measurement"); + collector + }); + #[cfg(not(feature = "monitor"))] + let collector = None; + #[cfg(feature = "monitor")] + if matches!(mode, Mode::Recording) { + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + } + let mut case = Self { + runtime, + handles: Vec::with_capacity(usize::try_from(OPERATIONS).expect("64 operations fit in usize")), + collector, + stop, + captures, + }; + black_box(case.run(workload, 4)); + case + } + + fn run(&mut self, workload: Workload, iterations: u64) -> Duration { + let start = Instant::now(); + match workload { + Workload::LocalSpawn => { + let task = self + .runtime + .scheduler() + .spawn_anywhere(iterations * OPERATIONS, |cx, operations| async move { + for _ in 0..operations { + cx.scheduler() + .spawn(async |_| black_box(())) + .await + .expect("benchmark child succeeds"); + } + }); + futures::executor::block_on(task).expect("benchmark parent succeeds"); + } + Workload::RepeatedPoll => { + let task = self + .runtime + .scheduler() + .spawn_anywhere(iterations * OPERATIONS, |_, operations| RepeatedPoll(operations)); + futures::executor::block_on(task).expect("benchmark task succeeds"); + } + Workload::RemoteBatch | Workload::BlockingBatch => { + for _ in 0..iterations { + self.handles.clear(); + for _ in 0..OPERATIONS { + let task = if matches!(workload, Workload::RemoteBatch) { + self.runtime.scheduler().spawn_anywhere((), |_, ()| async { black_box(()) }) + } else { + self.runtime.scheduler().spawn_blocking(|| black_box(())) + }; + self.handles.push(task); + } + for task in &mut self.handles { + futures::executor::block_on(black_box(task)).expect("benchmark task succeeds"); + } + } + } + } + start.elapsed() + } +} + +impl Drop for Case { + fn drop(&mut self) { + self.stop.store(true, Ordering::Release); + if let Some(collector) = self.collector.take() { + collector.thread().unpark(); + collector.join().expect("benchmark diagnostic caller succeeds"); + eprintln!("Completed process snapshots: {}", self.captures.load(Ordering::Relaxed)); + } + #[cfg(feature = "monitor")] + seismograph::recorder(Configuration::default()); + } +} + +struct RepeatedPoll(u64); + +impl Future for RepeatedPoll { + type Output = (); + + fn poll(mut self: Pin<&mut Self>, cx: &mut PollContext<'_>) -> Poll<()> { + if self.0 == 0 { + Poll::Ready(()) + } else { + self.0 = black_box(self.0 - 1); + cx.waker().wake_by_ref(); + Poll::Pending + } + } +} + +fn benchmarks(criterion: &mut Criterion) { + let mut group = criterion.benchmark_group("arty_inspection/tasks"); + group.throughput(Throughput::Elements(OPERATIONS)); + for workload in Workload::ALL { + for &mode in Mode::ALL { + let mut case = None; + group.bench_function(BenchmarkId::new(workload.name(), mode.name()), |bencher| { + let case = case.get_or_insert_with(|| Case::new(mode, workload)); + bencher.iter_custom(|iterations| case.run(workload, iterations)); + }); + } + } + group.finish(); +} + +criterion_group! { + name = inspection; + config = Criterion::default() + .sample_size(50) + .warm_up_time(Duration::from_secs(1)) + .measurement_time(Duration::from_secs(3)); + targets = benchmarks +} +criterion_main!(inspection); diff --git a/crates/arty/benches/arty_spawn_allocations.rs b/crates/arty/benches/arty_spawn_allocations.rs new file mode 100644 index 000000000..ef38b0eb8 --- /dev/null +++ b/crates/arty/benches/arty_spawn_allocations.rs @@ -0,0 +1,182 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! Measures warmed task spawning with Metabench. +//! +//! Run with `--criterion --allocations`. Metabench runs allocation cases in +//! separate processes. To exclude monitoring dependencies, use a separate +//! package with Arty as a normal, runtime-only dependency. +//! Arty's own dev-dependencies can enable monitoring through feature unification. +//! Runtime setup and warm-up are excluded. +//! Unrelated process threads can contribute allocations. The local case measures +//! 4096 sequential children in one parent. The remote case measures 64 batches +//! of 64 tasks on four workers, including dispatch and joins. The blocking case +//! uses the same batches and one blocking worker. Reports show totals per +//! 4096-task workload, not per task. +//! The blocking backlog case queues 4096 tasks behind a gate. Its timer covers +//! releasing the gate and joining all tasks, but excludes submission. +//! Its allocation counts include the gate and its task. + +use std::borrow::BorrowMut; +use std::hint::black_box; +use std::sync::{Arc, Barrier}; +use std::time::{Duration, Instant}; + +use arty::runtime::{BlockingPoolPolicy, MonitorPolicy, Runtime, WorkersPolicy}; +use arty::task::JoinHandle; +use criterion::{BenchmarkId, Criterion, Throughput}; +use metabench::benchmark; + +#[derive(Clone, Copy, Debug)] +enum Workload { + Local, + Remote, + Blocking, + BlockingBacklog, +} + +impl Workload { + const fn name(self) -> &'static str { + match self { + Self::Local => "local", + Self::Remote => "remote", + Self::Blocking => "blocking", + Self::BlockingBacklog => "blocking_backlog", + } + } +} + +#[derive(Debug)] +struct Case { + runtime: Runtime, + handles: Vec>, + workload: Workload, +} + +impl Case { + fn new(workload: Workload, enabled: bool) -> Self { + let runtime = Runtime::builder() + .workers(WorkersPolicy::exactly(if matches!(workload, Workload::Local) { 1 } else { 4 })) + .blocking_pool(BlockingPoolPolicy::shared().max(1)) + .monitor(if enabled { + MonitorPolicy::enabled() + } else { + MonitorPolicy::disabled() + }) + .build() + .expect("benchmark requires the configured worker count"); + let mut case = Self { + runtime, + handles: Vec::with_capacity(if matches!(workload, Workload::BlockingBacklog) { 4096 } else { 64 }), + workload, + }; + black_box(case.run()); + case + } + + fn run(&mut self) -> Duration { + if matches!(self.workload, Workload::BlockingBacklog) { + self.handles.clear(); + let gate = Arc::new(Barrier::new(2)); + let worker_gate = Arc::clone(&gate); + let first = self.runtime.scheduler().spawn_blocking(move || { + worker_gate.wait(); + }); + for _ in 0..4096 { + self.handles.push(self.runtime.scheduler().spawn_blocking(|| black_box(()))); + } + let start = Instant::now(); + gate.wait(); + futures::executor::block_on(first).expect("benchmark gate succeeds"); + for task in &mut self.handles { + futures::executor::block_on(task).expect("benchmark task succeeds"); + } + return start.elapsed(); + } + let start = Instant::now(); + match self.workload { + Workload::Local => { + let parent = self.runtime.scheduler().spawn_anywhere((), |cx, ()| async move { + for _ in 0..4096 { + cx.scheduler() + .spawn(async |_| black_box(())) + .await + .expect("benchmark child succeeds"); + } + }); + futures::executor::block_on(parent).expect("benchmark parent succeeds"); + } + Workload::Remote | Workload::Blocking | Workload::BlockingBacklog => { + for _ in 0..64 { + self.handles.clear(); + for _ in 0..64 { + let task = match self.workload { + Workload::Blocking => self.runtime.scheduler().spawn_blocking(|| black_box(())), + Workload::Remote => self.runtime.scheduler().spawn_anywhere((), |_, ()| async { black_box(()) }), + Workload::Local | Workload::BlockingBacklog => unreachable!("local and backlog spawning use separate paths"), + }; + self.handles.push(task); + } + for task in &mut self.handles { + futures::executor::block_on(task).expect("benchmark task succeeds"); + } + } + } + } + start.elapsed() + } +} + +fn configured_criterion() -> Criterion { + Criterion::default() + .sample_size(50) + .warm_up_time(Duration::from_millis(500)) + .measurement_time(Duration::from_secs(2)) +} + +fn criterion_benchmarks(criterion: &mut Criterion) { + let mut group = criterion.benchmark_group(SPAWN.group_name()); + group.throughput(Throughput::Elements(4096)); + for workload in [Workload::Local, Workload::Remote, Workload::Blocking, Workload::BlockingBacklog] { + for enabled in [true, false] { + let mode = if enabled { "idle" } else { "disabled" }; + group.bench_function( + BenchmarkId::new(SPAWN.benchmark_name(), format!("{}_{mode}", workload.name())), + |bencher| { + let mut case = Case::new(workload, enabled); + bencher.iter_custom(|iterations| { + let mut elapsed = Duration::ZERO; + for _ in 0..iterations { + elapsed += spawn_workload(&mut case).0; + } + elapsed + }); + }, + ); + } + } + group.finish(); +} + +#[benchmark(SPAWN, "arty_spawn_allocations/tasks", "spawn")] +#[bench::local_idle(Box::new(Case::new(Workload::Local, true)))] +#[bench::local_disabled(Box::new(Case::new(Workload::Local, false)))] +#[bench::remote_idle(Box::new(Case::new(Workload::Remote, true)))] +#[bench::remote_disabled(Box::new(Case::new(Workload::Remote, false)))] +#[bench::blocking_idle(Box::new(Case::new(Workload::Blocking, true)))] +#[bench::blocking_disabled(Box::new(Case::new(Workload::Blocking, false)))] +#[bench::blocking_backlog_idle(Box::new(Case::new(Workload::BlockingBacklog, true)))] +#[bench::blocking_backlog_disabled(Box::new(Case::new(Workload::BlockingBacklog, false)))] +fn spawn_workload>(mut input: S) -> (Duration, S) { + let elapsed = input.borrow_mut().run(); + (elapsed, input) +} + +metabench::main!( + criterion = { + factory = configured_criterion, + benchmarks = criterion_benchmarks, + unit = "ns", + }, + benchmarks = [SPAWN], +); diff --git a/crates/arty/src/io/handle.rs b/crates/arty/src/io/handle.rs index 680d77d9c..d8b608978 100644 --- a/crates/arty/src/io/handle.rs +++ b/crates/arty/src/io/handle.rs @@ -275,7 +275,10 @@ mod tests { #[test] fn blocking_marker_io_lookup_survives_scheduler_slot_teardown() { - let runtime = crate::runtime::Runtime::new().unwrap(); + let runtime = crate::runtime::Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) + .build() + .unwrap(); let context = runtime.scheduler().block_on(async |cx| cx).unwrap(); let scheduler = context.scheduler().clone(); let io = context.io().clone(); diff --git a/crates/arty/src/lib.rs b/crates/arty/src/lib.rs index 51398af2d..7b9df475c 100644 --- a/crates/arty/src/lib.rs +++ b/crates/arty/src/lib.rs @@ -70,13 +70,18 @@ //! //! - `rt`: Enables the runtime, task, and I/O APIs. //! - `macros`: Enables [`main`] and [`test`]. It also enables `rt`. -//! - `monitor`: Enables the Seismograph monitor dependency. It forwards -//! monitoring support for `performables` when `performables` is enabled. It does -//! not start monitoring or emit new telemetry. +//! - `monitor`: Connects runtime workers and tasks to Seismograph. An enabled +//! [`MonitorPolicy`](crate::runtime::MonitorPolicy) starts a local connection for +//! the Seismograph CLI. Task collection runs on request. Event recording stays +//! off until an application or the CLI enables it. This feature also forwards +//! monitoring support for `performables` when `performables` is enabled. //! - `test-util`: Enables testing utilities, including //! [`ClockControl`](crate::time::ClockControl). //! Enable it in dev-dependencies, not production dependencies. +#[cfg(all(test, feature = "macros", feature = "monitor", not(miri)))] +extern crate self as arty; + use arty_io_core as _; /// Runs an async entry point on an Arty runtime. /// @@ -103,7 +108,8 @@ use arty_io_core as _; /// causes runtime construction to fail. /// - `workers = all` starts one async worker per available processor. /// - `monitor = enabled` or `monitor = disabled` selects whether the runtime -/// is prepared for monitoring. The default is `enabled`. +/// starts a local Seismograph monitor when the `monitor` feature is present. +/// The default is `enabled`. /// - `panic = shutdown` or `panic = ignore` selects whether the runtime stops /// after Arty catches a task panic. The default is `shutdown`. /// @@ -147,15 +153,6 @@ use arty_io_core as _; /// # #[cfg(not(all(feature = "macros", feature = "rt")))] fn main() {} /// ``` /// -/// Disable monitoring for the runtime: -/// -/// ``` -/// # #[cfg(all(feature = "macros", feature = "rt"))] -/// #[arty::main(monitor = disabled)] -/// async fn main(_cx: arty::task::Context) {} -/// # #[cfg(not(all(feature = "macros", feature = "rt")))] fn main() {} -/// ``` -/// /// Keep the runtime running after a task panic that Arty catches: /// /// ``` @@ -259,7 +256,7 @@ pub use arty_macros::main; /// /// Enable monitoring for a test that exercises monitoring behavior: /// -/// ```test_harness +/// ```no_run,test_harness /// # #[cfg(all(feature = "macros", feature = "rt"))] /// #[arty::test(monitor = enabled)] /// async fn monitored(_cx: arty::task::Context) {} diff --git a/crates/arty/src/runtime/bootstrap/startup.rs b/crates/arty/src/runtime/bootstrap/startup.rs index 0ebef1d2b..2176dacc8 100644 --- a/crates/arty/src/runtime/bootstrap/startup.rs +++ b/crates/arty/src/runtime/bootstrap/startup.rs @@ -25,7 +25,7 @@ fn build_with_processors( thread_builder: &ThreadBuilder, available: &ProcessorSet, ) -> Result { - let (workers, shutdown) = prepare_workers( + let prepared = prepare_workers( processor_config, clock, thread_builder, @@ -33,19 +33,41 @@ fn build_with_processors( |name| std::env::var(name).ok(), asynchronous::start, )?; + let workers = prepared.workers; let dispatcher = DispatcherCore::new( NonEmpty::from_vec(workers.iter().map(|worker| worker.endpoint().clone()).collect()) .expect("the number is either hardcoded or validated in the builder, so can never be zero"), - shutdown, + prepared.shutdown, ); let dispatcher_client = DispatcherClient::new(dispatcher); + #[cfg(feature = "monitor")] + dispatcher_client.set_diagnostics( + prepared + .diagnostics + .as_ref() + .map(crate::runtime::monitoring::RuntimeDiagnostics::handle), + ); let mut join_handles = Vec::with_capacity(workers.len()); for worker in workers { join_handles.push(worker.activate(dispatcher_client.clone())); } let worker_joiner = WorkerJoiner::new(join_handles); - Ok(Runtime::with_dispatcher(dispatcher_client, worker_joiner)) + let runtime = Runtime::with_dispatcher(dispatcher_client, worker_joiner); + #[cfg(feature = "monitor")] + let runtime = { + let mut runtime = runtime; + runtime.set_diagnostics(prepared.diagnostics); + runtime + }; + Ok(runtime) +} + +struct PreparedWorkers { + workers: Vec, + shutdown: Shutdown, + #[cfg(feature = "monitor")] + diagnostics: Option, } fn prepare_workers( @@ -55,12 +77,13 @@ fn prepare_workers( available: &ProcessorSet, environment: impl FnOnce(&str) -> Option, mut start: impl FnMut(asynchronous::StartOptions) -> W, -) -> Result<(Vec, Shutdown), Error> { +) -> Result, Error> { let name = processor_config.name; name.validate()?; let processors = processor_config.workers_policy.select(available)?; - let mut workers = Vec::with_capacity(processors.len()); + let worker_count = processors.len(); + let mut workers = Vec::with_capacity(worker_count); // Respect the RUST_MIN_STACK environment variable if set and larger than the configured stack size. let stack_size = select_stack_size(processor_config.stack_size, environment); @@ -69,6 +92,13 @@ fn prepare_workers( let shutdown = Shutdown::default(); let io_runtime = IoRuntime::new(observed::Sink::noop()); let blocking_pools = processor_config.blocking_pool_policy.into_pools(name.clone())?; + #[cfg(feature = "monitor")] + let (diagnostics, monitor_options) = { + let diagnostics = crate::runtime::monitoring::RuntimeDiagnostics::start(monitor_options.policy(), &name, worker_count)?; + let mut monitor_options = monitor_options; + monitor_options.set_runtime(diagnostics.as_ref().map(crate::runtime::monitoring::RuntimeDiagnostics::handle)); + (diagnostics, monitor_options) + }; for (worker_index, processor) in processors.into_iter().enumerate() { workers.push(start(asynchronous::StartOptions { @@ -86,7 +116,12 @@ fn prepare_workers( })); } - Ok((workers, shutdown)) + Ok(PreparedWorkers { + workers, + shutdown, + #[cfg(feature = "monitor")] + diagnostics, + }) } fn select_stack_size(configured: std::num::NonZeroUsize, environment: impl FnOnce(&str) -> Option) -> usize { @@ -128,7 +163,7 @@ mod tests { fn nov4_startup_passes_the_selected_stack_size_to_each_native_worker_request() { let hardware = many_cpus::SystemHardware::fake(HardwareBuilder::from_counts(NonZero::new(2).unwrap(), NonZero::new(1).unwrap())); for environment in [None, Some("32769")] { - let (requests, shutdown) = prepare_workers( + let prepared = prepare_workers( RuntimeConfig { name: crate::runtime::config::RuntimeName::default(), workers_policy: WorkersPolicy::at_most(2), @@ -147,6 +182,8 @@ mod tests { |request| request, ) .unwrap(); + let requests = prepared.workers; + let shutdown = prepared.shutdown; assert_eq!(requests.len(), 2); assert!( requests @@ -260,7 +297,7 @@ mod tests { let hardware = fake_hardware(PROCESSORS); let runtime = runtime_with_processors( WorkersPolicy::exactly(WORKERS), - MonitorPolicy::enabled().into(), + MonitorPolicy::disabled().into(), &hardware.processors(), ); let cx = runtime.scheduler().block_on(async |cx| cx).unwrap(); diff --git a/crates/arty/src/runtime/builder.rs b/crates/arty/src/runtime/builder.rs index 132c8fe04..44d4b35cb 100644 --- a/crates/arty/src/runtime/builder.rs +++ b/crates/arty/src/runtime/builder.rs @@ -42,7 +42,7 @@ pub struct RuntimeBuilder { } impl RuntimeBuilder { - /// Sets the runtime name used in worker thread names. + /// Sets the runtime name used in worker thread names and monitoring. /// /// Async threads use `-async-`, with indices starting at zero. /// Shared blocking pools use `-blocking-`. @@ -89,7 +89,9 @@ impl RuntimeBuilder { /// /// The default is [`MonitorPolicy::enabled`]. The `monitor` Cargo feature /// enables the Seismograph dependencies that monitoring needs. - /// This choice does not start a monitor or add task measurements. + /// Building an enabled runtime starts a local connection for the Seismograph CLI. + /// Task collection runs on request. This does not enable event recording or + /// change which tasks run. See the [monitoring guide](crate::runtime#monitoring). /// /// # Examples /// @@ -217,6 +219,7 @@ impl RuntimeBuilder { /// /// Returns [`Error`] if the worker count or blocking-pool limit is zero, /// or if the runtime name contains a null character (`\0`). + /// With monitoring enabled, also returns an error if the local monitor cannot start. /// /// # Panics /// diff --git a/crates/arty/src/runtime/config/monitor_options.rs b/crates/arty/src/runtime/config/monitor_options.rs index 2922d37ac..2745207ed 100644 --- a/crates/arty/src/runtime/config/monitor_options.rs +++ b/crates/arty/src/runtime/config/monitor_options.rs @@ -7,6 +7,8 @@ use crate::runtime::config::MonitorPolicy; #[derive(Clone, Debug)] pub(crate) struct MonitorOptions { policy: MonitorPolicy, + #[cfg(feature = "monitor")] + runtime: Option, } impl MonitorOptions { @@ -19,6 +21,16 @@ impl MonitorOptions { pub(crate) const fn policy(&self) -> &MonitorPolicy { &self.policy } + + #[cfg(feature = "monitor")] + pub(crate) fn set_runtime(&mut self, runtime: Option) { + self.runtime = runtime; + } + + #[cfg(feature = "monitor")] + pub(crate) fn runtime(&self) -> Option<&seismograph_runtime::RuntimeHandle> { + self.runtime.as_ref() + } } impl Default for MonitorOptions { @@ -29,7 +41,11 @@ impl Default for MonitorOptions { impl From for MonitorOptions { fn from(policy: MonitorPolicy) -> Self { - Self { policy } + Self { + policy, + #[cfg(feature = "monitor")] + runtime: None, + } } } diff --git a/crates/arty/src/runtime/config/monitor_policy.rs b/crates/arty/src/runtime/config/monitor_policy.rs index d336a348e..dad7fcbb7 100644 --- a/crates/arty/src/runtime/config/monitor_policy.rs +++ b/crates/arty/src/runtime/config/monitor_policy.rs @@ -8,11 +8,13 @@ /// The enabled policy is the default. /// /// The `monitor` Cargo feature enables the Seismograph dependencies that -/// monitoring needs. This choice does not start a monitor or add task measurements. +/// monitoring needs. Building an enabled runtime starts a local monitor. +/// Task collection and poll timing run only when requested. This policy does +/// not enable event recording or install an allocator. /// /// # Examples /// -/// ``` +/// ```no_run /// use arty::runtime::{MonitorPolicy, Runtime}; /// /// let runtime = Runtime::builder() @@ -21,7 +23,6 @@ /// runtime.stop()?; /// # Ok::<(), Box>(()) /// ``` -#[repr(transparent)] #[derive(Debug, Clone)] pub struct MonitorPolicy { enabled: bool, @@ -40,7 +41,6 @@ impl MonitorPolicy { Self { enabled: false } } - #[cfg(test)] pub(crate) const fn is_enabled(&self) -> bool { self.enabled } diff --git a/crates/arty/src/runtime/config/runtime_name.rs b/crates/arty/src/runtime/config/runtime_name.rs index e44b2919e..f5ba02dac 100644 --- a/crates/arty/src/runtime/config/runtime_name.rs +++ b/crates/arty/src/runtime/config/runtime_name.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use crate::runtime::error::Error; -/// Creates thread names for one runtime. +/// Names one runtime and its threads. #[derive(Clone, Debug)] pub(crate) struct RuntimeName(Arc); @@ -14,6 +14,11 @@ impl RuntimeName { Self(Arc::from(name.as_ref())) } + #[cfg(feature = "monitor")] + pub(crate) fn as_str(&self) -> &str { + &self.0 + } + pub(crate) fn async_worker(&self, index: usize) -> String { format!("{}-async-{index}", self.0) } diff --git a/crates/arty/src/runtime/control.rs b/crates/arty/src/runtime/control.rs index 6cd3281f6..d10c6d5c3 100644 --- a/crates/arty/src/runtime/control.rs +++ b/crates/arty/src/runtime/control.rs @@ -105,7 +105,7 @@ impl RuntimeControl { /// ``` /// use arty::runtime::Runtime; /// - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let control = runtime /// .scheduler() /// .block_on(async |cx| cx.control().clone())?; diff --git a/crates/arty/src/runtime/dispatch/client.rs b/crates/arty/src/runtime/dispatch/client.rs index 84f75d2ef..978e7d0bb 100644 --- a/crates/arty/src/runtime/dispatch/client.rs +++ b/crates/arty/src/runtime/dispatch/client.rs @@ -26,6 +26,16 @@ pub(crate) struct DispatcherClient { } impl DispatcherClient { + #[cfg(feature = "monitor")] + pub(crate) fn set_diagnostics(&self, runtime: Option) { + self.core.set_diagnostics(runtime); + } + + #[cfg(feature = "monitor")] + pub(crate) fn register_task_provider(&self, thread: ThreadId, handle: &seismograph_runtime::worker::WorkerHandle) { + self.core.register_task_provider(thread, handle); + } + pub(crate) const fn new(core: Arc) -> Self { Self { core } } @@ -68,13 +78,18 @@ impl DispatcherClient { self.core.spawn_on_worker(worker_index, future_factory) } - pub(crate) fn spawn(&self, future_factory: FF) -> JoinHandle + pub(crate) fn spawn( + &self, + future_factory: FF, + monitoring: crate::runtime::monitoring::TaskMonitoring, + ) -> JoinHandle where FF: FnOnce(Context) -> F + Send + 'static, F: Future + 'static, R: Send + 'static, + S: Fn() -> usize + Send + 'static, { - self.core.spawn(future_factory) + self.core.spawn(future_factory, monitoring) } pub(crate) fn spawn_relocating(&self, source: &thread_aware::Thread, future_factory: FF) -> JoinHandle diff --git a/crates/arty/src/runtime/dispatch/core.rs b/crates/arty/src/runtime/dispatch/core.rs index 76a573f55..297c1be63 100644 --- a/crates/arty/src/runtime/dispatch/core.rs +++ b/crates/arty/src/runtime/dispatch/core.rs @@ -14,6 +14,7 @@ use performables::arc::Arc; use thread_aware::{Thread, ThreadAware}; use crate::runtime::hook::HookRegistration; +use crate::runtime::monitoring::TaskMonitoring; use crate::runtime::shutdown::{Shutdown, ShutdownState}; use crate::runtime::worker::asynchronous::{CommandSender, LocalWorker, QueuedFactory, QueuedIoAccess, SpawnCommand, SystemCommand}; use crate::runtime::worker::blocking::Worker; @@ -68,6 +69,8 @@ pub(crate) struct DispatcherCore { next_async_worker_index: AtomicUsize, remote_results: JoinHandlePool, + #[cfg(feature = "monitor")] + diagnostics: std::sync::OnceLock, } impl DispatcherCore { @@ -102,11 +105,44 @@ impl DispatcherCore { worker_indices, next_async_worker_index: AtomicUsize::new(0), remote_results: JoinHandlePool::new(shutdown), + #[cfg(feature = "monitor")] + diagnostics: std::sync::OnceLock::new(), }) } + #[cfg(feature = "monitor")] + pub(crate) fn set_diagnostics(&self, runtime: Option) { + if let Some(runtime) = runtime { + self.diagnostics + .set(runtime) + .expect("bootstrap sets runtime diagnostics exactly once"); + } + } + + #[cfg(feature = "monitor")] + pub(crate) fn register_task_provider(&self, thread: ThreadId, handle: &seismograph_runtime::worker::WorkerHandle) { + let index = self + .worker_index(thread) + .expect("worker diagnostics use a registered runtime thread"); + let endpoint = &self.worker_endpoints[usize::from(index)]; + let commands = endpoint.commands.system_sender().clone(); + let waker = endpoint.waker.clone(); + handle + .register_task_provider(move |request| { + if commands.send(SystemCommand::Inspect { request }).is_ok() { + waker.wake_by_ref(); + } + // A failed send drops the request and reports collection failure. + }) + .expect("bootstrap registers providers before their workers can stop"); + } + pub(crate) fn stop_inner(&self, cause: ShutdownCause) -> ShutdownCause { self.shutdown().begin(cause); + #[cfg(feature = "monitor")] + if let Some(runtime) = self.diagnostics.get() { + runtime.stopping(); + } let cause = self.shutdown().cause().unwrap_or_default(); // Shutdown stores the cause independently. This atomic only elects the // broadcaster. Observing `true` does not mean notification is complete. @@ -128,13 +164,14 @@ impl DispatcherCore { WorkerIndex(self.next_async_worker_index.fetch_add(1, Ordering::Relaxed) % self.worker_endpoints.len()) } - pub(crate) fn spawn(&self, future_factory: FF) -> JoinHandle + pub(crate) fn spawn(&self, future_factory: FF, monitoring: TaskMonitoring) -> JoinHandle where FF: FnOnce(Context) -> F + Send + 'static, F: Future + 'static, R: Send + 'static, + S: Fn() -> usize + Send + 'static, { - self.enqueue(self.next_worker_index(), TaskPlacement::Any, future_factory) + self.enqueue(self.next_worker_index(), TaskPlacement::Any, future_factory, monitoring) } pub(crate) fn spawn_relocating(&self, source: &Thread, future_factory: FF) -> JoinHandle @@ -265,7 +302,7 @@ impl DispatcherCore { F: Future + 'static, R: Send + 'static, { - self.enqueue(worker_index, TaskPlacement::SameThread, future_factory) + self.enqueue(worker_index, TaskPlacement::SameThread, future_factory, TaskMonitoring::capture()) } /// Records the execution worker so a successful result can relocate when its join is polled. @@ -276,14 +313,22 @@ impl DispatcherCore { R: ThreadAware + 'static, { let source = self.worker_endpoints[usize::from(worker_index)].thread.clone(); - self.enqueue(worker_index, placement, future_factory).relocate_result_from(source) + self.enqueue(worker_index, placement, future_factory, TaskMonitoring::capture()) + .relocate_result_from(source) } - fn enqueue(&self, worker_index: WorkerIndex, placement: TaskPlacement, future_factory: FF) -> JoinHandle + fn enqueue( + &self, + worker_index: WorkerIndex, + placement: TaskPlacement, + future_factory: FF, + monitoring: TaskMonitoring, + ) -> JoinHandle where FF: FnOnce(Context) -> F + Send + 'static, F: Future + 'static, R: Send + 'static, + S: Fn() -> usize + Send + 'static, { if let Some(join_handle) = JoinHandle::try_from_shutdown(self.shutdown()) { drop(future_factory); @@ -293,7 +338,7 @@ impl DispatcherCore { .worker_endpoints .get(usize::from(worker_index)) .expect("worker index must identify a registered runtime worker"); - let (future_factory, join_handle) = prepare_remote(&self.remote_results, future_factory, placement); + let (future_factory, join_handle) = prepare_remote(&self.remote_results, future_factory, placement, monitoring); // A worker can close its channel before this send if shutdown already // reached that worker. The join handle detects the disconnection. @@ -389,9 +434,15 @@ mod tests { let dispatcher = DispatcherCore::new(NonEmpty::new(worker), Shutdown::default()); - dispatcher.spawn(|_| async move { unreachable!("we do not expect the task to be executed") }); + dispatcher.spawn( + |_| async move { unreachable!("we do not expect the task to be executed") }, + TaskMonitoring::capture(), + ); - dispatcher.spawn(|_| async move { unreachable!("we do not expect the task to be executed") }); + dispatcher.spawn( + |_| async move { unreachable!("we do not expect the task to be executed") }, + TaskMonitoring::capture(), + ); assert_eq!(drain_spawn(&worker_rx).len(), 2); } @@ -408,9 +459,15 @@ mod tests { let dispatcher = DispatcherCore::new(NonEmpty::from_vec(vec![worker1, worker2]).unwrap(), Shutdown::default()); - dispatcher.spawn(|_| async move { unreachable!("we do not expect the task to be executed") }); + dispatcher.spawn( + |_| async move { unreachable!("we do not expect the task to be executed") }, + TaskMonitoring::capture(), + ); - dispatcher.spawn(|_| async move { unreachable!("we do not expect the task to be executed") }); + dispatcher.spawn( + |_| async move { unreachable!("we do not expect the task to be executed") }, + TaskMonitoring::capture(), + ); assert_eq!(drain_spawn(&worker1_rx).len(), 1); assert_eq!(drain_spawn(&worker2_rx).len(), 1); @@ -425,7 +482,10 @@ mod tests { let dispatcher = DispatcherCore::new(NonEmpty::from_vec(vec![worker1, worker2]).unwrap(), Shutdown::default()); for _ in 0..4 { - dispatcher.spawn(|_| async move { unreachable!("we do not expect the task to be executed") }); + dispatcher.spawn( + |_| async move { unreachable!("we do not expect the task to be executed") }, + TaskMonitoring::capture(), + ); } assert_eq!(drain_spawn(&worker1_rx).len(), 2); @@ -496,7 +556,7 @@ mod tests { let dispatcher = DispatcherCore::new(NonEmpty::new(endpoint(&threads[0]).0), Shutdown::default()); dispatcher.stop_inner(ShutdownError::new(Error::worker_panicked()).into()); - let mut join = pin!(dispatcher.spawn(|_| async {})); + let mut join = pin!(dispatcher.spawn(|_| async {}, TaskMonitoring::capture())); let Poll::Ready(Err(error)) = join.as_mut().poll(&mut task::Context::from_waker(Waker::noop())) else { panic!("shutdown rejection must be immediately ready"); }; @@ -532,6 +592,48 @@ mod tests { assert_eq!(drain_system(&worker2_rx).len(), 0); } + #[cfg(feature = "monitor")] + #[test] + fn dispatcher_stop_updates_its_registered_runtime() { + use seismograph_runtime::snapshot::{CaptureOptions, RuntimeState}; + + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(seismograph_runtime::RuntimeMetadata::new("dispatcher-stop", 1)); + let threads = test_threads(1); + let (worker, _worker_rx) = endpoint(&threads[0]); + let core = DispatcherCore::new(NonEmpty::new(worker), Shutdown::default()); + let client = DispatcherClient::new(core); + client.set_diagnostics(None); + client.set_diagnostics(Some(runtime.handle())); + + client.stop(ShutdownCause::default()); + + assert_eq!( + runtime.handle().capture(CaptureOptions::default()).unwrap().state, + RuntimeState::Stopping + ); + } + + #[cfg(feature = "monitor")] + #[test] + fn a_closed_worker_command_channel_reports_collection_failure() { + use seismograph_runtime::snapshot::{CaptureOptions, TaskCollection}; + use seismograph_runtime::worker::{WorkerMetadata, WorkerRole}; + + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(seismograph_runtime::RuntimeMetadata::new("closed-worker-channel", 1)); + let threads = test_threads(1); + let (endpoint, receiver) = endpoint(&threads[0]); + let dispatcher = DispatcherCore::new(NonEmpty::new(endpoint), Shutdown::default()); + let worker = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + dispatcher.register_task_provider(threads[0].id(), &worker.handle()); + drop(receiver); + + let snapshot = runtime.handle().capture(CaptureOptions::default()).unwrap(); + assert!(snapshot.tasks.is_empty()); + assert_eq!(snapshot.workers[0].task_collection, TaskCollection::Failed); + } + #[test] fn spawn_after_command_channel_closed_returns_disconnected_join_handle() { // After a worker closes its command channel during shutdown, later @@ -550,7 +652,7 @@ mod tests { // Close the command channel. drop(command_rx); - let join_handle = dispatcher.spawn(|_| async move {}); + let join_handle = dispatcher.spawn(|_| async move {}, TaskMonitoring::capture()); let join_handle = pin!(join_handle); let mut cx = task::Context::from_waker(Waker::noop()); diff --git a/crates/arty/src/runtime/handle.rs b/crates/arty/src/runtime/handle.rs index 7f7c8ad5d..e961e78b0 100644 --- a/crates/arty/src/runtime/handle.rs +++ b/crates/arty/src/runtime/handle.rs @@ -45,7 +45,7 @@ use crate::task::RuntimeScheduler; /// ``` /// use arty::runtime::Runtime; /// -/// let runtime = Runtime::new()?; +/// let runtime = Runtime::builder().build()?; /// let task = runtime.scheduler().spawn_anywhere((), |_, ()| async { 42 }); /// assert_eq!(futures::executor::block_on(task)?, 42); /// runtime.stop()?; @@ -56,6 +56,8 @@ pub struct Runtime { scheduler: RuntimeScheduler, worker_joiner: WorkerJoiner, shutdown_on_drop: bool, + #[cfg(feature = "monitor")] + diagnostics: Option, } impl Runtime { @@ -76,7 +78,7 @@ impl Runtime { /// /// # Examples /// - /// ``` + /// ```no_run /// use arty::runtime::Runtime; /// /// let runtime = Runtime::new()?; @@ -118,7 +120,7 @@ impl Runtime { /// ``` /// use arty::runtime::Runtime; /// - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let scheduler = runtime.scheduler(); /// let first = scheduler.spawn_anywhere((), |_, ()| async { 20 }); /// let second = scheduler.spawn_anywhere((), |_, ()| async { 22 }); @@ -134,6 +136,19 @@ impl Runtime { &self.scheduler } + /// Returns the runtime's diagnostic handle for collection tests. + /// + /// Returns `None` when monitoring is disabled. Capture from a diagnostic + /// thread, not from an Arty worker that must answer the collection request. + /// This does not change the process-wide recorder. + #[cfg(all(feature = "monitor", test, not(miri)))] + #[must_use] + pub(crate) fn diagnostics(&self) -> Option { + self.diagnostics + .as_ref() + .map(crate::runtime::monitoring::RuntimeDiagnostics::handle) + } + /// Takes ownership of this runtime, requests shutdown, and waits for its workers to stop. /// /// Cancels pending async tasks and prevents queued blocking functions from @@ -149,6 +164,8 @@ impl Runtime { /// Returns an error if a worker panicked or an I/O driver failed during /// startup or normal operation. It does not return I/O driver shutdown /// failures. It waits for all workers to stop before it reports a failure. + /// With monitoring enabled, also reports a stored monitor failure unless a + /// worker or runtime failure takes precedence. /// To receive a task failure, await its [`JoinHandle`](crate::task::JoinHandle). /// /// # Panics @@ -162,7 +179,7 @@ impl Runtime { /// ``` /// use arty::runtime::Runtime; /// - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let error = runtime.scheduler().block_on(async |cx| { /// cx.control().request_stop(); /// cx.scheduler() @@ -179,7 +196,7 @@ impl Runtime { } // #[gamma::skip(parameter.default_shadow, tag = "timeout", reason = "The November 4 afternoon campaign timed out when resetting current_worker made a worker try to join itself.")] - fn stop_with(&mut self, current_worker: bool, wait: impl FnOnce(&Self) -> Result<(), Error>) -> Result<(), Error> { + fn stop_with(&mut self, current_worker: bool, wait: impl FnOnce(&mut Self) -> Result<(), Error>) -> Result<(), Error> { if // #[gamma::skip(cond.always_false, tag = "timeout", reason = "November 4 campaign: this mutation exceeded the test timeout")] current_worker { @@ -194,19 +211,40 @@ impl Runtime { wait(self) } - fn wait(&self) -> Result<(), Error> { + #[cfg_attr( + not(feature = "monitor"), + expect( + clippy::needless_pass_by_ref_mut, + reason = "monitoring consumes the diagnostic registration after workers stop" + ) + )] + fn wait(&mut self) -> Result<(), Error> { let wait_error = self.worker_joiner.join().err(); + #[cfg(feature = "monitor")] + let monitor_error = self.diagnostics.take().and_then(|diagnostics| diagnostics.finish().err()); if let Some(error) = self.scheduler.dispatcher.shutdown().error() { return Err(Error::shutdown_failure(error)); } + #[cfg(feature = "monitor")] + { + monitor_result(wait_error, monitor_error) + } + #[cfg(not(feature = "monitor"))] wait_error.map_or(Ok(()), Err) } + #[cfg(feature = "monitor")] + pub(crate) fn set_diagnostics(&mut self, diagnostics: Option) { + self.diagnostics = diagnostics; + } + pub(crate) const fn with_dispatcher(dispatcher: DispatcherClient, worker_joiner: WorkerJoiner) -> Self { Self { scheduler: RuntimeScheduler::new(dispatcher), worker_joiner, shutdown_on_drop: true, + #[cfg(feature = "monitor")] + diagnostics: None, } } } @@ -230,6 +268,12 @@ impl Drop for Runtime { } } +#[cfg(feature = "monitor")] +#[cfg_attr(coverage_nightly, coverage(off))] // Native monitor error forwarding does not justify a fault-injection fixture. +fn monitor_result(worker_error: Option, monitor_error: Option) -> Result<(), Error> { + worker_error.or(monitor_error).map_or(Ok(()), Err) +} + #[cold] #[inline(never)] #[expect(clippy::panic, reason = "runtime ownership on a worker is a programming error")] @@ -337,6 +381,7 @@ mod tests { #[test] fn ignored_task_worker_stop_skips_a_later_active_task() { let runtime = Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .on(crate::runtime::hook::Panic::IGNORE) .build() @@ -390,6 +435,7 @@ mod tests { #[test] fn ignored_task_worker_drop_skips_a_later_active_task() { let runtime = Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .on(crate::runtime::hook::Panic::IGNORE) .build() @@ -498,6 +544,7 @@ mod tests { fn single_worker_runtime() -> Runtime { Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .blocking_pool(crate::runtime::BlockingPoolPolicy::shared().max(1)) .build() diff --git a/crates/arty/src/runtime/mod.rs b/crates/arty/src/runtime/mod.rs index 3c2caf83b..28997cee1 100644 --- a/crates/arty/src/runtime/mod.rs +++ b/crates/arty/src/runtime/mod.rs @@ -44,8 +44,9 @@ //! default thread limits can change. Use [`BlockingPoolPolicy::max`] to set //! a maximum. //! - The clock follows real time. -//! - [`MonitorPolicy::default`] enables monitoring in the runtime configuration. -//! This choice does not start a monitor or add task measurements. +//! - [`MonitorPolicy::default`] starts a local Seismograph monitor when the +//! `monitor` feature is present. Task collection runs on request. This does +//! not enable event recording. //! - Without a [`Panic`](hook::Panic) hook, the runtime stops after Arty //! catches a panic while it creates or polls an async task. //! - A blocking function panic returns through its join handle without @@ -54,6 +55,28 @@ //! The [`test`](crate::test) attribute disables monitoring by default. //! See [`main`](crate::main) and [`test`](crate::test) for their other defaults. //! +//! ## Monitoring +//! +//! A monitor is a local connection through which the Seismograph CLI can +//! collect diagnostics. Each enabled runtime owns one connection and removes it +//! after its workers stop. [`RuntimeBuilder::name`] sets the displayed runtime name. +//! Use [`MonitorPolicy::disabled`] to run without this connection or task inspection. +//! +//! Collection reads live async tasks and queued or running blocking functions +//! from their native storage. Each request limits the number of tasks per worker. +//! A blocked async worker can time out. The result reports incomplete collection +//! rather than treating missing tasks as an empty worker. +//! +//! Poll timing runs only during a requested measurement interval or event recording. +//! Completed tasks are not kept in a second task registry. +//! History that was not recorded stays unknown. A blocking function that started +//! before observation has no measured start time for that call. +//! +//! Monitor connections are separate, but the recorder and diagnostic sources are +//! process-wide. Recording, clearing event buffers, and snapshot capture through +//! one connection can affect or include other runtimes in the same process. +//! Monitor startup does not change recorder configuration or install an allocator. +//! //! ## Configuration types //! //! - [`WorkersPolicy`] selects how many async workers run and which processors they use. @@ -137,6 +160,7 @@ pub(crate) mod dispatch; mod error; mod handle; pub mod hook; +pub(crate) mod monitoring; mod shutdown; mod shutdown_cause; #[cfg(test)] diff --git a/crates/arty/src/runtime/monitoring/asynchronous.rs b/crates/arty/src/runtime/monitoring/asynchronous.rs new file mode 100644 index 000000000..5fd6bf6a2 --- /dev/null +++ b/crates/arty/src/runtime/monitoring/asynchronous.rs @@ -0,0 +1,228 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +use std::any::Any; +use std::cell::{Cell, RefCell}; + +use arty_executor::{Executor, InspectionError, InspectionStatus, PollObserver}; +use seismograph_runtime::RuntimeHandle; +use seismograph_runtime::snapshot::TaskCollection; +use seismograph_runtime::task::{TaskPoll, TaskRequest}; +use seismograph_runtime::worker::{WorkerHandle, WorkerMetadata, WorkerRegistration, WorkerRole}; + +use crate::runtime::dispatch::DispatcherClient; +use crate::runtime::monitoring::task::{Description, set_current, set_events}; + +#[derive(Debug)] +pub(crate) struct WorkerDiagnostics { + _registration: WorkerRegistration, + handle: WorkerHandle, + poll: RefCell>, + failed: Cell, +} + +impl WorkerDiagnostics { + pub(crate) fn new(runtime: &RuntimeHandle, dispatcher: &DispatcherClient, thread: &thread_aware::Thread) -> Self { + let registration = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let handle = registration.handle(); + dispatcher.register_task_provider(thread.id(), &handle); + registration.attach_current_thread(); + set_events(Some(handle.task_events())); + Self { + _registration: registration, + handle, + poll: RefCell::new(None), + failed: Cell::new(false), + } + } + + pub(crate) fn is_observed(&self) -> bool { + self.handle.is_observed() + } + + pub(crate) fn collect(&self, executor: &mut Executor, request: TaskRequest) { + if request.is_canceled() { + return; + } + let mut tasks = Vec::new(); + let status = collection_status(executor.inspect(request.limit(), |description: &Description| { + if let Description::Snapshot(task) = description { + tasks.push(task.clone()); + } + })); + let status = if self.failed.replace(false) { + TaskCollection::Failed + } else { + status + }; + request.respond(tasks, status); + } + + pub(crate) fn parked(&self) { + self.handle.parked(); + } + + pub(crate) fn unparked(&self) { + self.handle.unparked(); + } +} + +impl PollObserver for WorkerDiagnostics { + fn poll_started(&self, description: &dyn Any) { + if let Some(Description::Poll(id)) = description.downcast_ref::() { + set_current(Some(*id)); + *self.poll.borrow_mut() = Some(self.handle.task_poll_started(*id)); + } else { + self.failed.set(true); + } + } + + fn poll_finished(&self, _completed: bool) { + set_current(None); + if let Some(poll) = self.poll.borrow_mut().take() { + self.handle.task_poll_finished(poll); + } + } + + fn inspection_failed(&self, _error: InspectionError) { + self.failed.set(true); + } +} + +impl Drop for WorkerDiagnostics { + fn drop(&mut self) { + set_current(None); + set_events(None); + } +} + +fn collection_status(result: Result) -> TaskCollection { + match result { + Ok(InspectionStatus::Complete) => TaskCollection::Complete, + Ok(InspectionStatus::Truncated) => TaskCollection::Truncated, + Err(InspectionError::Busy) => TaskCollection::Busy, + Err(InspectionError::InvalidLimit | InspectionError::Panicked) => TaskCollection::Failed, + } +} + +#[cfg(test)] +mod tests { + #[cfg(all(feature = "test-util", not(miri)))] + use std::sync::mpsc; + + use super::*; + + #[test] + fn inspection_outcomes_preserve_completeness_and_failure() { + assert_eq!( + [ + Ok(InspectionStatus::Complete), + Ok(InspectionStatus::Truncated), + Err(InspectionError::Busy), + Err(InspectionError::InvalidLimit), + Err(InspectionError::Panicked), + ] + .map(collection_status), + [ + TaskCollection::Complete, + TaskCollection::Truncated, + TaskCollection::Busy, + TaskCollection::Failed, + TaskCollection::Failed, + ] + ); + } + + #[cfg(all(feature = "test-util", not(miri)))] + #[test] + fn worker_bridge_preserves_demand_failures_and_thread_local_lifecycle() { + use seismograph_runtime::RuntimeMetadata; + use seismograph_runtime::snapshot::{CaptureOptions, WorkerState}; + use testing_aids::TEST_TIMEOUT; + + use crate::runtime::monitoring::task::{current_events, current_task}; + + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("worker-bridge", 1)); + let thread = thread_aware::ThreadBuilder::default().build(std::thread::current().id()); + let dispatcher = DispatcherClient::for_test(&thread); + let diagnostics = WorkerDiagnostics::new(&runtime.handle(), &dispatcher, &thread); + let (sender, receiver) = mpsc::channel(); + diagnostics + .handle + .register_task_provider(move |request| sender.send(request).unwrap()) + .unwrap(); + let mut executor = Executor::builder().build(); + let capture = |executor: &mut Executor| { + std::thread::scope(|scope| { + let handle = runtime.handle(); + let captured = scope.spawn(move || { + handle + .capture(CaptureOptions { + timeout: TEST_TIMEOUT.min(std::time::Duration::from_secs(5)), + ..CaptureOptions::default() + }) + .unwrap() + }); + let request = receiver.recv_timeout(TEST_TIMEOUT).unwrap(); + assert!(diagnostics.is_observed()); + diagnostics.collect(executor, request); + captured.join().unwrap() + }) + }; + + assert!(current_events().is_some()); + assert!(!seismograph::recorder::without_recording(|| diagnostics.is_observed())); + let completion = crate::task::handle::JoinHandlePool::new(crate::runtime::shutdown::Shutdown::default()); + let (_handle, sender) = completion.handle(); + let parent = seismograph_runtime::allocate_task_id(); + { + let _parent = crate::runtime::monitoring::task::enter_current(parent); + crate::runtime::monitoring::add_task::<(), _, _, _>( + &executor.tasks(), + std::future::pending::<()>(), + arty_executor::TaskPlacement::Any, + sender, + ); + } + let snapshot = capture(&mut executor); + assert_eq!(snapshot.tasks[0].parent, Some(parent)); + assert_eq!( + snapshot.tasks[0].activity.unwrap().state, + seismograph_runtime::snapshot::TaskActivityState::Ready + ); + diagnostics.parked(); + assert_eq!(capture(&mut executor).workers[0].state, WorkerState::Parked); + diagnostics.unparked(); + assert_eq!(capture(&mut executor).workers[0].state, WorkerState::Running); + + diagnostics.inspection_failed(InspectionError::Panicked); + assert_eq!(capture(&mut executor).workers[0].task_collection, TaskCollection::Failed); + assert_eq!(capture(&mut executor).workers[0].task_collection, TaskCollection::Complete); + diagnostics.poll_started(&()); + diagnostics.poll_finished(false); + assert_eq!(capture(&mut executor).workers[0].task_collection, TaskCollection::Failed); + + let (_handle, sender) = completion.handle(); + executor + .tasks() + .task(std::future::pending::<()>()) + .inspect(|_, _| Some(Description::Poll(seismograph_runtime::allocate_task_id()))) + .register(sender); + assert_eq!(capture(&mut executor).tasks.len(), 1); + + let task = seismograph_runtime::allocate_task_id(); + diagnostics.poll_started(&Description::Poll(task)); + assert_eq!(current_task(), Some(task)); + diagnostics.poll_finished(false); + assert_eq!(current_task(), None); + diagnostics.poll_started(&Description::Poll(task)); + drop(diagnostics); + assert_eq!(current_task(), None); + assert!(current_events().is_none()); + assert_eq!( + runtime.handle().capture(CaptureOptions::default()).unwrap().workers[0].state, + WorkerState::Stopped + ); + } +} diff --git a/crates/arty/src/runtime/monitoring/blocking.rs b/crates/arty/src/runtime/monitoring/blocking.rs new file mode 100644 index 000000000..06b878ad4 --- /dev/null +++ b/crates/arty/src/runtime/monitoring/blocking.rs @@ -0,0 +1,233 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +use std::marker::PhantomData; + +#[cfg(feature = "monitor")] +use super::TaskInfo; +use crate::runtime::shutdown::Shutdown; +#[cfg(feature = "monitor")] +use crate::runtime::shutdown::ShutdownState; +use crate::task::preparation::BlockingTask; + +pub(crate) enum BlockingJob { + Plain(Box), + #[cfg(feature = "monitor")] + Monitored(std::sync::Arc), +} + +impl BlockingJob { + pub(crate) fn run(self) { + match self { + Self::Plain(body) => body(), + #[cfg(feature = "monitor")] + Self::Monitored(body) => body.run(), + } + } + + #[cfg(feature = "monitor")] + pub(crate) fn monitoring(&self) -> Option<&std::sync::Arc> { + match self { + Self::Plain(_) => None, + Self::Monitored(body) => Some(body), + } + } +} + +#[cfg(feature = "monitor")] +pub(crate) trait MonitoredBlockingJob: Send + Sync { + fn info(&self) -> &TaskInfo; + fn run(&self); +} + +#[derive(Clone, Debug, Default)] +pub(crate) struct BlockingPoolMonitoring { + #[cfg(feature = "monitor")] + events: std::sync::Arc>, +} + +impl BlockingPoolMonitoring { + #[cfg_attr( + not(feature = "monitor"), + expect( + clippy::unused_self, + clippy::extra_unused_type_parameters, + reason = "The monitoring interface is unchanged when monitoring is compiled out." + ) + )] + pub(crate) fn task(&self, shutdown: &Shutdown) -> BlockingTaskMonitoring { + #[cfg(not(feature = "monitor"))] + let _ = shutdown; + BlockingTaskMonitoring { + #[cfg(feature = "monitor")] + info: matches!(shutdown.state(), ShutdownState::Running) + .then(|| self.events().map(TaskInfo::new::)) + .flatten(), + } + } + + #[cfg(feature = "monitor")] + pub(crate) fn enable(&self, events: seismograph_runtime::task::EventContext) { + self.events.set(events).expect("monitoring is enabled once under the queue lock"); + } + + #[cfg(feature = "monitor")] + pub(crate) fn events(&self) -> Option { + self.events.get().copied() + } +} + +#[derive(Default)] +pub(crate) struct BlockingTaskMonitoring { + #[cfg(feature = "monitor")] + info: Option, +} + +impl BlockingTaskMonitoring { + #[cfg_attr( + not(feature = "monitor"), + expect(clippy::unused_self, reason = "Task monitoring is empty when monitoring is compiled out.") + )] + pub(crate) fn prepare(self, task: BlockingTask) -> BlockingJob + where + F: FnOnce() -> R + Send + 'static, + R: Send + 'static, + { + #[cfg(feature = "monitor")] + if let Some(info) = self.info { + return BlockingJob::Monitored(std::sync::Arc::new(MonitoredTask { + task: std::sync::Mutex::new(task), + info, + })); + } + BlockingJob::Plain(Box::new(move || task.run(BlockingTaskEvents::default()))) + } +} + +#[derive(Clone, Copy, Default)] +pub(crate) struct BlockingTaskEvents<'a> { + #[cfg(feature = "monitor")] + info: Option<&'a TaskInfo>, + marker: PhantomData<&'a ()>, +} + +#[cfg_attr( + not(feature = "monitor"), + expect(clippy::unused_self, reason = "Task events do nothing when monitoring is compiled out.") +)] +impl BlockingTaskEvents<'_> { + pub(crate) fn completed(self) { + #[cfg(feature = "monitor")] + if let Some(info) = self.info { + info.completed(); + } + } + + pub(crate) fn panicked(self) { + #[cfg(feature = "monitor")] + if let Some(info) = self.info { + info.panicked(); + } + } +} + +// Inspection shares this allocation with execution. Only taking the callback +// needs the mutex; neither the callback nor its destructor runs under the lock. +#[cfg(feature = "monitor")] +struct MonitoredTask { + task: std::sync::Mutex>, + info: TaskInfo, +} + +#[cfg(feature = "monitor")] +impl MonitoredBlockingJob for MonitoredTask +where + F: FnOnce() -> R + Send + 'static, + R: Send + 'static, +{ + fn info(&self) -> &TaskInfo { + &self.info + } + + fn run(&self) { + let task = std::mem::replace( + &mut *self.task.lock().expect("taking a blocking callback does not execute user code"), + BlockingTask { pending: None }, + ); + task.run(BlockingTaskEvents { + info: Some(&self.info), + marker: PhantomData, + }); + } +} + +#[cfg(feature = "monitor")] +impl Drop for MonitoredTask { + fn drop(&mut self) { + if self + .task + .get_mut() + .expect("taking a blocking callback does not execute user code") + .pending + .is_some() + { + self.info.canceled(); + } + } +} + +#[derive(Default)] +pub(crate) struct BlockingWorkerMonitoring { + #[cfg(feature = "monitor")] + pub(crate) handle: Option, +} + +#[cfg_attr( + not(feature = "monitor"), + expect(clippy::unused_self, reason = "Worker monitoring is empty when monitoring is compiled out.") +)] +impl BlockingWorkerMonitoring { + pub(crate) fn parked(&self) { + #[cfg(feature = "monitor")] + if let Some(worker) = &self.handle { + worker.parked(); + } + } + + pub(crate) fn unparked(&self) { + #[cfg(feature = "monitor")] + if let Some(worker) = &self.handle { + worker.unparked(); + } + } + + pub(crate) fn run(&self, body: BlockingJob, canceled: bool) -> std::thread::Result<()> { + #[cfg(feature = "monitor")] + let observation = self.handle.as_ref().filter(|_| !canceled).and_then(|worker| { + body.monitoring().map(|body| { + let info = body.info(); + info.started_on(worker.task_events()); + let poll = worker.is_observed().then(|| worker.task_poll_started(info.id())); + (worker, poll) + }) + }); + let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + #[cfg(feature = "monitor")] + let _current = observation + .as_ref() + .and_then(|(_, poll)| poll.as_ref()) + .and_then(|_| body.monitoring()) + .map(|body| super::task::enter_current(body.info().id())); + if canceled { + drop(body); + } else { + body.run(); + } + })); + #[cfg(feature = "monitor")] + if let Some((worker, Some(poll))) = observation { + worker.task_poll_finished(poll); + } + outcome + } +} diff --git a/crates/arty/src/runtime/monitoring/mod.rs b/crates/arty/src/runtime/monitoring/mod.rs new file mode 100644 index 000000000..208bc7e87 --- /dev/null +++ b/crates/arty/src/runtime/monitoring/mod.rs @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! Private task monitoring and native task inspection. + +#[cfg(feature = "monitor")] +mod asynchronous; +mod blocking; +#[cfg(feature = "monitor")] +mod runtime; +mod submission; +#[cfg(feature = "monitor")] +mod task; + +#[cfg(test)] +#[cfg(all(feature = "monitor", not(miri)))] +mod tests; + +// Hold for the full lifetime of tests that use the process registry or recorder. +#[cfg(all(test, feature = "monitor"))] +pub(crate) static TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +#[cfg(feature = "monitor")] +pub(crate) use asynchronous::WorkerDiagnostics; +#[cfg(feature = "monitor")] +pub(crate) use blocking::MonitoredBlockingJob; +pub(crate) use blocking::{BlockingJob, BlockingPoolMonitoring, BlockingTaskEvents, BlockingTaskMonitoring, BlockingWorkerMonitoring}; +#[cfg(feature = "monitor")] +pub(crate) use runtime::RuntimeDiagnostics; +pub(crate) use submission::{TaskMonitoring, add_task}; +#[cfg(feature = "monitor")] +pub(crate) use task::TaskInfo; +#[cfg(all(test, feature = "monitor"))] +pub(crate) use task::current_task; diff --git a/crates/arty/src/runtime/monitoring/runtime.rs b/crates/arty/src/runtime/monitoring/runtime.rs new file mode 100644 index 000000000..e285d171a --- /dev/null +++ b/crates/arty/src/runtime/monitoring/runtime.rs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +use seismograph::monitor::Monitor; +use seismograph_runtime::{RuntimeHandle, RuntimeMetadata, RuntimeRegistration}; + +use crate::runtime::config::RuntimeName; +use crate::runtime::{Error, MonitorPolicy}; + +#[derive(Debug)] +pub(crate) struct RuntimeDiagnostics { + monitor: Monitor, + registration: RuntimeRegistration, +} + +impl RuntimeDiagnostics { + pub(crate) fn start(policy: &MonitorPolicy, name: &RuntimeName, workers: usize) -> Result, Error> { + if !policy.is_enabled() { + return Ok(None); + } + let workers = u32::try_from(workers).map_err(Error::new)?; + let builder = Monitor::builder().name(name.as_str()); + #[cfg(test)] + let builder = builder.io(seismograph_protocol::io::MonitorIo::new_mocked().0); + let monitor = builder.start().map_err(Error::new)?; + let registration = seismograph_runtime::register_runtime(RuntimeMetadata::new(name.as_str(), workers)); + Ok(Some(Self { monitor, registration })) + } + + pub(crate) fn handle(&self) -> RuntimeHandle { + self.registration.handle() + } + + pub(crate) fn finish(self) -> Result<(), Error> { + self.registration.stopped(); + let error = self.monitor.last_error(); + drop(self); + error.map_or(Ok(()), |message| Err(Error::new(MonitorFailure::new(message)))) + } +} + +#[ohno::error] +#[display("the runtime monitor failed: {message}")] +struct MonitorFailure { + message: String, +} diff --git a/crates/arty/src/runtime/monitoring/submission.rs b/crates/arty/src/runtime/monitoring/submission.rs new file mode 100644 index 000000000..f73a57945 --- /dev/null +++ b/crates/arty/src/runtime/monitoring/submission.rs @@ -0,0 +1,122 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +use std::marker::PhantomData; + +use arty_executor::{TaskCompletion, TaskPlacement, TaskSet}; + +/// Captures submission metadata before a task crosses threads. +pub(crate) struct TaskMonitoring { + #[cfg(feature = "monitor")] + parent: Option, + #[cfg(feature = "monitor")] + body_size: S, + marker: PhantomData (Body, S)>, +} + +impl TaskMonitoring { + pub(crate) fn capture() -> TaskMonitoring usize + Send> { + // A function item stores no bytes; only erased scoped futures carry a size. + TaskMonitoring::new(size_of::) + } + + pub(crate) fn with_body_size(body_size: usize) -> TaskMonitoring usize + Send> { + TaskMonitoring::new(move || body_size) + } +} + +impl TaskMonitoring { + fn new(body_size: S) -> Self { + #[cfg(not(feature = "monitor"))] + drop(body_size); + Self { + #[cfg(feature = "monitor")] + parent: super::task::current_task(), + #[cfg(feature = "monitor")] + body_size, + marker: PhantomData, + } + } +} + +impl TaskMonitoring +where + Body: 'static, + S: Fn() -> usize + 'static, +{ + #[cfg_attr( + not(feature = "monitor"), + expect(clippy::unused_self, reason = "Submission metadata is empty when monitoring is compiled out.") + )] + pub(crate) fn register(self, tasks: &TaskSet, future: F, placement: TaskPlacement, completion: T) + where + F: IntoFuture + 'static, + R: 'static, + T: TaskCompletion, + { + #[cfg(feature = "monitor")] + super::task::add_task_with_metadata::(tasks, future, placement, completion, self.parent, self.body_size); + #[cfg(not(feature = "monitor"))] + tasks.task(future).placement(placement).register(completion); + } +} + +pub(crate) fn add_task(tasks: &TaskSet, future: F, placement: TaskPlacement, completion: T) +where + Body: 'static, + F: IntoFuture + 'static, + R: 'static, + T: TaskCompletion, +{ + TaskMonitoring::::capture().register(tasks, future, placement, completion); +} + +#[cfg(all(test, feature = "monitor"))] +mod tests { + use std::pin::Pin; + + use arty_executor::{Executor, InspectionStatus}; + use seismograph_runtime::RuntimeMetadata; + use seismograph_runtime::worker::{WorkerMetadata, WorkerRole}; + + use super::*; + use crate::runtime::monitoring::task::{Description, enter_current, set_events}; + use crate::runtime::shutdown::Shutdown; + use crate::task::handle::JoinHandlePool; + + #[test] + fn erased_submission_preserves_its_original_size_and_submission_parent() { + type ErasedFuture = Pin>>; + + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("submission-metadata", 1)); + let worker = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let parent = seismograph_runtime::allocate_task_id(); + let monitoring = { + let _parent = enter_current(parent); + TaskMonitoring::::with_body_size(37) + }; + let _other_parent = enter_current(seismograph_runtime::allocate_task_id()); + set_events(Some(worker.handle().task_events())); + let _events = scopeguard::guard((), |()| set_events(None)); + let mut executor = Executor::builder().build(); + let results = JoinHandlePool::new(Shutdown::default()); + let (_handle, sender) = results.handle(); + let future: ErasedFuture = Box::pin(std::future::pending()); + monitoring.register(&executor.tasks(), future, TaskPlacement::Any, sender); + let mut snapshots = Vec::new(); + + let status = executor + .inspect(1, |description: &Description| { + if let Description::Snapshot(task) = description { + snapshots.push(task.clone()); + } + }) + .unwrap(); + + assert_eq!(status, InspectionStatus::Complete); + assert_eq!(snapshots.len(), 1); + assert_eq!(snapshots[0].parent, Some(parent)); + assert_eq!(snapshots[0].future_size_bytes, Some(37)); + } +} diff --git a/crates/arty/src/runtime/monitoring/task.rs b/crates/arty/src/runtime/monitoring/task.rs new file mode 100644 index 000000000..b9a9d0c70 --- /dev/null +++ b/crates/arty/src/runtime/monitoring/task.rs @@ -0,0 +1,412 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +use std::any::TypeId; +use std::cell::{Cell, OnceCell, RefCell}; +use std::collections::HashMap; +use std::sync::OnceLock; + +use arty_executor::{ShutdownCause, TaskCompletion, TaskPanicPayload, TaskPlacement, TaskSet, TaskState}; +use seismograph::recorder::event::{EventClass, EventTimestamp}; +use seismograph::recorder::runtime::{TaskId, TypeDescriptorId}; +use seismograph_runtime::snapshot::{Task, TaskActivityState}; +use seismograph_runtime::task::EventContext; + +thread_local! { + static EVENTS: Cell> = const { Cell::new(None) }; + static CURRENT: Cell> = const { Cell::new(None) }; + // This cache contains types, not live tasks. It is touched only during observation. + static TYPES: RefCell> = RefCell::new(HashMap::new()); +} + +pub(crate) fn current_events() -> Option { + EVENTS.with(Cell::get) +} + +pub(super) fn set_events(events: Option) { + EVENTS.with(|slot| slot.set(events)); +} + +pub(super) fn set_current(task: Option) { + CURRENT.with(|slot| slot.set(task)); +} + +pub(crate) fn current_task() -> Option { + CURRENT.with(Cell::get) +} + +#[derive(Debug)] +#[must_use] +pub(crate) struct CurrentTask(Option); + +impl Drop for CurrentTask { + fn drop(&mut self) { + set_current(self.0); + } +} + +pub(crate) fn enter_current(task: TaskId) -> CurrentTask { + CurrentTask(CURRENT.with(|slot| slot.replace(Some(task)))) +} + +#[expect( + clippy::large_enum_variant, + reason = "descriptions are transient stack values; boxing snapshots would allocate during native inspection" +)] +pub(super) enum Description { + Snapshot(Task), + Poll(TaskId), +} + +fn descriptor() -> TypeDescriptorId { + TYPES.with_borrow_mut(|types| { + *types + .entry(TypeId::of::()) + .or_insert_with(seismograph_runtime::allocate_type_descriptor_id) + }) +} + +#[derive(Debug)] +struct TaskMetadata { + describe_type: fn() -> TypeDescriptorId, + body_size: usize, + events: EventContext, + parent: Option, + spawned_at: Option, +} + +impl TaskMetadata { + fn new(events: EventContext) -> Self { + Self { + describe_type: descriptor::, + body_size: size_of::(), + events, + parent: current_task(), + spawned_at: None, + } + } + + fn record_spawn(&mut self) -> Option { + self.events + .spawned_with(self.describe_type, self.parent, self.body_size) + .map(|spawn| { + self.spawned_at = Some(spawn.timestamp); + TaskIdentity { + id: spawn.id, + descriptor: spawn.descriptor, + } + }) + } + + fn identity(&self) -> TaskIdentity { + TaskIdentity { + id: seismograph_runtime::allocate_task_id(), + descriptor: (self.describe_type)(), + } + } + + fn describe(&self, identity: &TaskIdentity, state: TaskActivityState) -> Task { + let mut task = Task::new(identity.id, identity.descriptor, state); + task.parent = self.parent; + task.spawned_at = self.spawned_at; + task.future_size_bytes = Some(u64::try_from(self.body_size).expect("a task body fits the platform address space")); + task + } +} + +#[derive(Debug)] +struct TaskIdentity { + id: TaskId, + descriptor: TypeDescriptorId, +} + +#[derive(Debug)] +pub(crate) struct TaskInfo { + metadata: TaskMetadata, + identity: OnceLock, + execution_events: OnceLock, +} + +impl TaskInfo { + pub(crate) fn new(events: EventContext) -> Self { + let mut metadata = TaskMetadata::new::(events); + let identity = metadata.record_spawn().map_or_else(OnceLock::new, OnceLock::from); + Self { + metadata, + identity, + execution_events: OnceLock::new(), + } + } + + pub(crate) fn id(&self) -> TaskId { + self.identity.get_or_init(|| self.metadata.identity()).id + } + + pub(crate) fn describe(&self, state: TaskActivityState) -> Task { + self.metadata + .describe(self.identity.get_or_init(|| self.metadata.identity()), state) + } + + pub(crate) fn started_on(&self, events: EventContext) { + self.execution_events + .set(events) + .expect("a blocking function starts on exactly one worker"); + } + + pub(crate) fn completed(&self) { + if let Some(id) = self.terminal_id() { + self.execution_events.get().unwrap_or(&self.metadata.events).completed(id); + } + } + + pub(crate) fn panicked(&self) { + if let Some(id) = self.terminal_id() { + self.execution_events.get().unwrap_or(&self.metadata.events).panicked(id); + } + } + + pub(crate) fn canceled(&self) { + if let Some(id) = self.terminal_id() { + self.execution_events.get().unwrap_or(&self.metadata.events).canceled(id); + } + } + + fn terminal_id(&self) -> Option { + self.identity + .get() + .map(|identity| identity.id) + .or_else(|| seismograph::recorder::is_enabled(EventClass::RuntimeTask).then(|| self.id())) + } +} + +// Async metadata stays in the executor's owner-only payload. Blocking metadata +// above remains synchronized because queue inspection and execution can overlap. +#[derive(Debug)] +struct AsyncTaskInfo { + events: EventContext, + parent: Option, + spawned_at: Option, + identity: OnceCell, +} + +impl AsyncTaskInfo { + fn new(events: EventContext, parent: Option, body_size: usize) -> Self { + let spawn = events.spawned_with(descriptor::, parent, body_size); + Self { + events, + parent, + spawned_at: spawn.map(|spawn| spawn.timestamp), + identity: spawn.map_or_else(OnceCell::new, |spawn| OnceCell::from(spawn.id)), + } + } + + fn id(&self) -> TaskId { + *self.identity.get_or_init(seismograph_runtime::allocate_task_id) + } + + fn describe(&self, state: TaskActivityState, body_size: usize) -> Task { + let mut task = Task::new(self.id(), descriptor::(), state); + task.parent = self.parent; + task.spawned_at = self.spawned_at; + task.future_size_bytes = Some(u64::try_from(body_size).expect("a task body fits the platform address space")); + task + } + + fn terminal_id(&self) -> Option { + self.identity + .get() + .copied() + .or_else(|| seismograph::recorder::is_enabled(EventClass::RuntimeTask).then(|| self.id())) + } +} + +#[derive(Debug)] +struct Completion { + target: T, + info: AsyncTaskInfo, +} + +impl> TaskCompletion for Completion { + fn completed(self, result: R) { + if let Some(id) = self.info.terminal_id() { + self.info.events.completed(id); + } + self.target.completed(result); + } + + fn panicked(self, panic: TaskPanicPayload, cause: Option) { + if let Some(id) = self.info.terminal_id() { + self.info.events.panicked(id); + } + self.target.panicked(panic, cause); + } + + fn cancelled(self, cause: ShutdownCause) { + if let Some(id) = self.info.terminal_id() { + self.info.events.canceled(id); + } + self.target.cancelled(cause); + } +} + +pub(crate) fn add_task_with_metadata( + tasks: &TaskSet, + future: F, + placement: TaskPlacement, + completion: T, + parent: Option, + body_size: S, +) where + Body: 'static, + F: IntoFuture + 'static, + R: 'static, + T: TaskCompletion, + S: Fn() -> usize + 'static, +{ + let Some(events) = current_events() else { + tasks.task(future).placement(placement).register(completion); + return; + }; + let completion = Completion { + target: completion, + info: AsyncTaskInfo::new::(events, parent, body_size()), + }; + tasks + .task(future) + .placement(placement) + .register_with_inspection(completion, move |_, completion, state| { + let info = &completion.info; + Some(seismograph::recorder::without_recording(|| match state { + TaskState::Ready => Description::Snapshot(info.describe::(TaskActivityState::Ready, body_size())), + TaskState::Waiting => Description::Snapshot(info.describe::(TaskActivityState::Waiting, body_size())), + TaskState::Running => Description::Poll(info.id()), + })) + }); +} + +#[cfg(test)] +mod tests { + use std::error::Error as _; + + use arty_executor::{ShutdownCause, ShutdownError, TaskCompletion}; + use seismograph_runtime::RuntimeMetadata; + use seismograph_runtime::worker::{WorkerMetadata, WorkerRole}; + + use super::{AsyncTaskInfo, Completion, TaskInfo, current_task, enter_current}; + use crate::runtime::shutdown::{Shutdown, ShutdownState}; + use crate::task::handle::JoinHandlePool; + + #[test] + fn monitored_panic_preserves_the_payload_and_executor_shutdown_cause() { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("panic-forwarding", 1)); + let worker = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + + for stopping in [false, true] { + let pool = JoinHandlePool::new(Shutdown::default()); + let (handle, target) = pool.handle::(); + let cause = stopping.then(|| ShutdownCause::panic(&42_u32)); + let expected = cause.as_ref().and_then(ShutdownCause::as_error).cloned(); + let completion = Completion { + target, + info: AsyncTaskInfo::new::<()>(worker.handle().task_events(), None, 0), + }; + + completion.panicked(Box::new(42_u32), cause); + + if let Some(expected) = expected { + assert!(std::ptr::addr_eq(pool.shutdown().error().unwrap().inner(), expected.inner())); + } else { + assert_eq!(pool.shutdown().state(), ShutdownState::Running); + } + assert_eq!(*handle.join().unwrap_err().into_panic().unwrap().downcast::().unwrap(), 42); + } + } + + #[test] + fn monitored_cancellation_delivers_the_executor_shutdown_cause() { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("cancellation-forwarding", 1)); + let worker = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let pool = JoinHandlePool::new(Shutdown::default()); + let (handle, target) = pool.handle::(); + let cause = ShutdownCause::panic(&"executor failure"); + let expected = cause.as_error().unwrap().clone(); + let completion = Completion { + target, + info: AsyncTaskInfo::new::<()>(worker.handle().task_events(), None, 0), + }; + + completion.cancelled(cause); + let error = handle.join().unwrap_err(); + + assert!(error.is_shutdown()); + let source = error.source().unwrap().downcast_ref::().unwrap(); + assert!(std::ptr::addr_eq(source.inner(), expected.inner())); + assert!(std::ptr::addr_eq(pool.shutdown().error().unwrap().inner(), expected.inner())); + } + + #[test] + fn current_task_scope_restores_its_parent_after_return_and_panic() { + let parent = seismograph_runtime::allocate_task_id(); + let child = seismograph_runtime::allocate_task_id(); + assert_eq!(current_task(), None); + { + let _parent = enter_current(parent); + { + let _child = enter_current(child); + assert_eq!(current_task(), Some(child)); + } + assert_eq!(current_task(), Some(parent)); + let result = std::panic::catch_unwind(|| { + let _child = enter_current(child); + assert_eq!(current_task(), Some(child)); + panic!("scope restores the parent during unwinding"); + }); + assert!(result.is_err()); + assert_eq!(current_task(), Some(parent)); + } + assert_eq!(current_task(), None); + } + + #[test] + fn async_metadata_is_smaller_and_stays_owner_local() { + static_assertions::assert_not_impl_any!(AsyncTaskInfo: Sync); + static_assertions::assert_impl_all!(TaskInfo: Send, Sync); + assert!(size_of::() < size_of::()); + #[cfg(target_pointer_width = "64")] + assert_eq!(size_of::(), 56); + } + + #[test] + fn unobserved_blocking_outcomes_do_not_allocate_task_identities() { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(seismograph_runtime::RuntimeMetadata::new("idle-outcomes", 1)); + let worker = runtime.register_worker(seismograph_runtime::worker::WorkerMetadata::new( + seismograph_runtime::worker::WorkerRole::Blocking, + )); + seismograph::recorder::without_recording(|| { + let info = TaskInfo::new::<()>(worker.handle().task_events()); + info.completed(); + info.panicked(); + info.canceled(); + assert!(info.identity.get().is_none()); + }); + } + + #[test] + fn absent_worker_events_preserve_plain_task_execution() { + super::set_events(None); + let mut executor = arty_executor::Executor::builder().build(); + let ran = std::rc::Rc::new(std::cell::Cell::new(false)); + let completed = std::rc::Rc::clone(&ran); + let future = async move { completed.set(true) }; + let completion = crate::task::handle::JoinHandlePool::new(crate::runtime::shutdown::Shutdown::default()); + let (_handle, sender) = completion.handle(); + + super::add_task_with_metadata::<(), _, _, _, _>(&executor.tasks(), future, arty_executor::TaskPlacement::Any, sender, None, || 0); + let _ = executor.execute_cycle(); + + assert!(ran.get()); + } +} diff --git a/crates/arty/src/runtime/monitoring/tests.rs b/crates/arty/src/runtime/monitoring/tests.rs new file mode 100644 index 000000000..0d602387e --- /dev/null +++ b/crates/arty/src/runtime/monitoring/tests.rs @@ -0,0 +1,1111 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! Arty supplies native task state independently of event recording. + +use std::future::{pending, poll_fn}; +use std::pin::Pin; +use std::sync::{Arc, Condvar, Mutex, MutexGuard, mpsc}; +use std::task::{Context, Poll}; +use std::time::Duration; + +use seismograph::recorder::event::EventKind; +use seismograph::recorder::{Configuration, Policy}; +use seismograph::snapshot::SnapshotOptions; +use seismograph_runtime::snapshot::{ + CaptureOptions, Runtime as RuntimeSnapshot, RuntimeState, TaskActivityState, TaskCollection, TaskMetricsScope, +}; +use seismograph_runtime::worker::WorkerRole; +use testing_aids::TEST_TIMEOUT; +use thread_aware::Unaware; + +use crate::runtime::hook::Panic; +use crate::runtime::{BlockingPoolPolicy, MonitorPolicy, Runtime, WorkersPolicy}; + +struct RecorderGuard { + _lock: MutexGuard<'static, ()>, +} + +impl RecorderGuard { + fn new() -> Self { + let lock = super::TEST_LOCK.lock().unwrap(); + seismograph::recorder(Configuration::default()); + Self { _lock: lock } + } +} + +impl Drop for RecorderGuard { + fn drop(&mut self) { + seismograph::recorder(Configuration::default()); + } +} + +fn runtime(name: &str) -> Runtime { + Runtime::builder() + .workers(WorkersPolicy::exactly(1)) + .blocking_pool(BlockingPoolPolicy::shared().max(1)) + .name(name) + .on(Panic::IGNORE) + .build() + .unwrap() +} + +fn runtimes() -> Vec { + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let snapshot = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + snapshot + .sources + .iter() + .find(|source| source.id == seismograph_runtime::snapshot::source::ID) + .map_or_else(Vec::new, |source| { + seismograph_runtime::snapshot::decode(&source.data).unwrap().runtimes + }) +} + +fn capture(runtime: &Runtime) -> RuntimeSnapshot { + runtime + .diagnostics() + .unwrap() + .capture(CaptureOptions { + timeout: Duration::from_secs(5), + ..CaptureOptions::default() + }) + .unwrap() +} + +#[test] +fn monitor_policy_defaults_to_enabled() { + let _recording = RecorderGuard::new(); + let runtime = Runtime::new().unwrap(); + let diagnostics = runtime.diagnostics().unwrap(); + let workers = runtime + .scheduler() + .block_on(async |cx| cx.control().workers().into_iter().count()) + .unwrap(); + let active = diagnostics.capture(CaptureOptions::default()).unwrap(); + runtime.stop().unwrap(); + let stopped = runtimes().into_iter().find(|runtime| runtime.id == diagnostics.id()).unwrap().state; + + assert_eq!( + (workers, active.name.as_str(), active.state, stopped), + (1, "arty", RuntimeState::Running, RuntimeState::Stopped) + ); +} + +#[test] +fn explicit_monitor_policies_preserve_runtime_behavior() { + let _recording = RecorderGuard::new(); + for policy in [MonitorPolicy::disabled(), MonitorPolicy::enabled()] { + let runtime = Runtime::builder() + .workers(WorkersPolicy::exactly(1)) + .monitor(policy) + .build() + .unwrap(); + + assert_eq!(runtime.scheduler().block_on(async |_| 42).unwrap(), 42); + runtime.stop().unwrap(); + } +} + +#[cfg(feature = "macros")] +#[arty_macros::test(monitor = enabled)] +async fn test_macro_can_enable_monitoring(cx: crate::task::Context) { + let _recording = RecorderGuard::new(); + assert!(super::task::current_events().is_some()); + assert_eq!(cx.scheduler().spawn(async |_| 42).await.unwrap(), 42); +} + +#[test] +fn disabled_monitoring_registers_no_runtime() { + let _recording = RecorderGuard::new(); + let runtime = Runtime::builder() + .name("disabled-monitor") + .monitor(MonitorPolicy::disabled()) + .build() + .unwrap(); + + let value = runtime.scheduler().block_on(async |_| 42).unwrap(); + let registered = runtimes().iter().any(|runtime| runtime.name == "disabled-monitor"); + runtime.stop().unwrap(); + + assert_eq!((value, registered), (42, false)); +} + +#[test] +fn local_and_remote_tasks_are_collected_without_recording() { + let _recording = RecorderGuard::new(); + let runtime = runtime("native-tasks"); + let (started, ready) = mpsc::channel(); + let parent = runtime + .scheduler() + .spawn_anywhere(Unaware(started), |cx, Unaware(started)| async move { + cx.scheduler() + .spawn(move |_| { + let mut started = Some(started); + poll_fn(move |_| { + if let Some(started) = started.take() { + started.send(()).unwrap(); + } + Poll::<()>::Pending + }) + }) + .await + .unwrap(); + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + + let snapshot = capture(&runtime); + runtime.stop().unwrap(); + assert_eq!(snapshot.name, "native-tasks"); + assert!(parent.join().unwrap_err().is_shutdown()); + let observed = snapshot + .tasks + .iter() + .map(|task| { + ( + task.activity.unwrap().state, + task.metrics_scope, + task.spawned_at, + task.last_worker_id.is_some(), + task.future_size_bytes.is_some(), + ) + }) + .collect::>(); + + assert_eq!( + observed, + vec![(TaskActivityState::Waiting, TaskMetricsScope::Uncollected, None, true, true); 2] + ); + assert!( + snapshot + .workers + .iter() + .all(|worker| worker.task_collection == TaskCollection::Complete) + ); +} + +#[test] +fn task_identity_survives_repeated_collection_and_recording_changes() { + let _recording = RecorderGuard::new(); + let runtime = runtime("stable-identities"); + let (started, ready) = mpsc::channel(); + let task = runtime.scheduler().spawn_anywhere(Unaware(started), |_, Unaware(started)| { + let mut started = Some(started); + poll_fn(move |_| { + if let Some(started) = started.take() { + started.send(()).unwrap(); + } + Poll::<()>::Pending + }) + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + let first = capture(&runtime); + runtime.scheduler().block_on(async |_| ()).unwrap(); + + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let second = capture(&runtime); + runtime.scheduler().block_on(async |_| ()).unwrap(); + seismograph::recorder(Configuration::default()); + let third = capture(&runtime); + runtime.stop().unwrap(); + assert!(task.join().unwrap_err().is_shutdown()); + + for snapshot in [&first, &second, &third] { + assert_eq!(snapshot.tasks.len(), 1); + assert!( + snapshot + .workers + .iter() + .all(|worker| worker.task_collection == TaskCollection::Complete) + ); + } + assert_eq!( + (second.tasks[0].id, third.tasks[0].id, second.tasks[0].spawned_at), + (first.tasks[0].id, first.tasks[0].id, None) + ); + assert_eq!( + (second.tasks[0].type_descriptor, third.tasks[0].type_descriptor), + (first.tasks[0].type_descriptor, first.tasks[0].type_descriptor) + ); +} + +#[test] +fn queued_and_running_blocking_tasks_come_from_native_pool_storage() { + let _recording = RecorderGuard::new(); + let runtime = runtime("blocking-tasks"); + let (started, ready) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let first = runtime.scheduler().spawn_blocking(move || { + started.send(()).unwrap(); + released.recv_timeout(TEST_TIMEOUT).unwrap(); + 1 + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + let second = runtime.scheduler().spawn_blocking(|| 2); + + let snapshot = capture(&runtime); + release.send(()).unwrap(); + assert_eq!((first.join().unwrap(), second.join().unwrap()), (1, 2)); + runtime.stop().unwrap(); + let states = snapshot + .tasks + .iter() + .map(|task| { + let worker = snapshot + .workers + .iter() + .find(|worker| Some(worker.id) == task.last_worker_id) + .unwrap(); + (worker.role, task.activity.unwrap().state, task.metrics_scope) + }) + .collect::>(); + + assert_eq!( + states, + [ + (WorkerRole::Blocking, TaskActivityState::Running, TaskMetricsScope::Uncollected), + (WorkerRole::Blocking, TaskActivityState::Ready, TaskMetricsScope::Uncollected), + ] + ); +} + +#[test] +fn draining_queued_blocking_tasks_does_not_report_worker_resumes() { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = runtime("blocking-queue-resumes"); + let (started, ready) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let first = runtime.scheduler().spawn_blocking(move || { + started.send(seismograph::recorder::thread_id()).unwrap(); + released.recv_timeout(TEST_TIMEOUT).unwrap(); + }); + let thread_id = ready.recv_timeout(TEST_TIMEOUT).unwrap(); + let second = runtime.scheduler().spawn_blocking(|| ()); + let third = runtime.scheduler().spawn_blocking(|| ()); + let (started, ready) = mpsc::channel(); + let (finish, finished) = mpsc::channel(); + let last = runtime.scheduler().spawn_blocking(move || { + started.send(()).unwrap(); + finished.recv_timeout(TEST_TIMEOUT).unwrap(); + }); + release.send(()).unwrap(); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + let runtime_snapshot = capture(&runtime); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let snapshot = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + finish.send(()).unwrap(); + first.join().unwrap(); + second.join().unwrap(); + third.join().unwrap(); + last.join().unwrap(); + runtime.stop().unwrap(); + + let worker = runtime_snapshot + .workers + .iter() + .find(|worker| worker.role == WorkerRole::Blocking) + .unwrap(); + let events = snapshot + .events + .events + .iter() + .filter(|event| { + event + .runtime() + .is_some_and(|payload| payload.runtime_id == runtime_snapshot.id && payload.worker_id == Some(worker.id)) + }) + .skip_while(|event| event.kind != EventKind::TaskPollStarted) + .collect::>(); + let polls = events.iter().filter(|event| event.kind == EventKind::TaskPollStarted).count(); + let transitions = events + .iter() + .filter_map(|event| match event.kind { + EventKind::WorkerParked | EventKind::WorkerUnparked => Some(event.kind), + _ => None, + }) + .collect::>(); + assert_eq!((worker.thread_id, polls, transitions), (Some(thread_id), 4, Vec::new())); +} + +#[test] +fn stopped_runtime_does_not_record_new_blocking_tasks() { + let _recording = RecorderGuard::new(); + let runtime = runtime("stopped-blocking-submission"); + let id = runtime.diagnostics().unwrap().id(); + let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); + runtime.stop().unwrap(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let outcome = scheduler.spawn_blocking(|| panic!("a stopped pool must not run a function")).join(); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let snapshot = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + let spawned = snapshot + .events + .events + .iter() + .filter(|event| event.kind == EventKind::TaskSpawned && event.runtime().is_some_and(|payload| payload.runtime_id == id)) + .count(); + + assert_eq!((outcome.unwrap_err().is_shutdown(), spawned), (true, 0)); +} + +#[test] +fn recording_enabled_during_blocking_execution_reports_real_outcomes() { + let _recording = RecorderGuard::new(); + for (panics, stops) in [(false, false), (true, false), (false, true)] { + seismograph::recorder(Configuration::default()); + let runtime = runtime("late-blocking-recording"); + let id = runtime.diagnostics().unwrap().id(); + let control = runtime.scheduler().block_on(async |cx| cx.control().clone()).unwrap(); + let (started, ready) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let running = runtime.scheduler().spawn_blocking(move || { + started.send(()).unwrap(); + released.recv_timeout(TEST_TIMEOUT).unwrap(); + assert!(!panics, "recorded blocking panic"); + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + let queued = stops.then(|| runtime.scheduler().spawn_blocking(|| panic!("canceled function must not run"))); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + if stops { + control.request_stop(); + } + release.send(()).unwrap(); + let outcome = running.join(); + if panics { + assert!(outcome.unwrap_err().is_panic()); + } else { + outcome.unwrap(); + } + if let Some(queued) = queued { + assert!(queued.join().unwrap_err().is_shutdown()); + } + runtime.stop().unwrap(); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let snapshot = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + let tasks = snapshot + .events + .events + .iter() + .filter(|event| { + matches!( + event.kind, + EventKind::TaskSpawned + | EventKind::TaskPollStarted + | EventKind::TaskPollFinished + | EventKind::TaskCompleted + | EventKind::TaskPanicked + | EventKind::TaskCanceled + ) && event.runtime().is_some_and(|payload| payload.runtime_id == id) + }) + .collect::>(); + let outcomes = tasks.iter().map(|event| event.kind).collect::>(); + let expected = if panics { + vec![EventKind::TaskPanicked] + } else if stops { + vec![EventKind::TaskCompleted, EventKind::TaskCanceled] + } else { + vec![EventKind::TaskCompleted] + }; + assert_eq!(outcomes, expected); + let identities = tasks.iter().map(|event| event.runtime().unwrap().subject_id).collect::>(); + assert!(identities.iter().all(|id| *id != 0)); + if stops { + assert_ne!(identities[0], identities[1]); + } + } +} + +#[test] +fn recording_enabled_during_an_async_poll_reports_real_outcomes() { + let _recording = RecorderGuard::new(); + for (panics, stops) in [(false, false), (true, false), (false, true)] { + seismograph::recorder(Configuration::default()); + let runtime = runtime("late-async-recording"); + let id = runtime.diagnostics().unwrap().id(); + let control = runtime.scheduler().block_on(async |cx| cx.control().clone()).unwrap(); + let (started, ready) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let running = runtime.scheduler().spawn_anywhere( + Unaware((started, released, panics, stops)), + |_, Unaware((started, released, panics, stops))| async move { + started.send(()).unwrap(); + released.recv_timeout(TEST_TIMEOUT).unwrap(); + if stops { + pending::<()>().await; + } + assert!(!panics, "recorded async panic"); + }, + ); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + if stops { + control.request_stop(); + } + release.send(()).unwrap(); + let outcome = running.join(); + let expected = if panics { + assert!(outcome.unwrap_err().is_panic()); + EventKind::TaskPanicked + } else if stops { + assert!(outcome.unwrap_err().is_shutdown()); + EventKind::TaskCanceled + } else { + outcome.unwrap(); + EventKind::TaskCompleted + }; + runtime.stop().unwrap(); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let snapshot = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + let tasks = snapshot + .events + .events + .iter() + .filter(|event| { + matches!( + event.kind, + EventKind::TaskSpawned + | EventKind::TaskPollStarted + | EventKind::TaskPollFinished + | EventKind::TaskCompleted + | EventKind::TaskPanicked + | EventKind::TaskCanceled + ) && event.runtime().is_some_and(|payload| payload.runtime_id == id) + }) + .collect::>(); + assert_eq!(tasks.iter().map(|event| event.kind).collect::>(), [expected]); + assert_ne!(tasks[0].runtime().unwrap().subject_id, 0); + } +} + +#[test] +fn recording_reports_real_async_and_blocking_outcomes() { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = runtime("recorded-outcomes"); + let id = runtime.diagnostics().unwrap().id(); + let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); + let completed = scheduler.spawn(async |_| 42); + assert_eq!(completed.join().unwrap(), 42); + assert!( + scheduler + .spawn(async |_| panic!("recorded async panic")) + .join() + .unwrap_err() + .is_panic() + ); + assert_eq!(scheduler.spawn_blocking(|| 7).join().unwrap(), 7); + assert!( + scheduler + .spawn_blocking(|| panic!("recorded blocking panic")) + .join() + .unwrap_err() + .is_panic() + ); + let (started, ready) = mpsc::channel(); + let canceled = scheduler.spawn(move |_| async move { + started.send(()).unwrap(); + pending::<()>().await; + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + + runtime.stop().unwrap(); + assert!(canceled.join().unwrap_err().is_shutdown()); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let snapshot = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + let outcomes = snapshot + .events + .events + .iter() + .filter(|event| event.runtime().is_some_and(|event| event.runtime_id == id)) + .filter_map(|event| match event.kind { + EventKind::TaskCompleted | EventKind::TaskPanicked | EventKind::TaskCanceled => Some(event.kind), + _ => None, + }) + .collect::>(); + + let mut starts = Vec::new(); + let mut finishes = Vec::new(); + for event in &snapshot.events.events { + if let Some(payload) = event.runtime() + && payload.runtime_id == id + { + match event.kind { + EventKind::TaskPollStarted => starts.push(payload.subject_id), + EventKind::TaskPollFinished => finishes.push(payload.subject_id), + _ => {} + } + } + } + starts.sort_unstable(); + finishes.sort_unstable(); + assert_eq!( + ( + outcomes.iter().filter(|kind| **kind == EventKind::TaskCompleted).count(), + outcomes.iter().filter(|kind| **kind == EventKind::TaskPanicked).count(), + outcomes.iter().filter(|kind| **kind == EventKind::TaskCanceled).count(), + starts.len(), + starts == finishes, + ), + (3, 2, 1, 6, true) + ); +} + +#[test] +fn native_tasks_reuse_their_recorded_spawn_timestamp_and_identity() { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = runtime("coherent-spawn-metadata"); + let (started, ready) = mpsc::channel(); + let asynchronous = runtime + .scheduler() + .spawn_anywhere(Unaware(started.clone()), |_, Unaware(started)| async move { + started.send(()).unwrap(); + pending::<()>().await; + }); + let (release, released) = mpsc::channel(); + let blocking = runtime.scheduler().spawn_blocking(move || { + started.send(()).unwrap(); + released.recv().unwrap(); + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + + let native = capture(&runtime); + release.send(()).unwrap(); + blocking.join().unwrap(); + runtime.stop().unwrap(); + assert!(asynchronous.join().unwrap_err().is_shutdown()); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let recorded = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + + assert_eq!(native.tasks.len(), 2); + for task in &native.tasks { + let spawn = recorded + .events + .events + .iter() + .find(|event| { + event.kind == EventKind::TaskSpawned + && event + .runtime() + .is_some_and(|payload| payload.runtime_id == native.id && payload.subject_id == task.id.get()) + }) + .unwrap(); + let payload = spawn.runtime().unwrap(); + assert_eq!( + (task.spawned_at, task.type_descriptor.get(), task.future_size_bytes), + (Some(spawn.timestamp), payload.value_0, Some(payload.value_1 - 1)) + ); + } +} + +struct PendingBody { + started: Option>, + release: Option>, + _payload: [u8; N], +} + +impl PendingBody { + fn new(started: mpsc::Sender<()>) -> Self { + Self { + started: Some(started), + release: None, + _payload: [0; N], + } + } +} + +impl Future for PendingBody { + type Output = (); + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { + if let Some(started) = self.started.take() { + started.send(()).unwrap(); + } + self.release + .as_mut() + .map_or(Poll::Pending, |release| Pin::new(release).poll(cx).map(Result::unwrap)) + } +} + +#[test] +fn async_task_sizes_precede_wrapping_and_erasure() { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = runtime("original-body-sizes"); + let (scheduler, destination) = runtime + .scheduler() + .block_on(async |cx| (cx.scheduler().clone(), cx.thread().clone())) + .unwrap(); + let (started, ready) = mpsc::channel(); + let anywhere = runtime + .scheduler() + .spawn_anywhere(Unaware(started.clone()), |_, Unaware(started)| PendingBody::<64>::new(started)); + let at = scheduler.spawn_at(&destination, Unaware(started.clone()), |Unaware(started)| { + PendingBody::<128>::new(started) + }); + let (release, released) = futures::channel::oneshot::channel(); + let native = std::thread::scope(|scope| { + let borrowed = &started; + let runtime = &runtime; + let blocked = scope.spawn(move || { + runtime.scheduler().block_on(|_| { + let mut body = PendingBody::<256>::new(borrowed.clone()); + body.release = Some(released); + body + }) + }); + for _ in 0..3 { + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + } + let native = capture(runtime); + release.send(()).unwrap(); + blocked.join().unwrap().unwrap(); + native + }); + runtime.stop().unwrap(); + assert!(anywhere.join().unwrap_err().is_shutdown()); + assert!(at.join().unwrap_err().is_shutdown()); + let mut sizes = native.tasks.iter().map(|task| task.future_size_bytes.unwrap()).collect::>(); + sizes.sort_unstable(); + assert_eq!( + sizes, + [ + u64::try_from(size_of::>()).unwrap(), + u64::try_from(size_of::>()).unwrap(), + u64::try_from(size_of::>()).unwrap(), + ] + ); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let recorded = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + for task in &native.tasks { + let spawn = recorded + .events + .events + .iter() + .find(|event| { + event.kind == EventKind::TaskSpawned + && event + .runtime() + .is_some_and(|payload| payload.runtime_id == native.id && payload.subject_id == task.id.get()) + }) + .unwrap(); + assert_eq!(task.future_size_bytes, Some(spawn.runtime().unwrap().value_1 - 1)); + } +} + +#[test] +fn queued_remote_children_retain_their_recorded_parent() { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = Runtime::builder() + .workers(WorkersPolicy::exactly(2)) + .name("remote-parent-links") + .build() + .unwrap(); + let (started, ready) = mpsc::channel(); + let parent = runtime + .scheduler() + .spawn_anywhere(Unaware(started), |cx, Unaware(started)| async move { + let destination = cx + .control() + .workers() + .into_iter() + .find(|worker| *worker != cx.thread()) + .unwrap() + .clone(); + let at = cx.scheduler().spawn_at(&destination, Unaware(started.clone()), |Unaware(started)| { + PendingBody::<64>::new(started) + }); + let anywhere = cx + .scheduler() + .spawn_anywhere(Unaware(started), |Unaware(started)| PendingBody::<128>::new(started)); + at.await.unwrap(); + anywhere.await.unwrap(); + }); + for _ in 0..2 { + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + } + let native = capture(&runtime); + runtime.stop().unwrap(); + assert!(parent.join().unwrap_err().is_shutdown()); + assert_eq!(native.tasks.len(), 3); + let root = native.tasks.iter().find(|task| task.parent.is_none()).unwrap(); + let children = native.tasks.iter().filter(|task| task.id != root.id).collect::>(); + assert!(children.iter().all(|task| task.parent == Some(root.id))); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let recorded = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + for child in children { + let spawn = recorded + .events + .events + .iter() + .find(|event| { + event.kind == EventKind::TaskSpawned + && event + .runtime() + .is_some_and(|payload| payload.runtime_id == native.id && payload.subject_id == child.id.get()) + }) + .unwrap(); + assert_eq!(spawn.runtime().unwrap().related_id, root.id.get()); + } +} + +#[test] +fn blocking_children_retain_their_recorded_parent() { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = runtime("blocking-parent-links"); + let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); + let (started, ready) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let parent = runtime.scheduler().spawn_blocking(move || { + let child = scheduler.spawn_blocking(|| 42); + started.send(child).unwrap(); + released.recv_timeout(TEST_TIMEOUT).unwrap(); + panic!("recorded parent panic"); + }); + let child = ready.recv_timeout(TEST_TIMEOUT).unwrap(); + let native = capture(&runtime); + release.send(()).unwrap(); + assert!(parent.join().unwrap_err().is_panic()); + assert_eq!(child.join().unwrap(), 42); + runtime.stop().unwrap(); + assert_eq!(native.tasks.len(), 2); + let parent = native + .tasks + .iter() + .find(|task| task.activity.unwrap().state == TaskActivityState::Running) + .unwrap(); + let child = native + .tasks + .iter() + .find(|task| task.activity.unwrap().state == TaskActivityState::Ready) + .unwrap(); + assert_eq!(child.parent, Some(parent.id)); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let recorded = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + let spawn = recorded + .events + .events + .iter() + .find(|event| { + event.kind == EventKind::TaskSpawned + && event + .runtime() + .is_some_and(|payload| payload.runtime_id == native.id && payload.subject_id == child.id.get()) + }) + .unwrap(); + assert_eq!(spawn.runtime().unwrap().related_id, parent.id.get()); +} + +#[test] +fn collection_reports_the_native_task_limit_instead_of_claiming_completeness() { + let _recording = RecorderGuard::new(); + let runtime = runtime("bounded-tasks"); + let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); + for _ in 0..260 { + drop(scheduler.spawn(async |_| pending::<()>().await)); + } + let (started, ready) = mpsc::channel(); + drop(scheduler.spawn(move |_| async move { + started.send(()).unwrap(); + pending::<()>().await; + })); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + + let snapshot = capture(&runtime); + runtime.stop().unwrap(); + let core = snapshot.workers.iter().find(|worker| worker.role == WorkerRole::Core).unwrap(); + + assert_eq!((snapshot.tasks.len(), core.task_collection), (256, TaskCollection::Truncated)); +} + +#[test] +fn a_blocked_async_worker_times_out_and_can_answer_a_later_request() { + let _recording = RecorderGuard::new(); + let runtime = runtime("blocked-worker"); + let (started, ready) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let task = runtime + .scheduler() + .spawn_anywhere(Unaware((started, released)), |_, Unaware((started, released))| async move { + started.send(()).unwrap(); + released.recv_timeout(TEST_TIMEOUT).unwrap(); + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + + let blocked = runtime + .diagnostics() + .unwrap() + .capture_with_timeout(CaptureOptions::default()) + .unwrap(); + release.send(()).unwrap(); + task.join().unwrap(); + // Completion precedes the next command batch, which retires the canceled request. + runtime.scheduler().block_on(async |_| ()).unwrap(); + let recovered = capture(&runtime); + runtime.stop().unwrap(); + let core_status = |snapshot: &RuntimeSnapshot| { + snapshot + .workers + .iter() + .find(|worker| worker.role == WorkerRole::Core) + .unwrap() + .task_collection + }; + + assert_eq!( + (core_status(&blocked), core_status(&recovered)), + (TaskCollection::TimedOut, TaskCollection::Complete) + ); +} + +#[test] +fn stopping_one_runtime_retires_only_its_workers() { + let _recording = RecorderGuard::new(); + let first = runtime("runtime-one"); + let second = runtime("runtime-two"); + + first.stop().unwrap(); + let first = runtimes().into_iter().find(|runtime| runtime.name == "runtime-one").unwrap(); + let second_snapshot = capture(&second); + second.stop().unwrap(); + + assert_eq!( + (first.state, first.tasks.is_empty(), second_snapshot.state), + (RuntimeState::Stopped, true, RuntimeState::Running) + ); +} + +#[test] +fn shutdown_records_cancellation_for_a_queued_blocking_function() { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = runtime("canceled-blocking"); + let control = runtime.scheduler().block_on(async |cx| cx.control().clone()).unwrap(); + let (started, ready) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let running = runtime.scheduler().spawn_blocking(move || { + started.send(()).unwrap(); + released.recv_timeout(TEST_TIMEOUT).unwrap(); + }); + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + let queued = runtime.scheduler().spawn_blocking(|| panic!("a canceled function must not run")); + let snapshot = capture(&runtime); + let id = snapshot + .tasks + .iter() + .find(|task| task.activity.unwrap().state == TaskActivityState::Ready) + .unwrap() + .id; + + control.request_stop(); + release.send(()).unwrap(); + runtime.stop().unwrap(); + running.join().unwrap(); + let canceled = queued.join().unwrap_err(); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let events = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + let outcomes = events + .events + .events + .iter() + .filter_map(|event| { + event + .runtime() + .filter(|payload| { + payload.runtime_id == snapshot.id + && payload.subject_id == id.get() + && matches!( + event.kind, + EventKind::TaskCompleted + | EventKind::TaskPanicked + | EventKind::TaskCanceled + | EventKind::TaskPollStarted + | EventKind::TaskPollFinished + ) + }) + .map(|_| event.kind) + }) + .collect::>(); + + assert_eq!((canceled.is_shutdown(), outcomes), (true, vec![EventKind::TaskCanceled])); +} + +#[test] +fn task_sizes_describe_user_futures_before_runtime_wrapping() { + struct Body { + started: Option>, + _data: [u8; 37], + } + + impl Future for Body { + type Output = (); + + fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<()> { + if let Some(started) = self.get_mut().started.take() { + started.send(()).unwrap(); + } + Poll::Pending + } + } + + let _recording = RecorderGuard::new(); + let runtime = runtime("user-bodies"); + let (started, ready) = mpsc::channel(); + drop( + runtime + .scheduler() + .spawn_anywhere(Unaware(started.clone()), |_, Unaware(started)| Body { + started: Some(started), + _data: [0; 37], + }), + ); + let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); + drop(scheduler.spawn_anywhere(Unaware(started.clone()), |Unaware(started)| Body { + started: Some(started), + _data: [0; 37], + })); + runtime + .scheduler() + .block_on(async move |cx| { + drop(cx.scheduler().spawn(move |_| Body { + started: Some(started), + _data: [0; 37], + })); + }) + .unwrap(); + for _ in 0..3 { + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + } + + let snapshot = capture(&runtime); + runtime.stop().unwrap(); + let sizes = snapshot.tasks.iter().map(|task| task.future_size_bytes).collect::>(); + + assert_eq!(sizes, vec![Some(size_of::() as u64); 3]); +} + +fn growing_pool(name: &str, policy: BlockingPoolPolicy, expected_blocking_workers: usize) { + let _recording = RecorderGuard::new(); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let runtime = Runtime::builder() + .workers(WorkersPolicy::exactly(2)) + .blocking_pool(policy.max(2)) + .name(name) + .build() + .unwrap(); + let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); + let release = Arc::new((Mutex::new(false), Condvar::new())); + let (started, ready) = mpsc::channel(); + let tasks = (0..6) + .map(|_| { + let release = Arc::clone(&release); + let started = started.clone(); + scheduler.spawn_blocking(move || { + started.send(()).unwrap(); + let (released, changed) = &*release; + let mut released = released.lock().unwrap(); + while !*released { + let waited = changed.wait_timeout(released, TEST_TIMEOUT).unwrap(); + assert!(!waited.1.timed_out(), "the test must release its blocking functions"); + released = waited.0; + } + }) + }) + .collect::>(); + for _ in 0..2 { + ready.recv_timeout(TEST_TIMEOUT).unwrap(); + } + + let snapshot = capture(&runtime); + *release.0.lock().unwrap() = true; + release.1.notify_all(); + for task in tasks { + task.join().unwrap(); + } + runtime.stop().unwrap(); + let bytes = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let events = seismograph::snapshot::decode(bytes.as_bytes()).unwrap(); + let mut matched_workers = Vec::new(); + for task in &snapshot.tasks { + let worker_for = |kind| { + events + .events + .events + .iter() + .find_map(|event| { + event + .runtime() + .filter(|payload| event.kind == kind && payload.runtime_id == snapshot.id && payload.subject_id == task.id.get()) + .and_then(|payload| payload.worker_id) + }) + .unwrap() + }; + matched_workers.push(worker_for(EventKind::TaskPollStarted) == worker_for(EventKind::TaskCompleted)); + } + let running = snapshot + .tasks + .iter() + .filter(|task| task.activity.unwrap().state == TaskActivityState::Running) + .count(); + let queued = snapshot + .tasks + .iter() + .filter(|task| task.activity.unwrap().state == TaskActivityState::Ready) + .count(); + let workers = snapshot.workers.iter().filter(|worker| worker.role == WorkerRole::Blocking).count(); + + assert_eq!( + (workers, running, queued, matched_workers), + (expected_blocking_workers, 2, 4, vec![true; 6]) + ); +} + +#[test] +fn a_shared_pool_registers_new_threads_and_records_outcomes_on_the_execution_worker() { + growing_pool("growing-shared-pool", BlockingPoolPolicy::shared(), 2); +} + +#[test] +fn a_per_worker_pool_registers_new_threads_without_merging_other_pools() { + growing_pool("growing-per-worker-pool", BlockingPoolPolicy::per_worker(), 3); +} diff --git a/crates/arty/src/runtime/worker/asynchronous/command.rs b/crates/arty/src/runtime/worker/asynchronous/command.rs index 54394f80c..a0b72d28e 100644 --- a/crates/arty/src/runtime/worker/asynchronous/command.rs +++ b/crates/arty/src/runtime/worker/asynchronous/command.rs @@ -71,6 +71,10 @@ pub(crate) enum SystemCommand { #[debug("AccessIo")] AccessIo { access: QueuedIoAccess }, + /// Copies task descriptions from native executor storage between cycles. + #[cfg(feature = "monitor")] + #[debug("Inspect")] + Inspect { request: seismograph_runtime::task::TaskRequest }, /// Appends one worker-local runtime hook. #[debug("RegisterHook")] RegisterHook { hook: HookRegistration, source: Option }, @@ -199,6 +203,7 @@ mod tests { #[test] fn queued_factory_into_inner_returns_the_factory() { let runtime = crate::runtime::Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -219,7 +224,12 @@ mod tests { #[test] fn queued_factory_disposal_contains_join_notification_panic() { let result_pool = crate::task::handle::JoinHandlePool::new(crate::runtime::shutdown::Shutdown::default()); - let (factory, join) = prepare_remote(&result_pool, |_| async { 42 }, TaskPlacement::Any); + let (factory, join) = prepare_remote( + &result_pool, + |_| async { 42 }, + TaskPlacement::Any, + crate::runtime::monitoring::TaskMonitoring::capture(), + ); let mut join = pin!(join); let wake = Arc::new(PanicWake(AtomicBool::new(false))); let waker = Waker::from(Arc::clone(&wake)); diff --git a/crates/arty/src/runtime/worker/asynchronous/local_worker.rs b/crates/arty/src/runtime/worker/asynchronous/local_worker.rs index 9494bde9d..df8facb7f 100644 --- a/crates/arty/src/runtime/worker/asynchronous/local_worker.rs +++ b/crates/arty/src/runtime/worker/asynchronous/local_worker.rs @@ -227,7 +227,7 @@ impl SchedulerContext { { let (handle, result_tx) = self.results.handle(); let inner = DeferredTaskFactory::new(future_factory, self.context.clone()); - self.tasks.task(inner).placement(placement).register(result_tx); + crate::runtime::monitoring::add_task::(&self.tasks, inner, placement, result_tx); handle } } diff --git a/crates/arty/src/runtime/worker/asynchronous/thread.rs b/crates/arty/src/runtime/worker/asynchronous/thread.rs index 9b954178b..c32d99274 100644 --- a/crates/arty/src/runtime/worker/asynchronous/thread.rs +++ b/crates/arty/src/runtime/worker/asynchronous/thread.rs @@ -148,7 +148,12 @@ impl ThreadEntry { let signal = Arc::new(asynchronous::Signal::default()); let worker_waker = asynchronous::Signal::waker(&signal); - let blocking_worker = blocking::Worker::new(blocking_pools.pool_for_worker(worker_index), shutdown); + let blocking_pool = blocking_pools.pool_for_worker(worker_index); + #[cfg(feature = "monitor")] + if let Some(runtime) = monitor_options.runtime() { + blocking_pool.enable_monitoring(runtime); + } + let blocking_worker = blocking::Worker::new(blocking_pool, shutdown); endpoint_tx .send(Endpoint { diff --git a/crates/arty/src/runtime/worker/asynchronous/worker.rs b/crates/arty/src/runtime/worker/asynchronous/worker.rs index 1b99582b9..77c92f97c 100644 --- a/crates/arty/src/runtime/worker/asynchronous/worker.rs +++ b/crates/arty/src/runtime/worker/asynchronous/worker.rs @@ -109,6 +109,8 @@ pub(crate) struct Worker { cycle_clock: SimpleClock, signal: Arc, monitor_options: MonitorOptions, + #[cfg(feature = "monitor")] + diagnostics: Option, _single_threaded: PhantomData<*const ()>, } @@ -156,6 +158,10 @@ impl Worker { let tasks = executor.tasks(); let executor_shutdown = executor.shutdown(); + #[cfg(feature = "monitor")] + let diagnostics = monitor_options + .runtime() + .map(|runtime| crate::runtime::monitoring::WorkerDiagnostics::new(runtime, &dispatcher, &thread)); let context = Context::new(dispatcher, clock, io, thread); LocalWorker::install_scheduler(context.clone(), tasks.clone(), executor_shutdown, hook_registrations); @@ -171,6 +177,8 @@ impl Worker { cycle_clock, signal, monitor_options, + #[cfg(feature = "monitor")] + diagnostics, _single_threaded: PhantomData, } } @@ -179,9 +187,7 @@ impl Worker { /// The configuration-facing `RuntimeBuilder` delegates construction to bootstrap. /// After this method returns, the thread will end. pub(crate) fn run(&mut self) { - // Keep the worker-owned policy available at the boundary where monitoring - // will be attached in a later stage. - let _monitor_policy = self.monitor_options.policy(); + let _monitoring_enabled = self.monitor_options.policy().is_enabled(); self.execute_phase(); } @@ -197,6 +203,13 @@ impl Worker { CycleAction::Poll | CycleAction::WaitForSignal(_) => Duration::ZERO, }; + #[cfg(feature = "monitor")] + if !matches!(action, CycleAction::Poll) + && let Some(diagnostics) = &self.diagnostics + { + diagnostics.parked(); + } + if RuntimeDriverHost::execute_cycle_current(max_wait).is_err() { return self.finish_shutdown(); } @@ -208,6 +221,13 @@ impl Worker { } } + #[cfg(feature = "monitor")] + if !matches!(action, CycleAction::Poll) + && let Some(diagnostics) = &self.diagnostics + { + diagnostics.unparked(); + } + ControlFlow::Continue(()) } @@ -229,6 +249,12 @@ impl Worker { .expect("system command processing only succeeds while the worker is ready") .executor; + #[cfg(feature = "monitor")] + let cycle_outcome = match &self.diagnostics { + Some(diagnostics) if diagnostics.is_observed() => executor.execute_cycle_observed(diagnostics), + _ => executor.execute_cycle(), + }; + #[cfg(not(feature = "monitor"))] let cycle_outcome = executor.execute_cycle(); if let CycleOutcome::Shutdown(cause) = cycle_outcome { @@ -279,19 +305,25 @@ impl Worker { ControlFlow::Break(()) } - fn process_system_commands(&self) -> Option { - let Some(ReadyWorkerState { commands, context, .. }) = &self.state else { + fn process_system_commands(&mut self) -> Option { + let Some(state) = &mut self.state else { return None; }; let mut processed = 0_usize; - for command in std::iter::from_fn(|| commands.system_receiver().try_recv().ok()).take(COMMANDS_PER_CYCLE) { + for command in std::iter::from_fn(|| state.commands.system_receiver().try_recv().ok()).take(COMMANDS_PER_CYCLE) { if matches!(self.shutdown.state(), ShutdownState::Started) { return None; } match command { SystemCommand::AccessIo { access } => { - access.into_inner()(context); + access.into_inner()(&state.context); + } + #[cfg(feature = "monitor")] + SystemCommand::Inspect { request } => { + if let Some(diagnostics) = &self.diagnostics { + diagnostics.collect(&mut state.executor, request); + } } SystemCommand::RegisterHook { hook, source } => { LocalWorker::install_hook(hook, source.as_ref()); @@ -346,6 +378,8 @@ impl Worker { // Safe executor shutdown synchronously cancels and releases all remaining task storage. executor.shutdown().trigger(cause); drop(executor); + #[cfg(feature = "monitor")] + drop(self.diagnostics.take()); let driver_host = RuntimeDriverHost::uninstall(); drop(driver_host); } @@ -1192,6 +1226,48 @@ mod tests { assert_eq!(processed.load(Ordering::Relaxed), COMMANDS_PER_CYCLE + 1); } + #[cfg(feature = "monitor")] + #[test] + fn inspection_without_worker_diagnostics_reports_failure() { + use seismograph_runtime::snapshot::{CaptureOptions, TaskCollection}; + use seismograph_runtime::worker::{WorkerMetadata, WorkerRole}; + + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let (commands, command_rx) = command_channel(); + let mut worker = test_worker( + (commands.clone(), command_rx), + test_blocking_worker(), + InactiveClock::new_isolated(), + Arc::new(Signal::default()), + ); + let runtime = seismograph_runtime::register_runtime(seismograph_runtime::RuntimeMetadata::new("missing-worker-diagnostics", 1)); + let registration = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let (sender, receiver) = std::sync::mpsc::channel(); + registration + .handle() + .register_task_provider(move |request| sender.send(request).unwrap()) + .unwrap(); + + let snapshot = std::thread::scope(|scope| { + let handle = runtime.handle(); + let captured = scope.spawn(move || { + handle + .capture(CaptureOptions { + timeout: Duration::from_secs(5), + ..CaptureOptions::default() + }) + .unwrap() + }); + let request = receiver.recv_timeout(TEST_TIMEOUT).unwrap(); + commands.system_sender().send(SystemCommand::Inspect { request }).unwrap(); + assert_eq!(worker.process_system_commands(), Some(false)); + captured.join().unwrap() + }); + + assert!(snapshot.tasks.is_empty()); + assert_eq!(snapshot.workers[0].task_collection, TaskCollection::Failed); + } + #[test] fn system_commands_arriving_during_processing_run_in_the_same_cycle() { let (commands, command_rx) = command_channel(); @@ -1254,7 +1330,9 @@ mod tests { }))) .unwrap(); } - let join = worker_context(&worker).dispatcher().spawn(|_| async {}); + let join = worker_context(&worker) + .dispatcher() + .spawn(|_| async {}, crate::runtime::monitoring::TaskMonitoring::capture()); commands.system_sender().send(SystemCommand::Shutdown).unwrap(); assert!(worker.execute_step().is_break()); @@ -1305,6 +1383,95 @@ mod tests { assert!(!worker.monitor_options.policy().is_enabled()); } + #[cfg(all(feature = "monitor", feature = "test-util", not(miri)))] + #[test] + fn monitoring_cycles_only_park_when_waiting_and_leave_idle_polls_unobserved() { + use seismograph_runtime::RuntimeHandle; + use seismograph_runtime::snapshot::{CaptureOptions, WorkerState}; + + #[derive(Clone)] + struct ProbeContext(RuntimeHandle, Arc>>); + + impl ThreadAware for ProbeContext { + fn relocate(&mut self, _source: Option<&Thread>, _destination: &Thread) {} + } + + impl IoContext for ProbeContext { + type ContextOptions = Self; + type Driver = Self; + + fn new(options: Self) -> Self { + options + } + + fn driver(&mut self, _options: DriverOptions) -> Result { + Ok(self.clone()) + } + } + + impl Driver for ProbeContext { + fn waker(&self) -> Waker { + Waker::noop().clone() + } + + fn execute_cycle(&mut self, _cycle: &mut Cycle) -> Result<(), DriverError> { + let snapshot = self.0.capture_with_timeout(CaptureOptions::default()).unwrap(); + self.1.lock().unwrap().push(snapshot.workers[0].state); + Ok(()) + } + + fn shutdown(self) -> Result<(), DriverError> { + Ok(()) + } + } + + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(seismograph_runtime::RuntimeMetadata::new("cycle-observation", 1)); + let mut options = MonitorOptions::default(); + options.set_runtime(Some(runtime.handle())); + let (commands, command_rx) = command_channel(); + let mut worker = test_worker_with_monitor_options( + (commands.clone(), command_rx), + test_blocking_worker(), + InactiveClock::new_isolated(), + Arc::new(Signal::default()), + options, + ); + let polled = std::rc::Rc::new(std::cell::Cell::new(false)); + let completed = std::rc::Rc::clone(&polled); + crate::runtime::monitoring::add_task::<(), _, _, _>( + &worker.tasks, + async move { + assert_eq!(crate::runtime::monitoring::current_task(), None); + completed.set(true); + }, + arty_executor::TaskPlacement::Any, + DiscardCompletion, + ); + seismograph::recorder::without_recording(|| { + worker.prepare_cycle(); + }); + assert!(polled.get()); + + let states = Arc::new(std::sync::Mutex::new(Vec::new())); + let _io = worker_context(&worker) + .io() + .context::(&ProbeContext(runtime.handle(), Arc::clone(&states))); + assert!(worker.execute_step().is_continue()); + assert_eq!( + runtime.handle().capture_with_timeout(CaptureOptions::default()).unwrap().workers[0].state, + WorkerState::Running + ); + for _ in 0..COMMANDS_PER_CYCLE { + commands.spawn_sender().send(enqueue(RemoteFutureFactory::new(|_, _| {}))).unwrap(); + } + assert!(worker.execute_step().is_continue()); + + assert_eq!(*states.lock().unwrap(), vec![WorkerState::Parked, WorkerState::Running]); + commands.system_sender().send(SystemCommand::Shutdown).unwrap(); + worker.run(); + } + #[cfg(not(miri))] #[test] fn worker_without_primary_waits_for_timer_or_notification() { @@ -1936,13 +2103,16 @@ mod tests { }))) .unwrap(); let invoked = Arc::new(AtomicBool::new(false)); - let join = worker_context(&worker).dispatcher().spawn({ - let invoked = Arc::clone(&invoked); - move |_| { - invoked.store(true, Ordering::Relaxed); - std::future::ready(()) - } - }); + let join = worker_context(&worker).dispatcher().spawn( + { + let invoked = Arc::clone(&invoked); + move |_| { + invoked.store(true, Ordering::Relaxed); + std::future::ready(()) + } + }, + crate::runtime::monitoring::TaskMonitoring::capture(), + ); assert!(worker.execute_step().is_break()); diff --git a/crates/arty/src/runtime/worker/blocking/mod.rs b/crates/arty/src/runtime/worker/blocking/mod.rs index 1669e0656..75650a680 100644 --- a/crates/arty/src/runtime/worker/blocking/mod.rs +++ b/crates/arty/src/runtime/worker/blocking/mod.rs @@ -8,6 +8,7 @@ mod local_worker; mod pools; +mod queue; mod worker; pub(crate) use local_worker::LocalWorker; diff --git a/crates/arty/src/runtime/worker/blocking/queue.rs b/crates/arty/src/runtime/worker/blocking/queue.rs new file mode 100644 index 000000000..34e777208 --- /dev/null +++ b/crates/arty/src/runtime/worker/blocking/queue.rs @@ -0,0 +1,447 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! Native storage for queued blocking functions and active worker slots. +//! Worker handles are retained on their execution thread. The pool shares one +//! immutable task-event context so submissions do not lock this queue for it. + +use std::collections::VecDeque; +use std::sync::{Arc, Condvar, Mutex}; + +#[cfg(feature = "monitor")] +use crate::runtime::monitoring::MonitoredBlockingJob; +use crate::runtime::monitoring::{BlockingJob, BlockingPoolMonitoring, BlockingWorkerMonitoring}; + +pub(super) struct QueuedJob { + pub(super) body: BlockingJob, + pub(super) shutdown: Option, +} + +#[derive(Default)] +struct WorkerSlot { + #[cfg(feature = "monitor")] + running: bool, + #[cfg(feature = "monitor")] + registration: Option, + #[cfg(feature = "monitor")] + current: Option>, +} + +#[derive(Default)] +struct State { + jobs: VecDeque, + workers: Vec, + stopped: bool, + #[cfg(feature = "monitor")] + runtime: Option, +} + +#[derive(Default)] +pub(super) struct Queue { + state: Mutex, + ready: Condvar, + pub(super) monitoring: BlockingPoolMonitoring, +} + +impl Queue { + pub(super) fn push(&self, job: QueuedJob) -> bool { + let mut state = self + .state + .lock() + .expect("a blocking queue operation must not panic while holding its lock"); + if state.stopped { + drop(state); + drop(job); + return false; + } + state.jobs.push_back(job); + drop(state); + self.ready.notify_one(); + true + } + + pub(super) fn add_worker(self: &Arc) -> usize { + let mut state = self + .state + .lock() + .expect("a blocking queue operation must not panic while holding its lock"); + let index = state.workers.len(); + #[cfg(feature = "monitor")] + let registration = state.runtime.as_ref().map(|runtime| self.register_worker(runtime, index)); + let slot = WorkerSlot::default(); + #[cfg(feature = "monitor")] + let slot = WorkerSlot { registration, ..slot }; + state.workers.push(slot); + index + } + + #[cfg_attr( + not(feature = "monitor"), + expect( + clippy::needless_pass_by_ref_mut, + reason = "The worker handle is assigned only when monitoring is compiled in." + ) + )] + pub(super) fn pop(&self, index: usize, worker: &mut BlockingWorkerMonitoring) -> Option { + #[cfg(not(feature = "monitor"))] + let _ = index; + // Drop the previous job after the queue guard, including on early return. + #[cfg(feature = "monitor")] + let mut previous; + let mut state = self + .state + .lock() + .expect("a blocking queue operation must not panic while holding its lock"); + #[cfg(feature = "monitor")] + { + let slot = &mut state.workers[index]; + previous = slot.current.take(); + slot.running = false; + } + loop { + #[cfg(feature = "monitor")] + if worker.handle.is_none() + && let Some(registration) = &state.workers[index].registration + { + let handle = registration.handle(); + handle.attach_current_thread(); + worker.handle = Some(handle); + } + if let Some(job) = state.jobs.pop_front() { + #[cfg(feature = "monitor")] + if worker.handle.is_some() { + state.workers[index].running = true; + state.workers[index].current = job.body.monitoring().cloned(); + } + return Some(job); + } + if state.stopped { + return None; + } + #[cfg(feature = "monitor")] + if previous.is_some() { + drop(state); + drop(previous.take()); + state = self + .state + .lock() + .expect("a blocking queue operation must not panic while holding its lock"); + continue; + } + worker.parked(); + state = self + .ready + .wait(state) + .expect("a blocking queue operation must not panic while holding its lock"); + worker.unparked(); + } + } + + pub(super) fn stop(&self) { + self.state + .lock() + .expect("a blocking queue operation must not panic while holding its lock") + .stopped = true; + self.ready.notify_all(); + } + + #[cfg_attr( + not(feature = "monitor"), + expect( + clippy::unused_self, + reason = "There is no worker registration to retire when monitoring is compiled out." + ) + )] + pub(super) fn retire(&self, index: usize) { + #[cfg(not(feature = "monitor"))] + let _ = index; + #[cfg(feature = "monitor")] + { + let mut state = self + .state + .lock() + .expect("a blocking queue operation must not panic while holding its lock"); + let registration = state.workers[index].registration.take(); + drop(state); + drop(registration); + } + } + + #[cfg(feature = "monitor")] + pub(super) fn enable_monitoring(self: &Arc, runtime: &seismograph_runtime::RuntimeHandle) { + let mut state = self + .state + .lock() + .expect("a blocking queue operation must not panic while holding its lock"); + if state.runtime.is_some() { + return; + } + for (index, slot) in state.workers.iter_mut().enumerate() { + slot.registration = Some(self.register_worker(runtime, index)); + } + let events = state + .workers + .first() + .and_then(|slot| slot.registration.as_ref()) + .expect("a blocking pool starts with at least one worker") + .handle() + .task_events(); + self.monitoring.enable(events); + state.runtime = Some(runtime.clone()); + drop(state); + self.ready.notify_all(); + } + + #[cfg(feature = "monitor")] + fn register_worker( + self: &Arc, + runtime: &seismograph_runtime::RuntimeHandle, + index: usize, + ) -> seismograph_runtime::worker::WorkerRegistration { + use seismograph_runtime::worker::{WorkerMetadata, WorkerRole}; + + let registration = runtime.register_worker(WorkerMetadata::new(WorkerRole::Blocking)); + let queue = Arc::downgrade(self); + registration + .handle() + .register_task_provider(move |request| { + if let Some(queue) = queue.upgrade() { + queue.collect(index, request); + } + }) + .expect("a newly registered blocking worker has not stopped"); + registration + } + + #[cfg(feature = "monitor")] + fn collect(&self, index: usize, request: seismograph_runtime::task::TaskRequest) { + use seismograph_runtime::snapshot::{TaskActivityState, TaskCollection}; + + if request.is_canceled() { + return; + } + let state = self + .state + .lock() + .expect("a blocking queue operation must not panic while holding its lock"); + let current = state.workers[index] + .running + .then_some(( + state.workers[index].current.as_ref().map(|body| body.info()), + TaskActivityState::Running, + )) + .into_iter(); + // The first worker owns inspection of the shared submission queue. + let queued = state + .jobs + .iter() + .take(if index == 0 { usize::MAX } else { 0 }) + .map(|job| (job.body.monitoring().map(|body| body.info()), TaskActivityState::Ready)); + let mut descriptions = current.chain(queued); + let tasks = descriptions + .by_ref() + .take(request.limit()) + .filter_map(|(info, activity)| info.map(|info| info.describe(activity))) + .collect(); + let status = if descriptions.next().is_some() { + TaskCollection::Truncated + } else { + TaskCollection::Complete + }; + drop(state); + request.respond(tasks, status); + } +} + +#[cfg(all(test, feature = "monitor", feature = "test-util"))] +mod tests { + use seismograph_runtime::RuntimeMetadata; + use seismograph_runtime::snapshot::{CaptureOptions, TaskActivityState, TaskCollection, WorkerState}; + use seismograph_runtime::worker::{WorkerMetadata, WorkerRole}; + + use super::*; + use crate::runtime::monitoring::TaskInfo; + + struct TestJob(TaskInfo); + + impl MonitoredBlockingJob for TestJob { + fn info(&self) -> &TaskInfo { + &self.0 + } + + fn run(&self) {} + } + + #[test] + fn blocking_slots_advance_and_retire_without_retaining_a_running_task() { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("blocking-slots", 1)); + let queue = Arc::new(Queue::default()); + let index = queue.add_worker(); + queue.enable_monitoring(&runtime.handle()); + queue.enable_monitoring(&runtime.handle()); + let body: Arc = Arc::new(TestJob(TaskInfo::new::<()>(queue.monitoring.events().unwrap()))); + assert!(queue.push(QueuedJob { + body: BlockingJob::Monitored(Arc::clone(&body)), + shutdown: None, + })); + let mut worker = BlockingWorkerMonitoring::default(); + let job = queue.pop(index, &mut worker).unwrap(); + let active = runtime.handle().capture(CaptureOptions::default()).unwrap(); + + assert!(queue.push(QueuedJob { + body: BlockingJob::Plain(Box::new(|| {})), + shutdown: None, + })); + drop(job); + let next = queue.pop(index, &mut worker).unwrap(); + let finished = runtime.handle().capture(CaptureOptions::default()).unwrap(); + drop(next); + queue.stop(); + assert!(queue.pop(index, &mut worker).is_none()); + let stopped = runtime.handle().capture(CaptureOptions::default()).unwrap(); + queue.retire(index); + let retired = runtime.handle().capture(CaptureOptions::default()).unwrap(); + + assert_eq!(active.tasks.len(), 1); + assert_eq!(active.tasks[0].id, body.info().id()); + assert_eq!(active.tasks[0].activity.unwrap().state, TaskActivityState::Running); + assert!(finished.tasks.is_empty()); + assert!(stopped.tasks.is_empty()); + assert!(retired.tasks.is_empty()); + assert_eq!(retired.workers[0].state, WorkerState::Stopped); + } + + #[test] + fn dequeuing_or_stopping_releases_a_canceled_capture_outside_the_queue_lock() { + struct Capture(Arc, std::sync::mpsc::Sender); + + impl Drop for Capture { + fn drop(&mut self) { + self.1.send(self.0.state.try_lock().is_ok()).unwrap(); + } + } + + for stopped in [false, true] { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("blocking-capture-drop", 1)); + let queue = Arc::new(Queue::default()); + let index = queue.add_worker(); + queue.enable_monitoring(&runtime.handle()); + let (dropped, received) = std::sync::mpsc::channel(); + let capture = Capture(Arc::clone(&queue), dropped); + let pool = crate::task::handle::JoinHandlePool::new(crate::runtime::shutdown::Shutdown::default()); + let (body, handle) = crate::task::preparation::prepare_blocking( + &pool, + move || drop(capture), + queue.monitoring.task::<()>(&crate::runtime::shutdown::Shutdown::default()), + ); + assert!(queue.push(QueuedJob { body, shutdown: None })); + let mut worker = BlockingWorkerMonitoring::default(); + drop(queue.pop(index, &mut worker).unwrap()); + + if stopped { + queue.stop(); + } else { + assert!(queue.push(QueuedJob { + body: BlockingJob::Plain(Box::new(|| {})), + shutdown: None, + })); + } + let next = queue.pop(index, &mut worker); + + assert_eq!(next.is_none(), stopped); + assert!(received.try_recv().unwrap()); + assert!(handle.join().unwrap_err().is_shutdown()); + } + } + + #[test] + fn an_idle_blocking_worker_releases_its_canceled_capture_before_waiting() { + struct Capture(Arc, std::sync::mpsc::Sender); + + impl Drop for Capture { + fn drop(&mut self) { + let mut state = self.0.state.try_lock().unwrap(); + let released = state.workers[0].current.is_none() && !state.workers[0].running; + state.stopped = true; + self.1.send(released).unwrap(); + } + } + + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("idle-blocking-drop", 1)); + let queue = Arc::new(Queue::default()); + let index = queue.add_worker(); + queue.enable_monitoring(&runtime.handle()); + let (dropped, received) = std::sync::mpsc::channel(); + let capture = Capture(Arc::clone(&queue), dropped); + let pool = crate::task::handle::JoinHandlePool::new(crate::runtime::shutdown::Shutdown::default()); + let (body, handle) = crate::task::preparation::prepare_blocking( + &pool, + move || drop(capture), + queue.monitoring.task::<()>(&crate::runtime::shutdown::Shutdown::default()), + ); + assert!(queue.push(QueuedJob { body, shutdown: None })); + let mut worker = BlockingWorkerMonitoring::default(); + drop(queue.pop(index, &mut worker).unwrap()); + + assert!(queue.pop(index, &mut worker).is_none()); + + assert!(received.try_recv().unwrap()); + assert!(handle.join().unwrap_err().is_shutdown()); + } + + #[test] + fn blocking_collection_handles_missing_metadata_limits_and_canceled_requests() { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("blocking-limits", 1)); + let queue = Arc::new(Queue::default()); + queue.add_worker(); + queue.enable_monitoring(&runtime.handle()); + for body in [ + BlockingJob::Plain(Box::new(|| {})), + BlockingJob::Monitored(Arc::new(TestJob(TaskInfo::new::<()>(queue.monitoring.events().unwrap())))), + ] { + assert!(queue.push(QueuedJob { body, shutdown: None })); + } + let truncated = runtime + .handle() + .capture(CaptureOptions { + limit_per_worker: 1, + ..CaptureOptions::default() + }) + .unwrap(); + let complete = runtime.handle().capture(CaptureOptions::default()).unwrap(); + + let source = seismograph_runtime::register_runtime(RuntimeMetadata::new("canceled-blocking-request", 1)); + let worker = source.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let (sender, receiver) = std::sync::mpsc::channel(); + worker + .handle() + .register_task_provider(move |request| sender.send(request).unwrap()) + .unwrap(); + source.handle().capture_with_timeout(CaptureOptions::default()).unwrap(); + queue.collect(0, receiver.try_recv().unwrap()); + + assert!(truncated.tasks.is_empty()); + assert_eq!(truncated.workers[0].task_collection, TaskCollection::Truncated); + assert_eq!(complete.tasks.len(), 1); + assert_eq!(complete.tasks[0].activity.unwrap().state, TaskActivityState::Ready); + assert_eq!(complete.workers[0].task_collection, TaskCollection::Complete); + } + + #[test] + fn a_dropped_blocking_queue_reports_collection_failure() { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let runtime = seismograph_runtime::register_runtime(RuntimeMetadata::new("dropped-blocking-queue", 1)); + let queue = Arc::new(Queue::default()); + let worker = queue.register_worker(&runtime.handle(), 0); + drop(queue); + + let snapshot = runtime.handle().capture(CaptureOptions::default()).unwrap(); + assert!(snapshot.tasks.is_empty()); + assert_eq!(snapshot.workers[0].task_collection, TaskCollection::Failed); + drop(worker); + } +} diff --git a/crates/arty/src/runtime/worker/blocking/worker.rs b/crates/arty/src/runtime/worker/blocking/worker.rs index 551d6d102..e79e575d5 100644 --- a/crates/arty/src/runtime/worker/blocking/worker.rs +++ b/crates/arty/src/runtime/worker/blocking/worker.rs @@ -9,19 +9,20 @@ use std::thread; use arty_executor::discard_task_panic_payload; use performables::arc::Arc; -use performables::sync::channel; use performables::sync::mutex::Mutex; use crate::runtime::config::RuntimeName; +#[cfg(test)] +use crate::runtime::monitoring::BlockingJob; +use crate::runtime::monitoring::{BlockingPoolMonitoring, BlockingWorkerMonitoring}; use crate::runtime::shutdown::{Shutdown, ShutdownState}; +use crate::runtime::worker::blocking::queue::{Queue, QueuedJob}; use crate::runtime::worker::blocking::{LocalWorker, PoolIdentity}; use crate::task::handle::{JoinHandle, JoinHandlePool}; use crate::task::preparation::prepare_blocking; const ERR_POISONED_LOCK: &str = "poisoned lock - cannot continue execution because security and privacy guarantees can no longer be upheld"; -type Job = Box; - fn attach_thread(pool: PoolIdentity) { LocalWorker::attach(pool); } @@ -38,21 +39,21 @@ fn attach_thread(pool: PoolIdentity) { struct JoinableThreadPool { name: RuntimeName, async_worker_index: Option, - jobs: Option>, + jobs: Option>, workers: Vec>, identity: PoolIdentity, - receiver: channel::Receiver, + receiver: std::sync::Arc, queued_count: Arc, active_count: Arc, } impl JoinableThreadPool { fn new(name: RuntimeName, async_worker_index: Option, thread_count: usize, identity: PoolIdentity) -> Self { - let (jobs, receiver) = channel::unbounded(); + let receiver = std::sync::Arc::new(Queue::default()); let mut pool = Self { name, async_worker_index, - jobs: Some(jobs), + jobs: Some(std::sync::Arc::clone(&receiver)), workers: Vec::with_capacity(thread_count), identity, receiver, @@ -63,15 +64,23 @@ impl JoinableThreadPool { pool } + #[cfg(test)] fn execute(&self, job: F) -> bool where F: FnOnce() + Send + 'static, { + self.enqueue(QueuedJob { + body: BlockingJob::Plain(Box::new(job)), + shutdown: None, + }) + } + + fn enqueue(&self, job: QueuedJob) -> bool { let Some(jobs) = &self.jobs else { return false; }; self.queued_count.fetch_add(1, Ordering::SeqCst); - if jobs.send(Box::new(job)).is_err() { + if !jobs.push(job) { self.queued_count.fetch_sub(1, Ordering::SeqCst); return false; } @@ -80,7 +89,8 @@ impl JoinableThreadPool { fn set_num_threads(&mut self, thread_count: usize) { while self.workers.len() < thread_count { - let receiver = self.receiver.clone(); + let receiver = std::sync::Arc::clone(&self.receiver); + let index = receiver.add_worker(); let queued_count = Arc::clone(&self.queued_count); let active_count = Arc::clone(&self.active_count); let identity = Arc::clone(&self.identity); @@ -90,16 +100,22 @@ impl JoinableThreadPool { .spawn(move || { // #[gamma::skip(call_result.default, reason = "attach_thread returns unit; default_after preserves its side effects and unit result.")] attach_thread(identity); - while let Ok(job) = receiver.recv() { + let mut diagnostics = BlockingWorkerMonitoring::default(); + while let Some(job) = receiver.pop(index, &mut diagnostics) { active_count.fetch_add(1, Ordering::SeqCst); queued_count.fetch_sub(1, Ordering::SeqCst); - let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(job)); + let canceled = job + .shutdown + .as_ref() + .is_some_and(|shutdown| matches!(shutdown.state(), ShutdownState::Started)); + let outcome = diagnostics.run(job.body, canceled); active_count.fetch_sub(1, Ordering::SeqCst); if let Err(panic) = outcome { // #[gamma::skip(stmt.delete_call, tag = "timeout", reason = "November 4 campaign: this mutation exceeded the test timeout")] discard_task_panic_payload(panic); } } + receiver.retire(index); }) .expect("blocking worker thread creation must succeed"); // #[gamma::skip(stmt.delete_call, tag = "outofmem", reason = "November 4 campaign: this mutation exceeded the memory limit")] @@ -126,7 +142,9 @@ impl JoinableThreadPool { } fn shutdown_and_join(&mut self) { - drop(self.jobs.take()); + if let Some(jobs) = self.jobs.take() { + jobs.stop(); + } for worker in self.workers.drain(..) { if let Err(panic) = worker.join() { discard_task_panic_payload(panic); @@ -166,21 +184,18 @@ impl Worker { R: Send + 'static, { let runtime_shutdown = self.remote_results.shutdown().clone(); - let (task, join_handle) = prepare_blocking(&self.remote_results, body); + let monitoring = self.pool.monitoring.task::(&runtime_shutdown); + let (task, join_handle) = prepare_blocking(&self.remote_results, body, monitoring); if matches!(runtime_shutdown.state(), ShutdownState::Started) { drop(task); return join_handle; } let join_handle = join_handle.blocking_pool(Arc::clone(&self.pool.identity)); - let task = move || { - if matches!(runtime_shutdown.state(), ShutdownState::Started) { - drop(task); - } else { - task(); - } + let job = QueuedJob { + body: task, + shutdown: Some(runtime_shutdown), }; - - if submit_while_running(self.remote_results.shutdown(), task, |task| self.pool.execute(task)) { + if submit_while_running(self.remote_results.shutdown(), job, |job| self.pool.enqueue(job)) { self.pool.grow_if_overloaded(); } @@ -207,6 +222,7 @@ pub(crate) struct Pool { inner: Arc>>, identity: PoolIdentity, max_thread_count: NonZeroUsize, + monitoring: BlockingPoolMonitoring, } impl Pool { @@ -230,24 +246,42 @@ impl Pool { // Start with one thread and let the pool grow as needed. let identity = Arc::new(()); let thread_pool = JoinableThreadPool::new(name, async_worker_index, Self::INITIAL_THREAD_COUNT, Arc::clone(&identity)); + let monitoring = thread_pool.receiver.monitoring.clone(); Self { inner: Arc::new(Mutex::new(Some(thread_pool))), identity, max_thread_count: max_thread_count.unwrap_or(Self::MAX_THREAD_COUNT_DEFAULT), + monitoring, } } + #[cfg(test)] pub(crate) fn execute(&self, f: F) -> bool where F: FnOnce() + Send + 'static, { + self.enqueue(QueuedJob { + body: BlockingJob::Plain(Box::new(f)), + shutdown: None, + }) + } + + fn enqueue(&self, job: QueuedJob) -> bool { let pool = self.inner.lock_result().expect(ERR_POISONED_LOCK); let Some(pool) = pool.as_ref() else { // Shutdown closed the shared pool before this racing submission arrived. return false; }; - pool.execute(f) + pool.enqueue(job) + } + + #[cfg(feature = "monitor")] + pub(crate) fn enable_monitoring(&self, runtime: &seismograph_runtime::RuntimeHandle) { + let pool = self.inner.lock_result().expect(ERR_POISONED_LOCK); + if let Some(pool) = pool.as_ref() { + pool.receiver.enable_monitoring(runtime); + } } fn join(&self) { @@ -324,7 +358,7 @@ mod tests { use performables::sync::mutex::Mutex; use testing_aids::{TEST_TIMEOUT, execute_or_abandon}; - use super::{Job, JoinableThreadPool, LocalWorker, Pool, Worker, attach_thread}; + use super::{JoinableThreadPool, LocalWorker, Pool, Worker, attach_thread}; use crate::runtime::config::RuntimeName; use crate::runtime::shutdown::Shutdown; @@ -539,6 +573,7 @@ mod tests { fn runtime_releases_pool_even_when_a_scheduler_is_retained() { execute_or_abandon(|| { let runtime = crate::runtime::Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -627,14 +662,13 @@ mod tests { } #[test] - fn joinable_pool_reports_disconnected_submission() { - let (jobs, disconnected) = performables::sync::channel::unbounded::(); - drop(disconnected); - let (_, receiver) = performables::sync::channel::unbounded(); + fn joinable_pool_reports_closed_submission() { + let receiver = std::sync::Arc::new(super::Queue::default()); + receiver.stop(); let mut pool = JoinableThreadPool { name: RuntimeName::default(), async_worker_index: None, - jobs: Some(jobs), + jobs: Some(std::sync::Arc::clone(&receiver)), workers: Vec::new(), identity: Arc::new(()), receiver, @@ -647,6 +681,27 @@ mod tests { pool.shutdown_and_join(); } + #[cfg(feature = "monitor")] + #[test] + fn a_closed_pool_does_not_register_workers_when_monitoring_is_enabled() { + let _monitoring = crate::runtime::monitoring::TEST_LOCK.lock().unwrap(); + let pool = Pool::new(RuntimeName::default(), None, Some(limit(1))); + pool.join(); + let runtime = seismograph_runtime::register_runtime(seismograph_runtime::RuntimeMetadata::new("closed-pool", 1)); + + pool.enable_monitoring(&runtime.handle()); + + assert!( + runtime + .handle() + .capture(seismograph_runtime::snapshot::CaptureOptions::default()) + .unwrap() + .workers + .is_empty() + ); + assert!(!pool.execute(|| panic!("a closed pool must not accept work"))); + } + #[test] fn joinable_pool_contains_job_panics() { let pool = joinable_pool(1); @@ -661,7 +716,7 @@ mod tests { #[test] fn joinable_pool_discards_worker_panic() { let worker = thread::spawn(|| panic!("worker failure")); - let (_, receiver) = performables::sync::channel::unbounded(); + let receiver = std::sync::Arc::new(super::Queue::default()); let mut pool = JoinableThreadPool { name: RuntimeName::default(), async_worker_index: None, diff --git a/crates/arty/src/task/handle/error.rs b/crates/arty/src/task/handle/error.rs index b63fbe1b7..b2cbc7992 100644 --- a/crates/arty/src/task/handle/error.rs +++ b/crates/arty/src/task/handle/error.rs @@ -44,7 +44,7 @@ use crate::runtime::ShutdownCause; /// ``` /// use arty::runtime::Runtime; /// -/// let runtime = Runtime::new()?; +/// let runtime = Runtime::builder().build()?; /// let error = runtime.scheduler().block_on(async |cx| { /// cx.control().request_stop(); /// cx.scheduler() @@ -101,7 +101,7 @@ impl JoinError { /// ``` /// use arty::runtime::Runtime; /// - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let error = runtime.scheduler().block_on(async |cx| { /// cx.control().request_stop(); /// cx.scheduler() diff --git a/crates/arty/src/task/preparation/blocking.rs b/crates/arty/src/task/preparation/blocking.rs index d4ae187c3..b5f9e00bd 100644 --- a/crates/arty/src/task/preparation/blocking.rs +++ b/crates/arty/src/task/preparation/blocking.rs @@ -5,10 +5,15 @@ use std::panic::{AssertUnwindSafe, catch_unwind}; use arty_executor::TaskCompletion; +use crate::runtime::monitoring::{BlockingJob, BlockingTaskEvents, BlockingTaskMonitoring}; use crate::task::handle::{JoinHandle, JoinHandlePool, Sender}; use crate::task::preparation::discard_panicking_drop; -pub(crate) fn prepare_blocking(result_pool: &JoinHandlePool, body: F) -> (impl FnOnce() + Send + 'static, JoinHandle) +pub(crate) fn prepare_blocking( + result_pool: &JoinHandlePool, + body: F, + monitoring: BlockingTaskMonitoring, +) -> (BlockingJob, JoinHandle) where F: FnOnce() -> R + Send + 'static, R: Send + 'static, @@ -17,11 +22,11 @@ where let task = BlockingTask { pending: Some((body, result_tx)), }; - (move || task.run(), handle) + (monitoring.prepare(task), handle) } -struct BlockingTask { - pending: Option<(F, Sender)>, +pub(crate) struct BlockingTask { + pub(crate) pending: Option<(F, Sender)>, } impl BlockingTask @@ -29,13 +34,15 @@ where F: FnOnce() -> R + Send + 'static, R: Send + 'static, { - fn run(mut self) { + pub(crate) fn run(mut self, monitoring: BlockingTaskEvents<'_>) { let (body, result_tx) = self.pending.take().expect("a blocking task is consumed exactly once"); match catch_unwind(AssertUnwindSafe(body)) { Ok(result) => { + monitoring.completed(); result_tx.completed(result); } Err(panic) => { + monitoring.panicked(); result_tx.panicked(panic, None); } } diff --git a/crates/arty/src/task/preparation/mod.rs b/crates/arty/src/task/preparation/mod.rs index 04ed2ba90..c5b5f4048 100644 --- a/crates/arty/src/task/preparation/mod.rs +++ b/crates/arty/src/task/preparation/mod.rs @@ -11,7 +11,7 @@ mod cleanup; mod deferred_task; mod remote; -pub(crate) use blocking::prepare_blocking; +pub(crate) use blocking::{BlockingTask, prepare_blocking}; pub(crate) use cleanup::discard_panicking_drop; #[cfg(test)] use deferred_task::DeferredTask; diff --git a/crates/arty/src/task/preparation/remote.rs b/crates/arty/src/task/preparation/remote.rs index 4db4b9a32..7a67a1215 100644 --- a/crates/arty/src/task/preparation/remote.rs +++ b/crates/arty/src/task/preparation/remote.rs @@ -6,6 +6,7 @@ use std::cell::Cell; use arty_executor::{TaskPlacement, TaskSet}; use plurality::{Box as PoolBox, MultiPool, coerce}; +use crate::runtime::monitoring::TaskMonitoring; use crate::task::Context; use crate::task::handle::{JoinHandle, JoinHandlePool}; use crate::task::preparation::{DeferredTaskFactory, discard_panicking_drop}; @@ -88,22 +89,24 @@ impl RemoteFutureFactory { } } -pub(crate) fn prepare_remote( +pub(crate) fn prepare_remote( result_pool: &JoinHandlePool, future_factory: FF, placement: TaskPlacement, + monitoring: TaskMonitoring, ) -> (RemoteFutureFactory, JoinHandle) where FF: FnOnce(Context) -> F + Send + 'static, F: Future + 'static, R: Send + 'static, + S: Fn() -> usize + Send + 'static, { let (handle, result_tx) = result_pool.handle(); let future_factory = TaskFactory::new(future_factory); let future_factory = RemoteFutureFactory::new(move |cx, tasks| { let future_factory = future_factory.into_inner(); let inner = DeferredTaskFactory::new(future_factory, cx); - tasks.task(inner).placement(placement).register(result_tx); + monitoring.register(tasks, inner, placement, result_tx); }); (future_factory, handle) } diff --git a/crates/arty/src/task/preparation/tests.rs b/crates/arty/src/task/preparation/tests.rs index 6c3663b8f..eeaaf9a0d 100644 --- a/crates/arty/src/task/preparation/tests.rs +++ b/crates/arty/src/task/preparation/tests.rs @@ -13,12 +13,17 @@ use arty_executor::{CycleOutcome, Executor, TaskPlacement}; use futures::executor::block_on; use super::*; +use crate::runtime::monitoring::{BlockingTaskMonitoring, TaskMonitoring}; use crate::runtime::{Runtime, Shutdown, WorkersPolicy}; use crate::task::Context; use crate::task::handle::JoinHandlePool; fn test_context() -> (Runtime, Context) { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let cx = runtime.scheduler().block_on(async |cx| cx).unwrap(); (runtime, cx) } @@ -41,6 +46,7 @@ fn remote_factory_remains_deferred_until_poll() { } }, TaskPlacement::Any, + TaskMonitoring::capture(), ); let mut executor = Executor::builder().owner_waker(Waker::noop().clone()).build(); @@ -90,8 +96,8 @@ impl Wake for PanicWake { #[test] fn blocking_task_delivers_its_result() { let result_pool = JoinHandlePool::new(Shutdown::default()); - let (task, handle) = prepare_blocking(&result_pool, || 42); - task(); + let (task, handle) = prepare_blocking(&result_pool, || 42, BlockingTaskMonitoring::default()); + task.run(); assert_eq!(block_on(handle).unwrap(), 42); assert!(result_pool.shutdown().cause().is_none()); @@ -100,8 +106,8 @@ fn blocking_task_delivers_its_result() { #[test] fn blocking_panic_is_delivered_to_joiner() { let result_pool = JoinHandlePool::new(Shutdown::default()); - let (task, handle) = prepare_blocking(&result_pool, || panic!("blocking task")); - task(); + let (task, handle) = prepare_blocking(&result_pool, || panic!("blocking task"), BlockingTaskMonitoring::default()); + task.run(); assert!(block_on(handle).unwrap_err().is_panic()); assert!(result_pool.shutdown().cause().is_none()); @@ -117,6 +123,7 @@ fn remote_factory_panic_is_delivered_to_joiner() { &result_pool, |_| -> std::future::Ready<()> { panic_any(Payload(42)) }, TaskPlacement::Any, + TaskMonitoring::capture(), ); let mut executor = Executor::builder().owner_waker(Waker::noop().clone()).build(); let (_runtime, cx) = test_context(); @@ -130,7 +137,7 @@ fn discarded_preparation_reports_the_shutdown_cause() { let shutdown = Shutdown::default(); shutdown.begin(arty_executor::ShutdownError::new(std::io::Error::other("blocking shutdown")).into()); let result_pool = JoinHandlePool::new(shutdown); - let (task, handle) = prepare_blocking(&result_pool, || 42); + let (task, handle) = prepare_blocking(&result_pool, || 42, BlockingTaskMonitoring::default()); drop(task); let mut handle = pin!(handle); let mut context = PollContext::from_waker(Waker::noop()); @@ -147,7 +154,7 @@ fn discarded_remote_factory_reports_the_shutdown_cause() { let shutdown = Shutdown::default(); shutdown.begin(arty_executor::ShutdownError::new(std::io::Error::other("queued shutdown")).into()); let result_pool = JoinHandlePool::new(shutdown); - let (factory, handle) = prepare_remote(&result_pool, |_| async { 42 }, TaskPlacement::Any); + let (factory, handle) = prepare_remote(&result_pool, |_| async { 42 }, TaskPlacement::Any, TaskMonitoring::capture()); drop(factory); @@ -168,7 +175,7 @@ fn discarded_blocking_task_contains_body_destructor_panic() { let capture = PanicOnDrop; let result_pool = JoinHandlePool::new(Shutdown::default()); - let (task, handle) = prepare_blocking(&result_pool, move || drop(capture)); + let (task, handle) = prepare_blocking(&result_pool, move || drop(capture), BlockingTaskMonitoring::default()); catch_unwind(AssertUnwindSafe(|| drop(task))).unwrap(); assert!(block_on(handle).unwrap_err().is_shutdown()); } @@ -176,7 +183,7 @@ fn discarded_blocking_task_contains_body_destructor_panic() { #[test] fn discarded_blocking_task_contains_join_waker_panic() { let result_pool = JoinHandlePool::new(Shutdown::default()); - let (task, handle) = prepare_blocking(&result_pool, || 42); + let (task, handle) = prepare_blocking(&result_pool, || 42, BlockingTaskMonitoring::default()); let mut handle = pin!(handle); let waker = Waker::from(Arc::new(PanicWake)); assert!(handle.as_mut().poll(&mut PollContext::from_waker(&waker)).is_pending()); diff --git a/crates/arty/src/task/runtime_scheduler.rs b/crates/arty/src/task/runtime_scheduler.rs index 579367ef4..e84493a7d 100644 --- a/crates/arty/src/task/runtime_scheduler.rs +++ b/crates/arty/src/task/runtime_scheduler.rs @@ -105,7 +105,7 @@ impl RuntimeScheduler { /// ``` /// use arty::runtime::Runtime; /// - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let mut message = String::from("Hello"); /// runtime /// .scheduler() @@ -135,7 +135,10 @@ impl RuntimeScheduler { // is destroyed. The final sender is dropped after those fields, including on // cancellation or panic. Receiving the result alone is not a destruction guarantee. let factory = unsafe { std::mem::transmute::, BoxedFutureFactory<'static, R>>(factory) }; - futures::executor::block_on(self.dispatcher.spawn(factory)).map_err(Error::new) + let task = self + .dispatcher + .spawn(factory, crate::runtime::monitoring::TaskMonitoring::with_body_size(size_of::())); + futures::executor::block_on(task).map_err(Error::new) } /// Lets the runtime choose a worker for an async task and relocates its data. @@ -160,7 +163,7 @@ impl RuntimeScheduler { /// ``` /// use arty::runtime::Runtime; /// - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let task = runtime /// .scheduler() /// .spawn_anywhere(String::from("Arty"), |_, name| async move { name.len() }); @@ -175,7 +178,7 @@ impl RuntimeScheduler { /// use arty::runtime::Runtime; /// /// struct SendOnly(u32); - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let task = runtime /// .scheduler() /// .spawn_anywhere((), |_, ()| async { SendOnly(42) }); @@ -190,11 +193,14 @@ impl RuntimeScheduler { F: Future + 'static, R: Send + 'static, { - self.dispatcher.spawn(move |cx| { - let mut data = data; - data.relocate(None, cx.thread()); - future_factory(cx, data) - }) + self.dispatcher.spawn( + move |cx| { + let mut data = data; + data.relocate(None, cx.thread()); + future_factory(cx, data) + }, + crate::runtime::monitoring::TaskMonitoring::capture(), + ) } /// Runs a function on a thread from a blocking pool. @@ -218,7 +224,7 @@ impl RuntimeScheduler { /// ``` /// use arty::runtime::Runtime; /// - /// let runtime = Runtime::new()?; + /// let runtime = Runtime::builder().build()?; /// let task = runtime.scheduler().spawn_blocking(|| 6 * 7); /// assert_eq!(futures::executor::block_on(task)?, 42); /// # Ok::<(), Box>(()) @@ -417,6 +423,7 @@ mod tests { #[test] fn scoped_poll_panic_is_joined_after_borrowed_future_destruction() { let runtime = Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .on(crate::runtime::hook::Panic::IGNORE) .build() diff --git a/crates/arty/src/task/scheduler.rs b/crates/arty/src/task/scheduler.rs index d3b747fb9..095f47713 100644 --- a/crates/arty/src/task/scheduler.rs +++ b/crates/arty/src/task/scheduler.rs @@ -393,6 +393,7 @@ mod tests { #[test] fn worker_execution_registers_the_current_scheduler() { let runtime = crate::runtime::Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -424,6 +425,7 @@ mod tests { } let runtime = crate::runtime::Runtime::builder() + .monitor(crate::runtime::MonitorPolicy::disabled()) .workers(crate::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); diff --git a/crates/arty/tests/behavior_contracts.rs b/crates/arty/tests/behavior_contracts.rs index 0685102e5..be1bd1314 100644 --- a/crates/arty/tests/behavior_contracts.rs +++ b/crates/arty/tests/behavior_contracts.rs @@ -27,7 +27,11 @@ use testing_aids::TEST_TIMEOUT; use thread_aware::Unaware; fn runtime(workers: usize) -> Runtime { - Runtime::builder().workers(WorkersPolicy::exactly(workers)).build().unwrap() + Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(workers)) + .build() + .unwrap() } fn worker_scheduler(runtime: &Runtime) -> Scheduler { @@ -97,6 +101,7 @@ fn assert_blocking_pool_limit(max_threads: usize) { const QUEUED_TASK_COUNT: usize = 6; let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .blocking_pool(BlockingPoolPolicy::shared().max(max_threads)) .build() @@ -173,6 +178,7 @@ fn the_last_processor_setting_replaces_invalid_and_previous_counts() { WorkersPolicy::default(), ] { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(previous) .workers(WorkersPolicy::at_most(1)) .build() @@ -191,6 +197,7 @@ fn zero_stack_size_is_rejected_by_the_setter() { #[test] fn resource_setters_preserve_worker_count_and_pool_policy() { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .blocking_pool(BlockingPoolPolicy::per_worker()) .workers(WorkersPolicy::at_most(1)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) @@ -273,6 +280,7 @@ fn nested_block_on_rejects_the_factory_before_submission() { fn worker_context_belongs_to_one_runtime_and_rejects_nested_block_on() { completes_without_deadlock(|| { let first = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) .build() @@ -375,6 +383,7 @@ fn configured_clock_control_remains_the_worker_time_source() { let control = ClockControl::new(); let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .clock(control.clone()) .build() @@ -424,6 +433,7 @@ fn stopping_one_runtime_does_not_stop_another() { fn dropping_another_runtime_from_a_worker_panics() { let target = runtime(1); let caller = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .on(Panic::IGNORE) .build() @@ -505,7 +515,7 @@ fn rejected_remote_joins_report_shutdown_then_reject_repolling() { mod entry_points { use arty::task::Context; - #[arty::main(workers = 1)] + #[arty::main(workers = 1, monitor = disabled)] async fn application_error(_cx: Context) -> Result<(), &'static str> { Err("application") } diff --git a/crates/arty/tests/block_on_spawn.rs b/crates/arty/tests/block_on_spawn.rs index c0a632311..09801ec2d 100644 --- a/crates/arty/tests/block_on_spawn.rs +++ b/crates/arty/tests/block_on_spawn.rs @@ -11,7 +11,9 @@ use arty::runtime::Runtime; fn capture_by_value() { #[expect(clippy::useless_vec, reason = "using a vec to test with a type using pointers on the inside")] let vec = vec![10, 11]; - let result = Runtime::new() + let result = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() .expect("Failed to create runtime") .scheduler() .block_on(async move |_ctx| vec.len() + 10) @@ -23,7 +25,9 @@ fn capture_by_value() { fn move_and_spawn() { #[expect(clippy::useless_vec, reason = "using a vec to test with a type using pointers on the inside")] let vec = vec![10, 11]; - let result = Runtime::new() + let result = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() .expect("Failed to create runtime") .scheduler() .block_on(async move |ctx| { @@ -37,7 +41,9 @@ fn move_and_spawn() { #[test] fn capture_reference() { let vec = vec![10, 11]; - let result = Runtime::new() + let result = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() .expect("Failed to create runtime") .scheduler() .block_on(async |_ctx| { @@ -51,7 +57,9 @@ fn capture_reference() { #[test] fn capture_mut_reference() { let mut vec = vec![10, 11]; - let result = Runtime::new() + let result = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() .expect("Failed to create runtime") .scheduler() .block_on(async |_ctx| { diff --git a/crates/arty/tests/construction.rs b/crates/arty/tests/construction.rs index 0f1a532a7..c69294624 100644 --- a/crates/arty/tests/construction.rs +++ b/crates/arty/tests/construction.rs @@ -137,7 +137,11 @@ fn worker_names(runtime: &Runtime) -> Vec<(String, String)> { #[test] fn default_worker_names_use_arty() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); assert_eq!( worker_names(&runtime), @@ -166,6 +170,7 @@ fn runtime_name_is_copied_and_replaces_the_previous_name() { ] { let name = String::from("requests"); let builder = Runtime::builder() + .monitor(MonitorPolicy::disabled()) .name("invalid\0name") .name(&name) .workers(WorkersPolicy::exactly(2)) @@ -186,8 +191,16 @@ fn runtime_name_is_copied_and_replaces_the_previous_name() { #[test] fn runtime_names_are_independent() { - let first = Runtime::builder().name(String::from("first")).build().unwrap(); - let second = Runtime::builder().name("second").build().unwrap(); + let first = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .name(String::from("first")) + .build() + .unwrap(); + let second = Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .name("second") + .build() + .unwrap(); assert_eq!( worker_names(&first), @@ -203,7 +216,7 @@ fn runtime_names_are_independent() { #[test] fn empty_runtime_name_is_accepted() { - let runtime = Runtime::builder().name("").build().unwrap(); + let runtime = Runtime::builder().monitor(MonitorPolicy::disabled()).name("").build().unwrap(); assert_eq!(worker_names(&runtime), vec![("-async-0".to_owned(), "-blocking-0".to_owned())]); runtime.stop().unwrap(); @@ -219,38 +232,17 @@ fn null_in_runtime_name_is_rejected_when_built() { } } -#[test] -fn monitor_policy_defaults_to_enabled() { - let runtime = Runtime::builder() - .workers(WorkersPolicy::exactly(1)) - .monitor(MonitorPolicy::default()) - .build() - .unwrap(); - - assert_eq!(runtime.scheduler().block_on(async |_| 42).unwrap(), 42); - runtime.stop().unwrap(); -} - -#[test] -fn explicit_monitor_policies_preserve_runtime_behavior() { - for policy in [MonitorPolicy::disabled(), MonitorPolicy::enabled()] { - let runtime = Runtime::builder() - .workers(WorkersPolicy::exactly(1)) - .monitor(policy) - .build() - .unwrap(); - - assert_eq!(runtime.scheduler().block_on(async |_| 42).unwrap(), 42); - runtime.stop().unwrap(); - } -} - #[test] fn default_runtime_construction_uses_one_worker() { for runtime in [ + #[cfg(not(feature = "monitor"))] Runtime::new().unwrap(), - Runtime::builder().build().unwrap(), - Runtime::builder().workers(WorkersPolicy::default()).build().unwrap(), + Runtime::builder().monitor(MonitorPolicy::disabled()).build().unwrap(), + Runtime::builder() + .monitor(MonitorPolicy::disabled()) + .workers(WorkersPolicy::default()) + .build() + .unwrap(), ] { runtime .scheduler() @@ -307,6 +299,7 @@ fn zero_shared_blocking_limits_are_rejected_when_the_runtime_is_built() { #[test] fn zero_shared_blocking_limit_can_be_replaced_before_building() { let runtime = Runtime::builder() + .monitor(MonitorPolicy::disabled()) .blocking_pool(BlockingPoolPolicy::shared().max(0)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) .build() diff --git a/crates/arty/tests/io/cycles.rs b/crates/arty/tests/io/cycles.rs index b1370cedf..25e69c4fe 100644 --- a/crates/arty/tests/io/cycles.rs +++ b/crates/arty/tests/io/cycles.rs @@ -214,6 +214,7 @@ fn notification_during_first_driver_construction_prevents_primary_wait() { fn controlled_timer_fires_while_a_primary_driver_is_initialized() { let control = tick::ClockControl::new(); let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .clock(control.clone()) .build() diff --git a/crates/arty/tests/io/main.rs b/crates/arty/tests/io/main.rs index f849a6d32..192e6ef65 100644 --- a/crates/arty/tests/io/main.rs +++ b/crates/arty/tests/io/main.rs @@ -560,7 +560,11 @@ impl Driver for TestDriver { } fn runtime(workers: usize) -> Runtime { - Runtime::builder().workers(WorkersPolicy::exactly(workers)).build().unwrap() + Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(workers)) + .build() + .unwrap() } fn worker_schedulers(runtime: &Runtime, workers: usize) -> Vec { diff --git a/crates/arty/tests/join_failures.rs b/crates/arty/tests/join_failures.rs index 3c2aa1445..b852d32d4 100644 --- a/crates/arty/tests/join_failures.rs +++ b/crates/arty/tests/join_failures.rs @@ -27,6 +27,7 @@ use thread_aware::Unaware; #[cfg(test)] fn runtime_builder() -> RuntimeBuilder { Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) } @@ -82,6 +83,7 @@ async fn async_and_blocking_panics_are_join_errors(cx: Context) { #[test] fn blocking_callback_cannot_wait_for_blocking_work() { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .blocking_pool(BlockingPoolPolicy::shared().max(2)) .build() diff --git a/crates/arty/tests/panic/policy.rs b/crates/arty/tests/panic/policy.rs index da08b495a..ecb824e57 100644 --- a/crates/arty/tests/panic/policy.rs +++ b/crates/arty/tests/panic/policy.rs @@ -28,6 +28,7 @@ enum PanicResponse { fn runtime_builder(workers: usize, response: PanicResponse) -> RuntimeBuilder { let builder = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(workers)) // Two threads keep blocking-work scenarios bounded without serializing // callbacks whose completion or cancellation the test observes. diff --git a/crates/arty/tests/panic/self_wake.rs b/crates/arty/tests/panic/self_wake.rs index 3ead9f756..073aefdba 100644 --- a/crates/arty/tests/panic/self_wake.rs +++ b/crates/arty/tests/panic/self_wake.rs @@ -45,6 +45,7 @@ impl Drop for SelfWakePanic { fn every_worker_retires_self_waking_panics_before_reusing_task_storage() { let repetitions = if cfg!(miri) { 2 } else { 32 }; let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(2)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) .on(Panic::IGNORE) diff --git a/crates/arty/tests/panic_support/mod.rs b/crates/arty/tests/panic_support/mod.rs index 114fa4f3d..e2212d45a 100644 --- a/crates/arty/tests/panic_support/mod.rs +++ b/crates/arty/tests/panic_support/mod.rs @@ -8,6 +8,7 @@ use arty::runtime::{BlockingPoolPolicy, Runtime, WorkersPolicy}; pub(crate) fn runtime() -> Runtime { Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) .on(Panic::IGNORE) diff --git a/crates/arty/tests/relocation.rs b/crates/arty/tests/relocation.rs index bc1b501df..c6b3572f1 100644 --- a/crates/arty/tests/relocation.rs +++ b/crates/arty/tests/relocation.rs @@ -65,7 +65,11 @@ impl Driver for RelocationIoDriver { #[test] fn relocating_context_leaves_shared_source_clones_on_the_source_worker() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); let (source, original, scheduler) = runtime .scheduler() .spawn_anywhere( @@ -119,7 +123,11 @@ impl ThreadAware for ReturnedProbe { #[test] fn spawn_anywhere_keeps_round_robin_selection_and_local_children() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); runtime .scheduler() .block_on(async |cx| { @@ -155,8 +163,21 @@ fn spawn_anywhere_keeps_round_robin_selection_and_local_children() { #[test] fn spawn_anywhere_relocates_results_on_the_source_worker() { for (runtime, workers) in [ - (Runtime::new().unwrap(), 1), - (Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(), 2), + ( + Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() + .unwrap(), + 1, + ), + ( + Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(), + 2, + ), ] { runtime .scheduler() @@ -197,7 +218,11 @@ fn spawn_anywhere_relocates_results_on_the_source_worker() { #[test] fn spawn_at_relocates_results_to_the_source_worker() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); let source = runtime.scheduler().block_on(async |cx| cx).unwrap(); let destination = source .control() @@ -239,7 +264,11 @@ fn spawn_at_relocates_results_to_the_source_worker() { // to the worker that receives the result. #[test] fn spawn_anywhere_returns_context_bound_to_the_source_worker() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); runtime .scheduler() .block_on(async |cx| { @@ -261,7 +290,11 @@ fn spawn_anywhere_returns_context_bound_to_the_source_worker() { // selects the result destination, not the submitting worker. #[test] fn spawn_anywhere_relocates_results_to_the_worker_that_receives_the_join() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); let source = runtime.scheduler().block_on(async |cx| cx).unwrap(); let destination = runtime.scheduler().block_on(async |cx| cx).unwrap(); assert_ne!(source.thread(), destination.thread()); @@ -291,7 +324,11 @@ fn spawn_anywhere_relocates_results_to_the_worker_that_receives_the_join() { // receiving Arty worker. #[test] fn spawn_anywhere_results_joined_outside_arty_are_not_relocated() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let source = runtime.scheduler().block_on(async |cx| cx.thread().clone()).unwrap(); let scheduler = runtime.scheduler().block_on(async |cx| cx.scheduler().clone()).unwrap(); let result = scheduler @@ -321,7 +358,11 @@ fn a_foreign_owner_cannot_rebind_a_registered_thread_id() { } } - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let (source, mut scheduler) = runtime .scheduler() .spawn_anywhere((), |cx, ()| async move { (cx.thread().clone(), cx.scheduler().clone()) }) @@ -372,6 +413,7 @@ struct RelocationObservation { #[cfg(test)] fn relocate_and_observe(policy: BlockingPoolPolicy, target: RelocationTarget) -> RelocationObservation { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(2)) .blocking_pool(policy) .build() @@ -507,7 +549,11 @@ fn relocating_context_uses_same_pool_when_shared() { #[test] fn relocated_context_initializes_io_on_the_destination_worker() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); let initialized = Arc::new(Mutex::new(Vec::new())); let destination = runtime .scheduler() @@ -549,8 +595,16 @@ fn relocated_context_initializes_io_on_the_destination_worker() { #[test] fn foreign_owner_relocation_preserves_runtime_binding() { - let source_runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); - let destination_runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let source_runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); + let destination_runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let mut cx = source_runtime .scheduler() .spawn_anywhere((), |cx, ()| async move { cx }) @@ -581,7 +635,11 @@ fn repeated_spawn_after_relocation_uses_destination() { // Exercise the cached route repeatedly without turning this regression test into a load test. const SPAWN_COUNT: usize = 100; - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(2)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(2)) + .build() + .unwrap(); let workers: Vec<_> = (0..2) .map(|_| runtime.scheduler().spawn_anywhere((), |cx, ()| async move { cx })) .map(|handle| handle.join().unwrap()) @@ -601,8 +659,16 @@ fn repeated_spawn_after_relocation_uses_destination() { #[test] fn foreign_owner_relocation_preserves_bare_scheduler_binding() { - let source_runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); - let destination_runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let source_runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); + let destination_runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let cx = source_runtime .scheduler() .spawn_anywhere((), |cx, ()| async move { cx }) diff --git a/crates/arty/tests/runtime_contracts.rs b/crates/arty/tests/runtime_contracts.rs index a716accb1..315b09928 100644 --- a/crates/arty/tests/runtime_contracts.rs +++ b/crates/arty/tests/runtime_contracts.rs @@ -34,6 +34,7 @@ use tick::FutureExt; #[cfg(test)] fn runtime(workers: usize) -> Runtime { Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(workers)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) .build() @@ -321,6 +322,7 @@ fn factory_and_poll_panics_return_errors_and_preserve_runtime_usability() { panic_any(Payload(1)) } let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .blocking_pool(BlockingPoolPolicy::shared().max(1)) .on(Panic::IGNORE) @@ -370,6 +372,7 @@ fn scoped_borrowed_storage_is_destroyed_before_success_or_error() { fn dropping_a_runtime_on_a_blocking_worker_panics() { for policy in [BlockingPoolPolicy::per_worker(), BlockingPoolPolicy::shared().max(1)] { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .blocking_pool(policy) .build() @@ -386,7 +389,11 @@ fn dropping_a_runtime_on_a_blocking_worker_panics() { #[test] fn repeated_stop_requests_do_not_break_shutdown() { - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(1)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(1)) + .build() + .unwrap(); let control = support::runtime_control(&runtime); control.request_stop(); control.request_stop(); diff --git a/crates/arty/tests/spawning.rs b/crates/arty/tests/spawning.rs index c80b9964b..cd17c7405 100644 --- a/crates/arty/tests/spawning.rs +++ b/crates/arty/tests/spawning.rs @@ -37,6 +37,7 @@ fn assert_send_sync() { #[test] fn spawned_future_preserves_mutable_self_borrows_across_polls() { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -108,6 +109,7 @@ async fn spawn_some_tasks(cx: Context) { #[test] fn local_factory_runs_once_and_preserves_future_borrows() { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -183,6 +185,7 @@ fn relocating_factories_remain_deferred_and_preserve_future_borrows() { }; let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -221,7 +224,11 @@ fn test_worker_affinity() { eprintln!("requires six processors; two-worker affinity is covered by runtime_contracts"); return; } - let runtime = Runtime::builder().workers(WorkersPolicy::exactly(6)).build().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .workers(WorkersPolicy::exactly(6)) + .build() + .unwrap(); let (thread1, scheduler1) = runtime .scheduler() .spawn_anywhere((), |cx, ()| async move { (thread::current().id(), cx.scheduler().clone()) }) @@ -242,7 +249,10 @@ fn test_worker_affinity() { #[test] fn remote_factories_create_non_send_futures_on_the_worker() { - let runtime = Runtime::new().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() + .unwrap(); let (created, polled, associated) = runtime .scheduler() .spawn_anywhere((), |cx: arty::task::Context, ()| { @@ -274,6 +284,7 @@ fn queued_factory_outlives_its_submitting_thread_and_accepts_non_unpin_captures( static_assertions::assert_impl_all!(Capture: Send); static_assertions::assert_not_impl_any!(Capture: Unpin); let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::at_most(1)) .build() .unwrap(); @@ -329,7 +340,10 @@ fn runtime_spawn_anywhere_relocates_payload_with_unknown_source() { } } - let runtime = Runtime::new().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() + .unwrap(); let (source, destination, worker, executed_on) = runtime .scheduler() .spawn_anywhere( @@ -363,6 +377,7 @@ fn runtime_spawn_anywhere_accepts_send_only_non_sync_results() { static_assertions::assert_impl_all!(ResultValue: Send); static_assertions::assert_not_impl_any!(ResultValue: Sync, ThreadAware); let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::at_most(1)) .build() .unwrap(); @@ -440,6 +455,7 @@ async fn spawn_at_relocates_payload_to_each_selected_worker_despite_interleaving #[test] fn spawn_at_returns_shutdown_joins_without_invoking_factories() { let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::at_most(2)) .build() .unwrap(); @@ -475,6 +491,7 @@ fn spawn_at_rejects_a_worker_from_another_runtime() { } let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); @@ -483,6 +500,7 @@ fn spawn_at_rejects_a_worker_from_another_runtime() { .block_on(async |cx| (cx.scheduler().clone(), cx.control().clone())) .unwrap(); let other = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(arty::runtime::WorkersPolicy::exactly(1)) .build() .unwrap(); diff --git a/crates/arty/tests/stop.rs b/crates/arty/tests/stop.rs index a8fd12092..04f9d6cb8 100644 --- a/crates/arty/tests/stop.rs +++ b/crates/arty/tests/stop.rs @@ -28,7 +28,10 @@ fn canary() -> (impl Future, BoxedReceiver<()>, std::sync::Weak<()> #[test] fn stop_via_runtime() { execute_or_abandon(|| { - let runtime = Runtime::new().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() + .unwrap(); let (canary, started, observer) = canary(); @@ -46,7 +49,10 @@ fn stop_via_runtime() { #[test] fn stop_via_async_task() { execute_or_abandon(|| { - let runtime = Runtime::new().unwrap(); + let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() + .unwrap(); let (canary, started, observer) = canary(); @@ -74,7 +80,10 @@ fn stop_via_async_task() { #[test] fn stop_scoped() { execute_or_abandon(move || { - _ = Runtime::new().unwrap(); + _ = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() + .unwrap(); }) .unwrap(); } @@ -87,7 +96,9 @@ fn stop_in_run() { let task_was_executed = Arc::clone(&task_was_executed); move || { - Runtime::new() + Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) + .build() .unwrap() .scheduler() .block_on(async move |_| { diff --git a/crates/arty/tests/test_macro.rs b/crates/arty/tests/test_macro.rs index b11f7d6da..e17923a4b 100644 --- a/crates/arty/tests/test_macro.rs +++ b/crates/arty/tests/test_macro.rs @@ -107,11 +107,6 @@ async fn default_test_runtime_has_exactly_one_worker(cx: Context) { } } -#[test(monitor = enabled)] -async fn test_can_enable_monitoring(cx: Context) { - assert_eq!(cx.scheduler().spawn(async |_| 42).await.unwrap(), 42); -} - #[test(monitor = disabled, workers = 1)] async fn test_can_disable_monitoring_explicitly(cx: Context) { assert_eq!(cx.scheduler().spawn(async |_| 42).await.unwrap(), 42); @@ -130,7 +125,7 @@ async fn worker_count_is_exact_even_when_processors_are_shared(cx: Context) { assert_eq!(workers.len(), 3); } -#[arty::main] +#[arty::main(monitor = disabled)] #[::core::prelude::v1::test] async fn default_main_runtime_has_exactly_one_worker(cx: Context) { let tasks: Vec<_> = cx diff --git a/crates/arty/tests/time.rs b/crates/arty/tests/time.rs index e00785e2b..42684ff2c 100644 --- a/crates/arty/tests/time.rs +++ b/crates/arty/tests/time.rs @@ -109,6 +109,7 @@ fn controlled_timer_callback_submission_wakes_bound_worker() { let control = ClockControl::new(); let runtime = Runtime::builder() + .monitor(arty::runtime::MonitorPolicy::disabled()) .workers(WorkersPolicy::exactly(1)) .clock(control.clone()) .build() diff --git a/crates/arty_executor/src/executor_shared.rs b/crates/arty_executor/src/executor_shared.rs index cdc5d7573..1dadc0602 100644 --- a/crates/arty_executor/src/executor_shared.rs +++ b/crates/arty_executor/src/executor_shared.rs @@ -16,7 +16,7 @@ use observed::Sink; use performables::sync::channel; use plurality::{Arc as PoolArc, MultiPool}; -use crate::inspection::Inspector; +use crate::inspection::CompletionInspector; use crate::wake::RemoteWake; use crate::{ PanicPolicy, Shutdown, TaskCompletion, TaskHandle, TaskMetadata, TypeErasedTask, WakeShared, WakeSignal, allocate_task, @@ -126,7 +126,7 @@ impl ExecutorShared { F: IntoFuture + 'static, F::Output: 'static, T: TaskCompletion, - I: Inspector, + I: CompletionInspector, { if self.shutdown().is_triggered() { if let Err(panic) = catch_unwind(AssertUnwindSafe(|| drop(inspector))) { diff --git a/crates/arty_executor/src/inspection.rs b/crates/arty_executor/src/inspection.rs index 827340cdd..75ac0eaaf 100644 --- a/crates/arty_executor/src/inspection.rs +++ b/crates/arty_executor/src/inspection.rs @@ -70,6 +70,11 @@ pub trait PollObserver: std::fmt::Debug { } /// Builds a temporary description while the native future is borrowed on its owner. +pub(crate) trait CompletionInspector: 'static { + #[cfg(feature = "inspection")] + fn inspect(&self, future: &F, completion: &T, state: TaskState, visitor: &mut dyn FnMut(&dyn Any)); +} + // The private module seals this trait. Public visibility permits opaque builder returns. pub trait Inspector: 'static { #[cfg(feature = "inspection")] @@ -77,11 +82,25 @@ pub trait Inspector: 'static { fn inspect(&self, future: &F, state: TaskState, visitor: &mut dyn FnMut(&dyn Any)); } +impl CompletionInspector for () { + #[cfg(feature = "inspection")] + fn inspect(&self, _future: &F, _completion: &T, _state: TaskState, _visitor: &mut dyn FnMut(&dyn Any)) {} +} + impl Inspector for () { #[cfg(feature = "inspection")] fn inspect(&self, _future: &F, _state: TaskState, _visitor: &mut dyn FnMut(&dyn Any)) {} } +pub(crate) struct FutureInspector(pub(crate) I); + +impl> CompletionInspector for FutureInspector { + #[cfg(feature = "inspection")] + fn inspect(&self, future: &F, _completion: &T, state: TaskState, visitor: &mut dyn FnMut(&dyn Any)) { + self.0.inspect(future, state, visitor); + } +} + #[cfg(feature = "inspection")] /// Stores the description closure inline with its native task. pub(crate) struct InspectWith { @@ -99,6 +118,19 @@ impl InspectWith { } } +#[cfg(feature = "inspection")] +impl CompletionInspector for InspectWith +where + I: Fn(&F, &T, TaskState) -> Option + 'static, + D: 'static, +{ + fn inspect(&self, future: &F, completion: &T, state: TaskState, visitor: &mut dyn FnMut(&dyn Any)) { + if let Some(description) = (self.inspect)(future, completion, state) { + visitor(&description); + } + } +} + #[cfg(feature = "inspection")] impl Inspector for InspectWith where diff --git a/crates/arty_executor/src/task.rs b/crates/arty_executor/src/task.rs index d99795f66..9bdba3479 100644 --- a/crates/arty_executor/src/task.rs +++ b/crates/arty_executor/src/task.rs @@ -18,7 +18,7 @@ use plurality::{Arc as PoolArc, MultiPool}; use crate::TaskHandle; #[cfg(feature = "inspection")] use crate::TaskState; -use crate::inspection::Inspector; +use crate::inspection::CompletionInspector as Inspector; use crate::{ ExecutorShared, ShutdownCause, TaskCreatedReporter, TaskHandleSlot, TaskMetadata, TaskPanicPayload, TaskPtr, WakeSignal, discard_task_panic_payload, @@ -42,7 +42,7 @@ where F: Future + 'static, R: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { /// `None` after the task completes or the executor abandons it during shutdown. /// @@ -84,7 +84,7 @@ where F: Future + 'static, R: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { } @@ -94,7 +94,7 @@ where F: Future + 'static, R: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { } @@ -103,7 +103,7 @@ where F: Future + 'static, R: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { fn new(future: F, result_target: T, metadata: TaskMetadata, sink: Sink, inspector: I) -> Self { Self { @@ -138,7 +138,7 @@ where F: Future + 'static, F::Output: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { pool.alloc_arc_pin_with(|| Task::new(future, completion, metadata, sink, inspector)) } @@ -177,7 +177,7 @@ where F: Future + 'static, R: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { #[cfg(feature = "inspection")] fn inspect(&self, state: TaskState, visitor: &mut dyn FnMut(&dyn Any)) { @@ -186,8 +186,9 @@ where let payload = unsafe { &*self.payload.get() }; if let Some(payload) = payload && let Some(future) = &payload.future.inner + && let Some(completion) = &payload.result_target { - payload.inspector.inspect(future, state, visitor); + payload.inspector.inspect(future, completion, state, visitor); } } @@ -251,7 +252,7 @@ where F: Future + 'static, R: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { fn pinned_wake_signal(self: Pin<&Self>) -> Pin<&WakeSignal> { let signal = self.get_ref().wake_signal.get().expect("task must be initialized before poll()"); @@ -408,7 +409,7 @@ where F: Future + 'static, R: 'static, T: TaskCompletion, - I: Inspector, + I: Inspector, { /// The future that will be polled by the executor to progress the task. future: FutureStorage, @@ -868,6 +869,7 @@ mod tests { let task = handle.as_ref(); let mut descriptions = 0; + task.inspect(TaskState::Ready, &mut |_| descriptions += 1); task.abort(ShutdownCause::default()); task.inspect(TaskState::Ready, &mut |_| descriptions += 1); drop(handle); diff --git a/crates/arty_executor/src/task_builder.rs b/crates/arty_executor/src/task_builder.rs index 60fcaad7b..a168e7f7c 100644 --- a/crates/arty_executor/src/task_builder.rs +++ b/crates/arty_executor/src/task_builder.rs @@ -87,6 +87,33 @@ where /// Registration does not wake the executor owner. The caller must arrange an /// [`execute_cycle`](crate::Executor::execute_cycle) after registration. pub fn register(self, completion: impl TaskCompletion) { - self.shared.add_task(self.future, self.metadata, completion, self.inspector); + self.shared.add_task( + self.future, + self.metadata, + completion, + crate::inspection::FutureInspector(self.inspector), + ); + } +} + +impl TaskBuilder<'_, F, I> +where + F: IntoFuture + 'static, + F::Output: 'static, +{ + /// Registers a task whose inspector borrows its future and completion target. + /// + /// The callback replaces any configured inspector. It runs only during + /// explicit inspection or observed polls. The completion target stores + /// metadata inline with the task and receives terminal results as usual. + #[cfg(feature = "inspection")] + pub fn register_with_inspection(self, completion: T, inspect: J) + where + T: TaskCompletion, + D: 'static, + J: Fn(&F::IntoFuture, &T, crate::TaskState) -> Option + 'static, + { + self.shared + .add_task(self.future, self.metadata, completion, crate::inspection::InspectWith::new(inspect)); } } diff --git a/crates/arty_executor/tests/inspection.rs b/crates/arty_executor/tests/inspection.rs index 12075837b..09a9cf3f1 100644 --- a/crates/arty_executor/tests/inspection.rs +++ b/crates/arty_executor/tests/inspection.rs @@ -27,6 +27,111 @@ impl TaskCompletion for Completion { fn cancelled(self, _cause: ShutdownCause) {} } +#[derive(Debug)] +struct InspectedCompletion { + inspections: Cell, + outcome: Rc>>, +} + +impl TaskCompletion<()> for InspectedCompletion { + fn completed(self, (): ()) { + self.outcome.set(Some(("completed", self.inspections.get()))); + } + + fn panicked(self, panic: TaskPanicPayload, _: Option) { + arty_executor::discard_task_panic_payload(panic); + self.outcome.set(Some(("panicked", self.inspections.get()))); + } + + fn cancelled(self, _: ShutdownCause) { + self.outcome.set(Some(("cancelled", self.inspections.get()))); + } +} + +#[expect(clippy::unwrap_used, reason = "test helpers use unwrap for concise failure backtraces")] +fn inspect_completion(future: impl Future + 'static, poll: bool) -> (Vec, Option<(&'static str, u32)>) { + let mut executor = Executor::builder().build(); + let outcome = Rc::new(Cell::new(None)); + executor.tasks().task(future).register_with_inspection( + InspectedCompletion { + inspections: Cell::new(0), + outcome: Rc::clone(&outcome), + }, + |_, completion, _| { + completion.inspections.set(completion.inspections.get() + 1); + Some(completion.inspections.get()) + }, + ); + let mut descriptions = Vec::new(); + executor.inspect(1, |value: &u32| descriptions.push(*value)).unwrap(); + if poll { + let _ = executor.execute_cycle(); + } + drop(executor); + (descriptions, outcome.get()) +} + +#[test] +fn inspection_borrows_the_target_that_receives_success() { + assert_eq!(inspect_completion(std::future::ready(()), true), (vec![1], Some(("completed", 1)))); +} + +#[test] +fn inspection_borrows_the_target_that_receives_a_panic() { + assert_eq!( + inspect_completion(async { panic!("inspected task panic") }, true), + (vec![1], Some(("panicked", 1))) + ); +} + +#[test] +fn inspection_borrows_the_target_that_receives_cancellation() { + assert_eq!( + inspect_completion(std::future::pending::<()>(), false), + (vec![1], Some(("cancelled", 1))) + ); +} + +#[test] +fn ordinary_cycles_do_not_inspect_the_completion_target() { + let mut executor = Executor::builder().build(); + let outcome = Rc::new(Cell::new(None)); + executor.tasks().task(std::future::ready(())).register_with_inspection( + InspectedCompletion { + inspections: Cell::new(0), + outcome: Rc::clone(&outcome), + }, + |_, _, _| -> Option<()> { panic!("ordinary cycles must not inspect completion metadata") }, + ); + let _ = executor.execute_cycle(); + + assert_eq!(outcome.get(), Some(("completed", 0))); +} + +#[test] +fn completion_inspection_can_decline_without_losing_the_result() { + let mut executor = Executor::builder().build(); + let outcome = Rc::new(Cell::new(None)); + executor.tasks().task(std::future::ready(())).register_with_inspection( + InspectedCompletion { + inspections: Cell::new(0), + outcome: Rc::clone(&outcome), + }, + |_, completion, _| { + completion.inspections.set(completion.inspections.get() + 1); + None:: + }, + ); + let mut descriptions = Vec::new(); + assert_eq!( + executor.inspect(1, |value: &u32| descriptions.push(*value)).unwrap(), + InspectionStatus::Complete + ); + assert!(descriptions.is_empty()); + let _ = executor.execute_cycle(); + assert_eq!(outcome.get(), Some(("completed", 1))); +} + struct InspectionInput { converted: Rc>, shutdown: Option, @@ -666,6 +771,46 @@ fn observed_polls_pair_callbacks_only_for_described_tasks() { assert_eq!((observer.starts.get(), observer.finishes.get(), observer.errors.get()), (1, 1, 0)); } +#[test] +fn observed_polls_borrow_the_completion_target() { + let mut executor = Executor::builder().build(); + let outcome = Rc::new(Cell::new(None)); + executor.tasks().task(std::future::ready(())).register_with_inspection( + InspectedCompletion { + inspections: Cell::new(41), + outcome: Rc::clone(&outcome), + }, + |_, completion, state| { + assert_eq!(state, TaskState::Running); + completion.inspections.set(completion.inspections.get() + 1); + Some(completion.inspections.get()) + }, + ); + let observer = Observer::default(); + + let _ = executor.execute_cycle_observed(&observer); + + assert_eq!((observer.starts.get(), observer.finishes.get(), observer.errors.get()), (1, 1, 0)); + assert_eq!(outcome.get(), Some(("completed", 42))); +} + +#[test] +fn rejected_tasks_cancel_the_completion_target_without_inspecting_it() { + let executor = Executor::builder().build(); + executor.shutdown().trigger(ShutdownCause::default()); + let outcome = Rc::new(Cell::new(None)); + + executor.tasks().task(std::future::pending::<()>()).register_with_inspection( + InspectedCompletion { + inspections: Cell::new(7), + outcome: Rc::clone(&outcome), + }, + |_, _, _| -> Option<()> { panic!("a stopped executor must not inspect rejected tasks") }, + ); + + assert_eq!(outcome.get(), Some(("cancelled", 7))); +} + #[test] fn a_panicking_description_does_not_prevent_its_task_from_running() { let mut executor = Executor::builder().build(); diff --git a/crates/seismograph_runtime/Cargo.toml b/crates/seismograph_runtime/Cargo.toml index 2009c237d..b549254c6 100644 --- a/crates/seismograph_runtime/Cargo.toml +++ b/crates/seismograph_runtime/Cargo.toml @@ -25,6 +25,9 @@ exclude = true [package.metadata.cargo_check_external_types] allowed_external_types = ["seismograph::*"] +[features] +test-util = ["tick/test-util"] + [dependencies] seismograph.workspace = true tick.workspace = true diff --git a/crates/seismograph_runtime/src/collection.rs b/crates/seismograph_runtime/src/collection.rs index c713023e6..cad7f8b0d 100644 --- a/crates/seismograph_runtime/src/collection.rs +++ b/crates/seismograph_runtime/src/collection.rs @@ -120,6 +120,17 @@ pub(crate) fn capture(runtime: &Arc, options: CaptureOptions) -> ) } +#[cfg(feature = "test-util")] +pub(crate) fn capture_with_timeout(runtime: &Arc, options: CaptureOptions) -> Runtime { + capture_with( + runtime, + options, + &SimpleClock::new_frozen(), + |_| {}, + |_, _| Err(mpsc::RecvTimeoutError::Timeout), + ) +} + fn capture_with( runtime: &Arc, options: CaptureOptions, @@ -184,7 +195,16 @@ fn capture_with( worker.snapshot(batch.collection) }) .collect(); - tasks.sort_unstable_by_key(|task| task.id); + // A shared-queue task can start between two workers' responses. Keep its + // executing observation rather than also reporting its earlier queue entry. + tasks.sort_unstable_by_key(|task| { + ( + task.id, + task.activity + .is_none_or(|activity| activity.state != crate::snapshot::TaskActivityState::Running), + ) + }); + tasks.dedup_by_key(|task| task.id); let state = RuntimeState::from_wire_value(runtime.state.load(Ordering::Acquire)).expect("runtime state stores enum values"); Runtime { id: runtime.id, @@ -473,6 +493,31 @@ mod tests { ) } + #[cfg(feature = "test-util")] + #[test] + fn forced_timeout_marks_an_unanswered_worker_without_waiting() { + let runtime = register_runtime(RuntimeMetadata::new("forced-timeout", 1)); + let worker = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let (sender, receiver) = mpsc::channel(); + worker + .handle() + .register_task_provider(move |request| sender.send(request).unwrap()) + .unwrap(); + + let captured = runtime + .handle() + .capture_with_timeout(CaptureOptions { + measure_for: Duration::from_secs(1), + ..CaptureOptions::default() + }) + .unwrap(); + let request = receiver.try_recv().unwrap(); + + assert_eq!(captured.workers[0].task_collection, TaskCollection::TimedOut); + assert!(request.is_canceled()); + assert!(!worker.handle().is_observed()); + } + #[test] fn a_poll_from_a_finished_window_cannot_enter_the_next_window() { let _test = crate::tests::test_lock(); diff --git a/crates/seismograph_runtime/src/lib.rs b/crates/seismograph_runtime/src/lib.rs index b9cebc1db..6d58507dd 100644 --- a/crates/seismograph_runtime/src/lib.rs +++ b/crates/seismograph_runtime/src/lib.rs @@ -213,11 +213,30 @@ impl RuntimeHandle { /// Returns an error for invalid limits or a runtime that already stopped. /// Worker timeouts and provider failures appear in the returned worker rows. pub fn capture(&self, options: snapshot::CaptureOptions) -> Result { + self.validate_capture(&options)?; + Ok(without_recording(|| collection::capture(&self.control, options))) + } + + /// Dispatches a capture and simulates a timeout while waiting for responses. + /// + /// Uses a frozen clock and does not wait for measurement or responses. + /// Queued requests are canceled, just as after a real timeout. + /// + /// # Errors + /// + /// Returns an error for invalid limits or a runtime that already stopped. + #[cfg(feature = "test-util")] + pub fn capture_with_timeout(&self, options: snapshot::CaptureOptions) -> Result { + self.validate_capture(&options)?; + Ok(without_recording(|| collection::capture_with_timeout(&self.control, options))) + } + + fn validate_capture(&self, options: &snapshot::CaptureOptions) -> Result<(), seismograph::Error> { options.validate()?; if self.control.state.load(Ordering::Acquire) == RuntimeState::Stopped.wire_value() { return Err(seismograph::Error::new("cannot inspect a stopped runtime")); } - Ok(without_recording(|| collection::capture(&self.control, options))) + Ok(()) } /// Marks the runtime as stopping and records the transition once. diff --git a/crates/seismograph_runtime/src/snapshot.rs b/crates/seismograph_runtime/src/snapshot.rs index f76bccbb1..dbd4be3c8 100644 --- a/crates/seismograph_runtime/src/snapshot.rs +++ b/crates/seismograph_runtime/src/snapshot.rs @@ -395,6 +395,9 @@ pub struct Runtime { /// Active and retired worker metadata. pub workers: Vec, /// Spawn metadata for tasks that have not reached a terminal state. + /// + /// Captures contain at most one row per task identity, sorted by identity. + /// If worker responses contain both queued and running observations, the capture keeps the running observation. pub tasks: Vec, } diff --git a/crates/seismograph_runtime/src/task.rs b/crates/seismograph_runtime/src/task.rs index dcb2cb30c..6288e4fe4 100644 --- a/crates/seismograph_runtime/src/task.rs +++ b/crates/seismograph_runtime/src/task.rs @@ -23,6 +23,20 @@ pub struct EventContext { backtrace: BacktraceCapture, } +/// Identity and timestamp assigned while building a task creation event. +/// +/// Sampling or a concurrent recording change can omit the event after this +/// metadata is assigned. Keep the identity for inspection and later outcomes. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct TaskSpawn { + /// Process-wide task identity. + pub id: TaskId, + /// Concrete task type identity used by the event. + pub descriptor: TypeDescriptorId, + /// Timestamp used by the task creation event. + pub timestamp: EventTimestamp, +} + impl EventContext { pub(crate) const fn new(runtime: RuntimeId, worker: Option, backtrace: BacktraceCapture) -> Self { Self { @@ -44,7 +58,30 @@ impl EventContext { /// Panics if process task identities are exhausted or `body_size` cannot be /// encoded as a value smaller than `u64::MAX`. #[must_use] + #[inline] pub fn spawned(&self, descriptor: TypeDescriptorId, parent: Option, body_size: usize) -> Option { + self.spawned_with(|| descriptor, parent, body_size).map(|spawn| spawn.id) + } + + /// Builds a task creation event and returns its identity and timestamp. + /// + /// Calls `descriptor` only when the recorder runs the event builder. + /// Disabled or suppressed recording does not call it. Sampling and + /// concurrent recording changes can still omit the event after it runs. + /// Reuse the returned timestamp in native task metadata to avoid another + /// clock read. Measure `body_size` before wrapping or type erasure. + /// + /// # Panics + /// + /// Propagates a panic from `descriptor`. Panics if process task identities + /// are exhausted or `body_size` cannot be encoded below `u64::MAX`. + #[must_use] + pub fn spawned_with( + &self, + descriptor: impl FnOnce() -> TypeDescriptorId, + parent: Option, + body_size: usize, + ) -> Option { let mut assigned = None; seismograph::record(EventClass::RuntimeTask, || { let size = u64::try_from(body_size) @@ -52,9 +89,11 @@ impl EventContext { .and_then(|size| size.checked_add(1)) .expect("a concrete task body must be smaller than u64::MAX"); let id = crate::allocate_task_id(); - assigned = Some(id); + let descriptor = descriptor(); + let timestamp = EventTimestamp::now(); + assigned = Some(TaskSpawn { id, descriptor, timestamp }); Some(Record::runtime( - EventTimestamp::now(), + timestamp, EventKind::TaskSpawned, RuntimeEvent { runtime_id: self.runtime, diff --git a/crates/seismograph_runtime/tests/on_demand.rs b/crates/seismograph_runtime/tests/on_demand.rs index 148533eb7..fa482fbfc 100644 --- a/crates/seismograph_runtime/tests/on_demand.rs +++ b/crates/seismograph_runtime/tests/on_demand.rs @@ -73,6 +73,63 @@ fn all_native_constructor_states_survive_process_snapshot_encoding() { ); } +#[test] +fn captures_keep_one_running_row_when_a_task_also_appears_in_a_queue() { + let _lock = RECORDING.lock().unwrap(); + for running_first in [false, true] { + let runtime = register_runtime(RuntimeMetadata::new("shared-queue-transition", 2)); + let first = runtime.register_worker(WorkerMetadata::new(WorkerRole::Blocking)); + let second = runtime.register_worker(WorkerMetadata::new(WorkerRole::Blocking)); + let task = allocate_task_id(); + let other_task = allocate_task_id(); + let descriptor = allocate_type_descriptor_id(); + let (running, ready) = if running_first { (&first, &second) } else { (&second, &first) }; + running + .handle() + .register_task_provider(move |request| { + request.respond( + vec![Task::new(task, descriptor, TaskActivityState::Running)], + TaskCollection::Complete, + ); + }) + .unwrap(); + ready + .handle() + .register_task_provider(move |request| { + request.respond( + vec![ + Task::new(task, descriptor, TaskActivityState::Ready), + Task::new(other_task, descriptor, TaskActivityState::Ready), + ], + TaskCollection::Truncated, + ); + }) + .unwrap(); + + let captured = runtime.handle().capture(CaptureOptions::default()).unwrap(); + + assert_eq!(captured.tasks.len(), 2); + assert_eq!( + ( + captured.tasks[0].id, + captured.tasks[0].last_worker_id, + captured.tasks[0].activity.unwrap().state + ), + (task, Some(running.id()), TaskActivityState::Running) + ); + assert_eq!(captured.tasks[1].id, other_task); + assert_eq!( + captured + .workers + .iter() + .find(|worker| worker.id == ready.id()) + .unwrap() + .task_collection, + TaskCollection::Truncated + ); + } +} + #[test] fn worker_metadata_transitions_and_runtime_retirement_remain_visible() { use seismograph_runtime::snapshot::{RuntimeState, WorkerState}; @@ -377,6 +434,8 @@ fn event_and_capture_values_preserve_copy_and_unwind_contracts() { fn copied() {} value::(); copied::(); + value::(); + copied::(); value::(); value::(); value::(); @@ -708,3 +767,77 @@ fn disabled_task_event_context_does_not_initialize_event_buffers() { assert_eq!((created, events.events.len(), events.threads.len()), (None, 0, 0)); } + +#[test] +fn lazy_spawn_metadata_matches_the_recorded_event() { + let _lock = RECORDING.lock().unwrap(); + seismograph::recorder(Configuration::default()); + let runtime = register_runtime(RuntimeMetadata::new("lazy-spawn-metadata", 1)); + let worker = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let context = worker.handle().task_events(); + let descriptor = allocate_type_descriptor_id(); + let parent = allocate_task_id(); + let calls = std::cell::Cell::new(0); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + + let spawned = context + .spawned_with( + || { + calls.set(calls.get() + 1); + descriptor + }, + Some(parent), + 64, + ) + .unwrap(); + let encoded = seismograph::snapshot(SnapshotOptions::default()).unwrap(); + let snapshot = seismograph::snapshot::decode(encoded.as_bytes()).unwrap(); + seismograph::recorder(Configuration::default()); + let event = snapshot + .events + .events + .iter() + .find(|event| { + event.kind == EventKind::TaskSpawned + && event + .runtime() + .is_some_and(|payload| payload.runtime_id == runtime.id() && payload.subject_id == spawned.id.get()) + }) + .unwrap(); + let payload = event.runtime().unwrap(); + + assert_eq!( + ( + calls.get(), + spawned.descriptor, + spawned.timestamp, + payload.related_id, + payload.value_0, + payload.value_1 + ), + (1, descriptor, event.timestamp, parent.get(), descriptor.get(), 65) + ); +} + +#[test] +fn disabled_and_suppressed_spawns_do_not_build_type_metadata() { + let _lock = RECORDING.lock().unwrap(); + seismograph::recorder(Configuration::default()); + let runtime = register_runtime(RuntimeMetadata::new("lazy-disabled-spawn", 1)); + let worker = runtime.register_worker(WorkerMetadata::new(WorkerRole::Core)); + let context = worker.handle().task_events(); + let descriptor = || panic!("disabled or suppressed recording must not build task type metadata"); + + let disabled = context.spawned_with(descriptor, None, 0); + seismograph::recorder(Configuration { + runtime_tasks: Policy::all(false), + ..Configuration::default() + }); + let suppressed = without_recording(|| context.spawned_with(descriptor, None, 0)); + seismograph::recorder(Configuration::default()); + + assert_eq!((disabled, suppressed), (None, None)); +}