-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dd: use an alarm thread instead of elapsed() calls #4447
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf7b90b
dd: use an alarm thread instead of elapsed() calls
Freaky 52c93a4
dd: Simplify loop of progress Alarm thread
Freaky 546631c
dd: Tidy includes
Freaky 01a8623
dd: Add documentation to Alarm struct
Freaky e7557c2
Merge branch 'main' into dd-alarm-timer
sylvestre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,9 +36,12 @@ use std::os::unix::{ | |
io::{AsRawFd, FromRawFd}, | ||
}; | ||
use std::path::Path; | ||
use std::sync::mpsc; | ||
use std::sync::{ | ||
atomic::{AtomicBool, Ordering::Relaxed}, | ||
mpsc, Arc, | ||
}; | ||
use std::thread; | ||
use std::time; | ||
use std::time::{Duration, Instant}; | ||
|
||
use clap::{crate_version, Arg, Command}; | ||
use gcd::Gcd; | ||
|
@@ -75,6 +78,45 @@ struct Settings { | |
status: Option<StatusLevel>, | ||
} | ||
|
||
/// A timer which triggers on a given interval | ||
/// | ||
/// After being constructed with [`Alarm::with_interval`], [`Alarm::is_triggered`] | ||
/// will return true once per the given [`Duration`]. | ||
/// | ||
/// Can be cloned, but the trigger status is shared across all instances so only | ||
/// the first caller each interval will yield true. | ||
/// | ||
/// When all instances are dropped the background thread will exit on the next interval. | ||
#[derive(Debug, Clone)] | ||
pub struct Alarm { | ||
interval: Duration, | ||
trigger: Arc<AtomicBool>, | ||
} | ||
|
||
impl Alarm { | ||
pub fn with_interval(interval: Duration) -> Self { | ||
let trigger = Arc::new(AtomicBool::default()); | ||
|
||
let weak_trigger = Arc::downgrade(&trigger); | ||
thread::spawn(move || { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So now if I understand correctly, there are three threads: the main thread, this thread which has a periodic alarm, and the progress output thread. Is that right? |
||
while let Some(trigger) = weak_trigger.upgrade() { | ||
thread::sleep(interval); | ||
trigger.store(true, Relaxed); | ||
} | ||
}); | ||
|
||
Self { interval, trigger } | ||
} | ||
|
||
pub fn is_triggered(&self) -> bool { | ||
self.trigger.swap(false, Relaxed) | ||
} | ||
|
||
pub fn get_interval(&self) -> Duration { | ||
self.interval | ||
} | ||
} | ||
|
||
/// A number in blocks or bytes | ||
/// | ||
/// Some values (seek, skip, iseek, oseek) can have values either in blocks or in bytes. | ||
|
@@ -760,7 +802,7 @@ fn dd_copy(mut i: Input, mut o: Output) -> std::io::Result<()> { | |
// of its report includes the throughput in bytes per second, | ||
// which requires knowing how long the process has been | ||
// running. | ||
let start = time::Instant::now(); | ||
let start = Instant::now(); | ||
|
||
// A good buffer size for reading. | ||
// | ||
|
@@ -780,7 +822,6 @@ fn dd_copy(mut i: Input, mut o: Output) -> std::io::Result<()> { | |
// information. | ||
let (prog_tx, rx) = mpsc::channel(); | ||
let output_thread = thread::spawn(gen_prog_updater(rx, i.settings.status)); | ||
let mut progress_as_secs = 0; | ||
|
||
// Optimization: if no blocks are to be written, then don't | ||
// bother allocating any buffers. | ||
|
@@ -813,6 +854,12 @@ fn dd_copy(mut i: Input, mut o: Output) -> std::io::Result<()> { | |
// This is the max size needed. | ||
let mut buf = vec![BUF_INIT_BYTE; bsize]; | ||
|
||
// Spawn a timer thread to provide a scheduled signal indicating when we | ||
// should send an update of our progress to the reporting thread. | ||
// | ||
// This avoids the need to query the OS monotonic clock for every block. | ||
let alarm = Alarm::with_interval(Duration::from_secs(1)); | ||
|
||
// Index in the input file where we are reading bytes and in | ||
// the output file where we are writing bytes. | ||
// | ||
|
@@ -871,9 +918,8 @@ fn dd_copy(mut i: Input, mut o: Output) -> std::io::Result<()> { | |
// error. | ||
rstat += rstat_update; | ||
wstat += wstat_update; | ||
let prog_update = ProgUpdate::new(rstat, wstat, start.elapsed(), false); | ||
if prog_update.duration.as_secs() >= progress_as_secs { | ||
progress_as_secs = prog_update.duration.as_secs() + 1; | ||
if alarm.is_triggered() { | ||
let prog_update = ProgUpdate::new(rstat, wstat, start.elapsed(), false); | ||
prog_tx.send(prog_update).unwrap_or(()); | ||
} | ||
} | ||
|
@@ -885,7 +931,7 @@ fn finalize<T>( | |
output: &mut Output, | ||
rstat: ReadStat, | ||
wstat: WriteStat, | ||
start: time::Instant, | ||
start: Instant, | ||
prog_tx: &mpsc::Sender<ProgUpdate>, | ||
output_thread: thread::JoinHandle<T>, | ||
) -> std::io::Result<()> { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice if you could add some documentations / comment here :)