cpu-miner.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 = 5;
  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: 5)" },
  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. return true;
  420. }
  421. static void *workio_thread(void *userdata)
  422. {
  423. struct thr_info *mythr = userdata;
  424. bool ok = true;
  425. while (ok) {
  426. struct workio_cmd *wc;
  427. /* wait for workio_cmd sent to us, on our queue */
  428. wc = tq_pop(mythr->q, NULL);
  429. if (!wc) {
  430. ok = false;
  431. break;
  432. }
  433. /* process workio_cmd */
  434. switch (wc->cmd) {
  435. case WC_GET_WORK:
  436. ok = workio_get_work(wc);
  437. break;
  438. case WC_SUBMIT_WORK:
  439. ok = workio_submit_work(wc);
  440. break;
  441. default: /* should never happen */
  442. ok = false;
  443. break;
  444. }
  445. workio_cmd_free(wc);
  446. }
  447. tq_freeze(mythr->q);
  448. return NULL;
  449. }
  450. static void hashmeter(int thr_id, struct timeval *diff,
  451. unsigned long hashes_done)
  452. {
  453. struct timeval temp_tv_end, total_diff;
  454. double khashes, secs;
  455. double total_secs;
  456. double local_mhashes, local_secs;
  457. static local_hashes_done = 0;
  458. /* Don't bother calculating anything if we're not displaying it */
  459. if (opt_quiet || !opt_log_interval)
  460. return;
  461. khashes = hashes_done / 1000.0;
  462. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  463. if (opt_debug)
  464. applog(LOG_DEBUG, "[thread %d: %lu hashes, %.0f khash/sec]",
  465. thr_id, hashes_done, hashes_done / secs);
  466. gettimeofday(&temp_tv_end, NULL);
  467. timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end);
  468. local_secs = (double)total_diff.tv_sec + ((double)total_diff.tv_usec / 1000000.0);
  469. if (opt_n_threads + nDevs > 1) {
  470. /* Totals are updated by all threads so can race without locking */
  471. pthread_mutex_lock(&hash_lock);
  472. total_mhashes_done += (double)hashes_done / 1000000.0;
  473. local_hashes_done += hashes_done;
  474. if (total_diff.tv_sec < opt_log_interval) {
  475. /* Only update the total every opt_log_interval seconds */
  476. pthread_mutex_unlock(&hash_lock);
  477. return;
  478. }
  479. gettimeofday(&total_tv_end, NULL);
  480. pthread_mutex_unlock(&hash_lock);
  481. } else {
  482. total_mhashes_done += (double)hashes_done / 1000000.0;
  483. local_hashes_done += hashes_done;
  484. if (total_diff.tv_sec < opt_log_interval)
  485. return;
  486. gettimeofday(&total_tv_end, NULL);
  487. }
  488. timeval_subtract(&total_diff, &total_tv_end, &total_tv_start);
  489. total_secs = (double)total_diff.tv_sec +
  490. ((double)total_diff.tv_usec / 1000000.0);
  491. local_mhashes = local_hashes_done / 1000000.0;
  492. local_hashes_done = 0;
  493. applog(LOG_INFO, "[%.2f | %.2f Mhash/s] [%d Accepted] [%d Rejected]",
  494. local_mhashes / local_secs,
  495. total_mhashes_done / total_secs, accepted, rejected);
  496. }
  497. static bool get_work(struct thr_info *thr, struct work *work)
  498. {
  499. struct workio_cmd *wc;
  500. struct work *work_heap;
  501. /* fill out work request message */
  502. wc = calloc(1, sizeof(*wc));
  503. if (!wc)
  504. return false;
  505. wc->cmd = WC_GET_WORK;
  506. wc->thr = thr;
  507. /* send work request to workio thread */
  508. if (!tq_push(thr_info[work_thr_id].q, wc)) {
  509. workio_cmd_free(wc);
  510. return false;
  511. }
  512. /* wait for response, a unit of work */
  513. work_heap = tq_pop(thr->q, NULL);
  514. if (!work_heap)
  515. return false;
  516. /* copy returned work into storage provided by caller */
  517. memcpy(work, work_heap, sizeof(*work));
  518. free(work_heap);
  519. return true;
  520. }
  521. static bool submit_work(struct thr_info *thr, const struct work *work_in)
  522. {
  523. struct workio_cmd *wc;
  524. /* fill out work request message */
  525. wc = calloc(1, sizeof(*wc));
  526. if (!wc)
  527. return false;
  528. wc->u.work = malloc(sizeof(*work_in));
  529. if (!wc->u.work)
  530. goto err_out;
  531. wc->cmd = WC_SUBMIT_WORK;
  532. wc->thr = thr;
  533. memcpy(wc->u.work, work_in, sizeof(*work_in));
  534. /* send solution to workio thread */
  535. if (!tq_push(thr_info[work_thr_id].q, wc))
  536. goto err_out;
  537. return true;
  538. err_out:
  539. workio_cmd_free(wc);
  540. return false;
  541. }
  542. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
  543. {
  544. work->data[64+12+0] = (nonce>>0) & 0xff;
  545. work->data[64+12+1] = (nonce>>8) & 0xff;
  546. work->data[64+12+2] = (nonce>>16) & 0xff;
  547. work->data[64+12+3] = (nonce>>24) & 0xff;
  548. return submit_work(thr, work);
  549. }
  550. static inline int cpu_from_thr_id(int thr_id)
  551. {
  552. return (thr_id - nDevs) % num_processors;
  553. }
  554. static void *miner_thread(void *userdata)
  555. {
  556. struct thr_info *mythr = userdata;
  557. int thr_id = mythr->id;
  558. uint32_t max_nonce = 0xffffff;
  559. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  560. * and if that fails, then SCHED_BATCH. No need for this to be an
  561. * error if it fails */
  562. setpriority(PRIO_PROCESS, 0, 19);
  563. drop_policy();
  564. /* Cpu affinity only makes sense if the number of threads is a multiple
  565. * of the number of CPUs */
  566. if (!(opt_n_threads % num_processors))
  567. affine_to_cpu(cpu_from_thr_id(thr_id), thr_id % num_processors);
  568. while (1) {
  569. struct work work __attribute__((aligned(128)));
  570. unsigned long hashes_done;
  571. struct timeval tv_start, tv_end, diff;
  572. uint64_t max64;
  573. bool rc;
  574. /* obtain new work from internal workio thread */
  575. if (unlikely(!get_work(mythr, &work))) {
  576. applog(LOG_ERR, "work retrieval failed, exiting "
  577. "mining thread %d", mythr->id);
  578. goto out;
  579. }
  580. hashes_done = 0;
  581. gettimeofday(&tv_start, NULL);
  582. /* scan nonces for a proof-of-work hash */
  583. switch (opt_algo) {
  584. case ALGO_C:
  585. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  586. work.hash1, work.hash, work.target,
  587. max_nonce, &hashes_done);
  588. break;
  589. #ifdef WANT_X8664_SSE2
  590. case ALGO_SSE2_64: {
  591. unsigned int rc5 =
  592. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  593. work.hash1, work.hash,
  594. work.target,
  595. max_nonce, &hashes_done);
  596. rc = (rc5 == -1) ? false : true;
  597. }
  598. break;
  599. #endif
  600. #ifdef WANT_SSE2_4WAY
  601. case ALGO_4WAY: {
  602. unsigned int rc4 =
  603. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  604. work.hash1, work.hash,
  605. work.target,
  606. max_nonce, &hashes_done);
  607. rc = (rc4 == -1) ? false : true;
  608. }
  609. break;
  610. #endif
  611. #ifdef WANT_VIA_PADLOCK
  612. case ALGO_VIA:
  613. rc = scanhash_via(thr_id, work.data, work.target,
  614. max_nonce, &hashes_done);
  615. break;
  616. #endif
  617. case ALGO_CRYPTOPP:
  618. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  619. work.hash1, work.hash, work.target,
  620. max_nonce, &hashes_done);
  621. break;
  622. #ifdef WANT_CRYPTOPP_ASM32
  623. case ALGO_CRYPTOPP_ASM32:
  624. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  625. work.hash1, work.hash, work.target,
  626. max_nonce, &hashes_done);
  627. break;
  628. #endif
  629. default:
  630. /* should never happen */
  631. goto out;
  632. }
  633. /* record scanhash elapsed time */
  634. gettimeofday(&tv_end, NULL);
  635. timeval_subtract(&diff, &tv_end, &tv_start);
  636. hashmeter(thr_id, &diff, hashes_done);
  637. /* adjust max_nonce to meet target scan time */
  638. if (diff.tv_usec > 500000)
  639. diff.tv_sec++;
  640. if (diff.tv_sec > 0) {
  641. max64 =
  642. ((uint64_t)hashes_done * opt_scantime) / diff.tv_sec;
  643. if (max64 > 0xfffffffaULL)
  644. max64 = 0xfffffffaULL;
  645. max_nonce = max64;
  646. }
  647. /* if nonce found, submit work */
  648. if (unlikely(rc)) {
  649. applog(LOG_INFO, "CPU %d found something?", cpu_from_thr_id(thr_id));
  650. if (!submit_work(mythr, &work))
  651. break;
  652. }
  653. }
  654. out:
  655. tq_freeze(mythr->q);
  656. return NULL;
  657. }
  658. enum {
  659. STAT_SLEEP_INTERVAL = 1,
  660. STAT_CTR_INTERVAL = 10000000,
  661. FAILURE_INTERVAL = 30,
  662. };
  663. static _clState *clStates[16];
  664. static inline cl_int queue_kernel_parameters(dev_blk_ctx *blk, cl_kernel *kernel,
  665. struct _cl_mem *output)
  666. {
  667. cl_int status = 0;
  668. int num = 0;
  669. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  670. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  671. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  672. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  673. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  674. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  675. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  676. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  677. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  678. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  679. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  680. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  681. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  682. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  683. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  684. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  685. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  686. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  687. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  688. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  689. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  690. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  691. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  692. status |= clSetKernelArg(*kernel, num++, sizeof(output), (void *)&output);
  693. return status;
  694. }
  695. static inline int gpu_from_thr_id(int thr_id)
  696. {
  697. return thr_id;
  698. }
  699. static void *gpuminer_thread(void *userdata)
  700. {
  701. struct thr_info *mythr = userdata;
  702. struct timeval tv_start;
  703. int thr_id = mythr->id;
  704. uint32_t res[128], blank_res[128];
  705. cl_kernel *kernel;
  706. memset(blank_res, 0, BUFFERSIZE);
  707. size_t globalThreads[1];
  708. size_t localThreads[1];
  709. cl_int status;
  710. _clState *clState = clStates[thr_id];
  711. kernel = &clState->kernel;
  712. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  713. BUFFERSIZE, blank_res, 0, NULL, NULL);
  714. if (unlikely(status != CL_SUCCESS))
  715. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  716. struct work *work = malloc(sizeof(struct work));
  717. bool need_work = true;
  718. unsigned int threads = 1 << (15 + scan_intensity);
  719. unsigned int vectors = clState->preferred_vwidth;
  720. unsigned int hashes_done = threads * vectors;
  721. gettimeofday(&tv_start, NULL);
  722. globalThreads[0] = threads;
  723. localThreads[0] = clState->max_work_size / vectors;
  724. while (1) {
  725. struct timeval tv_end, diff, tv_workstart;
  726. unsigned int i;
  727. clFinish(clState->commandQueue);
  728. if (need_work) {
  729. gettimeofday(&tv_workstart, NULL);
  730. /* obtain new work from internal workio thread */
  731. if (unlikely(!get_work(mythr, work))) {
  732. applog(LOG_ERR, "work retrieval failed, exiting "
  733. "gpu mining thread %d", mythr->id);
  734. goto out;
  735. }
  736. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  737. work->blk.nonce = 0;
  738. status = queue_kernel_parameters(&work->blk, kernel, clState->outputBuffer);
  739. if (unlikely(status != CL_SUCCESS))
  740. { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
  741. work_restart[thr_id].restart = 0;
  742. need_work = false;
  743. if (opt_debug)
  744. applog(LOG_DEBUG, "getwork");
  745. } else {
  746. status = clSetKernelArg(*kernel, 14, sizeof(uint), (void *)&work->blk.nonce);
  747. if (unlikely(status != CL_SUCCESS))
  748. { applog(LOG_ERR, "Error: clSetKernelArg of nonce failed."); goto out; }
  749. }
  750. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  751. globalThreads, localThreads, 0, NULL, NULL);
  752. if (unlikely(status != CL_SUCCESS))
  753. { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
  754. /* 127 is used as a flag to say nonces exist */
  755. if (unlikely(res[127])) {
  756. /* Clear the buffer again */
  757. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  758. BUFFERSIZE, blank_res, 0, NULL, NULL);
  759. if (unlikely(status != CL_SUCCESS))
  760. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  761. for (i = 0; i < 127; i++) {
  762. if (res[i]) {
  763. applog(LOG_INFO, "GPU %d found something?", gpu_from_thr_id(thr_id));
  764. postcalc_hash(mythr, &work->blk, work, res[i]);
  765. } else
  766. break;
  767. }
  768. clFinish(clState->commandQueue);
  769. }
  770. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  771. BUFFERSIZE, res, 0, NULL, NULL);
  772. if (unlikely(status != CL_SUCCESS))
  773. { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
  774. gettimeofday(&tv_end, NULL);
  775. timeval_subtract(&diff, &tv_end, &tv_start);
  776. hashmeter(thr_id, &diff, hashes_done);
  777. gettimeofday(&tv_start, NULL);
  778. work->blk.nonce += hashes_done;
  779. timeval_subtract(&diff, &tv_end, &tv_workstart);
  780. if (diff.tv_sec > opt_scantime ||
  781. work->blk.nonce > MAXTHREADS - hashes_done ||
  782. work_restart[thr_id].restart)
  783. need_work = true;
  784. }
  785. out:
  786. tq_freeze(mythr->q);
  787. return NULL;
  788. }
  789. static void restart_threads(void)
  790. {
  791. int i;
  792. for (i = 0; i < opt_n_threads + nDevs; i++)
  793. work_restart[i].restart = 1;
  794. }
  795. static void *longpoll_thread(void *userdata)
  796. {
  797. struct thr_info *mythr = userdata;
  798. CURL *curl = NULL;
  799. char *copy_start, *hdr_path, *lp_url = NULL;
  800. bool need_slash = false;
  801. int failures = 0;
  802. hdr_path = tq_pop(mythr->q, NULL);
  803. if (!hdr_path)
  804. goto out;
  805. /* full URL */
  806. if (strstr(hdr_path, "://")) {
  807. lp_url = hdr_path;
  808. hdr_path = NULL;
  809. }
  810. /* absolute path, on current server */
  811. else {
  812. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  813. if (rpc_url[strlen(rpc_url) - 1] != '/')
  814. need_slash = true;
  815. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  816. if (!lp_url)
  817. goto out;
  818. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  819. }
  820. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  821. curl = curl_easy_init();
  822. if (unlikely(!curl)) {
  823. applog(LOG_ERR, "CURL initialization failed");
  824. goto out;
  825. }
  826. while (1) {
  827. json_t *val;
  828. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  829. false, true);
  830. if (likely(val)) {
  831. failures = 0;
  832. json_decref(val);
  833. applog(LOG_INFO, "LONGPOLL detected new block");
  834. restart_threads();
  835. } else {
  836. if (failures++ < 10) {
  837. sleep(30);
  838. applog(LOG_ERR,
  839. "longpoll failed, sleeping for 30s");
  840. } else {
  841. applog(LOG_ERR,
  842. "longpoll failed, ending thread");
  843. goto out;
  844. }
  845. }
  846. }
  847. out:
  848. free(hdr_path);
  849. free(lp_url);
  850. tq_freeze(mythr->q);
  851. if (curl)
  852. curl_easy_cleanup(curl);
  853. return NULL;
  854. }
  855. static void show_usage(void)
  856. {
  857. int i;
  858. printf("minerd version %s\n\n", VERSION);
  859. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  860. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  861. struct option_help *h;
  862. h = &options_help[i];
  863. printf("--%s\n%s\n\n", h->name, h->helptext);
  864. }
  865. exit(1);
  866. }
  867. static void parse_arg (int key, char *arg)
  868. {
  869. int v, i;
  870. switch(key) {
  871. case 'a':
  872. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  873. if (algo_names[i] &&
  874. !strcmp(arg, algo_names[i])) {
  875. opt_algo = i;
  876. break;
  877. }
  878. }
  879. if (i == ARRAY_SIZE(algo_names))
  880. show_usage();
  881. break;
  882. case 'c': {
  883. json_error_t err;
  884. if (opt_config)
  885. json_decref(opt_config);
  886. opt_config = json_load_file(arg, &err);
  887. if (!json_is_object(opt_config)) {
  888. applog(LOG_ERR, "JSON decode of %s failed", arg);
  889. show_usage();
  890. }
  891. break;
  892. }
  893. case 'D':
  894. opt_debug = true;
  895. break;
  896. case 'I':
  897. v = atoi(arg);
  898. if (v < 0 || v > 10) /* sanity check */
  899. show_usage();
  900. scan_intensity = v;
  901. break;
  902. case 'l':
  903. v = atoi(arg);
  904. if (v < 0 || v > 9999) /* sanity check */
  905. show_usage();
  906. opt_log_interval = v;
  907. break;
  908. case 'p':
  909. free(rpc_pass);
  910. rpc_pass = strdup(arg);
  911. break;
  912. case 'P':
  913. opt_protocol = true;
  914. break;
  915. case 'q':
  916. opt_quiet = true;
  917. break;
  918. case 'r':
  919. v = atoi(arg);
  920. if (v < -1 || v > 9999) /* sanity check */
  921. show_usage();
  922. opt_retries = v;
  923. break;
  924. case 'R':
  925. v = atoi(arg);
  926. if (v < 1 || v > 9999) /* sanity check */
  927. show_usage();
  928. opt_fail_pause = v;
  929. break;
  930. case 's':
  931. v = atoi(arg);
  932. if (v < 1 || v > 9999) /* sanity check */
  933. show_usage();
  934. opt_scantime = v;
  935. break;
  936. case 't':
  937. v = atoi(arg);
  938. if (v < 0 || v > 9999) /* sanity check */
  939. show_usage();
  940. opt_n_threads = v;
  941. break;
  942. case 'u':
  943. free(rpc_user);
  944. rpc_user = strdup(arg);
  945. break;
  946. case 1001: /* --url */
  947. if (strncmp(arg, "http://", 7) &&
  948. strncmp(arg, "https://", 8))
  949. show_usage();
  950. free(rpc_url);
  951. rpc_url = strdup(arg);
  952. break;
  953. case 1002: /* --userpass */
  954. if (!strchr(arg, ':'))
  955. show_usage();
  956. free(rpc_userpass);
  957. rpc_userpass = strdup(arg);
  958. break;
  959. case 1003:
  960. want_longpoll = false;
  961. break;
  962. case 1004:
  963. use_syslog = true;
  964. break;
  965. default:
  966. show_usage();
  967. }
  968. }
  969. static void parse_config(void)
  970. {
  971. int i;
  972. json_t *val;
  973. if (!json_is_object(opt_config))
  974. return;
  975. for (i = 0; i < ARRAY_SIZE(options); i++) {
  976. if (!options[i].name)
  977. break;
  978. if (!strcmp(options[i].name, "config"))
  979. continue;
  980. val = json_object_get(opt_config, options[i].name);
  981. if (!val)
  982. continue;
  983. if (options[i].has_arg && json_is_string(val)) {
  984. char *s = strdup(json_string_value(val));
  985. if (!s)
  986. break;
  987. parse_arg(options[i].val, s);
  988. free(s);
  989. } else if (!options[i].has_arg && json_is_true(val))
  990. parse_arg(options[i].val, "");
  991. else
  992. applog(LOG_ERR, "JSON option %s invalid",
  993. options[i].name);
  994. }
  995. }
  996. static void parse_cmdline(int argc, char *argv[])
  997. {
  998. int key;
  999. while (1) {
  1000. key = getopt_long(argc, argv, "a:c:qDPr:s:t:h?", options, NULL);
  1001. if (key < 0)
  1002. break;
  1003. parse_arg(key, optarg);
  1004. }
  1005. parse_config();
  1006. }
  1007. int main (int argc, char *argv[])
  1008. {
  1009. struct thr_info *thr;
  1010. unsigned int i;
  1011. char name[32];
  1012. #ifdef WIN32
  1013. opt_n_threads = 1;
  1014. #else
  1015. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  1016. opt_n_threads = num_processors;
  1017. #endif /* !WIN32 */
  1018. nDevs = clDevicesNum();
  1019. if (opt_ndevs) {
  1020. applog(LOG_INFO, "%i", nDevs);
  1021. return nDevs;
  1022. }
  1023. rpc_url = strdup(DEF_RPC_URL);
  1024. /* parse command line */
  1025. parse_cmdline(argc, argv);
  1026. if (!rpc_userpass) {
  1027. if (!rpc_user || !rpc_pass) {
  1028. applog(LOG_ERR, "No login credentials supplied");
  1029. return 1;
  1030. }
  1031. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  1032. if (!rpc_userpass)
  1033. return 1;
  1034. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  1035. }
  1036. if (unlikely(pthread_mutex_init(&time_lock, NULL)))
  1037. return 1;
  1038. if (unlikely(pthread_mutex_init(&hash_lock, NULL)))
  1039. return 1;
  1040. #ifdef HAVE_SYSLOG_H
  1041. if (use_syslog)
  1042. openlog("cpuminer", LOG_PID, LOG_USER);
  1043. #endif
  1044. work_restart = calloc(opt_n_threads + nDevs, sizeof(*work_restart));
  1045. if (!work_restart)
  1046. return 1;
  1047. thr_info = calloc(opt_n_threads + 2 + nDevs, sizeof(*thr));
  1048. if (!thr_info)
  1049. return 1;
  1050. /* init workio thread info */
  1051. work_thr_id = opt_n_threads + nDevs;
  1052. thr = &thr_info[work_thr_id];
  1053. thr->id = work_thr_id;
  1054. thr->q = tq_new();
  1055. if (!thr->q)
  1056. return 1;
  1057. /* start work I/O thread */
  1058. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  1059. applog(LOG_ERR, "workio thread create failed");
  1060. return 1;
  1061. }
  1062. /* init longpoll thread info */
  1063. if (want_longpoll) {
  1064. longpoll_thr_id = opt_n_threads + nDevs + 1;
  1065. thr = &thr_info[longpoll_thr_id];
  1066. thr->id = longpoll_thr_id;
  1067. thr->q = tq_new();
  1068. if (!thr->q)
  1069. return 1;
  1070. /* start longpoll thread */
  1071. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  1072. applog(LOG_ERR, "longpoll thread create failed");
  1073. return 1;
  1074. }
  1075. } else
  1076. longpoll_thr_id = -1;
  1077. gettimeofday(&total_tv_start, NULL);
  1078. gettimeofday(&total_tv_end, NULL);
  1079. /* start GPU mining threads */
  1080. for (i = 0; i < nDevs; i++) {
  1081. thr = &thr_info[i];
  1082. thr->id = i;
  1083. thr->q = tq_new();
  1084. if (!thr->q)
  1085. return 1;
  1086. applog(LOG_INFO, "Init GPU %i", i);
  1087. clStates[i] = initCl(i, name, sizeof(name));
  1088. if (!clStates[i]) {
  1089. applog(LOG_ERR, "Failed to init GPU %d", i);
  1090. continue;
  1091. }
  1092. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1093. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  1094. applog(LOG_ERR, "thread %d create failed", i);
  1095. return 1;
  1096. }
  1097. sleep(1); /* don't pound RPC server all at once */
  1098. }
  1099. applog(LOG_INFO, "%d gpu miner threads started", i);
  1100. /* start CPU mining threads */
  1101. for (i = nDevs; i < nDevs + opt_n_threads; i++) {
  1102. thr = &thr_info[i];
  1103. thr->id = i;
  1104. thr->q = tq_new();
  1105. if (!thr->q)
  1106. return 1;
  1107. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  1108. applog(LOG_ERR, "thread %d create failed", i);
  1109. return 1;
  1110. }
  1111. sleep(1); /* don't pound RPC server all at once */
  1112. }
  1113. applog(LOG_INFO, "%d cpu miner threads started, "
  1114. "using SHA256 '%s' algorithm.",
  1115. opt_n_threads,
  1116. algo_names[opt_algo]);
  1117. /* Restart count as it will be wrong till all threads are started */
  1118. pthread_mutex_lock(&hash_lock);
  1119. gettimeofday(&total_tv_start, NULL);
  1120. gettimeofday(&total_tv_end, NULL);
  1121. total_mhashes_done = 0;
  1122. pthread_mutex_unlock(&hash_lock);
  1123. /* main loop - simply wait for workio thread to exit */
  1124. pthread_join(thr_info[work_thr_id].pth, NULL);
  1125. applog(LOG_INFO, "workio thread dead, exiting.");
  1126. return 0;
  1127. }