Brak opisu
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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #[macro_use]
  2. extern crate clap;
  3. extern crate time;
  4. extern crate task1;
  5. extern crate sys_info;
  6. use std::sync::mpsc::channel;
  7. use time::get_time;
  8. use clap::App;
  9. use std::{process, thread};
  10. use task1::verify_product;
  11. use std::sync::Arc;
  12. pub fn main() {
  13. let yaml = load_yaml!("cli.yml");
  14. let matches = App::from_yaml(yaml).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 sync = matches.is_present("sync");
  20. let wait = matches.is_present("wait");
  21. let verbosity = matches.occurrences_of("verbose");
  22. let mut time_measurement = false;
  23. // Falls irgendein Zeichen nicht hexadezimal ist, breche ab.
  24. if diff.chars().any(|c| !c.is_digit(16)) {
  25. println!("Difficulty is not hexadecimal.");
  26. process::exit(1)
  27. }
  28. // Falls das Unterkommando timings angegeben wurde aktiviere die Zeitmessung.
  29. if let Some(ref sub_command) = matches.subcommand {
  30. if sub_command.name.eq("timings") {
  31. time_measurement = true;
  32. }
  33. }
  34. if verbosity >= 1 {
  35. println!("--------------------------------------------");
  36. println!("Container: \"{}\"", sys_info::hostname().unwrap_or("-".to_string()));
  37. println!("Physical CPUs : {}", sys_info::cpu_num().unwrap_or(0));
  38. println!("Logical CPUs : {}", sys_info::cpu_num().unwrap_or(0));
  39. println!("CPU Speed : {}", sys_info::cpu_speed().unwrap_or(0));
  40. println!("Load Average : {:?}", sys_info::loadavg().unwrap());
  41. println!("Processes : {}", sys_info::proc_total().unwrap_or(0));
  42. println!("--------------------------------------------");
  43. }
  44. match (base.parse::<usize>(), threads.parse::<usize>()) {
  45. (Ok(b), Ok(t)) => {
  46. println!("Please wait...");
  47. if verbosity >= 1 {
  48. println!("Searching with {} threads", t);
  49. }
  50. let start = get_time();
  51. let max = <usize>::max_value();
  52. let mut children = vec![];
  53. let (tx, rx) = channel();
  54. let mut found = &false;
  55. for i in 0..t {
  56. let d = diff.clone();
  57. let tx = tx.clone();
  58. children.push(thread::spawn(move || {
  59. let mut n = i;
  60. while n < max {
  61. if n % (100000 + i) == 0 {
  62. println!("Thread {}: {}", i, n);
  63. }
  64. if *found && sync {
  65. return n;
  66. }
  67. if let Some(solution) = verify_product(b, n, &d) {
  68. *found = true;
  69. tx.send(solution).unwrap();
  70. if sync {
  71. break;
  72. }
  73. }
  74. n += t;
  75. }
  76. n
  77. }));
  78. }
  79. match rx.recv() {
  80. Ok(sol) => {
  81. let end = get_time();
  82. println!("Number: {} --> hash: {}", sol.number, sol.hash);
  83. if time_measurement {
  84. let diff = end - start;
  85. let s = diff.num_seconds();
  86. let ms = diff.num_milliseconds();
  87. let us = diff.num_microseconds().unwrap_or(ms * 1000);
  88. println!("(Duration {}s / {}ms / {}us)", s, ms, us);
  89. }
  90. }
  91. Err(_) => {}
  92. }
  93. if wait {
  94. for child in children {
  95. let loops = child.join();
  96. println!("Loops: {}", loops.unwrap());
  97. }
  98. }
  99. }
  100. (_, Err(_)) => {
  101. println!("Number of threads is not integer.");
  102. process::exit(1)
  103. }
  104. (Err(_), _) => {
  105. println!("Base is not integer.");
  106. process::exit(1)
  107. }
  108. };
  109. }