Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.rs 4.7KB

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