main.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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 "config.h"
  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. #include <math.h>
  20. #include <stdarg.h>
  21. #include <assert.h>
  22. #ifndef WIN32
  23. #include <sys/resource.h>
  24. #endif
  25. #include <ccan/opt/opt.h>
  26. #include <jansson.h>
  27. #include <curl/curl.h>
  28. #include "compat.h"
  29. #include "miner.h"
  30. #include "findnonce.h"
  31. #include "ocl.h"
  32. #define PROGRAM_NAME "cgminer"
  33. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  34. #define DEF_RPC_USERNAME "rpcuser"
  35. #define DEF_RPC_PASSWORD "rpcpass"
  36. #define DEF_RPC_USERPASS DEF_RPC_USERNAME ":" DEF_RPC_PASSWORD
  37. #ifdef __linux /* Linux specific policy and affinity management */
  38. #include <sched.h>
  39. static inline void drop_policy(void)
  40. {
  41. struct sched_param param;
  42. #ifdef SCHED_BATCH
  43. #ifdef SCHED_IDLE
  44. if (unlikely(sched_setscheduler(0, SCHED_IDLE, &param) == -1))
  45. #endif
  46. sched_setscheduler(0, SCHED_BATCH, &param);
  47. #endif
  48. }
  49. static inline void affine_to_cpu(int id, int cpu)
  50. {
  51. cpu_set_t set;
  52. CPU_ZERO(&set);
  53. CPU_SET(cpu, &set);
  54. sched_setaffinity(0, sizeof(&set), &set);
  55. applog(LOG_INFO, "Binding cpu mining thread %d to cpu %d", id, cpu);
  56. }
  57. #else
  58. static inline void drop_policy(void)
  59. {
  60. }
  61. static inline void affine_to_cpu(int id, int cpu)
  62. {
  63. }
  64. #endif
  65. enum workio_commands {
  66. WC_GET_WORK,
  67. WC_SUBMIT_WORK,
  68. WC_DIE,
  69. };
  70. struct workio_cmd {
  71. enum workio_commands cmd;
  72. struct thr_info *thr;
  73. union {
  74. struct work *work;
  75. } u;
  76. };
  77. enum sha256_algos {
  78. ALGO_C, /* plain C */
  79. ALGO_4WAY, /* parallel SSE2 */
  80. ALGO_VIA, /* VIA padlock */
  81. ALGO_CRYPTOPP, /* Crypto++ (C) */
  82. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  83. ALGO_SSE2_64, /* SSE2 for x86_64 */
  84. };
  85. static const char *algo_names[] = {
  86. [ALGO_C] = "c",
  87. #ifdef WANT_SSE2_4WAY
  88. [ALGO_4WAY] = "4way",
  89. #endif
  90. #ifdef WANT_VIA_PADLOCK
  91. [ALGO_VIA] = "via",
  92. #endif
  93. [ALGO_CRYPTOPP] = "cryptopp",
  94. #ifdef WANT_CRYPTOPP_ASM32
  95. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  96. #endif
  97. #ifdef WANT_X8664_SSE2
  98. [ALGO_SSE2_64] = "sse2_64",
  99. #endif
  100. };
  101. bool opt_debug = false;
  102. bool opt_protocol = false;
  103. bool want_longpoll = true;
  104. bool have_longpoll = false;
  105. bool use_syslog = false;
  106. static bool opt_quiet = false;
  107. static int opt_retries = -1;
  108. static int opt_fail_pause = 5;
  109. static int opt_log_interval = 5;
  110. bool opt_log_output = false;
  111. static int opt_queue = 1;
  112. int opt_vectors;
  113. int opt_worksize;
  114. int opt_scantime = 60;
  115. static const bool opt_time = true;
  116. #ifdef WANT_X8664_SSE2
  117. static enum sha256_algos opt_algo = ALGO_SSE2_64;
  118. #else
  119. static enum sha256_algos opt_algo = ALGO_C;
  120. #endif
  121. static int nDevs;
  122. static int opt_g_threads = 2;
  123. static int gpu_threads;
  124. static bool forced_n_threads;
  125. static int opt_n_threads;
  126. static int num_processors;
  127. static int scan_intensity = 4;
  128. static char *rpc_url;
  129. static char *rpc_userpass;
  130. static char *rpc_user, *rpc_pass;
  131. struct thr_info *thr_info;
  132. static int work_thr_id;
  133. int longpoll_thr_id;
  134. static int stage_thr_id;
  135. struct work_restart *work_restart = NULL;
  136. pthread_mutex_t time_lock;
  137. static pthread_mutex_t hash_lock;
  138. static pthread_mutex_t qd_lock;
  139. static pthread_mutex_t stgd_lock;
  140. static double total_mhashes_done;
  141. static struct timeval total_tv_start, total_tv_end;
  142. static int accepted, rejected;
  143. int hw_errors;
  144. static int total_queued, total_staged, lp_staged;
  145. static bool localgen = false;
  146. static unsigned int getwork_requested = 0;
  147. static char current_block[37];
  148. static char longpoll_block[37];
  149. static char blank[37];
  150. static void applog_and_exit(const char *fmt, ...)
  151. {
  152. va_list ap;
  153. va_start(ap, fmt);
  154. vapplog(LOG_ERR, fmt, ap);
  155. va_end(ap);
  156. exit(1);
  157. }
  158. /* FIXME: Use asprintf for better errors. */
  159. static char *set_algo(const char *arg, enum sha256_algos *algo)
  160. {
  161. enum sha256_algos i;
  162. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  163. if (algo_names[i] && !strcmp(arg, algo_names[i])) {
  164. *algo = i;
  165. return NULL;
  166. }
  167. }
  168. return "Unknown algorithm";
  169. }
  170. static void show_algo(char buf[OPT_SHOW_LEN], const enum sha256_algos *algo)
  171. {
  172. strncpy(buf, algo_names[*algo], OPT_SHOW_LEN);
  173. }
  174. static char *set_int_range(const char *arg, int *i, int min, int max)
  175. {
  176. char *err = opt_set_intval(arg, i);
  177. if (err)
  178. return err;
  179. if (*i < min || *i > max)
  180. return "Value out of range";
  181. return NULL;
  182. }
  183. static char *set_int_0_to_9999(const char *arg, int *i)
  184. {
  185. return set_int_range(arg, i, 0, 9999);
  186. }
  187. static char *set_int_0_to_14(const char *arg, int *i)
  188. {
  189. return set_int_range(arg, i, 0, 14);
  190. }
  191. static char *force_nthreads_int(const char *arg, int *i)
  192. {
  193. forced_n_threads = true;
  194. return set_int_range(arg, i, 0, 9999);
  195. }
  196. static char *set_int_0_to_10(const char *arg, int *i)
  197. {
  198. return set_int_range(arg, i, 0, 10);
  199. }
  200. static char *set_int_1_to_10(const char *arg, int *i)
  201. {
  202. return set_int_range(arg, i, 1, 10);
  203. }
  204. static char *set_url(const char *arg, char **p)
  205. {
  206. opt_set_charp(arg, p);
  207. if (strncmp(arg, "http://", 7) &&
  208. strncmp(arg, "https://", 8))
  209. return "URL must start with http:// or https://";
  210. return NULL;
  211. }
  212. static char *set_vector(const char *arg, int *i)
  213. {
  214. char *err = opt_set_intval(arg, i);
  215. if (err)
  216. return err;
  217. if (*i != 1 && *i != 2 && *i != 4)
  218. return "Valid vectors are 1, 2 or 4";
  219. return NULL;
  220. }
  221. static char *enable_debug(bool *flag)
  222. {
  223. *flag = true;
  224. /* Turn out verbose output, too. */
  225. opt_log_output = true;
  226. return NULL;
  227. }
  228. /* These options are available from config file or commandline */
  229. static struct opt_table opt_config_table[] = {
  230. OPT_WITH_ARG("--algo|-a",
  231. set_algo, show_algo, &opt_algo,
  232. "Specify sha256 implementation:\n"
  233. "\tc\t\tLinux kernel sha256, implemented in C"
  234. #ifdef WANT_SSE2_4WAY
  235. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  236. #endif
  237. #ifdef WANT_VIA_PADLOCK
  238. "\n\tvia\t\tVIA padlock implementation"
  239. #endif
  240. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  241. #ifdef WANT_CRYPTOPP_ASM32
  242. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  243. #endif
  244. #ifdef WANT_X8664_SSE2
  245. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  246. #endif
  247. ),
  248. OPT_WITH_ARG("--cpu-threads|-t",
  249. force_nthreads_int, opt_show_intval, &opt_n_threads,
  250. "Number of miner CPU threads"),
  251. OPT_WITHOUT_ARG("--debug|-D",
  252. enable_debug, &opt_debug,
  253. "Enable debug output"),
  254. #ifdef HAVE_OPENCL
  255. OPT_WITH_ARG("--gpu-threads|-g",
  256. set_int_0_to_10, opt_show_intval, &opt_g_threads,
  257. "Number of threads per GPU (0 - 10)"),
  258. OPT_WITH_ARG("--intensity|-I",
  259. set_int_0_to_14, opt_show_intval, &scan_intensity,
  260. "Intensity of GPU scanning (0 - 14)"),
  261. #endif
  262. OPT_WITH_ARG("--log|-l",
  263. set_int_0_to_9999, opt_show_intval, &opt_log_interval,
  264. "Interval in seconds between log output"),
  265. OPT_WITHOUT_ARG("--no-longpoll",
  266. opt_set_invbool, &want_longpoll,
  267. "Disable X-Long-Polling support"),
  268. OPT_WITH_ARG("--pass|-p",
  269. opt_set_charp, NULL, &rpc_pass,
  270. "Password for bitcoin JSON-RPC server"),
  271. OPT_WITHOUT_ARG("--protocol-dump|-P",
  272. opt_set_bool, &opt_protocol,
  273. "Verbose dump of protocol-level activities"),
  274. OPT_WITH_ARG("--queue|-Q",
  275. set_int_1_to_10, opt_show_intval, &opt_queue,
  276. "Number of extra work items to queue (1 - 10)"),
  277. OPT_WITHOUT_ARG("--quiet|-q",
  278. opt_set_bool, &opt_quiet,
  279. "Disable per-thread hashmeter output"),
  280. OPT_WITH_ARG("--retries|-r",
  281. opt_set_intval, opt_show_intval, &opt_retries,
  282. "Number of times to retry before giving up, if JSON-RPC call fails (-1 means never)"),
  283. OPT_WITH_ARG("--retry-pause|-R",
  284. set_int_0_to_9999, opt_show_intval, &opt_fail_pause,
  285. "Number of seconds to pause, between retries"),
  286. OPT_WITH_ARG("--scan-time|-s",
  287. set_int_0_to_9999, opt_show_intval, &opt_scantime,
  288. "Upper bound on time spent scanning current work, in seconds"),
  289. #ifdef HAVE_SYSLOG_H
  290. OPT_WITHOUT_ARG("--syslog",
  291. opt_set_bool, &use_syslog,
  292. "Use system log for output messages (default: standard error)"),
  293. #endif
  294. OPT_WITH_ARG("--url|-o",
  295. set_url, opt_show_charp, &rpc_url,
  296. "URL for bitcoin JSON-RPC server"),
  297. OPT_WITH_ARG("--user|-u",
  298. opt_set_charp, NULL, &rpc_user,
  299. "Username for bitcoin JSON-RPC server"),
  300. #ifdef HAVE_OPENCL
  301. OPT_WITH_ARG("--vectors|-v",
  302. set_vector, NULL, &opt_vectors,
  303. "Override detected optimal vector width (1, 2 or 4)"),
  304. #endif
  305. OPT_WITHOUT_ARG("--verbose",
  306. opt_set_bool, &opt_log_output,
  307. "Log verbose output to stderr as well as status output"),
  308. #ifdef HAVE_OPENCL
  309. OPT_WITH_ARG("--worksize|-w",
  310. set_int_0_to_9999, opt_show_intval, &opt_worksize,
  311. "Override detected optimal worksize"),
  312. #endif
  313. OPT_WITH_ARG("--userpass|-O",
  314. opt_set_charp, NULL, &rpc_userpass,
  315. "Username:Password pair for bitcoin JSON-RPC server"),
  316. OPT_ENDTABLE
  317. };
  318. static char *parse_config(json_t *config)
  319. {
  320. static char err_buf[200];
  321. json_t *val;
  322. struct opt_table *opt;
  323. for (opt = opt_config_table; opt->type != OPT_END; opt++) {
  324. char *p, *name;
  325. /* We don't handle subtables. */
  326. assert(!(opt->type & OPT_SUBTABLE));
  327. /* Pull apart the option name(s). */
  328. name = strdup(opt->names);
  329. for (p = strtok(name, "|"); p; p = strtok(NULL, "|")) {
  330. char *err;
  331. /* Ignore short options. */
  332. if (p[1] != '-')
  333. continue;
  334. val = json_object_get(config, p+2);
  335. if (!val)
  336. continue;
  337. if ((opt->type & OPT_HASARG) && json_is_string(val)) {
  338. err = opt->cb_arg(json_string_value(val),
  339. opt->u.arg);
  340. } else if ((opt->type&OPT_NOARG) && json_is_true(val)) {
  341. err = opt->cb(opt->u.arg);
  342. } else {
  343. err = "Invalid value";
  344. }
  345. if (err) {
  346. sprintf(err_buf, "Parsing JSON option %s: %s",
  347. p, err);
  348. return err_buf;
  349. }
  350. }
  351. free(name);
  352. }
  353. return NULL;
  354. }
  355. static char *load_config(const char *arg, void *unused)
  356. {
  357. json_error_t err;
  358. json_t *config;
  359. config = json_load_file(arg, &err);
  360. if (!json_is_object(config))
  361. return "JSON decode of file failed";
  362. /* Parse the config now, so we can override it. That can keep pointers
  363. * so don't free config object. */
  364. return parse_config(config);
  365. }
  366. static char *print_ndevs_and_exit(int *ndevs)
  367. {
  368. printf("%i", *ndevs);
  369. exit(*ndevs);
  370. }
  371. /* These options are available from commandline only */
  372. static struct opt_table opt_cmdline_table[] = {
  373. OPT_WITH_ARG("--config|-c",
  374. load_config, NULL, NULL,
  375. "Load a JSON-format configuration file\n"
  376. "See example-cfg.json for an example configuration."),
  377. OPT_WITHOUT_ARG("--help|-h",
  378. opt_usage_and_exit,
  379. #ifdef HAVE_OPENCL
  380. "\nBuilt with CPU and GPU mining support.\n\n",
  381. #else
  382. "\nBuilt with CPU mining support only.\n\n",
  383. #endif
  384. "Print this message"),
  385. OPT_WITHOUT_ARG("--ndevs|-n",
  386. print_ndevs_and_exit, &nDevs,
  387. "Display number of detected GPUs and exit"),
  388. OPT_ENDTABLE
  389. };
  390. static bool jobj_binary(const json_t *obj, const char *key,
  391. void *buf, size_t buflen)
  392. {
  393. const char *hexstr;
  394. json_t *tmp;
  395. tmp = json_object_get(obj, key);
  396. if (unlikely(!tmp)) {
  397. applog(LOG_ERR, "JSON key '%s' not found", key);
  398. return false;
  399. }
  400. hexstr = json_string_value(tmp);
  401. if (unlikely(!hexstr)) {
  402. applog(LOG_ERR, "JSON key '%s' is not a string", key);
  403. return false;
  404. }
  405. if (!hex2bin(buf, hexstr, buflen))
  406. return false;
  407. return true;
  408. }
  409. static bool work_decode(const json_t *val, struct work *work)
  410. {
  411. if (unlikely(!jobj_binary(val, "midstate",
  412. work->midstate, sizeof(work->midstate)))) {
  413. applog(LOG_ERR, "JSON inval midstate");
  414. goto err_out;
  415. }
  416. if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data)))) {
  417. applog(LOG_ERR, "JSON inval data");
  418. goto err_out;
  419. }
  420. if (unlikely(!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1)))) {
  421. applog(LOG_ERR, "JSON inval hash1");
  422. goto err_out;
  423. }
  424. if (unlikely(!jobj_binary(val, "target", work->target, sizeof(work->target)))) {
  425. applog(LOG_ERR, "JSON inval target");
  426. goto err_out;
  427. }
  428. memset(work->hash, 0, sizeof(work->hash));
  429. return true;
  430. err_out:
  431. return false;
  432. }
  433. static double total_secs;
  434. static char statusline[256];
  435. static inline void print_status(void)
  436. {
  437. printf("%s\r", statusline);
  438. fflush(stdout);
  439. }
  440. static bool submit_fail = false;
  441. static bool submit_upstream_work(const struct work *work)
  442. {
  443. char *hexstr = NULL;
  444. json_t *val, *res;
  445. char s[345];
  446. bool rc = false;
  447. struct cgpu_info *cgpu = thr_info[work->thr_id].cgpu;
  448. CURL *curl = curl_easy_init();
  449. double utility, efficiency;
  450. if (unlikely(!curl)) {
  451. applog(LOG_ERR, "CURL initialisation failed");
  452. return rc;
  453. }
  454. /* build hex string */
  455. hexstr = bin2hex(work->data, sizeof(work->data));
  456. if (unlikely(!hexstr)) {
  457. applog(LOG_ERR, "submit_upstream_work OOM");
  458. goto out_nofree;
  459. }
  460. /* build JSON-RPC request */
  461. sprintf(s,
  462. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  463. hexstr);
  464. if (opt_debug)
  465. applog(LOG_DEBUG, "DBG: sending RPC call: %s", s);
  466. /* issue JSON-RPC request */
  467. val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false);
  468. if (unlikely(!val)) {
  469. applog(LOG_INFO, "submit_upstream_work json_rpc_call failed");
  470. if (!submit_fail) {
  471. submit_fail = true;
  472. applog(LOG_WARNING, "Upstream communication failure, caching submissions");
  473. }
  474. goto out;
  475. } else if (submit_fail) {
  476. submit_fail = false;
  477. applog(LOG_WARNING, "Upstream communication resumed, submitting work");
  478. }
  479. res = json_object_get(val, "result");
  480. /* Theoretically threads could race when modifying accepted and
  481. * rejected values but the chance of two submits completing at the
  482. * same time is zero so there is no point adding extra locking */
  483. if (json_is_true(res)) {
  484. cgpu->accepted++;
  485. accepted++;
  486. if (opt_debug)
  487. applog(LOG_DEBUG, "PROOF OF WORK RESULT: true (yay!!!)");
  488. if (!opt_quiet)
  489. printf("[Accepted] ");
  490. } else {
  491. cgpu->rejected++;
  492. rejected++;
  493. if (opt_debug)
  494. applog(LOG_DEBUG, "PROOF OF WORK RESULT: false (booooo)");
  495. if (!opt_quiet)
  496. printf("[Rejected] ");
  497. }
  498. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  499. efficiency = getwork_requested ? cgpu->accepted * 100.0 / getwork_requested : 0.0;
  500. if (!opt_quiet) {
  501. printf("[%sPU %d] [%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m] \n",
  502. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, cgpu->total_mhashes / total_secs,
  503. getwork_requested, cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  504. efficiency, utility);
  505. print_status();
  506. }
  507. applog(LOG_INFO, "%sPU %d Requested:%d Accepted:%d Rejected:%d HW errors:%d Efficiency:%.0f%% Utility:%.2f/m",
  508. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, getwork_requested, cgpu->accepted, cgpu->rejected, cgpu->hw_errors, efficiency, utility
  509. );
  510. json_decref(val);
  511. rc = true;
  512. out:
  513. free(hexstr);
  514. out_nofree:
  515. curl_easy_cleanup(curl);
  516. return rc;
  517. }
  518. static const char *rpc_req =
  519. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  520. static bool get_upstream_work(struct work *work)
  521. {
  522. json_t *val;
  523. bool rc = false;
  524. CURL *curl = curl_easy_init();
  525. if (unlikely(!curl)) {
  526. applog(LOG_ERR, "CURL initialisation failed");
  527. return rc;
  528. }
  529. val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
  530. want_longpoll, false);
  531. if (unlikely(!val)) {
  532. applog(LOG_DEBUG, "Failed json_rpc_call in get_upstream_work");
  533. goto out;
  534. }
  535. rc = work_decode(json_object_get(val, "result"), work);
  536. json_decref(val);
  537. out:
  538. curl_easy_cleanup(curl);
  539. return rc;
  540. }
  541. static void workio_cmd_free(struct workio_cmd *wc)
  542. {
  543. if (!wc)
  544. return;
  545. switch (wc->cmd) {
  546. case WC_SUBMIT_WORK:
  547. free(wc->u.work);
  548. break;
  549. default: /* do nothing */
  550. break;
  551. }
  552. memset(wc, 0, sizeof(*wc)); /* poison */
  553. free(wc);
  554. }
  555. static void kill_work(void)
  556. {
  557. struct workio_cmd *wc;
  558. applog(LOG_INFO, "Received kill message");
  559. wc = calloc(1, sizeof(*wc));
  560. if (unlikely(!wc)) {
  561. applog(LOG_ERR, "Failed to calloc wc in kill_work");
  562. /* We're just trying to die anyway, so forget graceful */
  563. exit (1);
  564. }
  565. wc->cmd = WC_DIE;
  566. wc->thr = 0;
  567. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  568. applog(LOG_ERR, "Failed to tq_push work in kill_work");
  569. exit (1);
  570. }
  571. }
  572. static void *get_work_thread(void *userdata)
  573. {
  574. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  575. struct work *ret_work;
  576. int failures = 0;
  577. pthread_detach(pthread_self());
  578. ret_work = calloc(1, sizeof(*ret_work));
  579. if (unlikely(!ret_work)) {
  580. applog(LOG_ERR, "Failed to calloc ret_work in workio_get_work");
  581. kill_work();
  582. goto out;
  583. }
  584. /* obtain new work from bitcoin via JSON-RPC */
  585. while (!get_upstream_work(ret_work)) {
  586. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  587. applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
  588. free(ret_work);
  589. kill_work();
  590. goto out;
  591. }
  592. /* pause, then restart work-request loop */
  593. applog(LOG_DEBUG, "json_rpc_call failed on get work, retry after %d seconds",
  594. opt_fail_pause);
  595. sleep(opt_fail_pause);
  596. }
  597. /* send work to requesting thread */
  598. if (unlikely(!tq_push(thr_info[stage_thr_id].q, ret_work))) {
  599. applog(LOG_ERR, "Failed to tq_push work in workio_get_work");
  600. kill_work();
  601. free(ret_work);
  602. }
  603. out:
  604. workio_cmd_free(wc);
  605. return NULL;
  606. }
  607. static bool workio_get_work(struct workio_cmd *wc)
  608. {
  609. pthread_t get_thread;
  610. if (unlikely(pthread_create(&get_thread, NULL, get_work_thread, (void *)wc))) {
  611. applog(LOG_ERR, "Failed to create get_work_thread");
  612. return false;
  613. }
  614. return true;
  615. }
  616. static void *submit_work_thread(void *userdata)
  617. {
  618. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  619. int failures = 0;
  620. char *hexstr;
  621. pthread_detach(pthread_self());
  622. hexstr = bin2hex(wc->u.work->data, 36);
  623. if (unlikely(!hexstr)) {
  624. applog(LOG_ERR, "submit_work_thread OOM");
  625. goto out;
  626. }
  627. if (unlikely(strncmp(hexstr, current_block, 36))) {
  628. applog(LOG_WARNING, "Stale work detected, discarding");
  629. goto out_free;
  630. }
  631. /* submit solution to bitcoin via JSON-RPC */
  632. while (!submit_upstream_work(wc->u.work)) {
  633. if (unlikely(strncmp(hexstr, current_block, 36))) {
  634. applog(LOG_WARNING, "Stale work detected, discarding");
  635. goto out_free;
  636. }
  637. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  638. applog(LOG_ERR, "Failed %d retries ...terminating workio thread", opt_retries);
  639. kill_work();
  640. goto out_free;
  641. }
  642. /* pause, then restart work-request loop */
  643. applog(LOG_INFO, "json_rpc_call failed on submit_work, retry after %d seconds",
  644. opt_fail_pause);
  645. sleep(opt_fail_pause);
  646. }
  647. out_free:
  648. free(hexstr);
  649. out:
  650. workio_cmd_free(wc);
  651. return NULL;
  652. }
  653. static bool workio_submit_work(struct workio_cmd *wc)
  654. {
  655. pthread_t submit_thread;
  656. if (unlikely(pthread_create(&submit_thread, NULL, submit_work_thread, (void *)wc))) {
  657. applog(LOG_ERR, "Failed to create submit_work_thread");
  658. return false;
  659. }
  660. return true;
  661. }
  662. static void inc_staged(int inc, bool lp)
  663. {
  664. pthread_mutex_lock(&stgd_lock);
  665. total_staged += inc;
  666. if (lp)
  667. lp_staged += inc;
  668. pthread_mutex_unlock(&stgd_lock);
  669. }
  670. static void dec_staged(int inc)
  671. {
  672. pthread_mutex_lock(&stgd_lock);
  673. if (lp_staged)
  674. lp_staged -= inc;
  675. total_staged -= inc;
  676. pthread_mutex_unlock(&stgd_lock);
  677. }
  678. static int requests_staged(void)
  679. {
  680. int ret;
  681. pthread_mutex_lock(&stgd_lock);
  682. ret = total_staged;
  683. pthread_mutex_unlock(&stgd_lock);
  684. return ret;
  685. }
  686. static void *stage_thread(void *userdata)
  687. {
  688. struct thr_info *mythr = userdata;
  689. bool ok = true;
  690. unsigned int i;
  691. for (i = 0; i < 36; i++) {
  692. strcat(current_block, "0");
  693. strcat(blank, "0");
  694. }
  695. while (ok) {
  696. struct work *work = NULL;
  697. char *hexstr;
  698. work = tq_pop(mythr->q, NULL);
  699. if (unlikely(!work)) {
  700. applog(LOG_ERR, "Failed to tq_pop in stage_thread");
  701. ok = false;
  702. break;
  703. }
  704. hexstr = bin2hex(work->data, 36);
  705. if (unlikely(!hexstr)) {
  706. applog(LOG_ERR, "stage_thread OOM");
  707. break;
  708. }
  709. /* current_block is blanked out on successful longpoll */
  710. if (likely(strncmp(current_block, blank, 36))) {
  711. if (unlikely(strncmp(hexstr, current_block, 36))) {
  712. if (want_longpoll)
  713. applog(LOG_WARNING, "New block detected, possible missed longpoll, flushing work queue");
  714. else
  715. applog(LOG_WARNING, "New block detected, flushing work queue ");
  716. /* As we can't flush the work from here, signal
  717. * the wakeup thread to restart all the
  718. * threads */
  719. work_restart[stage_thr_id].restart = 1;
  720. }
  721. } else
  722. memcpy(longpoll_block, hexstr, 36);
  723. memcpy(current_block, hexstr, 36);
  724. free(hexstr);
  725. if (unlikely(!tq_push(thr_info[0].q, work))) {
  726. applog(LOG_ERR, "Failed to tq_push work in stage_thread");
  727. ok = false;
  728. break;
  729. }
  730. inc_staged(1, false);
  731. }
  732. tq_freeze(mythr->q);
  733. return NULL;
  734. }
  735. static void *workio_thread(void *userdata)
  736. {
  737. struct thr_info *mythr = userdata;
  738. bool ok = true;
  739. while (ok) {
  740. struct workio_cmd *wc;
  741. /* wait for workio_cmd sent to us, on our queue */
  742. wc = tq_pop(mythr->q, NULL);
  743. if (unlikely(!wc)) {
  744. applog(LOG_ERR, "Failed to tq_pop in workio_thread");
  745. ok = false;
  746. break;
  747. }
  748. /* process workio_cmd */
  749. switch (wc->cmd) {
  750. case WC_GET_WORK:
  751. ok = workio_get_work(wc);
  752. break;
  753. case WC_SUBMIT_WORK:
  754. ok = workio_submit_work(wc);
  755. break;
  756. case WC_DIE:
  757. default:
  758. ok = false;
  759. break;
  760. }
  761. }
  762. tq_freeze(mythr->q);
  763. return NULL;
  764. }
  765. static void hashmeter(int thr_id, struct timeval *diff,
  766. unsigned long hashes_done)
  767. {
  768. struct timeval temp_tv_end, total_diff;
  769. double khashes, secs;
  770. double local_secs;
  771. double utility, efficiency = 0.0;
  772. static double local_mhashes_done = 0;
  773. static double rolling_local = 0;
  774. double local_mhashes = (double)hashes_done / 1000000.0;
  775. /* Don't bother calculating anything if we're not displaying it */
  776. if (opt_quiet || !opt_log_interval)
  777. return;
  778. khashes = hashes_done / 1000.0;
  779. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  780. if (thr_id >= 0) {
  781. /* So we can call hashmeter from a non worker thread */
  782. struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
  783. if (opt_debug)
  784. applog(LOG_DEBUG, "[thread %d: %lu hashes, %.0f khash/sec]",
  785. thr_id, hashes_done, hashes_done / secs);
  786. cgpu->local_mhashes += local_mhashes;
  787. cgpu->total_mhashes += local_mhashes;
  788. }
  789. /* Totals are updated by all threads so can race without locking */
  790. pthread_mutex_lock(&hash_lock);
  791. gettimeofday(&temp_tv_end, NULL);
  792. timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end);
  793. local_secs = (double)total_diff.tv_sec + ((double)total_diff.tv_usec / 1000000.0);
  794. total_mhashes_done += local_mhashes;
  795. local_mhashes_done += local_mhashes;
  796. if (total_diff.tv_sec < opt_log_interval)
  797. /* Only update the total every opt_log_interval seconds */
  798. goto out_unlock;
  799. gettimeofday(&total_tv_end, NULL);
  800. /* Use a rolling average by faking an exponential decay over 5 * log */
  801. rolling_local = ((rolling_local * 0.9) + local_mhashes_done) / 1.9;
  802. timeval_subtract(&total_diff, &total_tv_end, &total_tv_start);
  803. total_secs = (double)total_diff.tv_sec +
  804. ((double)total_diff.tv_usec / 1000000.0);
  805. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  806. efficiency = getwork_requested ? accepted * 100.0 / getwork_requested : 0.0;
  807. sprintf(statusline, "[(%ds):%.1f (avg):%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m] ",
  808. opt_log_interval, rolling_local / local_secs, total_mhashes_done / total_secs,
  809. getwork_requested, accepted, rejected, hw_errors, efficiency, utility);
  810. print_status();
  811. applog(LOG_INFO, "[Rate (%ds):%.1f (avg):%.2f Mhash/s] [Requested:%d Accepted:%d Rejected:%d HW errors:%d Efficiency:%.0f%% Utility:%.2f/m]",
  812. opt_log_interval, rolling_local / local_secs, total_mhashes_done / total_secs,
  813. getwork_requested, accepted, rejected, hw_errors, efficiency, utility);
  814. local_mhashes_done = 0;
  815. out_unlock:
  816. pthread_mutex_unlock(&hash_lock);
  817. }
  818. /* This is overkill, but at least we'll know accurately how much work is
  819. * queued to prevent ever being left without work */
  820. static void inc_queued(void)
  821. {
  822. pthread_mutex_lock(&qd_lock);
  823. total_queued++;
  824. pthread_mutex_unlock(&qd_lock);
  825. }
  826. static void dec_queued(void)
  827. {
  828. pthread_mutex_lock(&qd_lock);
  829. total_queued--;
  830. pthread_mutex_unlock(&qd_lock);
  831. dec_staged(1);
  832. }
  833. static int requests_queued(void)
  834. {
  835. int ret;
  836. pthread_mutex_lock(&qd_lock);
  837. ret = total_queued;
  838. pthread_mutex_unlock(&qd_lock);
  839. return ret;
  840. }
  841. /* All work is queued flagged as being for thread 0 and then the mining thread
  842. * flags it as its own */
  843. static bool queue_request(void)
  844. {
  845. struct thr_info *thr = &thr_info[0];
  846. struct workio_cmd *wc;
  847. /* fill out work request message */
  848. wc = calloc(1, sizeof(*wc));
  849. if (unlikely(!wc)) {
  850. applog(LOG_ERR, "Failed to tq_pop in queue_request");
  851. return false;
  852. }
  853. wc->cmd = WC_GET_WORK;
  854. wc->thr = thr;
  855. /* send work request to workio thread */
  856. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  857. applog(LOG_ERR, "Failed to tq_push in queue_request");
  858. workio_cmd_free(wc);
  859. return false;
  860. }
  861. inc_queued();
  862. return true;
  863. }
  864. static bool discard_request(void)
  865. {
  866. struct thr_info *thr = &thr_info[0];
  867. struct work *work_heap;
  868. /* Just in case we fell in a hole and missed a queue filling */
  869. if (unlikely(!requests_queued())) {
  870. applog(LOG_WARNING, "Tried to discard_request with nil queued");
  871. return true;
  872. }
  873. work_heap = tq_pop(thr->q, NULL);
  874. if (unlikely(!work_heap)) {
  875. applog(LOG_ERR, "Failed to tq_pop in discard_request");
  876. return false;
  877. }
  878. free(work_heap);
  879. dec_queued();
  880. return true;
  881. }
  882. static void flush_requests(bool longpoll)
  883. {
  884. int i, extra;
  885. extra = requests_queued();
  886. /* When flushing from longpoll, we don't know the new work yet. When
  887. * not flushing from longpoll, the first work item is valid so do not
  888. * discard it */
  889. if (longpoll)
  890. memcpy(current_block, blank, 36);
  891. else
  892. extra--;
  893. /* Temporarily increase the staged count so that get_work thinks there
  894. * is work available instead of making threads reuse existing work */
  895. inc_staged(extra, true);
  896. for (i = 0; i < extra; i++) {
  897. /* Queue a whole batch of new requests */
  898. if (unlikely(!queue_request())) {
  899. applog(LOG_ERR, "Failed to queue requests in flush_requests");
  900. kill_work();
  901. break;
  902. }
  903. /* Pop off the old requests. Cancelling the requests would be better
  904. * but is tricky */
  905. if (unlikely(!discard_request())) {
  906. applog(LOG_ERR, "Failed to discard requests in flush_requests");
  907. kill_work();
  908. break;
  909. }
  910. }
  911. }
  912. static bool get_work(struct work *work, bool queued)
  913. {
  914. struct thr_info *thr = &thr_info[0];
  915. struct work *work_heap;
  916. bool ret = false;
  917. int failures = 0;
  918. retry:
  919. if (unlikely(!queued && !queue_request())) {
  920. applog(LOG_WARNING, "Failed to queue_request in get_work");
  921. goto out;
  922. }
  923. if (!requests_staged()) {
  924. uint32_t *work_ntime;
  925. uint32_t ntime;
  926. /* Only print this message once each time we shift to localgen */
  927. if (!localgen)
  928. applog(LOG_WARNING, "Server not providing work fast enough, generating work locally");
  929. localgen = true;
  930. work_ntime = (uint32_t *)(work->data + 68);
  931. ntime = be32toh(*work_ntime);
  932. ntime++;
  933. *work_ntime = htobe32(ntime);
  934. ret = true;
  935. goto out;
  936. } else if (localgen) {
  937. localgen = false;
  938. applog(LOG_WARNING, "Resumed retrieving work from server");
  939. }
  940. /* wait for 1st response, or get cached response */
  941. work_heap = tq_pop(thr->q, NULL);
  942. if (unlikely(!work_heap)) {
  943. applog(LOG_WARNING, "Failed to tq_pop in get_work");
  944. goto out;
  945. }
  946. dec_queued();
  947. memcpy(work, work_heap, sizeof(*work));
  948. ret = true;
  949. free(work_heap);
  950. out:
  951. if (unlikely(ret == false)) {
  952. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  953. applog(LOG_ERR, "Failed %d times to get_work");
  954. return ret;
  955. }
  956. applog(LOG_DEBUG, "Retrying after %d seconds", opt_fail_pause);
  957. sleep(opt_fail_pause);
  958. goto retry;
  959. }
  960. return ret;
  961. }
  962. static bool submit_work_sync(struct thr_info *thr, const struct work *work_in)
  963. {
  964. struct workio_cmd *wc;
  965. /* fill out work request message */
  966. wc = calloc(1, sizeof(*wc));
  967. if (unlikely(!wc)) {
  968. applog(LOG_ERR, "Failed to calloc wc in submit_work_sync");
  969. return false;
  970. }
  971. wc->u.work = malloc(sizeof(*work_in));
  972. if (unlikely(!wc->u.work)) {
  973. applog(LOG_ERR, "Failed to calloc work in submit_work_sync");
  974. goto err_out;
  975. }
  976. wc->cmd = WC_SUBMIT_WORK;
  977. wc->thr = thr;
  978. memcpy(wc->u.work, work_in, sizeof(*work_in));
  979. /* send solution to workio thread */
  980. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  981. applog(LOG_ERR, "Failed to tq_push work in submit_work_sync");
  982. goto err_out;
  983. }
  984. return true;
  985. err_out:
  986. workio_cmd_free(wc);
  987. return false;
  988. }
  989. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
  990. {
  991. work->data[64+12+0] = (nonce>>0) & 0xff;
  992. work->data[64+12+1] = (nonce>>8) & 0xff;
  993. work->data[64+12+2] = (nonce>>16) & 0xff;
  994. work->data[64+12+3] = (nonce>>24) & 0xff;
  995. return submit_work_sync(thr, work);
  996. }
  997. static inline int cpu_from_thr_id(int thr_id)
  998. {
  999. return (thr_id - gpu_threads) % num_processors;
  1000. }
  1001. static void *miner_thread(void *userdata)
  1002. {
  1003. struct thr_info *mythr = userdata;
  1004. const int thr_id = mythr->id;
  1005. uint32_t max_nonce = 0xffffff;
  1006. bool needs_work = true;
  1007. /* Try to cycle approximately 5 times before each log update */
  1008. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1009. /* Request the next work item at 2/3 of the scantime */
  1010. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1011. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1012. bool requested = true;
  1013. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  1014. * and if that fails, then SCHED_BATCH. No need for this to be an
  1015. * error if it fails */
  1016. setpriority(PRIO_PROCESS, 0, 19);
  1017. drop_policy();
  1018. /* Cpu affinity only makes sense if the number of threads is a multiple
  1019. * of the number of CPUs */
  1020. if (!(opt_n_threads % num_processors))
  1021. affine_to_cpu(thr_id - gpu_threads, cpu_from_thr_id(thr_id));
  1022. while (1) {
  1023. struct work work __attribute__((aligned(128)));
  1024. unsigned long hashes_done;
  1025. struct timeval tv_workstart, tv_start, tv_end, diff;
  1026. uint64_t max64;
  1027. bool rc;
  1028. if (needs_work) {
  1029. gettimeofday(&tv_workstart, NULL);
  1030. /* obtain new work from internal workio thread */
  1031. if (unlikely(!get_work(&work, requested))) {
  1032. applog(LOG_ERR, "work retrieval failed, exiting "
  1033. "mining thread %d", mythr->id);
  1034. goto out;
  1035. }
  1036. work.thr_id = thr_id;
  1037. needs_work = requested = false;
  1038. work.blk.nonce = 0;
  1039. }
  1040. hashes_done = 0;
  1041. gettimeofday(&tv_start, NULL);
  1042. /* scan nonces for a proof-of-work hash */
  1043. switch (opt_algo) {
  1044. case ALGO_C:
  1045. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  1046. work.hash1, work.hash, work.target,
  1047. max_nonce, &hashes_done,
  1048. work.blk.nonce);
  1049. break;
  1050. #ifdef WANT_X8664_SSE2
  1051. case ALGO_SSE2_64: {
  1052. unsigned int rc5 =
  1053. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  1054. work.hash1, work.hash,
  1055. work.target,
  1056. max_nonce, &hashes_done,
  1057. work.blk.nonce);
  1058. rc = (rc5 == -1) ? false : true;
  1059. }
  1060. break;
  1061. #endif
  1062. #ifdef WANT_SSE2_4WAY
  1063. case ALGO_4WAY: {
  1064. unsigned int rc4 =
  1065. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  1066. work.hash1, work.hash,
  1067. work.target,
  1068. max_nonce, &hashes_done,
  1069. work.blk.nonce);
  1070. rc = (rc4 == -1) ? false : true;
  1071. }
  1072. break;
  1073. #endif
  1074. #ifdef WANT_VIA_PADLOCK
  1075. case ALGO_VIA:
  1076. rc = scanhash_via(thr_id, work.data, work.target,
  1077. max_nonce, &hashes_done,
  1078. work.blk.nonce);
  1079. break;
  1080. #endif
  1081. case ALGO_CRYPTOPP:
  1082. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  1083. work.hash1, work.hash, work.target,
  1084. max_nonce, &hashes_done,
  1085. work.blk.nonce);
  1086. break;
  1087. #ifdef WANT_CRYPTOPP_ASM32
  1088. case ALGO_CRYPTOPP_ASM32:
  1089. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  1090. work.hash1, work.hash, work.target,
  1091. max_nonce, &hashes_done,
  1092. work.blk.nonce);
  1093. break;
  1094. #endif
  1095. default:
  1096. /* should never happen */
  1097. goto out;
  1098. }
  1099. /* record scanhash elapsed time */
  1100. gettimeofday(&tv_end, NULL);
  1101. timeval_subtract(&diff, &tv_end, &tv_start);
  1102. hashes_done -= work.blk.nonce;
  1103. hashmeter(thr_id, &diff, hashes_done);
  1104. work.blk.nonce += hashes_done;
  1105. /* adjust max_nonce to meet target cycle time */
  1106. if (diff.tv_usec > 500000)
  1107. diff.tv_sec++;
  1108. if (diff.tv_sec && diff.tv_sec != cycle) {
  1109. max64 = work.blk.nonce +
  1110. ((uint64_t)hashes_done * cycle) / diff.tv_sec;
  1111. } else
  1112. max64 = work.blk.nonce + hashes_done;
  1113. if (max64 > 0xfffffffaULL)
  1114. max64 = 0xfffffffaULL;
  1115. max_nonce = max64;
  1116. /* if nonce found, submit work */
  1117. if (unlikely(rc)) {
  1118. if (opt_debug)
  1119. applog(LOG_DEBUG, "CPU %d found something?", cpu_from_thr_id(thr_id));
  1120. if (unlikely(!submit_work_sync(mythr, &work))) {
  1121. applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
  1122. break;
  1123. }
  1124. work.blk.nonce += 4;
  1125. }
  1126. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1127. if (!requested && (diff.tv_sec > request_interval || work.blk.nonce > request_nonce)) {
  1128. if (unlikely(!queue_request())) {
  1129. applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
  1130. goto out;
  1131. }
  1132. requested = true;
  1133. }
  1134. if (diff.tv_sec > opt_scantime || work_restart[thr_id].restart ||
  1135. work.blk.nonce >= MAXTHREADS - hashes_done)
  1136. needs_work = true;
  1137. }
  1138. out:
  1139. tq_freeze(mythr->q);
  1140. return NULL;
  1141. }
  1142. enum {
  1143. STAT_SLEEP_INTERVAL = 1,
  1144. STAT_CTR_INTERVAL = 10000000,
  1145. FAILURE_INTERVAL = 30,
  1146. };
  1147. #ifdef HAVE_OPENCL
  1148. static _clState *clStates[16];
  1149. static inline cl_int queue_kernel_parameters(_clState *clState, dev_blk_ctx *blk)
  1150. {
  1151. cl_kernel *kernel = &clState->kernel;
  1152. cl_int status = 0;
  1153. int num = 0;
  1154. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  1155. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  1156. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  1157. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  1158. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  1159. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  1160. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  1161. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  1162. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  1163. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  1164. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  1165. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  1166. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  1167. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  1168. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  1169. if (clState->hasBitAlign == true) {
  1170. /* Parameters for phatk kernel */
  1171. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W2);
  1172. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W16);
  1173. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W17);
  1174. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreVal4);
  1175. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->T1);
  1176. } else {
  1177. /* Parameters for poclbm kernel */
  1178. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  1179. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  1180. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  1181. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  1182. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  1183. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  1184. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  1185. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  1186. }
  1187. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  1188. (void *)&clState->outputBuffer);
  1189. return status;
  1190. }
  1191. static inline int gpu_from_thr_id(int thr_id)
  1192. {
  1193. return thr_id % nDevs;
  1194. }
  1195. static void *gpuminer_thread(void *userdata)
  1196. {
  1197. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1198. struct timeval tv_start, tv_end, diff;
  1199. struct thr_info *mythr = userdata;
  1200. const int thr_id = mythr->id;
  1201. uint32_t *res, *blank_res;
  1202. size_t globalThreads[1];
  1203. size_t localThreads[1];
  1204. cl_int status;
  1205. _clState *clState = clStates[thr_id];
  1206. const cl_kernel *kernel = &clState->kernel;
  1207. struct work *work = malloc(sizeof(struct work));
  1208. unsigned const int threads = 1 << (15 + scan_intensity);
  1209. unsigned const int vectors = clState->preferred_vwidth;
  1210. unsigned const int hashes = threads * vectors;
  1211. unsigned int hashes_done = 0;
  1212. /* Request the next work item at 2/3 of the scantime */
  1213. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1214. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1215. bool requested = true;
  1216. res = calloc(BUFFERSIZE, 1);
  1217. blank_res = calloc(BUFFERSIZE, 1);
  1218. if (!res || !blank_res) {
  1219. applog(LOG_ERR, "Failed to calloc in gpuminer_thread");
  1220. goto out;
  1221. }
  1222. gettimeofday(&tv_start, NULL);
  1223. globalThreads[0] = threads;
  1224. localThreads[0] = clState->work_size;
  1225. diff.tv_sec = ~0UL;
  1226. gettimeofday(&tv_end, NULL);
  1227. while (1) {
  1228. struct timeval tv_workstart;
  1229. /* This finish flushes the readbuffer set with CL_FALSE later */
  1230. clFinish(clState->commandQueue);
  1231. if (diff.tv_sec > opt_scantime || work->blk.nonce >= MAXTHREADS - hashes || work_restart[thr_id].restart) {
  1232. /* Ignore any reads since we're getting new work and queue a clean buffer */
  1233. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1234. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1235. if (unlikely(status != CL_SUCCESS))
  1236. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1237. memset(res, 0, BUFFERSIZE);
  1238. gettimeofday(&tv_workstart, NULL);
  1239. /* obtain new work from internal workio thread */
  1240. if (unlikely(!get_work(work, requested))) {
  1241. applog(LOG_ERR, "work retrieval failed, exiting "
  1242. "gpu mining thread %d", mythr->id);
  1243. goto out;
  1244. }
  1245. work->thr_id = thr_id;
  1246. requested = false;
  1247. getwork_requested++;
  1248. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1249. work->blk.nonce = 0;
  1250. work_restart[thr_id].restart = 0;
  1251. if (opt_debug)
  1252. applog(LOG_DEBUG, "getwork thread %d", thr_id);
  1253. /* Flushes the writebuffer set with CL_FALSE above */
  1254. clFinish(clState->commandQueue);
  1255. }
  1256. status = queue_kernel_parameters(clState, &work->blk);
  1257. if (unlikely(status != CL_SUCCESS))
  1258. { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
  1259. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1260. if (res[MAXBUFFERS]) {
  1261. /* Clear the buffer again */
  1262. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1263. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1264. if (unlikely(status != CL_SUCCESS))
  1265. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1266. if (opt_debug)
  1267. applog(LOG_DEBUG, "GPU %d found something?", gpu_from_thr_id(thr_id));
  1268. postcalc_hash_async(mythr, work, res);
  1269. memset(res, 0, BUFFERSIZE);
  1270. clFinish(clState->commandQueue);
  1271. }
  1272. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1273. globalThreads, localThreads, 0, NULL, NULL);
  1274. if (unlikely(status != CL_SUCCESS))
  1275. { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
  1276. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1277. BUFFERSIZE, res, 0, NULL, NULL);
  1278. if (unlikely(status != CL_SUCCESS))
  1279. { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
  1280. gettimeofday(&tv_end, NULL);
  1281. timeval_subtract(&diff, &tv_end, &tv_start);
  1282. hashes_done += hashes;
  1283. work->blk.nonce += hashes;
  1284. if (diff.tv_usec > 500000)
  1285. diff.tv_sec++;
  1286. if (diff.tv_sec >= cycle) {
  1287. hashmeter(thr_id, &diff, hashes_done);
  1288. gettimeofday(&tv_start, NULL);
  1289. hashes_done = 0;
  1290. }
  1291. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1292. if (!requested && (diff.tv_sec > request_interval || work->blk.nonce > request_nonce)) {
  1293. if (unlikely(!queue_request())) {
  1294. applog(LOG_ERR, "Failed to queue_request in gpuminer_thread %d", thr_id);
  1295. goto out;
  1296. }
  1297. requested = true;
  1298. }
  1299. }
  1300. out:
  1301. tq_freeze(mythr->q);
  1302. return NULL;
  1303. }
  1304. #endif /* HAVE_OPENCL */
  1305. static void restart_threads(bool longpoll)
  1306. {
  1307. int i;
  1308. /* Discard old queued requests and get new ones */
  1309. flush_requests(longpoll);
  1310. for (i = 0; i < opt_n_threads + gpu_threads; i++)
  1311. work_restart[i].restart = 1;
  1312. }
  1313. static void *longpoll_thread(void *userdata)
  1314. {
  1315. struct thr_info *mythr = userdata;
  1316. CURL *curl = NULL;
  1317. char *copy_start, *hdr_path, *lp_url = NULL;
  1318. bool need_slash = false;
  1319. int failures = 0;
  1320. unsigned int i;
  1321. hdr_path = tq_pop(mythr->q, NULL);
  1322. if (!hdr_path)
  1323. goto out;
  1324. /* full URL */
  1325. if (strstr(hdr_path, "://")) {
  1326. lp_url = hdr_path;
  1327. hdr_path = NULL;
  1328. }
  1329. /* absolute path, on current server */
  1330. else {
  1331. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  1332. if (rpc_url[strlen(rpc_url) - 1] != '/')
  1333. need_slash = true;
  1334. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  1335. if (!lp_url)
  1336. goto out;
  1337. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  1338. }
  1339. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  1340. curl = curl_easy_init();
  1341. if (unlikely(!curl)) {
  1342. applog(LOG_ERR, "CURL initialisation failed");
  1343. goto out;
  1344. }
  1345. for (i = 0; i < 36; i++)
  1346. strcat(longpoll_block, "0");
  1347. while (1) {
  1348. json_t *val;
  1349. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  1350. false, true);
  1351. if (likely(val)) {
  1352. failures = 0;
  1353. json_decref(val);
  1354. /* Keep track of who ordered a restart_threads to make
  1355. * sure it's only done once per new block */
  1356. if (likely(!strncmp(longpoll_block, blank, 36) ||
  1357. !strncmp(longpoll_block, current_block, 36))) {
  1358. applog(LOG_WARNING, "LONGPOLL detected new block, flushing work queue ");
  1359. restart_threads(true);
  1360. } else
  1361. applog(LOG_WARNING, "LONGPOLL received - new block detected and work flushed already ");
  1362. } else {
  1363. if (failures++ < 10) {
  1364. sleep(30);
  1365. applog(LOG_WARNING,
  1366. "longpoll failed, sleeping for 30s");
  1367. } else {
  1368. applog(LOG_ERR,
  1369. "longpoll failed, ending thread");
  1370. goto out;
  1371. }
  1372. }
  1373. memcpy(longpoll_block, current_block, 36);
  1374. }
  1375. out:
  1376. free(hdr_path);
  1377. free(lp_url);
  1378. tq_freeze(mythr->q);
  1379. if (curl)
  1380. curl_easy_cleanup(curl);
  1381. return NULL;
  1382. }
  1383. /* Makes sure the hashmeter keeps going even if mining threads stall */
  1384. static void *wakeup_thread(void *userdata)
  1385. {
  1386. const unsigned int interval = opt_log_interval / 2 ? : 1;
  1387. struct timeval zero_tv;
  1388. memset(&zero_tv, 0, sizeof(struct timeval));
  1389. while (1) {
  1390. sleep(interval);
  1391. if (requests_queued() < opt_queue)
  1392. queue_request();
  1393. hashmeter(-1, &zero_tv, 0);
  1394. if (unlikely(work_restart[stage_thr_id].restart)) {
  1395. restart_threads(false);
  1396. work_restart[stage_thr_id].restart = 0;
  1397. }
  1398. }
  1399. return NULL;
  1400. }
  1401. int main (int argc, char *argv[])
  1402. {
  1403. struct thr_info *thr;
  1404. unsigned int i;
  1405. char name[32];
  1406. struct cgpu_info *gpus = NULL, *cpus = NULL;
  1407. if (unlikely(pthread_mutex_init(&time_lock, NULL)))
  1408. return 1;
  1409. if (unlikely(pthread_mutex_init(&hash_lock, NULL)))
  1410. return 1;
  1411. if (unlikely(pthread_mutex_init(&qd_lock, NULL)))
  1412. return 1;
  1413. if (unlikely(pthread_mutex_init(&stgd_lock, NULL)))
  1414. return 1;
  1415. #ifdef WIN32
  1416. opt_n_threads = num_processors = 1;
  1417. #else
  1418. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  1419. opt_n_threads = num_processors;
  1420. #endif /* !WIN32 */
  1421. #ifdef HAVE_OPENCL
  1422. nDevs = clDevicesNum();
  1423. if (nDevs < 0)
  1424. return 1;
  1425. #endif
  1426. if (nDevs)
  1427. opt_n_threads = 0;
  1428. rpc_url = strdup(DEF_RPC_URL);
  1429. /* parse command line */
  1430. opt_register_table(opt_config_table,
  1431. "Options for both config file and command line");
  1432. opt_register_table(opt_cmdline_table,
  1433. "Options for command line only");
  1434. opt_parse(&argc, argv, applog_and_exit);
  1435. if (argc != 1) {
  1436. applog(LOG_ERR, "Unexpected extra commandline arguments");
  1437. return 1;
  1438. }
  1439. gpu_threads = nDevs * opt_g_threads;
  1440. if (!gpu_threads && !forced_n_threads) {
  1441. /* Maybe they turned GPU off; restore default CPU threads. */
  1442. opt_n_threads = num_processors;
  1443. }
  1444. if (!rpc_userpass) {
  1445. if (!rpc_user || !rpc_pass) {
  1446. applog(LOG_ERR, "No login credentials supplied");
  1447. return 1;
  1448. }
  1449. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  1450. if (!rpc_userpass)
  1451. return 1;
  1452. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  1453. }
  1454. if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
  1455. return 1;
  1456. #ifdef HAVE_SYSLOG_H
  1457. if (use_syslog)
  1458. openlog("cpuminer", LOG_PID, LOG_USER);
  1459. #endif
  1460. work_restart = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*work_restart));
  1461. if (!work_restart)
  1462. return 1;
  1463. thr_info = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*thr));
  1464. if (!thr_info)
  1465. return 1;
  1466. /* init workio thread info */
  1467. work_thr_id = opt_n_threads + gpu_threads;
  1468. thr = &thr_info[work_thr_id];
  1469. thr->id = work_thr_id;
  1470. thr->q = tq_new();
  1471. if (!thr->q)
  1472. return 1;
  1473. /* start work I/O thread */
  1474. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  1475. applog(LOG_ERR, "workio thread create failed");
  1476. return 1;
  1477. }
  1478. /* init longpoll thread info */
  1479. if (want_longpoll) {
  1480. longpoll_thr_id = opt_n_threads + gpu_threads + 1;
  1481. thr = &thr_info[longpoll_thr_id];
  1482. thr->id = longpoll_thr_id;
  1483. thr->q = tq_new();
  1484. if (!thr->q)
  1485. return 1;
  1486. /* start longpoll thread */
  1487. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  1488. applog(LOG_ERR, "longpoll thread create failed");
  1489. return 1;
  1490. }
  1491. pthread_detach(thr->pth);
  1492. } else
  1493. longpoll_thr_id = -1;
  1494. gettimeofday(&total_tv_start, NULL);
  1495. gettimeofday(&total_tv_end, NULL);
  1496. if (opt_n_threads ) {
  1497. cpus = calloc(num_processors, sizeof(struct cgpu_info));
  1498. if (unlikely(!cpus)) {
  1499. applog(LOG_ERR, "Failed to calloc cpus");
  1500. return 1;
  1501. }
  1502. }
  1503. if (gpu_threads) {
  1504. gpus = calloc(nDevs, sizeof(struct cgpu_info));
  1505. if (unlikely(!gpus)) {
  1506. applog(LOG_ERR, "Failed to calloc gpus");
  1507. return 1;
  1508. }
  1509. }
  1510. stage_thr_id = opt_n_threads + gpu_threads + 3;
  1511. thr = &thr_info[stage_thr_id];
  1512. thr->q = tq_new();
  1513. if (!thr->q)
  1514. return 1;
  1515. /* start stage thread */
  1516. if (pthread_create(&thr->pth, NULL, stage_thread, thr)) {
  1517. applog(LOG_ERR, "stage thread create failed");
  1518. return 1;
  1519. }
  1520. /* Put enough work in the queue */
  1521. for (i = 0; i < opt_queue + opt_n_threads + gpu_threads; i++) {
  1522. if (unlikely(!queue_request())) {
  1523. applog(LOG_ERR, "Failed to queue_request in main");
  1524. return 1;
  1525. }
  1526. }
  1527. #ifdef HAVE_OPENCL
  1528. /* start GPU mining threads */
  1529. for (i = 0; i < gpu_threads; i++) {
  1530. int gpu = gpu_from_thr_id(i);
  1531. thr = &thr_info[i];
  1532. thr->id = i;
  1533. gpus[gpu].is_gpu = 1;
  1534. gpus[gpu].cpu_gpu = gpu;
  1535. thr->cgpu = &gpus[gpu];
  1536. thr->q = tq_new();
  1537. if (!thr->q) {
  1538. applog(LOG_ERR, "tq_new failed in starting gpu mining threads");
  1539. return 1;
  1540. }
  1541. applog(LOG_INFO, "Init GPU thread %i", i);
  1542. clStates[i] = initCl(gpu, name, sizeof(name));
  1543. if (!clStates[i]) {
  1544. applog(LOG_ERR, "Failed to init GPU thread %d", i);
  1545. continue;
  1546. }
  1547. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1548. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  1549. applog(LOG_ERR, "thread %d create failed", i);
  1550. return 1;
  1551. }
  1552. pthread_detach(thr->pth);
  1553. }
  1554. applog(LOG_INFO, "%d gpu miner threads started", i);
  1555. #endif
  1556. /* start CPU mining threads */
  1557. for (i = gpu_threads; i < gpu_threads + opt_n_threads; i++) {
  1558. int cpu = cpu_from_thr_id(i);
  1559. thr = &thr_info[i];
  1560. thr->id = i;
  1561. cpus[cpu].cpu_gpu = cpu;
  1562. thr->cgpu = &cpus[cpu];
  1563. thr->q = tq_new();
  1564. if (!thr->q) {
  1565. applog(LOG_ERR, "tq_new failed in starting cpu mining threads");
  1566. return 1;
  1567. }
  1568. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  1569. applog(LOG_ERR, "thread %d create failed", i);
  1570. return 1;
  1571. }
  1572. pthread_detach(thr->pth);
  1573. }
  1574. applog(LOG_INFO, "%d cpu miner threads started, "
  1575. "using SHA256 '%s' algorithm.",
  1576. opt_n_threads,
  1577. algo_names[opt_algo]);
  1578. thr = &thr_info[opt_n_threads + gpu_threads + 2];
  1579. /* start wakeup thread */
  1580. if (pthread_create(&thr->pth, NULL, wakeup_thread, NULL)) {
  1581. applog(LOG_ERR, "wakeup thread create failed");
  1582. return 1;
  1583. }
  1584. /* Restart count as it will be wrong till all threads are started */
  1585. pthread_mutex_lock(&hash_lock);
  1586. gettimeofday(&total_tv_start, NULL);
  1587. gettimeofday(&total_tv_end, NULL);
  1588. total_mhashes_done = 0;
  1589. pthread_mutex_unlock(&hash_lock);
  1590. /* main loop - simply wait for workio thread to exit */
  1591. pthread_join(thr_info[work_thr_id].pth, NULL);
  1592. curl_global_cleanup();
  1593. if (gpu_threads)
  1594. free(gpus);
  1595. if (opt_n_threads)
  1596. free(cpus);
  1597. applog(LOG_INFO, "workio thread dead, exiting.");
  1598. return 0;
  1599. }