driver-cpu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2011-2012 Luke Dashjr
  4. * Copyright 2010 Jeff Garzik
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <signal.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #ifndef WIN32
  22. #include <sys/wait.h>
  23. #include <sys/resource.h>
  24. #endif
  25. #include <libgen.h>
  26. #include "compat.h"
  27. #include "miner.h"
  28. #include "bench_block.h"
  29. #include "driver-cpu.h"
  30. #if defined(unix)
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #endif
  34. #if defined(__linux) && defined(cpu_set_t) /* Linux specific policy and affinity management */
  35. #include <sched.h>
  36. static inline void drop_policy(void)
  37. {
  38. struct sched_param param;
  39. #ifdef SCHED_BATCH
  40. #ifdef SCHED_IDLE
  41. if (unlikely(sched_setscheduler(0, SCHED_IDLE, &param) == -1))
  42. #endif
  43. sched_setscheduler(0, SCHED_BATCH, &param);
  44. #endif
  45. }
  46. static inline void affine_to_cpu(int id, int cpu)
  47. {
  48. cpu_set_t set;
  49. CPU_ZERO(&set);
  50. CPU_SET(cpu, &set);
  51. sched_setaffinity(0, sizeof(&set), &set);
  52. applog(LOG_INFO, "Binding cpu mining thread %d to cpu %d", id, cpu);
  53. }
  54. #else
  55. static inline void drop_policy(void)
  56. {
  57. }
  58. static inline void affine_to_cpu(int id, int cpu)
  59. {
  60. }
  61. #endif
  62. /* TODO: resolve externals */
  63. extern bool submit_work_sync(struct thr_info *thr, const struct work *work_in, struct timeval *tv);
  64. extern char *set_int_range(const char *arg, int *i, int min, int max);
  65. extern int dev_from_id(int thr_id);
  66. /* chipset-optimized hash functions */
  67. extern bool ScanHash_4WaySSE2(struct thr_info*, const unsigned char *pmidstate,
  68. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  69. const unsigned char *ptarget,
  70. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  71. extern bool ScanHash_altivec_4way(struct thr_info*, const unsigned char *pmidstate,
  72. unsigned char *pdata,
  73. unsigned char *phash1, unsigned char *phash,
  74. const unsigned char *ptarget,
  75. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  76. extern bool scanhash_via(struct thr_info*, const unsigned char *pmidstate,
  77. unsigned char *pdata,
  78. unsigned char *phash1, unsigned char *phash,
  79. const unsigned char *target,
  80. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  81. extern bool scanhash_c(struct thr_info*, const unsigned char *midstate, unsigned char *data,
  82. unsigned char *hash1, unsigned char *hash,
  83. const unsigned char *target,
  84. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  85. extern bool scanhash_cryptopp(struct thr_info*, const unsigned char *midstate,unsigned char *data,
  86. unsigned char *hash1, unsigned char *hash,
  87. const unsigned char *target,
  88. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  89. extern bool scanhash_asm32(struct thr_info*, const unsigned char *midstate,unsigned char *data,
  90. unsigned char *hash1, unsigned char *hash,
  91. const unsigned char *target,
  92. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  93. extern bool scanhash_sse2_64(struct thr_info*, const unsigned char *pmidstate, unsigned char *pdata,
  94. unsigned char *phash1, unsigned char *phash,
  95. const unsigned char *ptarget,
  96. uint32_t max_nonce, uint32_t *last_nonce,
  97. uint32_t nonce);
  98. extern bool scanhash_sse4_64(struct thr_info*, const unsigned char *pmidstate, unsigned char *pdata,
  99. unsigned char *phash1, unsigned char *phash,
  100. const unsigned char *ptarget,
  101. uint32_t max_nonce, uint32_t *last_nonce,
  102. uint32_t nonce);
  103. extern bool scanhash_sse2_32(struct thr_info*, const unsigned char *pmidstate, unsigned char *pdata,
  104. unsigned char *phash1, unsigned char *phash,
  105. const unsigned char *ptarget,
  106. uint32_t max_nonce, uint32_t *last_nonce,
  107. uint32_t nonce);
  108. extern bool scanhash_scrypt(struct thr_info *thr, int thr_id, unsigned char *pdata, unsigned char *scratchbuf,
  109. const unsigned char *ptarget,
  110. uint32_t max_nonce, unsigned long *hashes_done);
  111. #ifdef WANT_CPUMINE
  112. static size_t max_name_len = 0;
  113. static char *name_spaces_pad = NULL;
  114. const char *algo_names[] = {
  115. [ALGO_C] = "c",
  116. #ifdef WANT_SSE2_4WAY
  117. [ALGO_4WAY] = "4way",
  118. #endif
  119. #ifdef WANT_VIA_PADLOCK
  120. [ALGO_VIA] = "via",
  121. #endif
  122. [ALGO_CRYPTOPP] = "cryptopp",
  123. #ifdef WANT_CRYPTOPP_ASM32
  124. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  125. #endif
  126. #ifdef WANT_X8632_SSE2
  127. [ALGO_SSE2_32] = "sse2_32",
  128. #endif
  129. #ifdef WANT_X8664_SSE2
  130. [ALGO_SSE2_64] = "sse2_64",
  131. #endif
  132. #ifdef WANT_X8664_SSE4
  133. [ALGO_SSE4_64] = "sse4_64",
  134. #endif
  135. #ifdef WANT_ALTIVEC_4WAY
  136. [ALGO_ALTIVEC_4WAY] = "altivec_4way",
  137. #endif
  138. #ifdef WANT_SCRYPT
  139. [ALGO_SCRYPT] = "scrypt",
  140. #endif
  141. };
  142. static const sha256_func sha256_funcs[] = {
  143. [ALGO_C] = (sha256_func)scanhash_c,
  144. #ifdef WANT_SSE2_4WAY
  145. [ALGO_4WAY] = (sha256_func)ScanHash_4WaySSE2,
  146. #endif
  147. #ifdef WANT_ALTIVEC_4WAY
  148. [ALGO_ALTIVEC_4WAY] = (sha256_func) ScanHash_altivec_4way,
  149. #endif
  150. #ifdef WANT_VIA_PADLOCK
  151. [ALGO_VIA] = (sha256_func)scanhash_via,
  152. #endif
  153. [ALGO_CRYPTOPP] = (sha256_func)scanhash_cryptopp,
  154. #ifdef WANT_CRYPTOPP_ASM32
  155. [ALGO_CRYPTOPP_ASM32] = (sha256_func)scanhash_asm32,
  156. #endif
  157. #ifdef WANT_X8632_SSE2
  158. [ALGO_SSE2_32] = (sha256_func)scanhash_sse2_32,
  159. #endif
  160. #ifdef WANT_X8664_SSE2
  161. [ALGO_SSE2_64] = (sha256_func)scanhash_sse2_64,
  162. #endif
  163. #ifdef WANT_X8664_SSE4
  164. [ALGO_SSE4_64] = (sha256_func)scanhash_sse4_64,
  165. #endif
  166. #ifdef WANT_SCRYPT
  167. [ALGO_SCRYPT] = (sha256_func)scanhash_scrypt
  168. #endif
  169. };
  170. #endif
  171. #ifdef WANT_CPUMINE
  172. #if defined(WANT_X8664_SSE2) && defined(__SSE2__)
  173. enum sha256_algos opt_algo = ALGO_SSE2_64;
  174. #elif defined(WANT_X8632_SSE2) && defined(__SSE2__)
  175. enum sha256_algos opt_algo = ALGO_SSE2_32;
  176. #else
  177. enum sha256_algos opt_algo = ALGO_C;
  178. #endif
  179. bool opt_usecpu = false;
  180. static int cpur_thr_id;
  181. static bool forced_n_threads;
  182. #endif
  183. #ifdef WANT_CPUMINE
  184. // Algo benchmark, crash-prone, system independent stage
  185. double bench_algo_stage3(
  186. enum sha256_algos algo
  187. )
  188. {
  189. // Use a random work block pulled from a pool
  190. static uint8_t bench_block[] = { CGMINER_BENCHMARK_BLOCK };
  191. struct work work __attribute__((aligned(128)));
  192. unsigned char hash1[64];
  193. size_t bench_size = sizeof(work);
  194. size_t work_size = sizeof(bench_block);
  195. size_t min_size = (work_size < bench_size ? work_size : bench_size);
  196. memset(&work, 0, sizeof(work));
  197. memcpy(&work, &bench_block, min_size);
  198. struct thr_info dummy = {0};
  199. struct timeval end;
  200. struct timeval start;
  201. uint32_t max_nonce = (1<<22);
  202. uint32_t last_nonce = 0;
  203. hex2bin(hash1, "00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000", 64);
  204. gettimeofday(&start, 0);
  205. {
  206. sha256_func func = sha256_funcs[algo];
  207. (*func)(
  208. &dummy,
  209. work.midstate,
  210. work.data,
  211. hash1,
  212. work.hash,
  213. work.target,
  214. max_nonce,
  215. &last_nonce,
  216. work.blk.nonce
  217. );
  218. }
  219. gettimeofday(&end, 0);
  220. uint64_t usec_end = ((uint64_t)end.tv_sec)*1000*1000 + end.tv_usec;
  221. uint64_t usec_start = ((uint64_t)start.tv_sec)*1000*1000 + start.tv_usec;
  222. uint64_t usec_elapsed = usec_end - usec_start;
  223. double rate = -1.0;
  224. if (0<usec_elapsed) {
  225. rate = (1.0*(last_nonce+1))/usec_elapsed;
  226. }
  227. return rate;
  228. }
  229. #if defined(unix)
  230. // Change non-blocking status on a file descriptor
  231. static void set_non_blocking(
  232. int fd,
  233. int yes
  234. )
  235. {
  236. int flags = fcntl(fd, F_GETFL, 0);
  237. if (flags<0) {
  238. perror("fcntl(GET) failed");
  239. exit(1);
  240. }
  241. flags = yes ? (flags|O_NONBLOCK) : (flags&~O_NONBLOCK);
  242. int r = fcntl(fd, F_SETFL, flags);
  243. if (r<0) {
  244. perror("fcntl(SET) failed");
  245. exit(1);
  246. }
  247. }
  248. #endif // defined(unix)
  249. // Algo benchmark, crash-safe, system-dependent stage
  250. static double bench_algo_stage2(
  251. enum sha256_algos algo
  252. )
  253. {
  254. // Here, the gig is to safely run a piece of code that potentially
  255. // crashes. Unfortunately, the Right Way (tm) to do this is rather
  256. // heavily platform dependent :(
  257. double rate = -1.23457;
  258. #if defined(unix)
  259. // Make a pipe: [readFD, writeFD]
  260. int pfd[2];
  261. int r = pipe(pfd);
  262. if (r<0) {
  263. perror("pipe - failed to create pipe for --algo auto");
  264. exit(1);
  265. }
  266. // Make pipe non blocking
  267. set_non_blocking(pfd[0], 1);
  268. set_non_blocking(pfd[1], 1);
  269. // Don't allow a crashing child to kill the main process
  270. sighandler_t sr0 = signal(SIGPIPE, SIG_IGN);
  271. sighandler_t sr1 = signal(SIGPIPE, SIG_IGN);
  272. if (SIG_ERR==sr0 || SIG_ERR==sr1) {
  273. perror("signal - failed to edit signal mask for --algo auto");
  274. exit(1);
  275. }
  276. // Fork a child to do the actual benchmarking
  277. pid_t child_pid = fork();
  278. if (child_pid<0) {
  279. perror("fork - failed to create a child process for --algo auto");
  280. exit(1);
  281. }
  282. // Do the dangerous work in the child, knowing we might crash
  283. if (0==child_pid) {
  284. // TODO: some umask trickery to prevent coredumps
  285. // Benchmark this algorithm
  286. double r = bench_algo_stage3(algo);
  287. // We survived, send result to parent and bail
  288. int loop_count = 0;
  289. while (1) {
  290. ssize_t bytes_written = write(pfd[1], &r, sizeof(r));
  291. int try_again = (0==bytes_written || (bytes_written<0 && EAGAIN==errno));
  292. int success = (sizeof(r)==(size_t)bytes_written);
  293. if (success)
  294. break;
  295. if (!try_again) {
  296. perror("write - child failed to write benchmark result to pipe");
  297. exit(1);
  298. }
  299. if (5<loop_count) {
  300. applog(LOG_ERR, "child tried %d times to communicate with parent, giving up", loop_count);
  301. exit(1);
  302. }
  303. ++loop_count;
  304. sleep(1);
  305. }
  306. exit(0);
  307. }
  308. // Parent waits for a result from child
  309. int loop_count = 0;
  310. while (1) {
  311. // Wait for child to die
  312. int status;
  313. int r = waitpid(child_pid, &status, WNOHANG);
  314. if ((child_pid==r) || (r<0 && ECHILD==errno)) {
  315. // Child died somehow. Grab result and bail
  316. double tmp;
  317. ssize_t bytes_read = read(pfd[0], &tmp, sizeof(tmp));
  318. if (sizeof(tmp)==(size_t)bytes_read)
  319. rate = tmp;
  320. break;
  321. } else if (r<0) {
  322. perror("bench_algo: waitpid failed. giving up.");
  323. exit(1);
  324. }
  325. // Give up on child after a ~60s
  326. if (60<loop_count) {
  327. kill(child_pid, SIGKILL);
  328. waitpid(child_pid, &status, 0);
  329. break;
  330. }
  331. // Wait a bit longer
  332. ++loop_count;
  333. sleep(1);
  334. }
  335. // Close pipe
  336. r = close(pfd[0]);
  337. if (r<0) {
  338. perror("close - failed to close read end of pipe for --algo auto");
  339. exit(1);
  340. }
  341. r = close(pfd[1]);
  342. if (r<0) {
  343. perror("close - failed to close read end of pipe for --algo auto");
  344. exit(1);
  345. }
  346. #elif defined(WIN32)
  347. // Get handle to current exe
  348. HINSTANCE module = GetModuleHandle(0);
  349. if (!module) {
  350. applog(LOG_ERR, "failed to retrieve module handle");
  351. exit(1);
  352. }
  353. // Create a unique name
  354. char unique_name[32];
  355. snprintf(
  356. unique_name,
  357. sizeof(unique_name)-1,
  358. "cgminer-%p",
  359. (void*)module
  360. );
  361. // Create and init a chunked of shared memory
  362. HANDLE map_handle = CreateFileMapping(
  363. INVALID_HANDLE_VALUE, // use paging file
  364. NULL, // default security attributes
  365. PAGE_READWRITE, // read/write access
  366. 0, // size: high 32-bits
  367. 4096, // size: low 32-bits
  368. unique_name // name of map object
  369. );
  370. if (NULL==map_handle) {
  371. applog(LOG_ERR, "could not create shared memory");
  372. exit(1);
  373. }
  374. void *shared_mem = MapViewOfFile(
  375. map_handle, // object to map view of
  376. FILE_MAP_WRITE, // read/write access
  377. 0, // high offset: map from
  378. 0, // low offset: beginning
  379. 0 // default: map entire file
  380. );
  381. if (NULL==shared_mem) {
  382. applog(LOG_ERR, "could not map shared memory");
  383. exit(1);
  384. }
  385. SetEnvironmentVariable("CGMINER_SHARED_MEM", unique_name);
  386. CopyMemory(shared_mem, &rate, sizeof(rate));
  387. // Get path to current exe
  388. char cmd_line[256 + MAX_PATH];
  389. const size_t n = sizeof(cmd_line)-200;
  390. DWORD size = GetModuleFileName(module, cmd_line, n);
  391. if (0==size) {
  392. applog(LOG_ERR, "failed to retrieve module path");
  393. exit(1);
  394. }
  395. // Construct new command line based on that
  396. char *p = strlen(cmd_line) + cmd_line;
  397. sprintf(p, " --bench-algo %d", algo);
  398. SetEnvironmentVariable("CGMINER_BENCH_ALGO", "1");
  399. // Launch a debug copy of cgminer
  400. STARTUPINFO startup_info;
  401. PROCESS_INFORMATION process_info;
  402. ZeroMemory(&startup_info, sizeof(startup_info));
  403. ZeroMemory(&process_info, sizeof(process_info));
  404. startup_info.cb = sizeof(startup_info);
  405. BOOL ok = CreateProcess(
  406. NULL, // No module name (use command line)
  407. cmd_line, // Command line
  408. NULL, // Process handle not inheritable
  409. NULL, // Thread handle not inheritable
  410. FALSE, // Set handle inheritance to FALSE
  411. DEBUG_ONLY_THIS_PROCESS,// We're going to debug the child
  412. NULL, // Use parent's environment block
  413. NULL, // Use parent's starting directory
  414. &startup_info, // Pointer to STARTUPINFO structure
  415. &process_info // Pointer to PROCESS_INFORMATION structure
  416. );
  417. if (!ok) {
  418. applog(LOG_ERR, "CreateProcess failed with error %d\n", GetLastError() );
  419. exit(1);
  420. }
  421. // Debug the child (only clean way to catch exceptions)
  422. while (1) {
  423. // Wait for child to do something
  424. DEBUG_EVENT debug_event;
  425. ZeroMemory(&debug_event, sizeof(debug_event));
  426. BOOL ok = WaitForDebugEvent(&debug_event, 60 * 1000);
  427. if (!ok)
  428. break;
  429. // Decide if event is "normal"
  430. int go_on =
  431. CREATE_PROCESS_DEBUG_EVENT== debug_event.dwDebugEventCode ||
  432. CREATE_THREAD_DEBUG_EVENT == debug_event.dwDebugEventCode ||
  433. EXIT_THREAD_DEBUG_EVENT == debug_event.dwDebugEventCode ||
  434. EXCEPTION_DEBUG_EVENT == debug_event.dwDebugEventCode ||
  435. LOAD_DLL_DEBUG_EVENT == debug_event.dwDebugEventCode ||
  436. OUTPUT_DEBUG_STRING_EVENT == debug_event.dwDebugEventCode ||
  437. UNLOAD_DLL_DEBUG_EVENT == debug_event.dwDebugEventCode;
  438. if (!go_on)
  439. break;
  440. // Some exceptions are also "normal", apparently.
  441. if (EXCEPTION_DEBUG_EVENT== debug_event.dwDebugEventCode) {
  442. int go_on =
  443. EXCEPTION_BREAKPOINT== debug_event.u.Exception.ExceptionRecord.ExceptionCode;
  444. if (!go_on)
  445. break;
  446. }
  447. // If nothing unexpected happened, let child proceed
  448. ContinueDebugEvent(
  449. debug_event.dwProcessId,
  450. debug_event.dwThreadId,
  451. DBG_CONTINUE
  452. );
  453. }
  454. // Clean up child process
  455. TerminateProcess(process_info.hProcess, 1);
  456. CloseHandle(process_info.hProcess);
  457. CloseHandle(process_info.hThread);
  458. // Reap return value and cleanup
  459. CopyMemory(&rate, shared_mem, sizeof(rate));
  460. (void)UnmapViewOfFile(shared_mem);
  461. (void)CloseHandle(map_handle);
  462. #else
  463. // Not linux, not unix, not WIN32 ... do our best
  464. rate = bench_algo_stage3(algo);
  465. #endif // defined(unix)
  466. // Done
  467. return rate;
  468. }
  469. static void bench_algo(
  470. double *best_rate,
  471. enum sha256_algos *best_algo,
  472. enum sha256_algos algo
  473. )
  474. {
  475. size_t n = max_name_len - strlen(algo_names[algo]);
  476. memset(name_spaces_pad, ' ', n);
  477. name_spaces_pad[n] = 0;
  478. applog(
  479. LOG_ERR,
  480. "\"%s\"%s : benchmarking algorithm ...",
  481. algo_names[algo],
  482. name_spaces_pad
  483. );
  484. double rate = bench_algo_stage2(algo);
  485. if (rate<0.0) {
  486. applog(
  487. LOG_ERR,
  488. "\"%s\"%s : algorithm fails on this platform",
  489. algo_names[algo],
  490. name_spaces_pad
  491. );
  492. } else {
  493. applog(
  494. LOG_ERR,
  495. "\"%s\"%s : algorithm runs at %.5f MH/s",
  496. algo_names[algo],
  497. name_spaces_pad,
  498. rate
  499. );
  500. if (*best_rate<rate) {
  501. *best_rate = rate;
  502. *best_algo = algo;
  503. }
  504. }
  505. }
  506. // Figure out the longest algorithm name
  507. void init_max_name_len()
  508. {
  509. size_t i;
  510. size_t nb_names = sizeof(algo_names)/sizeof(algo_names[0]);
  511. for (i=0; i<nb_names; ++i) {
  512. const char *p = algo_names[i];
  513. size_t name_len = p ? strlen(p) : 0;
  514. if (max_name_len<name_len)
  515. max_name_len = name_len;
  516. }
  517. name_spaces_pad = (char*) malloc(max_name_len+16);
  518. if (0==name_spaces_pad) {
  519. perror("malloc failed");
  520. exit(1);
  521. }
  522. }
  523. // Pick the fastest CPU hasher
  524. static enum sha256_algos pick_fastest_algo()
  525. {
  526. double best_rate = -1.0;
  527. enum sha256_algos best_algo = 0;
  528. applog(LOG_ERR, "benchmarking all sha256 algorithms ...");
  529. bench_algo(&best_rate, &best_algo, ALGO_C);
  530. #if defined(WANT_SSE2_4WAY)
  531. bench_algo(&best_rate, &best_algo, ALGO_4WAY);
  532. #endif
  533. #if defined(WANT_VIA_PADLOCK)
  534. bench_algo(&best_rate, &best_algo, ALGO_VIA);
  535. #endif
  536. bench_algo(&best_rate, &best_algo, ALGO_CRYPTOPP);
  537. #if defined(WANT_CRYPTOPP_ASM32)
  538. bench_algo(&best_rate, &best_algo, ALGO_CRYPTOPP_ASM32);
  539. #endif
  540. #if defined(WANT_X8632_SSE2)
  541. bench_algo(&best_rate, &best_algo, ALGO_SSE2_32);
  542. #endif
  543. #if defined(WANT_X8664_SSE2)
  544. bench_algo(&best_rate, &best_algo, ALGO_SSE2_64);
  545. #endif
  546. #if defined(WANT_X8664_SSE4)
  547. bench_algo(&best_rate, &best_algo, ALGO_SSE4_64);
  548. #endif
  549. #if defined(WANT_ALTIVEC_4WAY)
  550. bench_algo(&best_rate, &best_algo, ALGO_ALTIVEC_4WAY);
  551. #endif
  552. size_t n = max_name_len - strlen(algo_names[best_algo]);
  553. memset(name_spaces_pad, ' ', n);
  554. name_spaces_pad[n] = 0;
  555. applog(
  556. LOG_ERR,
  557. "\"%s\"%s : is fastest algorithm at %.5f MH/s",
  558. algo_names[best_algo],
  559. name_spaces_pad,
  560. best_rate
  561. );
  562. return best_algo;
  563. }
  564. /* FIXME: Use asprintf for better errors. */
  565. char *set_algo(const char *arg, enum sha256_algos *algo)
  566. {
  567. enum sha256_algos i;
  568. if (opt_scrypt)
  569. return "Can only use scrypt algorithm";
  570. if (!strcmp(arg, "auto")) {
  571. *algo = pick_fastest_algo();
  572. return NULL;
  573. }
  574. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  575. if (algo_names[i] && !strcmp(arg, algo_names[i])) {
  576. *algo = i;
  577. return NULL;
  578. }
  579. }
  580. return "Unknown algorithm";
  581. }
  582. #ifdef WANT_SCRYPT
  583. void set_scrypt_algo(enum sha256_algos *algo)
  584. {
  585. *algo = ALGO_SCRYPT;
  586. }
  587. #endif
  588. void show_algo(char buf[OPT_SHOW_LEN], const enum sha256_algos *algo)
  589. {
  590. strncpy(buf, algo_names[*algo], OPT_SHOW_LEN);
  591. }
  592. #endif
  593. #ifdef WANT_CPUMINE
  594. char *force_nthreads_int(const char *arg, int *i)
  595. {
  596. forced_n_threads = true;
  597. return set_int_range(arg, i, 0, 9999);
  598. }
  599. #endif
  600. #ifdef WANT_CPUMINE
  601. static void cpu_detect()
  602. {
  603. int i;
  604. // Reckon number of cores in the box
  605. #if defined(WIN32)
  606. {
  607. DWORD system_am;
  608. DWORD process_am;
  609. BOOL ok = GetProcessAffinityMask(
  610. GetCurrentProcess(),
  611. &system_am,
  612. &process_am
  613. );
  614. if (!ok) {
  615. applog(LOG_ERR, "couldn't figure out number of processors :(");
  616. num_processors = 1;
  617. } else {
  618. size_t n = 32;
  619. num_processors = 0;
  620. while (n--)
  621. if (process_am & (1<<n))
  622. ++num_processors;
  623. }
  624. }
  625. #else
  626. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  627. #endif /* !WIN32 */
  628. if (opt_n_threads < 0 || !forced_n_threads) {
  629. if (total_devices && !opt_usecpu)
  630. opt_n_threads = 0;
  631. else
  632. opt_n_threads = num_processors;
  633. }
  634. if (num_processors < 1)
  635. return;
  636. cpus = calloc(opt_n_threads, sizeof(struct cgpu_info));
  637. if (unlikely(!cpus))
  638. quit(1, "Failed to calloc cpus");
  639. for (i = 0; i < opt_n_threads; ++i) {
  640. struct cgpu_info *cgpu;
  641. cgpu = &cpus[i];
  642. cgpu->api = &cpu_api;
  643. cgpu->deven = DEV_ENABLED;
  644. cgpu->threads = 1;
  645. cgpu->kname = algo_names[opt_algo];
  646. add_cgpu(cgpu);
  647. }
  648. }
  649. static void reinit_cpu_device(struct cgpu_info *cpu)
  650. {
  651. tq_push(thr_info[cpur_thr_id].q, cpu);
  652. }
  653. static bool cpu_thread_prepare(struct thr_info *thr)
  654. {
  655. thread_reportin(thr);
  656. return true;
  657. }
  658. static uint64_t cpu_can_limit_work(struct thr_info *thr)
  659. {
  660. return 0xffff;
  661. }
  662. static bool cpu_thread_init(struct thr_info *thr)
  663. {
  664. const int thr_id = thr->id;
  665. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  666. * and if that fails, then SCHED_BATCH. No need for this to be an
  667. * error if it fails */
  668. setpriority(PRIO_PROCESS, 0, 19);
  669. drop_policy();
  670. /* Cpu affinity only makes sense if the number of threads is a multiple
  671. * of the number of CPUs */
  672. if (!(opt_n_threads % num_processors))
  673. affine_to_cpu(dev_from_id(thr_id), dev_from_id(thr_id) % num_processors);
  674. return true;
  675. }
  676. static int64_t cpu_scanhash(struct thr_info *thr, struct work *work, int64_t max_nonce)
  677. {
  678. const int thr_id = thr->id;
  679. unsigned char hash1[64];
  680. uint32_t first_nonce = work->blk.nonce;
  681. uint32_t last_nonce;
  682. bool rc;
  683. hex2bin(hash1, "00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000", 64);
  684. CPUSearch:
  685. last_nonce = first_nonce;
  686. rc = false;
  687. /* scan nonces for a proof-of-work hash */
  688. {
  689. sha256_func func = sha256_funcs[opt_algo];
  690. rc = (*func)(
  691. thr,
  692. work->midstate,
  693. work->data,
  694. hash1,
  695. work->hash,
  696. work->target,
  697. max_nonce,
  698. &last_nonce,
  699. work->blk.nonce
  700. );
  701. }
  702. /* if nonce found, submit work */
  703. if (unlikely(rc)) {
  704. applog(LOG_DEBUG, "CPU %d found something?", dev_from_id(thr_id));
  705. if (unlikely(!submit_work_sync(thr, work, NULL))) {
  706. applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
  707. }
  708. work->blk.nonce = last_nonce + 1;
  709. goto CPUSearch;
  710. }
  711. else
  712. if (unlikely(last_nonce == first_nonce))
  713. return 0;
  714. work->blk.nonce = last_nonce + 1;
  715. return last_nonce - first_nonce + 1;
  716. }
  717. struct device_api cpu_api = {
  718. .dname = "cpu",
  719. .name = "CPU",
  720. .api_detect = cpu_detect,
  721. .reinit_device = reinit_cpu_device,
  722. .thread_prepare = cpu_thread_prepare,
  723. .can_limit_work = cpu_can_limit_work,
  724. .thread_init = cpu_thread_init,
  725. .scanhash = cpu_scanhash,
  726. };
  727. #endif