🔀 merge(E2): the export names its clocks — elapsed, converging remaining, percent, esc always in sight
Browse files at this commit1 changed file +166 -5
- crates/app/src/main.rs +166 -5
crates/app/src/main.rsmodified+166 -5
| @@ -999,6 +999,11 @@ struct Player { | |||
| 999 | /// handle is held until the worker settles, because its last act is to | 999 | /// handle is held until the worker settles, because its last act is to |
| 1000 | /// delete the output file and a second export must not be what it deletes. | 1000 | /// delete the output file and a second export must not be what it deletes. |
| 1001 | cancelling: bool, | 1001 | cancelling: bool, |
| 1002 | + /// When the running export started, and how far it had come at each sample | ||
| 1003 | + /// since, as `(elapsed, progress)` marks. The elapsed clock and the | ||
| 1004 | + /// rolling-window estimate the progress line reads; see [`note_progress`]. | ||
| 1005 | + export_started: Option<Instant>, | ||
| 1006 | + export_marks: Vec<(f32, f32)>, | ||
| 1002 | /// Where an export writes. Built once from the source path, which is not | 1007 | /// Where an export writes. Built once from the source path, which is not |
| 1003 | /// otherwise kept. | 1008 | /// otherwise kept. |
| 1004 | export_path: PathBuf, | 1009 | export_path: PathBuf, |
| @@ -4008,6 +4013,10 @@ impl Player { | |||
| 4008 | } | 4013 | } |
| 4009 | session.pause(); | 4014 | session.pause(); |
| 4010 | self.export = Some(session.export_to_with(&self.export_path, &settings)); | 4015 | self.export = Some(session.export_to_with(&self.export_path, &settings)); |
| 4016 | + // The clock starts with the worker, not with the first repaint that | ||
| 4017 | + // happens to notice it. | ||
| 4018 | + self.export_started = Some(Instant::now()); | ||
| 4019 | + self.export_marks.clear(); | ||
| 4011 | // The card has been answered; the progress line takes the panel from | 4020 | // The card has been answered; the progress line takes the panel from |
| 4012 | // here, and it is the running export's escape that matters now. | 4021 | // here, and it is the running export's escape that matters now. |
| 4013 | self.export_open = false; | 4022 | self.export_open = false; |
| @@ -4026,10 +4035,23 @@ impl Player { | |||
| 4026 | /// Takes the export's verdict once it has one. The only place the app | 4035 | /// Takes the export's verdict once it has one. The only place the app |
| 4027 | /// touches the handle's completion side. | 4036 | /// touches the handle's completion side. |
| 4028 | fn poll_export(&mut self) { | 4037 | fn poll_export(&mut self) { |
| 4038 | + // Sampled here rather than while drawing: a repaint stays a repaint, | ||
| 4039 | + // and this runs once per repaint either way. | ||
| 4040 | + if let (Some(progress), Some(started)) = ( | ||
| 4041 | + self.exporting().map(ExportHandle::progress), | ||
| 4042 | + self.export_started, | ||
| 4043 | + ) { | ||
| 4044 | + note_progress( | ||
| 4045 | + &mut self.export_marks, | ||
| 4046 | + started.elapsed().as_secs_f32(), | ||
| 4047 | + progress, | ||
| 4048 | + ); | ||
| 4049 | + } | ||
| 4029 | let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else { | 4050 | let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else { |
| 4030 | return; | 4051 | return; |
| 4031 | }; | 4052 | }; |
| 4032 | self.export = None; | 4053 | self.export = None; |
| 4054 | + self.export_started = None; | ||
| 4033 | // A cancellation is reported as an error, and the one who asked for it | 4055 | // A cancellation is reported as an error, and the one who asked for it |
| 4034 | // has had the editor back since the keystroke. Nothing to say. | 4056 | // has had the editor back since the keystroke. Nothing to say. |
| 4035 | if std::mem::take(&mut self.cancelling) { | 4057 | if std::mem::take(&mut self.cancelling) { |
| @@ -4984,10 +5006,25 @@ impl Player { | |||
| 4984 | } | 5006 | } |
| 4985 | let (hint, filled) = if let Some(export) = self.exporting() { | 5007 | let (hint, filled) = if let Some(export) = self.exporting() { |
| 4986 | let progress = export.progress(); | 5008 | let progress = export.progress(); |
| 5009 | + let elapsed = self | ||
| 5010 | + .export_started | ||
| 5011 | + .map_or(0., |t| t.elapsed().as_secs_f32()); | ||
| 5012 | + // Two numbers that must both be honest: the one that counts up is | ||
| 5013 | + // measured, the one that counts down is a guess and says so. | ||
| 5014 | + let left = eta_secs(&self.export_marks, elapsed, progress).map_or_else( | ||
| 5015 | + || "estimating…".to_owned(), | ||
| 5016 | + |s| format!("~{} left", clock(s)), | ||
| 5017 | + ); | ||
| 4987 | ( | 5018 | ( |
| 4988 | format!( | 5019 | format!( |
| 4989 | - "EXPORTING {}% · {} · {} — {} cancels", | 5020 | + // Clocks, then the way out, then what is encoding: at the |
| 5021 | + // 640 px floor the tail is what truncation eats, and the | ||
| 5022 | + // codec pair is the one part of this line the card already | ||
| 5023 | + // said. The escape must not be what goes missing. | ||
| 5024 | + "EXPORTING {}% · {} elapsed · {left} — {} cancels · {} · {}", | ||
| 4990 | (progress * 100.) as u32, | 5025 | (progress * 100.) as u32, |
| 5026 | + clock(elapsed), | ||
| 5027 | + key(ActionId::CancelExport), | ||
| 4991 | // The row that was picked; the engine's line below names the | 5028 | // The row that was picked; the engine's line below names the |
| 4992 | // seats alone, since the library is what identifies a codec. | 5029 | // seats alone, since the library is what identifies a codec. |
| 4993 | format_label(self.format), | 5030 | format_label(self.format), |
| @@ -4996,7 +5033,6 @@ impl Player { | |||
| 4996 | export | 5033 | export |
| 4997 | .encoders() | 5034 | .encoders() |
| 4998 | .unwrap_or_else(|| "opening the encoder".to_string()), | 5035 | .unwrap_or_else(|| "opening the encoder".to_string()), |
| 4999 | - key(ActionId::CancelExport) | ||
| 5000 | ), | 5036 | ), |
| 5001 | progress, | 5037 | progress, |
| 5002 | ) | 5038 | ) |
| @@ -10715,6 +10751,57 @@ fn timecode(t: f64, fps: f64) -> String { | |||
| 10715 | ) | 10751 | ) |
| 10716 | } | 10752 | } |
| 10717 | 10753 | ||
| 10754 | +/// Wall clock for a progress line: `M:SS`, minutes past the hour included | ||
| 10755 | +/// rather than an hours field nobody reads on an export. | ||
| 10756 | +fn clock(secs: f32) -> String { | ||
| 10757 | + let secs = secs.max(0.) as u64; | ||
| 10758 | + format!("{}:{:02}", secs / 60, secs % 60) | ||
| 10759 | +} | ||
| 10760 | + | ||
| 10761 | +/// How often a progress mark is worth keeping, how far back the rate is | ||
| 10762 | +/// measured, and the least span that may answer at all. An export crosses | ||
| 10763 | +/// hardware and software segments that run at different speeds, so the | ||
| 10764 | +/// estimate is a window's average and never the instant's. | ||
| 10765 | +const ETA_SAMPLE: f32 = 0.5; | ||
| 10766 | +const ETA_WINDOW: f32 = 8.; | ||
| 10767 | +const ETA_SPAN: f32 = 1.5; | ||
| 10768 | + | ||
| 10769 | +/// Records where the export has got to and forgets what has fallen out of the | ||
| 10770 | +/// window. One mark per `ETA_SAMPLE`, a window's worth kept: a bounded list | ||
| 10771 | +/// whichever way the encode goes. | ||
| 10772 | +fn note_progress(marks: &mut Vec<(f32, f32)>, elapsed: f32, progress: f32) { | ||
| 10773 | + if marks.last().is_none_or(|&(t, _)| elapsed - t >= ETA_SAMPLE) { | ||
| 10774 | + marks.push((elapsed, progress)); | ||
| 10775 | + } | ||
| 10776 | + while marks.len() > 2 && marks[0].0 < elapsed - ETA_WINDOW { | ||
| 10777 | + marks.remove(0); | ||
| 10778 | + } | ||
| 10779 | +} | ||
| 10780 | + | ||
| 10781 | +/// Seconds left at the window's rate, or `None` while nothing measurable has | ||
| 10782 | +/// happened yet -- which the line says as "estimating…" rather than as a | ||
| 10783 | +/// number it would have to take back. | ||
| 10784 | +fn eta_secs(marks: &[(f32, f32)], elapsed: f32, progress: f32) -> Option<f32> { | ||
| 10785 | + // A finished pass is not a guess: it is over. | ||
| 10786 | + if progress >= 1. { | ||
| 10787 | + return Some(0.); | ||
| 10788 | + } | ||
| 10789 | + if progress <= 0. || elapsed < ETA_SPAN { | ||
| 10790 | + return None; | ||
| 10791 | + } | ||
| 10792 | + // Two rates, averaged. The window's follows the encode across a | ||
| 10793 | + // hardware-to-software handover; the whole run's is what keeps a window | ||
| 10794 | + // that is all stall from throwing the number minutes out and back. Neither | ||
| 10795 | + // alone reads well: raw window rate spikes eightfold on either edge of a | ||
| 10796 | + // stall, and the run average alone never notices a segment change. | ||
| 10797 | + let overall = progress / elapsed; | ||
| 10798 | + let recent = marks | ||
| 10799 | + .first() | ||
| 10800 | + .filter(|&&(t, _)| elapsed - t >= ETA_SPAN) | ||
| 10801 | + .map_or(overall, |&(t, p)| (progress - p) / (elapsed - t)); | ||
| 10802 | + Some(2. * (1. - progress) / (recent + overall)) | ||
| 10803 | +} | ||
| 10804 | + | ||
| 10718 | #[cfg(test)] | 10805 | #[cfg(test)] |
| 10719 | mod tests { | 10806 | mod tests { |
| 10720 | use super::{ | 10807 | use super::{ |
| @@ -10740,9 +10827,10 @@ mod tests { | |||
| 10740 | whole_take, window_title, | 10827 | whole_take, window_title, |
| 10741 | }; | 10828 | }; |
| 10742 | use super::{ | 10829 | use super::{ |
| 10743 | - Choice, Edge, FITS, LaneKind, PPS_DEFAULT, RESOLUTIONS, Repeat, Scale, View, | 10830 | + Choice, ETA_SPAN, Edge, FITS, LaneKind, PPS_DEFAULT, RESOLUTIONS, Repeat, Scale, View, |
| 10744 | - ZOOM_MAX_SECONDS, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, fit_choices, | 10831 | + ZOOM_MAX_SECONDS, ZOOM_MIN_FRAMES, ZOOM_STEP, clock, eta_secs, file_name, file_uri, |
| 10745 | - landing, lane_refuses, library_rows, live_idx, next_fit, next_resolution, px_along, | 10832 | + fit_choices, landing, lane_refuses, library_rows, live_idx, next_fit, next_resolution, |
| 10833 | + note_progress, px_along, | ||
| 10746 | repeats, resolution_choices, resolution_ladder, span_label, trimmed_clip, unscannable, | 10834 | repeats, resolution_choices, resolution_ladder, span_label, trimmed_clip, unscannable, |
| 10747 | visible_slice, | 10835 | visible_slice, |
| 10748 | }; | 10836 | }; |
| @@ -13624,6 +13712,77 @@ mod tests { | |||
| 13624 | ); | 13712 | ); |
| 13625 | } | 13713 | } |
| 13626 | 13714 | ||
| 13715 | + /// The progress line's two clocks, driven the way a repaint drives them: | ||
| 13716 | + /// steady work, a stall where hardware hands over to software, then steady | ||
| 13717 | + /// work again. The estimate may not whipsaw, may not vanish once it has | ||
| 13718 | + /// been given, and must meet the elapsed clock at the end. | ||
| 13719 | + #[test] | ||
| 13720 | + fn the_export_estimate_rides_out_a_stall_and_converges() { | ||
| 13721 | + let (mut marks, mut elapsed, mut progress) = (Vec::new(), 0f32, 0f32); | ||
| 13722 | + // 2%/s, a 12 s stall at 40% -- longer than the window, so the window | ||
| 13723 | + // alone would have nothing left to measure -- and the same rate to the | ||
| 13724 | + // end: 62 s of wall clock for 50 s of work. | ||
| 13725 | + let rate = 0.02; | ||
| 13726 | + let (mut quiet, mut before_stall, mut after_stall, mut last) = (0f32, 0., 0., f32::MAX); | ||
| 13727 | + while progress < 1. { | ||
| 13728 | + let stalled = (20. ..32.).contains(&elapsed); | ||
| 13729 | + note_progress(&mut marks, elapsed, progress); | ||
| 13730 | + // What is really left, to hold every guess against. | ||
| 13731 | + let truth = (1. - progress) / rate + if stalled { 32. - elapsed } else { 0. }; | ||
| 13732 | + match eta_secs(&marks, elapsed, progress) { | ||
| 13733 | + // "estimating…" is only allowed before there is a span to | ||
| 13734 | + // measure, and never again after the first number. | ||
| 13735 | + None => { | ||
| 13736 | + assert!(elapsed < ETA_SPAN + 1., "estimate vanished at {elapsed}"); | ||
| 13737 | + quiet = elapsed; | ||
| 13738 | + } | ||
| 13739 | + Some(left) => { | ||
| 13740 | + // No guess is ever wilder than four times the truth: that | ||
| 13741 | + // is the eightfold spike a raw window rate throws on either | ||
| 13742 | + // edge of the stall. | ||
| 13743 | + assert!(left <= truth * 4., "at {elapsed}s: {left} vs {truth}"); | ||
| 13744 | + // While the rate holds, the answer is the true one and it | ||
| 13745 | + // only ever counts down. | ||
| 13746 | + if elapsed >= 5. && elapsed < 20. { | ||
| 13747 | + assert!((left - truth).abs() <= truth * 0.15, "{elapsed}s: {left}"); | ||
| 13748 | + assert!(left < last, "estimate grew while the rate held"); | ||
| 13749 | + } | ||
| 13750 | + // Eight seconds past the stall it has caught up again. | ||
| 13751 | + if elapsed >= 40. { | ||
| 13752 | + assert!((left - truth).abs() <= truth * 0.25, "{elapsed}s: {left}"); | ||
| 13753 | + assert!(left < last + 0.001, "estimate grew after the stall"); | ||
| 13754 | + } | ||
| 13755 | + if (20. ..20.25).contains(&elapsed) { | ||
| 13756 | + before_stall = left; | ||
| 13757 | + } | ||
| 13758 | + if (31.5..31.75).contains(&elapsed) { | ||
| 13759 | + after_stall = left; | ||
| 13760 | + } | ||
| 13761 | + last = left; | ||
| 13762 | + } | ||
| 13763 | + } | ||
| 13764 | + // A window's worth of marks and no more, however long the encode. | ||
| 13765 | + assert!(marks.len() <= 20, "{} marks", marks.len()); | ||
| 13766 | + elapsed += 0.25; | ||
| 13767 | + if !stalled { | ||
| 13768 | + progress = (progress + rate * 0.25).min(1.); | ||
| 13769 | + } | ||
| 13770 | + } | ||
| 13771 | + assert!(quiet > 0. && quiet < ETA_SPAN + 1.); | ||
| 13772 | + // The stall stretched the guess instead of erasing it, and by more than | ||
| 13773 | + // the stopped clock adds on its own. | ||
| 13774 | + assert!( | ||
| 13775 | + after_stall > before_stall + 12., | ||
| 13776 | + "{before_stall} -> {after_stall}" | ||
| 13777 | + ); | ||
| 13778 | + // Both clocks meet: a finished pass has nothing left. | ||
| 13779 | + assert_eq!(eta_secs(&marks, elapsed, 1.), Some(0.)); | ||
| 13780 | + assert!((elapsed - 62.).abs() < 1., "{elapsed}"); | ||
| 13781 | + assert_eq!(clock(83.4), "1:23"); | ||
| 13782 | + assert_eq!(clock(114.), "1:54"); | ||
| 13783 | + assert_eq!(clock(-1.), "0:00"); | ||
| 13784 | + } | ||
| 13785 | + | ||
| 13627 | #[test] | 13786 | #[test] |
| 13628 | fn every_codec_row_is_offered_or_says_why_not() { | 13787 | fn every_codec_row_is_offered_or_says_why_not() { |
| 13629 | // One row per codec, and the boxes it can go in are the container row's | 13788 | // One row per codec, and the boxes it can go in are the container row's |
| @@ -14508,6 +14667,8 @@ fn main() { | |||
| 14508 | last_target: 0, | 14667 | last_target: 0, |
| 14509 | export: None, | 14668 | export: None, |
| 14510 | cancelling: false, | 14669 | cancelling: false, |
| 14670 | + export_started: None, | ||
| 14671 | + export_marks: Vec::new(), | ||
| 14511 | export_path: out.clone(), | 14672 | export_path: out.clone(), |
| 14512 | project_path: project.clone(), | 14673 | project_path: project.clone(), |
| 14513 | keymap: keymap.clone(), | 14674 | keymap: keymap.clone(), |