1pub const FILES_IN_FLIGHT_HELP: &str = "Maximum concurrent file-like operations. Local automatic defaults resolve from available CPU parallelism on this host, with a floor of 4. Use a positive integer for a finite ceiling or 'unlimited' to remove the user ceiling; lower internal safety ceilings may still apply. This applies to file copy, link, comparison, removal, chmod, and generation work.";
16
17#[derive(Debug, Clone, clap::Args)]
18pub struct CommonArgs {
19 #[arg(long, help_heading = "Progress & output")]
22 pub progress: bool,
23 #[arg(long, value_name = "TYPE", help_heading = "Progress & output")]
27 pub progress_type: Option<crate::ProgressType>,
28 #[arg(long, value_name = "DELAY", help_heading = "Progress & output")]
34 pub progress_delay: Option<String>,
35 #[arg(short = 'v', long = "verbose", action = clap::ArgAction::Count, help_heading = "Progress & output")]
37 pub verbose: u8,
38 #[arg(long, value_name = "PREFIX", help_heading = "Profiling")]
44 pub timings: Option<String>,
45 #[arg(long, help_heading = "Profiling")]
47 pub timings_detail: bool,
48 #[arg(
51 long,
52 default_value = "0",
53 value_name = "N",
54 help_heading = "Performance & throttling"
55 )]
56 pub ops_throttle: usize,
57 #[arg(
61 long,
62 default_value = "0",
63 value_name = "N",
64 help_heading = "Performance & throttling"
65 )]
66 pub iops_throttle: usize,
67 #[arg(long, value_name = "N|unlimited", value_parser = parse_files_in_flight_limit,
69 help = FILES_IN_FLIGHT_HELP,
70 help_heading = "Performance & throttling",
71 conflicts_with = "max_open_files")]
72 pub max_files_in_flight: Option<crate::ConcurrencyLimit>,
73 #[arg(
74 long,
75 value_name = "N",
76 hide = true,
77 conflicts_with = "max_files_in_flight"
78 )]
79 pub max_open_files: Option<usize>,
80 #[arg(
83 long,
84 default_value = "0",
85 value_name = "N",
86 help_heading = "Advanced settings"
87 )]
88 pub max_workers: usize,
89 #[arg(
91 long,
92 default_value = "0",
93 value_name = "N",
94 help_heading = "Advanced settings"
95 )]
96 pub max_blocking_threads: usize,
97 #[arg(long, help_heading = "Congestion control")]
100 pub auto_meta_throttle: bool,
101 #[arg(
103 long,
104 default_value = "1",
105 value_name = "N",
106 help_heading = "Congestion control"
107 )]
108 pub auto_meta_initial_cwnd: u32,
109 #[arg(
111 long,
112 default_value = "1",
113 value_name = "N",
114 help_heading = "Congestion control (advanced)"
115 )]
116 pub auto_meta_min_cwnd: u32,
117 #[arg(
119 long,
120 default_value = "4096",
121 value_name = "N",
122 help_heading = "Congestion control"
123 )]
124 pub auto_meta_max_cwnd: u32,
125 #[arg(
135 long,
136 default_value = "1.3",
137 value_name = "F",
138 help_heading = "Congestion control (advanced)"
139 )]
140 pub auto_meta_alpha: f64,
141 #[arg(
145 long,
146 default_value = "1.8",
147 value_name = "F",
148 help_heading = "Congestion control (advanced)"
149 )]
150 pub auto_meta_beta: f64,
151 #[arg(
159 long,
160 default_value = "0.1",
161 value_name = "F",
162 help_heading = "Congestion control (advanced)"
163 )]
164 pub auto_meta_baseline_percentile: f64,
165 #[arg(
169 long,
170 default_value = "0.5",
171 value_name = "F",
172 help_heading = "Congestion control (advanced)"
173 )]
174 pub auto_meta_current_percentile: f64,
175 #[arg(
177 long,
178 default_value = "1",
179 value_name = "N",
180 help_heading = "Congestion control (advanced)"
181 )]
182 pub auto_meta_increase_step: u32,
183 #[arg(
185 long,
186 default_value = "1",
187 value_name = "N",
188 help_heading = "Congestion control (advanced)"
189 )]
190 pub auto_meta_decrease_step: u32,
191 #[arg(
194 long,
195 default_value = "10s",
196 value_name = "DUR",
197 help_heading = "Congestion control (advanced)"
198 )]
199 pub auto_meta_long_window: humantime::Duration,
200 #[arg(
203 long,
204 default_value = "1s",
205 value_name = "DUR",
206 help_heading = "Congestion control (advanced)"
207 )]
208 pub auto_meta_short_window: humantime::Duration,
209 #[arg(
211 long,
212 default_value = "50ms",
213 value_name = "DUR",
214 help_heading = "Congestion control (advanced)"
215 )]
216 pub auto_meta_tick_interval: humantime::Duration,
217 #[arg(long, help_heading = "Congestion control")]
221 pub auto_meta_histogram: bool,
222 #[arg(long, value_name = "PATH", help_heading = "Congestion control")]
228 pub auto_meta_histogram_log: Option<std::path::PathBuf>,
229 #[arg(
233 long,
234 default_value = "1s",
235 value_name = "DUR",
236 help_heading = "Congestion control"
237 )]
238 pub auto_meta_histogram_interval: humantime::Duration,
239}
240
241impl CommonArgs {
242 #[must_use]
244 pub fn tracing_config(&self, identifier: &str) -> crate::TracingConfig {
245 crate::TracingConfig {
246 timings_prefix: self.timings.clone(),
247 timings_detail: self.timings_detail,
248 ..crate::TracingConfig::local(identifier)
249 }
250 }
251 #[must_use]
252 pub fn resolve_files_in_flight(&self) -> crate::ResolvedFilesInFlight {
253 match (self.max_files_in_flight, self.max_open_files) {
254 (Some(value), None) => crate::ResolvedFilesInFlight::explicit_limit(value),
255 (None, Some(value)) => crate::ResolvedFilesInFlight::legacy(value),
256 (None, None) => crate::ResolvedFilesInFlight::automatic(),
257 (Some(_), Some(_)) => unreachable!("clap rejects conflicting file limits"),
258 }
259 }
260 #[must_use]
264 pub fn output_config(&self, quiet: bool, print_summary: bool) -> crate::OutputConfig {
265 crate::OutputConfig {
266 quiet,
267 verbose: self.verbose,
268 print_summary,
269 ..Default::default()
270 }
271 }
272 #[must_use]
274 pub fn runtime_config(&self) -> crate::RuntimeConfig {
275 crate::RuntimeConfig {
276 max_workers: self.max_workers,
277 max_blocking_threads: self.max_blocking_threads,
278 }
279 }
280 #[must_use]
282 pub fn throttle_config(
283 &self,
284 files_in_flight: crate::ResolvedFilesInFlight,
285 chunk_size: u64,
286 ) -> crate::ThrottleConfig {
287 let auto_meta_implied = self.auto_meta_throttle
288 || self.auto_meta_histogram
289 || self.auto_meta_histogram_log.is_some();
290 let auto_meta = auto_meta_implied.then(|| crate::AutoMetaThrottleConfig {
291 initial_cwnd: self.auto_meta_initial_cwnd,
292 min_cwnd: self.auto_meta_min_cwnd,
293 max_cwnd: self.auto_meta_max_cwnd,
294 alpha: self.auto_meta_alpha,
295 beta: self.auto_meta_beta,
296 increase_step: self.auto_meta_increase_step,
297 decrease_step: self.auto_meta_decrease_step,
298 baseline_percentile: self.auto_meta_baseline_percentile,
299 current_percentile: self.auto_meta_current_percentile,
300 long_window: self.auto_meta_long_window.into(),
301 short_window: self.auto_meta_short_window.into(),
302 tick_interval: self.auto_meta_tick_interval.into(),
303 });
304 crate::ThrottleConfig {
305 files_in_flight,
306 apply_files_in_flight: true,
307 admission_policy: crate::AdmissionPolicy::Local,
308 ops_throttle: self.ops_throttle,
309 iops_throttle: self.iops_throttle,
310 chunk_size,
311 auto_meta,
312 histogram_enabled: self.auto_meta_histogram || self.auto_meta_histogram_log.is_some(),
313 histogram_log_path: self.auto_meta_histogram_log.clone(),
314 histogram_interval: self.auto_meta_histogram_interval.into(),
315 }
316 }
317 #[must_use]
325 pub fn progress_requested(&self) -> bool {
326 self.progress
327 || self.progress_type.is_some()
328 || self.progress_delay.is_some()
329 || self.auto_meta_histogram
330 }
331
332 #[must_use]
337 pub fn user_progress_settings(
338 &self,
339 kind: crate::progress::LocalProgressKind,
340 ) -> Option<crate::ProgressSettings> {
341 if !self.progress_requested() {
342 return None;
343 }
344 Some(crate::ProgressSettings {
345 progress_type: crate::GeneralProgressType::User {
346 progress_type: self.progress_type.unwrap_or_default(),
347 kind,
348 },
349 progress_delay: self.progress_delay.clone(),
350 })
351 }
352}
353
354pub fn parse_positive_usize(value: &str) -> Result<std::num::NonZeroUsize, String> {
355 let value = value.parse::<usize>().map_err(|error| error.to_string())?;
356 std::num::NonZeroUsize::new(value).ok_or_else(|| "value must be at least 1".to_string())
357}
358
359pub fn parse_files_in_flight_limit(value: &str) -> Result<crate::ConcurrencyLimit, String> {
360 if value == "unlimited" {
361 return Ok(crate::ConcurrencyLimit::Unlimited);
362 }
363 parse_positive_usize(value).map(crate::ConcurrencyLimit::Limited)
364}
365
366pub fn parse_positive_u64(value: &str) -> Result<u64, String> {
367 let value = value.parse::<u64>().map_err(|error| error.to_string())?;
368 (value > 0)
369 .then_some(value)
370 .ok_or_else(|| "value must be at least 1".to_string())
371}
372
373#[cfg(test)]
374mod implies_tests {
375 use super::*;
376 use clap::Parser;
377 use std::num::NonZeroUsize;
378
379 #[derive(Debug, Parser)]
380 struct TestCli {
381 #[command(flatten)]
382 common: CommonArgs,
383 }
384
385 #[test]
386 fn max_files_in_flight_resolves_cli_sources() {
387 let explicit = TestCli::try_parse_from(["test", "--max-files-in-flight=1"])
388 .expect("positive max-files-in-flight must parse")
389 .common
390 .resolve_files_in_flight();
391 assert_eq!(
392 explicit,
393 crate::ResolvedFilesInFlight::explicit(NonZeroUsize::new(1).unwrap())
394 );
395 let explicit_unlimited =
396 TestCli::try_parse_from(["test", "--max-files-in-flight=unlimited"])
397 .expect("explicit unlimited max-files-in-flight must parse")
398 .common
399 .resolve_files_in_flight();
400 assert_eq!(
401 explicit_unlimited,
402 crate::ResolvedFilesInFlight::unlimited()
403 );
404 let automatic = TestCli::try_parse_from(["test"])
405 .expect("omitted max-files-in-flight must parse")
406 .common
407 .resolve_files_in_flight();
408 assert_eq!(automatic.source(), crate::FilesInFlightSource::Automatic);
409 let legacy_zero = TestCli::try_parse_from(["test", "--max-open-files=0"])
410 .expect("legacy zero must parse")
411 .common
412 .resolve_files_in_flight();
413 assert_eq!(legacy_zero.limit(), crate::ConcurrencyLimit::Unlimited);
414 assert_eq!(
415 legacy_zero.source(),
416 crate::FilesInFlightSource::DeprecatedMaxOpenFiles
417 );
418 let legacy_positive = TestCli::try_parse_from(["test", "--max-open-files=7"])
419 .expect("positive legacy value must parse")
420 .common
421 .resolve_files_in_flight();
422 assert_eq!(legacy_positive, crate::ResolvedFilesInFlight::legacy(7));
423 }
424
425 #[test]
426 fn max_files_in_flight_rejects_invalid_and_conflicting_values() {
427 let zero = TestCli::try_parse_from(["test", "--max-files-in-flight=0"])
428 .expect_err("zero max-files-in-flight must be rejected");
429 assert!(zero.to_string().contains("value must be at least 1"));
430 assert!(TestCli::try_parse_from(["test", "--max-files-in-flight=nope"]).is_err());
431 assert!(
432 TestCli::try_parse_from(["test", "--max-files-in-flight=1", "--max-open-files=1",])
433 .is_err()
434 );
435 }
436
437 #[test]
438 fn max_files_in_flight_help_stays_local_to_shared_tools() {
439 let help = TestCli::try_parse_from(["test", "--help"])
440 .expect_err("help must stop clap parsing")
441 .to_string();
442 assert!(help.contains(
443 "Local automatic defaults resolve from available CPU parallelism on this host"
444 ));
445 assert!(
446 !help.contains("source rcpd"),
447 "shared CLI help must not describe remote negotiation: {help}"
448 );
449 }
450
451 #[test]
452 fn long_help_only_advertises_max_files_in_flight() {
453 let help = TestCli::try_parse_from(["test", "--help"])
454 .expect_err("help must stop clap parsing")
455 .to_string();
456 assert!(help.contains("--max-files-in-flight"));
457 assert!(!help.contains("--max-open-files"));
458 }
459
460 #[test]
461 fn auto_meta_histogram_implies_throttle_at_cli() {
462 let cli = TestCli::parse_from(["test", "--auto-meta-histogram"]);
463 let throttle = cli
464 .common
465 .throttle_config(cli.common.resolve_files_in_flight(), 0);
466 assert!(
467 throttle.auto_meta.is_some(),
468 "histogram flag must imply auto_meta"
469 );
470 assert!(throttle.histogram_enabled);
471 }
472
473 #[test]
474 fn auto_meta_histogram_log_implies_throttle_at_cli() {
475 let cli = TestCli::parse_from(["test", "--auto-meta-histogram-log", "/tmp/x.hdr"]);
476 let throttle = cli
477 .common
478 .throttle_config(cli.common.resolve_files_in_flight(), 0);
479 assert!(
480 throttle.auto_meta.is_some(),
481 "histogram-log flag must imply auto_meta"
482 );
483 assert!(throttle.histogram_log_path.is_some());
484 }
485
486 #[test]
487 fn no_auto_meta_flags_means_no_throttle() {
488 let cli = TestCli::parse_from(["test"]);
489 let throttle = cli
490 .common
491 .throttle_config(cli.common.resolve_files_in_flight(), 0);
492 assert!(throttle.auto_meta.is_none());
493 assert!(!throttle.histogram_enabled);
494 }
495
496 #[test]
505 fn panel_flag_does_not_set_explicit_throttle_field() {
506 let cli = TestCli::parse_from(["test", "--auto-meta-histogram"]);
507 assert!(
510 cli.common
511 .throttle_config(cli.common.resolve_files_in_flight(), 0)
512 .auto_meta
513 .is_some()
514 );
515 assert!(
518 !cli.common.auto_meta_throttle,
519 "--auto-meta-histogram must not set auto_meta_throttle"
520 );
521 assert!(cli.common.auto_meta_histogram_log.is_none());
523 }
524
525 #[test]
526 fn auto_meta_histogram_implies_progress() {
527 let cli = TestCli::parse_from(["test", "--auto-meta-histogram"]);
528 assert!(
529 cli.common.progress_requested(),
530 "--auto-meta-histogram alone must imply progress so the panel actually renders",
531 );
532 }
533
534 #[test]
535 fn auto_meta_histogram_log_does_not_imply_progress() {
536 let cli = TestCli::parse_from(["test", "--auto-meta-histogram-log", "/tmp/x.hdr"]);
539 assert!(
540 !cli.common.progress_requested(),
541 "--auto-meta-histogram-log alone should NOT imply progress",
542 );
543 }
544}