cpu-miner.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. #define _GNU_SOURCE
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <unistd.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #ifndef WIN32
  20. #include <sys/resource.h>
  21. #endif
  22. #include <getopt.h>
  23. #include <jansson.h>
  24. #include <curl/curl.h>
  25. #include "compat.h"
  26. #include "miner.h"
  27. #define PROGRAM_NAME "minerd"
  28. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  29. #define DEF_RPC_USERNAME "rpcuser"
  30. #define DEF_RPC_PASSWORD "rpcpass"
  31. #define DEF_RPC_USERPASS DEF_RPC_USERNAME ":" DEF_RPC_PASSWORD
  32. #ifdef __linux /* Linux specific policy and affinity management */
  33. #include <sched.h>
  34. static inline void drop_policy(void)
  35. {
  36. struct sched_param param;
  37. if (unlikely(sched_setscheduler(0, SCHED_IDLE, &param) == -1))
  38. sched_setscheduler(0, SCHED_BATCH, &param);
  39. }
  40. static inline void affine_to_cpu(int id, int cpu)
  41. {
  42. cpu_set_t set;
  43. CPU_ZERO(&set);
  44. CPU_SET(cpu, &set);
  45. sched_setaffinity(0, sizeof(&set), &set);
  46. applog(LOG_INFO, "Binding thread %d to cpu %d", id, cpu);
  47. }
  48. #else
  49. static inline void drop_policy(void)
  50. {
  51. }
  52. static inline void affine_to_cpu(int id, int cpu)
  53. {
  54. }
  55. #endif
  56. enum workio_commands {
  57. WC_GET_WORK,
  58. WC_SUBMIT_WORK,
  59. };
  60. struct workio_cmd {
  61. enum workio_commands cmd;
  62. struct thr_info *thr;
  63. union {
  64. struct work *work;
  65. } u;
  66. };
  67. enum sha256_algos {
  68. ALGO_C, /* plain C */
  69. ALGO_4WAY, /* parallel SSE2 */
  70. ALGO_VIA, /* VIA padlock */
  71. ALGO_CRYPTOPP, /* Crypto++ (C) */
  72. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  73. ALGO_SSE2_64, /* SSE2 for x86_64 */
  74. };
  75. static const char *algo_names[] = {
  76. [ALGO_C] = "c",
  77. #ifdef WANT_SSE2_4WAY
  78. [ALGO_4WAY] = "4way",
  79. #endif
  80. #ifdef WANT_VIA_PADLOCK
  81. [ALGO_VIA] = "via",
  82. #endif
  83. [ALGO_CRYPTOPP] = "cryptopp",
  84. #ifdef WANT_CRYPTOPP_ASM32
  85. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  86. #endif
  87. #ifdef WANT_X8664_SSE2
  88. [ALGO_SSE2_64] = "sse2_64",
  89. #endif
  90. };
  91. bool opt_debug = false;
  92. bool opt_protocol = false;
  93. bool want_longpoll = true;
  94. bool have_longpoll = false;
  95. bool use_syslog = false;
  96. static bool opt_quiet = false;
  97. static int opt_retries = 10;
  98. static int opt_fail_pause = 30;
  99. int opt_scantime = 5;
  100. static json_t *opt_config;
  101. static const bool opt_time = true;
  102. #ifdef WANT_X8664_SSE2
  103. static enum sha256_algos opt_algo = ALGO_SSE2_64;
  104. #else
  105. static enum sha256_algos opt_algo = ALGO_C;
  106. #endif
  107. static int opt_n_threads;
  108. static int num_processors;
  109. static char *rpc_url;
  110. static char *rpc_userpass;
  111. static char *rpc_user, *rpc_pass;
  112. struct thr_info *thr_info;
  113. static int work_thr_id;
  114. int longpoll_thr_id;
  115. struct work_restart *work_restart = NULL;
  116. pthread_mutex_t time_lock;
  117. struct option_help {
  118. const char *name;
  119. const char *helptext;
  120. };
  121. static struct option_help options_help[] = {
  122. { "help",
  123. "(-h) Display this help text" },
  124. { "config FILE",
  125. "(-c FILE) JSON-format configuration file (default: none)\n"
  126. "See example-cfg.json for an example configuration." },
  127. { "algo XXX",
  128. "(-a XXX) Specify sha256 implementation:\n"
  129. "\tc\t\tLinux kernel sha256, implemented in C (default)"
  130. #ifdef WANT_SSE2_4WAY
  131. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  132. #endif
  133. #ifdef WANT_VIA_PADLOCK
  134. "\n\tvia\t\tVIA padlock implementation"
  135. #endif
  136. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  137. #ifdef WANT_CRYPTOPP_ASM32
  138. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  139. #endif
  140. #ifdef WANT_X8664_SSE2
  141. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  142. #endif
  143. },
  144. { "quiet",
  145. "(-q) Disable per-thread hashmeter output (default: off)" },
  146. { "debug",
  147. "(-D) Enable debug output (default: off)" },
  148. { "no-longpoll",
  149. "Disable X-Long-Polling support (default: enabled)" },
  150. { "protocol-dump",
  151. "(-P) Verbose dump of protocol-level activities (default: off)" },
  152. { "retries N",
  153. "(-r N) Number of times to retry, if JSON-RPC call fails\n"
  154. "\t(default: 10; use -1 for \"never\")" },
  155. { "retry-pause N",
  156. "(-R N) Number of seconds to pause, between retries\n"
  157. "\t(default: 30)" },
  158. { "scantime N",
  159. "(-s N) Upper bound on time spent scanning current work,\n"
  160. "\tin seconds. (default: 5)" },
  161. #ifdef HAVE_SYSLOG_H
  162. { "syslog",
  163. "Use system log for output messages (default: standard error)" },
  164. #endif
  165. { "threads N",
  166. "(-t N) Number of miner threads (default: 1)" },
  167. { "url URL",
  168. "URL for bitcoin JSON-RPC server "
  169. "(default: " DEF_RPC_URL ")" },
  170. { "userpass USERNAME:PASSWORD",
  171. "Username:Password pair for bitcoin JSON-RPC server "
  172. "(default: " DEF_RPC_USERPASS ")" },
  173. { "user USERNAME",
  174. "(-u USERNAME) Username for bitcoin JSON-RPC server "
  175. "(default: " DEF_RPC_USERNAME ")" },
  176. { "pass PASSWORD",
  177. "(-p PASSWORD) Password for bitcoin JSON-RPC server "
  178. "(default: " DEF_RPC_PASSWORD ")" },
  179. };
  180. static struct option options[] = {
  181. { "algo", 1, NULL, 'a' },
  182. { "config", 1, NULL, 'c' },
  183. { "debug", 0, NULL, 'D' },
  184. { "help", 0, NULL, 'h' },
  185. { "no-longpoll", 0, NULL, 1003 },
  186. { "pass", 1, NULL, 'p' },
  187. { "protocol-dump", 0, NULL, 'P' },
  188. { "quiet", 0, NULL, 'q' },
  189. { "threads", 1, NULL, 't' },
  190. { "retries", 1, NULL, 'r' },
  191. { "retry-pause", 1, NULL, 'R' },
  192. { "scantime", 1, NULL, 's' },
  193. #ifdef HAVE_SYSLOG_H
  194. { "syslog", 0, NULL, 1004 },
  195. #endif
  196. { "url", 1, NULL, 1001 },
  197. { "user", 1, NULL, 'u' },
  198. { "userpass", 1, NULL, 1002 },
  199. { }
  200. };
  201. struct work {
  202. unsigned char data[128];
  203. unsigned char hash1[64];
  204. unsigned char midstate[32];
  205. unsigned char target[32];
  206. unsigned char hash[32];
  207. };
  208. static bool jobj_binary(const json_t *obj, const char *key,
  209. void *buf, size_t buflen)
  210. {
  211. const char *hexstr;
  212. json_t *tmp;
  213. tmp = json_object_get(obj, key);
  214. if (unlikely(!tmp)) {
  215. applog(LOG_ERR, "JSON key '%s' not found", key);
  216. return false;
  217. }
  218. hexstr = json_string_value(tmp);
  219. if (unlikely(!hexstr)) {
  220. applog(LOG_ERR, "JSON key '%s' is not a string", key);
  221. return false;
  222. }
  223. if (!hex2bin(buf, hexstr, buflen))
  224. return false;
  225. return true;
  226. }
  227. static bool work_decode(const json_t *val, struct work *work)
  228. {
  229. if (unlikely(!jobj_binary(val, "midstate",
  230. work->midstate, sizeof(work->midstate)))) {
  231. applog(LOG_ERR, "JSON inval midstate");
  232. goto err_out;
  233. }
  234. if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data)))) {
  235. applog(LOG_ERR, "JSON inval data");
  236. goto err_out;
  237. }
  238. if (unlikely(!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1)))) {
  239. applog(LOG_ERR, "JSON inval hash1");
  240. goto err_out;
  241. }
  242. if (unlikely(!jobj_binary(val, "target", work->target, sizeof(work->target)))) {
  243. applog(LOG_ERR, "JSON inval target");
  244. goto err_out;
  245. }
  246. memset(work->hash, 0, sizeof(work->hash));
  247. return true;
  248. err_out:
  249. return false;
  250. }
  251. static bool submit_upstream_work(CURL *curl, const struct work *work)
  252. {
  253. char *hexstr = NULL;
  254. json_t *val, *res;
  255. char s[345];
  256. bool rc = false;
  257. /* build hex string */
  258. hexstr = bin2hex(work->data, sizeof(work->data));
  259. if (unlikely(!hexstr)) {
  260. applog(LOG_ERR, "submit_upstream_work OOM");
  261. goto out;
  262. }
  263. /* build JSON-RPC request */
  264. sprintf(s,
  265. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  266. hexstr);
  267. if (opt_debug)
  268. applog(LOG_DEBUG, "DBG: sending RPC call: %s", s);
  269. /* issue JSON-RPC request */
  270. val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false);
  271. if (unlikely(!val)) {
  272. applog(LOG_ERR, "submit_upstream_work json_rpc_call failed");
  273. goto out;
  274. }
  275. res = json_object_get(val, "result");
  276. applog(LOG_INFO, "PROOF OF WORK RESULT: %s",
  277. json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
  278. json_decref(val);
  279. rc = true;
  280. out:
  281. free(hexstr);
  282. return rc;
  283. }
  284. static const char *rpc_req =
  285. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  286. static bool get_upstream_work(CURL *curl, struct work *work)
  287. {
  288. json_t *val;
  289. bool rc;
  290. val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
  291. want_longpoll, false);
  292. if (!val)
  293. return false;
  294. rc = work_decode(json_object_get(val, "result"), work);
  295. json_decref(val);
  296. return rc;
  297. }
  298. static void workio_cmd_free(struct workio_cmd *wc)
  299. {
  300. if (!wc)
  301. return;
  302. switch (wc->cmd) {
  303. case WC_SUBMIT_WORK:
  304. free(wc->u.work);
  305. break;
  306. default: /* do nothing */
  307. break;
  308. }
  309. memset(wc, 0, sizeof(*wc)); /* poison */
  310. free(wc);
  311. }
  312. static bool workio_get_work(struct workio_cmd *wc, CURL *curl)
  313. {
  314. struct work *ret_work;
  315. int failures = 0;
  316. ret_work = calloc(1, sizeof(*ret_work));
  317. if (!ret_work)
  318. return false;
  319. /* obtain new work from bitcoin via JSON-RPC */
  320. while (!get_upstream_work(curl, ret_work)) {
  321. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  322. applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
  323. free(ret_work);
  324. return false;
  325. }
  326. /* pause, then restart work-request loop */
  327. applog(LOG_ERR, "json_rpc_call failed, retry after %d seconds",
  328. opt_fail_pause);
  329. sleep(opt_fail_pause);
  330. }
  331. /* send work to requesting thread */
  332. if (!tq_push(wc->thr->q, ret_work))
  333. free(ret_work);
  334. return true;
  335. }
  336. static bool workio_submit_work(struct workio_cmd *wc, CURL *curl)
  337. {
  338. int failures = 0;
  339. /* submit solution to bitcoin via JSON-RPC */
  340. while (!submit_upstream_work(curl, wc->u.work)) {
  341. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  342. applog(LOG_ERR, "...terminating workio thread");
  343. return false;
  344. }
  345. /* pause, then restart work-request loop */
  346. applog(LOG_ERR, "...retry after %d seconds",
  347. opt_fail_pause);
  348. sleep(opt_fail_pause);
  349. }
  350. return true;
  351. }
  352. static void *workio_thread(void *userdata)
  353. {
  354. struct thr_info *mythr = userdata;
  355. CURL *curl;
  356. bool ok = true;
  357. curl = curl_easy_init();
  358. if (unlikely(!curl)) {
  359. applog(LOG_ERR, "CURL initialization failed");
  360. return NULL;
  361. }
  362. while (ok) {
  363. struct workio_cmd *wc;
  364. /* wait for workio_cmd sent to us, on our queue */
  365. wc = tq_pop(mythr->q, NULL);
  366. if (!wc) {
  367. ok = false;
  368. break;
  369. }
  370. /* process workio_cmd */
  371. switch (wc->cmd) {
  372. case WC_GET_WORK:
  373. ok = workio_get_work(wc, curl);
  374. break;
  375. case WC_SUBMIT_WORK:
  376. ok = workio_submit_work(wc, curl);
  377. break;
  378. default: /* should never happen */
  379. ok = false;
  380. break;
  381. }
  382. workio_cmd_free(wc);
  383. }
  384. tq_freeze(mythr->q);
  385. curl_easy_cleanup(curl);
  386. return NULL;
  387. }
  388. static void hashmeter(int thr_id, const struct timeval *diff,
  389. unsigned long hashes_done)
  390. {
  391. double khashes, secs;
  392. khashes = hashes_done / 1000.0;
  393. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  394. if (!opt_quiet)
  395. applog(LOG_INFO, "thread %d: %lu hashes, %.2f khash/sec",
  396. thr_id, hashes_done,
  397. khashes / secs);
  398. }
  399. static bool get_work(struct thr_info *thr, struct work *work)
  400. {
  401. struct workio_cmd *wc;
  402. struct work *work_heap;
  403. /* fill out work request message */
  404. wc = calloc(1, sizeof(*wc));
  405. if (!wc)
  406. return false;
  407. wc->cmd = WC_GET_WORK;
  408. wc->thr = thr;
  409. /* send work request to workio thread */
  410. if (!tq_push(thr_info[work_thr_id].q, wc)) {
  411. workio_cmd_free(wc);
  412. return false;
  413. }
  414. /* wait for response, a unit of work */
  415. work_heap = tq_pop(thr->q, NULL);
  416. if (!work_heap)
  417. return false;
  418. /* copy returned work into storage provided by caller */
  419. memcpy(work, work_heap, sizeof(*work));
  420. free(work_heap);
  421. return true;
  422. }
  423. static bool submit_work(struct thr_info *thr, const struct work *work_in)
  424. {
  425. struct workio_cmd *wc;
  426. /* fill out work request message */
  427. wc = calloc(1, sizeof(*wc));
  428. if (!wc)
  429. return false;
  430. wc->u.work = malloc(sizeof(*work_in));
  431. if (!wc->u.work)
  432. goto err_out;
  433. wc->cmd = WC_SUBMIT_WORK;
  434. wc->thr = thr;
  435. memcpy(wc->u.work, work_in, sizeof(*work_in));
  436. /* send solution to workio thread */
  437. if (!tq_push(thr_info[work_thr_id].q, wc))
  438. goto err_out;
  439. return true;
  440. err_out:
  441. workio_cmd_free(wc);
  442. return false;
  443. }
  444. static void *miner_thread(void *userdata)
  445. {
  446. struct thr_info *mythr = userdata;
  447. int thr_id = mythr->id;
  448. uint32_t max_nonce = 0xffffff;
  449. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  450. * and if that fails, then SCHED_BATCH. No need for this to be an
  451. * error if it fails */
  452. setpriority(PRIO_PROCESS, 0, 19);
  453. drop_policy();
  454. /* Cpu affinity only makes sense if the number of threads is a multiple
  455. * of the number of CPUs */
  456. if (!(opt_n_threads % num_processors))
  457. affine_to_cpu(mythr->id, mythr->id % num_processors);
  458. while (1) {
  459. struct work work __attribute__((aligned(128)));
  460. unsigned long hashes_done;
  461. struct timeval tv_start, tv_end, diff;
  462. uint64_t max64;
  463. bool rc;
  464. /* obtain new work from internal workio thread */
  465. if (unlikely(!get_work(mythr, &work))) {
  466. applog(LOG_ERR, "work retrieval failed, exiting "
  467. "mining thread %d", mythr->id);
  468. goto out;
  469. }
  470. hashes_done = 0;
  471. gettimeofday(&tv_start, NULL);
  472. /* scan nonces for a proof-of-work hash */
  473. switch (opt_algo) {
  474. case ALGO_C:
  475. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  476. work.hash1, work.hash, work.target,
  477. max_nonce, &hashes_done);
  478. break;
  479. #ifdef WANT_X8664_SSE2
  480. case ALGO_SSE2_64: {
  481. unsigned int rc5 =
  482. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  483. work.hash1, work.hash,
  484. work.target,
  485. max_nonce, &hashes_done);
  486. rc = (rc5 == -1) ? false : true;
  487. }
  488. break;
  489. #endif
  490. #ifdef WANT_SSE2_4WAY
  491. case ALGO_4WAY: {
  492. unsigned int rc4 =
  493. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  494. work.hash1, work.hash,
  495. work.target,
  496. max_nonce, &hashes_done);
  497. rc = (rc4 == -1) ? false : true;
  498. }
  499. break;
  500. #endif
  501. #ifdef WANT_VIA_PADLOCK
  502. case ALGO_VIA:
  503. rc = scanhash_via(thr_id, work.data, work.target,
  504. max_nonce, &hashes_done);
  505. break;
  506. #endif
  507. case ALGO_CRYPTOPP:
  508. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  509. work.hash1, work.hash, work.target,
  510. max_nonce, &hashes_done);
  511. break;
  512. #ifdef WANT_CRYPTOPP_ASM32
  513. case ALGO_CRYPTOPP_ASM32:
  514. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  515. work.hash1, work.hash, work.target,
  516. max_nonce, &hashes_done);
  517. break;
  518. #endif
  519. default:
  520. /* should never happen */
  521. goto out;
  522. }
  523. /* record scanhash elapsed time */
  524. gettimeofday(&tv_end, NULL);
  525. timeval_subtract(&diff, &tv_end, &tv_start);
  526. hashmeter(thr_id, &diff, hashes_done);
  527. /* adjust max_nonce to meet target scan time */
  528. if (diff.tv_usec > 500000)
  529. diff.tv_sec++;
  530. if (diff.tv_sec > 0) {
  531. max64 =
  532. ((uint64_t)hashes_done * opt_scantime) / diff.tv_sec;
  533. if (max64 > 0xfffffffaULL)
  534. max64 = 0xfffffffaULL;
  535. max_nonce = max64;
  536. }
  537. /* if nonce found, submit work */
  538. if (rc && !submit_work(mythr, &work))
  539. break;
  540. }
  541. out:
  542. tq_freeze(mythr->q);
  543. return NULL;
  544. }
  545. static void restart_threads(void)
  546. {
  547. int i;
  548. for (i = 0; i < opt_n_threads; i++)
  549. work_restart[i].restart = 1;
  550. }
  551. static void *longpoll_thread(void *userdata)
  552. {
  553. struct thr_info *mythr = userdata;
  554. CURL *curl = NULL;
  555. char *copy_start, *hdr_path, *lp_url = NULL;
  556. bool need_slash = false;
  557. int failures = 0;
  558. hdr_path = tq_pop(mythr->q, NULL);
  559. if (!hdr_path)
  560. goto out;
  561. /* full URL */
  562. if (strstr(hdr_path, "://")) {
  563. lp_url = hdr_path;
  564. hdr_path = NULL;
  565. }
  566. /* absolute path, on current server */
  567. else {
  568. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  569. if (rpc_url[strlen(rpc_url) - 1] != '/')
  570. need_slash = true;
  571. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  572. if (!lp_url)
  573. goto out;
  574. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  575. }
  576. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  577. curl = curl_easy_init();
  578. if (unlikely(!curl)) {
  579. applog(LOG_ERR, "CURL initialization failed");
  580. goto out;
  581. }
  582. while (1) {
  583. json_t *val;
  584. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  585. false, true);
  586. if (likely(val)) {
  587. failures = 0;
  588. json_decref(val);
  589. applog(LOG_INFO, "LONGPOLL detected new block");
  590. restart_threads();
  591. } else {
  592. if (failures++ < 10) {
  593. sleep(30);
  594. applog(LOG_ERR,
  595. "longpoll failed, sleeping for 30s");
  596. } else {
  597. applog(LOG_ERR,
  598. "longpoll failed, ending thread");
  599. goto out;
  600. }
  601. }
  602. }
  603. out:
  604. free(hdr_path);
  605. free(lp_url);
  606. tq_freeze(mythr->q);
  607. if (curl)
  608. curl_easy_cleanup(curl);
  609. return NULL;
  610. }
  611. static void show_usage(void)
  612. {
  613. int i;
  614. printf("minerd version %s\n\n", VERSION);
  615. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  616. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  617. struct option_help *h;
  618. h = &options_help[i];
  619. printf("--%s\n%s\n\n", h->name, h->helptext);
  620. }
  621. exit(1);
  622. }
  623. static void parse_arg (int key, char *arg)
  624. {
  625. int v, i;
  626. switch(key) {
  627. case 'a':
  628. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  629. if (algo_names[i] &&
  630. !strcmp(arg, algo_names[i])) {
  631. opt_algo = i;
  632. break;
  633. }
  634. }
  635. if (i == ARRAY_SIZE(algo_names))
  636. show_usage();
  637. break;
  638. case 'c': {
  639. json_error_t err;
  640. if (opt_config)
  641. json_decref(opt_config);
  642. opt_config = json_load_file(arg, &err);
  643. if (!json_is_object(opt_config)) {
  644. applog(LOG_ERR, "JSON decode of %s failed", arg);
  645. show_usage();
  646. }
  647. break;
  648. }
  649. case 'q':
  650. opt_quiet = true;
  651. break;
  652. case 'D':
  653. opt_debug = true;
  654. break;
  655. case 'p':
  656. free(rpc_pass);
  657. rpc_pass = strdup(arg);
  658. break;
  659. case 'P':
  660. opt_protocol = true;
  661. break;
  662. case 'r':
  663. v = atoi(arg);
  664. if (v < -1 || v > 9999) /* sanity check */
  665. show_usage();
  666. opt_retries = v;
  667. break;
  668. case 'R':
  669. v = atoi(arg);
  670. if (v < 1 || v > 9999) /* sanity check */
  671. show_usage();
  672. opt_fail_pause = v;
  673. break;
  674. case 's':
  675. v = atoi(arg);
  676. if (v < 1 || v > 9999) /* sanity check */
  677. show_usage();
  678. opt_scantime = v;
  679. break;
  680. case 't':
  681. v = atoi(arg);
  682. if (v < 1 || v > 9999) /* sanity check */
  683. show_usage();
  684. opt_n_threads = v;
  685. break;
  686. case 'u':
  687. free(rpc_user);
  688. rpc_user = strdup(arg);
  689. break;
  690. case 1001: /* --url */
  691. if (strncmp(arg, "http://", 7) &&
  692. strncmp(arg, "https://", 8))
  693. show_usage();
  694. free(rpc_url);
  695. rpc_url = strdup(arg);
  696. break;
  697. case 1002: /* --userpass */
  698. if (!strchr(arg, ':'))
  699. show_usage();
  700. free(rpc_userpass);
  701. rpc_userpass = strdup(arg);
  702. break;
  703. case 1003:
  704. want_longpoll = false;
  705. break;
  706. case 1004:
  707. use_syslog = true;
  708. break;
  709. default:
  710. show_usage();
  711. }
  712. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  713. if (!opt_n_threads)
  714. opt_n_threads = num_processors;
  715. }
  716. static void parse_config(void)
  717. {
  718. int i;
  719. json_t *val;
  720. if (!json_is_object(opt_config))
  721. return;
  722. for (i = 0; i < ARRAY_SIZE(options); i++) {
  723. if (!options[i].name)
  724. break;
  725. if (!strcmp(options[i].name, "config"))
  726. continue;
  727. val = json_object_get(opt_config, options[i].name);
  728. if (!val)
  729. continue;
  730. if (options[i].has_arg && json_is_string(val)) {
  731. char *s = strdup(json_string_value(val));
  732. if (!s)
  733. break;
  734. parse_arg(options[i].val, s);
  735. free(s);
  736. } else if (!options[i].has_arg && json_is_true(val))
  737. parse_arg(options[i].val, "");
  738. else
  739. applog(LOG_ERR, "JSON option %s invalid",
  740. options[i].name);
  741. }
  742. }
  743. static void parse_cmdline(int argc, char *argv[])
  744. {
  745. int key;
  746. while (1) {
  747. key = getopt_long(argc, argv, "a:c:qDPr:s:t:h?", options, NULL);
  748. if (key < 0)
  749. break;
  750. parse_arg(key, optarg);
  751. }
  752. parse_config();
  753. }
  754. int main (int argc, char *argv[])
  755. {
  756. struct thr_info *thr;
  757. int i;
  758. rpc_url = strdup(DEF_RPC_URL);
  759. /* parse command line */
  760. parse_cmdline(argc, argv);
  761. if (!rpc_userpass) {
  762. if (!rpc_user || !rpc_pass) {
  763. applog(LOG_ERR, "No login credentials supplied");
  764. return 1;
  765. }
  766. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  767. if (!rpc_userpass)
  768. return 1;
  769. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  770. }
  771. pthread_mutex_init(&time_lock, NULL);
  772. #ifdef HAVE_SYSLOG_H
  773. if (use_syslog)
  774. openlog("cpuminer", LOG_PID, LOG_USER);
  775. #endif
  776. work_restart = calloc(opt_n_threads, sizeof(*work_restart));
  777. if (!work_restart)
  778. return 1;
  779. thr_info = calloc(opt_n_threads + 2, sizeof(*thr));
  780. if (!thr_info)
  781. return 1;
  782. /* init workio thread info */
  783. work_thr_id = opt_n_threads;
  784. thr = &thr_info[work_thr_id];
  785. thr->id = work_thr_id;
  786. thr->q = tq_new();
  787. if (!thr->q)
  788. return 1;
  789. /* start work I/O thread */
  790. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  791. applog(LOG_ERR, "workio thread create failed");
  792. return 1;
  793. }
  794. /* init longpoll thread info */
  795. if (want_longpoll) {
  796. longpoll_thr_id = opt_n_threads + 1;
  797. thr = &thr_info[longpoll_thr_id];
  798. thr->id = longpoll_thr_id;
  799. thr->q = tq_new();
  800. if (!thr->q)
  801. return 1;
  802. /* start longpoll thread */
  803. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  804. applog(LOG_ERR, "longpoll thread create failed");
  805. return 1;
  806. }
  807. } else
  808. longpoll_thr_id = -1;
  809. /* start mining threads */
  810. for (i = 0; i < opt_n_threads; i++) {
  811. thr = &thr_info[i];
  812. thr->id = i;
  813. thr->q = tq_new();
  814. if (!thr->q)
  815. return 1;
  816. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  817. applog(LOG_ERR, "thread %d create failed", i);
  818. return 1;
  819. }
  820. sleep(1); /* don't pound RPC server all at once */
  821. }
  822. applog(LOG_INFO, "%d miner threads started, "
  823. "using SHA256 '%s' algorithm.",
  824. opt_n_threads,
  825. algo_names[opt_algo]);
  826. /* main loop - simply wait for workio thread to exit */
  827. pthread_join(thr_info[work_thr_id].pth, NULL);
  828. applog(LOG_INFO, "workio thread dead, exiting.");
  829. return 0;
  830. }