diff --git a/V2/sleep/Cargo.lock b/V2/sleep/Cargo.lock new file mode 100644 index 0000000..c5f4c43 --- /dev/null +++ b/V2/sleep/Cargo.lock @@ -0,0 +1,61 @@ +[root] +name = "sleep" +version = "0.1.0" +dependencies = [ + "shuteye 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_syscall" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "shuteye" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135" +"checksum redox_syscall 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "29dbdfd4b9df8ab31dec47c6087b7b13cbf4a776f335e4de8efba8288dda075b" +"checksum shuteye 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2fae018891baadd06ff0281ff137ccd1641044d9bb9498b1138ffecf28f30172" +"checksum time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "211b63c112206356ef1ff9b19355f43740fc3f85960c598a93d3a3d3ba7beade" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/V2/sleep/Cargo.toml b/V2/sleep/Cargo.toml new file mode 100644 index 0000000..30c2dfa --- /dev/null +++ b/V2/sleep/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "sleep" +version = "0.1.0" +authors = ["Simon Wörner "] + +[dependencies] +# libc = ">=0.2.21" +shuteye = ">=0.2.0" +time = ">=0.1.36" diff --git a/V2/sleep/src/main.rs b/V2/sleep/src/main.rs new file mode 100644 index 0000000..10f8fef --- /dev/null +++ b/V2/sleep/src/main.rs @@ -0,0 +1,60 @@ +extern crate shuteye; +extern crate time; + +use std::time::Duration; +use shuteye::sleep; +use time::precise_time_ns; + +fn measure_duration(sleep_duration: Duration) -> Duration { + let start = precise_time_ns(); + sleep(sleep_duration); + let end = precise_time_ns(); + + let duration = end - start; + Duration::new(duration / 1_000_000_000, (duration % 1_000_000_000) as u32) +} + +fn measure_delay(sleep_duration: Duration) -> (Duration, Duration) { + let duration = measure_duration(sleep_duration); + (duration, duration - sleep_duration) +} + +fn measure_delay_loop(sleep_duration: Duration, count: u64) -> Vec<(Duration, Duration)> { + println!("Starting measurment with period {}s {}ns for {} loops", + sleep_duration.as_secs(), + sleep_duration.subsec_nanos(), + count); + let mut data: Vec<(Duration, Duration)> = Vec::with_capacity(count as usize); + + for _ in 0..count { + let (duration, delay) = measure_delay(sleep_duration); + data.push((duration, delay)); + println!("value: {} s {} ns\tdelay: {} s {} ns\trelativ delay = {:.2}%\traw_data: {:?}", + duration.as_secs(), + duration.subsec_nanos(), + delay.as_secs(), + delay.subsec_nanos(), + (delay.subsec_nanos() * 100) as f64 / duration.subsec_nanos() as f64, + delay); + } + + data +} + +fn main() { + println!("Hello, world!"); + + let data = measure_delay_loop(Duration::new(0, 10_000_000), 100); + println!("{:?}", data); + + /*let sleep_time = 5000; + println!("sleep_time = {}", sleep_time); + + let start = precise_time_ns(); + sleep(Duration::from_millis(5000)); + let end = precise_time_ns(); + let diff = end-start; + + println!("start = {}ms\nend = {}\ndiff = {}", start, end, diff); + println!("diff - sleep_time = {}", diff - sleep_time * 1000 * 1000);*/ +}