|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+extern crate clap;
|
|
|
2
|
+extern crate time;
|
|
|
3
|
+extern crate task1;
|
|
|
4
|
+extern crate sys_info;
|
|
|
5
|
+
|
|
|
6
|
+use std::sync::mpsc::channel;
|
|
|
7
|
+use time::get_time;
|
|
|
8
|
+use clap::{Arg, App, SubCommand};
|
|
|
9
|
+use std::{process, thread};
|
|
|
10
|
+use task1::verify_product;
|
|
|
11
|
+use std::sync::Arc;
|
|
|
12
|
+
|
|
|
13
|
+pub fn main() {
|
|
|
14
|
+ let matches = create_app().get_matches();
|
|
|
15
|
+ let base = matches.value_of("base").unwrap_or("1");
|
|
|
16
|
+ let diff = Arc::new(matches.value_of("difficulty").unwrap_or("1").to_string());
|
|
|
17
|
+ let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
|
|
|
18
|
+ let threads = matches.value_of("threads").unwrap_or(&cpus);
|
|
|
19
|
+ let mut time_measurement = false;
|
|
|
20
|
+
|
|
|
21
|
+ if diff.chars().any(|c| !c.is_digit(16)) {
|
|
|
22
|
+ println!("Difficulty is not hexadecimal.");
|
|
|
23
|
+ process::exit(1)
|
|
|
24
|
+ }
|
|
|
25
|
+
|
|
|
26
|
+ if let Some(ref sub_command) = matches.subcommand {
|
|
|
27
|
+ if sub_command.name.eq("timings") {
|
|
|
28
|
+ time_measurement = true;
|
|
|
29
|
+ }
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ if true {
|
|
|
33
|
+ println!("--------------------------------------------");
|
|
|
34
|
+ println!(
|
|
|
35
|
+ "Container: : \"{}\"",
|
|
|
36
|
+ sys_info::hostname().unwrap_or("-".to_string())
|
|
|
37
|
+ );
|
|
|
38
|
+ println!("Physical CPUs : {}", sys_info::cpu_num().unwrap_or(0));
|
|
|
39
|
+ println!("Logical CPUs : {}", sys_info::cpu_num().unwrap_or(0));
|
|
|
40
|
+ println!("CPU Speed : {}", sys_info::cpu_speed().unwrap_or(0));
|
|
|
41
|
+ println!("Load Average : {:?}", sys_info::loadavg().unwrap());
|
|
|
42
|
+ println!("Processes : {}", sys_info::proc_total().unwrap_or(0));
|
|
|
43
|
+ println!("--------------------------------------------");
|
|
|
44
|
+ }
|
|
|
45
|
+
|
|
|
46
|
+ match (base.parse::<usize>(), threads.parse::<usize>()) {
|
|
|
47
|
+ (Ok(b), Ok(t)) => {
|
|
|
48
|
+ println!("Using base: {}", b);
|
|
|
49
|
+ println!("Using difficulty: {}", diff);
|
|
|
50
|
+ println!("Please wait...");
|
|
|
51
|
+ let start = get_time();
|
|
|
52
|
+ let max = <usize>::max_value();
|
|
|
53
|
+ let mut children = vec![];
|
|
|
54
|
+
|
|
|
55
|
+ let (tx, rx) = channel();
|
|
|
56
|
+
|
|
|
57
|
+ for i in 0..t {
|
|
|
58
|
+ let d = diff.clone();
|
|
|
59
|
+ let tx = tx.clone();
|
|
|
60
|
+
|
|
|
61
|
+ children.push(thread::spawn(move || {
|
|
|
62
|
+ let from = max / t * i;
|
|
|
63
|
+ let to = max / t * (i + 1);
|
|
|
64
|
+ println!("{} - {} | Thread {} | Difficulty is: {}", from, to, i, d);
|
|
|
65
|
+
|
|
|
66
|
+ for n in from..to {
|
|
|
67
|
+ if let Some(x) = verify_product(b, n, &d) {
|
|
|
68
|
+ let end = get_time();
|
|
|
69
|
+ println!("Number: {} --> hash: {}", x.number, x.hash);
|
|
|
70
|
+ if time_measurement {
|
|
|
71
|
+ let diff = end - start;
|
|
|
72
|
+ let s = diff.num_seconds();
|
|
|
73
|
+ let ms = diff.num_milliseconds();
|
|
|
74
|
+ let us = diff.num_microseconds().unwrap_or(ms * 1000);
|
|
|
75
|
+ println!("(Duration {}s / {}ms / {}us)", s, ms, us);
|
|
|
76
|
+ }
|
|
|
77
|
+ tx.send("Finished").unwrap();
|
|
|
78
|
+ }
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ }));
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ match rx.recv() {
|
|
|
85
|
+ Ok(msg) => {
|
|
|
86
|
+ println!("{}", msg);
|
|
|
87
|
+ }
|
|
|
88
|
+ Err(_) => {}
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ /*
|
|
|
92
|
+ for child in children {
|
|
|
93
|
+ let _ = child.join();
|
|
|
94
|
+ }
|
|
|
95
|
+ */
|
|
|
96
|
+ }
|
|
|
97
|
+ (_, Err(_)) => {
|
|
|
98
|
+ println!("Number of threads is not integer.");
|
|
|
99
|
+ process::exit(1)
|
|
|
100
|
+ }
|
|
|
101
|
+ (Err(_), _) => {
|
|
|
102
|
+ println!("Base is not integer.");
|
|
|
103
|
+ process::exit(1)
|
|
|
104
|
+ }
|
|
|
105
|
+ };
|
|
|
106
|
+}
|
|
|
107
|
+
|
|
|
108
|
+fn create_app<'a, 'b>() -> App<'a, 'b> {
|
|
|
109
|
+ App::new("Hash256")
|
|
|
110
|
+ .version("1.0")
|
|
|
111
|
+ .author("Lorenz Bung & Joshua Rutschmann")
|
|
|
112
|
+ .about(
|
|
|
113
|
+ "Calculates the Hashvalue of the given base, number and difficulty.",
|
|
|
114
|
+ )
|
|
|
115
|
+ .arg(
|
|
|
116
|
+ Arg::with_name("base")
|
|
|
117
|
+ .value_name("base")
|
|
|
118
|
+ .help("The base of the hash to be calculated on.")
|
|
|
119
|
+ .takes_value(true)
|
|
|
120
|
+ .required(true),
|
|
|
121
|
+ )
|
|
|
122
|
+ .arg(
|
|
|
123
|
+ Arg::with_name("difficulty")
|
|
|
124
|
+ .value_name("difficulty")
|
|
|
125
|
+ .help("The difficulty of the calculated hash.")
|
|
|
126
|
+ .takes_value(true)
|
|
|
127
|
+ .required(true),
|
|
|
128
|
+ )
|
|
|
129
|
+ .arg(
|
|
|
130
|
+ Arg::with_name("threads")
|
|
|
131
|
+ .value_name("threads")
|
|
|
132
|
+ .help(
|
|
|
133
|
+ "Sets the number of the threads to use (default = number of cpus)",
|
|
|
134
|
+ )
|
|
|
135
|
+ .takes_value(true)
|
|
|
136
|
+ .required(false),
|
|
|
137
|
+ )
|
|
|
138
|
+ .subcommand(
|
|
|
139
|
+ SubCommand::with_name("timings")
|
|
|
140
|
+ .about("controls timing features")
|
|
|
141
|
+ .version("1.0")
|
|
|
142
|
+ .author("Lorenz Bung & Joshua Rutschmann"),
|
|
|
143
|
+ )
|
|
|
144
|
+}
|