cpu-miner.c 33 KB

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