cpu-miner.c 34 KB

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