cpu-miner.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. * Copyright 2010 Jeff Garzik
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "cpuminer-config.h"
  11. #define _GNU_SOURCE
  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 <sys/time.h>
  19. #include <time.h>
  20. #include <math.h>
  21. #ifndef WIN32
  22. #include <sys/resource.h>
  23. #endif
  24. #include <getopt.h>
  25. #include <jansson.h>
  26. #include <curl/curl.h>
  27. #include "compat.h"
  28. #include "miner.h"
  29. #include "findnonce.h"
  30. #include "ocl.h"
  31. #define PROGRAM_NAME "minerd"
  32. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  33. #define DEF_RPC_USERNAME "rpcuser"
  34. #define DEF_RPC_PASSWORD "rpcpass"
  35. #define DEF_RPC_USERPASS DEF_RPC_USERNAME ":" DEF_RPC_PASSWORD
  36. #ifdef __linux /* Linux specific policy and affinity management */
  37. #include <sched.h>
  38. static inline void drop_policy(void)
  39. {
  40. struct sched_param param;
  41. #ifdef SCHED_IDLE
  42. if (unlikely(sched_setscheduler(0, SCHED_IDLE, &param) == -1))
  43. #endif
  44. #ifdef SCHED_BATCH
  45. sched_setscheduler(0, SCHED_BATCH, &param);
  46. #endif
  47. }
  48. static inline void affine_to_cpu(int id, int cpu)
  49. {
  50. cpu_set_t set;
  51. CPU_ZERO(&set);
  52. CPU_SET(cpu, &set);
  53. sched_setaffinity(0, sizeof(&set), &set);
  54. applog(LOG_INFO, "Binding thread %d to cpu %d", id, cpu);
  55. }
  56. #else
  57. static inline void drop_policy(void)
  58. {
  59. }
  60. static inline void affine_to_cpu(int id, int cpu)
  61. {
  62. }
  63. #endif
  64. enum workio_commands {
  65. WC_GET_WORK,
  66. WC_SUBMIT_WORK,
  67. };
  68. struct workio_cmd {
  69. enum workio_commands cmd;
  70. struct thr_info *thr;
  71. union {
  72. struct work *work;
  73. } u;
  74. };
  75. enum sha256_algos {
  76. ALGO_C, /* plain C */
  77. ALGO_4WAY, /* parallel SSE2 */
  78. ALGO_VIA, /* VIA padlock */
  79. ALGO_CRYPTOPP, /* Crypto++ (C) */
  80. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  81. ALGO_SSE2_64, /* SSE2 for x86_64 */
  82. };
  83. static const char *algo_names[] = {
  84. [ALGO_C] = "c",
  85. #ifdef WANT_SSE2_4WAY
  86. [ALGO_4WAY] = "4way",
  87. #endif
  88. #ifdef WANT_VIA_PADLOCK
  89. [ALGO_VIA] = "via",
  90. #endif
  91. [ALGO_CRYPTOPP] = "cryptopp",
  92. #ifdef WANT_CRYPTOPP_ASM32
  93. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  94. #endif
  95. #ifdef WANT_X8664_SSE2
  96. [ALGO_SSE2_64] = "sse2_64",
  97. #endif
  98. };
  99. bool opt_debug = false;
  100. bool opt_protocol = false;
  101. bool opt_ndevs = false;
  102. bool want_longpoll = true;
  103. bool have_longpoll = false;
  104. bool use_syslog = false;
  105. static bool opt_quiet = false;
  106. static int opt_retries = 10;
  107. static int opt_fail_pause = 30;
  108. static int opt_log_interval = 5;
  109. int opt_scantime = 60;
  110. static json_t *opt_config;
  111. static const bool opt_time = true;
  112. #ifdef WANT_X8664_SSE2
  113. static enum sha256_algos opt_algo = ALGO_SSE2_64;
  114. #else
  115. static enum sha256_algos opt_algo = ALGO_C;
  116. #endif
  117. static int nDevs;
  118. static int opt_n_threads = 1;
  119. static int num_processors;
  120. static int scan_intensity = 5;
  121. static char *rpc_url;
  122. static char *rpc_userpass;
  123. static char *rpc_user, *rpc_pass;
  124. struct thr_info *thr_info;
  125. static int work_thr_id;
  126. int longpoll_thr_id;
  127. struct work_restart *work_restart = NULL;
  128. pthread_mutex_t time_lock;
  129. static pthread_mutex_t hash_lock;
  130. static double total_mhashes_done;
  131. static struct timeval total_tv_start, total_tv_end;
  132. static int accepted, rejected;
  133. struct option_help {
  134. const char *name;
  135. const char *helptext;
  136. };
  137. static struct option_help options_help[] = {
  138. { "help",
  139. "(-h) Display this help text" },
  140. { "config FILE",
  141. "(-c FILE) JSON-format configuration file (default: none)\n"
  142. "See example-cfg.json for an example configuration." },
  143. { "algo XXX",
  144. "(-a XXX) Specify sha256 implementation:\n"
  145. "\tc\t\tLinux kernel sha256, implemented in C (default)"
  146. #ifdef WANT_SSE2_4WAY
  147. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  148. #endif
  149. #ifdef WANT_VIA_PADLOCK
  150. "\n\tvia\t\tVIA padlock implementation"
  151. #endif
  152. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  153. #ifdef WANT_CRYPTOPP_ASM32
  154. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  155. #endif
  156. #ifdef WANT_X8664_SSE2
  157. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  158. #endif
  159. },
  160. { "quiet",
  161. "(-q) Disable per-thread hashmeter output (default: off)" },
  162. { "debug",
  163. "(-D) Enable debug output (default: off)" },
  164. { "intensity",
  165. "(-I) Intensity of scanning (0 - 10, default 5)" },
  166. { "log",
  167. "(-l) Interval in seconds between log output (default 5)" },
  168. { "ndevs",
  169. "(-n) Display number of detected GPUs" },
  170. { "no-longpoll",
  171. "Disable X-Long-Polling support (default: enabled)" },
  172. { "protocol-dump",
  173. "(-P) Verbose dump of protocol-level activities (default: off)" },
  174. { "retries N",
  175. "(-r N) Number of times to retry, if JSON-RPC call fails\n"
  176. "\t(default: 10; use -1 for \"never\")" },
  177. { "retry-pause N",
  178. "(-R N) Number of seconds to pause, between retries\n"
  179. "\t(default: 30)" },
  180. { "scantime N",
  181. "(-s N) Upper bound on time spent scanning current work,\n"
  182. "\tin seconds. (default: 60)" },
  183. #ifdef HAVE_SYSLOG_H
  184. { "syslog",
  185. "Use system log for output messages (default: standard error)" },
  186. #endif
  187. { "threads N",
  188. "(-t N) Number of miner CPU threads (default: number of processors)" },
  189. { "url URL",
  190. "URL for bitcoin JSON-RPC server "
  191. "(default: " DEF_RPC_URL ")" },
  192. { "userpass USERNAME:PASSWORD",
  193. "Username:Password pair for bitcoin JSON-RPC server "
  194. "(default: " DEF_RPC_USERPASS ")" },
  195. { "user USERNAME",
  196. "(-u USERNAME) Username for bitcoin JSON-RPC server "
  197. "(default: " DEF_RPC_USERNAME ")" },
  198. { "pass PASSWORD",
  199. "(-p PASSWORD) Password for bitcoin JSON-RPC server "
  200. "(default: " DEF_RPC_PASSWORD ")" },
  201. };
  202. static struct option options[] = {
  203. { "algo", 1, NULL, 'a' },
  204. { "config", 1, NULL, 'c' },
  205. { "debug", 0, NULL, 'D' },
  206. { "help", 0, NULL, 'h' },
  207. { "intensity", 1, NULL, 'I' },
  208. { "log", 1, NULL, 'l' },
  209. { "ndevs", 0, NULL, 'n' },
  210. { "no-longpoll", 0, NULL, 1003 },
  211. { "pass", 1, NULL, 'p' },
  212. { "protocol-dump", 0, NULL, 'P' },
  213. { "quiet", 0, NULL, 'q' },
  214. { "threads", 1, NULL, 't' },
  215. { "retries", 1, NULL, 'r' },
  216. { "retry-pause", 1, NULL, 'R' },
  217. { "scantime", 1, NULL, 's' },
  218. #ifdef HAVE_SYSLOG_H
  219. { "syslog", 0, NULL, 1004 },
  220. #endif
  221. { "url", 1, NULL, 1001 },
  222. { "user", 1, NULL, 'u' },
  223. { "userpass", 1, NULL, 1002 },
  224. };
  225. struct work {
  226. unsigned char data[128];
  227. unsigned char hash1[64];
  228. unsigned char midstate[32];
  229. unsigned char target[32];
  230. unsigned char hash[32];
  231. uint32_t output[1];
  232. uint32_t res_nonce;
  233. uint32_t valid;
  234. dev_blk_ctx blk;
  235. };
  236. static bool jobj_binary(const json_t *obj, const char *key,
  237. void *buf, size_t buflen)
  238. {
  239. const char *hexstr;
  240. json_t *tmp;
  241. tmp = json_object_get(obj, key);
  242. if (unlikely(!tmp)) {
  243. applog(LOG_ERR, "JSON key '%s' not found", key);
  244. return false;
  245. }
  246. hexstr = json_string_value(tmp);
  247. if (unlikely(!hexstr)) {
  248. applog(LOG_ERR, "JSON key '%s' is not a string", key);
  249. return false;
  250. }
  251. if (!hex2bin(buf, hexstr, buflen))
  252. return false;
  253. return true;
  254. }
  255. static bool work_decode(const json_t *val, struct work *work)
  256. {
  257. if (unlikely(!jobj_binary(val, "midstate",
  258. work->midstate, sizeof(work->midstate)))) {
  259. applog(LOG_ERR, "JSON inval midstate");
  260. goto err_out;
  261. }
  262. if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data)))) {
  263. applog(LOG_ERR, "JSON inval data");
  264. goto err_out;
  265. }
  266. if (unlikely(!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1)))) {
  267. applog(LOG_ERR, "JSON inval hash1");
  268. goto err_out;
  269. }
  270. if (unlikely(!jobj_binary(val, "target", work->target, sizeof(work->target)))) {
  271. applog(LOG_ERR, "JSON inval target");
  272. goto err_out;
  273. }
  274. memset(work->hash, 0, sizeof(work->hash));
  275. return true;
  276. err_out:
  277. return false;
  278. }
  279. static bool submit_upstream_work(CURL *curl, char *hexstr)
  280. {
  281. json_t *val, *res;
  282. char s[345];
  283. bool rc = false;
  284. /* build JSON-RPC request */
  285. sprintf(s,
  286. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  287. hexstr);
  288. if (opt_debug)
  289. applog(LOG_DEBUG, "DBG: sending RPC call: %s", s);
  290. /* issue JSON-RPC request */
  291. val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false);
  292. if (unlikely(!val)) {
  293. applog(LOG_ERR, "submit_upstream_work json_rpc_call failed");
  294. goto out;
  295. }
  296. res = json_object_get(val, "result");
  297. /* Theoretically threads could race when modifying accepted and
  298. * rejected values but the chance of two submits completing at the
  299. * same time is zero so there is no point adding extra locking */
  300. if (json_is_true(res)) {
  301. accepted++;
  302. applog(LOG_INFO, "PROOF OF WORK RESULT: true (yay!!!)");
  303. } else {
  304. rejected++;
  305. applog(LOG_INFO, "PROOF OF WORK RESULT: false (booooo)");
  306. }
  307. json_decref(val);
  308. rc = true;
  309. out:
  310. return rc;
  311. }
  312. static const char *rpc_req =
  313. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  314. static bool get_upstream_work(CURL *curl, struct work *work)
  315. {
  316. json_t *val;
  317. bool rc;
  318. val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
  319. want_longpoll, false);
  320. if (!val)
  321. return false;
  322. rc = work_decode(json_object_get(val, "result"), work);
  323. json_decref(val);
  324. return rc;
  325. }
  326. static void workio_cmd_free(struct workio_cmd *wc)
  327. {
  328. if (!wc)
  329. return;
  330. switch (wc->cmd) {
  331. case WC_SUBMIT_WORK:
  332. free(wc->u.work);
  333. break;
  334. default: /* do nothing */
  335. break;
  336. }
  337. memset(wc, 0, sizeof(*wc)); /* poison */
  338. free(wc);
  339. }
  340. static bool workio_get_work(struct workio_cmd *wc)
  341. {
  342. struct work *ret_work;
  343. int failures = 0;
  344. bool ret = false;
  345. CURL *curl;
  346. ret_work = calloc(1, sizeof(*ret_work));
  347. if (!ret_work)
  348. goto out;
  349. curl = curl_easy_init();
  350. if (unlikely(!curl)) {
  351. applog(LOG_ERR, "CURL initialization failed");
  352. return ret;
  353. }
  354. /* obtain new work from bitcoin via JSON-RPC */
  355. while (!get_upstream_work(curl, ret_work)) {
  356. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  357. applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
  358. free(ret_work);
  359. goto out;
  360. }
  361. /* pause, then restart work-request loop */
  362. applog(LOG_ERR, "json_rpc_call failed, retry after %d seconds",
  363. opt_fail_pause);
  364. sleep(opt_fail_pause);
  365. }
  366. ret = true;
  367. /* send work to requesting thread */
  368. if (!tq_push(wc->thr->q, ret_work))
  369. free(ret_work);
  370. out:
  371. curl_easy_cleanup(curl);
  372. return ret;
  373. }
  374. static void *submit_thread(void *userdata)
  375. {
  376. char *hexstr = (char *)userdata;
  377. int failures = 0;
  378. CURL *curl;
  379. curl = curl_easy_init();
  380. if (unlikely(!curl)) {
  381. applog(LOG_ERR, "CURL initialization failed");
  382. return NULL;
  383. }
  384. /* submit solution to bitcoin via JSON-RPC */
  385. while (!submit_upstream_work(curl, hexstr)) {
  386. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  387. applog(LOG_ERR, "...terminating workio thread");
  388. exit (1);
  389. }
  390. /* pause, then restart work-request loop */
  391. applog(LOG_ERR, "...retry after %d seconds",
  392. opt_fail_pause);
  393. sleep(opt_fail_pause);
  394. }
  395. free(hexstr);
  396. out:
  397. curl_easy_cleanup(curl);
  398. }
  399. /* Work is submitted asynchronously by creating a thread for each submit
  400. * thus avoiding the mining threads having to wait till work is submitted
  401. * before they can continue working. */
  402. static bool workio_submit_work(struct workio_cmd *wc)
  403. {
  404. struct work *work;
  405. pthread_t thr;
  406. char *hexstr;
  407. pid_t child;
  408. work = wc->u.work;
  409. /* build hex string */
  410. hexstr = bin2hex(work->data, sizeof(work->data));
  411. if (unlikely(!hexstr)) {
  412. applog(LOG_ERR, "workio_submit_work OOM");
  413. return false;
  414. }
  415. if (pthread_create(&thr, NULL, submit_thread, (void *)hexstr)) {
  416. applog(LOG_ERR, "Failed to create submit_thread");
  417. return false;
  418. }
  419. pthread_detach(thr);
  420. return true;
  421. }
  422. static void *workio_thread(void *userdata)
  423. {
  424. struct thr_info *mythr = userdata;
  425. bool ok = true;
  426. while (ok) {
  427. struct workio_cmd *wc;
  428. /* wait for workio_cmd sent to us, on our queue */
  429. wc = tq_pop(mythr->q, NULL);
  430. if (!wc) {
  431. ok = false;
  432. break;
  433. }
  434. /* process workio_cmd */
  435. switch (wc->cmd) {
  436. case WC_GET_WORK:
  437. ok = workio_get_work(wc);
  438. break;
  439. case WC_SUBMIT_WORK:
  440. ok = workio_submit_work(wc);
  441. break;
  442. default: /* should never happen */
  443. ok = false;
  444. break;
  445. }
  446. workio_cmd_free(wc);
  447. }
  448. tq_freeze(mythr->q);
  449. return NULL;
  450. }
  451. static void hashmeter(int thr_id, struct timeval *diff,
  452. unsigned long hashes_done)
  453. {
  454. struct timeval temp_tv_end, total_diff;
  455. double khashes, secs;
  456. double total_secs;
  457. double local_mhashes, local_secs;
  458. static local_hashes_done = 0;
  459. /* Don't bother calculating anything if we're not displaying it */
  460. if (opt_quiet || !opt_log_interval)
  461. return;
  462. khashes = hashes_done / 1000.0;
  463. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  464. if (opt_debug)
  465. applog(LOG_DEBUG, "[thread %d: %lu hashes, %.0f khash/sec]",
  466. thr_id, hashes_done, hashes_done / secs);
  467. gettimeofday(&temp_tv_end, NULL);
  468. timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end);
  469. local_secs = (double)total_diff.tv_sec + ((double)total_diff.tv_usec / 1000000.0);
  470. if (opt_n_threads + nDevs > 1) {
  471. /* Totals are updated by all threads so can race without locking */
  472. pthread_mutex_lock(&hash_lock);
  473. total_mhashes_done += (double)hashes_done / 1000000.0;
  474. local_hashes_done += hashes_done;
  475. if (total_diff.tv_sec < opt_log_interval) {
  476. /* Only update the total every opt_log_interval seconds */
  477. pthread_mutex_unlock(&hash_lock);
  478. return;
  479. }
  480. gettimeofday(&total_tv_end, NULL);
  481. pthread_mutex_unlock(&hash_lock);
  482. } else {
  483. total_mhashes_done += (double)hashes_done / 1000000.0;
  484. local_hashes_done += hashes_done;
  485. if (total_diff.tv_sec < opt_log_interval)
  486. return;
  487. gettimeofday(&total_tv_end, NULL);
  488. }
  489. timeval_subtract(&total_diff, &total_tv_end, &total_tv_start);
  490. total_secs = (double)total_diff.tv_sec +
  491. ((double)total_diff.tv_usec / 1000000.0);
  492. local_mhashes = local_hashes_done / 1000000.0;
  493. local_hashes_done = 0;
  494. applog(LOG_INFO, "[%.2f | %.2f Mhash/s] [%d Accepted] [%d Rejected]",
  495. local_mhashes / local_secs,
  496. total_mhashes_done / total_secs, accepted, rejected);
  497. }
  498. static bool get_work(struct thr_info *thr, struct work *work)
  499. {
  500. struct workio_cmd *wc;
  501. struct work *work_heap;
  502. /* fill out work request message */
  503. wc = calloc(1, sizeof(*wc));
  504. if (!wc)
  505. return false;
  506. wc->cmd = WC_GET_WORK;
  507. wc->thr = thr;
  508. /* send work request to workio thread */
  509. if (!tq_push(thr_info[work_thr_id].q, wc)) {
  510. workio_cmd_free(wc);
  511. return false;
  512. }
  513. /* wait for response, a unit of work */
  514. work_heap = tq_pop(thr->q, NULL);
  515. if (!work_heap)
  516. return false;
  517. /* copy returned work into storage provided by caller */
  518. memcpy(work, work_heap, sizeof(*work));
  519. free(work_heap);
  520. return true;
  521. }
  522. static bool submit_work(struct thr_info *thr, const struct work *work_in)
  523. {
  524. struct workio_cmd *wc;
  525. /* fill out work request message */
  526. wc = calloc(1, sizeof(*wc));
  527. if (!wc)
  528. return false;
  529. wc->u.work = malloc(sizeof(*work_in));
  530. if (!wc->u.work)
  531. goto err_out;
  532. wc->cmd = WC_SUBMIT_WORK;
  533. wc->thr = thr;
  534. memcpy(wc->u.work, work_in, sizeof(*work_in));
  535. /* send solution to workio thread */
  536. if (!tq_push(thr_info[work_thr_id].q, wc))
  537. goto err_out;
  538. return true;
  539. err_out:
  540. workio_cmd_free(wc);
  541. return false;
  542. }
  543. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
  544. {
  545. work->data[64+12+0] = (nonce>>0) & 0xff;
  546. work->data[64+12+1] = (nonce>>8) & 0xff;
  547. work->data[64+12+2] = (nonce>>16) & 0xff;
  548. work->data[64+12+3] = (nonce>>24) & 0xff;
  549. return submit_work(thr, work);
  550. }
  551. static inline int cpu_from_thr_id(int thr_id)
  552. {
  553. return (thr_id - nDevs) % num_processors;
  554. }
  555. static void *miner_thread(void *userdata)
  556. {
  557. struct thr_info *mythr = userdata;
  558. int thr_id = mythr->id;
  559. uint32_t max_nonce = 0xffffff;
  560. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  561. * and if that fails, then SCHED_BATCH. No need for this to be an
  562. * error if it fails */
  563. setpriority(PRIO_PROCESS, 0, 19);
  564. drop_policy();
  565. /* Cpu affinity only makes sense if the number of threads is a multiple
  566. * of the number of CPUs */
  567. if (!(opt_n_threads % num_processors))
  568. affine_to_cpu(cpu_from_thr_id(thr_id), thr_id % num_processors);
  569. while (1) {
  570. struct work work __attribute__((aligned(128)));
  571. unsigned long hashes_done;
  572. struct timeval tv_start, tv_end, diff;
  573. uint64_t max64;
  574. bool rc;
  575. /* obtain new work from internal workio thread */
  576. if (unlikely(!get_work(mythr, &work))) {
  577. applog(LOG_ERR, "work retrieval failed, exiting "
  578. "mining thread %d", mythr->id);
  579. goto out;
  580. }
  581. hashes_done = 0;
  582. gettimeofday(&tv_start, NULL);
  583. /* scan nonces for a proof-of-work hash */
  584. switch (opt_algo) {
  585. case ALGO_C:
  586. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  587. work.hash1, work.hash, work.target,
  588. max_nonce, &hashes_done);
  589. break;
  590. #ifdef WANT_X8664_SSE2
  591. case ALGO_SSE2_64: {
  592. unsigned int rc5 =
  593. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  594. work.hash1, work.hash,
  595. work.target,
  596. max_nonce, &hashes_done);
  597. rc = (rc5 == -1) ? false : true;
  598. }
  599. break;
  600. #endif
  601. #ifdef WANT_SSE2_4WAY
  602. case ALGO_4WAY: {
  603. unsigned int rc4 =
  604. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  605. work.hash1, work.hash,
  606. work.target,
  607. max_nonce, &hashes_done);
  608. rc = (rc4 == -1) ? false : true;
  609. }
  610. break;
  611. #endif
  612. #ifdef WANT_VIA_PADLOCK
  613. case ALGO_VIA:
  614. rc = scanhash_via(thr_id, work.data, work.target,
  615. max_nonce, &hashes_done);
  616. break;
  617. #endif
  618. case ALGO_CRYPTOPP:
  619. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  620. work.hash1, work.hash, work.target,
  621. max_nonce, &hashes_done);
  622. break;
  623. #ifdef WANT_CRYPTOPP_ASM32
  624. case ALGO_CRYPTOPP_ASM32:
  625. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  626. work.hash1, work.hash, work.target,
  627. max_nonce, &hashes_done);
  628. break;
  629. #endif
  630. default:
  631. /* should never happen */
  632. goto out;
  633. }
  634. /* record scanhash elapsed time */
  635. gettimeofday(&tv_end, NULL);
  636. timeval_subtract(&diff, &tv_end, &tv_start);
  637. hashmeter(thr_id, &diff, hashes_done);
  638. /* adjust max_nonce to meet target scan time */
  639. if (diff.tv_usec > 500000)
  640. diff.tv_sec++;
  641. if (diff.tv_sec > 0) {
  642. max64 =
  643. ((uint64_t)hashes_done * opt_scantime) / diff.tv_sec;
  644. if (max64 > 0xfffffffaULL)
  645. max64 = 0xfffffffaULL;
  646. max_nonce = max64;
  647. }
  648. /* if nonce found, submit work */
  649. if (unlikely(rc)) {
  650. applog(LOG_INFO, "CPU %d found something?", cpu_from_thr_id(thr_id));
  651. if (!submit_work(mythr, &work))
  652. break;
  653. }
  654. }
  655. out:
  656. tq_freeze(mythr->q);
  657. return NULL;
  658. }
  659. enum {
  660. STAT_SLEEP_INTERVAL = 1,
  661. STAT_CTR_INTERVAL = 10000000,
  662. FAILURE_INTERVAL = 30,
  663. };
  664. static _clState *clStates[16];
  665. static inline cl_int queue_kernel_parameters(dev_blk_ctx *blk, cl_kernel *kernel,
  666. struct _cl_mem *output)
  667. {
  668. cl_int status = 0;
  669. int num = 0;
  670. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  671. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  672. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  673. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  674. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  675. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  676. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  677. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  678. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  679. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  680. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  681. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  682. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  683. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  684. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  685. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  686. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  687. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  688. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  689. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  690. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  691. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  692. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  693. status |= clSetKernelArg(*kernel, num++, sizeof(output), (void *)&output);
  694. return status;
  695. }
  696. static inline int gpu_from_thr_id(int thr_id)
  697. {
  698. return thr_id;
  699. }
  700. static void *gpuminer_thread(void *userdata)
  701. {
  702. struct thr_info *mythr = userdata;
  703. struct timeval tv_start;
  704. int thr_id = mythr->id;
  705. uint32_t res[128], blank_res[128];
  706. cl_kernel *kernel;
  707. memset(blank_res, 0, BUFFERSIZE);
  708. size_t globalThreads[1];
  709. size_t localThreads[1];
  710. cl_int status;
  711. _clState *clState = clStates[thr_id];
  712. kernel = &clState->kernel;
  713. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  714. BUFFERSIZE, blank_res, 0, NULL, NULL);
  715. if (unlikely(status != CL_SUCCESS))
  716. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  717. struct work *work = malloc(sizeof(struct work));
  718. bool need_work = true;
  719. unsigned int threads = 1 << (15 + scan_intensity);
  720. unsigned int vectors = clState->preferred_vwidth;
  721. unsigned int hashes_done = threads * vectors;
  722. gettimeofday(&tv_start, NULL);
  723. globalThreads[0] = threads;
  724. localThreads[0] = clState->max_work_size / vectors;
  725. while (1) {
  726. struct timeval tv_end, diff, tv_workstart;
  727. unsigned int i;
  728. clFinish(clState->commandQueue);
  729. if (need_work) {
  730. gettimeofday(&tv_workstart, NULL);
  731. /* obtain new work from internal workio thread */
  732. if (unlikely(!get_work(mythr, work))) {
  733. applog(LOG_ERR, "work retrieval failed, exiting "
  734. "gpu mining thread %d", mythr->id);
  735. goto out;
  736. }
  737. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  738. work->blk.nonce = 0;
  739. status = queue_kernel_parameters(&work->blk, kernel, clState->outputBuffer);
  740. if (unlikely(status != CL_SUCCESS))
  741. { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
  742. work_restart[thr_id].restart = 0;
  743. need_work = false;
  744. if (opt_debug)
  745. applog(LOG_DEBUG, "getwork");
  746. } else {
  747. status = clSetKernelArg(*kernel, 14, sizeof(uint), (void *)&work->blk.nonce);
  748. if (unlikely(status != CL_SUCCESS))
  749. { applog(LOG_ERR, "Error: clSetKernelArg of nonce failed."); goto out; }
  750. }
  751. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  752. globalThreads, localThreads, 0, NULL, NULL);
  753. if (unlikely(status != CL_SUCCESS))
  754. { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
  755. /* 127 is used as a flag to say nonces exist */
  756. if (unlikely(res[127])) {
  757. /* Clear the buffer again */
  758. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  759. BUFFERSIZE, blank_res, 0, NULL, NULL);
  760. if (unlikely(status != CL_SUCCESS))
  761. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  762. for (i = 0; i < 127; i++) {
  763. if (res[i]) {
  764. applog(LOG_INFO, "GPU %d found something?", gpu_from_thr_id(thr_id));
  765. postcalc_hash(mythr, &work->blk, work, res[i]);
  766. } else
  767. break;
  768. }
  769. clFinish(clState->commandQueue);
  770. }
  771. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  772. BUFFERSIZE, res, 0, NULL, NULL);
  773. if (unlikely(status != CL_SUCCESS))
  774. { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
  775. gettimeofday(&tv_end, NULL);
  776. timeval_subtract(&diff, &tv_end, &tv_start);
  777. hashmeter(thr_id, &diff, hashes_done);
  778. gettimeofday(&tv_start, NULL);
  779. work->blk.nonce += hashes_done;
  780. timeval_subtract(&diff, &tv_end, &tv_workstart);
  781. if (diff.tv_sec > opt_scantime ||
  782. work->blk.nonce > MAXTHREADS - hashes_done ||
  783. work_restart[thr_id].restart)
  784. need_work = true;
  785. }
  786. out:
  787. tq_freeze(mythr->q);
  788. return NULL;
  789. }
  790. static void restart_threads(void)
  791. {
  792. int i;
  793. for (i = 0; i < opt_n_threads + nDevs; i++)
  794. work_restart[i].restart = 1;
  795. }
  796. static void *longpoll_thread(void *userdata)
  797. {
  798. struct thr_info *mythr = userdata;
  799. CURL *curl = NULL;
  800. char *copy_start, *hdr_path, *lp_url = NULL;
  801. bool need_slash = false;
  802. int failures = 0;
  803. hdr_path = tq_pop(mythr->q, NULL);
  804. if (!hdr_path)
  805. goto out;
  806. /* full URL */
  807. if (strstr(hdr_path, "://")) {
  808. lp_url = hdr_path;
  809. hdr_path = NULL;
  810. }
  811. /* absolute path, on current server */
  812. else {
  813. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  814. if (rpc_url[strlen(rpc_url) - 1] != '/')
  815. need_slash = true;
  816. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  817. if (!lp_url)
  818. goto out;
  819. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  820. }
  821. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  822. curl = curl_easy_init();
  823. if (unlikely(!curl)) {
  824. applog(LOG_ERR, "CURL initialization failed");
  825. goto out;
  826. }
  827. while (1) {
  828. json_t *val;
  829. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  830. false, true);
  831. if (likely(val)) {
  832. failures = 0;
  833. json_decref(val);
  834. applog(LOG_INFO, "LONGPOLL detected new block");
  835. restart_threads();
  836. } else {
  837. if (failures++ < 10) {
  838. sleep(30);
  839. applog(LOG_ERR,
  840. "longpoll failed, sleeping for 30s");
  841. } else {
  842. applog(LOG_ERR,
  843. "longpoll failed, ending thread");
  844. goto out;
  845. }
  846. }
  847. }
  848. out:
  849. free(hdr_path);
  850. free(lp_url);
  851. tq_freeze(mythr->q);
  852. if (curl)
  853. curl_easy_cleanup(curl);
  854. return NULL;
  855. }
  856. static void show_usage(void)
  857. {
  858. int i;
  859. printf("minerd version %s\n\n", VERSION);
  860. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  861. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  862. struct option_help *h;
  863. h = &options_help[i];
  864. printf("--%s\n%s\n\n", h->name, h->helptext);
  865. }
  866. exit(1);
  867. }
  868. static void parse_arg (int key, char *arg)
  869. {
  870. int v, i;
  871. switch(key) {
  872. case 'a':
  873. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  874. if (algo_names[i] &&
  875. !strcmp(arg, algo_names[i])) {
  876. opt_algo = i;
  877. break;
  878. }
  879. }
  880. if (i == ARRAY_SIZE(algo_names))
  881. show_usage();
  882. break;
  883. case 'c': {
  884. json_error_t err;
  885. if (opt_config)
  886. json_decref(opt_config);
  887. opt_config = json_load_file(arg, &err);
  888. if (!json_is_object(opt_config)) {
  889. applog(LOG_ERR, "JSON decode of %s failed", arg);
  890. show_usage();
  891. }
  892. break;
  893. }
  894. case 'D':
  895. opt_debug = true;
  896. break;
  897. case 'I':
  898. v = atoi(arg);
  899. if (v < 0 || v > 10) /* sanity check */
  900. show_usage();
  901. scan_intensity = v;
  902. break;
  903. case 'l':
  904. v = atoi(arg);
  905. if (v < 0 || v > 9999) /* sanity check */
  906. show_usage();
  907. opt_log_interval = v;
  908. break;
  909. case 'p':
  910. free(rpc_pass);
  911. rpc_pass = strdup(arg);
  912. break;
  913. case 'P':
  914. opt_protocol = true;
  915. break;
  916. case 'q':
  917. opt_quiet = true;
  918. break;
  919. case 'r':
  920. v = atoi(arg);
  921. if (v < -1 || v > 9999) /* sanity check */
  922. show_usage();
  923. opt_retries = v;
  924. break;
  925. case 'R':
  926. v = atoi(arg);
  927. if (v < 1 || v > 9999) /* sanity check */
  928. show_usage();
  929. opt_fail_pause = v;
  930. break;
  931. case 's':
  932. v = atoi(arg);
  933. if (v < 1 || v > 9999) /* sanity check */
  934. show_usage();
  935. opt_scantime = v;
  936. break;
  937. case 't':
  938. v = atoi(arg);
  939. if (v < 0 || v > 9999) /* sanity check */
  940. show_usage();
  941. opt_n_threads = v;
  942. break;
  943. case 'u':
  944. free(rpc_user);
  945. rpc_user = strdup(arg);
  946. break;
  947. case 1001: /* --url */
  948. if (strncmp(arg, "http://", 7) &&
  949. strncmp(arg, "https://", 8))
  950. show_usage();
  951. free(rpc_url);
  952. rpc_url = strdup(arg);
  953. break;
  954. case 1002: /* --userpass */
  955. if (!strchr(arg, ':'))
  956. show_usage();
  957. free(rpc_userpass);
  958. rpc_userpass = strdup(arg);
  959. break;
  960. case 1003:
  961. want_longpoll = false;
  962. break;
  963. case 1004:
  964. use_syslog = true;
  965. break;
  966. default:
  967. show_usage();
  968. }
  969. }
  970. static void parse_config(void)
  971. {
  972. int i;
  973. json_t *val;
  974. if (!json_is_object(opt_config))
  975. return;
  976. for (i = 0; i < ARRAY_SIZE(options); i++) {
  977. if (!options[i].name)
  978. break;
  979. if (!strcmp(options[i].name, "config"))
  980. continue;
  981. val = json_object_get(opt_config, options[i].name);
  982. if (!val)
  983. continue;
  984. if (options[i].has_arg && json_is_string(val)) {
  985. char *s = strdup(json_string_value(val));
  986. if (!s)
  987. break;
  988. parse_arg(options[i].val, s);
  989. free(s);
  990. } else if (!options[i].has_arg && json_is_true(val))
  991. parse_arg(options[i].val, "");
  992. else
  993. applog(LOG_ERR, "JSON option %s invalid",
  994. options[i].name);
  995. }
  996. }
  997. static void parse_cmdline(int argc, char *argv[])
  998. {
  999. int key;
  1000. while (1) {
  1001. key = getopt_long(argc, argv, "a:c:qDPr:s:t:h?", options, NULL);
  1002. if (key < 0)
  1003. break;
  1004. parse_arg(key, optarg);
  1005. }
  1006. parse_config();
  1007. }
  1008. int main (int argc, char *argv[])
  1009. {
  1010. struct thr_info *thr;
  1011. unsigned int i;
  1012. char name[32];
  1013. #ifdef WIN32
  1014. opt_n_threads = 1;
  1015. #else
  1016. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  1017. opt_n_threads = num_processors;
  1018. #endif /* !WIN32 */
  1019. nDevs = clDevicesNum();
  1020. if (opt_ndevs) {
  1021. applog(LOG_INFO, "%i", nDevs);
  1022. return nDevs;
  1023. }
  1024. rpc_url = strdup(DEF_RPC_URL);
  1025. /* parse command line */
  1026. parse_cmdline(argc, argv);
  1027. if (!rpc_userpass) {
  1028. if (!rpc_user || !rpc_pass) {
  1029. applog(LOG_ERR, "No login credentials supplied");
  1030. return 1;
  1031. }
  1032. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  1033. if (!rpc_userpass)
  1034. return 1;
  1035. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  1036. }
  1037. if (unlikely(pthread_mutex_init(&time_lock, NULL)))
  1038. return 1;
  1039. if (unlikely(pthread_mutex_init(&hash_lock, NULL)))
  1040. return 1;
  1041. #ifdef HAVE_SYSLOG_H
  1042. if (use_syslog)
  1043. openlog("cpuminer", LOG_PID, LOG_USER);
  1044. #endif
  1045. work_restart = calloc(opt_n_threads + nDevs, sizeof(*work_restart));
  1046. if (!work_restart)
  1047. return 1;
  1048. thr_info = calloc(opt_n_threads + 2 + nDevs, sizeof(*thr));
  1049. if (!thr_info)
  1050. return 1;
  1051. /* init workio thread info */
  1052. work_thr_id = opt_n_threads + nDevs;
  1053. thr = &thr_info[work_thr_id];
  1054. thr->id = work_thr_id;
  1055. thr->q = tq_new();
  1056. if (!thr->q)
  1057. return 1;
  1058. /* start work I/O thread */
  1059. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  1060. applog(LOG_ERR, "workio thread create failed");
  1061. return 1;
  1062. }
  1063. /* init longpoll thread info */
  1064. if (want_longpoll) {
  1065. longpoll_thr_id = opt_n_threads + nDevs + 1;
  1066. thr = &thr_info[longpoll_thr_id];
  1067. thr->id = longpoll_thr_id;
  1068. thr->q = tq_new();
  1069. if (!thr->q)
  1070. return 1;
  1071. /* start longpoll thread */
  1072. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  1073. applog(LOG_ERR, "longpoll thread create failed");
  1074. return 1;
  1075. }
  1076. } else
  1077. longpoll_thr_id = -1;
  1078. gettimeofday(&total_tv_start, NULL);
  1079. gettimeofday(&total_tv_end, NULL);
  1080. /* start GPU mining threads */
  1081. for (i = 0; i < nDevs; i++) {
  1082. thr = &thr_info[i];
  1083. thr->id = i;
  1084. thr->q = tq_new();
  1085. if (!thr->q)
  1086. return 1;
  1087. applog(LOG_INFO, "Init GPU %i", i);
  1088. clStates[i] = initCl(i, name, sizeof(name));
  1089. if (!clStates[i]) {
  1090. applog(LOG_ERR, "Failed to init GPU %d", i);
  1091. continue;
  1092. }
  1093. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1094. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  1095. applog(LOG_ERR, "thread %d create failed", i);
  1096. return 1;
  1097. }
  1098. sleep(1); /* don't pound RPC server all at once */
  1099. }
  1100. applog(LOG_INFO, "%d gpu miner threads started", i);
  1101. /* start CPU mining threads */
  1102. for (i = nDevs; i < nDevs + opt_n_threads; i++) {
  1103. thr = &thr_info[i];
  1104. thr->id = i;
  1105. thr->q = tq_new();
  1106. if (!thr->q)
  1107. return 1;
  1108. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  1109. applog(LOG_ERR, "thread %d create failed", i);
  1110. return 1;
  1111. }
  1112. sleep(1); /* don't pound RPC server all at once */
  1113. }
  1114. applog(LOG_INFO, "%d cpu miner threads started, "
  1115. "using SHA256 '%s' algorithm.",
  1116. opt_n_threads,
  1117. algo_names[opt_algo]);
  1118. /* Restart count as it will be wrong till all threads are started */
  1119. pthread_mutex_lock(&hash_lock);
  1120. gettimeofday(&total_tv_start, NULL);
  1121. gettimeofday(&total_tv_end, NULL);
  1122. total_mhashes_done = 0;
  1123. pthread_mutex_unlock(&hash_lock);
  1124. /* main loop - simply wait for workio thread to exit */
  1125. pthread_join(thr_info[work_thr_id].pth, NULL);
  1126. applog(LOG_INFO, "workio thread dead, exiting.");
  1127. return 0;
  1128. }