Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/notes/2.33.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Thank you to [Klaviyo](https://www.klaviyo.com/) for their Platinum tier support

### General

The `indicatif-spinner` `dynamic_ui_renderer` (default), now renders on a dedicated thread. This should reduce jitter and display a smoother spinner under heavy load.

### Goals

### Backends
Expand Down
120 changes: 98 additions & 22 deletions src/rust/ui/src/instance/indicatif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::cmp;
use std::collections::HashMap;
use std::future;
use std::sync::Arc;
use std::time::SystemTime;
use std::thread;
use std::time::{Duration, SystemTime};

use futures::FutureExt;
use futures::future::BoxFuture;
Expand All @@ -15,19 +16,25 @@ use indicatif::ProgressBar;
use indicatif::ProgressDrawTarget;
use indicatif::ProgressStyle;
use indicatif::WeakProgressBar;
use parking_lot::Mutex;
use parking_lot::{Condvar, Mutex};

use workunit_store::SpanId;
use workunit_store::format_workunit_duration_ms;

use super::TaskState;
use crate::ConsoleUI;

#[derive(Clone)]
struct Task {
label: String,
start_time: SystemTime,
}

pub struct IndicatifInstance {
tasks_to_display: IndexSet<SpanId>,
// NB: Kept for Drop.
_multi_progress: MultiProgress,
bars: Vec<ProgressBar>,
snapshot: Arc<Mutex<Vec<Option<Task>>>>,
stopping: Arc<(Mutex<bool>, Condvar)>,
join_handle: Option<thread::JoinHandle<()>>,
}

impl IndicatifInstance {
Expand Down Expand Up @@ -59,17 +66,34 @@ impl IndicatifInstance {
.collect::<Vec<_>>();

*stderr_dest_bar_guard = Some(bars[0].downgrade());
drop(stderr_dest_bar_guard);

Ok(IndicatifInstance {
tasks_to_display: IndexSet::new(),
let snapshot = Arc::new(Mutex::new((0..bars.len()).map(|_| None).collect()));
let stopping = Arc::new((Mutex::new(false), Condvar::new()));

let render_loop = RenderLoop {
_multi_progress: multi_progress,
bars,
snapshot: snapshot.clone(),
stopping: stopping.clone(),
interval: ConsoleUI::render_interval(),
};
let join_handle = thread::Builder::new()
.name("pants-indicatif-render".into())
.spawn(move || render_loop.run())
.map_err(|e| format!("Failed to spawn indicatif render thread: {e}"))?;

Ok(IndicatifInstance {
tasks_to_display: IndexSet::new(),
snapshot,
stopping,
join_handle: Some(join_handle),
})
}

pub fn teardown(self) -> BoxFuture<'static, ()> {
// When the MultiProgress completes, the Term(Destination) is dropped, which will restore
// direct access to the Console.
// Drop signals the render thread to stop, joins it, and then drops the MultiProgress
// (which restores direct access to the Console).
std::mem::drop(self);
future::ready(()).boxed()
}
Expand All @@ -92,23 +116,75 @@ impl IndicatifInstance {
},
);

let now = SystemTime::now();
for (n, pbar) in self.bars.iter().enumerate() {
let maybe_label = tasks_to_display.get_index(n).map(|span_id| {
let (label, start_time) = heavy_hitters.get(span_id).unwrap();
let duration_label = match now.duration_since(*start_time).ok() {
None => "(Waiting)".to_string(),
Some(duration) => format_workunit_duration_ms(duration),
};
format!("{duration_label} {label}")
let mut snap = self.snapshot.lock();
for slot in snap.iter_mut() {
*slot = None;
}
for (n, span_id) in tasks_to_display.iter().enumerate() {
if n >= snap.len() {
break;
}
let (label, start_time) = heavy_hitters.get(span_id).unwrap();
snap[n] = Some(Task {
label: label.clone(),
start_time: *start_time,
});
}
}
}

match maybe_label {
Some(label) => pbar.set_message(label),
None => pbar.set_message(""),
impl Drop for IndicatifInstance {
fn drop(&mut self) {
{
let (lock, cv) = &*self.stopping;
*lock.lock() = true;
cv.notify_all();
}
if let Some(h) = self.join_handle.take() {
let _ = h.join();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably this could catch a panic in the thread, e.g. if let Err(e) = h.join() { log::warn!("indicatif render thread panicked: {e:?}"); }. But I think it's pretty common to ignore this. Up to you.

}
}
}

struct RenderLoop {
// NB: Kept for Drop so the terminal is restored when the render thread exits.
_multi_progress: MultiProgress,
bars: Vec<ProgressBar>,
snapshot: Arc<Mutex<Vec<Option<Task>>>>,
stopping: Arc<(Mutex<bool>, Condvar)>,
interval: Duration,
}

impl RenderLoop {
fn run(self) {
loop {
// Clone the current snapshot out quickly so we don't hold the caller's lock
// while writing to the terminal.
let snap = self.snapshot.lock().clone();
let now = SystemTime::now();
for (n, pbar) in self.bars.iter().enumerate() {
match snap.get(n).and_then(|e| e.as_ref()) {
Some(task) => {
let duration_label = match now.duration_since(task.start_time).ok() {
None => "(Waiting)".to_string(),
Some(duration) => format_workunit_duration_ms(duration),
};
pbar.set_message(format!("{duration_label} {}", task.label));
}
None => pbar.set_message(""),
}
pbar.tick();
}

pbar.tick();
let (lock, cv) = &*self.stopping;
let mut stopping = lock.lock();
if *stopping {
break;
}
cv.wait_for(&mut stopping, self.interval);
if *stopping {
break;
}
}
}
}
Expand Down
Loading