cpu-miner.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Copyright 2010 Jeff Garzik
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "cpuminer-config.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <unistd.h>
  16. #include <sys/time.h>
  17. #include <time.h>
  18. #ifndef WIN32
  19. #include <sys/resource.h>
  20. #endif
  21. #include <getopt.h>
  22. #include <jansson.h>
  23. #include <curl/curl.h>
  24. #include "compat.h"
  25. #include "miner.h"
  26. #define PROGRAM_NAME "minerd"
  27. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  28. #define DEF_RPC_USERPASS "rpcuser:rpcpass"
  29. struct thr_info {
  30. int id;
  31. pthread_t pth;
  32. struct thread_q *q;
  33. };
  34. enum workio_commands {
  35. WC_GET_WORK,
  36. WC_SUBMIT_WORK,
  37. };
  38. struct workio_cmd {
  39. enum workio_commands cmd;
  40. struct thr_info *thr;
  41. union {
  42. struct work *work;
  43. } u;
  44. };
  45. enum sha256_algos {
  46. ALGO_C, /* plain C */
  47. ALGO_4WAY, /* parallel SSE2 */
  48. ALGO_VIA, /* VIA padlock */
  49. ALGO_CRYPTOPP, /* Crypto++ (C) */
  50. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  51. ALGO_SSE2_64, /* SSE2 for x86_64 */
  52. };
  53. static const char *algo_names[] = {
  54. [ALGO_C] = "c",
  55. #ifdef WANT_SSE2_4WAY
  56. [ALGO_4WAY] = "4way",
  57. #endif
  58. #ifdef WANT_VIA_PADLOCK
  59. [ALGO_VIA] = "via",
  60. #endif
  61. [ALGO_CRYPTOPP] = "cryptopp",
  62. #ifdef WANT_CRYPTOPP_ASM32
  63. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  64. #endif
  65. #ifdef WANT_X8664_SSE2
  66. [ALGO_SSE2_64] = "sse2_64",
  67. #endif
  68. };
  69. bool opt_debug = false;
  70. bool opt_protocol = false;
  71. static bool opt_quiet = false;
  72. static int opt_retries = 10;
  73. static int opt_fail_pause = 30;
  74. static int opt_scantime = 5;
  75. static json_t *opt_config;
  76. static const bool opt_time = true;
  77. static enum sha256_algos opt_algo = ALGO_C;
  78. static int opt_n_threads = 1;
  79. static char *rpc_url;
  80. static char *userpass;
  81. static struct thr_info *thr_info;
  82. static int work_thr_id;
  83. struct option_help {
  84. const char *name;
  85. const char *helptext;
  86. };
  87. static struct option_help options_help[] = {
  88. { "help",
  89. "(-h) Display this help text" },
  90. { "config FILE",
  91. "(-c FILE) JSON-format configuration file (default: none)\n"
  92. "See example-cfg.json for an example configuration." },
  93. { "algo XXX",
  94. "(-a XXX) Specify sha256 implementation:\n"
  95. "\tc\t\tLinux kernel sha256, implemented in C (default)"
  96. #ifdef WANT_SSE2_4WAY
  97. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  98. #endif
  99. #ifdef WANT_VIA_PADLOCK
  100. "\n\tvia\t\tVIA padlock implementation"
  101. #endif
  102. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  103. #ifdef WANT_CRYPTOPP_ASM32
  104. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  105. #endif
  106. #ifdef WANT_X8664_SSE2
  107. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  108. #endif
  109. },
  110. { "quiet",
  111. "(-q) Disable per-thread hashmeter output (default: off)" },
  112. { "debug",
  113. "(-D) Enable debug output (default: off)" },
  114. { "protocol-dump",
  115. "(-P) Verbose dump of protocol-level activities (default: off)" },
  116. { "retries N",
  117. "(-r N) Number of times to retry, if JSON-RPC call fails\n"
  118. "\t(default: 10; use -1 for \"never\")" },
  119. { "retry-pause N",
  120. "(-R N) Number of seconds to pause, between retries\n"
  121. "\t(default: 30)" },
  122. { "scantime N",
  123. "(-s N) Upper bound on time spent scanning current work,\n"
  124. "\tin seconds. (default: 5)" },
  125. { "threads N",
  126. "(-t N) Number of miner threads (default: 1)" },
  127. { "url URL",
  128. "URL for bitcoin JSON-RPC server "
  129. "(default: " DEF_RPC_URL ")" },
  130. { "userpass USERNAME:PASSWORD",
  131. "Username:Password pair for bitcoin JSON-RPC server "
  132. "(default: " DEF_RPC_USERPASS ")" },
  133. };
  134. static struct option options[] = {
  135. { "help", 0, NULL, 'h' },
  136. { "algo", 1, NULL, 'a' },
  137. { "config", 1, NULL, 'c' },
  138. { "quiet", 0, NULL, 'q' },
  139. { "debug", 0, NULL, 'D' },
  140. { "protocol-dump", 0, NULL, 'P' },
  141. { "threads", 1, NULL, 't' },
  142. { "retries", 1, NULL, 'r' },
  143. { "retry-pause", 1, NULL, 'R' },
  144. { "scantime", 1, NULL, 's' },
  145. { "url", 1, NULL, 1001 },
  146. { "userpass", 1, NULL, 1002 },
  147. { }
  148. };
  149. struct work {
  150. unsigned char data[128];
  151. unsigned char hash1[64];
  152. unsigned char midstate[32];
  153. unsigned char target[32];
  154. unsigned char hash[32];
  155. };
  156. static bool jobj_binary(const json_t *obj, const char *key,
  157. void *buf, size_t buflen)
  158. {
  159. const char *hexstr;
  160. json_t *tmp;
  161. tmp = json_object_get(obj, key);
  162. if (!tmp) {
  163. fprintf(stderr, "JSON key '%s' not found\n", key);
  164. return false;
  165. }
  166. hexstr = json_string_value(tmp);
  167. if (!hexstr) {
  168. fprintf(stderr, "JSON key '%s' is not a string\n", key);
  169. return false;
  170. }
  171. if (!hex2bin(buf, hexstr, buflen))
  172. return false;
  173. return true;
  174. }
  175. static bool work_decode(const json_t *val, struct work *work)
  176. {
  177. if (!jobj_binary(val, "midstate",
  178. work->midstate, sizeof(work->midstate))) {
  179. fprintf(stderr, "JSON inval midstate\n");
  180. goto err_out;
  181. }
  182. if (!jobj_binary(val, "data", work->data, sizeof(work->data))) {
  183. fprintf(stderr, "JSON inval data\n");
  184. goto err_out;
  185. }
  186. if (!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1))) {
  187. fprintf(stderr, "JSON inval hash1\n");
  188. goto err_out;
  189. }
  190. if (!jobj_binary(val, "target", work->target, sizeof(work->target))) {
  191. fprintf(stderr, "JSON inval target\n");
  192. goto err_out;
  193. }
  194. memset(work->hash, 0, sizeof(work->hash));
  195. return true;
  196. err_out:
  197. return false;
  198. }
  199. static bool submit_upstream_work(CURL *curl, const struct work *work)
  200. {
  201. char *hexstr = NULL;
  202. json_t *val, *res;
  203. char s[345], timestr[64];
  204. time_t now;
  205. struct tm *tm;
  206. bool rc = false;
  207. now = time(NULL);
  208. /* build hex string */
  209. hexstr = bin2hex(work->data, sizeof(work->data));
  210. if (!hexstr) {
  211. fprintf(stderr, "submit_upstream_work OOM\n");
  212. goto out;
  213. }
  214. /* build JSON-RPC request */
  215. sprintf(s,
  216. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  217. hexstr);
  218. if (opt_debug)
  219. fprintf(stderr, "DBG: sending RPC call:\n%s", s);
  220. /* issue JSON-RPC request */
  221. val = json_rpc_call(curl, rpc_url, userpass, s);
  222. if (!val) {
  223. fprintf(stderr, "submit_upstream_work json_rpc_call failed\n");
  224. goto out;
  225. }
  226. res = json_object_get(val, "result");
  227. tm = localtime(&now);
  228. strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
  229. printf("[%s] PROOF OF WORK RESULT: %s\n",
  230. timestr, json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
  231. json_decref(val);
  232. rc = true;
  233. out:
  234. free(hexstr);
  235. return rc;
  236. }
  237. static bool get_upstream_work(CURL *curl, struct work *work)
  238. {
  239. static const char *rpc_req =
  240. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  241. json_t *val;
  242. bool rc;
  243. val = json_rpc_call(curl, rpc_url, userpass, rpc_req);
  244. if (!val)
  245. return false;
  246. rc = work_decode(json_object_get(val, "result"), work);
  247. json_decref(val);
  248. return rc;
  249. }
  250. static void workio_cmd_free(struct workio_cmd *wc)
  251. {
  252. if (!wc)
  253. return;
  254. switch (wc->cmd) {
  255. case WC_SUBMIT_WORK:
  256. free(wc->u.work);
  257. break;
  258. default: /* do nothing */
  259. break;
  260. }
  261. memset(wc, 0, sizeof(*wc)); /* poison */
  262. free(wc);
  263. }
  264. static bool workio_get_work(struct workio_cmd *wc, CURL *curl)
  265. {
  266. struct work *ret_work;
  267. int failures = 0;
  268. ret_work = calloc(1, sizeof(*ret_work));
  269. if (!ret_work)
  270. return false;
  271. /* obtain new work from bitcoin via JSON-RPC */
  272. while (!get_upstream_work(curl, ret_work)) {
  273. fprintf(stderr, "json_rpc_call failed, ");
  274. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  275. fprintf(stderr, "terminating workio thread\n");
  276. free(ret_work);
  277. return false;
  278. }
  279. /* pause, then restart work-request loop */
  280. fprintf(stderr, "retry after %d seconds\n",
  281. opt_fail_pause);
  282. sleep(opt_fail_pause);
  283. }
  284. /* send work to requesting thread */
  285. if (!tq_push(wc->thr->q, ret_work))
  286. free(ret_work);
  287. return true;
  288. }
  289. static bool workio_submit_work(struct workio_cmd *wc, CURL *curl)
  290. {
  291. int failures = 0;
  292. /* submit solution to bitcoin via JSON-RPC */
  293. while (!submit_upstream_work(curl, wc->u.work)) {
  294. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  295. fprintf(stderr, "...terminating workio thread\n");
  296. return false;
  297. }
  298. /* pause, then restart work-request loop */
  299. fprintf(stderr, "...retry after %d seconds\n",
  300. opt_fail_pause);
  301. sleep(opt_fail_pause);
  302. }
  303. return true;
  304. }
  305. static void *workio_thread(void *userdata)
  306. {
  307. struct thr_info *mythr = userdata;
  308. CURL *curl;
  309. bool ok = true;
  310. curl = curl_easy_init();
  311. if (!curl) {
  312. fprintf(stderr, "CURL initialization failed\n");
  313. return NULL;
  314. }
  315. while (ok) {
  316. struct workio_cmd *wc;
  317. /* wait for workio_cmd sent to us, on our queue */
  318. wc = tq_pop(mythr->q, NULL);
  319. if (!wc) {
  320. ok = false;
  321. break;
  322. }
  323. /* process workio_cmd */
  324. switch (wc->cmd) {
  325. case WC_GET_WORK:
  326. ok = workio_get_work(wc, curl);
  327. break;
  328. case WC_SUBMIT_WORK:
  329. ok = workio_submit_work(wc, curl);
  330. break;
  331. default: /* should never happen */
  332. ok = false;
  333. break;
  334. }
  335. workio_cmd_free(wc);
  336. }
  337. tq_freeze(mythr->q);
  338. curl_easy_cleanup(curl);
  339. return NULL;
  340. }
  341. static void hashmeter(int thr_id, const struct timeval *diff,
  342. unsigned long hashes_done)
  343. {
  344. double khashes, secs;
  345. khashes = hashes_done / 1000.0;
  346. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  347. if (!opt_quiet)
  348. printf("HashMeter(%d): %lu hashes, %.2f khash/sec\n",
  349. thr_id, hashes_done,
  350. khashes / secs);
  351. }
  352. static bool get_work(struct thr_info *thr, struct work *work)
  353. {
  354. struct workio_cmd *wc;
  355. struct work *work_heap;
  356. /* fill out work request message */
  357. wc = calloc(1, sizeof(*wc));
  358. if (!wc)
  359. return false;
  360. wc->cmd = WC_GET_WORK;
  361. wc->thr = thr;
  362. /* send work request to workio thread */
  363. if (!tq_push(thr_info[work_thr_id].q, wc)) {
  364. workio_cmd_free(wc);
  365. return false;
  366. }
  367. /* wait for response, a unit of work */
  368. work_heap = tq_pop(thr->q, NULL);
  369. if (!work_heap)
  370. return false;
  371. /* copy returned work into storage provided by caller */
  372. memcpy(work, work_heap, sizeof(*work));
  373. free(work_heap);
  374. return true;
  375. }
  376. static bool submit_work(struct thr_info *thr, const struct work *work_in)
  377. {
  378. struct workio_cmd *wc;
  379. /* fill out work request message */
  380. wc = calloc(1, sizeof(*wc));
  381. if (!wc)
  382. return false;
  383. wc->u.work = malloc(sizeof(*work_in));
  384. if (!wc->u.work)
  385. goto err_out;
  386. wc->cmd = WC_SUBMIT_WORK;
  387. wc->thr = thr;
  388. memcpy(wc->u.work, work_in, sizeof(*work_in));
  389. /* send solution to workio thread */
  390. if (!tq_push(thr_info[work_thr_id].q, wc))
  391. goto err_out;
  392. return true;
  393. err_out:
  394. workio_cmd_free(wc);
  395. return false;
  396. }
  397. static void *miner_thread(void *userdata)
  398. {
  399. struct thr_info *mythr = userdata;
  400. int thr_id = mythr->id;
  401. uint32_t max_nonce = 0xffffff;
  402. while (1) {
  403. struct work work __attribute__((aligned(128)));
  404. unsigned long hashes_done;
  405. struct timeval tv_start, tv_end, diff;
  406. bool rc;
  407. /* obtain new work from internal workio thread */
  408. if (!get_work(mythr, &work)) {
  409. fprintf(stderr, "work retrieval failed, exiting "
  410. "mining thread %d\n", mythr->id);
  411. goto out;
  412. }
  413. hashes_done = 0;
  414. gettimeofday(&tv_start, NULL);
  415. /* scan nonces for a proof-of-work hash */
  416. switch (opt_algo) {
  417. case ALGO_C:
  418. rc = scanhash_c(work.midstate, work.data + 64,
  419. work.hash1, work.hash, work.target,
  420. max_nonce, &hashes_done);
  421. break;
  422. #ifdef WANT_X8664_SSE2
  423. case ALGO_SSE2_64: {
  424. unsigned int rc5 =
  425. scanhash_sse2_64(work.midstate, work.data + 64,
  426. work.hash1, work.hash,
  427. work.target,
  428. max_nonce, &hashes_done);
  429. rc = (rc5 == -1) ? false : true;
  430. }
  431. break;
  432. #endif
  433. #ifdef WANT_SSE2_4WAY
  434. case ALGO_4WAY: {
  435. unsigned int rc4 =
  436. ScanHash_4WaySSE2(work.midstate, work.data + 64,
  437. work.hash1, work.hash,
  438. work.target,
  439. max_nonce, &hashes_done);
  440. rc = (rc4 == -1) ? false : true;
  441. }
  442. break;
  443. #endif
  444. #ifdef WANT_VIA_PADLOCK
  445. case ALGO_VIA:
  446. rc = scanhash_via(work.data, work.target,
  447. max_nonce, &hashes_done);
  448. break;
  449. #endif
  450. case ALGO_CRYPTOPP:
  451. rc = scanhash_cryptopp(work.midstate, work.data + 64,
  452. work.hash1, work.hash, work.target,
  453. max_nonce, &hashes_done);
  454. break;
  455. #ifdef WANT_CRYPTOPP_ASM32
  456. case ALGO_CRYPTOPP_ASM32:
  457. rc = scanhash_asm32(work.midstate, work.data + 64,
  458. work.hash1, work.hash, work.target,
  459. max_nonce, &hashes_done);
  460. break;
  461. #endif
  462. default:
  463. /* should never happen */
  464. goto out;
  465. }
  466. /* record scanhash elapsed time */
  467. gettimeofday(&tv_end, NULL);
  468. timeval_subtract(&diff, &tv_end, &tv_start);
  469. hashmeter(thr_id, &diff, hashes_done);
  470. /* adjust max_nonce to meet target scan time */
  471. if (diff.tv_sec > (opt_scantime * 2))
  472. max_nonce /= 2; /* large decrease */
  473. else if ((diff.tv_sec > opt_scantime) &&
  474. (max_nonce > 1500000))
  475. max_nonce -= 1000000; /* small decrease */
  476. else if ((diff.tv_sec < opt_scantime) &&
  477. (max_nonce < 0xffffec76))
  478. max_nonce += 100000; /* small increase */
  479. /* if nonce found, submit work */
  480. if (rc && !submit_work(mythr, &work))
  481. break;
  482. }
  483. out:
  484. tq_freeze(mythr->q);
  485. return NULL;
  486. }
  487. static void show_usage(void)
  488. {
  489. int i;
  490. printf("minerd version %s\n\n", VERSION);
  491. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  492. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  493. struct option_help *h;
  494. h = &options_help[i];
  495. printf("--%s\n%s\n\n", h->name, h->helptext);
  496. }
  497. exit(1);
  498. }
  499. static void parse_arg (int key, char *arg)
  500. {
  501. int v, i;
  502. switch(key) {
  503. case 'a':
  504. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  505. if (algo_names[i] &&
  506. !strcmp(arg, algo_names[i])) {
  507. opt_algo = i;
  508. break;
  509. }
  510. }
  511. if (i == ARRAY_SIZE(algo_names))
  512. show_usage();
  513. break;
  514. case 'c': {
  515. json_error_t err;
  516. if (opt_config)
  517. json_decref(opt_config);
  518. opt_config = json_load_file(arg, &err);
  519. if (!json_is_object(opt_config)) {
  520. fprintf(stderr, "JSON decode of %s failed\n", arg);
  521. show_usage();
  522. }
  523. break;
  524. }
  525. case 'q':
  526. opt_quiet = true;
  527. break;
  528. case 'D':
  529. opt_debug = true;
  530. break;
  531. case 'P':
  532. opt_protocol = true;
  533. break;
  534. case 'r':
  535. v = atoi(arg);
  536. if (v < -1 || v > 9999) /* sanity check */
  537. show_usage();
  538. opt_retries = v;
  539. break;
  540. case 'R':
  541. v = atoi(arg);
  542. if (v < 1 || v > 9999) /* sanity check */
  543. show_usage();
  544. opt_fail_pause = v;
  545. break;
  546. case 's':
  547. v = atoi(arg);
  548. if (v < 1 || v > 9999) /* sanity check */
  549. show_usage();
  550. opt_scantime = v;
  551. break;
  552. case 't':
  553. v = atoi(arg);
  554. if (v < 1 || v > 9999) /* sanity check */
  555. show_usage();
  556. opt_n_threads = v;
  557. break;
  558. case 1001: /* --url */
  559. if (strncmp(arg, "http://", 7) &&
  560. strncmp(arg, "https://", 8))
  561. show_usage();
  562. free(rpc_url);
  563. rpc_url = strdup(arg);
  564. break;
  565. case 1002: /* --userpass */
  566. if (!strchr(arg, ':'))
  567. show_usage();
  568. free(userpass);
  569. userpass = strdup(arg);
  570. break;
  571. default:
  572. show_usage();
  573. }
  574. }
  575. static void parse_config(void)
  576. {
  577. int i;
  578. json_t *val;
  579. if (!json_is_object(opt_config))
  580. return;
  581. for (i = 0; i < ARRAY_SIZE(options); i++) {
  582. if (!options[i].name)
  583. break;
  584. if (!strcmp(options[i].name, "config"))
  585. continue;
  586. val = json_object_get(opt_config, options[i].name);
  587. if (!val)
  588. continue;
  589. if (options[i].has_arg && json_is_string(val)) {
  590. char *s = strdup(json_string_value(val));
  591. if (!s)
  592. break;
  593. parse_arg(options[i].val, s);
  594. free(s);
  595. } else if (!options[i].has_arg && json_is_true(val))
  596. parse_arg(options[i].val, "");
  597. else
  598. fprintf(stderr, "JSON option %s invalid\n",
  599. options[i].name);
  600. }
  601. }
  602. static void parse_cmdline(int argc, char *argv[])
  603. {
  604. int key;
  605. while (1) {
  606. key = getopt_long(argc, argv, "a:c:qDPr:s:t:h?", options, NULL);
  607. if (key < 0)
  608. break;
  609. parse_arg(key, optarg);
  610. }
  611. parse_config();
  612. }
  613. int main (int argc, char *argv[])
  614. {
  615. struct thr_info *thr;
  616. int i;
  617. rpc_url = strdup(DEF_RPC_URL);
  618. userpass = strdup(DEF_RPC_USERPASS);
  619. /* parse command line */
  620. parse_cmdline(argc, argv);
  621. /* set our priority to the highest (aka "nicest, least intrusive") */
  622. if (setpriority(PRIO_PROCESS, 0, 19))
  623. perror("setpriority");
  624. thr_info = calloc(opt_n_threads + 1, sizeof(*thr));
  625. if (!thr_info)
  626. return 1;
  627. work_thr_id = opt_n_threads;
  628. thr = &thr_info[work_thr_id];
  629. thr->id = opt_n_threads;
  630. thr->q = tq_new();
  631. if (!thr->q)
  632. return 1;
  633. /* start work I/O thread */
  634. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  635. fprintf(stderr, "workio thread create failed\n");
  636. return 1;
  637. }
  638. /* start mining threads */
  639. for (i = 0; i < opt_n_threads; i++) {
  640. thr = &thr_info[i];
  641. thr->id = i;
  642. thr->q = tq_new();
  643. if (!thr->q)
  644. return 1;
  645. if (pthread_create(&thr->pth, NULL, miner_thread, thr)) {
  646. fprintf(stderr, "thread %d create failed\n", i);
  647. return 1;
  648. }
  649. sleep(1); /* don't pound RPC server all at once */
  650. }
  651. fprintf(stderr, "%d miner threads started, "
  652. "using SHA256 '%s' algorithm.\n",
  653. opt_n_threads,
  654. algo_names[opt_algo]);
  655. /* main loop - simply wait for workio thread to exit */
  656. pthread_join(thr_info[work_thr_id].pth, NULL);
  657. fprintf(stderr, "workio thread dead, exiting.\n");
  658. return 0;
  659. }