To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit b4f2ac1d authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Implement speed measurement

parent cec9fed7
......@@ -9,6 +9,7 @@ use std::io::BufReader;
use std::fs::{File};
use std::fmt::Display;
use std::sync::{Arc, Mutex};
use std::time::{Instant, Duration};
use ansi_term::Style;
use ansi_term::Colour::*;
......@@ -55,7 +56,7 @@ fn show_disk_status(disks: &Vec<Arc<DiskInformation>>) {
let longest = disks.iter().fold(0, |b, e| std::cmp::max(b, e.name.len())) +1;
for disk in disks {
println!("{:6}) {}:{:width$} {} -- {:?}",
println!("{:6}) {}:{:width$} {} -- {}",
disk.id,
BOLD.paint(&*disk.name),
" ",
......@@ -91,7 +92,7 @@ fn parse_disks() -> Vec<Arc<DiskInformation>> {
id: i,
name: name.into(),
size: KiloByte(no_blocks),
state: Mutex::new(Detected),
state: Mutex::new(Detected), // TODO erkenne wann eine Disk nicht überschrieben werden darf
}));
}
}
......@@ -132,7 +133,32 @@ struct DiskInformation {
state: Mutex<DiskState>,
}
#[derive(Debug,Eq,Ord)]
#[derive(Debug,PartialEq,Eq)]
enum DiskState {
Detected,
Protected,
QueueForWhipe,
Whiping{ progress: ByteUnit, per_second: ByteUnit },
Whiped,
WhipeFailed,
}
use DiskState::*;
impl Display for DiskState {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
&Detected => write!(f, "Detected"),
&Protected => write!(f, "Protected"),
&QueueForWhipe => write!(f, "Queued for Whipe"),
&Whiping { ref progress, ref per_second } => write!(f, "Whiping {} ({}/s)", progress, per_second),
&Whiped => write!(f, "Whiped successfully"),
&WhipeFailed=> write!(f, "Whipe failed"),
}
}
}
#[derive(Debug,Eq,Ord,Clone)]
enum ByteUnit {
Byte(u64),
KiloByte(u64),
......@@ -146,6 +172,21 @@ impl ByteUnit {
&KiloByte(v) => Byte(1024 * v),
}
}
pub fn byte_per_second(&self, periode: Duration) -> ByteUnit {
match self {
&Byte(v) => {
let secs = periode.as_secs();
Byte(if secs == 0 {
0
} else {
v / secs
})
},
something_else => something_else.to_byte().byte_per_second(periode),
}
}
}
impl std::cmp::PartialEq<Self> for ByteUnit {
......@@ -274,7 +315,7 @@ fn select_disk_to_whipe(disks: &Vec<Arc<DiskInformation>>) {
match read_one_line_from_stdin().trim().parse() {
Ok(id) => {
if let Some(disk) = disks.iter().find(|ref e| e.id == id) {
if let Some(disk) = disks.iter().find(|ref e| e.id == id && *e.state.lock().unwrap() == Detected) {
while {
println!("\nWhiping is {}!\nDestroy any data on {} ({})\nEnter {} to continue or {} to abort?", UNDERLINE.paint("non-reversable"), BOLD.paint(&*disk.name), disk.size, BOLD.paint("y"), BOLD.paint("a"));
......@@ -302,6 +343,7 @@ fn update_jobs(disks: &Vec<Arc<DiskInformation>>, pool: &threadpool::ThreadPool)
if *disk.state.lock().unwrap() == QueueForWhipe {
println!("Queued {} for whipe", BOLD.paint(&*disk.name));
let disk = disk.clone();
pool.execute(move || {
match whipe_disk(&disk) {
Ok(_) => {
......@@ -335,11 +377,21 @@ fn whipe_disk(disk: &Arc<DiskInformation>) -> std::io::Result<()> {
.write(true)
.open(&*format!("/dev/{}", disk.name))?;
let mut i: u64 = 0;
let now = Instant::now();
let mut last_position = Byte(0);
while position < disk.size {
rng.fill_bytes(&mut buf);
disk_file.write_all(&buf)?;
position += Byte(WRITE_BUFFER_SIZE as u64)
position += Byte(WRITE_BUFFER_SIZE as u64);
i += 1;
if i % 1024 == 0 {
let delta = position.clone() - last_position;
*disk.state.lock().unwrap() = Whiping{ progress: position.clone(), per_second: delta.byte_per_second(now.elapsed()) };
last_position = position.clone();
}
}
Ok( () )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment